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 073e068 commit 68a120f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 52 deletions.
42 changes: 7 additions & 35 deletions src/filter/ruleComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ exports.RuleController_ = class {
*/
this.timeRangeMode = {
widget: 'datepicker',
maxValue: this.createDate_(),
minValue: this.createWeekAgoDate_(),
maxValue: null,
minValue: null,
maxDefValue: null,
minDefValue: null,
mode: 'range',
Expand All @@ -244,8 +244,8 @@ exports.RuleController_ = class {
*/
this.timeValueMode = {
widget: 'datepicker',
maxValue: this.createDate_(),
minValue: this.createDate_(),
maxValue: null,
minValue: null,
maxDefValue: null,
minDefValue: null,
mode: 'value',
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 Expand Up @@ -605,31 +602,6 @@ exports.RuleController_ = class {
this.clone.upperBoundary = date['end'];
}

/**
* @param {number=} opt_timeDelta Time delta to go back in the past.
* @return {string} ISO string of the date
* @private
*/
createDate_(opt_timeDelta) {

const date = new Date();

if (opt_timeDelta !== undefined) {
const time = date.getTime() - opt_timeDelta;
date.setTime(time);
}

return date.toISOString();
}

/**
* @return {string} ISO string of the date
* @private
*/
createWeekAgoDate_() {
return this.createDate_(1000 * 60 * 60 * 24 * 7); // A week ago date
}

/**
* @param {number} time Time.
* @return {string} Date
Expand Down
30 changes: 20 additions & 10 deletions src/misc/Time.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
*/
const exports = function() {};

exports.prototype.createDate = function(value, defaultValue = null) {
return value !== null ? new Date(value) : defaultValue;
}

/**
* @param {Date} date
* @param {number|null=} defaultValue
* @return {number|null}
*/
exports.prototype.getTime = function(date, defaultValue = null) {
return date !== null ? date.getTime() : defaultValue;
}

/**
* Get options regarding the time property of a node;
Expand All @@ -25,21 +37,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 68a120f

Please sign in to comment.