vendor/assets/vis/timeline/component/CurrentTime.js in vis-rails-0.0.6 vs vendor/assets/vis/timeline/component/CurrentTime.js in vis-rails-1.0.0
- old
+ new
@@ -1,101 +1,96 @@
/**
* A current time bar
- * @param {Component} parent
- * @param {Component[]} [depends] Components on which this components depends
- * (except for the parent)
+ * @param {Range} range
* @param {Object} [options] Available parameters:
* {Boolean} [showCurrentTime]
* @constructor CurrentTime
* @extends Component
*/
-function CurrentTime (parent, depends, options) {
+function CurrentTime (range, options) {
this.id = util.randomUUID();
- this.parent = parent;
- this.depends = depends;
+ this.range = range;
this.options = options || {};
this.defaultOptions = {
showCurrentTime: false
};
+
+ this._create();
}
CurrentTime.prototype = new Component();
CurrentTime.prototype.setOptions = Component.prototype.setOptions;
/**
- * Get the container element of the bar, which can be used by a child to
- * add its own widgets.
- * @returns {HTMLElement} container
+ * Create the HTML DOM for the current time bar
+ * @private
*/
-CurrentTime.prototype.getContainer = function () {
- return this.frame;
+CurrentTime.prototype._create = function _create () {
+ var bar = document.createElement('div');
+ bar.className = 'currenttime';
+ bar.style.position = 'absolute';
+ bar.style.top = '0px';
+ bar.style.height = '100%';
+
+ this.bar = bar;
};
/**
+ * Get the frame element of the current time bar
+ * @returns {HTMLElement} frame
+ */
+CurrentTime.prototype.getFrame = function getFrame() {
+ return this.bar;
+};
+
+/**
* Repaint the component
- * @return {Boolean} changed
+ * @return {boolean} Returns true if the component is resized
*/
-CurrentTime.prototype.repaint = function () {
- var bar = this.frame,
- parent = this.parent,
- parentContainer = parent.parent.getContainer();
+CurrentTime.prototype.repaint = function repaint() {
+ var parent = this.parent;
- if (!parent) {
- throw new Error('Cannot repaint bar: no parent attached');
- }
+ var now = new Date();
+ var x = this.options.toScreen(now);
- if (!parentContainer) {
- throw new Error('Cannot repaint bar: parent has no container element');
- }
+ this.bar.style.left = x + 'px';
+ this.bar.title = 'Current time: ' + now;
- if (!this.getOption('showCurrentTime')) {
- if (bar) {
- parentContainer.removeChild(bar);
- delete this.frame;
- }
+ return false;
+};
- return false;
- }
+/**
+ * Start auto refreshing the current time bar
+ */
+CurrentTime.prototype.start = function start() {
+ var me = this;
- if (!bar) {
- bar = document.createElement('div');
- bar.className = 'currenttime';
- bar.style.position = 'absolute';
- bar.style.top = '0px';
- bar.style.height = '100%';
+ function update () {
+ me.stop();
- parentContainer.appendChild(bar);
- this.frame = bar;
- }
+ // determine interval to refresh
+ var scale = me.range.conversion(me.parent.width).scale;
+ var interval = 1 / scale / 10;
+ if (interval < 30) interval = 30;
+ if (interval > 1000) interval = 1000;
- if (!parent.conversion) {
- parent._updateConversion();
+ me.repaint();
+
+ // start a timer to adjust for the new time
+ me.currentTimeTimer = setTimeout(update, interval);
}
- var now = new Date();
- var x = parent.toScreen(now);
+ update();
+};
- bar.style.left = x + 'px';
- bar.title = 'Current time: ' + now;
-
- // start a timer to adjust for the new time
+/**
+ * Stop auto refreshing the current time bar
+ */
+CurrentTime.prototype.stop = function stop() {
if (this.currentTimeTimer !== undefined) {
clearTimeout(this.currentTimeTimer);
delete this.currentTimeTimer;
}
-
- var timeline = this;
- var interval = 1 / parent.conversion.scale / 2;
-
- if (interval < 30) {
- interval = 30;
- }
-
- this.currentTimeTimer = setTimeout(function() {
- timeline.repaint();
- }, interval);
-
- return false;
};