@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.
package/src/lines.js ADDED
@@ -0,0 +1,356 @@
1
+ /**
2
+ * @file
3
+ *
4
+ * Defines the {@link lines} class.
5
+ *
6
+ * @module lines
7
+ */
8
+
9
+ define([
10
+ './line',
11
+ './line-indicator',
12
+ './utils'
13
+ ], function(
14
+ Line,
15
+ LineIndicator,
16
+ Utils) {
17
+ 'use strict';
18
+
19
+ function Lines(peaks, view, layer) {
20
+ this._peaks = peaks;
21
+ this._view = view;
22
+ this._layer = layer;
23
+ this._linesBySourceId = {};
24
+ this._linesByPosition = {};
25
+ this._autoAddToLayer = false;
26
+ this._areSourceInteractionsAllowed = true;
27
+ this._areSegmentInteractionsAllowed = true;
28
+
29
+ this._lineId = 0;
30
+
31
+ this._lineIndicator = new LineIndicator(
32
+ peaks,
33
+ view,
34
+ document.getElementById('line-indicator-container')
35
+ );
36
+
37
+ this._peaks.on('line.heightChanged', this._onLineHeightChanged.bind(this));
38
+ this._peaks.on('line.add', this._onLineAdd.bind(this));
39
+ this._peaks.on('line.remove', this._onLineRemove.bind(this));
40
+ }
41
+
42
+ Lines.prototype._onLineHeightChanged = function(position) {
43
+ this._updateLinesPosition(position);
44
+ this._view.updateTimeline();
45
+ };
46
+
47
+ Lines.prototype._onLineAdd = function(position) {
48
+ this._createLine(position);
49
+ this._setInteractions(position);
50
+ this._updateLinesPosition(position);
51
+ };
52
+
53
+ Lines.prototype._onLineRemove = function(position) {
54
+ var oldLine = this.removeLine(position);
55
+ var lineNewY = oldLine.getY();
56
+
57
+ this._updateLinesPosition(position, lineNewY);
58
+ };
59
+
60
+ Lines.prototype.addSourceGroup = function(sourceGroup, position) {
61
+ if (!this._linesByPosition[position]) {
62
+ this._createLine(position);
63
+ this._setInteractions(position);
64
+ }
65
+
66
+ sourceGroup.getSource().position = position;
67
+ this._linesByPosition[position].addSourceGroup(sourceGroup);
68
+ this._linesBySourceId[sourceGroup.getSource().id] = this._linesByPosition[position];
69
+ };
70
+
71
+ Lines.prototype.addSegments = function(segmentsGroup, position) {
72
+ this._createLine(position);
73
+
74
+ this._linesByPosition[position].allowInteractions(this._areSegmentInteractionsAllowed);
75
+ this._linesByPosition[position].addSegments(segmentsGroup);
76
+ this._segmentsLine = this._linesByPosition[position];
77
+
78
+ this._setInteractions(position);
79
+
80
+ this._updateLinesPosition(position);
81
+ };
82
+
83
+ Lines.prototype.removeSourceGroup = function(source, isPermanent) {
84
+ var sourceGroup = this._linesByPosition[source.position].removeSourceGroup(source, isPermanent);
85
+
86
+ if (isPermanent) {
87
+ delete this._linesBySourceId[source.id];
88
+ this._updateLinesPosition(source.position);
89
+ }
90
+
91
+ return sourceGroup;
92
+ };
93
+
94
+ Lines.prototype.removeLine = function(pos) {
95
+ var oldLine = this._linesByPosition[pos];
96
+
97
+ oldLine.destroy();
98
+
99
+ delete this._linesByPosition[pos];
100
+
101
+ this._lineIndicator.removeIndicator(oldLine.getId(), false);
102
+
103
+ return oldLine;
104
+ };
105
+
106
+ Lines.prototype.isLineVisible = function(position) {
107
+ return this._linesByPosition[position].isVisible();
108
+ };
109
+
110
+ Lines.prototype.getVisibleLines = function() {
111
+ var positions = {};
112
+
113
+ for (var position in this._linesByPosition) {
114
+ if (Utils.objectHasProperty(this._linesByPosition, position)) {
115
+ if (this._linesByPosition[position].isVisible()) {
116
+ positions[position] = true;
117
+ }
118
+ }
119
+ }
120
+ return positions;
121
+ };
122
+
123
+ Lines.prototype.addToLayer = function(layer) {
124
+ for (var position in this._linesByPosition) {
125
+ if (Utils.objectHasProperty(this._linesByPosition, position)) {
126
+ this._linesByPosition[position].addToLayer(layer);
127
+ }
128
+ }
129
+
130
+ this._autoAddToLayer = true;
131
+ };
132
+
133
+ Lines.prototype.updateLines = function(position) {
134
+ this._updateLinesPosition(position);
135
+ };
136
+
137
+ Lines.prototype._updateLinesPosition = function(position, forceNewY) {
138
+ var line = this._linesByPosition[position];
139
+ var dy = null;
140
+ var newY;
141
+
142
+ if (forceNewY) {
143
+ newY = forceNewY;
144
+ }
145
+ else {
146
+ newY = line.getY() + line.lineHeight() + this._peaks.options.interline;
147
+ }
148
+
149
+ for (var pos in this._linesByPosition) {
150
+ if (Utils.objectHasProperty(this._linesByPosition, pos)) {
151
+ if (parseInt(pos, 10) > position) {
152
+ if (dy === null) {
153
+ dy = newY - this._linesByPosition[pos].getY();
154
+ this._linesByPosition[pos].moveOnY(dy);
155
+ }
156
+ else {
157
+ this._linesByPosition[pos].moveOnY(dy);
158
+ }
159
+ }
160
+ }
161
+ }
162
+
163
+ this._lineIndicator.updateIndicators();
164
+ this._lineIndicator.draw();
165
+
166
+ this._view.drawSourcesLayer();
167
+ };
168
+
169
+ Lines.prototype.setOffsetY = function(frameOffset) {
170
+ for (var position in this._linesByPosition) {
171
+ if (Utils.objectHasProperty(this._linesByPosition, position)) {
172
+ var line = this._linesByPosition[position];
173
+
174
+ line.y(line.getInitialY() - frameOffset);
175
+ }
176
+ }
177
+
178
+ this._lineIndicator.updateIndicators();
179
+ this._lineIndicator.draw();
180
+ };
181
+
182
+ Lines.prototype.height = function() {
183
+ var height = 2 * this._peaks.options.padding;
184
+
185
+ for (var position in this._linesByPosition) {
186
+ if (Utils.objectHasProperty(this._linesByPosition, position)) {
187
+ height += this._linesByPosition[position].lineHeight() + this._peaks.options.interline;
188
+ }
189
+ }
190
+
191
+ height -= this._peaks.options.interline;
192
+
193
+ return height;
194
+ };
195
+
196
+ Lines.prototype.linesLength = function() {
197
+ var length = 0;
198
+
199
+ for (var position in this._linesByPosition) {
200
+ if (Utils.objectHasProperty(this._linesByPosition, position)) {
201
+ var lineLength = this._linesByPosition[position].lineLength();
202
+
203
+ if (lineLength > length) {
204
+ length = lineLength;
205
+ }
206
+ }
207
+ }
208
+
209
+ return length;
210
+ };
211
+
212
+ Lines.prototype.manageVerticalPosition = function(source, newY) {
213
+ if (newY !== null && newY !== undefined) {
214
+ var pos = this.getLineOnPosition(newY);
215
+
216
+ if (pos[0] === pos[1]
217
+ && pos[0] !== source.position
218
+ && !this._linesByPosition[pos[0]].isSegmentsLine()) {
219
+ this.moveSourceToPosition(source, pos[0]);
220
+ }
221
+ }
222
+ };
223
+
224
+ Lines.prototype.getLineOnPosition = function(y) {
225
+ var height;
226
+ var pos = [-1, Number.MAX_VALUE];
227
+
228
+ for (var position in this._linesByPosition) {
229
+ if (Utils.objectHasProperty(this._linesByPosition, position)) {
230
+ height = this._linesByPosition[position].lineHeight();
231
+
232
+ if (y > this._linesByPosition[position].getY()) {
233
+ pos[0] = Math.max(pos[0], parseInt(position, 10));
234
+ }
235
+
236
+ if (y < this._linesByPosition[position].getY() + height) {
237
+ pos[1] = Math.min(pos[1], parseInt(position, 10));
238
+ }
239
+ }
240
+ }
241
+
242
+ return pos;
243
+ };
244
+
245
+ Lines.prototype.moveSourceToPosition = function(source, pos) {
246
+ var sourceGroup = this._linesByPosition[source.position].removeSourceGroup(source, true);
247
+
248
+ delete this._linesBySourceId[source.id];
249
+
250
+ sourceGroup.moveTo(this._linesByPosition[pos].getKonvaGroup());
251
+
252
+ this._updateLinesPosition(source.position);
253
+
254
+ this.addSourceGroup(sourceGroup, pos);
255
+ };
256
+
257
+ Lines.prototype._getNextLineId = function() {
258
+ this._lineId++;
259
+ return this._lineId;
260
+ };
261
+
262
+ Lines.prototype._createLine = function(position) {
263
+ var y = this._peaks.options.padding;
264
+ var currentPos;
265
+ var newLinesByPosition = Object.assign({},this._linesByPosition);
266
+
267
+ for (var pos in this._linesByPosition) {
268
+ if (Utils.objectHasProperty(this._linesByPosition, pos)) {
269
+ currentPos = parseInt(pos, 10);
270
+ if (currentPos < position) {
271
+ y += this._linesByPosition[pos].lineHeight() + this._peaks.options.interline;
272
+ }
273
+ else {
274
+ if (this._linesByPosition[position]) {
275
+ this._linesByPosition[currentPos].updatePosition(currentPos + 1);
276
+ newLinesByPosition[currentPos + 1] = this._linesByPosition[currentPos];
277
+ }
278
+ }
279
+ }
280
+ }
281
+
282
+ var line = new Line(this._peaks, this._view, y, this._getNextLineId(), position);
283
+
284
+ this._lineIndicator.addIndicator(line);
285
+
286
+ if (this._autoAddToLayer) {
287
+ line.addToLayer(this._layer);
288
+ }
289
+
290
+ newLinesByPosition[position] = line;
291
+ this._linesByPosition = newLinesByPosition;
292
+ };
293
+
294
+ Lines.prototype.updateSegments = function(frameStartTime, frameEndTime) {
295
+ if (this._segmentsLine) {
296
+ this._segmentsLine.updateSegments(frameStartTime, frameEndTime);
297
+ }
298
+ };
299
+
300
+ Lines.prototype.manageCollision = function(source, newStartX, newEndX) {
301
+ return this._linesBySourceId[source.id].manageCollision(source, newStartX, newEndX);
302
+ };
303
+
304
+ Lines.prototype.manageSourceOrder = function(source, newStartX, newEndX) {
305
+ return this._linesBySourceId[source.id].manageSourceOrder(source, newStartX, newEndX);
306
+ };
307
+
308
+ Lines.prototype.rescale = function() {
309
+ for (var position in this._linesByPosition) {
310
+ if (Utils.objectHasProperty(this._linesByPosition, position)) {
311
+ this._linesByPosition[position].rescale();
312
+ }
313
+ }
314
+ };
315
+
316
+ Lines.prototype._setInteractions = function(position) {
317
+ var line = this._linesByPosition[position];
318
+
319
+ if (this._areInteractionsOverridden) {
320
+ line.allowInteractions(this._areInteractionsAllowed);
321
+ }
322
+ else {
323
+ line.allowInteractions(
324
+ line.isSegmentsLine() ?
325
+ this._areSegmentInteractionsAllowed :
326
+ this._areSourceInteractionsAllowed
327
+ );
328
+ }
329
+ };
330
+
331
+ Lines.prototype.overrideInteractions = function(bool, areInteractionsAllowed) {
332
+ this._areInteractionsOverridden = typeof bool !== 'undefined' ?
333
+ bool : this._areInteractionsOverridden;
334
+ this._areInteractionsAllowed = typeof areInteractionsAllowed !== 'undefined' ?
335
+ areInteractionsAllowed : this._areInteractionsAllowed;
336
+ for (var position in this._linesByPosition) {
337
+ if (Utils.objectHasProperty(this._linesByPosition, position)) {
338
+ this._setInteractions(position);
339
+ }
340
+ }
341
+ };
342
+
343
+ Lines.prototype.allowInteractions = function(forSources, forSegments) {
344
+ this._areSourceInteractionsAllowed = typeof forSources !== 'undefined' ?
345
+ forSources : this._areSourceInteractionsAllowed;
346
+ this._areSegmentInteractionsAllowed = typeof forSegments !== 'undefined' ?
347
+ forSegments : this._areSegmentInteractionsAllowed;
348
+ for (var position in this._linesByPosition) {
349
+ if (Utils.objectHasProperty(this._linesByPosition, position)) {
350
+ this._setInteractions(position);
351
+ }
352
+ }
353
+ };
354
+
355
+ return Lines;
356
+ });