From a18cc50a43a3f35ec208ff09a83dfb7db1526ef4 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Wed, 2 Sep 2015 16:53:10 -0700 Subject: [PATCH] [Time Conductor] Begin binding control to data --- platform/commonUI/general/bundle.json | 2 +- .../templates/controls/time-controller.html | 4 +- .../controllers/TimeConductorController.js | 64 ++++++++++++++++--- 3 files changed, 58 insertions(+), 12 deletions(-) diff --git a/platform/commonUI/general/bundle.json b/platform/commonUI/general/bundle.json index cd239c7d08..73de6ba4a2 100644 --- a/platform/commonUI/general/bundle.json +++ b/platform/commonUI/general/bundle.json @@ -60,7 +60,7 @@ { "key": "TimeConductorController", "implementation": "controllers/TimeConductorController.js", - "depends": [ "$scope" ] + "depends": [ "$scope", "now" ] }, { "key": "TreeNodeController", diff --git a/platform/commonUI/general/res/templates/controls/time-controller.html b/platform/commonUI/general/res/templates/controls/time-controller.html index 346b1b7143..97638bfbd7 100644 --- a/platform/commonUI/general/res/templates/controls/time-controller.html +++ b/platform/commonUI/general/res/templates/controls/time-controller.html @@ -25,7 +25,7 @@ properly on the range left and right bounds.
Start: - End: + End:
@@ -41,7 +41,7 @@ properly on the range left and right bounds.
{{startInnerText}}
-
{{startOuterText}}
+
{{endInnerText}}
diff --git a/platform/commonUI/general/src/controllers/TimeConductorController.js b/platform/commonUI/general/src/controllers/TimeConductorController.js index a186651c0c..d87a844336 100644 --- a/platform/commonUI/general/src/controllers/TimeConductorController.js +++ b/platform/commonUI/general/src/controllers/TimeConductorController.js @@ -22,29 +22,41 @@ /*global define,Promise*/ define( - [], - function () { + ['moment'], + function (moment) { "use strict"; + var DATE_FORMAT = "YYYY-MM-DD HH:mm:ss"; + /** - * A ToggleController is used to activate/deactivate things. - * A common usage is for "twistie" - * * @memberof platform/commonUI/general * @constructor */ - function TimeConductorController($scope) { + function TimeConductorController($scope, now) { var tickCount = 2; $scope.state = false; + $scope.ticks = []; - $scope.ticks = [0, 1]; + function formatTimestamp(ts) { + return moment.utc(ts).format(DATE_FORMAT); + } + + // From 0.0-1.0 to "0%"-"1%" + function toPercent(p) { + return (100 * p) + "%"; + } function updateTicks() { - var i; + var i, p, ts, start, end, span; + end = $scope.ngModel.outer[1]; + start = $scope.ngModel.outer[0]; + span = end - start; $scope.ticks = []; for (i = 0; i < tickCount; i += 1) { - $scope.ticks.push(i / (tickCount - 1)); + p = i / (tickCount - 1); + ts = p * span + start; + $scope.ticks.push(formatTimestamp(ts)); } } @@ -54,7 +66,41 @@ define( updateTicks(); } + function updateFromParameters(ngModel) { + var t = now(), span; + + ngModel = ngModel || { + outer: [ t - 24 * 3600 * 1000, t ], + inner: [ t - 24 * 3600 * 1000, t ] + }; + + // First, dates for the date pickers for outer bounds + $scope.startOuterDate = new Date(ngModel.outer[0]); + $scope.endOuterDate = new Date(ngModel.outer[1]); + + // Then readable dates for the knobs + $scope.startInnerText = formatTimestamp(ngModel.inner[0]); + $scope.endInnerText = formatTimestamp(ngModel.inner[1]); + + // And positions for the knobs + span = ngModel.outer[1] - ngModel.outer[0]; + $scope.startInnerPct = + toPercent((ngModel.inner[0] - ngModel.outer[0]) / span); + $scope.endInnerPct = + toPercent((ngModel.outer[1] - ngModel.inner[1]) / span); + + // Stick it back is scope (in case we just set defaults) + $scope.ngModel = ngModel; + + updateTicks(); + } + + // Initialize scope to defaults + updateFromParameters(); + + $scope.$watchCollection("ngModel", updateFromParameters); $scope.$watch("spanWidth", updateSpanWidth); + } return TimeConductorController;