Merge pull request #475 from nasa/open325b

[Time Conductor] Update validation logic
This commit is contained in:
Victor Woeltjen 2016-01-27 12:44:14 -08:00
commit b5c8ecc85f
4 changed files with 43 additions and 43 deletions

View File

@ -24,7 +24,11 @@
<input type="text" <input type="text"
ng-model="textValue" ng-model="textValue"
ng-blur="restoreTextValue(); ngBlur()" ng-blur="restoreTextValue(); ngBlur()"
ng-class="{ error: textInvalid }"> ng-class="{
error: textInvalid ||
(structure.validate &&
!structure.validate(ngModel[field]))
}">
</input> </input>
<a class="ui-symbol icon icon-calendar" <a class="ui-symbol icon icon-calendar"
ng-if="structure.format === 'utc' || !structure.format" ng-if="structure.format === 'utc' || !structure.format"

View File

@ -25,7 +25,7 @@
<span class="l-time-range-inputs-elem ui-symbol type-icon">&#x43;</span> <span class="l-time-range-inputs-elem ui-symbol type-icon">&#x43;</span>
<span class="l-time-range-input"> <span class="l-time-range-input">
<mct-control key="'datetime-field'" <mct-control key="'datetime-field'"
structure="{ format: parameters.format }" structure="{ format: parameters.format, validate: validateStart }"
ng-model="formModel" ng-model="formModel"
ng-blur="updateBoundsFromForm()" ng-blur="updateBoundsFromForm()"
field="'start'" field="'start'"
@ -37,7 +37,7 @@
<span class="l-time-range-input" ng-controller="ToggleController as t2"> <span class="l-time-range-input" ng-controller="ToggleController as t2">
<mct-control key="'datetime-field'" <mct-control key="'datetime-field'"
structure="{ format: parameters.format }" structure="{ format: parameters.format, validate: validateEnd }"
ng-model="formModel" ng-model="formModel"
ng-blur="updateBoundsFromForm()" ng-blur="updateBoundsFromForm()"
field="'end'" field="'end'"

View File

@ -43,7 +43,7 @@ define(
function TimeRangeController($scope, formatService, defaultFormat, now) { function TimeRangeController($scope, formatService, defaultFormat, now) {
var tickCount = 2, var tickCount = 2,
innerMinimumSpan = 1000, // 1 second innerMinimumSpan = 1000, // 1 second
outerMinimumSpan = 1000 * 60 * 60, // 1 hour outerMinimumSpan = 1000, // 1 second
initialDragValue, initialDragValue,
formatter = formatService.getFormat(defaultFormat); formatter = formatService.getFormat(defaultFormat);
@ -185,13 +185,6 @@ define(
function updateOuterStart(t) { function updateOuterStart(t) {
var ngModel = $scope.ngModel; var ngModel = $scope.ngModel;
ngModel.outer.start = t;
ngModel.outer.end = Math.max(
ngModel.outer.start + outerMinimumSpan,
ngModel.outer.end
);
ngModel.inner.start = ngModel.inner.start =
Math.max(ngModel.outer.start, ngModel.inner.start); Math.max(ngModel.outer.start, ngModel.inner.start);
ngModel.inner.end = Math.max( ngModel.inner.end = Math.max(
@ -207,13 +200,6 @@ define(
function updateOuterEnd(t) { function updateOuterEnd(t) {
var ngModel = $scope.ngModel; var ngModel = $scope.ngModel;
ngModel.outer.end = t;
ngModel.outer.start = Math.min(
ngModel.outer.end - outerMinimumSpan,
ngModel.outer.start
);
ngModel.inner.end = ngModel.inner.end =
Math.min(ngModel.outer.end, ngModel.inner.end); Math.min(ngModel.outer.end, ngModel.inner.end);
ngModel.inner.start = Math.min( ngModel.inner.start = Math.min(
@ -233,11 +219,20 @@ define(
} }
function updateBoundsFromForm() { function updateBoundsFromForm() {
$scope.ngModel = $scope.ngModel || {}; var start = $scope.formModel.start,
$scope.ngModel.outer = { end = $scope.formModel.end;
start: $scope.formModel.start, if (end >= start + outerMinimumSpan) {
end: $scope.formModel.end $scope.ngModel = $scope.ngModel || {};
}; $scope.ngModel.outer = { start: start, end: end };
}
}
function validateStart(startValue) {
return startValue <= $scope.formModel.end - outerMinimumSpan;
}
function validateEnd(endValue) {
return endValue >= $scope.formModel.start + outerMinimumSpan;
} }
$scope.startLeftDrag = startLeftDrag; $scope.startLeftDrag = startLeftDrag;
@ -249,6 +244,9 @@ define(
$scope.updateBoundsFromForm = updateBoundsFromForm; $scope.updateBoundsFromForm = updateBoundsFromForm;
$scope.validateStart = validateStart;
$scope.validateEnd = validateEnd;
$scope.ticks = []; $scope.ticks = [];
// Initialize scope to defaults // Initialize scope to defaults

View File

@ -91,6 +91,24 @@ define(
.toHaveBeenCalledWith("ngModel", jasmine.any(Function)); .toHaveBeenCalledWith("ngModel", jasmine.any(Function));
}); });
it("exposes start time validator", function () {
var testValue = 42000000;
mockScope.formModel = { end: testValue };
expect(mockScope.validateStart(testValue + 1))
.toBe(false);
expect(mockScope.validateStart(testValue - 60 * 60 * 1000 - 1))
.toBe(true);
});
it("exposes end time validator", function () {
var testValue = 42000000;
mockScope.formModel = { start: testValue };
expect(mockScope.validateEnd(testValue - 1))
.toBe(false);
expect(mockScope.validateEnd(testValue + 60 * 60 * 1000 + 1))
.toBe(true);
});
describe("when changes are made via form entry", function () { describe("when changes are made via form entry", function () {
beforeEach(function () { beforeEach(function () {
mockScope.ngModel = { mockScope.ngModel = {
@ -194,26 +212,6 @@ define(
fireWatchCollection("ngModel", mockScope.ngModel); fireWatchCollection("ngModel", mockScope.ngModel);
}); });
it("enforces a minimum outer span", function () {
mockScope.ngModel.outer.end =
mockScope.ngModel.outer.start - DAY * 100;
fireWatch(
"ngModel.outer.end",
mockScope.ngModel.outer.end
);
expect(mockScope.ngModel.outer.end)
.toBeGreaterThan(mockScope.ngModel.outer.start);
mockScope.ngModel.outer.start =
mockScope.ngModel.outer.end + DAY * 100;
fireWatch(
"ngModel.outer.start",
mockScope.ngModel.outer.start
);
expect(mockScope.ngModel.outer.end)
.toBeGreaterThan(mockScope.ngModel.outer.start);
});
it("enforces a minimum inner span when outer span changes", function () { it("enforces a minimum inner span when outer span changes", function () {
mockScope.ngModel.outer.end = mockScope.ngModel.outer.end =
mockScope.ngModel.outer.start - DAY * 100; mockScope.ngModel.outer.start - DAY * 100;