@checksub_team/peaks_timeline 1.16.0-alpha.0 → 1.16.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.
@@ -118,16 +118,6 @@ define([
118
118
  this.draw();
119
119
  };
120
120
 
121
- LineIndicator.prototype._showMenu = function(menu) {
122
- menu.style.display = 'block';
123
- var containerRect = this._stage.container().getBoundingClientRect();
124
-
125
- menu.style.top = containerRect.top
126
- + this._stage.getPointerPosition().y
127
- - menu.offsetHeight + 'px';
128
- menu.style.left = containerRect.left + this._stage.getPointerPosition().x + 6 + 'px';
129
- };
130
-
131
121
  LineIndicator.prototype._createIndicator = function(line, type, text) {
132
122
  var indicator = new Konva.Group();
133
123
  var indicatorHeight = 0;
package/src/line.js CHANGED
@@ -7,9 +7,10 @@
7
7
  */
8
8
 
9
9
  define([
10
+ './source-group',
10
11
  'konva',
11
12
  './utils'
12
- ], function(Konva, Utils) {
13
+ ], function(SourceGroup, Konva, Utils) {
13
14
  'use strict';
14
15
 
15
16
  function Line(peaks, view, y, id, position) {
@@ -51,10 +52,10 @@ define([
51
52
  return this._id;
52
53
  };
53
54
 
54
- Line.prototype.isEmpty = function() {
55
+ Line.prototype.countRemainingElements = function() {
55
56
  return this.isSegmentsLine() ?
56
- this._segmentsGroup.isEmpty() :
57
- Object.keys(this._sources).length === 0;
57
+ this._segmentsGroup.countRemainingElements() :
58
+ Object.keys(this._sources).length;
58
59
  };
59
60
 
60
61
  Line.prototype.isSegmentsLine = function() {
@@ -93,87 +94,71 @@ define([
93
94
  return this._height;
94
95
  };
95
96
 
96
- Line.prototype.changeHeight = function(from, to) {
97
- if (this._sourceHeights[from]) {
98
- var oldHeight = this._height;
99
-
100
- if (this._sourceHeights[to]) {
101
- this._sourceHeights[to] += this._sourceHeights[from];
102
- }
103
- else {
104
- this._sourceHeights[to] = this._sourceHeights[from];
97
+ Line.prototype._addHeight = function(height) {
98
+ if (this._sourceHeights[height]) {
99
+ this._sourceHeights[height]++;
100
+ }
101
+ else {
102
+ this._sourceHeights[height] = 1;
103
+ if (height > this._height) {
104
+ this._height = height;
105
105
  }
106
+ }
107
+ };
106
108
 
107
- if (to > this._height) {
108
- this._height = to;
109
- }
110
- else if (from === this._height) {
109
+ Line.prototype._subtractHeight = function(height) {
110
+ if (Object.keys(this._sources).length === 0) {
111
+ this._height = this._peaks.options.emptyLineHeight;
112
+ this._sourceHeights = {};
113
+ }
114
+ else {
115
+ this._sourceHeights[height]--;
116
+
117
+ if (this._sourceHeights[height] === 0
118
+ && height === this._height) {
119
+ delete this._sourceHeights[height];
111
120
  this._height = 0;
112
- for (var height in this._sourceHeights) {
113
- if (Utils.objectHasProperty(this._sourceHeights, height)) {
114
- var parsedHeight = parseInt(height, 10);
121
+ for (var sourceHeight in this._sourceHeights) {
122
+ if (Utils.objectHasProperty(this._sourceHeights, sourceHeight)) {
123
+ var parsedHeight = parseInt(sourceHeight, 10);
115
124
 
116
- if (parsedHeight !== from) {
117
- if (parsedHeight > this._height) {
118
- this._height = parsedHeight;
119
- }
125
+ if (parsedHeight > this._height) {
126
+ this._height = parsedHeight;
120
127
  }
121
128
  }
122
129
  }
123
130
  }
124
-
125
- if (this._height !== oldHeight) {
126
- this._peaks.emit('line.heightChanged', this._position);
127
- }
128
-
129
- delete this._sourceHeights[from];
130
131
  }
131
132
  };
132
133
 
133
- Line.prototype.updateLineHeight = function(sourceGroup, action) {
134
+ Line.prototype.updateLineHeight = function(source, action) {
134
135
  var oldHeight = this._height;
136
+ var sourceGroup = this._sourcesGroup[source.id];
135
137
  var sourceGroupHeight;
136
138
 
137
139
  switch (action) {
138
140
  case 'add':
139
- sourceGroupHeight = sourceGroup.getCurrentHeight();
141
+ sourceGroupHeight = sourceGroup ?
142
+ sourceGroup.getCurrentHeight() :
143
+ SourceGroup.getHeights(source, this._peaks).current;
140
144
 
141
- if (this._sourceHeights[sourceGroupHeight]) {
142
- this._sourceHeights[sourceGroupHeight]++;
143
- }
144
- else {
145
- this._sourceHeights[sourceGroupHeight] = 1;
146
- if (sourceGroupHeight > this._height) {
147
- this._height = sourceGroupHeight;
148
- }
149
- }
145
+ this._addHeight(sourceGroupHeight);
150
146
  break;
151
147
  case 'remove':
152
- if (Object.keys(this._sources).length === 0) {
153
- this._height = this._peaks.options.emptyLineHeight;
154
- this._sourceHeights = {};
155
- }
156
- else {
157
- sourceGroupHeight = sourceGroup.getCurrentHeight();
148
+ sourceGroupHeight = sourceGroup ?
149
+ sourceGroup.getCurrentHeight() :
150
+ SourceGroup.getHeights(source, this._peaks).current;
158
151
 
159
- this._sourceHeights[sourceGroupHeight]--;
160
-
161
- if (this._sourceHeights[sourceGroupHeight] === 0
162
- && sourceGroupHeight === this._height) {
163
- delete this._sourceHeights[sourceGroupHeight];
164
- this._height = 0;
165
- for (var height in this._sourceHeights) {
166
- if (Utils.objectHasProperty(this._sourceHeights, height)) {
167
- var parsedHeight = parseInt(height, 10);
168
-
169
- if (parsedHeight > this._height) {
170
- this._height = parsedHeight;
171
- }
172
- }
173
- }
174
- }
175
- }
152
+ this._subtractHeight(sourceGroupHeight);
176
153
  break;
154
+ default:
155
+ // wrappingChanged
156
+ var { unwrapped, wrapped } = sourceGroup ?
157
+ sourceGroup.getHeights() :
158
+ SourceGroup.getHeights(source, this._peaks);
159
+
160
+ this._addHeight(source.wrapped ? wrapped : unwrapped);
161
+ this._subtractHeight(source.wrapped ? unwrapped : wrapped);
177
162
  }
178
163
 
179
164
  if (this._height !== oldHeight) {
@@ -190,10 +175,16 @@ define([
190
175
  layer.add(this._group);
191
176
  };
192
177
 
193
- Line.prototype.addSourceGroup = function(sourceGroup) {
194
- var source = sourceGroup.getSource();
195
-
196
- this._sourcesGroup[source.id] = sourceGroup;
178
+ /**
179
+ * Adds a source to the line.
180
+ * @param {Source} source - The source to add.
181
+ * @param {SourceGroup} sourceGroup - The source group of the source (optional).
182
+ * @returns {void}
183
+ */
184
+ Line.prototype.addSourceGroup = function(source, sourceGroup) {
185
+ if (sourceGroup) {
186
+ this._sourcesGroup[source.id] = sourceGroup;
187
+ }
197
188
 
198
189
  if (!this._sources[source.id]) {
199
190
  var newSource = {
@@ -218,15 +209,15 @@ define([
218
209
  ? this._sources[currentSource.prevSourceId].source.endTime
219
210
  : 0;
220
211
 
221
- var newTimes = this._canBePlacedBetween(
212
+ const { newStartTime, newEndTime } = this._canBePlacedBetween(
222
213
  source.startTime,
223
214
  source.endTime,
224
215
  startLimit,
225
216
  currentSource.source.startTime
226
217
  );
227
218
 
228
- if (newTimes) {
229
- source.updateTimes(newTimes.start, newTimes.end);
219
+ if (newStartTime !== null && newEndTime !== null) {
220
+ source.updateTimes(newStartTime, newEndTime);
230
221
 
231
222
  if (currentSource.prevSourceId) {
232
223
  this._sources[currentSource.prevSourceId].nextSourceId = source.id;
@@ -265,10 +256,10 @@ define([
265
256
  this._sources[source.id] = newSource;
266
257
  }
267
258
 
268
- this.updateLineHeight(sourceGroup, 'add');
259
+ this.updateLineHeight(source, 'add');
269
260
  }
270
261
 
271
- if (!sourceGroup.getParent() || !sourceGroup.isDescendantOf(this._group)) {
262
+ if (sourceGroup && (!sourceGroup.getParent() || !sourceGroup.isDescendantOf(this._group))) {
272
263
  sourceGroup.addToGroup(this._group);
273
264
  }
274
265
  };
@@ -295,32 +286,26 @@ define([
295
286
 
296
287
  Line.prototype._canBePlacedBetween = function(startTime, endTime, startLimit, endLimit) {
297
288
  var timeWidth = Utils.roundTime(endTime - startTime);
298
- var newTimes = null;
289
+ var newStartTime, newEndTime;
299
290
 
300
291
  if ((!endLimit && startTime > startLimit) || (startTime > startLimit && endTime < endLimit)) {
301
292
  // Can be placed at its wanted position with wanted start/end time
302
- newTimes = {
303
- start: startTime,
304
- end: endTime
305
- };
293
+ newStartTime = startTime;
294
+ newEndTime = endTime;
306
295
  }
307
296
  else if (Utils.roundTime(endLimit - startLimit) >= timeWidth) {
308
297
  // Can be placed at its wanted position but not with its wanted start/end time
309
298
  if (startTime > startLimit) {
310
- newTimes = {
311
- start: Utils.roundTime(endLimit - timeWidth),
312
- end: endLimit
313
- };
299
+ newStartTime = Utils.roundTime(endLimit - timeWidth);
300
+ newEndTime = endLimit;
314
301
  }
315
302
  else {
316
- newTimes = {
317
- start: startLimit,
318
- end: Utils.roundTime(startLimit + timeWidth)
319
- };
303
+ newStartTime = startLimit;
304
+ newEndTime = Utils.roundTime(startLimit + timeWidth);
320
305
  }
321
306
  }
322
307
 
323
- return newTimes;
308
+ return { newStartTime, newEndTime };
324
309
  };
325
310
 
326
311
  Line.prototype.removeSourceGroup = function(source, isPermanent) {
@@ -354,7 +339,7 @@ define([
354
339
  this._firstSourceId = sourceData.nextSourceId;
355
340
  }
356
341
 
357
- this.updateLineHeight(sourceGroup, 'remove');
342
+ this.updateLineHeight(source, 'remove');
358
343
  }
359
344
 
360
345
  return sourceGroup;
@@ -384,126 +369,116 @@ define([
384
369
  this._group.y(this._group.y() + dy);
385
370
  };
386
371
 
387
- Line.prototype.manageSourceOrder = function(source, newTimes) {
388
- var cursorTime = this._view.pixelsToTime(this._view.getPointerPosition().x);
372
+ Line.prototype.manageOrder = function(sources, startTime, endTime) {
373
+ const firstSource = sources[0];
374
+ const lastSource = sources[sources.length - 1];
375
+ const cursorTime = this._view.pixelsToTime(this._view.getPointerPosition().x);
376
+ var newStartTime = startTime;
377
+ var newEndTime = endTime;
389
378
  var tmpTimes;
390
379
 
391
- var sourceDuration = source.endTime - source.startTime;
380
+ var sourceDuration = Utils.roundTime(endTime - startTime);
392
381
 
393
- if (newTimes.start !== null && newTimes.end !== null) {
394
- if (this._sources[source.id].prevSourceId) {
382
+ if (typeof newStartTime === 'number' && typeof newEndTime === 'number') {
383
+ if (this._sources[firstSource.id].prevSourceId) {
395
384
  // there is another source to the left
396
- var previousStartTime = this._sources[this._sources[source.id].prevSourceId].source.startTime;
385
+ var previousStartTime = this._sources[this._sources[firstSource.id].prevSourceId].source.startTime;
397
386
 
398
- if (cursorTime + this._view.getTimeOffset() < previousStartTime) {
387
+ if (Utils.roundTime(cursorTime + this._view.getTimeOffset()) < previousStartTime) {
399
388
  // we want to change order
400
- tmpTimes = this._changeSourcePosition(
401
- source,
389
+ tmpTimes = this._changeSourcesPosition(
390
+ sources,
402
391
  sourceDuration,
403
392
  cursorTime + this._view.getTimeOffset()
404
393
  );
405
394
 
406
- if (tmpTimes) {
407
- newTimes.start = tmpTimes.start;
408
- newTimes.end = tmpTimes.end;
395
+ if (typeof tmpTimes.newStartTime === 'number' && typeof tmpTimes.newEndTime === 'number') {
396
+ newStartTime = tmpTimes.newStartTime;
397
+ newEndTime = tmpTimes.newEndTime;
409
398
  }
410
399
  }
411
400
  }
412
401
 
413
- if (this._sources[source.id].nextSourceId) {
402
+ if (this._sources[lastSource.id].nextSourceId) {
414
403
  // there is another source to the right
415
- var nextEndTime = this._sources[this._sources[source.id].nextSourceId].source.endTime;
404
+ var nextEndTime = this._sources[this._sources[lastSource.id].nextSourceId].source.endTime;
416
405
 
417
- if (cursorTime + this._view.getTimeOffset() > nextEndTime) {
406
+ if (Utils.roundTime(cursorTime + this._view.getTimeOffset()) > nextEndTime) {
418
407
  // we want to change order
419
- tmpTimes = this._changeSourcePosition(
420
- source,
408
+ tmpTimes = this._changeSourcesPosition(
409
+ sources,
421
410
  sourceDuration,
422
411
  cursorTime + this._view.getTimeOffset()
423
412
  );
424
413
 
425
- if (tmpTimes) {
426
- newTimes.start = tmpTimes.start;
427
- newTimes.end = tmpTimes.end;
414
+ if (typeof tmpTimes.newStartTime === 'number' && typeof tmpTimes.newEndTime === 'number') {
415
+ newStartTime = tmpTimes.newStartTime;
416
+ newEndTime = tmpTimes.newEndTime;
428
417
  }
429
418
  }
430
419
  }
431
420
  }
432
421
 
433
- if (newTimes.start) {
434
- newTimes.start = Utils.roundTime(newTimes.start);
435
- }
436
- if (newTimes.end) {
437
- newTimes.end = Utils.roundTime(newTimes.end);
438
- }
439
-
440
- return newTimes;
422
+ return { newStartTime, newEndTime };
441
423
  };
442
424
 
443
- Line.prototype._changeSourcePosition = function(source, sourceDuration, time) {
444
- if (source.orderable && this._firstSourceId) {
445
- var currentRange = {
446
- start: null,
447
- end: null
448
- };
449
- var startLimit = null;
450
- var endLimit = null;
425
+ Line.prototype._changeSourcesPosition = function(sources, sourceDuration, time) {
426
+ var currentRange = {
427
+ start: null,
428
+ end: null
429
+ };
430
+ var startLimit = null;
431
+ var endLimit = null;
451
432
 
452
- do {
453
- if (!currentRange.end) {
454
- currentRange.end = this._sources[this._firstSourceId];
455
- }
456
- else {
457
- currentRange.start = currentRange.end;
458
- currentRange.end = this._sources[currentRange.start.nextSourceId];
459
- }
433
+ let newStartTime, newEndTime;
460
434
 
461
- if (currentRange.start) {
462
- startLimit = currentRange.start.source.endTime;
463
- }
464
- else {
465
- startLimit = 0;
466
- }
435
+ do {
436
+ if (!currentRange.end) {
437
+ currentRange.end = this._sources[this._firstSourceId];
438
+ }
439
+ else {
440
+ currentRange.start = currentRange.end;
441
+ currentRange.end = this._sources[currentRange.start.nextSourceId];
442
+ }
467
443
 
468
- if (currentRange.end) {
469
- endLimit = currentRange.end.source.startTime;
470
- }
471
- else {
472
- endLimit = null;
473
- }
444
+ if (currentRange.start) {
445
+ startLimit = currentRange.start.source.endTime;
446
+ }
447
+ else {
448
+ startLimit = 0;
449
+ }
474
450
 
475
- if (time > startLimit && (endLimit === null || time < endLimit)) {
476
- var newTimes = this._canBePlacedBetween(
477
- time,
478
- time + sourceDuration,
479
- startLimit,
480
- endLimit
481
- );
451
+ if (currentRange.end) {
452
+ endLimit = currentRange.end.source.startTime;
453
+ }
454
+ else {
455
+ endLimit = null;
456
+ }
482
457
 
483
- if (newTimes) {
484
- if (newTimes.start) {
485
- newTimes.start = Utils.roundTime(newTimes.start);
486
- }
487
- if (newTimes.end) {
488
- newTimes.end = Utils.roundTime(newTimes.end);
489
- }
458
+ if (time > startLimit && (endLimit === null || time < endLimit)) {
459
+ ({ newStartTime, newEndTime } = this._canBePlacedBetween(
460
+ time,
461
+ time + sourceDuration,
462
+ startLimit,
463
+ endLimit
464
+ ));
490
465
 
491
- source.updateTimes(
492
- newTimes.start,
493
- newTimes.end
494
- );
495
- var prevSourceId = currentRange.start
496
- ? currentRange.start.source.id
497
- : null;
466
+ if (typeof newStartTime === 'number' && typeof newEndTime === 'number') {
467
+ let prevSourceId = currentRange.start
468
+ ? currentRange.start.source.id
469
+ : null;
498
470
 
471
+ sources.forEach(function(source) {
499
472
  this._moveSource(this._sources[source.id].source, prevSourceId);
500
- return newTimes;
501
- }
473
+ prevSourceId = source.id;
474
+ }.bind(this));
502
475
  }
503
- } while (currentRange.end);
504
- }
505
476
 
506
- return null;
477
+ return { newStartTime, newEndTime };
478
+ }
479
+ } while (currentRange.end);
480
+
481
+ return { newStartTime, newEndTime };
507
482
  };
508
483
 
509
484
  Line.prototype._moveSource = function(source, prevSourceId) {
@@ -544,26 +519,21 @@ define([
544
519
  this._sources[source.id] = sourceObj;
545
520
  };
546
521
 
547
- Line.prototype.manageCollision = function(source, newTimes) {
548
- var originalStartTime = null;
549
- var originalEndTime = null;
550
- var newStartTime = null;
551
- var newEndTime = null;
522
+ Line.prototype.manageCollision = function(sources, newStartTime, newEndTime) {
523
+ var originalStartTime = newStartTime;
524
+ var originalEndTime = newEndTime;
552
525
  var startLimited = false;
553
526
  var endLimited = false;
527
+ const firstSource = sources[0];
528
+ const lastSource = sources[sources.length - 1];
554
529
 
555
- newTimes.updateTimelineLength = false;
556
-
557
- if (newTimes.start !== null) {
530
+ if (typeof newStartTime === 'number') {
558
531
  // startMarker changed
559
- originalStartTime = newTimes.start;
560
- newStartTime = originalStartTime;
561
-
562
- if (source.startTime > newStartTime) {
532
+ if (firstSource.startTime > newStartTime) {
563
533
  // startMarker moved to the left
564
- if (this._sources[source.id].prevSourceId) {
534
+ if (this._sources[firstSource.id].prevSourceId) {
565
535
  // there is another source to the left
566
- var previousSource = this._sources[this._sources[source.id].prevSourceId]
536
+ var previousSource = this._sources[this._sources[firstSource.id].prevSourceId]
567
537
  .source;
568
538
 
569
539
  if (newStartTime < previousSource.endTime) {
@@ -581,16 +551,13 @@ define([
581
551
  }
582
552
  }
583
553
 
584
- if (newTimes.end !== null) {
554
+ if (typeof newEndTime === 'number') {
585
555
  // endMarker changed
586
- originalEndTime = newTimes.end;
587
- newEndTime = originalEndTime;
588
-
589
- if (source.endTime < newEndTime) {
556
+ if (lastSource.endTime < newEndTime) {
590
557
  // endMarker moved to the right
591
- if (this._sources[source.id].nextSourceId) {
558
+ if (this._sources[lastSource.id].nextSourceId) {
592
559
  // there is another source to the right
593
- var nextSource = this._sources[this._sources[source.id].nextSourceId]
560
+ var nextSource = this._sources[this._sources[lastSource.id].nextSourceId]
594
561
  .source;
595
562
 
596
563
  if (newEndTime > nextSource.startTime) {
@@ -603,7 +570,7 @@ define([
603
570
  }
604
571
 
605
572
  // Update the other edge if dragging and collision
606
- if (newStartTime !== null && newEndTime !== null) {
573
+ if (typeof newStartTime === 'number' && typeof newEndTime === 'number') {
607
574
  var timeWidth = originalEndTime - originalStartTime;
608
575
 
609
576
  if (startLimited) {
@@ -616,40 +583,29 @@ define([
616
583
  }
617
584
 
618
585
  // Check for minimal size of source
619
- if (newStartTime !== null && newEndTime === null) {
620
- if (Utils.roundTime(source.endTime - newStartTime) < source.minSize) {
621
- newStartTime = Utils.roundTime(source.endTime - source.minSize);
586
+ // We assume that only 1 source can be resized at a time
587
+ if (typeof newStartTime === 'number' && typeof newEndTime !== 'number') {
588
+ if (Utils.roundTime(sources[0].endTime - newStartTime) < sources[0].minSize) {
589
+ newStartTime = Utils.roundTime(sources[0].endTime - sources[0].minSize);
622
590
  }
623
591
  }
624
- else if (newEndTime !== null && newStartTime === null) {
625
- if (Utils.roundTime(newEndTime - source.startTime) < source.minSize) {
626
- newEndTime = Utils.roundTime(source.startTime + source.minSize);
592
+ else if (typeof newEndTime === 'number' && typeof newStartTime !== 'number') {
593
+ if (Utils.roundTime(newEndTime - sources[0].startTime) < sources[0].minSize) {
594
+ newEndTime = Utils.roundTime(sources[0].startTime + sources[0].minSize);
627
595
  }
628
596
  }
629
597
  else {
630
- if (Utils.roundTime(newEndTime - newStartTime) < source.minSize) {
631
- if (source.endTime !== newEndTime) {
632
- newEndTime = Utils.roundTime(newStartTime + source.minSize);
598
+ if (Utils.roundTime(newEndTime - newStartTime) < sources[0].minSize) {
599
+ if (sources[0].endTime !== newEndTime) {
600
+ newEndTime = Utils.roundTime(newStartTime + sources[0].minSize);
633
601
  }
634
- if (source.startTime !== newStartTime) {
635
- newStartTime = Utils.roundTime(newEndTime - source.minSize);
602
+ if (sources[0].startTime !== newStartTime) {
603
+ newStartTime = Utils.roundTime(newEndTime - sources[0].minSize);
636
604
  }
637
605
  }
638
606
  }
639
607
 
640
- if (newStartTime !== null && newStartTime !== originalStartTime) {
641
- newTimes.start = newStartTime;
642
- }
643
-
644
- if (newEndTime !== null && newEndTime !== originalEndTime) {
645
- newTimes.end = newEndTime;
646
-
647
- if (this._sources[source.id].nextSourceId === null) {
648
- newTimes.updateTimelineLength = true;
649
- }
650
- }
651
-
652
- return newTimes;
608
+ return { newStartTime, newEndTime };
653
609
  };
654
610
 
655
611
  Line.prototype.getSourcesAfter = function(time) {