Add of manual editing of date in input.

pull/2662/head
mhirtie 10 years ago
parent 9f83ee5cd7
commit 704bbc6cb8
  1. 78
      frontend/app/ui_components/date/date-range-directive.js

@ -27,6 +27,11 @@
//++ //++
module.exports = function(TimezoneService, $timeout) { module.exports = function(TimezoneService, $timeout) {
var datePattern = /^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/\d{4}$/i,
parseDate = TimezoneService.parseDate,
formattedDate = TimezoneService.formattedDate,
formattedISODate = TimezoneService.formattedISODate;
return { return {
restrict: 'EA', restrict: 'EA',
replace: true, replace: true,
@ -34,36 +39,54 @@ module.exports = function(TimezoneService, $timeout) {
startDate: '=', startDate: '=',
endDate: '=' endDate: '='
}, },
template: '<div class="daterange"><input /><div></div></div>', template: '<div class="daterange"><input ng-model="daterange" ng-change="change(this)" /><div></div></div>',
link: function(scope, element) { link: function(scope, element) {
var previous = -1, var previous,
current = -1, current,
div = element.find('div'), div = element.find('div'),
input = element.find('input'); input = element.find('input'),
setDate = function(date) {
div.datepicker('setDate', parseDate(date).toDate());
div.find('.ui-datepicker-current-day').click();
};
scope.change = function(scope) {
var range = scope.daterange.split(/\s+?-\s+?/i),
isMatching = range.every(function(date) {
return datePattern.test(date)
});
if(isMatching) {
range.forEach(function(date) {
setDate(date);
});
}
};
div.datepicker({ div.datepicker({
onSelect: function(dateText, inst) { onSelect: function(dateText, inst) {
previous = +current; previous = current;
current = inst.selectedDay; current = parseDate(new Date(inst.selectedYear, inst.selectedMonth, inst.selectedDay));
if(previous == -1 || previous == current) { if(previous == -1 || previous == current) {
previous = current; previous = current;
input.val(dateText); input.val(formattedDate(current));
} else { } else {
var start = new Date(inst.selectedYear, inst.selectedMonth, Math.min(previous,current)), var start = minDate(current, previous),
end = new Date(inst.selectedYear, inst.selectedMonth, Math.max(previous,current)); end = maxDate(current, previous);
$timeout(function(){ $timeout(function(){
scope.startDate = TimezoneService.formattedISODate(start); scope.startDate = formattedISODate(start);
scope.endDate = TimezoneService.formattedISODate(end); scope.endDate = formattedISODate(end);
input.val(TimezoneService.formattedDate(start) + ' - ' + input.val(formattedDate(start) + ' - ' +
TimezoneService.formattedDate(end)); formattedDate(end));
}); });
} }
}, },
beforeShowDay: function(selectedDay) { beforeShowDay: function(selectedDay) {
var isSelected = selectedDay.getDate() >= Math.min(previous, current) && var isSelected = parseDate(selectedDay) >= minDate(current, previous) &&
selectedDay.getDate() <= Math.max(previous, current); parseDate(selectedDay) <= maxDate(current, previous);
return [true, (isSelected ? 'date-range-selected' : '')]; return [true, isSelected ? 'date-range-selected' : ''];
} }
}) })
.position({ .position({
@ -72,11 +95,24 @@ module.exports = function(TimezoneService, $timeout) {
of: '.daterange input' of: '.daterange input'
}); });
div.datepicker('setDate', TimezoneService.parseDate(scope.startDate).toDate()); setDate(scope.startDate);
div.find('.ui-datepicker-current-day').click(); setDate(scope.endDate);
div.datepicker('setDate', TimezoneService.parseDate(scope.endDate).toDate());
div.find('.ui-datepicker-current-day').click();
} }
}; };
function minDate(firstDate, secondDate) {
if(secondDate && firstDate && firstDate.isAfter(secondDate)) {
return secondDate;
}
return firstDate;
}
function maxDate(firstDate, secondDate) {
if(secondDate && firstDate && !firstDate.isAfter(secondDate)) {
return secondDate;
}
return firstDate;
}
}; };

Loading…
Cancel
Save