@checksub_team/peaks_timeline 1.16.1 → 2.0.0-alpha.10

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 +4691 -4380
  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 +696 -0
  9. package/src/components/line-groups.js +591 -0
  10. package/src/{line-indicator.js → components/line-indicator.js} +313 -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} +1663 -1640
  18. package/src/{sources-layer.js → components/sources-layer.js} +722 -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 +180 -0
  23. package/src/main.js +109 -82
  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 -143
  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
package/src/main.js CHANGED
@@ -9,22 +9,24 @@
9
9
  define([
10
10
  'colors.css',
11
11
  'eventemitter2',
12
- './timeline-segments',
13
- './timeline-sources',
12
+ './segment-handler',
13
+ './source-handler',
14
+ './line-handler',
14
15
  './keyboard-handler',
15
16
  './player',
16
- './marker-factories',
17
- './timeline-zoomview',
17
+ './view',
18
+ './components/marker-factories',
18
19
  './utils'
19
20
  ], function(
20
21
  Colors,
21
22
  EventEmitter,
22
- TimelineSegments,
23
- TimelineSources,
23
+ SegmentHandler,
24
+ SourceHandler,
25
+ LineHandler,
24
26
  KeyboardHandler,
25
27
  Player,
28
+ View,
26
29
  MarkerFactories,
27
- TimelineZoomView,
28
30
  Utils) {
29
31
  'use strict';
30
32
 
@@ -133,6 +135,11 @@ define([
133
135
  */
134
136
  randomizeSegmentColor: true,
135
137
 
138
+ /**
139
+ * Block mouse clicks if a control key is pressed
140
+ */
141
+ blockUpdatingOnMouseClickWithCtrlKey: false,
142
+
136
143
  /**
137
144
  * Block mouse clicks if a meta key is pressed
138
145
  */
@@ -183,11 +190,7 @@ define([
183
190
  /**
184
191
  *
185
192
  */
186
- template: [
187
- '<div class="timeline">',
188
- '<div class="zoom-container"></div>',
189
- '</div>'
190
- ].join(''),
193
+ template: '<div class="timeline"></div>',
191
194
 
192
195
  /**
193
196
  * An object containing an AudioContext, used when creating waveform data
@@ -204,28 +207,6 @@ define([
204
207
  createSegmentMarker: MarkerFactories.createSegmentMarker,
205
208
  createSegmentLabel: MarkerFactories.createSegmentLabel,
206
209
 
207
- /**
208
- * External sources information.
209
- *
210
- * ```js
211
- * sources: {
212
- * id: 'unique-identifier-for-this-source',
213
- * title: 'Source #1',
214
- * url: 'https://my-website/my-resource.mp3',
215
- * dataUri: {
216
- * arraybuffer: 'url/to/data.dat',
217
- * json: 'url/to/data.json'
218
- * },
219
- * start: 1.03,
220
- * end: 5.06,
221
- * color: #0000ff,
222
- * wrapped: false, //show only a line or peaks/preview
223
- * position: 0 //position in the timeline (here on the first line)
224
- * }
225
- * ```
226
- */
227
- sources: null,
228
-
229
210
  /**
230
211
  * Height of a line, in pixels.
231
212
  * This height will correspond to the height of the background when the element is unwrapped.
@@ -409,7 +390,26 @@ define([
409
390
  * Indicates whether or not sources can be dragged
410
391
  * from one line to another
411
392
  */
412
- canMoveSourcesBetweenLines: true
393
+ canMoveSourcesBetweenLines: true,
394
+
395
+ /**
396
+ * Delay in milliseconds before a new line is created
397
+ * when dragging a source between 2 lines.
398
+ */
399
+ automaticLineCreationDelay: 100,
400
+
401
+ /**
402
+ * Default type of line indicator.
403
+ * Can be 'default', 'volume', 'noVolume', 'visibility', 'noVisibility'.
404
+ * This will be used when a new line is automatically created by a component.
405
+ */
406
+ defaultLineIndicatorType: 'default',
407
+
408
+ /**
409
+ * Default text of the line indicator.
410
+ * This will be used when a new line is automatically created by a component.
411
+ */
412
+ defaultLineIndicatorText: ''
413
413
  };
414
414
 
415
415
  /**
@@ -467,32 +467,31 @@ define([
467
467
  });
468
468
 
469
469
  /*
470
- Setup the layout
470
+ * Setup the layout
471
471
  */
472
- if (!instance.options.containers) {
473
- callback(new TypeError('Peaks.init(): The containers option must be a valid DOM object'));
472
+ if (!instance.options.container) {
473
+ callback(new TypeError('Peaks.init(): The container option must be a valid DOM object'));
474
474
  return;
475
475
  }
476
476
 
477
- var zoomviewContainer = instance.options.containers.zoomview;
478
-
479
- if (!Utils.isHTMLElement(zoomviewContainer)) {
480
- callback(new TypeError('Peaks.init(): The containers options must be valid HTML elements'));
477
+ if (!Utils.isHTMLElement(instance.options.container)) {
478
+ callback(new TypeError('Peaks.init(): The container must be a valid HTML element'));
481
479
  return;
482
480
  }
483
481
 
484
- if (zoomviewContainer && zoomviewContainer.clientWidth <= 0) {
482
+ if (instance.options.container && instance.options.container.clientWidth <= 0) {
485
483
  callback(new TypeError('Peaks.init(): Please ensure that the container is visible and has non-zero width'));
486
484
  return;
487
485
  }
488
486
 
489
487
  instance.player = new Player(instance);
490
- instance.segments = new TimelineSegments(instance);
491
- instance.sources = new TimelineSources(instance);
488
+ instance.segmentHandler = new SegmentHandler(instance);
489
+ instance.sourceHandler = new SourceHandler(instance);
490
+ instance.lineHandler = new LineHandler(instance);
492
491
 
493
492
  // Setup the UI components
494
- instance.view = new TimelineZoomView(
495
- zoomviewContainer,
493
+ instance.view = new View(
494
+ instance.options.container,
496
495
  instance
497
496
  );
498
497
 
@@ -502,10 +501,6 @@ define([
502
501
 
503
502
  instance._addWindowResizeHandler();
504
503
 
505
- if (instance.options.sources) {
506
- instance.sources.add(instance.options.sources);
507
- }
508
-
509
504
  document.fonts.ready.then(function() {
510
505
  setTimeout(function() {
511
506
  instance.emit('peaks.ready');
@@ -646,17 +641,34 @@ define([
646
641
  */
647
642
 
648
643
  Peaks.prototype.addSource = function(source) {
649
- this.sources.add(source);
644
+ this.sourceHandler.add(source);
650
645
  };
651
646
 
652
647
  /**
653
648
  * Destroy a source from the {@link Peaks} instance.
654
649
  *
655
650
  * @param {String} sourceId
651
+ * @param {Boolean} [notify=false] If true, emits a
652
+ * <code>source.destroyed</code> event with the destroyed {@link Source}
653
+ * object.
656
654
  */
657
655
 
658
656
  Peaks.prototype.destroySource = function(sourceId, notify) {
659
- this.sources.destroyById(sourceId, !notify);
657
+ const sources = this.sourceHandler.destroyById(sourceId);
658
+
659
+ if (notify && sources.length > 0) {
660
+ this.emit('source.destroyed', sources[0]);
661
+ }
662
+ };
663
+
664
+ /**
665
+ * Cut a source on the cutting time.
666
+ *
667
+ * @param {Object} sourceToCut The source to cut.
668
+ * @param {Number} cuttingTime The time in seconds where to cut the source.
669
+ */
670
+ Peaks.prototype.cutSource = function(sourceToCut, cuttingTime) {
671
+ this.sourceHandler.cutSource(sourceToCut, cuttingTime);
660
672
  };
661
673
 
662
674
  /**
@@ -666,7 +678,7 @@ define([
666
678
  */
667
679
 
668
680
  Peaks.prototype.showSource = function(sourceId) {
669
- this.sources.showById(sourceId);
681
+ this.sourceHandler.showById(sourceId);
670
682
  };
671
683
 
672
684
  /**
@@ -676,41 +688,65 @@ define([
676
688
  */
677
689
 
678
690
  Peaks.prototype.hideSource = function(sourceId) {
679
- this.sources.hideById(sourceId);
691
+ this.sourceHandler.hideById(sourceId);
680
692
  };
681
693
 
682
- Peaks.prototype.showSegments = function(lineId, position) {
683
- this.segments.addSegmentsToPosition(lineId, position);
694
+ /**
695
+ * Add a new line to the {@link Peaks} instance.
696
+ *
697
+ * @param {LineOptions} lineOptions
698
+ */
699
+ Peaks.prototype.addLine = function(lineOptions, notify) {
700
+ const lines = this.lineHandler.add(lineOptions);
701
+
702
+ if (notify && lines.length > 0) {
703
+ this.emit('line.added', lines[0]);
704
+ }
684
705
  };
685
706
 
686
707
  /**
687
- * Destroy a segment from the {@link Peaks} instance.
708
+ * Destroy a line from the {@link Peaks} instance.
688
709
  *
689
- * @param {String} segmentId
710
+ * @param {String} lineId
690
711
  */
712
+ Peaks.prototype.destroyLine = function(lineId, notify) {
713
+ const lines = this.lineHandler.removeById(lineId);
691
714
 
692
- Peaks.prototype.destroySegment = function(segmentId) {
693
- this.segments.removeById(segmentId);
715
+ if (notify && lines.length > 0) {
716
+ this.emit('line.destroyed', lines[0]);
717
+ }
694
718
  };
695
719
 
696
- Peaks.prototype.setDefaultMode = function() {
697
- this.emit('default_mode');
720
+ /**
721
+ * Move a line to a new position.
722
+ *
723
+ * @param {String} lineId The ID of the line to move.
724
+ * @param {Number} position The new position.
725
+ */
726
+ Peaks.prototype.moveLine = function(lineId, position) {
727
+ this.lineHandler.moveById(lineId, position);
698
728
  };
699
729
 
700
- Peaks.prototype.setCutMode = function() {
701
- this.emit('cut_mode');
730
+ Peaks.prototype.showSegments = function(segmentsGroupId, lineId) {
731
+ this.segmentHandler.addSegmentsToLine(segmentsGroupId, lineId);
702
732
  };
703
733
 
704
- Peaks.prototype.setIndicatorType = function(linePosition, type) {
705
- var lineId = this.view.getLineByPosition(linePosition).getId();
734
+ /**
735
+ * Destroy a segment from the {@link Peaks} instance.
736
+ *
737
+ * @param {String} segmentId
738
+ */
706
739
 
707
- this.emit('lineIndicator.setType', lineId, type);
740
+ Peaks.prototype.destroySegment = function(segmentId) {
741
+ this.segmentHandler.removeById(segmentId);
708
742
  };
709
743
 
710
- Peaks.prototype.setIndicatorText = function(linePosition, text) {
711
- var lineId = this.view.getLineByPosition(linePosition).getId();
744
+ Peaks.prototype.setDefaultMode = function() {
745
+ this.emit('handler.view.defaultmode');
746
+ };
712
747
 
713
- this.emit('lineIndicator.setText', lineId, text);
748
+ Peaks.prototype.setCutMode = function() {
749
+ this.emit('handler.view.cutmode');
714
750
  };
715
751
 
716
752
  Peaks.prototype.getVisibleSegments = function() {
@@ -724,13 +760,11 @@ define([
724
760
  };
725
761
 
726
762
  Peaks.prototype.overrideInteractions = function(bool, areInteractionsAllowed) {
727
- return this.view
728
- .overrideInteractions(bool, areInteractionsAllowed);
763
+ this.emit('main.overrideInteractions', bool, areInteractionsAllowed);
729
764
  };
730
765
 
731
766
  Peaks.prototype.allowInteractions = function(forSources, forSegments) {
732
- return this.view
733
- .allowInteractions(forSources, forSegments);
767
+ this.emit('main.allowInteractions', forSources, forSegments);
734
768
  };
735
769
 
736
770
  Peaks.prototype.getSelectedElements = function() {
@@ -771,13 +805,6 @@ define([
771
805
  window.removeEventListener('resize', this._onResize);
772
806
  };
773
807
 
774
- Peaks.prototype.setLineHeight = function(newLineHeight) {
775
- var oldHeight = this.options.lineHeight;
776
-
777
- this.options.lineHeight = newLineHeight;
778
- this.emit('options.set.line_height', oldHeight);
779
- };
780
-
781
808
  Peaks.prototype.zoomIn = function() {
782
809
  this.view.setZoom(
783
810
  this.view.getTimeToPixelsScale() + Math.floor(this.view.getTimeToPixelsScale() / 10) + 1
@@ -0,0 +1,156 @@
1
+ /**
2
+ * @file
3
+ *
4
+ * Defines the {@link Line} class.
5
+ *
6
+ * @module line
7
+ */
8
+
9
+ define([
10
+ '../utils'
11
+ ], function(Utils) {
12
+ 'use strict';
13
+
14
+ function validateLine(options, context) {
15
+ if (!Utils.isInteger(options.position) || options.position < 0) {
16
+ throw new TypeError('peaks.lines.' + context + ': position must be a non-negative integer');
17
+ }
18
+
19
+ if (Utils.isNullOrUndefined(options.indicatorType)) {
20
+ options.indicatorType = 'default';
21
+ }
22
+ else if (!Utils.isValidIndicatorType(options.indicatorType)) {
23
+ throw new TypeError('peaks.lines.' + context + ': indicatorType must be a valid indicator type');
24
+ }
25
+
26
+ if (Utils.isNullOrUndefined(options.indicatorText)) {
27
+ options.indicatorText = '';
28
+ }
29
+ else if (!Utils.isString(options.indicatorText)) {
30
+ throw new TypeError('peaks.lines.' + context + ': indicatorText must be a string');
31
+ }
32
+
33
+ if (Utils.isNullOrUndefined(options.locked)) {
34
+ options.locked = false;
35
+ }
36
+ else if (!Utils.isBoolean(options.locked)) {
37
+ throw new TypeError('peaks.lines.' + context + ': locked must be a boolean');
38
+ }
39
+ }
40
+
41
+ /**
42
+ * A line is a horizontal line that can be placed on the timeline.
43
+ * It can contain sources or segments.
44
+ *
45
+ * @class
46
+ * @alias Segment
47
+ * @param {Peaks} peaks The parent {@link Peaks} object.
48
+ * @param {String} id A unique identifier for the line.
49
+ * @param {Number} position Position of the line on the timeline.
50
+ * @param {String} indicatorType Type of the line indicator.
51
+ * @param {String} indicatorText Text to display above the line indicator.
52
+ */
53
+
54
+ function Line(peaks, id, position, indicatorType, indicatorText, locked) {
55
+ var opts = {
56
+ position: position,
57
+ indicatorType: indicatorType,
58
+ indicatorText: indicatorText,
59
+ locked: locked
60
+ };
61
+
62
+ validateLine(opts, 'add()');
63
+
64
+ this._peaks = peaks;
65
+ this._id = id;
66
+ this._position = opts.position;
67
+ this._indicatorType = opts.indicatorType;
68
+ this._indicatorText = opts.indicatorText;
69
+ this._locked = opts.locked;
70
+ }
71
+
72
+ Object.defineProperties(Line.prototype, {
73
+ id: {
74
+ enumerable: true,
75
+ get: function() {
76
+ return this._id;
77
+ }
78
+ },
79
+ position: {
80
+ enumerable: true,
81
+ get: function() {
82
+ return this._position;
83
+ },
84
+
85
+ set: function(pos) {
86
+ this._position = pos;
87
+ }
88
+ },
89
+ indicatorType: {
90
+ enumerable: true,
91
+ get: function() {
92
+ return this._indicatorType;
93
+ }
94
+ },
95
+ indicatorText: {
96
+ enumerable: true,
97
+ get: function() {
98
+ return this._indicatorText;
99
+ }
100
+ },
101
+ locked: {
102
+ enumerable: true,
103
+ get: function() {
104
+ return this._locked;
105
+ }
106
+ }
107
+ });
108
+
109
+ Line.prototype.update = function(options) {
110
+ var opts = {
111
+ position: this.position,
112
+ indicatorType: this.indicatorType,
113
+ indicatorText: this.indicatorText,
114
+ locked: this.locked
115
+ };
116
+
117
+ Utils.extend(opts, options);
118
+
119
+ validateLine(opts, 'update()');
120
+
121
+ this._position = opts.position;
122
+ this._indicatorType = opts.indicatorType;
123
+ this._indicatorText = opts.indicatorText;
124
+ this._locked = opts.locked;
125
+
126
+ this._peaks.emit('model.line.update', this);
127
+ };
128
+
129
+ /**
130
+ * Returns a serializable object containing only the properties defined with Object.defineProperties.
131
+ * This includes all enumerable properties that can be safely serialized.
132
+ *
133
+ * @returns {Object} A plain object containing the serializable properties of the line.
134
+ */
135
+ Line.prototype.toSerializable = function() {
136
+ var serializable = {};
137
+
138
+ // Add all the enumerable properties from the prototype
139
+ var proto = Object.getPrototypeOf(this);
140
+ var descriptors = Object.getOwnPropertyDescriptors(proto);
141
+
142
+ for (var prop in descriptors) {
143
+ if (Object.prototype.hasOwnProperty.call(descriptors, prop)) {
144
+ var descriptor = descriptors[prop];
145
+
146
+ if (descriptor.enumerable && descriptor.get && typeof descriptor.get === 'function') {
147
+ serializable[prop] = this[prop];
148
+ }
149
+ }
150
+ }
151
+
152
+ return serializable;
153
+ };
154
+
155
+ return Line;
156
+ });