@checksub_team/peaks_timeline 1.4.17

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.
@@ -0,0 +1,155 @@
1
+ /**
2
+ * @file
3
+ *
4
+ * Defines the {@link SegmentMarker} class.
5
+ *
6
+ * @module segment-marker
7
+ */
8
+
9
+ define([
10
+ 'konva'
11
+ ], function(Konva) {
12
+ 'use strict';
13
+
14
+ /**
15
+ * Parameters for the {@link SegmentMarker} constructor.
16
+ *
17
+ * @typedef {Object} SegmentMarkerOptions
18
+ * @global
19
+ * @property {Segment} segment
20
+ * @property {SegmentShape} segmentShape
21
+ * @property {Boolean} draggable If true, marker is draggable.
22
+ * @property {Boolean} startMarker If <code>true</code>, the marker indicates
23
+ * the start time of the segment. If <code>false</code>, the marker
24
+ * indicates the end time of the segment.
25
+ * @property {Function} onDrag
26
+ * @property {Function} onDragStart
27
+ * @property {Function} onDragEnd
28
+ */
29
+
30
+ /**
31
+ * Creates a Left or Right side segment handle marker.
32
+ *
33
+ * @class
34
+ * @alias SegmentMarker
35
+ *
36
+ * @param {SegmentMarkerOptions} options
37
+ */
38
+
39
+ function SegmentMarker(options) {
40
+ this._segment = options.segment;
41
+ this._marker = options.marker;
42
+ this._segmentShape = options.segmentShape;
43
+ this._draggable = options.draggable;
44
+ this._startMarker = options.startMarker;
45
+
46
+ this._onDrag = options.onDrag;
47
+ this._onDragStart = options.onDragStart;
48
+ this._onDragEnd = options.onDragEnd;
49
+
50
+ this._view = options.view;
51
+
52
+ this._dragBoundFunc = this._dragBoundFunc.bind(this);
53
+
54
+ this._group = new Konva.Group({
55
+ draggable: this._draggable,
56
+ dragBoundFunc: this._dragBoundFunc
57
+ });
58
+
59
+ this._bindDefaultEventHandlers();
60
+
61
+ this._marker.init(this._group);
62
+
63
+ if (this._startMarker) {
64
+ this._group.x(this._view.timeToPixels(this._segment.startTime));
65
+ }
66
+ else {
67
+ this._group.x(this._view.timeToPixels(this._segment.endTime));
68
+ }
69
+ }
70
+
71
+ SegmentMarker.prototype._bindDefaultEventHandlers = function() {
72
+ var self = this;
73
+
74
+ if (self._draggable) {
75
+ self._group.on('dragmove', function() {
76
+ self._onDrag(self);
77
+ });
78
+
79
+ self._group.on('dragstart', function() {
80
+ self._onDragStart(self);
81
+ });
82
+
83
+ self._group.on('dragend', function() {
84
+ self._onDragEnd(self);
85
+ });
86
+ }
87
+ };
88
+
89
+ SegmentMarker.prototype._dragBoundFunc = function(pos) {
90
+ if (this._startMarker) {
91
+ this._segmentShape.getSegmentsGroup().updateSegment(
92
+ this._segment,
93
+ pos.x + this._view.getFrameOffset(),
94
+ null
95
+ );
96
+ }
97
+ else {
98
+ this._segmentShape.getSegmentsGroup().updateSegment(
99
+ this._segment,
100
+ null,
101
+ pos.x + this._view.getFrameOffset() + this._marker.getHandleWidth()
102
+ );
103
+ }
104
+
105
+ return {
106
+ x: this._group.getAbsolutePosition().x,
107
+ y: this._group.getAbsolutePosition().y
108
+ };
109
+ };
110
+
111
+ SegmentMarker.prototype.addToGroup = function(group) {
112
+ group.add(this._group);
113
+ };
114
+
115
+ SegmentMarker.prototype.getSegment = function() {
116
+ return this._segment;
117
+ };
118
+
119
+ SegmentMarker.prototype.getX = function() {
120
+ return this._group.getX();
121
+ };
122
+
123
+ SegmentMarker.prototype.getWidth = function() {
124
+ return this._group.getWidth();
125
+ };
126
+
127
+ SegmentMarker.prototype.getHandleWidth = function() {
128
+ return this._marker.getWidth();
129
+ };
130
+
131
+ SegmentMarker.prototype.setHandleWidth = function(value) {
132
+ this._marker.setWidth(value);
133
+ };
134
+
135
+ SegmentMarker.prototype.isStartMarker = function() {
136
+ return this._startMarker;
137
+ };
138
+
139
+ SegmentMarker.prototype.setX = function(x) {
140
+ this._group.setX(x);
141
+ };
142
+
143
+ SegmentMarker.prototype.timeUpdated = function(time) {
144
+ if (this._marker.timeUpdated) {
145
+ this._marker.timeUpdated(time);
146
+ }
147
+ };
148
+
149
+ SegmentMarker.prototype.destroy = function() {
150
+ this._group.destroyChildren();
151
+ this._group.destroy();
152
+ };
153
+
154
+ return SegmentMarker;
155
+ });
@@ -0,0 +1,345 @@
1
+ /**
2
+ * @file
3
+ *
4
+ * Defines the {@link SegmentShape} class.
5
+ *
6
+ * @module segment-shape
7
+ */
8
+
9
+ define([
10
+ 'konva',
11
+ './segment-marker'
12
+ ], function(
13
+ Konva,
14
+ SegmentMarker) {
15
+ 'use strict';
16
+
17
+ var SEGMENT_WIDTH = 10;
18
+
19
+ /**
20
+ * Creates a waveform segment shape with optional start and end markers.
21
+ *
22
+ * @class
23
+ * @alias SegmentShape
24
+ *
25
+ * @param {Segment} segment
26
+ * @param {Peaks} peaks
27
+ * @param {SegmentsGroup} group
28
+ * @param {WaveformOverview|WaveformZoomView} view
29
+ */
30
+
31
+ function SegmentShape(segment, peaks, group, view) {
32
+ this._segment = segment;
33
+ this._peaks = peaks;
34
+ this._group = group;
35
+ this._view = view;
36
+ // this._waveformShape = null;
37
+ this._segmentsGroup = null;
38
+ this._label = null;
39
+ this._startMarker = null;
40
+ this._endMarker = null;
41
+ this._rectangle = null;
42
+
43
+ this._segmentHeight = this._peaks.options.segmentHeight;
44
+
45
+ this._rectangle = new Konva.Rect({
46
+ x: this._view.timeToPixels(this._segment.startTime),
47
+ y: 0,
48
+ width: this._view.timeToPixels(this._segment.endTime - this._segment.startTime),
49
+ height: this._segmentHeight,
50
+ cornerRadius: 10,
51
+ fill: segment.color + Math.round(segment.opacity * 255).toString(16),
52
+ stroke: segment.textColor + 'FF',
53
+ strokeWidth: 1,
54
+ draggable: true
55
+ });
56
+
57
+ var self = this;
58
+
59
+ this._rectangle.dragBoundFunc(function() {
60
+ var diff = self._view.getPointerPosition().x - self._mouseDownX;
61
+
62
+ self._group.updateSegment(
63
+ self._segment,
64
+ self._initialStartPixel + diff, self._initialEndPixel + diff
65
+ );
66
+
67
+ self._startMarker.timeUpdated(self._segment.startTime);
68
+ self._endMarker.timeUpdated(self._segment.endTime);
69
+
70
+ return {
71
+ x: this.absolutePosition().x,
72
+ y: this.absolutePosition().y
73
+ };
74
+ });
75
+
76
+ // Set up event handlers to show/hide the segment label text when the user
77
+ // hovers the mouse over the segment.
78
+ this._rectangle.on('mouseenter', this._onMouseEnter.bind(this));
79
+ this._rectangle.on('mouseleave', this._onMouseLeave.bind(this));
80
+ this._rectangle.on('click', this._onClick.bind(this));
81
+
82
+ this._rectangle.on('dragstart', this._onSegmentDragStart.bind(this));
83
+ this._rectangle.on('dragend', this._onSegmentDragEnd.bind(this));
84
+
85
+ // Event handlers for markers
86
+ this._onSegmentHandleDrag = this._onSegmentHandleDrag.bind(this);
87
+ this._onSegmentHandleDragStart = this._onSegmentHandleDragStart.bind(this);
88
+ this._onSegmentHandleDragEnd = this._onSegmentHandleDragEnd.bind(this);
89
+
90
+ this._label = this._peaks.options.createSegmentLabel({
91
+ x: this._view.timeToPixels(segment.startTime),
92
+ y: this._segmentY,
93
+ width: this._view.timeToPixels(this._segment.endTime - this._segment.startTime),
94
+ height: this._segmentHeight,
95
+ segment: segment,
96
+ view: this._view.getName(),
97
+ group: this._group,
98
+ fontSize: 12
99
+ });
100
+
101
+ this._createMarkers();
102
+ }
103
+
104
+ SegmentShape.prototype.update = function() {
105
+ var startPixel = this._view.timeToPixels(this._segment.startTime);
106
+ var endPixel = this._view.timeToPixels(this._segment.endTime);
107
+ var frameOffset = this._view.timeToPixels(this._view.getTimeOffset());
108
+
109
+ this._rectangle.x(startPixel - frameOffset);
110
+
111
+ this._rectangle.width(endPixel - startPixel);
112
+
113
+ var newWidth = Math.floor(this._rectangle.width() / 2);
114
+
115
+ if (this._rectangle.width()
116
+ < this._startMarker.getHandleWidth() + this._endMarker.getHandleWidth()) {
117
+ this._startMarker.setHandleWidth(newWidth);
118
+ this._endMarker.setHandleWidth(newWidth);
119
+ }
120
+ else if (this._startMarker.getHandleWidth() < SEGMENT_WIDTH
121
+ && newWidth > this._startMarker.getHandleWidth()) {
122
+ this._startMarker.setHandleWidth(Math.min(newWidth, SEGMENT_WIDTH));
123
+ this._endMarker.setHandleWidth(Math.min(newWidth, SEGMENT_WIDTH));
124
+ }
125
+
126
+ if (this._startMarker) {
127
+ this._startMarker.setX(startPixel - frameOffset);
128
+ this._startMarker.timeUpdated(this._segment.startTime);
129
+ }
130
+
131
+ if (this._endMarker) {
132
+ this._endMarker.setX(endPixel - this._endMarker.getHandleWidth() - frameOffset);
133
+ this._endMarker.timeUpdated(this._segment.endTime);
134
+ }
135
+
136
+ if (this._label) {
137
+ this._label.setX(startPixel - frameOffset);
138
+ this._label.setWidth(endPixel - startPixel);
139
+ }
140
+ };
141
+
142
+ SegmentShape.prototype.getSegmentHeight = function() {
143
+ return this._segmentHeight;
144
+ };
145
+
146
+ SegmentShape.prototype.getText = function() {
147
+ return this._label;
148
+ };
149
+
150
+ SegmentShape.prototype.getSegment = function() {
151
+ return this._segment;
152
+ };
153
+
154
+ SegmentShape.prototype.getSegmentsGroup = function() {
155
+ return this._segmentsGroup;
156
+ };
157
+
158
+ SegmentShape.prototype.getStartMarker = function() {
159
+ return this._startMarker;
160
+ };
161
+
162
+ SegmentShape.prototype.getEndMarker = function() {
163
+ return this._endMarker;
164
+ };
165
+
166
+ SegmentShape.prototype.addToGroup = function(group, segmentsGroup) {
167
+ if (segmentsGroup) {
168
+ this._segmentsGroup = segmentsGroup;
169
+ }
170
+
171
+ group.add(this._rectangle);
172
+
173
+ if (this._label) {
174
+ group.add(this._label);
175
+ }
176
+
177
+ if (this._startMarker) {
178
+ this._startMarker.addToGroup(group);
179
+ }
180
+
181
+ if (this._endMarker) {
182
+ this._endMarker.addToGroup(group);
183
+ }
184
+ };
185
+
186
+ SegmentShape.prototype._createMarkers = function() {
187
+ var editable = this._group.isEditingEnabled() && this._segment.editable;
188
+
189
+ var startMarker = this._peaks.options.createSegmentMarker({
190
+ peaks: this._peaks,
191
+ segment: this._segment,
192
+ draggable: editable,
193
+ startMarker: true,
194
+ group: this._group,
195
+ view: this._view,
196
+ showSegmentMarkers: this._peaks.options.showSegmentMarkers,
197
+ segmentHeight: this._segmentHeight,
198
+ segmentWidth: SEGMENT_WIDTH,
199
+ y: this._segmentY
200
+ });
201
+
202
+ if (startMarker) {
203
+ this._startMarker = new SegmentMarker({
204
+ segment: this._segment,
205
+ segmentShape: this,
206
+ draggable: editable,
207
+ startMarker: true,
208
+ marker: startMarker,
209
+ onDrag: this._onSegmentHandleDrag,
210
+ onDragStart: this._onSegmentHandleDragStart,
211
+ onDragEnd: this._onSegmentHandleDragEnd,
212
+ view: this._view
213
+ });
214
+ }
215
+
216
+ var endMarker = this._peaks.options.createSegmentMarker({
217
+ peaks: this._peaks,
218
+ segment: this._segment,
219
+ draggable: editable,
220
+ startMarker: false,
221
+ group: this._group,
222
+ view: this._view,
223
+ showSegmentMarkers: this._peaks.options.showSegmentMarkers,
224
+ segmentHeight: this._segmentHeight,
225
+ segmentWidth: SEGMENT_WIDTH,
226
+ y: this._segmentY
227
+ });
228
+
229
+ if (endMarker) {
230
+ this._endMarker = new SegmentMarker({
231
+ segment: this._segment,
232
+ segmentShape: this,
233
+ draggable: editable,
234
+ startMarker: false,
235
+ marker: endMarker,
236
+ onDrag: this._onSegmentHandleDrag,
237
+ onDragStart: this._onSegmentHandleDragStart,
238
+ onDragEnd: this._onSegmentHandleDragEnd,
239
+ view: this._view
240
+ });
241
+ }
242
+ };
243
+
244
+ SegmentShape.prototype._onMouseEnter = function() {
245
+ this._view.setCursor('pointer');
246
+
247
+ this._rectangle.fill(this._segment.selectedColor + Math.round(
248
+ this._segment.opacity * 255
249
+ ).toString(16));
250
+
251
+ this._view.drawSourcesLayer();
252
+
253
+ this._peaks.emit('segments.mouseenter', this._segment);
254
+ };
255
+
256
+ SegmentShape.prototype._onMouseLeave = function() {
257
+ this._view.setCursor('default');
258
+
259
+ this._rectangle.fill(this._segment.color + Math.round(
260
+ this._segment.opacity * 255
261
+ ).toString(16));
262
+
263
+ this._view.drawSourcesLayer();
264
+
265
+ this._peaks.emit('segments.mouseleave', this._segment);
266
+ };
267
+
268
+ SegmentShape.prototype._onClick = function() {
269
+ this._peaks.emit('segments.click', this._segment);
270
+ };
271
+
272
+ SegmentShape.prototype._onSegmentDragStart = function() {
273
+ this._view.setCursor('grab');
274
+
275
+ this._mouseDownX = this._view.getPointerPosition().x;
276
+ this._initialStartTime = this._segment.startTime;
277
+ this._initialStartPixel = this._view.timeToPixels(this._initialStartTime);
278
+ this._initialEndTime = this._segment.endTime;
279
+ this._initialEndPixel = this._view.timeToPixels(this._initialEndTime);
280
+
281
+ this._peaks.emit('segments.dragstart', this._segment);
282
+ };
283
+
284
+ SegmentShape.prototype._onSegmentDragEnd = function() {
285
+ this._view.setCursor('pointer');
286
+
287
+ this._peaks.emit('segments.dragend', this._segment);
288
+ };
289
+
290
+ /**
291
+ * @param {SegmentMarker} segmentMarker
292
+ */
293
+
294
+ SegmentShape.prototype._onSegmentHandleDrag = function() {
295
+ this._peaks.emit('segments.dragged');
296
+ };
297
+
298
+ /**
299
+ * @param {SegmentMarker} segmentMarker
300
+ */
301
+
302
+ SegmentShape.prototype._onSegmentHandleDragStart = function(segmentMarker) {
303
+ var startMarker = segmentMarker.isStartMarker();
304
+
305
+ this._peaks.emit('segments.dragstart', this._segment, startMarker);
306
+ };
307
+
308
+ /**
309
+ * @param {SegmentMarker} segmentMarker
310
+ */
311
+
312
+ SegmentShape.prototype._onSegmentHandleDragEnd = function(segmentMarker) {
313
+ var startMarker = segmentMarker.isStartMarker();
314
+
315
+ this._peaks.emit('segments.dragend', this._segment, startMarker);
316
+ };
317
+
318
+ SegmentShape.prototype.fitToView = function() {
319
+ if (this._startMarker) {
320
+ this._startMarker.fitToView();
321
+ }
322
+
323
+ if (this._endMarker) {
324
+ this._endMarker.fitToView();
325
+ }
326
+ };
327
+
328
+ SegmentShape.prototype.destroy = function() {
329
+ this._rectangle.destroy();
330
+
331
+ if (this._label) {
332
+ this._label.destroy();
333
+ }
334
+
335
+ if (this._startMarker) {
336
+ this._startMarker.destroy();
337
+ }
338
+
339
+ if (this._endMarker) {
340
+ this._endMarker.destroy();
341
+ }
342
+ };
343
+
344
+ return SegmentShape;
345
+ });