diff --git a/src/filter/ruleComponent.js b/src/filter/ruleComponent.js index b320932897ae..338421164db8 100644 --- a/src/filter/ruleComponent.js +++ b/src/filter/ruleComponent.js @@ -228,10 +228,10 @@ exports.RuleController_ = class { */ this.timeRangeMode = { widget: 'datepicker', - maxValue: this.createDate_(), - minValue: this.createWeekAgoDate_(), - maxDefValue: null, - minDefValue: null, + maxValue: null, + minValue: null, + maxDefValue: this.createDate_(), + minDefValue: this.createWeekAgoDate_(), mode: 'range', interval: [0, 1, 0, 0] }; @@ -244,10 +244,10 @@ exports.RuleController_ = class { */ this.timeValueMode = { widget: 'datepicker', - maxValue: this.createDate_(), - minValue: this.createDate_(), - maxDefValue: null, - minDefValue: null, + maxValue: null, + minValue: null, + maxDefValue: this.createDate_(), + minDefValue: this.createDate_(), mode: 'value', interval: [0, 1, 0, 0] }; @@ -424,24 +424,21 @@ exports.RuleController_ = class { this.unlisteners_.push(this.scope_.$watch( () => this.clone.getExpression(), (newVal) => { - const value = newVal === null ? this.createDate_() : newVal; - this.timeValueMode.minValue = value; + this.timeValueMode.minValue = newVal; } )); // Watch 'lowerBoundary' this.unlisteners_.push(this.scope_.$watch( () => this.clone.lowerBoundary, (newVal) => { - const value = newVal === null ? this.createWeekAgoDate_() : newVal; - this.timeRangeMode.minValue = value; + this.timeRangeMode.minValue = newVal; } )); // Watch 'upperBoundary' this.unlisteners_.push(this.scope_.$watch( () => this.clone.upperBoundary, (newVal) => { - const value = newVal === null ? this.createDate_() : newVal; - this.timeRangeMode.maxValue = value; + this.timeRangeMode.maxValue = newVal; } )); } else if (this.clone.type === ngeoFormatAttributeType.GEOMETRY) { diff --git a/src/misc/Time.js b/src/misc/Time.js index 42338a380313..b8aa0c20f1b6 100644 --- a/src/misc/Time.js +++ b/src/misc/Time.js @@ -11,6 +11,23 @@ */ const exports = function() {}; +/** + * @param {number|string|null} value The value + * @param {Date} defaultValue The default value + * @return {Date} the date + */ +exports.prototype.createDate = function(value, defaultValue = null) { + return value !== null ? new Date(value) : defaultValue; +}; + +/** + * @param {Date} date The date + * @param {number|null=} defaultValue The default value + * @return {number|null} the time + */ +exports.prototype.getTime = function(date, defaultValue = null) { + return date ? date.getTime() : defaultValue; +}; /** * Get options regarding the time property of a node; @@ -25,21 +42,19 @@ const exports = function() {}; */ exports.prototype.getOptions = function(time) { - const minDate = new Date(time.minValue); - const maxDate = new Date(time.maxValue); + const minDate = this.createDate(time.minValue); + const maxDate = this.createDate(time.maxValue); - const minDefaultDate = (time.minDefValue) ? - new Date(time.minDefValue) : minDate; - const maxDefaultDate = (time.maxDefValue) ? - new Date(time.maxDefValue) : maxDate; + const minDefaultDate = this.createDate(time.minDefValue, minDate); + const maxDefaultDate = this.createDate(time.maxDefValue, maxDate); const defaultValues = (time.mode === 'range') ? - [minDefaultDate.getTime(), maxDefaultDate.getTime()] : - minDefaultDate.getTime(); + [this.getTime(minDefaultDate), this.getTime(maxDefaultDate)] : + this.getTime(minDefaultDate); return { - minDate: minDate.getTime(), - maxDate: maxDate.getTime(), + minDate: this.getTime(minDate), + maxDate: this.getTime(maxDate), values: defaultValues }; }; diff --git a/src/misc/datepickerComponent.js b/src/misc/datepickerComponent.js index 933558f11bed..1fad68a0d1f2 100644 --- a/src/misc/datepickerComponent.js +++ b/src/misc/datepickerComponent.js @@ -212,8 +212,8 @@ exports.Controller_ = function($scope, $injector, if (angular.isDate(sDate) && (!this.isModeRange || angular.isDate(eDate))) { this.onDateSelected({ time: { - start: sDate.getTime(), - end: eDate ? eDate.getTime() : null + start: this.ngeoTime_.getTime(sDate), + end: this.ngeoTime_.getTime(eDate) } }); } @@ -226,17 +226,17 @@ exports.Controller_ = function($scope, $injector, exports.Controller_.prototype.init = function() { //fetch the initial options for the component const initialOptions_ = this.ngeoTime_.getOptions(this.time); - this.initialMinDate = new Date(initialOptions_.minDate); - this.initialMaxDate = new Date(initialOptions_.maxDate); + this.initialMinDate = this.ngeoTime_.createDate(initialOptions_.minDate); + this.initialMaxDate = this.ngeoTime_.createDate(initialOptions_.maxDate); this.isModeRange = this.time.mode === 'range'; if (this.isModeRange) { googAsserts.assertArray(initialOptions_.values); - this.sdate = new Date(initialOptions_.values[0]); - this.edate = new Date(initialOptions_.values[1]); + this.sdate = this.ngeoTime_.createDate(initialOptions_.values[0]); + this.edate = this.ngeoTime_.createDate(initialOptions_.values[1]); } else { googAsserts.assertNumber(initialOptions_.values); - this.sdate = new Date(initialOptions_.values); + this.sdate = this.ngeoTime_.createDate(initialOptions_.values); } };