@checksub_team/peaks_timeline 1.16.1 → 2.0.0-alpha.2

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 (37) hide show
  1. package/package.json +1 -1
  2. package/peaks.js +4716 -4409
  3. package/peaks.js.d.ts +5 -5
  4. package/src/{timeline-axis.js → components/axis.js} +244 -244
  5. package/src/{data-retriever.js → components/data-retriever.js} +117 -117
  6. package/src/{default-segment-marker.js → components/default-segment-marker.js} +132 -132
  7. package/src/{invoker.js → components/invoker.js} +81 -81
  8. package/src/components/line-group.js +692 -0
  9. package/src/components/line-groups.js +585 -0
  10. package/src/{line-indicator.js → components/line-indicator.js} +308 -303
  11. package/src/{marker-factories.js → components/marker-factories.js} +1 -1
  12. package/src/{mode-layer.js → components/mode-layer.js} +8 -12
  13. package/src/{playhead-layer.js → components/playhead-layer.js} +3 -3
  14. package/src/{segment-marker.js → components/segment-marker.js} +2 -2
  15. package/src/{segment-shape.js → components/segment-shape.js} +508 -508
  16. package/src/{segments-group.js → components/segments-group.js} +805 -801
  17. package/src/{source-group.js → components/source-group.js} +1661 -1640
  18. package/src/{sources-layer.js → components/sources-layer.js} +716 -730
  19. package/src/{waveform-builder.js → components/waveform-builder.js} +2 -2
  20. package/src/{waveform-shape.js → components/waveform-shape.js} +214 -214
  21. package/src/keyboard-handler.js +9 -9
  22. package/src/line-handler.js +179 -0
  23. package/src/main.js +110 -71
  24. package/src/models/line.js +156 -0
  25. package/src/{segment.js → models/segment.js} +420 -419
  26. package/src/{source.js → models/source.js} +1311 -1315
  27. package/src/player.js +2 -2
  28. package/src/{timeline-segments.js → segment-handler.js} +435 -435
  29. package/src/{timeline-sources.js → source-handler.js} +521 -514
  30. package/src/utils.js +5 -1
  31. package/src/{timeline-zoomview.js → view.js} +136 -137
  32. package/src/line.js +0 -690
  33. package/src/lines.js +0 -427
  34. /package/src/{data.js → components/data.js} +0 -0
  35. /package/src/{loader.js → components/loader.js} +0 -0
  36. /package/src/{mouse-drag-handler.js → components/mouse-drag-handler.js} +0 -0
  37. /package/src/{svgs.js → components/svgs.js} +0 -0
