-
Notifications
You must be signed in to change notification settings - Fork 380
Expand file tree
/
Copy pathRangeItem.js
More file actions
427 lines (379 loc) 路 12.5 KB
/
Copy pathRangeItem.js
File metadata and controls
427 lines (379 loc) 路 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import { ReservedGroupIds } from '../Group';
import Item from './Item';
/**
* @constructor RangeItem
* @extends Item
*/
class RangeItem extends Item {
/**
* @param {Object} data Object containing parameters start, end
* content, className.
* @param {{toScreen: function, toTime: function}} conversion
* Conversion functions from time to screen and vice versa
* @param {Object} [options] Configuration options
* // TODO: describe options
*/
constructor(data, conversion, options) {
super(data, conversion, options)
this.props = {
content: {
width: 0
}
};
this.overflow = false; // if contents can overflow (css styling), this flag is set to true
// validate data
if (data) {
if (data.start == undefined) {
throw new Error(`Property "start" missing in item ${data.id}`);
}
if (data.end == undefined) {
throw new Error(`Property "end" missing in item ${data.id}`);
}
}
}
/**
* Check whether this item is visible inside given range
*
* @param {timeline.Range} range with a timestamp for start and end
* @returns {boolean} True if visible
*/
isVisible(range) {
if (this.cluster) {
return false;
}
// determine visibility
return (this.data.start < range.end) && (this.data.end > range.start);
}
/**
* create DOM elements
* @private
*/
_createDomElement() {
if (!this.dom) {
// create DOM
this.dom = {};
// background box
this.dom.box = document.createElement('div');
// className is updated in redraw()
// frame box (to prevent the item contents from overflowing)
this.dom.frame = document.createElement('div');
this.dom.frame.className = 'vis-item-overflow';
this.dom.box.appendChild(this.dom.frame);
// visible frame box (showing the frame that is always visible)
this.dom.visibleFrame = document.createElement('div');
this.dom.visibleFrame.className = 'vis-item-visible-frame';
this.dom.box.appendChild(this.dom.visibleFrame);
// contents box
this.dom.content = document.createElement('div');
this.dom.content.className = 'vis-item-content';
this.dom.frame.appendChild(this.dom.content);
// attach this item as attribute
this.dom.box['vis-item'] = this;
this.dirty = true;
}
}
/**
* append element to DOM
* @private
*/
_appendDomElement() {
if (!this.parent) {
throw new Error('Cannot redraw item: no parent attached');
}
if (!this.dom.box.parentNode) {
const container = this.parent.groupId === ReservedGroupIds.UNGROUPED ? this.parent.dom.ungrouped : this.parent.dom.foreground;
if (!container) {
throw new Error('Cannot redraw item: parent has no foreground container element');
}
container.appendChild(this.dom.box);
}
this.displayed = true;
}
/**
* update dirty DOM components
* @private
*/
_updateDirtyDomComponents() {
// update dirty DOM. An item is marked dirty when:
// - the item is not yet rendered
// - the item's data is changed
// - the item is selected/deselected
if (this.dirty) {
this._updateContents(this.dom.content);
this._updateDataAttributes(this.dom.box);
this._updateStyle(this.dom.box);
const editable = (this.editable.updateTime || this.editable.updateGroup);
// update class
const className = (this.data.className ? (' ' + this.data.className) : '') +
(this.selected ? ' vis-selected' : '') +
(editable ? ' vis-editable' : ' vis-readonly');
this.dom.box.className = this.baseClassName + className;
// turn off max-width to be able to calculate the real width
// this causes an extra browser repaint/reflow, but so be it
this.dom.content.style.maxWidth = 'none';
}
}
/**
* get DOM component sizes
* @return {object}
* @private
*/
_getDomComponentsSizes() {
// determine from css whether this box has overflow
this.overflow = window.getComputedStyle(this.dom.frame).overflow !== 'hidden';
this.whiteSpace = window.getComputedStyle(this.dom.content).whiteSpace !== 'nowrap';
return {
content: {
width: this.dom.content.offsetWidth,
},
box: {
height: this.dom.box.offsetHeight
}
}
}
/**
* update DOM component sizes
* @param {array} sizes
* @private
*/
_updateDomComponentsSizes(sizes) {
this.props.content.width = sizes.content.width;
this.height = sizes.box.height;
this.dom.content.style.maxWidth = '';
this.dirty = false;
}
/**
* repaint DOM additional components
* @private
*/
_repaintDomAdditionals() {
this._repaintOnItemUpdateTimeTooltip(this.dom.box);
this._repaintDeleteButton(this.dom.box);
this._repaintDragCenter();
this._repaintDragLeft();
this._repaintDragRight();
}
/**
* Repaint the item
* @param {boolean} [returnQueue=false] return the queue
* @return {boolean} the redraw queue if returnQueue=true
*/
redraw(returnQueue) {
let sizes;
const queue = [
// create item DOM
this._createDomElement.bind(this),
// append DOM to parent DOM
this._appendDomElement.bind(this),
// update dirty DOM
this._updateDirtyDomComponents.bind(this),
() => {
if (this.dirty) {
sizes = this._getDomComponentsSizes.bind(this)();
}
},
() => {
if (this.dirty) {
this._updateDomComponentsSizes.bind(this)(sizes);
}
},
// repaint DOM additionals
this._repaintDomAdditionals.bind(this)
];
if (returnQueue) {
return queue;
} else {
let result;
queue.forEach(fn => {
result = fn();
});
return result;
}
}
/**
* Show the item in the DOM (when not already visible). The items DOM will
* be created when needed.
* @param {boolean} [returnQueue=false] whether to return a queue of functions to execute instead of just executing them
* @return {boolean} the redraw queue if returnQueue=true
*/
show(returnQueue) {
if (!this.displayed) {
return this.redraw(returnQueue);
}
}
/**
* Hide the item from the DOM (when visible)
*/
hide() {
if (this.displayed) {
const box = this.dom.box;
if (box.parentNode) {
box.parentNode.removeChild(box);
}
this.displayed = false;
}
}
/**
* Reposition the item horizontally
* @param {boolean} [limitSize=true] If true (default), the width of the range
* item will be limited, as the browser cannot
* display very wide divs. This means though
* that the applied left and width may
* not correspond to the ranges start and end
* @Override
*/
repositionX(limitSize) {
const parentWidth = this.parent.width;
let start = this.conversion.toScreen(this.data.start);
let end = this.conversion.toScreen(this.data.end);
const align = this.data.align === undefined ? this.options.align : this.data.align;
let contentStartPosition;
let contentWidth;
// limit the width of the range, as browsers cannot draw very wide divs
// unless limitSize: false is explicitly set in item data
if (this.data.limitSize !== false && (limitSize === undefined || limitSize === true)) {
if (start < -parentWidth) {
start = -parentWidth;
}
if (end > 2 * parentWidth) {
end = 2 * parentWidth;
}
}
//round to 3 decimals to compensate floating-point values rounding
const boxWidth = Math.max(Math.round((end - start) * 1000) / 1000, 1);
if (this.overflow) {
if (this.options.rtl) {
this.right = start;
} else {
this.left = start;
}
this.width = boxWidth + this.props.content.width;
contentWidth = this.props.content.width;
// Note: The calculation of width is an optimistic calculation, giving
// a width which will not change when moving the Timeline
// So no re-stacking needed, which is nicer for the eye;
}
else {
if (this.options.rtl) {
this.right = start;
} else {
this.left = start;
}
this.width = boxWidth;
contentWidth = Math.min(end - start, this.props.content.width);
}
if (this.options.rtl) {
this.dom.box.style.transform = `translateX(${this.right * -1}px)`;
} else {
this.dom.box.style.transform = `translateX(${this.left}px)`;
}
this.dom.box.style.width = `${boxWidth}px`;
if (this.whiteSpace) {
this.height = this.dom.box.offsetHeight;
}
switch (align) {
case 'left':
this.dom.content.style.transform = 'translateX(0)';
break;
case 'right':
if (this.options.rtl) {
const translateX = Math.max((boxWidth - contentWidth), 0) * -1;
this.dom.content.style.transform = `translateX(${translateX}px)`;
} else {
this.dom.content.style.transform = `translateX(${Math.max((boxWidth - contentWidth), 0)}px)`;
}
break;
case 'center':
if (this.options.rtl) {
const translateX = Math.max((boxWidth - contentWidth) / 2, 0) * -1;
this.dom.content.style.transform = `translateX(${translateX}px)`;
} else {
this.dom.content.style.transform = `translateX(${Math.max((boxWidth - contentWidth) / 2, 0)}px)`;
}
break;
default: // 'auto'
// when range exceeds left of the window, position the contents at the left of the visible area
if (this.overflow) {
if (end > 0) {
contentStartPosition = Math.max(-start, 0);
}
else {
contentStartPosition = -contentWidth; // ensure it's not visible anymore
}
}
else {
if (start < 0) {
contentStartPosition = -start;
}
else {
contentStartPosition = 0;
}
}
if (this.options.rtl) {
const translateX = contentStartPosition * -1;
this.dom.content.style.transform = `translateX(${translateX}px)`;
} else {
this.dom.content.style.transform = `translateX(${contentStartPosition}px)`;
// this.dom.content.style.width = `calc(100% - ${contentStartPosition}px)`;
}
}
}
/**
* Reposition the item vertically
* @Override
*/
repositionY() {
const orientation = this.options.orientation.item;
const box = this.dom.box;
if (orientation == 'top') {
box.style.top = `${this.top}px`;
}
else {
box.style.top = `${this.parent.height - this.top - this.height}px`;
}
}
/**
* Repaint a drag area on the left side of the range when the range is selected
* @protected
*/
_repaintDragLeft() {
if ((this.selected || this.options.itemsAlwaysDraggable.range) && this.editable.updateTime && !this.dom.dragLeft) {
// create and show drag area
const dragLeft = document.createElement('div');
dragLeft.className = 'vis-drag-left';
dragLeft.dragLeftItem = this;
this.dom.box.appendChild(dragLeft);
this.dom.dragLeft = dragLeft;
}
else if (!this.selected && !this.options.itemsAlwaysDraggable.range && this.dom.dragLeft) {
// delete drag area
if (this.dom.dragLeft.parentNode) {
this.dom.dragLeft.parentNode.removeChild(this.dom.dragLeft);
}
this.dom.dragLeft = null;
}
}
/**
* Repaint a drag area on the right side of the range when the range is selected
* @protected
*/
_repaintDragRight() {
if ((this.selected || this.options.itemsAlwaysDraggable.range) && this.editable.updateTime && !this.dom.dragRight) {
// create and show drag area
const dragRight = document.createElement('div');
dragRight.className = 'vis-drag-right';
dragRight.dragRightItem = this;
this.dom.box.appendChild(dragRight);
this.dom.dragRight = dragRight;
}
else if (!this.selected && !this.options.itemsAlwaysDraggable.range && this.dom.dragRight) {
// delete drag area
if (this.dom.dragRight.parentNode) {
this.dom.dragRight.parentNode.removeChild(this.dom.dragRight);
}
this.dom.dragRight = null;
}
}
}
RangeItem.prototype.baseClassName = 'vis-item vis-range';
export default RangeItem;