@oat-sa/tao-core-ui 1.5.4 → 1.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/dist/ckeditor/ckConfigurator.js +9 -1
  2. package/dist/mediaEditor/mediaEditorComponent.js +5 -3
  3. package/dist/mediaEditor/plugins/mediaDimension/mediaDimensionComponent.js +46 -25
  4. package/dist/mediaplayer/css/player.css +104 -14
  5. package/dist/mediaplayer/css/player.css.map +1 -1
  6. package/dist/mediaplayer/players/html5.js +767 -0
  7. package/dist/mediaplayer/players/youtube.js +470 -0
  8. package/dist/mediaplayer/players.js +35 -0
  9. package/dist/mediaplayer/support.js +134 -0
  10. package/dist/mediaplayer/utils/reminder.js +198 -0
  11. package/dist/mediaplayer/utils/timeObserver.js +149 -0
  12. package/dist/mediaplayer/youtubeManager.js +177 -0
  13. package/dist/mediaplayer.js +1251 -1912
  14. package/dist/previewer.js +25 -19
  15. package/package.json +1 -1
  16. package/scss/basic.scss +1 -0
  17. package/scss/inc/_jquery.nouislider.scss +254 -0
  18. package/src/ckeditor/ckConfigurator.js +10 -1
  19. package/src/itemButtonList/css/item-button-list.css +225 -0
  20. package/src/itemButtonList/css/item-button-list.css.map +1 -0
  21. package/src/mediaEditor/mediaEditorComponent.js +25 -26
  22. package/src/mediaEditor/plugins/mediaDimension/mediaDimensionComponent.js +83 -63
  23. package/src/mediaplayer/css/player.css +104 -14
  24. package/src/mediaplayer/css/player.css.map +1 -1
  25. package/src/mediaplayer/players/html5.js +564 -0
  26. package/src/mediaplayer/players/youtube.js +323 -0
  27. package/src/mediaplayer/players.js +29 -0
  28. package/src/mediaplayer/scss/player.scss +125 -16
  29. package/src/mediaplayer/support.js +126 -0
  30. package/src/mediaplayer/tpl/audio.tpl +6 -0
  31. package/src/mediaplayer/tpl/player.tpl +11 -32
  32. package/src/mediaplayer/tpl/source.tpl +1 -0
  33. package/src/mediaplayer/tpl/video.tpl +6 -0
  34. package/src/mediaplayer/tpl/youtube.tpl +1 -0
  35. package/src/mediaplayer/utils/reminder.js +184 -0
  36. package/src/mediaplayer/utils/timeObserver.js +143 -0
  37. package/src/mediaplayer/youtubeManager.js +161 -0
  38. package/src/mediaplayer.js +1217 -1901
  39. package/src/previewer.js +40 -33
  40. package/src/searchModal/css/advancedSearch.css +190 -0
  41. package/src/searchModal/css/advancedSearch.css.map +1 -0
  42. package/src/searchModal/css/searchModal.css +506 -0
  43. package/src/searchModal/css/searchModal.css.map +1 -0
