Skip to content

Commit

Permalink
Do not restrict the date picker to the previous week
Browse files Browse the repository at this point in the history
  • Loading branch information
gberaudo committed May 23, 2018
1 parent 527372e commit 69352e9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 31 deletions.
25 changes: 11 additions & 14 deletions src/filter/ruleComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
};
Expand All @@ -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]
};
Expand Down Expand Up @@ -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) {
Expand Down
35 changes: 25 additions & 10 deletions src/misc/Time.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
};
};
Expand Down
14 changes: 7 additions & 7 deletions src/misc/datepickerComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
});
}
Expand All @@ -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);
}
};

Expand Down

0 comments on commit 69352e9

Please sign in to comment.