From 01207417f3175dca90e6a23bc6237407602d13e5 Mon Sep 17 00:00:00 2001 From: Oleg Yapparov Date: Sun, 22 Feb 2015 13:51:21 +0100 Subject: [PATCH] Added id parameter to set/getCustomTime methods, docs update --- docs/timeline.html | 16 ++++++--- examples/timeline/34_add_custom_timebar.html | 6 +++- lib/timeline/Core.js | 35 ++++++++++++++++---- 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/docs/timeline.html b/docs/timeline.html index 391ff5cc..aebcac8d 100644 --- a/docs/timeline.html +++ b/docs/timeline.html @@ -910,9 +910,9 @@ timeline.clear({options: true}); // clear options only - getCustomTime() + getCustomTime([id]) Date - Retrieve the custom time. Only applicable when the option showCustomTime is true. + Retrieve the custom time. Only applicable when the option showCustomTime is true. If parameter id is provided, time of the custom time bar under that ID is returned. @@ -965,6 +965,14 @@ timeline.clear({options: true}); // clear options only + + removeCustomTime(id) + none + + Remove vertical bars previously added to the timeline via addCustomTime method. Parameter id is the ID of the custom vertical bar returned by addCustomTime method. + + + setCurrentTime(time) none @@ -974,9 +982,9 @@ timeline.clear({options: true}); // clear options only - setCustomTime(time) + setCustomTime(time [, id]) none - 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. + 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. Parameter id represents ID of the custom time bar, provided by addCustomTime method. diff --git a/examples/timeline/34_add_custom_timebar.html b/examples/timeline/34_add_custom_timebar.html index 426cb93a..9afab8b8 100644 --- a/examples/timeline/34_add_custom_timebar.html +++ b/examples/timeline/34_add_custom_timebar.html @@ -21,7 +21,7 @@

- +

timechange bar index: event: @@ -44,6 +44,10 @@ }; var timeline = new vis.Timeline(container, items, options); + // Set first time bar + customDate = new Date(customDate.getFullYear(), customDate.getMonth(), customDate.getDate() + 1); + timeline.addCustomTime(customDate); + document.getElementById('add').onclick = function () { customDate = new Date(customDate.getFullYear(), customDate.getMonth(), customDate.getDate() + 1); timeline.addCustomTime(customDate); diff --git a/lib/timeline/Core.js b/lib/timeline/Core.js index b018fac1..432089b5 100644 --- a/lib/timeline/Core.js +++ b/lib/timeline/Core.js @@ -282,25 +282,42 @@ Core.prototype.destroy = function () { /** * Set a custom time bar * @param {Date} time + * @param {int} id */ -Core.prototype.setCustomTime = function (time) { +Core.prototype.setCustomTime = function (time, id) { if (!this.customTime) { throw new Error('Cannot get custom time: Custom time bar is not enabled'); } - this.customTime.setCustomTime(time); + var barId = util.option.asNumber(id) || 0; + + this.components.forEach(function (element, index, components) { + if (element instanceof CustomTime && element.options.id === barId) { + element.setCustomTime(time); + } + }); }; /** * Retrieve the current custom time. * @return {Date} customTime + * @param {int} id */ -Core.prototype.getCustomTime = function() { +Core.prototype.getCustomTime = function(id) { if (!this.customTime) { throw new Error('Cannot get custom time: Custom time bar is not enabled'); } - return this.customTime.getCustomTime(); + var barId = util.option.asNumber(id) || 0, + customTime = this.customTime.getCustomTime();; + + this.components.forEach(function (element, index, components) { + if (element instanceof CustomTime && element.options.id === barId) { + customTime = element.getCustomTime(); + } + }); + + return customTime; }; /** @@ -355,9 +372,13 @@ Core.prototype.removeCustomTime = function (id) { this.components.forEach(function (bar, index, components) { if (bar instanceof CustomTime && bar.options.id === id) { - reduceLastId(); - components.splice(index, 1); - bar.destroy(); + + // Only the lines added by the user will be removed + if (bar.options.id > 0) { + reduceLastId(); + components.splice(index, 1); + bar.destroy(); + } } }); };