From 1d21066ee2bc5bb2cdbe45c421d831aa39739503 Mon Sep 17 00:00:00 2001 From: jos Date: Mon, 25 Aug 2014 11:36:05 +0200 Subject: [PATCH] Fixed #171: Implemented functions `setCurrentTime(date)` and `getCurrentTime()` --- HISTORY.md | 8 +++++--- docs/timeline.html | 17 +++++++++++++++- lib/timeline/Core.js | 28 +++++++++++++++++++++++++++ lib/timeline/component/CurrentTime.js | 24 ++++++++++++++++++++++- lib/timeline/component/CustomTime.js | 4 ++-- 5 files changed, 74 insertions(+), 7 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index c3e656ae..7a217bd7 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -6,9 +6,6 @@ http://visjs.org ### Timeline -- Fixed the `change` event sometimes being fired twice on IE10. -- Fixed canceling moving an item to another group did not move the item - back to the original group. - Added localization support. - Implemented option `clickToUse`. - Implemented function `focus(id)` to center a specific item (or multiple items) @@ -17,6 +14,10 @@ http://visjs.org focus selected nodes. - Implemented animated range change for functions `fit`, `focus`, `setSelection`, and `setWindow`. +- Implemented functions `setCurrentTime(date)` and `getCurrentTime()`. +- Fixed the `change` event sometimes being fired twice on IE10. +- Fixed canceling moving an item to another group did not move the item + back to the original group. ### Network @@ -34,6 +35,7 @@ http://visjs.org - Added 'customRange' for the Y axis and an example showing how it works. - Added localization support. - Implemented option `clickToUse`. +- Implemented functions `setCurrentTime(date)` and `getCurrentTime()`. ## 2014-08-14, version 3.2.0 diff --git a/docs/timeline.html b/docs/timeline.html index 93fc27fa..9c1542c1 100644 --- a/docs/timeline.html +++ b/docs/timeline.html @@ -757,6 +757,13 @@ timeline.clear({options: true}); // clear options only + + getCurrentTime() + Date + Get the current time. Only applicable when option showCurrentTime is true. + + + getCustomTime() Date @@ -764,10 +771,18 @@ timeline.clear({options: true}); // clear options only + + setCurrentTime(time) + none + Set a current time. This can be used for example to ensure that a client's time is synchronized with a shared server time. + time can be a Date object, numeric timestamp, or ISO date string. + Only applicable when option showCurrentTime is true. + + setCustomTime(time) none - Adjust the custom time bar. Only applicable when the option showCustomTime is true. time is a Date object. + Adjust the custom time bar. Only applicable when the option showCustomTime is true. time can be a Date object, numeric timestamp, or ISO date string. diff --git a/lib/timeline/Core.js b/lib/timeline/Core.js index 2db38778..37b22513 100644 --- a/lib/timeline/Core.js +++ b/lib/timeline/Core.js @@ -550,6 +550,34 @@ Core.prototype.repaint = function () { throw new Error('Function repaint is deprecated. Use redraw instead.'); }; +/** + * Set a current time. This can be used for example to ensure that a client's + * time is synchronized with a shared server time. + * Only applicable when option `showCurrentTime` is true. + * @param {Date | String | Number} time A Date, unix timestamp, or + * ISO date string. + */ +Core.prototype.setCurrentTime = function(time) { + if (!this.currentTime) { + throw new Error('Option showCurrentTime must be true'); + } + + this.currentTime.setCurrentTime(time); +}; + +/** + * Get the current time. + * Only applicable when option `showCurrentTime` is true. + * @return {Date} Returns the current time. + */ +Core.prototype.getCurrentTime = function() { + if (!this.currentTime) { + throw new Error('Option showCurrentTime must be true'); + } + + return this.currentTime.getCurrentTime(); +}; + /** * Convert a position on screen (pixels) to a datetime * @param {int} x Position on the screen in pixels diff --git a/lib/timeline/component/CurrentTime.js b/lib/timeline/component/CurrentTime.js index f3193da8..2cbb4f70 100644 --- a/lib/timeline/component/CurrentTime.js +++ b/lib/timeline/component/CurrentTime.js @@ -22,6 +22,7 @@ function CurrentTime (body, options) { locale: 'en' }; this.options = util.extend({}, this.defaultOptions); + this.offset = 0; this._create(); @@ -83,7 +84,7 @@ CurrentTime.prototype.redraw = function() { this.start(); } - var now = new Date(); + var now = new Date(new Date().valueOf() + this.offset); var x = this.body.util.toScreen(now); var locale = this.options.locales[this.options.locale]; @@ -138,4 +139,25 @@ CurrentTime.prototype.stop = function() { } }; +/** + * Set a current time. This can be used for example to ensure that a client's + * time is synchronized with a shared server time. + * @param {Date | String | Number} time A Date, unix timestamp, or + * ISO date string. + */ +CurrentTime.prototype.setCurrentTime = function(time) { + var t = util.convert(time, 'Date').valueOf(); + var now = new Date().valueOf(); + this.offset = t - now; + this.redraw(); +}; + +/** + * Get the current time. + * @return {Date} Returns the current time. + */ +CurrentTime.prototype.getCurrentTime = function() { + return new Date(new Date().valueOf() + this.offset); +}; + module.exports = CurrentTime; diff --git a/lib/timeline/component/CustomTime.js b/lib/timeline/component/CustomTime.js index 85f04fcf..44c2a4da 100644 --- a/lib/timeline/component/CustomTime.js +++ b/lib/timeline/component/CustomTime.js @@ -125,10 +125,10 @@ CustomTime.prototype.redraw = function () { /** * Set custom time. - * @param {Date} time + * @param {Date | number | string} time */ CustomTime.prototype.setCustomTime = function(time) { - this.customTime = new Date(time.valueOf()); + this.customTime = util.convert(time, 'Date'); this.redraw(); };