diff --git a/platform/commonUI/general/src/controllers/TimeRangeController.js b/platform/commonUI/general/src/controllers/TimeRangeController.js index 3ee05e1faf..eca72ce7ab 100644 --- a/platform/commonUI/general/src/controllers/TimeRangeController.js +++ b/platform/commonUI/general/src/controllers/TimeRangeController.js @@ -66,6 +66,8 @@ define([ this.outerMinimumSpan = 1000; // 1 second this.initialDragValue = undefined; this.formatter = formatService.getFormat(defaultFormat); + this.formStartChanged = false; + this.formEndChanged = false; this.$scope.ticks = []; @@ -79,7 +81,9 @@ define([ 'updateOuterEnd', 'updateFormat', 'validateStart', - 'validateEnd' + 'validateEnd', + 'onFormStartChange', + 'onFormEndChange' ].forEach(function (boundFn) { this[boundFn] = this[boundFn].bind(this); }, this); @@ -89,6 +93,8 @@ define([ this.$scope.$watch("ngModel.outer.start", this.updateOuterStart); this.$scope.$watch("ngModel.outer.end", this.updateOuterEnd); this.$scope.$watch("parameters.format", this.updateFormat); + this.$scope.$watch("formModel.start", this.onFormStartChange); + this.$scope.$watch("formModel.end", this.onFormEndChange); } TimeRangeController.prototype.formatTimestamp = function (ts) { @@ -255,11 +261,35 @@ define([ }; TimeRangeController.prototype.updateBoundsFromForm = function () { - var start = this.$scope.formModel.start, - end = this.$scope.formModel.end; - if (end >= start + this.outerMinimumSpan) { - this.$scope.ngModel = this.$scope.ngModel || {}; - this.$scope.ngModel.outer = { start: start, end: end }; + if (this.formStartChanged) { + this.$scope.ngModel.outer.start = + this.$scope.ngModel.inner.start = + this.$scope.formModel.start; + this.formStartChanged = false; + } + if (this.formEndChanged) { + this.$scope.ngModel.outer.end = + this.$scope.ngModel.inner.end = + this.$scope.formModel.end; + this.formEndChanged = false; + } + }; + + TimeRangeController.prototype.onFormStartChange = function ( + newValue, + oldValue + ) { + if (!this.formStartChanged && newValue !== oldValue) { + this.formStartChanged = true; + } + }; + + TimeRangeController.prototype.onFormEndChange = function ( + newValue, + oldValue + ) { + if (!this.formEndChanged && newValue !== oldValue) { + this.formEndChanged = true; } }; diff --git a/platform/commonUI/general/test/controllers/TimeRangeControllerSpec.js b/platform/commonUI/general/test/controllers/TimeRangeControllerSpec.js index 086947950a..20ad0413b6 100644 --- a/platform/commonUI/general/test/controllers/TimeRangeControllerSpec.js +++ b/platform/commonUI/general/test/controllers/TimeRangeControllerSpec.js @@ -119,25 +119,87 @@ define( start: DAY * 10000, end: DAY * 11000 }; - // These watches may not exist, but Angular would fire - // them if they did. + }); + + it('updates all changed bounds when requested', function () { fireWatchCollection("formModel", mockScope.formModel); fireWatch("formModel.start", mockScope.formModel.start); fireWatch("formModel.end", mockScope.formModel.end); - }); - it("does not immediately make changes to the model", function () { expect(mockScope.ngModel.outer.start) .not.toEqual(mockScope.formModel.start); + expect(mockScope.ngModel.inner.start) + .not.toEqual(mockScope.formModel.start); + expect(mockScope.ngModel.outer.end) .not.toEqual(mockScope.formModel.end); + expect(mockScope.ngModel.inner.end) + .not.toEqual(mockScope.formModel.end); + + controller.updateBoundsFromForm(); + + expect(mockScope.ngModel.outer.start) + .toEqual(mockScope.formModel.start); + expect(mockScope.ngModel.inner.start) + .toEqual(mockScope.formModel.start); + + expect(mockScope.ngModel.outer.end) + .toEqual(mockScope.formModel.end); + expect(mockScope.ngModel.inner.end) + .toEqual(mockScope.formModel.end); + }); + + it('updates changed start bound when requested', function () { + fireWatchCollection("formModel", mockScope.formModel); + fireWatch("formModel.start", mockScope.formModel.start); + + expect(mockScope.ngModel.outer.start) + .not.toEqual(mockScope.formModel.start); + expect(mockScope.ngModel.inner.start) + .not.toEqual(mockScope.formModel.start); + + expect(mockScope.ngModel.outer.end) + .not.toEqual(mockScope.formModel.end); + expect(mockScope.ngModel.inner.end) + .not.toEqual(mockScope.formModel.end); + + controller.updateBoundsFromForm(); + + expect(mockScope.ngModel.outer.start) + .toEqual(mockScope.formModel.start); + expect(mockScope.ngModel.inner.start) + .toEqual(mockScope.formModel.start); + + expect(mockScope.ngModel.outer.end) + .not.toEqual(mockScope.formModel.end); + expect(mockScope.ngModel.inner.end) + .not.toEqual(mockScope.formModel.end); }); - it("updates model bounds on request", function () { - controller.updateBoundsFromForm(); + it('updates changed end bound when requested', function () { + fireWatchCollection("formModel", mockScope.formModel); + fireWatch("formModel.end", mockScope.formModel.end); + expect(mockScope.ngModel.outer.start) - .toEqual(mockScope.formModel.start); + .not.toEqual(mockScope.formModel.start); + expect(mockScope.ngModel.inner.start) + .not.toEqual(mockScope.formModel.start); + expect(mockScope.ngModel.outer.end) + .not.toEqual(mockScope.formModel.end); + expect(mockScope.ngModel.inner.end) + .not.toEqual(mockScope.formModel.end); + + controller.updateBoundsFromForm(); + + expect(mockScope.ngModel.outer.start) + .not.toEqual(mockScope.formModel.start); + expect(mockScope.ngModel.inner.start) + .not.toEqual(mockScope.formModel.start); + + expect(mockScope.ngModel.outer.end) + .toEqual(mockScope.formModel.end); + expect(mockScope.ngModel.inner.end) .toEqual(mockScope.formModel.end); }); });