Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When a date is selected in absolute mode, set to start/end of day #10433

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/ui/public/directives/__tests__/timepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,15 @@ describe('timepicker directive', function () {
done();
});

});
it('should set from/to to start/end of day if set from timepicker', function (done) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should "timepicker" be "datepicker" in this test description and the one below?

$scope.absolute.from = new Date('2012-02-01 12:00');
$scope.absolute.to = new Date('2012-02-11 12:00');

$scope.$digest();

expect($scope.absolute.from.valueOf()).to.be(moment('2012-02-01 00:00:00.000').valueOf());
expect($scope.absolute.to.valueOf()).to.be(moment('2012-02-11 23:59:59.999').valueOf());
done();
});
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test ensuring that manually setting hour/minute/second still works?

});
12 changes: 10 additions & 2 deletions src/ui/public/timepicker/timepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,19 @@ module.directive('kbnTimepicker', function (quickRanges, timeUnits, refreshInter
});

$scope.$watch('absolute.from', function (date) {
if (_.isDate(date)) $scope.absolute.from = moment(date);
// If it is a date, it was set from the timepicker, so we set it to the start of day and transform it into a moment
if (_.isDate(date)) {
date.setHours(0, 0, 0, 0); // Start of day
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having to guess where the update came from, what if we used a getterSetter for the ng-model on the datepicker using ngModelOptions? Then this setHours logic could live inside the setter.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had this thought too, but I don't think the version of the date picker we're using supports ngModelOptions. I guess I should look into upgrading the library.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it need to do anything special to support it? I thought the getterSetter would be declared in timepicker.js and it would work without any modification to datepicker, since it would look like a regular model from datepicker's point of view.

$scope.absolute.from = moment(date);
}
});

$scope.$watch('absolute.to', function (date) {
if (_.isDate(date)) $scope.absolute.to = moment(date);
// If it is a date, it was set from the timepicker, so we set it to the end of day and transform it into a moment
if (_.isDate(date)) {
date.setHours(23, 59, 59, 999); // End of day
$scope.absolute.to = moment(date);
}
});

$scope.setMode = function (thisMode) {
Expand Down