1 | var util = require('vis-util');
|
2 | var Component = require('./Component');
|
3 | var moment = require('../../module/moment');
|
4 | var locales = require('../locales');
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function CurrentTime (body, options) {
|
16 | this.body = body;
|
17 |
|
18 |
|
19 | this.defaultOptions = {
|
20 | rtl: false,
|
21 | showCurrentTime: true,
|
22 | alignCurrentTime: undefined,
|
23 |
|
24 | moment: moment,
|
25 | locales: locales,
|
26 | locale: 'en'
|
27 | };
|
28 | this.options = util.extend({}, this.defaultOptions);
|
29 | this.offset = 0;
|
30 |
|
31 | this._create();
|
32 |
|
33 | this.setOptions(options);
|
34 | }
|
35 |
|
36 | CurrentTime.prototype = new Component();
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | CurrentTime.prototype._create = function() {
|
43 | var bar = document.createElement('div');
|
44 | bar.className = 'vis-current-time';
|
45 | bar.style.position = 'absolute';
|
46 | bar.style.top = '0px';
|
47 | bar.style.height = '100%';
|
48 |
|
49 | this.bar = bar;
|
50 | };
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | CurrentTime.prototype.destroy = function () {
|
56 | this.options.showCurrentTime = false;
|
57 | this.redraw();
|
58 |
|
59 | this.body = null;
|
60 | };
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | CurrentTime.prototype.setOptions = function(options) {
|
69 | if (options) {
|
70 |
|
71 | util.selectiveExtend(['rtl', 'showCurrentTime', 'alignCurrentTime', 'moment', 'locale', 'locales'], this.options, options);
|
72 | }
|
73 | };
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | CurrentTime.prototype.redraw = function() {
|
80 | if (this.options.showCurrentTime) {
|
81 | var parent = this.body.dom.backgroundVertical;
|
82 | if (this.bar.parentNode != parent) {
|
83 |
|
84 | if (this.bar.parentNode) {
|
85 | this.bar.parentNode.removeChild(this.bar);
|
86 | }
|
87 | parent.appendChild(this.bar);
|
88 |
|
89 | this.start();
|
90 | }
|
91 |
|
92 | var now = this.options.moment(new Date().valueOf() + this.offset);
|
93 |
|
94 | if (this.options.alignCurrentTime) {
|
95 | now = now.startOf(this.options.alignCurrentTime);
|
96 | }
|
97 |
|
98 | var x = this.body.util.toScreen(now);
|
99 |
|
100 | var locale = this.options.locales[this.options.locale];
|
101 | if (!locale) {
|
102 | if (!this.warned) {
|
103 | console.log('WARNING: options.locales[\'' + this.options.locale + '\'] not found. See http://visjs.org/docs/timeline/#Localization');
|
104 | this.warned = true;
|
105 | }
|
106 | locale = this.options.locales['en'];
|
107 | }
|
108 | var title = locale.current + ' ' + locale.time + ': ' + now.format('dddd, MMMM Do YYYY, H:mm:ss');
|
109 | title = title.charAt(0).toUpperCase() + title.substring(1);
|
110 |
|
111 | if (this.options.rtl) {
|
112 | this.bar.style.right = x + 'px';
|
113 | } else {
|
114 | this.bar.style.left = x + 'px';
|
115 | }
|
116 | this.bar.title = title;
|
117 | }
|
118 | else {
|
119 |
|
120 | if (this.bar.parentNode) {
|
121 | this.bar.parentNode.removeChild(this.bar);
|
122 | }
|
123 | this.stop();
|
124 | }
|
125 |
|
126 | return false;
|
127 | };
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | CurrentTime.prototype.start = function() {
|
133 | var me = this;
|
134 |
|
135 | |
136 |
|
137 |
|
138 | function update () {
|
139 | me.stop();
|
140 |
|
141 |
|
142 | var scale = me.body.range.conversion(me.body.domProps.center.width).scale;
|
143 | var interval = 1 / scale / 10;
|
144 | if (interval < 30) interval = 30;
|
145 | if (interval > 1000) interval = 1000;
|
146 |
|
147 | me.redraw();
|
148 | me.body.emitter.emit('currentTimeTick');
|
149 |
|
150 |
|
151 | me.currentTimeTimer = setTimeout(update, interval);
|
152 | }
|
153 |
|
154 | update();
|
155 | };
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | CurrentTime.prototype.stop = function() {
|
161 | if (this.currentTimeTimer !== undefined) {
|
162 | clearTimeout(this.currentTimeTimer);
|
163 | delete this.currentTimeTimer;
|
164 | }
|
165 | };
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | CurrentTime.prototype.setCurrentTime = function(time) {
|
174 | var t = util.convert(time, 'Date').valueOf();
|
175 | var now = new Date().valueOf();
|
176 | this.offset = t - now;
|
177 | this.redraw();
|
178 | };
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 | CurrentTime.prototype.getCurrentTime = function() {
|
185 | return new Date(new Date().valueOf() + this.offset);
|
186 | };
|
187 |
|
188 | module.exports = CurrentTime;
|