2015-09-02 23:31:58 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* Open MCT Web, Copyright (c) 2014-2015, United States Government
|
|
|
|
* as represented by the Administrator of the National Aeronautics and Space
|
|
|
|
* Administration. All rights reserved.
|
|
|
|
*
|
|
|
|
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0.
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
* Open MCT Web includes source code licensed under additional open source
|
|
|
|
* licenses. See the Open Source Licenses file (LICENSES.md) included with
|
|
|
|
* this source code distribution or the Licensing information page available
|
|
|
|
* at runtime from the About dialog for additional information.
|
|
|
|
*****************************************************************************/
|
|
|
|
/*global define,Promise*/
|
|
|
|
|
|
|
|
define(
|
2015-09-02 23:53:10 +00:00
|
|
|
['moment'],
|
|
|
|
function (moment) {
|
2015-09-02 23:31:58 +00:00
|
|
|
"use strict";
|
|
|
|
|
2015-09-02 23:53:10 +00:00
|
|
|
var DATE_FORMAT = "YYYY-MM-DD HH:mm:ss";
|
|
|
|
|
2015-09-02 23:31:58 +00:00
|
|
|
/**
|
|
|
|
* @memberof platform/commonUI/general
|
|
|
|
* @constructor
|
|
|
|
*/
|
2015-09-02 23:53:10 +00:00
|
|
|
function TimeConductorController($scope, now) {
|
2015-09-03 00:19:20 +00:00
|
|
|
var tickCount = 2,
|
|
|
|
initialDragValue;
|
|
|
|
|
2015-09-02 23:53:10 +00:00
|
|
|
function formatTimestamp(ts) {
|
|
|
|
return moment.utc(ts).format(DATE_FORMAT);
|
|
|
|
}
|
2015-09-02 23:31:58 +00:00
|
|
|
|
2015-09-03 18:03:17 +00:00
|
|
|
function parseTimestamp(text, fallback) {
|
|
|
|
var m = moment.utc(text, DATE_FORMAT);
|
|
|
|
return m.isValid() ? m.valueOf() : fallback;
|
|
|
|
}
|
|
|
|
|
2015-09-02 23:53:10 +00:00
|
|
|
// From 0.0-1.0 to "0%"-"1%"
|
|
|
|
function toPercent(p) {
|
|
|
|
return (100 * p) + "%";
|
|
|
|
}
|
2015-09-02 23:31:58 +00:00
|
|
|
|
|
|
|
function updateTicks() {
|
2015-09-02 23:53:10 +00:00
|
|
|
var i, p, ts, start, end, span;
|
|
|
|
end = $scope.ngModel.outer[1];
|
|
|
|
start = $scope.ngModel.outer[0];
|
|
|
|
span = end - start;
|
2015-09-02 23:31:58 +00:00
|
|
|
$scope.ticks = [];
|
|
|
|
for (i = 0; i < tickCount; i += 1) {
|
2015-09-02 23:53:10 +00:00
|
|
|
p = i / (tickCount - 1);
|
|
|
|
ts = p * span + start;
|
|
|
|
$scope.ticks.push(formatTimestamp(ts));
|
2015-09-02 23:31:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateSpanWidth(w) {
|
|
|
|
// Space about 100px apart
|
|
|
|
tickCount = Math.max(Math.floor(w / 100), 2);
|
|
|
|
updateTicks();
|
|
|
|
}
|
|
|
|
|
2015-09-03 18:03:17 +00:00
|
|
|
function updateViewFromModel(ngModel) {
|
2015-09-02 23:53:10 +00:00
|
|
|
var t = now(), span;
|
|
|
|
|
2015-09-03 18:03:17 +00:00
|
|
|
ngModel = ngModel || {};
|
|
|
|
ngModel.outer = ngModel.outer || [ t - 24 * 3600 * 1000, t ];
|
|
|
|
ngModel.inner = ngModel.inner || [ t - 24 * 3600 * 1000, t ];
|
2015-09-02 23:53:10 +00:00
|
|
|
|
|
|
|
// First, dates for the date pickers for outer bounds
|
2015-09-03 18:03:17 +00:00
|
|
|
$scope.startOuterDate = formatTimestamp(ngModel.outer[0]);
|
|
|
|
$scope.endOuterDate = formatTimestamp(ngModel.outer[1]);
|
2015-09-02 23:53:10 +00:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
2015-09-03 00:19:20 +00:00
|
|
|
function startLeftDrag() {
|
|
|
|
initialDragValue = $scope.ngModel.inner[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
function startRightDrag() {
|
|
|
|
initialDragValue = $scope.ngModel.inner[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
function startMiddleDrag() {
|
|
|
|
initialDragValue = [
|
|
|
|
$scope.ngModel.inner[0],
|
2015-09-03 00:25:41 +00:00
|
|
|
$scope.ngModel.inner[1]
|
2015-09-03 00:19:20 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
function toMillis(pixels) {
|
|
|
|
var span = $scope.ngModel.outer[1] - $scope.ngModel.outer[0];
|
|
|
|
return (pixels / $scope.spanWidth) * span;
|
|
|
|
}
|
|
|
|
|
|
|
|
function clamp(value, low, high) {
|
|
|
|
return Math.max(low, Math.min(high, value));
|
|
|
|
}
|
|
|
|
|
|
|
|
function leftDrag(pixels) {
|
|
|
|
var delta = toMillis(pixels);
|
|
|
|
$scope.ngModel.inner[0] = clamp(
|
|
|
|
initialDragValue + delta,
|
|
|
|
$scope.ngModel.outer[0],
|
|
|
|
$scope.ngModel.inner[1]
|
|
|
|
);
|
2015-09-03 18:03:17 +00:00
|
|
|
updateViewFromModel($scope.ngModel);
|
2015-09-03 00:19:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function rightDrag(pixels) {
|
|
|
|
var delta = toMillis(pixels);
|
|
|
|
$scope.ngModel.inner[1] = clamp(
|
|
|
|
initialDragValue + delta,
|
|
|
|
$scope.ngModel.inner[0],
|
|
|
|
$scope.ngModel.outer[1]
|
|
|
|
);
|
2015-09-03 18:03:17 +00:00
|
|
|
updateViewFromModel($scope.ngModel);
|
2015-09-03 00:19:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function middleDrag(pixels) {
|
|
|
|
var delta = toMillis(pixels),
|
|
|
|
index = delta < 0 ? 0 : 1,
|
2015-09-03 00:25:41 +00:00
|
|
|
opposite = Math.abs(1 - index);
|
2015-09-03 00:19:20 +00:00
|
|
|
|
2015-09-03 00:25:41 +00:00
|
|
|
// Adjust the position of the edge in the direction of drag
|
2015-09-03 00:19:20 +00:00
|
|
|
$scope.ngModel.inner[index] = clamp(
|
|
|
|
initialDragValue[index] + delta,
|
|
|
|
$scope.ngModel.outer[0],
|
|
|
|
$scope.ngModel.outer[1]
|
|
|
|
);
|
2015-09-03 00:25:41 +00:00
|
|
|
// Adjust opposite knob to maintain span
|
|
|
|
$scope.ngModel.inner[opposite] = $scope.ngModel.inner[index] +
|
|
|
|
initialDragValue[opposite] - initialDragValue[index];
|
|
|
|
|
2015-09-03 18:03:17 +00:00
|
|
|
updateViewFromModel($scope.ngModel);
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateOuterStart(text) {
|
|
|
|
var ngModel = $scope.ngModel;
|
|
|
|
ngModel.outer[0] = parseTimestamp(text, ngModel.outer[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateOuterEnd(text) {
|
|
|
|
var ngModel = $scope.ngModel;
|
|
|
|
ngModel.outer[1] = parseTimestamp(text, ngModel.outer[1]);
|
2015-09-03 00:19:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$scope.startLeftDrag = startLeftDrag;
|
|
|
|
$scope.startRightDrag = startRightDrag;
|
2015-09-03 00:25:41 +00:00
|
|
|
$scope.startMiddleDrag = startMiddleDrag;
|
2015-09-03 00:19:20 +00:00
|
|
|
$scope.leftDrag = leftDrag;
|
|
|
|
$scope.rightDrag = rightDrag;
|
2015-09-03 00:25:41 +00:00
|
|
|
$scope.middleDrag = middleDrag;
|
2015-09-03 00:19:20 +00:00
|
|
|
|
|
|
|
$scope.state = false;
|
|
|
|
$scope.ticks = [];
|
|
|
|
|
2015-09-02 23:53:10 +00:00
|
|
|
// Initialize scope to defaults
|
2015-09-03 18:03:17 +00:00
|
|
|
updateViewFromModel($scope.ngModel);
|
2015-09-02 23:53:10 +00:00
|
|
|
|
2015-09-03 18:03:17 +00:00
|
|
|
$scope.$watchCollection("ngModel", updateViewFromModel);
|
2015-09-02 23:31:58 +00:00
|
|
|
$scope.$watch("spanWidth", updateSpanWidth);
|
2015-09-03 18:03:17 +00:00
|
|
|
$scope.$watch("startOuterDate", updateOuterStart);
|
|
|
|
$scope.$watch("endOuterDate", updateOuterEnd);
|
2015-09-02 23:31:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TimeConductorController;
|
|
|
|
}
|
|
|
|
);
|