@@ -1,514 +1,521 @@
1
- /**
2
- * @file
3
- *
4
- * Defines the {@link TimelineSources} class.
5
- *
6
- * @module timeline-sources
7
- */
8
-
9
- define([
10
- './source',
11
- './utils'
12
- ], function(Source, Utils) {
13
- 'use strict';
14
-
15
- /**
16
- * Source parameters.
17
- *
18
- * @typedef {Object} SourceOptions
19
- * @global
20
- * @param {String} id A unique identifier for the source.
21
- * @param {String} title Name of the source.
22
- * @param {String} url Reference to the media element this source is representing.
23
- * @param {Number} startTime Source start time, in seconds.
24
- * @param {Number} endTime Source end time, in seconds.
25
- * @param {String} color Color of the background.
26
- * @param {Boolean} wrapped If <code>true</code> the source representation
27
- * will be reduced to a line, taking less space.
28
- * @param {Number} position Position of the source on the timeline.
29
- * Correspond to the index of the line.
30
- * @param {Boolean} segments If <code>true</code> this source will display segments;
31
- */
32
-
33
- /**
34
- * Handles all functionality related to the adding, removing and manipulation
35
- * of sources.
36
- *
37
- * @class
38
- * @alias TimelineSources
39
- *
40
- * @param {Peaks} peaks The parent Peaks object.
41
- */
42
-
43
- function TimelineSources(peaks) {
44
- this._peaks = peaks;
45
- this._sources = [];
46
- this._sourcesById = {};
47
- this._colorIndex = 0;
48
-
49
- this._onSourceCut = this._onSourceCut.bind(this);
50
-
51
- this._peaks.on('source.cut', this._onSourceCut);
52
- }
53
-
54
- TimelineSources.prototype._onSourceCut = function(sourceToCut, cuttingTime) {
55
- var originalMediaEndTime = sourceToCut.mediaEndTime;
56
- var originalEndTime = sourceToCut.endTime;
57
-
58
- // Cut the source
59
- sourceToCut.update({
60
- endTime: sourceToCut.startTime + cuttingTime,
61
- mediaEndTime: sourceToCut.mediaStartTime + cuttingTime
62
- });
63
-
64
- // Create the copy (cut)
65
- var newSourceOptions = {
66
- id: Utils.createUuidv4(),
67
- originId: sourceToCut.id,
68
- elementId: sourceToCut.elementId,
69
- title: sourceToCut.title,
70
- titleAlignments: sourceToCut.titleAlignments,
71
- url: sourceToCut.url,
72
- previewUrl: sourceToCut.previewUrl,
73
- binaryUrl: sourceToCut.binaryUrl,
74
- kind: sourceToCut.kind,
75
- subkind: sourceToCut.subkind,
76
- duration: sourceToCut.duration,
77
- startTime: sourceToCut.endTime,
78
- endTime: originalEndTime,
79
- mediaStartTime: sourceToCut.mediaEndTime,
80
- mediaEndTime: originalMediaEndTime,
81
- color: sourceToCut.color,
82
- backgroundColor: sourceToCut.backgroundColor,
83
- hoverBackgroundColor: sourceToCut.hoverBackgroundColor,
84
- selectedBackgroundColor: sourceToCut.selectedBackgroundColor,
85
- borderColor: sourceToCut.borderColor,
86
- selectedBorderColor: sourceToCut.selectedBorderColor,
87
- warningColor: sourceToCut.warningColor,
88
- warningWidth: sourceToCut.warningWidth,
89
- volumeSliderColor: sourceToCut.volumeSliderColor,
90
- volumeSliderWidth: sourceToCut.volumeSliderWidth,
91
- volumeSliderDraggingWidth: sourceToCut.volumeSliderDraggingWidth,
92
- textFont: sourceToCut.textFont,
93
- textFontSize: sourceToCut.textFontSize,
94
- textColor: sourceToCut.textColor,
95
- textBackgroundColor: sourceToCut.textBackgroundColor,
96
- textPosition: sourceToCut.textPosition,
97
- textAutoScroll: sourceToCut.textAutoScroll,
98
- borderWidth: sourceToCut.borderWidth,
99
- borderRadius: sourceToCut.borderRadius,
100
- wrapped: sourceToCut.wrapped,
101
- position: sourceToCut.position,
102
- draggable: sourceToCut.draggable,
103
- orderable: sourceToCut.orderable,
104
- resizable: sourceToCut.resizable,
105
- cuttable: sourceToCut.cuttable,
106
- deletable: sourceToCut.deletable,
107
- wrapping: sourceToCut.wrapping,
108
- previewHeight: sourceToCut.previewHeight,
109
- binaryHeight: sourceToCut.binaryHeight,
110
- markers: sourceToCut.markers,
111
- buttons: sourceToCut.buttons,
112
- markerColor: sourceToCut.markerColor,
113
- markerWidth: sourceToCut.markerWidth,
114
- volume: sourceToCut.volume,
115
- volumeRange: sourceToCut.volumeRange,
116
- loading: sourceToCut.loading
117
- };
118
-
119
- for (var key in sourceToCut) {
120
- if (key.startsWith('custom_')) {
121
- newSourceOptions[key] = sourceToCut[key];
122
- }
123
- }
124
-
125
- var newSource = this._add([newSourceOptions])[0];
126
-
127
- this._peaks.emit('sources.updated', [sourceToCut, newSource]);
128
- };
129
-
130
- /**
131
- * Returns a new unique Source id value.
132
- *
133
- * @private
134
- * @returns {String}
135
- */
136
-
137
- TimelineSources.prototype._getNextSourceId = function() {
138
- return 'peaks.source.' + Utils.createUuidv4();
139
- };
140
-
141
- /**
142
- * Adds a new Source object.
143
- *
144
- * @private
145
- * @param {Source} Source
146
- */
147
-
148
- TimelineSources.prototype._addSource = function(source) {
149
- this._sources.push(source);
150
-
151
- this._sourcesById[source.id] = source;
152
- };
153
-
154
- /**
155
- * Creates a new Source object.
156
- *
157
- * @private
158
- * @param {SourceOptions} options
159
- * @return {Source}
160
- */
161
-
162
- TimelineSources.prototype._createSource = function(options) {
163
- // Watch for anyone still trying to use the old
164
- // createSource(startTime, endTime, ...) API
165
- if (!Utils.isObject(options)) {
166
- throw new TypeError('peaks.sources.add(): expected a Source object parameter');
167
- }
168
-
169
- var customParams = [];
170
-
171
- Object.entries(options).forEach(function([key, value]) {
172
- if (key.startsWith('custom_')) {
173
- customParams.push(key, value);
174
- }
175
- });
176
-
177
- var source = new Source(
178
- this._peaks,
179
- options.id || this._getNextSourceId(),
180
- options.originId,
181
- options.elementId,
182
- options.title,
183
- options.titleAlignments,
184
- options.url,
185
- options.previewUrl,
186
- options.binaryUrl,
187
- options.kind,
188
- options.subkind,
189
- options.duration,
190
- options.startTime,
191
- options.endTime,
192
- options.mediaStartTime,
193
- options.mediaEndTime,
194
- options.color,
195
- options.backgroundColor,
196
- options.hoverBackgroundColor,
197
- options.selectedBackgroundColor,
198
- options.borderColor,
199
- options.selectedBorderColor,
200
- options.warningColor,
201
- options.warningWidth,
202
- options.volumeSliderColor,
203
- options.volumeSliderWidth,
204
- options.volumeSliderDraggingWidth,
205
- options.textFont,
206
- options.textFontSize,
207
- options.textColor,
208
- options.textBackgroundColor,
209
- options.textPosition,
210
- options.textAutoScroll,
211
- options.borderWidth,
212
- options.borderRadius,
213
- options.wrapped,
214
- options.position,
215
- options.draggable,
216
- options.orderable,
217
- options.resizable,
218
- options.cuttable,
219
- options.deletable,
220
- options.wrapping,
221
- options.previewHeight,
222
- options.binaryHeight,
223
- options.indicators,
224
- options.markers,
225
- options.buttons,
226
- options.markerColor,
227
- options.markerWidth,
228
- options.volume,
229
- options.volumeRange,
230
- options.loading,
231
- ...customParams
232
- );
233
-
234
- return source;
235
- };
236
-
237
- /**
238
- * Returns all sources.
239
- *
240
- * @returns {Array<Source>}
241
- */
242
-
243
- TimelineSources.prototype.getSources = function() {
244
- return this._sources;
245
- };
246
-
247
- /**
248
- * Returns all sources, serialized to a plain object.
249
- *
250
- * @returns {Array<Object>}
251
- */
252
-
253
- TimelineSources.prototype.getSourcesSerialized = function() {
254
- return this._sources.map(function(source) {
255
- return source.toSerializable();
256
- });
257
- };
258
-
259
- /**
260
- * Returns the Source with the given id, or <code>null</code> if not found.
261
- *
262
- * @param {String} id
263
- * @returns {Source|null}
264
- */
265
-
266
- TimelineSources.prototype.getSource = function(id) {
267
- return this._sourcesById[id] || null;
268
- };
269
-
270
- /**
271
- * Returns all sources that overlap a given point in time.
272
- *
273
- * @param {Number} time
274
- * @returns {Array<Source>}
275
- */
276
-
277
- TimelineSources.prototype.getSourcesAtTime = function(time) {
278
- return this._sources.filter(function(source) {
279
- return time >= source.startTime && time <= source.endTime;
280
- });
281
- };
282
-
283
- /**
284
- * Returns all sources that overlap a given time region.
285
- *
286
- * @param {Number} startTime The start of the time region, in seconds.
287
- * @param {Number} endTime The end of the time region, in seconds.
288
- *
289
- * @returns {Array<Source>}
290
- */
291
-
292
- TimelineSources.prototype.find = function(startTime, endTime) {
293
- return this._sources.filter(function(source) {
294
- return source.isVisible(startTime, endTime);
295
- });
296
- };
297
-
298
- /**
299
- * Adds one or more sources to the timeline.
300
- *
301
- * @param {SourceOptions|Array<SourceOptions>} sourceOrSources
302
- */
303
-
304
- TimelineSources.prototype.add = function(/* sourceOrSources */) {
305
- var sources = Array.isArray(arguments[0]) ?
306
- arguments[0] :
307
- Array.prototype.slice.call(arguments);
308
-
309
- this._add(sources);
310
- };
311
-
312
- TimelineSources.prototype._add = function(sources) {
313
- var self = this;
314
-
315
- sources = sources.map(function(sourceOptions) {
316
- var source = self._createSource(sourceOptions);
317
-
318
- if (Utils.objectHasProperty(self._sourcesById, source.id)) {
319
- var src = Object.values(self._sourcesById)[0];
320
-
321
- throw new Error('peaks.sources.add(): duplicate id. \n\n Source I = ' +
322
- JSON.stringify(
323
- {
324
- id: src.id,
325
- startTime: src.startTime,
326
- endTime: src.endTime
327
- }
328
- ) + ' Source II = ' +
329
- JSON.stringify(
330
- {
331
- id: source.id,
332
- startTime: source.startTime,
333
- endTime: source.endTime
334
- }
335
- )
336
- );
337
- }
338
-
339
- return source;
340
- });
341
-
342
- sources.forEach(function(source) {
343
- self._addSource(source);
344
- });
345
-
346
- this._peaks.emit('sources.add', sources);
347
-
348
- return sources;
349
- };
350
-
351
- /**
352
- * Returns the indexes of sources that match the given predicate.
353
- *
354
- * @private
355
- * @param {Function} predicate Predicate function to find matching sources.
356
- * @returns {Array<Number>} An array of indexes into the sources array of
357
- * the matching elements.
358
- */
359
-
360
- TimelineSources.prototype._findSource = function(predicate) {
361
- var indexes = [];
362
-
363
- for (var i = 0, length = this._sources.length; i < length; i++) {
364
- if (predicate(this._sources[i])) {
365
- indexes.push(i);
366
- }
367
- }
368
-
369
- return indexes;
370
- };
371
-
372
- /**
373
- * destroys the sources at the given array indexes.
374
- *
375
- * @private
376
- * @param {Array<Number>} indexes The array indexes to destroy.
377
- * @returns {Array<Source>} The destroyd {@link Source} objects.
378
- */
379
-
380
- TimelineSources.prototype._destroyIndexes = function(indexes) {
381
- var destroyed = [];
382
-
383
- for (var i = 0; i < indexes.length; i++) {
384
- var index = indexes[i] - destroyed.length;
385
-
386
- var itemDestroyed = this._sources.splice(index, 1)[0];
387
-
388
- delete this._sourcesById[itemDestroyed.id];
389
-
390
- destroyed.push(itemDestroyed);
391
- }
392
-
393
- return destroyed;
394
- };
395
-
396
- /**
397
- * destroys all sources that match a given predicate function.
398
- *
399
- * After removing the sources, this function also emits a
400
- * <code>sources.destroy</code> event with the destroyd {@link Source}
401
- * objects.
402
- *
403
- * @private
404
- * @param {Function} predicate A predicate function that identifies which
405
- * sources to destroy.
406
- * @returns {Array<Source>} The destroyd {@link Source} objects.
407
- */
408
-
409
- TimelineSources.prototype._destroySources = function(predicate, shouldBlockEvent) {
410
- var indexes = this._findSource(predicate);
411
-
412
- var destroyed = this._destroyIndexes(indexes);
413
-
414
- this._peaks.emit('sources.destroy', destroyed, shouldBlockEvent);
415
-
416
- return destroyed;
417
- };
418
-
419
- /**
420
- * destroys the given source.
421
- *
422
- * @param {Source} Source The source to destroy.
423
- * @returns {Array<Source>} The destroyd source.
424
- */
425
-
426
- TimelineSources.prototype.destroy = function(source, shouldBlockEvent) {
427
- return this._destroySources(function(s) {
428
- return s === source;
429
- }, shouldBlockEvent);
430
- };
431
-
432
- /**
433
- * destroys any sources with the given id.
434
- *
435
- * @param {String} id
436
- * @returns {Array<Source>} The destroyed {@link Source} objects.
437
- */
438
-
439
- TimelineSources.prototype.destroyById = function(sourceId, shouldBlockEvent) {
440
- return this._destroySources(function(source) {
441
- return source.id === sourceId;
442
- }, shouldBlockEvent);
443
- };
444
-
445
- /**
446
- * destroys any sources with the given start time, and optional end time.
447
- *
448
- * @param {Number} startTime sources with this start time are destroyd.
449
- * @param {Number?} endTime If present, only sources with both the given
450
- * start time and end time are destroyd.
451
- * @returns {Array<Source>} The destroyd {@link Source} objects.
452
- */
453
-
454
- TimelineSources.prototype.destroyByTime = function(startTime, endTime, shouldBlockEvent) {
455
- var fnFilter;
456
-
457
- if (endTime > 0) {
458
- fnFilter = function(source) {
459
- return source.startTime === startTime && source.endTime === endTime;
460
- };
461
- }
462
- else {
463
- fnFilter = function(source) {
464
- return source.startTime === startTime;
465
- };
466
- }
467
-
468
- return this._destroySources(fnFilter, shouldBlockEvent);
469
- };
470
-
471
- /**
472
- * destroys all sources.
473
- */
474
-
475
- TimelineSources.prototype.destroyAll = function(shouldBlockEvent) {
476
- this._destroySources(function() {
477
- return true;
478
- }, shouldBlockEvent);
479
- };
480
-
481
- /**
482
- * Set the wrapping of the source with the given ID to false.
483
- * The source will be shown in its complete form.
484
- *
485
- * @param {String} sourceId The id of the source.
486
- */
487
-
488
- TimelineSources.prototype.showById = function(sourceId) {
489
- // var indexes = this._findSource(function(source) {
490
- // return source.id === sourceId;
491
- // })
492
- this._sourcesById[sourceId].wrapped = false;
493
-
494
- this._peaks.emit('sources.show', [this._sourcesById[sourceId]]);
495
- };
496
-
497
- /**
498
- * Set the wrapping of the source with the given ID to true.
499
- * The source will be shown in its reduced form.
500
- *
501
- * @param {String} sourceId The id of the source.
502
- */
503
-
504
- TimelineSources.prototype.hideById = function(sourceId) {
505
- // var indexes = this._findSource(function(source) {
506
- // return source.id === sourceId;
507
- // })
508
- this._sourcesById[sourceId].wrapped = true;
509
-
510
- this._peaks.emit('sources.hide', [this._sourcesById[sourceId]]);
511
- };
512
-
513
- return TimelineSources;
514
- });
1
+ /**
2
+ * @file
3
+ *
4
+ * Defines the {@link SourceHandler} class.
5
+ *
6
+ * @module source-handler
7
+ */
8
+
9
+ define([
10
+ './models/source',
11
+ './utils'
12
+ ], function(Source, Utils) {
13
+ 'use strict';
14
+
15
+ /**
16
+ * Source parameters.
17
+ *
18
+ * @typedef {Object} SourceOptions
19
+ * @global
20
+ * @param {String} id A unique identifier for the source.
21
+ * @param {String} title Name of the source.
22
+ * @param {String} url Reference to the media element this source is representing.
23
+ * @param {Number} startTime Source start time, in seconds.
24
+ * @param {Number} endTime Source end time, in seconds.
25
+ * @param {String} color Color of the background.
26
+ * @param {Boolean} wrapped If <code>true</code> the source representation
27
+ * will be reduced to a line, taking less space.
28
+ * @param {Number} position Position of the source on the timeline.
29
+ * Correspond to the index of the line.
30
+ * @param {Boolean} segments If <code>true</code> this source will display segments;
31
+ */
32
+
33
+ /**
34
+ * Handles all functionality related to the adding, removing and manipulation
35
+ * of sources.
36
+ *
37
+ * @class
38
+ * @alias SourceHandler
39
+ *
40
+ * @param {Peaks} peaks The parent Peaks object.
41
+ */
42
+
43
+ function SourceHandler(peaks) {
44
+ this._peaks = peaks;
45
+ this._sources = [];
46
+ this._sourcesById = {};
47
+ this._sourcesByLineId = {};
48
+ this._colorIndex = 0;
49
+ }
50
+
51
+ SourceHandler.prototype.cutSource = function(sourceToCut, cuttingTime) {
52
+ var originalMediaEndTime = sourceToCut.mediaEndTime;
53
+ var originalEndTime = sourceToCut.endTime;
54
+
55
+ // Cut the source
56
+ sourceToCut.update({
57
+ endTime: sourceToCut.startTime + cuttingTime,
58
+ mediaEndTime: sourceToCut.mediaStartTime + cuttingTime
59
+ });
60
+
61
+ // Create the copy (cut)
62
+ var newSourceOptions = {
63
+ id: Utils.createUuidv4(),
64
+ lineId: sourceToCut.lineId,
65
+ originId: sourceToCut.id,
66
+ elementId: sourceToCut.elementId,
67
+ title: sourceToCut.title,
68
+ titleAlignments: sourceToCut.titleAlignments,
69
+ url: sourceToCut.url,
70
+ previewUrl: sourceToCut.previewUrl,
71
+ binaryUrl: sourceToCut.binaryUrl,
72
+ kind: sourceToCut.kind,
73
+ subkind: sourceToCut.subkind,
74
+ duration: sourceToCut.duration,
75
+ startTime: sourceToCut.endTime,
76
+ endTime: originalEndTime,
77
+ mediaStartTime: sourceToCut.mediaEndTime,
78
+ mediaEndTime: originalMediaEndTime,
79
+ color: sourceToCut.color,
80
+ backgroundColor: sourceToCut.backgroundColor,
81
+ hoverBackgroundColor: sourceToCut.hoverBackgroundColor,
82
+ selectedBackgroundColor: sourceToCut.selectedBackgroundColor,
83
+ borderColor: sourceToCut.borderColor,
84
+ selectedBorderColor: sourceToCut.selectedBorderColor,
85
+ warningColor: sourceToCut.warningColor,
86
+ warningWidth: sourceToCut.warningWidth,
87
+ volumeSliderColor: sourceToCut.volumeSliderColor,
88
+ volumeSliderWidth: sourceToCut.volumeSliderWidth,
89
+ volumeSliderDraggingWidth: sourceToCut.volumeSliderDraggingWidth,
90
+ textFont: sourceToCut.textFont,
91
+ textFontSize: sourceToCut.textFontSize,
92
+ textColor: sourceToCut.textColor,
93
+ textBackgroundColor: sourceToCut.textBackgroundColor,
94
+ textPosition: sourceToCut.textPosition,
95
+ textAutoScroll: sourceToCut.textAutoScroll,
96
+ borderWidth: sourceToCut.borderWidth,
97
+ borderRadius: sourceToCut.borderRadius,
98
+ wrapped: sourceToCut.wrapped,
99
+ draggable: sourceToCut.draggable,
100
+ orderable: sourceToCut.orderable,
101
+ resizable: sourceToCut.resizable,
102
+ cuttable: sourceToCut.cuttable,
103
+ deletable: sourceToCut.deletable,
104
+ wrapping: sourceToCut.wrapping,
105
+ previewHeight: sourceToCut.previewHeight,
106
+ binaryHeight: sourceToCut.binaryHeight,
107
+ markers: sourceToCut.markers,
108
+ buttons: sourceToCut.buttons,
109
+ markerColor: sourceToCut.markerColor,
110
+ markerWidth: sourceToCut.markerWidth,
111
+ volume: sourceToCut.volume,
112
+ volumeRange: sourceToCut.volumeRange,
113
+ loading: sourceToCut.loading
114
+ };
115
+
116
+ for (var key in sourceToCut) {
117
+ if (key.startsWith('custom_')) {
118
+ newSourceOptions[key] = sourceToCut[key];
119
+ }
120
+ }
121
+
122
+ var newSource = this._add([newSourceOptions])[0];
123
+
124
+ this._peaks.emit('sources.updated', [sourceToCut, newSource]);
125
+ };
126
+
127
+ /**
128
+ * Returns a new unique Source id value.
129
+ *
130
+ * @private
131
+ * @returns {String}
132
+ */
133
+
134
+ SourceHandler.prototype._getNextSourceId = function() {
135
+ return 'peaks.source.' + Utils.createUuidv4();
136
+ };
137
+
138
+ /**
139
+ * Adds a new Source object.
140
+ *
141
+ * @private
142
+ * @param {Source} Source
143
+ */
144
+
145
+ SourceHandler.prototype._addSource = function(source) {
146
+ this._sources.push(source);
147
+ this._sourcesById[source.id] = source;
148
+ if (Utils.isNullOrUndefined(this._sourcesByLineId[source.lineId])) {
149
+ this._sourcesByLineId[source.lineId] = {};
150
+ }
151
+ this._sourcesByLineId[source.lineId][source.id] = source;
152
+ };
153
+
154
+ /**
155
+ * Creates a new Source object.
156
+ *
157
+ * @private
158
+ * @param {SourceOptions} options
159
+ * @return {Source}
160
+ */
161
+
162
+ SourceHandler.prototype._createSource = function(options) {
163
+ // Watch for anyone still trying to use the old
164
+ // createSource(startTime, endTime, ...) API
165
+ if (!Utils.isObject(options)) {
166
+ throw new TypeError('peaks.sources.add(): expected a Source object parameter');
167
+ }
168
+
169
+ var customParams = [];
170
+
171
+ Object.entries(options).forEach(function([key, value]) {
172
+ if (key.startsWith('custom_')) {
173
+ customParams.push(key, value);
174
+ }
175
+ });
176
+
177
+ var source = new Source(
178
+ this._peaks,
179
+ options.id || this._getNextSourceId(),
180
+ options.lineId,
181
+ options.originId,
182
+ options.elementId,
183
+ options.title,
184
+ options.titleAlignments,
185
+ options.url,
186
+ options.previewUrl,
187
+ options.binaryUrl,
188
+ options.kind,
189
+ options.subkind,
190
+ options.duration,
191
+ options.startTime,
192
+ options.endTime,
193
+ options.mediaStartTime,
194
+ options.mediaEndTime,
195
+ options.color,
196
+ options.backgroundColor,
197
+ options.hoverBackgroundColor,
198
+ options.selectedBackgroundColor,
199
+ options.borderColor,
200
+ options.selectedBorderColor,
201
+ options.warningColor,
202
+ options.warningWidth,
203
+ options.volumeSliderColor,
204
+ options.volumeSliderWidth,
205
+ options.volumeSliderDraggingWidth,
206
+ options.textFont,
207
+ options.textFontSize,
208
+ options.textColor,
209
+ options.textBackgroundColor,
210
+ options.textPosition,
211
+ options.textAutoScroll,
212
+ options.borderWidth,
213
+ options.borderRadius,
214
+ options.wrapped,
215
+ options.draggable,
216
+ options.orderable,
217
+ options.resizable,
218
+ options.cuttable,
219
+ options.deletable,
220
+ options.wrapping,
221
+ options.previewHeight,
222
+ options.binaryHeight,
223
+ options.indicators,
224
+ options.markers,
225
+ options.buttons,
226
+ options.markerColor,
227
+ options.markerWidth,
228
+ options.volume,
229
+ options.volumeRange,
230
+ options.loading,
231
+ ...customParams
232
+ );
233
+
234
+ return source;
235
+ };
236
+
237
+ /**
238
+ * Returns all sources.
239
+ *
240
+ * @returns {Array<Source>}
241
+ */
242
+
243
+ SourceHandler.prototype.getSources = function() {
244
+ return this._sources;
245
+ };
246
+
247
+ SourceHandler.prototype.getSourcesByLineId = function(lineId) {
248
+ return Utils.isNullOrUndefined(lineId) ? this._sourcesByLineId : this._sourcesByLineId[lineId];
249
+ };
250
+
251
+ /**
252
+ * Returns all sources, serialized to a plain object.
253
+ *
254
+ * @returns {Array<Object>}
255
+ */
256
+
257
+ SourceHandler.prototype.getSourcesSerialized = function() {
258
+ return this._sources.map(function(source) {
259
+ return source.toSerializable();
260
+ });
261
+ };
262
+
263
+ /**
264
+ * Returns the Source with the given id, or <code>null</code> if not found.
265
+ *
266
+ * @param {String} id
267
+ * @returns {Source|null}
268
+ */
269
+
270
+ SourceHandler.prototype.getSource = function(id) {
271
+ return this._sourcesById[id] || null;
272
+ };
273
+
274
+ /**
275
+ * Returns all sources that overlap a given point in time.
276
+ *
277
+ * @param {Number} time
278
+ * @returns {Array<Source>}
279
+ */
280
+
281
+ SourceHandler.prototype.getSourcesAtTime = function(time) {
282
+ return this._sources.filter(function(source) {
283
+ return time >= source.startTime && time <= source.endTime;
284
+ });
285
+ };
286
+
287
+ /**
288
+ * Returns all sources that overlap a given time region.
289
+ *
290
+ * @param {Number} startTime The start of the time region, in seconds.
291
+ * @param {Number} endTime The end of the time region, in seconds.
292
+ *
293
+ * @returns {Array<Source>}
294
+ */
295
+
296
+ SourceHandler.prototype.find = function(startTime, endTime) {
297
+ return this._sources.filter(function(source) {
298
+ return source.isVisible(startTime, endTime);
299
+ });
300
+ };
301
+
302
+ /**
303
+ * Adds one or more sources to the timeline.
304
+ *
305
+ * @param {SourceOptions|Array<SourceOptions>} sourceOrSources
306
+ */
307
+
308
+ SourceHandler.prototype.add = function(/* sourceOrSources */) {
309
+ var sources = Array.isArray(arguments[0]) ?
310
+ arguments[0] :
311
+ Array.prototype.slice.call(arguments);
312
+
313
+ this._add(sources);
314
+ };
315
+
316
+ SourceHandler.prototype._add = function(sources) {
317
+ var self = this;
318
+
319
+ sources = sources.map(function(sourceOptions) {
320
+ if (
321
+ !Utils.isString(sourceOptions.lineId) ||
322
+ Utils.isNullOrUndefined(self._peaks.lineHandler.getLine(sourceOptions.lineId))
323
+ ) {
324
+ throw new Error('peaks.sources.add(): line with id ' + sourceOptions.lineId +
325
+ ' does not exist');
326
+ }
327
+
328
+ const source = self._createSource(sourceOptions);
329
+
330
+ if (Utils.objectHasProperty(self._sourcesById, source.id)) {
331
+ const src = Object.values(self._sourcesById)[0];
332
+
333
+ throw new Error('peaks.sources.add(): duplicate id. \n\n Source I = ' +
334
+ JSON.stringify(
335
+ src.toSerializable()
336
+ ) + ' Source II = ' +
337
+ JSON.stringify(
338
+ source.toSerializable()
339
+ )
340
+ );
341
+ }
342
+
343
+ return source;
344
+ });
345
+
346
+ sources.forEach(function(source) {
347
+ self._addSource(source);
348
+ });
349
+
350
+ this._peaks.emit('handler.sources.add', sources);
351
+
352
+ return sources;
353
+ };
354
+
355
+ /**
356
+ * Returns the indexes of sources that match the given predicate.
357
+ *
358
+ * @private
359
+ * @param {Function} predicate Predicate function to find matching sources.
360
+ * @returns {Array<Number>} An array of indexes into the sources array of
361
+ * the matching elements.
362
+ */
363
+
364
+ SourceHandler.prototype._findSource = function(predicate) {
365
+ var indexes = [];
366
+
367
+ for (var i = 0, length = this._sources.length; i < length; i++) {
368
+ if (predicate(this._sources[i])) {
369
+ indexes.push(i);
370
+ }
371
+ }
372
+
373
+ return indexes;
374
+ };
375
+
376
+ /**
377
+ * destroys the sources at the given array indexes.
378
+ *
379
+ * @private
380
+ * @param {Array<Number>} indexes The array indexes to destroy.
381
+ * @returns {Array<Source>} The destroyd {@link Source} objects.
382
+ */
383
+
384
+ SourceHandler.prototype._destroyIndexes = function(indexes) {
385
+ var destroyed = [];
386
+
387
+ for (var i = 0; i < indexes.length; i++) {
388
+ var index = indexes[i] - destroyed.length;
389
+
390
+ var itemDestroyed = this._sources.splice(index, 1)[0];
391
+
392
+ delete this._sourcesById[itemDestroyed.id];
393
+ delete this._sourcesByLineId[itemDestroyed.lineId][itemDestroyed.id];
394
+
395
+ destroyed.push(itemDestroyed);
396
+ }
397
+
398
+ return destroyed;
399
+ };
400
+
401
+ /**
402
+ * destroys all sources that match a given predicate function.
403
+ *
404
+ * After removing the sources, this function also emits a
405
+ * <code>sources.destroy</code> event with the destroyed {@link Source}
406
+ * objects.
407
+ *
408
+ * @private
409
+ * @param {Function} predicate A predicate function that identifies which
410
+ * sources to destroy.
411
+ * @returns {Array<Source>} The destroyed {@link Source} objects.
412
+ */
413
+
414
+ SourceHandler.prototype._destroySources = function(predicate) {
415
+ var indexes = this._findSource(predicate);
416
+
417
+ var destroyed = this._destroyIndexes(indexes);
418
+
419
+ this._peaks.emit('handler.sources.destroy', destroyed);
420
+
421
+ return destroyed;
422
+ };
423
+
424
+ /**
425
+ * destroys the given source.
426
+ *
427
+ * @param {Source} Source The source to destroy.
428
+ * @returns {Array<Source>} The destroyd source.
429
+ */
430
+
431
+ SourceHandler.prototype.destroy = function(source) {
432
+ return this._destroySources(function(s) {
433
+ return s === source;
434
+ });
435
+ };
436
+
437
+ /**
438
+ * destroys any sources with the given id.
439
+ *
440
+ * @param {String} id
441
+ * @returns {Array<Source>} The destroyed {@link Source} objects.
442
+ */
443
+
444
+ SourceHandler.prototype.destroyById = function(sourceId) {
445
+ return this._destroySources(function(source) {
446
+ return source.id === sourceId;
447
+ });
448
+ };
449
+
450
+ /**
451
+ * destroys any sources with the given start time, and optional end time.
452
+ *
453
+ * @param {Number} startTime sources with this start time are destroyd.
454
+ * @param {Number?} endTime If present, only sources with both the given
455
+ * start time and end time are destroyd.
456
+ * @returns {Array<Source>} The destroyd {@link Source} objects.
457
+ */
458
+
459
+ SourceHandler.prototype.destroyByTime = function(startTime, endTime) {
460
+ var fnFilter;
461
+
462
+ if (endTime > 0) {
463
+ fnFilter = function(source) {
464
+ return source.startTime === startTime && source.endTime === endTime;
465
+ };
466
+ }
467
+ else {
468
+ fnFilter = function(source) {
469
+ return source.startTime === startTime;
470
+ };
471
+ }
472
+
473
+ return this._destroySources(fnFilter);
474
+ };
475
+
476
+ /**
477
+ * destroys all sources.
478
+ */
479
+
480
+ SourceHandler.prototype.destroyAll = function() {
481
+ this._destroySources(function() {
482
+ return true;
483
+ });
484
+ };
485
+
486
+ /**
487
+ * Set the wrapping of the source with the given ID to false.
488
+ * The source will be shown in its complete form.
489
+ *
490
+ * @param {String} sourceId The id of the source.
491
+ */
492
+
493
+ SourceHandler.prototype.showById = function(sourceId) {
494
+ if (this._sourcesById[sourceId].wrapped === false) {
495
+ return;
496
+ }
497
+
498
+ this._sourcesById[sourceId].wrapped = false;
499
+
500
+ this._peaks.emit('handler.sources.show', [this._sourcesById[sourceId]]);
501
+ };
502
+
503
+ /**
504
+ * Set the wrapping of the source with the given ID to true.
505
+ * The source will be shown in its reduced form.
506
+ *
507
+ * @param {String} sourceId The id of the source.
508
+ */
509
+
510
+ SourceHandler.prototype.hideById = function(sourceId) {
511
+ if (this._sourcesById[sourceId].wrapped === true) {
512
+ return;
513
+ }
514
+
515
+ this._sourcesById[sourceId].wrapped = true;
516
+
517
+ this._peaks.emit('handler.sources.hide', [this._sourcesById[sourceId]]);
518
+ };
519
+
520
+ return SourceHandler;
521
+ });