/
rw.store.record.js
319 lines (266 loc) 路 8.66 KB
/
rw.store.record.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
'use strict';
( function ( mw, rw ) {
/**
* @class StateStore
* @constructor
*/
var RecordStore = function () {
this.data = {
metadata: {
language: rw.store.config.data.savedLanguage,
license: rw.store.config.data.savedLicense,
media: rw.store.config.data.languages[ rw.store.config.data.savedLanguage ].mediaType === rw.store.config.data.items.mediaTypeVideo ? 'video' : 'audio',
speaker: {
gender: '',
languages: {},
location: '',
name: '',
qid: '',
main: false,
new: false
}
},
words: [],
records: {},
status: {},
errors: {},
checkboxes: {},
statusCount: {
up: 0,
ready: 0,
stashing: 0,
stashed: 0,
uploading: 0,
uploaded: 0,
finalizing: 0,
done: 0,
error: 0
}
};
this.$api = new mw.Api();
};
RecordStore.prototype.setSpeaker = function ( speaker ) {
this.data.metadata.speaker.gender = speaker.gender;
Vue.set( this.data.metadata.speaker, 'languages', speaker.languages );
this.data.metadata.speaker.location = speaker.location;
this.data.metadata.speaker.name = speaker.name;
this.data.metadata.speaker.qid = speaker.qid;
this.data.metadata.speaker.main = speaker.main || false;
this.data.metadata.speaker.new = speaker.new || speaker.qid === null;
// if there are already some records done, remove them when changing speaker
this.clearAllRecords();
};
RecordStore.prototype.setStatus = function ( word, status ) {
// Set the status and update the counter accordingly
if ( this.data.status[ word ] === undefined ) {
Vue.set( this.data.status, word, status );
} else {
this.data.statusCount[ this.data.status[ word ] ]--;
this.data.status[ word ] = status;
}
this.data.statusCount[ status ]++;
};
RecordStore.prototype.setError = function ( word, error ) {
// Set the error and update the counter accordingly
if ( this.data.errors[ word ] === undefined ) {
Vue.set( this.data.errors, word, error );
} else {
if ( this.data.errors[ word ] !== false ) {
this.data.statusCount.error--;
}
this.data.errors[ word ] = error;
}
if ( error !== false ) {
this.data.statusCount.error++;
}
};
RecordStore.prototype.countStatus = function ( status ) {
var i,
counter = 0;
// Usefull? What about a direct read?
if ( Array.isArray( status ) === false ) {
return this.data.statusCount[ status ];
}
for ( i = 0; i < status.length; i++ ) {
counter += this.data.statusCount[ status[ i ] ];
}
return counter;
};
RecordStore.prototype.clearRecord = function ( word ) {
var i = this.data.words.indexOf( word );
// Check if the word is in our list
if ( i === -1 ) {
return false;
}
// Update global counters
this.data.statusCount[ this.data.status[ word ] ]--;
if ( this.data.errors[ word ] !== false ) {
this.data.statusCount.error--;
}
// Remove all mentions of this word
Vue.delete( this.data.records, this.data.words[ i ] );
Vue.delete( this.data.status, this.data.words[ i ] );
Vue.delete( this.data.errors, this.data.words[ i ] );
Vue.delete( this.data.checkboxes, this.data.words[ i ] );
this.data.words.splice( i, 1 );
return true;
};
RecordStore.prototype.clearAllRecords = function () {
var i;
// Empty global data arrays
for ( i = 0; i < this.data.words.length; i++ ) {
Vue.delete( this.data.records, this.data.words[ i ] );
Vue.delete( this.data.status, this.data.words[ i ] );
Vue.delete( this.data.errors, this.data.words[ i ] );
Vue.delete( this.data.checkboxes, this.data.words[ i ] );
}
// Reset global counters
for ( i in this.data.statusCount ) {
this.data.statusCount[ i ] = 0;
}
// Empty the main word list
this.data.words.splice( 0, this.data.words.length );
};
RecordStore.prototype.clearAllPublishedRecords = function () {
var i,
oldRecords = [];
for ( i = 0; i < this.data.words.length; i++ ) {
if ( this.data.status[ this.data.words[ i ] ] === 'done' ) {
oldRecords.push( this.data.words[ i ] );
this.clearRecord( this.data.words[ i ] );
i--; // to avoid missing a word, as an item has been removed from the array
} else {
this.resetRecord( this.data.words[ i ] );
}
}
return oldRecords;
};
RecordStore.prototype.resetRecord = function ( word ) {
this.data.records[ word ].reset();
this.setStatus( word, 'up' );
this.setError( word, false );
this.data.checkboxes[ word ] = true;
};
RecordStore.prototype.resetFaultyRecords = function () {
var i;
for ( i = 0; i < this.data.words.length; i++ ) {
// Check if the word is not stashed yet
if ( this.data.errors[ this.data.words[ i ] ] !== false ) {
this.resetRecord( this.data.words[ i ] );
}
}
};
RecordStore.prototype.resetStashingRecords = function () {
var i;
for ( i = 0; i < this.data.words.length; i++ ) {
// Check if the word is not stashed yet
if ( this.data.status[ this.data.words[ i ] ] === 'stashing' ) {
this.resetRecord( this.data.words[ i ] );
}
}
};
RecordStore.prototype.addWords = function ( words ) {
var i, word, extra;
// Allow to add a single word
if ( Array.isArray( words ) === false ) {
words = [ words ];
}
for ( i = 0; i < words.length; i++ ) {
word = words[ i ];
extra = {};
// Separate extra informations about the word, if any
if ( typeof word !== 'string' ) {
extra = word;
word = extra.text;
delete extra.text;
}
// Trim the word
word = word.replace( /\t/g, '' ).trim();
// Check if the string is valid
if ( word === '' ) {
continue;
}
// Avoid duplicate words in the list
if ( this.data.words.indexOf( word ) === -1 ) {
// Create a Record instance for this word
if ( this.data.records[ word ] === undefined ) {
this.data.records[ word ] = new rw.Record( word );
this.data.records[ word ].setLanguage( rw.store.config.data.languages[ this.data.metadata.language ] );
this.data.records[ word ].setSpeaker( this.data.metadata.speaker );
this.data.records[ word ].setLicense( this.data.metadata.license );
this.setStatus( word, 'up' );
this.setError( word, false );
Vue.set( this.data.checkboxes, word, true );
}
this.data.records[ word ].setExtra( extra );
// Add the word to the list
this.data.words.push( word );
}
}
};
RecordStore.prototype.randomiseList = function () {
var i, tmp, randomIndex;
// Fisher-Yates shuffle
for ( i = this.data.words.length - 1; i >= 0; i-- ) {
randomIndex = Math.floor( Math.random() * ( i + 1 ) );
tmp = this.data.words[ randomIndex ];
Vue.set( this.data.words, randomIndex, this.data.words[ i ] );
Vue.set( this.data.words, i, tmp );
}
};
RecordStore.prototype.doStash = function ( word, blob ) {
this.setStatus( word, 'ready' );
this.setError( word, false );
if ( blob !== undefined ) {
this.data.records[ word ].setBlob(
blob,
( this.data.metadata.media === 'audio' ? 'wav' : 'webm' )
);
}
this.setStatus( word, 'stashing' );
rw.requestQueue.push( this.data.records[ word ].uploadToStash.bind( this.data.records[ word ], this.$api ) ).then(
this.stashSuccess.bind( this, word ),
this.requestError.bind( this, word, 'ready' )
);
};
RecordStore.prototype.doPublish = function ( word ) {
this.setStatus( word, 'uploading' );
this.setError( word, false );
rw.requestQueue.push( this.data.records[ word ].finishUpload.bind( this.data.records[ word ], this.$api ) ).then(
this.doFinalize.bind( this, word ),
this.requestError.bind( this, word, 'stashed' )
);
};
RecordStore.prototype.doFinalize = function ( word ) {
this.setStatus( word, 'finalizing' );
this.setError( word, false );
rw.requestQueue.force( this.data.records[ word ].saveWbItem.bind( this.data.records[ word ], this.$api ) ).then(
this.publishSuccess.bind( this, word ),
this.requestError.bind( this, word, 'uploaded' )
);
};
RecordStore.prototype.stashSuccess = function ( word ) {
this.setStatus( word, 'stashed' );
this.data.checkboxes[ word ] = true;
};
RecordStore.prototype.publishSuccess = function ( word ) {
this.setStatus( word, 'done' );
this.data.checkboxes[ word ] = true;
};
RecordStore.prototype.requestError = function ( word, prevState, error, errorData ) {
var errorText = error;
if ( errorData !== undefined ) {
// If the upload has been abort, it means another piece of code
// is doing stuff right now, so don't mess-up with it
if ( errorData.textStatus === 'abort' ) {
return;
}
if ( errorData.error !== undefined && errorData.error.info !== undefined ) {
errorText = errorData.error.info;
}
}
this.setStatus( word, prevState );
this.setError( word, errorText );
};
rw.store.record = new RecordStore();
}( mediaWiki, mediaWiki.recordWizard ) );