1 | import Emitter from 'emitter-component';
|
2 | import Hammer from '../module/hammer';
|
3 | import hammerUtil from '../hammerUtil';
|
4 | import util from 'vis-util';
|
5 | import TimeAxis from './component/TimeAxis';
|
6 | import Activator from '../shared/Activator';
|
7 | import DateUtil from './DateUtil';
|
8 | import CustomTime from './component/CustomTime';
|
9 |
|
10 | import './component/css/animation.css';
|
11 | import './component/css/currenttime.css';
|
12 | import './component/css/panel.css';
|
13 | import './component/css/pathStyles.css';
|
14 | import './component/css/timeline.css';
|
15 | import '../shared/bootstrap.css';
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | function Core () {}
|
22 |
|
23 |
|
24 | Emitter(Core.prototype);
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | Core.prototype._create = function (container) {
|
34 | this.dom = {};
|
35 |
|
36 | this.dom.container = container;
|
37 | this.dom.container.style.position = 'relative';
|
38 |
|
39 | this.dom.root = document.createElement('div');
|
40 | this.dom.background = document.createElement('div');
|
41 | this.dom.backgroundVertical = document.createElement('div');
|
42 | this.dom.backgroundHorizontal = document.createElement('div');
|
43 | this.dom.centerContainer = document.createElement('div');
|
44 | this.dom.leftContainer = document.createElement('div');
|
45 | this.dom.rightContainer = document.createElement('div');
|
46 | this.dom.center = document.createElement('div');
|
47 | this.dom.left = document.createElement('div');
|
48 | this.dom.right = document.createElement('div');
|
49 | this.dom.top = document.createElement('div');
|
50 | this.dom.bottom = document.createElement('div');
|
51 | this.dom.shadowTop = document.createElement('div');
|
52 | this.dom.shadowBottom = document.createElement('div');
|
53 | this.dom.shadowTopLeft = document.createElement('div');
|
54 | this.dom.shadowBottomLeft = document.createElement('div');
|
55 | this.dom.shadowTopRight = document.createElement('div');
|
56 | this.dom.shadowBottomRight = document.createElement('div');
|
57 | this.dom.rollingModeBtn = document.createElement('div');
|
58 | this.dom.loadingScreen = document.createElement('div');
|
59 |
|
60 | this.dom.root.className = 'vis-timeline';
|
61 | this.dom.background.className = 'vis-panel vis-background';
|
62 | this.dom.backgroundVertical.className = 'vis-panel vis-background vis-vertical';
|
63 | this.dom.backgroundHorizontal.className = 'vis-panel vis-background vis-horizontal';
|
64 | this.dom.centerContainer.className = 'vis-panel vis-center';
|
65 | this.dom.leftContainer.className = 'vis-panel vis-left';
|
66 | this.dom.rightContainer.className = 'vis-panel vis-right';
|
67 | this.dom.top.className = 'vis-panel vis-top';
|
68 | this.dom.bottom.className = 'vis-panel vis-bottom';
|
69 | this.dom.left.className = 'vis-content';
|
70 | this.dom.center.className = 'vis-content';
|
71 | this.dom.right.className = 'vis-content';
|
72 | this.dom.shadowTop.className = 'vis-shadow vis-top';
|
73 | this.dom.shadowBottom.className = 'vis-shadow vis-bottom';
|
74 | this.dom.shadowTopLeft.className = 'vis-shadow vis-top';
|
75 | this.dom.shadowBottomLeft.className = 'vis-shadow vis-bottom';
|
76 | this.dom.shadowTopRight.className = 'vis-shadow vis-top';
|
77 | this.dom.shadowBottomRight.className = 'vis-shadow vis-bottom';
|
78 | this.dom.rollingModeBtn.className = 'vis-rolling-mode-btn';
|
79 | this.dom.loadingScreen.className = 'vis-loading-screen';
|
80 |
|
81 | this.dom.root.appendChild(this.dom.background);
|
82 | this.dom.root.appendChild(this.dom.backgroundVertical);
|
83 | this.dom.root.appendChild(this.dom.backgroundHorizontal);
|
84 | this.dom.root.appendChild(this.dom.centerContainer);
|
85 | this.dom.root.appendChild(this.dom.leftContainer);
|
86 | this.dom.root.appendChild(this.dom.rightContainer);
|
87 | this.dom.root.appendChild(this.dom.top);
|
88 | this.dom.root.appendChild(this.dom.bottom);
|
89 | this.dom.root.appendChild(this.dom.rollingModeBtn);
|
90 |
|
91 | this.dom.centerContainer.appendChild(this.dom.center);
|
92 | this.dom.leftContainer.appendChild(this.dom.left);
|
93 | this.dom.rightContainer.appendChild(this.dom.right);
|
94 | this.dom.centerContainer.appendChild(this.dom.shadowTop);
|
95 | this.dom.centerContainer.appendChild(this.dom.shadowBottom);
|
96 | this.dom.leftContainer.appendChild(this.dom.shadowTopLeft);
|
97 | this.dom.leftContainer.appendChild(this.dom.shadowBottomLeft);
|
98 | this.dom.rightContainer.appendChild(this.dom.shadowTopRight);
|
99 | this.dom.rightContainer.appendChild(this.dom.shadowBottomRight);
|
100 |
|
101 |
|
102 | this.props = {
|
103 | root: {},
|
104 | background: {},
|
105 | centerContainer: {},
|
106 | leftContainer: {},
|
107 | rightContainer: {},
|
108 | center: {},
|
109 | left: {},
|
110 | right: {},
|
111 | top: {},
|
112 | bottom: {},
|
113 | border: {},
|
114 | scrollTop: 0,
|
115 | scrollTopMin: 0
|
116 | };
|
117 |
|
118 | this.on('rangechange', function () {
|
119 | if (this.initialDrawDone === true) {
|
120 | this._redraw();
|
121 | }
|
122 | }.bind(this));
|
123 | this.on('rangechanged', function () {
|
124 | if (!this.initialRangeChangeDone) {
|
125 | this.initialRangeChangeDone = true;
|
126 | }
|
127 | }.bind(this));
|
128 | this.on('touch', this._onTouch.bind(this));
|
129 | this.on('panmove', this._onDrag.bind(this));
|
130 |
|
131 | var me = this;
|
132 | this._origRedraw = this._redraw.bind(this);
|
133 | this._redraw = util.throttle(this._origRedraw);
|
134 |
|
135 | this.on('_change', function (properties) {
|
136 | if (me.itemSet && me.itemSet.initialItemSetDrawn && properties && properties.queue == true) {
|
137 | me._redraw()
|
138 | } else {
|
139 | me._origRedraw();
|
140 | }
|
141 | });
|
142 |
|
143 |
|
144 |
|
145 | this.hammer = new Hammer(this.dom.root);
|
146 | var pinchRecognizer = this.hammer.get('pinch').set({enable: true});
|
147 | pinchRecognizer && hammerUtil.disablePreventDefaultVertically(pinchRecognizer);
|
148 | this.hammer.get('pan').set({threshold:5, direction: Hammer.DIRECTION_HORIZONTAL});
|
149 | this.listeners = {};
|
150 |
|
151 | var events = [
|
152 | 'tap', 'doubletap', 'press',
|
153 | 'pinch',
|
154 | 'pan', 'panstart', 'panmove', 'panend'
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | ];
|
161 | events.forEach(function (type) {
|
162 | var listener = function (event) {
|
163 | if (me.isActive()) {
|
164 | me.emit(type, event);
|
165 | }
|
166 | };
|
167 | me.hammer.on(type, listener);
|
168 | me.listeners[type] = listener;
|
169 | });
|
170 |
|
171 |
|
172 | hammerUtil.onTouch(this.hammer, function (event) {
|
173 | me.emit('touch', event);
|
174 | }.bind(this));
|
175 |
|
176 |
|
177 | hammerUtil.onRelease(this.hammer, function (event) {
|
178 | me.emit('release', event);
|
179 | }.bind(this));
|
180 |
|
181 | |
182 |
|
183 |
|
184 |
|
185 | function onMouseWheel(event) {
|
186 |
|
187 |
|
188 | const LINE_HEIGHT = 40;
|
189 | const PAGE_HEIGHT = 800;
|
190 |
|
191 | if (this.isActive()) {
|
192 | this.emit('mousewheel', event);
|
193 | }
|
194 |
|
195 |
|
196 | var deltaX = 0;
|
197 | var deltaY = 0;
|
198 |
|
199 |
|
200 | if ( 'detail' in event ) { deltaY = event.detail * -1; }
|
201 | if ( 'wheelDelta' in event ) { deltaY = event.wheelDelta; }
|
202 | if ( 'wheelDeltaY' in event ) { deltaY = event.wheelDeltaY; }
|
203 | if ( 'wheelDeltaX' in event ) { deltaX = event.wheelDeltaX * -1; }
|
204 |
|
205 |
|
206 | if ( 'axis' in event && event.axis === event.HORIZONTAL_AXIS ) {
|
207 | deltaX = deltaY * -1;
|
208 | deltaY = 0;
|
209 | }
|
210 |
|
211 |
|
212 | if ( 'deltaY' in event ) {
|
213 | deltaY = event.deltaY * -1;
|
214 | }
|
215 | if ( 'deltaX' in event ) {
|
216 | deltaX = event.deltaX;
|
217 | }
|
218 |
|
219 |
|
220 | if (event.deltaMode) {
|
221 | if (event.deltaMode === 1) {
|
222 | deltaX *= LINE_HEIGHT;
|
223 | deltaY *= LINE_HEIGHT;
|
224 | } else {
|
225 | deltaX *= LINE_HEIGHT;
|
226 | deltaY *= PAGE_HEIGHT;
|
227 | }
|
228 | }
|
229 |
|
230 |
|
231 | if (!this.options.zoomKey || event[this.options.zoomKey]) return;
|
232 |
|
233 |
|
234 | if (!this.options.verticalScroll && !this.options.horizontalScroll) return;
|
235 |
|
236 |
|
237 |
|
238 | event.preventDefault();
|
239 |
|
240 | if (this.options.verticalScroll && Math.abs(deltaY) >= Math.abs(deltaX)) {
|
241 | var current = this.props.scrollTop;
|
242 | var adjusted = current + deltaY;
|
243 |
|
244 | if (this.isActive()) {
|
245 | this._setScrollTop(adjusted);
|
246 | this._redraw();
|
247 | this.emit('scroll', event);
|
248 | }
|
249 | } else if (this.options.horizontalScroll) {
|
250 | var delta = Math.abs(deltaX) >= Math.abs(deltaY) ? deltaX : deltaY;
|
251 |
|
252 |
|
253 | var diff = (delta / 120) * (this.range.end - this.range.start) / 20;
|
254 |
|
255 | var newStart = this.range.start + diff;
|
256 | var newEnd = this.range.end + diff;
|
257 |
|
258 | var options = {
|
259 | animation: false,
|
260 | byUser: true,
|
261 | event: event
|
262 | };
|
263 | this.range.setRange(newStart, newEnd, options);
|
264 | }
|
265 | }
|
266 |
|
267 |
|
268 | if (this.dom.centerContainer.addEventListener) {
|
269 | const wheel = "onwheel" in document.createElement("div") ? "wheel" :
|
270 | document.onmousewheel !== undefined ? "mousewheel" :
|
271 | "DOMMouseScroll";
|
272 | this.dom.centerContainer.addEventListener(wheel, onMouseWheel.bind(this), false);
|
273 | } else {
|
274 |
|
275 | this.dom.centerContainer.attachEvent("onmousewheel", onMouseWheel.bind(this));
|
276 | }
|
277 |
|
278 | |
279 |
|
280 |
|
281 |
|
282 | function onMouseScrollSide(event) {
|
283 | if (!me.options.verticalScroll) return;
|
284 | event.preventDefault();
|
285 | if (me.isActive()) {
|
286 | var adjusted = -event.target.scrollTop;
|
287 | me._setScrollTop(adjusted);
|
288 | me._redraw();
|
289 | me.emit('scrollSide', event);
|
290 | }
|
291 | }
|
292 |
|
293 | this.dom.left.parentNode.addEventListener('scroll', onMouseScrollSide.bind(this));
|
294 | this.dom.right.parentNode.addEventListener('scroll', onMouseScrollSide.bind(this));
|
295 |
|
296 | var itemAddedToTimeline = false;
|
297 |
|
298 | |
299 |
|
300 |
|
301 |
|
302 |
|
303 | function handleDragOver(event) {
|
304 | if (event.preventDefault) {
|
305 | event.preventDefault();
|
306 | }
|
307 |
|
308 |
|
309 | if (!event.target.className.indexOf("vis") > -1) return;
|
310 |
|
311 |
|
312 | if (itemAddedToTimeline) return;
|
313 |
|
314 | event.dataTransfer.dropEffect = 'move';
|
315 | itemAddedToTimeline = true;
|
316 | return false;
|
317 | }
|
318 |
|
319 | |
320 |
|
321 |
|
322 |
|
323 |
|
324 | function handleDrop(event) {
|
325 |
|
326 | if(event.preventDefault) { event.preventDefault(); }
|
327 | if(event.stopPropagation) { event.stopPropagation(); }
|
328 |
|
329 | try {
|
330 | var itemData = JSON.parse(event.dataTransfer.getData("text"))
|
331 | if (!itemData || !itemData.content) return
|
332 | } catch (err) {
|
333 | return false;
|
334 | }
|
335 |
|
336 | itemAddedToTimeline = false;
|
337 | event.center = {
|
338 | x: event.clientX,
|
339 | y: event.clientY
|
340 | };
|
341 |
|
342 | if (itemData.target !== 'item') {
|
343 | me.itemSet._onAddItem(event);
|
344 | } else {
|
345 | me.itemSet._onDropObjectOnItem(event);
|
346 | }
|
347 | me.emit('drop', me.getEventProperties(event))
|
348 | return false;
|
349 | }
|
350 |
|
351 | this.dom.center.addEventListener('dragover', handleDragOver.bind(this), false);
|
352 | this.dom.center.addEventListener('drop', handleDrop.bind(this), false);
|
353 |
|
354 | this.customTimes = [];
|
355 |
|
356 |
|
357 | this.touch = {};
|
358 |
|
359 | this.redrawCount = 0;
|
360 | this.initialDrawDone = false;
|
361 | this.initialRangeChangeDone = false;
|
362 |
|
363 |
|
364 | if (!container) throw new Error('No container provided');
|
365 | container.appendChild(this.dom.root);
|
366 | container.appendChild(this.dom.loadingScreen);
|
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 | Core.prototype.setOptions = function (options) {
|
395 | if (options) {
|
396 |
|
397 | var fields = [
|
398 | 'width', 'height', 'minHeight', 'maxHeight', 'autoResize',
|
399 | 'start', 'end', 'clickToUse', 'dataAttributes', 'hiddenDates',
|
400 | 'locale', 'locales', 'moment', 'rtl', 'zoomKey', 'horizontalScroll', 'verticalScroll'
|
401 | ];
|
402 | util.selectiveExtend(fields, this.options, options);
|
403 |
|
404 | this.dom.rollingModeBtn.style.visibility = 'hidden';
|
405 |
|
406 | if (this.options.rtl) {
|
407 | this.dom.container.style.direction = "rtl";
|
408 | this.dom.backgroundVertical.className = 'vis-panel vis-background vis-vertical-rtl';
|
409 | }
|
410 |
|
411 | if (this.options.verticalScroll) {
|
412 | if (this.options.rtl) {
|
413 | this.dom.rightContainer.className = 'vis-panel vis-right vis-vertical-scroll';
|
414 | } else {
|
415 | this.dom.leftContainer.className = 'vis-panel vis-left vis-vertical-scroll';
|
416 | }
|
417 | }
|
418 |
|
419 | if (typeof this.options.orientation !== 'object') {
|
420 | this.options.orientation = {item:undefined,axis:undefined};
|
421 | }
|
422 | if ('orientation' in options) {
|
423 | if (typeof options.orientation === 'string') {
|
424 | this.options.orientation = {
|
425 | item: options.orientation,
|
426 | axis: options.orientation
|
427 | };
|
428 | }
|
429 | else if (typeof options.orientation === 'object') {
|
430 | if ('item' in options.orientation) {
|
431 | this.options.orientation.item = options.orientation.item;
|
432 | }
|
433 | if ('axis' in options.orientation) {
|
434 | this.options.orientation.axis = options.orientation.axis;
|
435 | }
|
436 | }
|
437 | }
|
438 |
|
439 | if (this.options.orientation.axis === 'both') {
|
440 | if (!this.timeAxis2) {
|
441 | var timeAxis2 = this.timeAxis2 = new TimeAxis(this.body);
|
442 | timeAxis2.setOptions = function (options) {
|
443 | var _options = options ? util.extend({}, options) : {};
|
444 | _options.orientation = 'top';
|
445 | TimeAxis.prototype.setOptions.call(timeAxis2, _options);
|
446 | };
|
447 | this.components.push(timeAxis2);
|
448 | }
|
449 | }
|
450 | else {
|
451 | if (this.timeAxis2) {
|
452 | var index = this.components.indexOf(this.timeAxis2);
|
453 | if (index !== -1) {
|
454 | this.components.splice(index, 1);
|
455 | }
|
456 | this.timeAxis2.destroy();
|
457 | this.timeAxis2 = null;
|
458 | }
|
459 | }
|
460 |
|
461 |
|
462 | if (typeof options.drawPoints == 'function') {
|
463 | options.drawPoints = {
|
464 | onRender: options.drawPoints
|
465 | };
|
466 | }
|
467 |
|
468 | if ('hiddenDates' in this.options) {
|
469 | DateUtil.convertHiddenOptions(this.options.moment, this.body, this.options.hiddenDates);
|
470 | }
|
471 |
|
472 | if ('clickToUse' in options) {
|
473 | if (options.clickToUse) {
|
474 | if (!this.activator) {
|
475 | this.activator = new Activator(this.dom.root);
|
476 | }
|
477 | }
|
478 | else {
|
479 | if (this.activator) {
|
480 | this.activator.destroy();
|
481 | delete this.activator;
|
482 | }
|
483 | }
|
484 | }
|
485 |
|
486 | if ('showCustomTime' in options) {
|
487 | throw new Error('Option `showCustomTime` is deprecated. Create a custom time bar via timeline.addCustomTime(time [, id])');
|
488 | }
|
489 |
|
490 |
|
491 | this._initAutoResize();
|
492 | }
|
493 |
|
494 |
|
495 | this.components.forEach(component => component.setOptions(options));
|
496 |
|
497 |
|
498 | if ('configure' in options) {
|
499 | if (!this.configurator) {
|
500 | this.configurator = this._createConfigurator();
|
501 | }
|
502 |
|
503 | this.configurator.setOptions(options.configure);
|
504 |
|
505 |
|
506 | var appliedOptions = util.deepExtend({}, this.options);
|
507 | this.components.forEach(function (component) {
|
508 | util.deepExtend(appliedOptions, component.options);
|
509 | });
|
510 | this.configurator.setModuleOptions({global: appliedOptions});
|
511 | }
|
512 |
|
513 | this._redraw();
|
514 | };
|
515 |
|
516 |
|
517 |
|
518 |
|
519 |
|
520 | Core.prototype.isActive = function () {
|
521 | return !this.activator || this.activator.active;
|
522 | };
|
523 |
|
524 |
|
525 |
|
526 |
|
527 | Core.prototype.destroy = function () {
|
528 |
|
529 | this.setItems(null);
|
530 | this.setGroups(null);
|
531 |
|
532 |
|
533 | this.off();
|
534 |
|
535 |
|
536 | this._stopAutoResize();
|
537 |
|
538 |
|
539 | if (this.dom.root.parentNode) {
|
540 | this.dom.root.parentNode.removeChild(this.dom.root);
|
541 | }
|
542 | this.dom = null;
|
543 |
|
544 |
|
545 | if (this.activator) {
|
546 | this.activator.destroy();
|
547 | delete this.activator;
|
548 | }
|
549 |
|
550 |
|
551 | for (var event in this.listeners) {
|
552 | if (this.listeners.hasOwnProperty(event)) {
|
553 | delete this.listeners[event];
|
554 | }
|
555 | }
|
556 | this.listeners = null;
|
557 | this.hammer && this.hammer.destroy();
|
558 | this.hammer = null;
|
559 |
|
560 |
|
561 | this.components.forEach(component => component.destroy());
|
562 |
|
563 | this.body = null;
|
564 | };
|
565 |
|
566 |
|
567 |
|
568 |
|
569 |
|
570 |
|
571 |
|
572 | Core.prototype.setCustomTime = function (time, id) {
|
573 | var customTimes = this.customTimes.filter(function (component) {
|
574 | return id === component.options.id;
|
575 | });
|
576 |
|
577 | if (customTimes.length === 0) {
|
578 | throw new Error('No custom time bar found with id ' + JSON.stringify(id))
|
579 | }
|
580 |
|
581 | if (customTimes.length > 0) {
|
582 | customTimes[0].setCustomTime(time);
|
583 | }
|
584 | };
|
585 |
|
586 |
|
587 |
|
588 |
|
589 |
|
590 |
|
591 | Core.prototype.getCustomTime = function(id) {
|
592 | var customTimes = this.customTimes.filter(function (component) {
|
593 | return component.options.id === id;
|
594 | });
|
595 |
|
596 | if (customTimes.length === 0) {
|
597 | throw new Error('No custom time bar found with id ' + JSON.stringify(id))
|
598 | }
|
599 | return customTimes[0].getCustomTime();
|
600 | };
|
601 |
|
602 |
|
603 |
|
604 |
|
605 |
|
606 |
|
607 |
|
608 | Core.prototype.setCustomTimeTitle = function(title, id) {
|
609 | var customTimes = this.customTimes.filter(function (component) {
|
610 | return component.options.id === id;
|
611 | });
|
612 |
|
613 | if (customTimes.length === 0) {
|
614 | throw new Error('No custom time bar found with id ' + JSON.stringify(id))
|
615 | }
|
616 | if (customTimes.length > 0) {
|
617 | return customTimes[0].setCustomTitle(title);
|
618 | }
|
619 | };
|
620 |
|
621 |
|
622 |
|
623 |
|
624 |
|
625 |
|
626 |
|
627 | Core.prototype.getEventProperties = function (event) {
|
628 | return { event: event };
|
629 | };
|
630 |
|
631 |
|
632 |
|
633 |
|
634 |
|
635 |
|
636 |
|
637 |
|
638 |
|
639 |
|
640 |
|
641 |
|
642 |
|
643 |
|
644 |
|
645 | Core.prototype.addCustomTime = function (time, id, options) {
|
646 | var timestamp = time !== undefined
|
647 | ? util.convert(time, 'Date').valueOf()
|
648 | : new Date();
|
649 |
|
650 | var exists = this.customTimes.some(function (customTime) {
|
651 | return customTime.options.id === id;
|
652 | });
|
653 | if (exists) {
|
654 | throw new Error('A custom time with id ' + JSON.stringify(id) + ' already exists');
|
655 | }
|
656 |
|
657 | var customTime = new CustomTime(this.body, util.extend({}, this.options, options, {
|
658 | time : timestamp,
|
659 | id : id
|
660 | }));
|
661 |
|
662 | this.customTimes.push(customTime);
|
663 | this.components.push(customTime);
|
664 | this._redraw();
|
665 |
|
666 | return id;
|
667 | };
|
668 |
|
669 |
|
670 |
|
671 |
|
672 |
|
673 |
|
674 | Core.prototype.removeCustomTime = function (id) {
|
675 | var customTimes = this.customTimes.filter(function (bar) {
|
676 | return (bar.options.id === id);
|
677 | });
|
678 |
|
679 | if (customTimes.length === 0) {
|
680 | throw new Error('No custom time bar found with id ' + JSON.stringify(id))
|
681 | }
|
682 |
|
683 | customTimes.forEach(function (customTime) {
|
684 | this.customTimes.splice(this.customTimes.indexOf(customTime), 1);
|
685 | this.components.splice(this.components.indexOf(customTime), 1);
|
686 | customTime.destroy();
|
687 | }.bind(this))
|
688 | };
|
689 |
|
690 |
|
691 |
|
692 |
|
693 |
|
694 |
|
695 | Core.prototype.getVisibleItems = function() {
|
696 | return this.itemSet && this.itemSet.getVisibleItems() || [];
|
697 | };
|
698 |
|
699 |
|
700 |
|
701 |
|
702 |
|
703 | Core.prototype.getVisibleGroups = function() {
|
704 | return this.itemSet && this.itemSet.getVisibleGroups() || [];
|
705 | };
|
706 |
|
707 |
|
708 |
|
709 |
|
710 |
|
711 |
|
712 |
|
713 |
|
714 |
|
715 |
|
716 |
|
717 |
|
718 | Core.prototype.fit = function(options, callback) {
|
719 | var range = this.getDataRange();
|
720 |
|
721 |
|
722 | if (range.min === null && range.max === null) {
|
723 | return;
|
724 | }
|
725 |
|
726 |
|
727 | var interval = range.max - range.min;
|
728 | var min = new Date(range.min.valueOf() - interval * 0.01);
|
729 | var max = new Date(range.max.valueOf() + interval * 0.01);
|
730 | var animation = (options && options.animation !== undefined) ? options.animation : true;
|
731 | this.range.setRange(min, max, { animation: animation }, callback);
|
732 | };
|
733 |
|
734 |
|
735 |
|
736 |
|
737 |
|
738 |
|
739 | Core.prototype.getDataRange = function() {
|
740 |
|
741 | throw new Error('Cannot invoke abstract method getDataRange');
|
742 | };
|
743 |
|
744 |
|
745 |
|
746 |
|
747 |
|
748 |
|
749 |
|
750 |
|
751 |
|
752 |
|
753 |
|
754 |
|
755 |
|
756 |
|
757 |
|
758 |
|
759 |
|
760 |
|
761 |
|
762 |
|
763 |
|
764 |
|
765 |
|
766 | Core.prototype.setWindow = function(start, end, options, callback) {
|
767 | if (typeof arguments[2] == "function") {
|
768 | callback = arguments[2];
|
769 | options = {};
|
770 | }
|
771 | var animation;
|
772 | var range;
|
773 | if (arguments.length == 1) {
|
774 | range = arguments[0];
|
775 | animation = (range.animation !== undefined) ? range.animation : true;
|
776 | this.range.setRange(range.start, range.end, { animation: animation });
|
777 | }
|
778 | else if (arguments.length == 2 && typeof arguments[1] == "function") {
|
779 | range = arguments[0];
|
780 | callback = arguments[1];
|
781 | animation = (range.animation !== undefined) ? range.animation : true;
|
782 | this.range.setRange(range.start, range.end, { animation: animation }, callback);
|
783 | }
|
784 | else {
|
785 | animation = (options && options.animation !== undefined) ? options.animation : true;
|
786 | this.range.setRange(start, end, { animation: animation }, callback);
|
787 | }
|
788 | };
|
789 |
|
790 |
|
791 |
|
792 |
|
793 |
|
794 |
|
795 |
|
796 |
|
797 |
|
798 |
|
799 |
|
800 |
|
801 |
|
802 | Core.prototype.moveTo = function(time, options, callback) {
|
803 | if (typeof arguments[1] == "function") {
|
804 | callback = arguments[1];
|
805 | options = {};
|
806 | }
|
807 | var interval = this.range.end - this.range.start;
|
808 | var t = util.convert(time, 'Date').valueOf();
|
809 |
|
810 | var start = t - interval / 2;
|
811 | var end = t + interval / 2;
|
812 | var animation = (options && options.animation !== undefined) ? options.animation : true;
|
813 |
|
814 | this.range.setRange(start, end, { animation: animation }, callback);
|
815 | };
|
816 |
|
817 |
|
818 |
|
819 |
|
820 |
|
821 | Core.prototype.getWindow = function() {
|
822 | var range = this.range.getRange();
|
823 | return {
|
824 | start: new Date(range.start),
|
825 | end: new Date(range.end)
|
826 | };
|
827 | };
|
828 |
|
829 |
|
830 |
|
831 |
|
832 |
|
833 |
|
834 |
|
835 |
|
836 |
|
837 |
|
838 |
|
839 |
|
840 |
|
841 | Core.prototype.zoomIn = function(percentage, options, callback) {
|
842 | if (!percentage || percentage < 0 || percentage > 1) return;
|
843 | if (typeof arguments[1] == "function") {
|
844 | callback = arguments[1];
|
845 | options = {};
|
846 | }
|
847 | var range = this.getWindow();
|
848 | var start = range.start.valueOf();
|
849 | var end = range.end.valueOf();
|
850 | var interval = end - start;
|
851 | var newInterval = interval / (1 + percentage);
|
852 | var distance = (interval - newInterval) / 2;
|
853 | var newStart = start + distance;
|
854 | var newEnd = end - distance;
|
855 |
|
856 | this.setWindow(newStart, newEnd, options, callback);
|
857 | };
|
858 |
|
859 |
|
860 |
|
861 |
|
862 |
|
863 |
|
864 |
|
865 |
|
866 |
|
867 |
|
868 |
|
869 |
|
870 |
|
871 | Core.prototype.zoomOut = function(percentage, options, callback) {
|
872 | if (!percentage || percentage < 0 || percentage > 1) return
|
873 | if (typeof arguments[1] == "function") {
|
874 | callback = arguments[1];
|
875 | options = {};
|
876 | }
|
877 | var range = this.getWindow();
|
878 | var start = range.start.valueOf();
|
879 | var end = range.end.valueOf();
|
880 | var interval = end - start;
|
881 | var newStart = start - interval * percentage / 2;
|
882 | var newEnd = end + interval * percentage / 2;
|
883 |
|
884 | this.setWindow(newStart, newEnd, options, callback);
|
885 | };
|
886 |
|
887 |
|
888 |
|
889 |
|
890 |
|
891 |
|
892 | Core.prototype.redraw = function() {
|
893 | this._redraw();
|
894 | };
|
895 |
|
896 |
|
897 |
|
898 |
|
899 |
|
900 |
|
901 | Core.prototype._redraw = function() {
|
902 | this.redrawCount++;
|
903 | var resized = false;
|
904 | var options = this.options;
|
905 | var props = this.props;
|
906 | var dom = this.dom;
|
907 |
|
908 | if (!dom || !dom.container || dom.root.offsetWidth == 0) return;
|
909 |
|
910 | DateUtil.updateHiddenDates(this.options.moment, this.body, this.options.hiddenDates);
|
911 |
|
912 |
|
913 | if (options.orientation == 'top') {
|
914 | util.addClassName(dom.root, 'vis-top');
|
915 | util.removeClassName(dom.root, 'vis-bottom');
|
916 | }
|
917 | else {
|
918 | util.removeClassName(dom.root, 'vis-top');
|
919 | util.addClassName(dom.root, 'vis-bottom');
|
920 | }
|
921 |
|
922 |
|
923 | dom.root.style.maxHeight = util.option.asSize(options.maxHeight, '');
|
924 | dom.root.style.minHeight = util.option.asSize(options.minHeight, '');
|
925 | dom.root.style.width = util.option.asSize(options.width, '');
|
926 |
|
927 |
|
928 | props.border.left = (dom.centerContainer.offsetWidth - dom.centerContainer.clientWidth) / 2;
|
929 | props.border.right = props.border.left;
|
930 | props.border.top = (dom.centerContainer.offsetHeight - dom.centerContainer.clientHeight) / 2;
|
931 | props.border.bottom = props.border.top;
|
932 | props.borderRootHeight= dom.root.offsetHeight - dom.root.clientHeight;
|
933 | props.borderRootWidth = dom.root.offsetWidth - dom.root.clientWidth;
|
934 |
|
935 |
|
936 |
|
937 | if (dom.centerContainer.clientHeight === 0) {
|
938 | props.border.left = props.border.top;
|
939 | props.border.right = props.border.left;
|
940 | }
|
941 | if (dom.root.clientHeight === 0) {
|
942 | props.borderRootWidth = props.borderRootHeight;
|
943 | }
|
944 |
|
945 |
|
946 |
|
947 | props.center.height = dom.center.offsetHeight;
|
948 | props.left.height = dom.left.offsetHeight;
|
949 | props.right.height = dom.right.offsetHeight;
|
950 | props.top.height = dom.top.clientHeight || -props.border.top;
|
951 | props.bottom.height = dom.bottom.clientHeight || -props.border.bottom;
|
952 |
|
953 |
|
954 |
|
955 |
|
956 |
|
957 | var contentHeight = Math.max(props.left.height, props.center.height, props.right.height);
|
958 | var autoHeight = props.top.height + contentHeight + props.bottom.height +
|
959 | props.borderRootHeight + props.border.top + props.border.bottom;
|
960 | dom.root.style.height = util.option.asSize(options.height, autoHeight + 'px');
|
961 |
|
962 |
|
963 | props.root.height = dom.root.offsetHeight;
|
964 | props.background.height = props.root.height - props.borderRootHeight;
|
965 | var containerHeight = props.root.height - props.top.height - props.bottom.height -
|
966 | props.borderRootHeight;
|
967 | props.centerContainer.height = containerHeight;
|
968 | props.leftContainer.height = containerHeight;
|
969 | props.rightContainer.height = props.leftContainer.height;
|
970 |
|
971 |
|
972 | props.root.width = dom.root.offsetWidth;
|
973 | props.background.width = props.root.width - props.borderRootWidth;
|
974 |
|
975 | if (!this.initialDrawDone) {
|
976 | props.scrollbarWidth = util.getScrollBarWidth();
|
977 | }
|
978 |
|
979 | if (options.verticalScroll) {
|
980 | if (options.rtl) {
|
981 | props.left.width = dom.leftContainer.clientWidth || -props.border.left;
|
982 | props.right.width = dom.rightContainer.clientWidth + props.scrollbarWidth || -props.border.right;
|
983 | } else {
|
984 | props.left.width = dom.leftContainer.clientWidth + props.scrollbarWidth || -props.border.left;
|
985 | props.right.width = dom.rightContainer.clientWidth || -props.border.right;
|
986 | }
|
987 | } else {
|
988 | props.left.width = dom.leftContainer.clientWidth || -props.border.left;
|
989 | props.right.width = dom.rightContainer.clientWidth || -props.border.right;
|
990 | }
|
991 |
|
992 | this._setDOM();
|
993 |
|
994 |
|
995 |
|
996 | var offset = this._updateScrollTop();
|
997 |
|
998 |
|
999 | if (options.orientation.item != 'top') {
|
1000 | offset += Math.max(props.centerContainer.height - props.center.height -
|
1001 | props.border.top - props.border.bottom, 0);
|
1002 | }
|
1003 | dom.center.style.top = offset + 'px';
|
1004 |
|
1005 |
|
1006 | var visibilityTop = props.scrollTop == 0 ? 'hidden' : '';
|
1007 | var visibilityBottom = props.scrollTop == props.scrollTopMin ? 'hidden' : '';
|
1008 | dom.shadowTop.style.visibility = visibilityTop;
|
1009 | dom.shadowBottom.style.visibility = visibilityBottom;
|
1010 | dom.shadowTopLeft.style.visibility = visibilityTop;
|
1011 | dom.shadowBottomLeft.style.visibility = visibilityBottom;
|
1012 | dom.shadowTopRight.style.visibility = visibilityTop;
|
1013 | dom.shadowBottomRight.style.visibility = visibilityBottom;
|
1014 |
|
1015 | if (options.verticalScroll) {
|
1016 | dom.rightContainer.className = 'vis-panel vis-right vis-vertical-scroll';
|
1017 | dom.leftContainer.className = 'vis-panel vis-left vis-vertical-scroll';
|
1018 |
|
1019 | dom.shadowTopRight.style.visibility = "hidden";
|
1020 | dom.shadowBottomRight.style.visibility = "hidden";
|
1021 | dom.shadowTopLeft.style.visibility = "hidden";
|
1022 | dom.shadowBottomLeft.style.visibility = "hidden";
|
1023 |
|
1024 | dom.left.style.top = '0px';
|
1025 | dom.right.style.top = '0px';
|
1026 | }
|
1027 |
|
1028 | if (!options.verticalScroll || props.center.height < props.centerContainer.height) {
|
1029 | dom.left.style.top = offset + 'px';
|
1030 | dom.right.style.top = offset + 'px';
|
1031 | dom.rightContainer.className = dom.rightContainer.className.replace(new RegExp('(?:^|\\s)'+ 'vis-vertical-scroll' + '(?:\\s|$)'), ' ');
|
1032 | dom.leftContainer.className = dom.leftContainer.className.replace(new RegExp('(?:^|\\s)'+ 'vis-vertical-scroll' + '(?:\\s|$)'), ' ');
|
1033 | props.left.width = dom.leftContainer.clientWidth || -props.border.left;
|
1034 | props.right.width = dom.rightContainer.clientWidth || -props.border.right;
|
1035 | this._setDOM();
|
1036 | }
|
1037 |
|
1038 |
|
1039 | var contentsOverflow = props.center.height > props.centerContainer.height;
|
1040 | this.hammer.get('pan').set({
|
1041 | direction: contentsOverflow ? Hammer.DIRECTION_ALL : Hammer.DIRECTION_HORIZONTAL
|
1042 | });
|
1043 |
|
1044 |
|
1045 | this.components.forEach(function (component) {
|
1046 | resized = component.redraw() || resized;
|
1047 | });
|
1048 | var MAX_REDRAW = 5;
|
1049 | if (resized) {
|
1050 | if (this.redrawCount < MAX_REDRAW) {
|
1051 | this.body.emitter.emit('_change');
|
1052 | return;
|
1053 | }
|
1054 | else {
|
1055 | console.log('WARNING: infinite loop in redraw?');
|
1056 | }
|
1057 | } else {
|
1058 | this.redrawCount = 0;
|
1059 | }
|
1060 |
|
1061 |
|
1062 | this.body.emitter.emit("changed");
|
1063 | };
|
1064 |
|
1065 | Core.prototype._setDOM = function () {
|
1066 | var props = this.props;
|
1067 | var dom = this.dom;
|
1068 |
|
1069 | props.leftContainer.width = props.left.width;
|
1070 | props.rightContainer.width = props.right.width;
|
1071 | var centerWidth = props.root.width - props.left.width - props.right.width - props.borderRootWidth;
|
1072 | props.center.width = centerWidth;
|
1073 | props.centerContainer.width = centerWidth;
|
1074 | props.top.width = centerWidth;
|
1075 | props.bottom.width = centerWidth;
|
1076 |
|
1077 |
|
1078 | dom.background.style.height = props.background.height + 'px';
|
1079 | dom.backgroundVertical.style.height = props.background.height + 'px';
|
1080 | dom.backgroundHorizontal.style.height = props.centerContainer.height + 'px';
|
1081 | dom.centerContainer.style.height = props.centerContainer.height + 'px';
|
1082 | dom.leftContainer.style.height = props.leftContainer.height + 'px';
|
1083 | dom.rightContainer.style.height = props.rightContainer.height + 'px';
|
1084 |
|
1085 | dom.background.style.width = props.background.width + 'px';
|
1086 | dom.backgroundVertical.style.width = props.centerContainer.width + 'px';
|
1087 | dom.backgroundHorizontal.style.width = props.background.width + 'px';
|
1088 | dom.centerContainer.style.width = props.center.width + 'px';
|
1089 | dom.top.style.width = props.top.width + 'px';
|
1090 | dom.bottom.style.width = props.bottom.width + 'px';
|
1091 |
|
1092 |
|
1093 | dom.background.style.left = '0';
|
1094 | dom.background.style.top = '0';
|
1095 | dom.backgroundVertical.style.left = (props.left.width + props.border.left) + 'px';
|
1096 | dom.backgroundVertical.style.top = '0';
|
1097 | dom.backgroundHorizontal.style.left = '0';
|
1098 | dom.backgroundHorizontal.style.top = props.top.height + 'px';
|
1099 | dom.centerContainer.style.left = props.left.width + 'px';
|
1100 | dom.centerContainer.style.top = props.top.height + 'px';
|
1101 | dom.leftContainer.style.left = '0';
|
1102 | dom.leftContainer.style.top = props.top.height + 'px';
|
1103 | dom.rightContainer.style.left = (props.left.width + props.center.width) + 'px';
|
1104 | dom.rightContainer.style.top = props.top.height + 'px';
|
1105 | dom.top.style.left = props.left.width + 'px';
|
1106 | dom.top.style.top = '0';
|
1107 | dom.bottom.style.left = props.left.width + 'px';
|
1108 | dom.bottom.style.top = (props.top.height + props.centerContainer.height) + 'px';
|
1109 | dom.center.style.left = '0';
|
1110 | dom.left.style.left = '0';
|
1111 | dom.right.style.left = '0';
|
1112 | };
|
1113 |
|
1114 |
|
1115 | Core.prototype.repaint = function () {
|
1116 | throw new Error('Function repaint is deprecated. Use redraw instead.');
|
1117 | };
|
1118 |
|
1119 |
|
1120 |
|
1121 |
|
1122 |
|
1123 |
|
1124 |
|
1125 |
|
1126 | Core.prototype.setCurrentTime = function(time) {
|
1127 | if (!this.currentTime) {
|
1128 | throw new Error('Option showCurrentTime must be true');
|
1129 | }
|
1130 |
|
1131 | this.currentTime.setCurrentTime(time);
|
1132 | };
|
1133 |
|
1134 |
|
1135 |
|
1136 |
|
1137 |
|
1138 |
|
1139 | Core.prototype.getCurrentTime = function() {
|
1140 | if (!this.currentTime) {
|
1141 | throw new Error('Option showCurrentTime must be true');
|
1142 | }
|
1143 |
|
1144 | return this.currentTime.getCurrentTime();
|
1145 | };
|
1146 |
|
1147 |
|
1148 |
|
1149 |
|
1150 |
|
1151 |
|
1152 |
|
1153 |
|
1154 | Core.prototype._toTime = function(x) {
|
1155 | return DateUtil.toTime(this, x, this.props.center.width);
|
1156 | };
|
1157 |
|
1158 |
|
1159 |
|
1160 |
|
1161 |
|
1162 |
|
1163 |
|
1164 |
|
1165 | Core.prototype._toGlobalTime = function(x) {
|
1166 | return DateUtil.toTime(this, x, this.props.root.width);
|
1167 |
|
1168 |
|
1169 | };
|
1170 |
|
1171 |
|
1172 |
|
1173 |
|
1174 |
|
1175 |
|
1176 |
|
1177 |
|
1178 |
|
1179 | Core.prototype._toScreen = function(time) {
|
1180 | return DateUtil.toScreen(this, time, this.props.center.width);
|
1181 | };
|
1182 |
|
1183 |
|
1184 |
|
1185 |
|
1186 |
|
1187 |
|
1188 |
|
1189 |
|
1190 |
|
1191 |
|
1192 |
|
1193 |
|
1194 | Core.prototype._toGlobalScreen = function(time) {
|
1195 | return DateUtil.toScreen(this, time, this.props.root.width);
|
1196 |
|
1197 |
|
1198 | };
|
1199 |
|
1200 |
|
1201 |
|
1202 |
|
1203 |
|
1204 |
|
1205 | Core.prototype._initAutoResize = function () {
|
1206 | if (this.options.autoResize == true) {
|
1207 | this._startAutoResize();
|
1208 | }
|
1209 | else {
|
1210 | this._stopAutoResize();
|
1211 | }
|
1212 | };
|
1213 |
|
1214 |
|
1215 |
|
1216 |
|
1217 |
|
1218 |
|
1219 | Core.prototype._startAutoResize = function () {
|
1220 | var me = this;
|
1221 |
|
1222 | this._stopAutoResize();
|
1223 |
|
1224 | this._onResize = function() {
|
1225 | if (me.options.autoResize != true) {
|
1226 |
|
1227 | me._stopAutoResize();
|
1228 | return;
|
1229 | }
|
1230 |
|
1231 | if (me.dom.root) {
|
1232 |
|
1233 |
|
1234 |
|
1235 |
|
1236 | if ((me.dom.root.offsetWidth != me.props.lastWidth) ||
|
1237 | (me.dom.root.offsetHeight != me.props.lastHeight)) {
|
1238 | me.props.lastWidth = me.dom.root.offsetWidth;
|
1239 | me.props.lastHeight = me.dom.root.offsetHeight;
|
1240 | me.props.scrollbarWidth = util.getScrollBarWidth();
|
1241 |
|
1242 | me.body.emitter.emit('_change');
|
1243 | }
|
1244 | }
|
1245 | };
|
1246 |
|
1247 |
|
1248 | util.addEventListener(window, 'resize', this._onResize);
|
1249 |
|
1250 |
|
1251 | if (me.dom.root) {
|
1252 | me.props.lastWidth = me.dom.root.offsetWidth;
|
1253 | me.props.lastHeight = me.dom.root.offsetHeight;
|
1254 | }
|
1255 |
|
1256 | this.watchTimer = setInterval(this._onResize, 1000);
|
1257 | };
|
1258 |
|
1259 |
|
1260 |
|
1261 |
|
1262 |
|
1263 | Core.prototype._stopAutoResize = function () {
|
1264 | if (this.watchTimer) {
|
1265 | clearInterval(this.watchTimer);
|
1266 | this.watchTimer = undefined;
|
1267 | }
|
1268 |
|
1269 |
|
1270 | if (this._onResize) {
|
1271 | util.removeEventListener(window, 'resize', this._onResize);
|
1272 | this._onResize = null;
|
1273 | }
|
1274 | };
|
1275 |
|
1276 |
|
1277 |
|
1278 |
|
1279 |
|
1280 |
|
1281 | Core.prototype._onTouch = function (event) {
|
1282 | this.touch.allowDragging = true;
|
1283 | this.touch.initialScrollTop = this.props.scrollTop;
|
1284 | };
|
1285 |
|
1286 |
|
1287 |
|
1288 |
|
1289 |
|
1290 |
|
1291 | Core.prototype._onPinch = function (event) {
|
1292 | this.touch.allowDragging = false;
|
1293 | };
|
1294 |
|
1295 |
|
1296 |
|
1297 |
|
1298 |
|
1299 |
|
1300 | Core.prototype._onDrag = function (event) {
|
1301 | if (!event) return
|
1302 |
|
1303 |
|
1304 | if (!this.touch.allowDragging) return;
|
1305 |
|
1306 | var delta = event.deltaY;
|
1307 |
|
1308 | var oldScrollTop = this._getScrollTop();
|
1309 | var newScrollTop = this._setScrollTop(this.touch.initialScrollTop + delta);
|
1310 |
|
1311 | if (this.options.verticalScroll) {
|
1312 | this.dom.left.parentNode.scrollTop = -this.props.scrollTop;
|
1313 | this.dom.right.parentNode.scrollTop = -this.props.scrollTop;
|
1314 | }
|
1315 |
|
1316 | if (newScrollTop != oldScrollTop) {
|
1317 | this.emit("verticalDrag");
|
1318 | }
|
1319 | };
|
1320 |
|
1321 |
|
1322 |
|
1323 |
|
1324 |
|
1325 |
|
1326 |
|
1327 | Core.prototype._setScrollTop = function (scrollTop) {
|
1328 | this.props.scrollTop = scrollTop;
|
1329 | this._updateScrollTop();
|
1330 | return this.props.scrollTop;
|
1331 | };
|
1332 |
|
1333 |
|
1334 |
|
1335 |
|
1336 |
|
1337 |
|
1338 | Core.prototype._updateScrollTop = function () {
|
1339 |
|
1340 | var scrollTopMin = Math.min(this.props.centerContainer.height - this.props.center.height, 0);
|
1341 | if (scrollTopMin != this.props.scrollTopMin) {
|
1342 |
|
1343 |
|
1344 | if (this.options.orientation.item != 'top') {
|
1345 | this.props.scrollTop += (scrollTopMin - this.props.scrollTopMin);
|
1346 | }
|
1347 | this.props.scrollTopMin = scrollTopMin;
|
1348 | }
|
1349 |
|
1350 |
|
1351 | if (this.props.scrollTop > 0) this.props.scrollTop = 0;
|
1352 | if (this.props.scrollTop < scrollTopMin) this.props.scrollTop = scrollTopMin;
|
1353 |
|
1354 | if (this.options.verticalScroll) {
|
1355 | this.dom.left.parentNode.scrollTop = -this.props.scrollTop;
|
1356 | this.dom.right.parentNode.scrollTop = -this.props.scrollTop;
|
1357 | }
|
1358 |
|
1359 | return this.props.scrollTop;
|
1360 | };
|
1361 |
|
1362 |
|
1363 |
|
1364 |
|
1365 |
|
1366 |
|
1367 | Core.prototype._getScrollTop = function () {
|
1368 | return this.props.scrollTop;
|
1369 | };
|
1370 |
|
1371 |
|
1372 |
|
1373 |
|
1374 |
|
1375 |
|
1376 | Core.prototype._createConfigurator = function () {
|
1377 | throw new Error('Cannot invoke abstract method _createConfigurator');
|
1378 | };
|
1379 |
|
1380 | export default Core;
|