@@ -0,0 +1,564 @@
1
+ /**
2
+ * This program is free software; you can redistribute it and/or
3
+ * modify it under the terms of the GNU General Public License
4
+ * as published by the Free Software Foundation; under version 2
5
+ * of the License (non-upgradable).
6
+ *
7
+ * This program is distributed in the hope that it will be useful,
8
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
+ * GNU General Public License for more details.
11
+ *
12
+ * You should have received a copy of the GNU General Public License
13
+ * along with this program; if not, write to the Free Software
14
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15
+ *
16
+ * Copyright (c) 2015-2021 (original work) Open Assessment Technologies SA ;
17
+ */
18
+
19
+ import $ from 'jquery';
20
+ import UrlParser from 'util/urlParser';
21
+ import eventifier from 'core/eventifier';
22
+ import support from 'ui/mediaplayer/support';
23
+ import audioTpl from 'ui/mediaplayer/tpl/audio';
24
+ import videoTpl from 'ui/mediaplayer/tpl/video';
25
+ import sourceTpl from 'ui/mediaplayer/tpl/source';
26
+ import reminderManagerFactory from 'ui/mediaplayer/utils/reminder';
27
+ import timeObserverFactory from 'ui/mediaplayer/utils/timeObserver';
28
+
29
+ /**
30
+ * CSS namespace
31
+ * @type {string}
32
+ */
33
+ const ns = '.mediaplayer';
34
+
35
+ /**
36
+ * Range value of the volume
37
+ * @type {number}
38
+ */
39
+ const volumeRange = 100;
40
+
41
+ /**
42
+ * Delay before considering a media stalled
43
+ * @type {number}
44
+ */
45
+ const stalledDetectionDelay = 2000;
46
+
47
+ /**
48
+ * List of media events that can be listened to for debugging
49
+ * @type {string[]}
50
+ */
51
+ const mediaEvents = [
52
+ 'abort',
53
+ 'canplay',
54
+ 'canplaythrough',
55
+ 'canshowcurrentframe',
56
+ 'dataunavailable',
57
+ 'durationchange',
58
+ 'emptied',
59
+ 'empty',
60
+ 'ended',
61
+ 'error',
62
+ 'loadeddata',
63
+ 'loadedfirstframe',
64
+ 'loadedmetadata',
65
+ 'loadstart',
66
+ 'pause',
67
+ 'play',
68
+ 'playing',
69
+ 'progress',
70
+ 'ratechange',
71
+ 'seeked',
72
+ 'seeking',
73
+ 'stalled',
74
+ 'suspend',
75
+ 'timeupdate',
76
+ 'volumechange',
77
+ 'waiting'
78
+ ];
79
+
80
+ /**
81
+ * List of player events that can be listened to for debugging
82
+ * @type {string[]}
83
+ */
84
+ const playerEvents = ['end', 'error', 'pause', 'play', 'playing', 'ready', 'resize', 'stalled', 'timeupdate'];
85
+
86
+ /**
87
+ * Defines a player object dedicated to the native HTML5 player
88
+ * @param {jQuery} $container - Where to render the player
89
+ * @param {object} config - The list of config entries
90
+ * @param {Array} config.sources - The list of media sources
91
+ * @param {string} [config.type] - The type of player (video or audio) (default: video)
92
+ * @param {boolean} [config.preview] - Enables the media preview (load media metadata)
93
+ * @param {boolean} [config.debug] - Enables the debug mode
94
+ * @param {number} [config.config.stalledDetectionDelay] - The delay before considering a media is stalled
95
+ * @returns {object} player
96
+ */
97
+ export default function html5PlayerFactory($container, config = {}) {
98
+ const type = config.type || 'video';
99
+ const sources = config.sources || [];
100
+ const updateObserver = reminderManagerFactory();
101
+ const timeObserver = timeObserverFactory();
102
+
103
+ config.stalledDetectionDelay = config.stalledDetectionDelay || stalledDetectionDelay;
104
+
105
+ let $media;
106
+ let media;
107
+ let state = {};
108
+
109
+ const getDebugContext = action => {
110
+ const networkState = media && media.networkState;
111
+ const readyState = media && media.readyState;
112
+ return `[html5-${type}(networkState=${networkState},readyState=${readyState}):${action}]`;
113
+ };
114
+ // eslint-disable-next-line
115
+ const debug = (action, ...args) =>
116
+ (config.debug === true || config.debug === action) && window.console.log(getDebugContext(action), ...args);
117
+
118
+ return eventifier({
119
+ init() {
120
+ const tpl = 'audio' === type ? audioTpl : videoTpl;
121
+ const page = new UrlParser(window.location);
122
+ let cors = false;
123
+ let preload = config.preview ? 'metadata' : 'none';
124
+ let poster = '';
125
+ let link = '';
126
+ let result = false;
127
+
128
+ state = {};
129
+
130
+ sources.forEach(source => {
131
+ if (!page.sameDomain(source.src)) {
132
+ cors = true;
133
+ }
134
+ if (source.poster) {
135
+ poster = source.poster;
136
+ }
137
+ if (source.link) {
138
+ link = source.link;
139
+ }
140
+ });
141
+
142
+ $media = $(tpl({ cors, preload, poster, link }));
143
+ $container.append($media);
144
+
145
+ media = $media.get(0);
146
+ result = !!(media && support.checkSupport(media));
147
+
148
+ // Remove the browser native controls if we can use the API instead
149
+ if (support.canControl()) {
150
+ $media.removeAttr('controls');
151
+ }
152
+
153
+ // Detect stalled video when the timer suddenly jump to the end
154
+ timeObserver.removeAllListeners().on('irregularity', position => {
155
+ if (state.playback && state.stallDetection) {
156
+ this.stalled(position);
157
+ }
158
+ });
159
+
160
+ $media
161
+ .on(`play${ns}`, () => {
162
+ state.playback = true;
163
+ state.playedViaApi = false;
164
+ timeObserver.init(media.currentTime, media.duration);
165
+ this.trigger('play');
166
+ })
167
+ .on(`pause${ns}`, () => {
168
+ if (
169
+ state.stallDetection &&
170
+ !state.pausedViaApi &&
171
+ updateObserver.running &&
172
+ updateObserver.elapsed < 100
173
+ ) {
174
+ // The pause event may be triggered after a connectivity issue as the player is out of data
175
+ this.stalled();
176
+ }
177
+ state.pausedViaApi = false;
178
+ state.playing = false;
179
+ updateObserver.stop();
180
+ this.trigger('pause');
181
+ })
182
+ .on(`seeked${ns}`, () => {
183
+ // When the user try changing the current playing position while the network is down,
184
+ // the player will end the playback by moving straight to the end.
185
+ if (state.seekedViaApi && Math.floor(state.seekAt) !== Math.floor(media.currentTime)) {
186
+ state.stallDetection = true;
187
+ }
188
+ state.seekedViaApi = false;
189
+ })
190
+ .on(`ended${ns}`, () => {
191
+ updateObserver.forget().stop();
192
+ timeObserver.update(media.currentTime);
193
+ state.playback = false;
194
+ state.playing = false;
195
+ this.trigger('end');
196
+ })
197
+ .on(`timeupdate${ns}`, () => {
198
+ state.playing = true;
199
+ updateObserver.start();
200
+ timeObserver.update(media.currentTime);
201
+ this.trigger('timeupdate');
202
+ })
203
+ .on('loadstart', () => {
204
+ if (media.networkState === HTMLMediaElement.NETWORK_NO_SOURCE) {
205
+ this.trigger('error');
206
+ }
207
+
208
+ if (!config.preview && media.networkState === HTMLMediaElement.NETWORK_IDLE) {
209
+ this.trigger('ready');
210
+ }
211
+
212
+ // The media may be unreachable straight from the beginning
213
+ this.detectStalledNetwork();
214
+ })
215
+ .on(`waiting${ns}`, () => {
216
+ // The "waiting" event means the player is pending data,
217
+ // it may be the symptom of a connectivity issue
218
+ this.detectStalledNetwork();
219
+ })
220
+ .on(`error${ns}`, () => {
221
+ if (
222
+ media.networkState === HTMLMediaElement.NETWORK_NO_SOURCE ||
223
+ (media.error instanceof MediaError &&
224
+ media.error.code === MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED)
225
+ ) {
226
+ // No source means the player does not support the supplied media.
227
+ // Or it can be more explicit with the not supported error.
228
+ // There is nothing that we can do from this stage.
229
+ this.trigger('error');
230
+ } else {
231
+ // Other errors need special attention as they can be recoverable
232
+ this.handleError(media.error);
233
+ }
234
+ })
235
+ .on('loadedmetadata', () => {
236
+ timeObserver.init(media.currentTime, media.duration);
237
+ this.ready();
238
+ })
239
+ .on(`canplay${ns}`, () => {
240
+ if (!state.stalled) {
241
+ this.ready();
242
+ }
243
+ })
244
+ .on(`stalled${ns}`, () => {
245
+ // The "stalled" event may be triggered once the player is halted after initialisation,
246
+ // but this does not mean the playback is actually stalled, hence we only take care of the playing state
247
+ if (state.playing && !media.paused) {
248
+ this.handleError(media.error);
249
+ }
250
+ })
251
+ .on(`playing${ns}`, () => {
252
+ if (state.stallDetection) {
253
+ // The "playing" event may occur after a connectivity issue.
254
+ // For the sake of the stall detection, we need to discard this event
255
+ return;
256
+ }
257
+ updateObserver.forget().start();
258
+ state.playing = true;
259
+ this.trigger('playing');
260
+ });
261
+
262
+ // install debug logger
263
+ if (config.debug) {
264
+ debug('installed', media);
265
+ mediaEvents.forEach(eventName => {
266
+ $media.on(eventName + ns, e => debug('media event', eventName, media && media.currentSrc, e));
267
+ });
268
+ playerEvents.forEach(eventName => {
269
+ this.on(eventName, (...args) =>
270
+ debug('player event', eventName, media && media.currentSrc, ...args)
271
+ );
272
+ });
273
+ }
274
+
275
+ result =
276
+ result &&
277
+ sources.reduce((supported, source) => this.addMedia(source.src, source.type) || supported, false);
278
+
279
+ return result;
280
+ },
281
+
282
+ handleError(error) {
283
+ // Discard legitimate and non-blocking errors
284
+ switch (error && error.name) {
285
+ case 'NotAllowedError':
286
+ debug('api call', 'handleError', 'the autoplay is not allowed without a user interaction', error);
287
+ return;
288
+
289
+ case 'AbortError':
290
+ debug('api call', 'handleError', 'the action has been aborted for some reason', error);
291
+ return;
292
+ }
293
+
294
+ debug('api call', 'handleError', error);
295
+
296
+ // Detect if the playback can continue a bit
297
+ const canContinueTemporarily =
298
+ media &&
299
+ (media.readyState === HTMLMediaElement.HAVE_ENOUGH_DATA ||
300
+ media.readyState === HTMLMediaElement.HAVE_FUTURE_DATA ||
301
+ media.readyState === HTMLMediaElement.HAVE_CURRENT_DATA);
302
+
303
+ // If a connectivity error occurs we may need to enter in stalled mode unless we can wait a bit
304
+ if (
305
+ error instanceof MediaError &&
306
+ (error.code === MediaError.MEDIA_ERR_NETWORK || error.code === MediaError.MEDIA_ERR_DECODE) &&
307
+ !canContinueTemporarily
308
+ ) {
309
+ this.stalled();
310
+ return;
311
+ }
312
+
313
+ // To this point, there is a big chance the media is stalled.
314
+ // We start an observer to remind as soon as an irregularity occurs on the time update
315
+ state.stallDetection = true;
316
+ updateObserver.remind(() => {
317
+ // The last time update is a bit old, the media is most probably stalled now
318
+ if (updateObserver.elapsed >= config.stalledDetectionDelay) {
319
+ this.stalled();
320
+ }
321
+ }, config.stalledDetectionDelay);
322
+
323
+ updateObserver.start();
324
+ },
325
+
326
+ ready() {
327
+ if (!state.ready) {
328
+ state.ready = true;
329
+ this.trigger('ready');
330
+ }
331
+ },
332
+
333
+ detectStalledNetwork() {
334
+ // Install an observer that will watch the network state after a small delay.
335
+ // It is needed since the network state may need time to settle.
336
+ setTimeout(() => {
337
+ if (
338
+ media &&
339
+ media.networkState === HTMLMediaElement.NETWORK_NO_SOURCE &&
340
+ media.readyState === HTMLMediaElement.HAVE_NOTHING
341
+ ) {
342
+ if (!state.ready) {
343
+ this.trigger('ready');
344
+ }
345
+ this.stalled();
346
+ }
347
+ }, config.stalledDetectionDelay);
348
+ },
349
+
350
+ stalled(position) {
351
+ debug('api call', 'stalled');
352
+
353
+ if (media) {
354
+ if ('undefined' !== typeof position) {
355
+ state.stalledAt = position;
356
+ } else {
357
+ state.stalledAt = timeObserver.position;
358
+ }
359
+ }
360
+ state.stalled = true;
361
+ state.stallDetection = false;
362
+ updateObserver.forget().stop();
363
+
364
+ this.pause();
365
+ this.trigger('stalled');
366
+ },
367
+
368
+ recover() {
369
+ debug('api call', 'recover');
370
+
371
+ state.stalled = false;
372
+ state.stallDetection = false;
373
+ if (media) {
374
+ // Special processing of video player to prevent visual glitch while reloading
375
+ if (media.tagName === 'VIDEO') {
376
+ // Temporarily set the size of the media to prevent a shrink while reloading it
377
+ $media.width($media.width());
378
+ $media.height($media.height());
379
+ $media.on('loadedmetadata.recover', () => {
380
+ $media.off('loadedmetadata.recover');
381
+ $media.css({ width: '', height: '' });
382
+ });
383
+ }
384
+
385
+ media.load();
386
+ if (state.stalledAt) {
387
+ this.seek(state.stalledAt);
388
+ }
389
+ if ((state.playback && !state.playing) || state.playedViaApi) {
390
+ this.play();
391
+ }
392
+ }
393
+ },
394
+
395
+ destroy() {
396
+ debug('api call', 'destroy');
397
+
398
+ this.stop();
399
+ this.removeAllListeners();
400
+ updateObserver.forget();
401
+ timeObserver.removeAllListeners();
402
+
403
+ if ($media) {
404
+ $media.off(ns).remove();
405
+ }
406
+
407
+ $media = void 0;
408
+ media = void 0;
409
+ state = {};
410
+ },
411
+
412
+ getMedia() {
413
+ debug('api call', 'getMedia', media);
414
+
415
+ return media;
416
+ },
417
+
418
+ getMediaSize() {
419
+ let size = {};
420
+ if (media) {
421
+ size = {
422
+ width: media.videoWidth,
423
+ height: media.videoHeight
424
+ };
425
+ }
426
+
427
+ debug('api call', 'getMediaSize', size);
428
+ return size;
429
+ },
430
+
431
+ getPosition() {
432
+ let position = 0;
433
+ if (media) {
434
+ position = media.currentTime;
435
+ }
436
+
437
+ debug('api call', 'getPosition', position);
438
+ return position;
439
+ },
440
+
441
+ getDuration() {
442
+ let duration = 0;
443
+ if (media) {
444
+ duration = media.duration;
445
+ }
446
+
447
+ debug('api call', 'getDuration', duration);
448
+ return duration;
449
+ },
450
+
451
+ getVolume() {
452
+ let volume = 0;
453
+ if (media) {
454
+ volume = parseFloat(media.volume) * volumeRange;
455
+ }
456
+
457
+ debug('api call', 'getVolume', volume);
458
+ return volume;
459
+ },
460
+
461
+ setVolume(volume) {
462
+ debug('api call', 'setVolume', volume);
463
+
464
+ if (media) {
465
+ media.volume = parseFloat(volume) / volumeRange;
466
+ }
467
+ },
468
+
469
+ setSize(width, height) {
470
+ debug('api call', 'setSize', width, height);
471
+
472
+ this.trigger('resize', width, height);
473
+ },
474
+
475
+ seek(time) {
476
+ debug('api call', 'seek', time);
477
+
478
+ if (media) {
479
+ media.currentTime = parseFloat(time);
480
+ state.seekedViaApi = true;
481
+ state.seekAt = media.currentTime;
482
+ timeObserver.seek(media.currentTime);
483
+ if (!state.playback) {
484
+ this.play();
485
+ }
486
+ }
487
+ },
488
+
489
+ play() {
490
+ debug('api call', 'play');
491
+
492
+ if (media) {
493
+ state.playedViaApi = true;
494
+ const startPlayPromise = media.play();
495
+ if ('undefined' !== typeof startPlayPromise) {
496
+ startPlayPromise.catch(error => this.handleError(error));
497
+ }
498
+ }
499
+ },
500
+
501
+ pause() {
502
+ debug('api call', 'pause');
503
+
504
+ if (media) {
505
+ if (!media.paused) {
506
+ state.pausedViaApi = true;
507
+ }
508
+ media.pause();
509
+ }
510
+ },
511
+
512
+ stop() {
513
+ debug('api call', 'stop');
514
+
515
+ if (media && media.duration && state.playback && !state.stalled) {
516
+ media.currentTime = media.duration;
517
+ }
518
+ },
519
+
520
+ mute(muted) {
521
+ debug('api call', 'mute', muted);
522
+
523
+ if (media) {
524
+ media.muted = !!muted;
525
+ }
526
+ },
527
+
528
+ isMuted() {
529
+ let mute = false;
530
+ if (media) {
531
+ mute = !!media.muted;
532
+ }
533
+
534
+ debug('api call', 'isMuted', mute);
535
+ return mute;
536
+ },
537
+
538
+ addMedia(src, srcType) {
539
+ debug('api call', 'addMedia', src, srcType);
540
+
541
+ if (media) {
542
+ if (!support.checkSupport(media, srcType)) {
543
+ return false;
544
+ }
545
+ }
546
+
547
+ if (src && $media) {
548
+ $media.append(sourceTpl({ src, type: srcType }));
549
+ return true;
550
+ }
551
+ return false;
552
+ },
553
+
554
+ setMedia(src, srcType) {
555
+ debug('api call', 'setMedia', src, srcType);
556
+
557
+ if ($media) {
558
+ $media.empty();
559
+ return this.addMedia(src, srcType);
560
+ }
561
+ return false;
562
+ }
563
+ });
564
+ }