@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.
- package/package.json +1 -1
- package/peaks.js +4716 -4409
- package/peaks.js.d.ts +5 -5
- package/src/{timeline-axis.js → components/axis.js} +244 -244
- package/src/{data-retriever.js → components/data-retriever.js} +117 -117
- package/src/{default-segment-marker.js → components/default-segment-marker.js} +132 -132
- package/src/{invoker.js → components/invoker.js} +81 -81
- package/src/components/line-group.js +692 -0
- package/src/components/line-groups.js +585 -0
- package/src/{line-indicator.js → components/line-indicator.js} +308 -303
- package/src/{marker-factories.js → components/marker-factories.js} +1 -1
- package/src/{mode-layer.js → components/mode-layer.js} +8 -12
- package/src/{playhead-layer.js → components/playhead-layer.js} +3 -3
- package/src/{segment-marker.js → components/segment-marker.js} +2 -2
- package/src/{segment-shape.js → components/segment-shape.js} +508 -508
- package/src/{segments-group.js → components/segments-group.js} +805 -801
- package/src/{source-group.js → components/source-group.js} +1661 -1640
- package/src/{sources-layer.js → components/sources-layer.js} +716 -730
- package/src/{waveform-builder.js → components/waveform-builder.js} +2 -2
- package/src/{waveform-shape.js → components/waveform-shape.js} +214 -214
- package/src/keyboard-handler.js +9 -9
- package/src/line-handler.js +179 -0
- package/src/main.js +110 -71
- package/src/models/line.js +156 -0
- package/src/{segment.js → models/segment.js} +420 -419
- package/src/{source.js → models/source.js} +1311 -1315
- package/src/player.js +2 -2
- package/src/{timeline-segments.js → segment-handler.js} +435 -435
- package/src/{timeline-sources.js → source-handler.js} +521 -514
- package/src/utils.js +5 -1
- package/src/{timeline-zoomview.js → view.js} +136 -137
- package/src/line.js +0 -690
- package/src/lines.js +0 -427
- /package/src/{data.js → components/data.js} +0 -0
- /package/src/{loader.js → components/loader.js} +0 -0
- /package/src/{mouse-drag-handler.js → components/mouse-drag-handler.js} +0 -0
- /package/src/{svgs.js → components/svgs.js} +0 -0
|
@@ -1,435 +1,435 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file
|
|
3
|
-
*
|
|
4
|
-
* Defines the {@link
|
|
5
|
-
*
|
|
6
|
-
* @module
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
define([
|
|
10
|
-
'colors.css',
|
|
11
|
-
'./segment',
|
|
12
|
-
'./utils'
|
|
13
|
-
], function(Colors, Segment, Utils) {
|
|
14
|
-
'use strict';
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Segment parameters.
|
|
18
|
-
*
|
|
19
|
-
* @typedef {Object} SegmentOptions
|
|
20
|
-
* @global
|
|
21
|
-
* @property {Number} startTime Segment start time, in seconds.
|
|
22
|
-
* @property {Number} endTime Segment end time, in seconds.
|
|
23
|
-
* @property {Boolean=} editable If <code>true</code> the segment start and
|
|
24
|
-
* end times can be adjusted via the user interface.
|
|
25
|
-
* Default: <code>false</code>.
|
|
26
|
-
* @property {String=} color Segment waveform color.
|
|
27
|
-
* Default: a random color.
|
|
28
|
-
* @property {String=} labelText Segment label text.
|
|
29
|
-
* Default: an empty string.
|
|
30
|
-
* @property {String=} id A unique segment identifier.
|
|
31
|
-
* Default: an automatically generated identifier.
|
|
32
|
-
*/
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Handles all functionality related to the adding, removing and manipulation
|
|
36
|
-
* of segments.
|
|
37
|
-
*
|
|
38
|
-
* @class
|
|
39
|
-
* @alias
|
|
40
|
-
*
|
|
41
|
-
* @param {Peaks} peaks The parent Peaks object.
|
|
42
|
-
*/
|
|
43
|
-
|
|
44
|
-
function
|
|
45
|
-
this._peaks = peaks;
|
|
46
|
-
this._segments = [];
|
|
47
|
-
this._segmentsById = {};
|
|
48
|
-
this._segmentIdCounter = 0;
|
|
49
|
-
this._colorIndex = 0;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Returns a new unique segment id value.
|
|
54
|
-
*
|
|
55
|
-
* @private
|
|
56
|
-
* @returns {String}
|
|
57
|
-
*/
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
return 'peaks.segment.' + this._segmentIdCounter++;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
var colors = [
|
|
64
|
-
Colors.navy,
|
|
65
|
-
Colors.blue,
|
|
66
|
-
Colors.aqua,
|
|
67
|
-
Colors.teal,
|
|
68
|
-
// Colors.olive,
|
|
69
|
-
// Colors.lime,
|
|
70
|
-
// Colors.green,
|
|
71
|
-
Colors.yellow,
|
|
72
|
-
Colors.orange,
|
|
73
|
-
Colors.red,
|
|
74
|
-
Colors.maroon,
|
|
75
|
-
Colors.fuchsia,
|
|
76
|
-
Colors.purple
|
|
77
|
-
// Colors.black,
|
|
78
|
-
// Colors.gray,
|
|
79
|
-
// Colors.silver
|
|
80
|
-
];
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* @private
|
|
84
|
-
* @returns {String}
|
|
85
|
-
*/
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
if (this._peaks.options.randomizeSegmentColor) {
|
|
89
|
-
if (++this._colorIndex === colors.length) {
|
|
90
|
-
this._colorIndex = 0;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return colors[this._colorIndex];
|
|
94
|
-
}
|
|
95
|
-
else {
|
|
96
|
-
return this._peaks.options.segmentColor;
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Adds a new segment object.
|
|
102
|
-
*
|
|
103
|
-
* @private
|
|
104
|
-
* @param {Segment} segment
|
|
105
|
-
*/
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
this._segments.push(segment);
|
|
109
|
-
|
|
110
|
-
this._segmentsById[segment.id] = segment;
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Creates a new segment object.
|
|
115
|
-
*
|
|
116
|
-
* @private
|
|
117
|
-
* @param {SegmentOptions} options
|
|
118
|
-
* @return {Segment}
|
|
119
|
-
*/
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
// Watch for anyone still trying to use the old
|
|
123
|
-
// createSegment(startTime, endTime, ...) API
|
|
124
|
-
if (!Utils.isObject(options)) {
|
|
125
|
-
throw new TypeError('peaks.segments.add(): expected a Segment object parameter');
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
var segment = new Segment(
|
|
129
|
-
this._peaks,
|
|
130
|
-
Utils.isNullOrUndefined(options.id) ? this._getNextSegmentId() : options.id,
|
|
131
|
-
options.startTime,
|
|
132
|
-
options.endTime,
|
|
133
|
-
options.duration,
|
|
134
|
-
options.labelText,
|
|
135
|
-
options.color || this._getSegmentColor(),
|
|
136
|
-
options.textColor || '#000000',
|
|
137
|
-
options.handleTextColor || '#000000',
|
|
138
|
-
options.hoverColor,
|
|
139
|
-
options.warningColor,
|
|
140
|
-
options.opacity || 1,
|
|
141
|
-
options.borderColor,
|
|
142
|
-
options.borderWidth,
|
|
143
|
-
options.borderRadius,
|
|
144
|
-
options.editable,
|
|
145
|
-
options.allowDeletion || false,
|
|
146
|
-
options.line,
|
|
147
|
-
options.indicators
|
|
148
|
-
);
|
|
149
|
-
|
|
150
|
-
return segment;
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Returns all segments.
|
|
155
|
-
*
|
|
156
|
-
* @returns {Array<Segment>}
|
|
157
|
-
*/
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
return this._segments;
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Returns all segments, serialized to a plain object.
|
|
165
|
-
*
|
|
166
|
-
* @returns {Array<Object>}
|
|
167
|
-
*/
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
return this._segments.map(function(segment) {
|
|
171
|
-
return segment.toSerializable();
|
|
172
|
-
});
|
|
173
|
-
};
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Add segments to the given line so they can be displayed.
|
|
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
|
-
return segment;
|
|
264
|
-
});
|
|
265
|
-
|
|
266
|
-
segments.forEach(function(segment) {
|
|
267
|
-
self._addSegment(segment);
|
|
268
|
-
});
|
|
269
|
-
|
|
270
|
-
this.refreshRelativeIds();
|
|
271
|
-
|
|
272
|
-
this._peaks.emit('segments.add', segments);
|
|
273
|
-
};
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* Set relative ids for all segments.
|
|
277
|
-
*
|
|
278
|
-
* @private
|
|
279
|
-
*/
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
var idsByPosition = {};
|
|
283
|
-
|
|
284
|
-
this._segments.sort(function(a, b) {
|
|
285
|
-
return a.startTime - b.startTime;
|
|
286
|
-
}).forEach(function(segment) {
|
|
287
|
-
segment.relativeId = (idsByPosition[segment.line] || 0) + 1;
|
|
288
|
-
idsByPosition[segment.line] = segment.relativeId;
|
|
289
|
-
});
|
|
290
|
-
|
|
291
|
-
this._peaks.emit('segments.relative_ids_refreshed');
|
|
292
|
-
};
|
|
293
|
-
|
|
294
|
-
/**
|
|
295
|
-
* Returns the indexes of segments that match the given predicate.
|
|
296
|
-
*
|
|
297
|
-
* @private
|
|
298
|
-
* @param {Function} predicate Predicate function to find matching segments.
|
|
299
|
-
* @returns {Array<Number>} An array of indexes into the segments array of
|
|
300
|
-
* the matching elements.
|
|
301
|
-
*/
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
var indexes = [];
|
|
305
|
-
|
|
306
|
-
for (var i = 0, length = this._segments.length; i < length; i++) {
|
|
307
|
-
if (predicate(this._segments[i])) {
|
|
308
|
-
indexes.push(i);
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
return indexes;
|
|
313
|
-
};
|
|
314
|
-
|
|
315
|
-
/**
|
|
316
|
-
* Removes the segments at the given array indexes.
|
|
317
|
-
*
|
|
318
|
-
* @private
|
|
319
|
-
* @param {Array<Number>} indexes The array indexes to remove.
|
|
320
|
-
* @returns {Array<Segment>} The removed {@link Segment} objects.
|
|
321
|
-
*/
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
var removed = [];
|
|
325
|
-
|
|
326
|
-
for (var i = 0; i < indexes.length; i++) {
|
|
327
|
-
var index = indexes[i] - removed.length;
|
|
328
|
-
|
|
329
|
-
var itemRemoved = this._segments.splice(index, 1)[0];
|
|
330
|
-
|
|
331
|
-
delete this._segmentsById[itemRemoved.id];
|
|
332
|
-
|
|
333
|
-
removed.push(itemRemoved);
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
return removed;
|
|
337
|
-
};
|
|
338
|
-
|
|
339
|
-
/**
|
|
340
|
-
* Removes all segments that match a given predicate function.
|
|
341
|
-
*
|
|
342
|
-
* After removing the segments, this function also emits a
|
|
343
|
-
* <code>segments.remove</code> event with the removed {@link Segment}
|
|
344
|
-
* objects.
|
|
345
|
-
*
|
|
346
|
-
* @private
|
|
347
|
-
* @param {Function} predicate A predicate function that identifies which
|
|
348
|
-
* segments to remove.
|
|
349
|
-
* @returns {Array<Segment>} The removed {@link Segment} objects.
|
|
350
|
-
*/
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
var indexes = this._findSegment(predicate);
|
|
354
|
-
|
|
355
|
-
var removed = this._removeIndexes(indexes);
|
|
356
|
-
|
|
357
|
-
this.refreshRelativeIds();
|
|
358
|
-
|
|
359
|
-
this._peaks.emit('segments.remove', removed);
|
|
360
|
-
|
|
361
|
-
return removed;
|
|
362
|
-
};
|
|
363
|
-
|
|
364
|
-
/**
|
|
365
|
-
* Removes the given segment.
|
|
366
|
-
*
|
|
367
|
-
* @param {Segment} segment The segment to remove.
|
|
368
|
-
* @returns {Array<Segment>} The removed segment.
|
|
369
|
-
*/
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
return this._removeSegments(function(s) {
|
|
373
|
-
return s === segment;
|
|
374
|
-
});
|
|
375
|
-
};
|
|
376
|
-
|
|
377
|
-
/**
|
|
378
|
-
* Removes any segments with the given id.
|
|
379
|
-
*
|
|
380
|
-
* @param {String} id
|
|
381
|
-
* @returns {Array<Segment>} The removed {@link Segment} objects.
|
|
382
|
-
*/
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
return this._removeSegments(function(segment) {
|
|
386
|
-
return segment.id === segmentId;
|
|
387
|
-
});
|
|
388
|
-
};
|
|
389
|
-
|
|
390
|
-
/**
|
|
391
|
-
* Removes any segments with the given start time, and optional end time.
|
|
392
|
-
*
|
|
393
|
-
* @param {Number} startTime Segments with this start time are removed.
|
|
394
|
-
* @param {Number?} endTime If present, only segments with both the given
|
|
395
|
-
* start time and end time are removed.
|
|
396
|
-
* @returns {Array<Segment>} The removed {@link Segment} objects.
|
|
397
|
-
*/
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
endTime = (typeof endTime === 'number') ? endTime : 0;
|
|
401
|
-
|
|
402
|
-
var fnFilter;
|
|
403
|
-
|
|
404
|
-
if (endTime > 0) {
|
|
405
|
-
fnFilter = function(segment) {
|
|
406
|
-
return segment.startTime === startTime && segment.endTime === endTime;
|
|
407
|
-
};
|
|
408
|
-
}
|
|
409
|
-
else {
|
|
410
|
-
fnFilter = function(segment) {
|
|
411
|
-
return segment.startTime === startTime;
|
|
412
|
-
};
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
return this._removeSegments(fnFilter);
|
|
416
|
-
};
|
|
417
|
-
|
|
418
|
-
/**
|
|
419
|
-
* Removes all segments.
|
|
420
|
-
*
|
|
421
|
-
* After removing the segments, this function emits a
|
|
422
|
-
* <code>segments.remove_all</code> event.
|
|
423
|
-
*/
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
var indexes = this._findSegment(function(segment) {
|
|
427
|
-
return segment.line === lineId;
|
|
428
|
-
});
|
|
429
|
-
|
|
430
|
-
this._removeIndexes(indexes);
|
|
431
|
-
this._peaks.emit('segments.remove_all', lineId);
|
|
432
|
-
};
|
|
433
|
-
|
|
434
|
-
return
|
|
435
|
-
});
|
|
1
|
+
/**
|
|
2
|
+
* @file
|
|
3
|
+
*
|
|
4
|
+
* Defines the {@link SegmentHandler} class.
|
|
5
|
+
*
|
|
6
|
+
* @module segment-handler
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
define([
|
|
10
|
+
'colors.css',
|
|
11
|
+
'./models/segment',
|
|
12
|
+
'./utils'
|
|
13
|
+
], function(Colors, Segment, Utils) {
|
|
14
|
+
'use strict';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Segment parameters.
|
|
18
|
+
*
|
|
19
|
+
* @typedef {Object} SegmentOptions
|
|
20
|
+
* @global
|
|
21
|
+
* @property {Number} startTime Segment start time, in seconds.
|
|
22
|
+
* @property {Number} endTime Segment end time, in seconds.
|
|
23
|
+
* @property {Boolean=} editable If <code>true</code> the segment start and
|
|
24
|
+
* end times can be adjusted via the user interface.
|
|
25
|
+
* Default: <code>false</code>.
|
|
26
|
+
* @property {String=} color Segment waveform color.
|
|
27
|
+
* Default: a random color.
|
|
28
|
+
* @property {String=} labelText Segment label text.
|
|
29
|
+
* Default: an empty string.
|
|
30
|
+
* @property {String=} id A unique segment identifier.
|
|
31
|
+
* Default: an automatically generated identifier.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Handles all functionality related to the adding, removing and manipulation
|
|
36
|
+
* of segments.
|
|
37
|
+
*
|
|
38
|
+
* @class
|
|
39
|
+
* @alias SegmentHandler
|
|
40
|
+
*
|
|
41
|
+
* @param {Peaks} peaks The parent Peaks object.
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
function SegmentHandler(peaks) {
|
|
45
|
+
this._peaks = peaks;
|
|
46
|
+
this._segments = [];
|
|
47
|
+
this._segmentsById = {};
|
|
48
|
+
this._segmentIdCounter = 0;
|
|
49
|
+
this._colorIndex = 0;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Returns a new unique segment id value.
|
|
54
|
+
*
|
|
55
|
+
* @private
|
|
56
|
+
* @returns {String}
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
SegmentHandler.prototype._getNextSegmentId = function() {
|
|
60
|
+
return 'peaks.segment.' + this._segmentIdCounter++;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
var colors = [
|
|
64
|
+
Colors.navy,
|
|
65
|
+
Colors.blue,
|
|
66
|
+
Colors.aqua,
|
|
67
|
+
Colors.teal,
|
|
68
|
+
// Colors.olive,
|
|
69
|
+
// Colors.lime,
|
|
70
|
+
// Colors.green,
|
|
71
|
+
Colors.yellow,
|
|
72
|
+
Colors.orange,
|
|
73
|
+
Colors.red,
|
|
74
|
+
Colors.maroon,
|
|
75
|
+
Colors.fuchsia,
|
|
76
|
+
Colors.purple
|
|
77
|
+
// Colors.black,
|
|
78
|
+
// Colors.gray,
|
|
79
|
+
// Colors.silver
|
|
80
|
+
];
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @private
|
|
84
|
+
* @returns {String}
|
|
85
|
+
*/
|
|
86
|
+
|
|
87
|
+
SegmentHandler.prototype._getSegmentColor = function() {
|
|
88
|
+
if (this._peaks.options.randomizeSegmentColor) {
|
|
89
|
+
if (++this._colorIndex === colors.length) {
|
|
90
|
+
this._colorIndex = 0;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return colors[this._colorIndex];
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
return this._peaks.options.segmentColor;
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Adds a new segment object.
|
|
102
|
+
*
|
|
103
|
+
* @private
|
|
104
|
+
* @param {Segment} segment
|
|
105
|
+
*/
|
|
106
|
+
|
|
107
|
+
SegmentHandler.prototype._addSegment = function(segment) {
|
|
108
|
+
this._segments.push(segment);
|
|
109
|
+
|
|
110
|
+
this._segmentsById[segment.id] = segment;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Creates a new segment object.
|
|
115
|
+
*
|
|
116
|
+
* @private
|
|
117
|
+
* @param {SegmentOptions} options
|
|
118
|
+
* @return {Segment}
|
|
119
|
+
*/
|
|
120
|
+
|
|
121
|
+
SegmentHandler.prototype._createSegment = function(options) {
|
|
122
|
+
// Watch for anyone still trying to use the old
|
|
123
|
+
// createSegment(startTime, endTime, ...) API
|
|
124
|
+
if (!Utils.isObject(options)) {
|
|
125
|
+
throw new TypeError('peaks.segments.add(): expected a Segment object parameter');
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
var segment = new Segment(
|
|
129
|
+
this._peaks,
|
|
130
|
+
Utils.isNullOrUndefined(options.id) ? this._getNextSegmentId() : options.id,
|
|
131
|
+
options.startTime,
|
|
132
|
+
options.endTime,
|
|
133
|
+
options.duration,
|
|
134
|
+
options.labelText,
|
|
135
|
+
options.color || this._getSegmentColor(),
|
|
136
|
+
options.textColor || '#000000',
|
|
137
|
+
options.handleTextColor || '#000000',
|
|
138
|
+
options.hoverColor,
|
|
139
|
+
options.warningColor,
|
|
140
|
+
options.opacity || 1,
|
|
141
|
+
options.borderColor,
|
|
142
|
+
options.borderWidth,
|
|
143
|
+
options.borderRadius,
|
|
144
|
+
options.editable,
|
|
145
|
+
options.allowDeletion || false,
|
|
146
|
+
options.line,
|
|
147
|
+
options.indicators
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
return segment;
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Returns all segments.
|
|
155
|
+
*
|
|
156
|
+
* @returns {Array<Segment>}
|
|
157
|
+
*/
|
|
158
|
+
|
|
159
|
+
SegmentHandler.prototype.getSegments = function() {
|
|
160
|
+
return this._segments;
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Returns all segments, serialized to a plain object.
|
|
165
|
+
*
|
|
166
|
+
* @returns {Array<Object>}
|
|
167
|
+
*/
|
|
168
|
+
|
|
169
|
+
SegmentHandler.prototype.getSegmentsSerialized = function() {
|
|
170
|
+
return this._segments.map(function(segment) {
|
|
171
|
+
return segment.toSerializable();
|
|
172
|
+
});
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Add segments to the given line so they can be displayed.
|
|
177
|
+
*/
|
|
178
|
+
|
|
179
|
+
SegmentHandler.prototype.addSegmentsToLine = function(segmentsGroupId, lineId) {
|
|
180
|
+
if (
|
|
181
|
+
!Utils.isString(lineId) ||
|
|
182
|
+
Utils.isNullOrUndefined(this._peaks.lineHandler.getLine(lineId))
|
|
183
|
+
) {
|
|
184
|
+
throw new Error('peaks.segmentHandler.addSegmentsToLine(): line with id ' + lineId +
|
|
185
|
+
' does not exist');
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
this._peaks.emit('handler.segments.show', segmentsGroupId, lineId);
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Returns the segment with the given id, or <code>null</code> if not found.
|
|
193
|
+
*
|
|
194
|
+
* @param {String} id
|
|
195
|
+
* @returns {Segment|null}
|
|
196
|
+
*/
|
|
197
|
+
|
|
198
|
+
SegmentHandler.prototype.getSegment = function(id) {
|
|
199
|
+
return this._segmentsById[id] || null;
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Returns all segments that overlap a given point in time.
|
|
204
|
+
*
|
|
205
|
+
* @param {Number} time
|
|
206
|
+
* @returns {Array<Segment>}
|
|
207
|
+
*/
|
|
208
|
+
|
|
209
|
+
SegmentHandler.prototype.getSegmentsAtTime = function(time) {
|
|
210
|
+
return this._segments.filter(function(segment) {
|
|
211
|
+
return time >= segment.startTime && time < segment.endTime;
|
|
212
|
+
});
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
SegmentHandler.prototype.setMagnetizing = function(bool) {
|
|
216
|
+
this._peaks.emit('handler.segments.setMagnetizing', bool);
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Returns all segments that overlap a given time region.
|
|
221
|
+
*
|
|
222
|
+
* @param {Number} startTime The start of the time region, in seconds.
|
|
223
|
+
* @param {Number} endTime The end of the time region, in seconds.
|
|
224
|
+
*
|
|
225
|
+
* @returns {Array<Segment>}
|
|
226
|
+
*/
|
|
227
|
+
|
|
228
|
+
SegmentHandler.prototype.find = function(startTime, endTime) {
|
|
229
|
+
return this._segments.filter(function(segment) {
|
|
230
|
+
return segment.isVisible(startTime, endTime);
|
|
231
|
+
});
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Adds one or more segments to the timeline.
|
|
236
|
+
*
|
|
237
|
+
* @param {SegmentOptions|Array<SegmentOptions>} segmentOrSegments
|
|
238
|
+
*/
|
|
239
|
+
|
|
240
|
+
SegmentHandler.prototype.add = function(/* segmentOrSegments */) {
|
|
241
|
+
var self = this;
|
|
242
|
+
|
|
243
|
+
var segments = Array.isArray(arguments[0]) ?
|
|
244
|
+
arguments[0] :
|
|
245
|
+
Array.prototype.slice.call(arguments);
|
|
246
|
+
|
|
247
|
+
segments = segments.map(function(segmentOptions) {
|
|
248
|
+
var segment = self._createSegment(segmentOptions);
|
|
249
|
+
|
|
250
|
+
if (Utils.objectHasProperty(self._segmentsById, segment.id)) {
|
|
251
|
+
var seg = Object.values(self._segmentsById)[0];
|
|
252
|
+
|
|
253
|
+
throw new Error('peaks.segments.add(): duplicate id. \n\n Segment I = ' +
|
|
254
|
+
JSON.stringify(
|
|
255
|
+
seg.toSerializable()
|
|
256
|
+
) + ' Segment II = ' +
|
|
257
|
+
JSON.stringify(
|
|
258
|
+
segment.toSerializable()
|
|
259
|
+
)
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
return segment;
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
segments.forEach(function(segment) {
|
|
267
|
+
self._addSegment(segment);
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
this.refreshRelativeIds();
|
|
271
|
+
|
|
272
|
+
this._peaks.emit('handler.segments.add', segments);
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Set relative ids for all segments.
|
|
277
|
+
*
|
|
278
|
+
* @private
|
|
279
|
+
*/
|
|
280
|
+
|
|
281
|
+
SegmentHandler.prototype.refreshRelativeIds = function() {
|
|
282
|
+
var idsByPosition = {};
|
|
283
|
+
|
|
284
|
+
this._segments.sort(function(a, b) {
|
|
285
|
+
return a.startTime - b.startTime;
|
|
286
|
+
}).forEach(function(segment) {
|
|
287
|
+
segment.relativeId = (idsByPosition[segment.line] || 0) + 1;
|
|
288
|
+
idsByPosition[segment.line] = segment.relativeId;
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
this._peaks.emit('handler.segments.relative_ids_refreshed');
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Returns the indexes of segments that match the given predicate.
|
|
296
|
+
*
|
|
297
|
+
* @private
|
|
298
|
+
* @param {Function} predicate Predicate function to find matching segments.
|
|
299
|
+
* @returns {Array<Number>} An array of indexes into the segments array of
|
|
300
|
+
* the matching elements.
|
|
301
|
+
*/
|
|
302
|
+
|
|
303
|
+
SegmentHandler.prototype._findSegment = function(predicate) {
|
|
304
|
+
var indexes = [];
|
|
305
|
+
|
|
306
|
+
for (var i = 0, length = this._segments.length; i < length; i++) {
|
|
307
|
+
if (predicate(this._segments[i])) {
|
|
308
|
+
indexes.push(i);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
return indexes;
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Removes the segments at the given array indexes.
|
|
317
|
+
*
|
|
318
|
+
* @private
|
|
319
|
+
* @param {Array<Number>} indexes The array indexes to remove.
|
|
320
|
+
* @returns {Array<Segment>} The removed {@link Segment} objects.
|
|
321
|
+
*/
|
|
322
|
+
|
|
323
|
+
SegmentHandler.prototype._removeIndexes = function(indexes) {
|
|
324
|
+
var removed = [];
|
|
325
|
+
|
|
326
|
+
for (var i = 0; i < indexes.length; i++) {
|
|
327
|
+
var index = indexes[i] - removed.length;
|
|
328
|
+
|
|
329
|
+
var itemRemoved = this._segments.splice(index, 1)[0];
|
|
330
|
+
|
|
331
|
+
delete this._segmentsById[itemRemoved.id];
|
|
332
|
+
|
|
333
|
+
removed.push(itemRemoved);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
return removed;
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Removes all segments that match a given predicate function.
|
|
341
|
+
*
|
|
342
|
+
* After removing the segments, this function also emits a
|
|
343
|
+
* <code>segments.remove</code> event with the removed {@link Segment}
|
|
344
|
+
* objects.
|
|
345
|
+
*
|
|
346
|
+
* @private
|
|
347
|
+
* @param {Function} predicate A predicate function that identifies which
|
|
348
|
+
* segments to remove.
|
|
349
|
+
* @returns {Array<Segment>} The removed {@link Segment} objects.
|
|
350
|
+
*/
|
|
351
|
+
|
|
352
|
+
SegmentHandler.prototype._removeSegments = function(predicate) {
|
|
353
|
+
var indexes = this._findSegment(predicate);
|
|
354
|
+
|
|
355
|
+
var removed = this._removeIndexes(indexes);
|
|
356
|
+
|
|
357
|
+
this.refreshRelativeIds();
|
|
358
|
+
|
|
359
|
+
this._peaks.emit('handler.segments.remove', removed);
|
|
360
|
+
|
|
361
|
+
return removed;
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Removes the given segment.
|
|
366
|
+
*
|
|
367
|
+
* @param {Segment} segment The segment to remove.
|
|
368
|
+
* @returns {Array<Segment>} The removed segment.
|
|
369
|
+
*/
|
|
370
|
+
|
|
371
|
+
SegmentHandler.prototype.remove = function(segment) {
|
|
372
|
+
return this._removeSegments(function(s) {
|
|
373
|
+
return s === segment;
|
|
374
|
+
});
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Removes any segments with the given id.
|
|
379
|
+
*
|
|
380
|
+
* @param {String} id
|
|
381
|
+
* @returns {Array<Segment>} The removed {@link Segment} objects.
|
|
382
|
+
*/
|
|
383
|
+
|
|
384
|
+
SegmentHandler.prototype.removeById = function(segmentId) {
|
|
385
|
+
return this._removeSegments(function(segment) {
|
|
386
|
+
return segment.id === segmentId;
|
|
387
|
+
});
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Removes any segments with the given start time, and optional end time.
|
|
392
|
+
*
|
|
393
|
+
* @param {Number} startTime Segments with this start time are removed.
|
|
394
|
+
* @param {Number?} endTime If present, only segments with both the given
|
|
395
|
+
* start time and end time are removed.
|
|
396
|
+
* @returns {Array<Segment>} The removed {@link Segment} objects.
|
|
397
|
+
*/
|
|
398
|
+
|
|
399
|
+
SegmentHandler.prototype.removeByTime = function(startTime, endTime) {
|
|
400
|
+
endTime = (typeof endTime === 'number') ? endTime : 0;
|
|
401
|
+
|
|
402
|
+
var fnFilter;
|
|
403
|
+
|
|
404
|
+
if (endTime > 0) {
|
|
405
|
+
fnFilter = function(segment) {
|
|
406
|
+
return segment.startTime === startTime && segment.endTime === endTime;
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
else {
|
|
410
|
+
fnFilter = function(segment) {
|
|
411
|
+
return segment.startTime === startTime;
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
return this._removeSegments(fnFilter);
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Removes all segments.
|
|
420
|
+
*
|
|
421
|
+
* After removing the segments, this function emits a
|
|
422
|
+
* <code>segments.remove_all</code> event.
|
|
423
|
+
*/
|
|
424
|
+
|
|
425
|
+
SegmentHandler.prototype.removeAll = function(lineId) {
|
|
426
|
+
var indexes = this._findSegment(function(segment) {
|
|
427
|
+
return segment.line === lineId;
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
this._removeIndexes(indexes);
|
|
431
|
+
this._peaks.emit('handler.segments.remove_all', lineId);
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
return SegmentHandler;
|
|
435
|
+
});
|