@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/segment.js ADDED
@@ -0,0 +1,229 @@
1
+ /**
2
+ * @file
3
+ *
4
+ * Defines the {@link Segment} class.
5
+ *
6
+ * @module segment
7
+ */
8
+
9
+ define([
10
+ './utils'
11
+ ], function(Utils) {
12
+ 'use strict';
13
+
14
+ function validateSegment(peaks, options, context) {
15
+ if (!Utils.isValidTime(options.startTime)) {
16
+ // eslint-disable-next-line max-len
17
+ throw new TypeError('peaks.segments.' + context + ': startTime should be a valid number');
18
+ }
19
+
20
+ if (!Utils.isValidTime(options.endTime)) {
21
+ // eslint-disable-next-line max-len
22
+ throw new TypeError('peaks.segments.' + context + ': endTime should be a valid number');
23
+ }
24
+
25
+ options.startTime = Utils.roundTime(options.startTime);
26
+ options.endTime = Utils.roundTime(options.endTime);
27
+
28
+ if (options.startTime < 0) {
29
+ options.startTime = 0;
30
+ }
31
+
32
+ if (options.endTime < 0) {
33
+ options.endTime = 0;
34
+ }
35
+ else if (options.endTime < options.startTime + peaks.options.minSegmentSize) {
36
+ options.endTime = options.startTime + peaks.options.minSegmentSize;
37
+ }
38
+
39
+ options.startTime = Utils.roundTime(options.startTime);
40
+ options.endTime = Utils.roundTime(options.endTime);
41
+
42
+ // if (options.endTime <= options.startTime) {
43
+ // // eslint-disable-next-line max-len
44
+ // throw new RangeError(
45
+ // 'peaks.segments.' + context + ': endTime should be greater than startTime'
46
+ // );
47
+ // }
48
+
49
+ if (options.opacity < 0 || options.opacity > 1) {
50
+ // eslint-disable-next-line max-len
51
+ throw new RangeError('peaks.segments.' + context + ': opacity should be between 0 and 1');
52
+ }
53
+
54
+ if (Utils.isNullOrUndefined(options.labelText)) {
55
+ // Set default label text
56
+ options.labelText = '';
57
+ }
58
+ else if (!Utils.isString(options.labelText)) {
59
+ throw new TypeError('peaks.segments.' + context + ': labelText must be a string');
60
+ }
61
+ }
62
+
63
+ /**
64
+ * A segment is a region of time, with associated label and color.
65
+ *
66
+ * @class
67
+ * @alias Segment
68
+ *
69
+ * @param {Peaks} peaks A reference to the Peaks instance.
70
+ * @param {String} id A unique identifier for the segment.
71
+ * @param {Number} startTime Segment start time, in seconds.
72
+ * @param {Number} endTime Segment end time, in seconds.
73
+ * @param {String} labelText Segment label text.
74
+ * @param {String} color Segment waveform color.
75
+ * @param {Number} opacity Segment waveform opacity.
76
+ * @param {Boolean} editable If <code>true</code> the segment start and
77
+ * end times can be adjusted via the user interface.
78
+ */
79
+
80
+ function Segment(peaks, id, startTime, endTime, labelText, color,
81
+ textColor, handleTextColor, opacity, editable) {
82
+ var opts = {
83
+ startTime: startTime,
84
+ endTime: endTime,
85
+ labelText: labelText,
86
+ color: color,
87
+ textColor: textColor,
88
+ handleTextColor: handleTextColor,
89
+ opacity: opacity,
90
+ editable: editable
91
+ };
92
+
93
+ validateSegment(peaks, opts, 'add()');
94
+
95
+ this._peaks = peaks;
96
+ this._id = id;
97
+ this._startTime = opts.startTime;
98
+ this._endTime = opts.endTime;
99
+ this._labelText = opts.labelText;
100
+ this._color = opts.color;
101
+ this._selectedColor = Utils.shadeColor(color, 20);
102
+ this._textColor = opts.textColor;
103
+ this._handleTextColor = opts.handleTextColor;
104
+ this._opacity = opts.opacity;
105
+ this._editable = opts.editable;
106
+ this._minSize = peaks.options.minSegmentSize;
107
+ }
108
+
109
+ Object.defineProperties(Segment.prototype, {
110
+ id: {
111
+ enumerable: true,
112
+ get: function() {
113
+ return this._id;
114
+ }
115
+ },
116
+ startTime: {
117
+ enumerable: true,
118
+ get: function() {
119
+ return this._startTime;
120
+ },
121
+
122
+ set: function(time) {
123
+ this._startTime = Utils.roundTime(time);
124
+ }
125
+ },
126
+ endTime: {
127
+ enumerable: true,
128
+ get: function() {
129
+ return this._endTime;
130
+ },
131
+
132
+ set: function(time) {
133
+ this._endTime = Utils.roundTime(time);
134
+ }
135
+ },
136
+ labelText: {
137
+ enumerable: true,
138
+ get: function() {
139
+ return this._labelText;
140
+ }
141
+ },
142
+ color: {
143
+ enumerable: true,
144
+ get: function() {
145
+ return this._color;
146
+ }
147
+ },
148
+ selectedColor: {
149
+ enumerable: true,
150
+ get: function() {
151
+ return this._selectedColor;
152
+ }
153
+ },
154
+ textColor: {
155
+ enumerable: true,
156
+ get: function() {
157
+ return this._textColor;
158
+ }
159
+ },
160
+ handleTextColor: {
161
+ enumerable: true,
162
+ get: function() {
163
+ return this._handleTextColor;
164
+ }
165
+ },
166
+ opacity: {
167
+ enumerable: true,
168
+ get: function() {
169
+ return this._opacity;
170
+ }
171
+ },
172
+ editable: {
173
+ enumerable: true,
174
+ get: function() {
175
+ return this._editable;
176
+ }
177
+ },
178
+ minSize: {
179
+ enumerable: true,
180
+ get: function() {
181
+ return this._minSize;
182
+ }
183
+ }
184
+ });
185
+
186
+ Segment.prototype.update = function(options) {
187
+ var opts = {
188
+ startTime: this.startTime,
189
+ endTime: this.endTime,
190
+ labelText: this.labelText,
191
+ color: this.color,
192
+ textColor: this.textColor,
193
+ handleTextColor: this.handleTextColor,
194
+ opacity: this.opacity,
195
+ editable: this.editable
196
+ };
197
+
198
+ Utils.extend(opts, options);
199
+
200
+ validateSegment(this._peaks, opts, 'update()');
201
+
202
+ this._startTime = opts.startTime;
203
+ this._endTime = opts.endTime;
204
+ this._labelText = opts.labelText;
205
+ this._color = opts.color;
206
+ this._textColor = opts.textColor;
207
+ this._handleTextColor = opts.handleTextColor;
208
+ this._opacity = opts.opacity;
209
+ this._editable = opts.editable;
210
+
211
+ this._peaks.emit('segment.updated', this);
212
+ };
213
+
214
+ /**
215
+ * Returns <code>true</code> if the segment overlaps a given time region.
216
+ *
217
+ * @param {Number} startTime The start of the time region, in seconds.
218
+ * @param {Number} endTime The end of the time region, in seconds.
219
+ * @returns {Boolean}
220
+ *
221
+ * @see http://wiki.c2.com/?TestIfDateRangesOverlap
222
+ */
223
+
224
+ Segment.prototype.isVisible = function(startTime, endTime) {
225
+ return this.startTime < endTime && startTime < this.endTime;
226
+ };
227
+
228
+ return Segment;
229
+ });