Skip to content

Commit

Permalink
fix(calendar): fire onchange only when date selection is complete
Browse files Browse the repository at this point in the history
This PR allow to fire onChange only when a date selection if complete. For a datetime for example, onChange is called when a date is selected, then when a hour is selected, and again when minutes are selected... which is IMHO is a complete nonsense. If I apply a method when a date is changed, i'd prefer trigger this change only when i have finished to select my date...
  • Loading branch information
prudho committed May 14, 2020
1 parent 1986ea2 commit 6ba815e
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions src/definitions/modules/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,33 +202,38 @@ $.fn.calendar = function(parameters) {
calendar: function () {
var i, r, c, p, row, cell, pageGrid;

var mode = module.get.mode();
var today = new Date();
var date = module.get.date();
var focusDate = module.get.focusDate();
var display = focusDate || date || settings.initialDate || today;
display = module.helper.dateInRange(display);
var
mode = module.get.mode(),
today = new Date(),
date = module.get.date(),
focusDate = module.get.focusDate(),
display = module.helper.dateInRange(focusDate || date || settings.initialDate || today)
;

if (!focusDate) {
focusDate = display;
module.set.focusDate(focusDate, false, false);
}

var isYear = mode === 'year';
var isMonth = mode === 'month';
var isDay = mode === 'day';
var isHour = mode === 'hour';
var isMinute = mode === 'minute';
var isTimeOnly = settings.type === 'time';
var
isYear = mode === 'year',
isMonth = mode === 'month',
isDay = mode === 'day',
isHour = mode === 'hour',
isMinute = mode === 'minute',
isTimeOnly = settings.type === 'time'
;

var multiMonth = Math.max(settings.multiMonth, 1);
var monthOffset = !isDay ? 0 : module.get.monthOffset();

var minute = display.getMinutes();
var hour = display.getHours();
var day = display.getDate();
var startMonth = display.getMonth() + monthOffset;
var year = display.getFullYear();
var
minute = display.getMinutes(),
hour = display.getHours(),
day = display.getDate(),
startMonth = display.getMonth() + monthOffset,
year = display.getFullYear()
;

var columns = isDay ? settings.showWeekNumbers ? 8 : 7 : isHour ? 4 : timeGap['column'];
var rows = isDay || isHour ? 6 : timeGap['row'];
Expand All @@ -254,17 +259,18 @@ $.fn.calendar = function(parameters) {
rows = Math.ceil(requiredCells / 7);
}

var yearChange = isYear ? 10 : isMonth ? 1 : 0;
var monthChange = isDay ? 1 : 0;
var dayChange = isHour || isMinute ? 1 : 0;
var prevNextDay = isHour || isMinute ? day : 1;
var prevDate = new Date(year - yearChange, month - monthChange, prevNextDay - dayChange, hour);
var nextDate = new Date(year + yearChange, month + monthChange, prevNextDay + dayChange, hour);

var prevLast = isYear ? new Date(Math.ceil(year / 10) * 10 - 9, 0, 0) :
isMonth ? new Date(year, 0, 0) : isDay ? new Date(year, month, 0) : new Date(year, month, day, -1);
var nextFirst = isYear ? new Date(Math.ceil(year / 10) * 10 + 1, 0, 1) :
isMonth ? new Date(year + 1, 0, 1) : isDay ? new Date(year, month + 1, 1) : new Date(year, month, day + 1);
var
yearChange = isYear ? 10 : isMonth ? 1 : 0,
monthChange = isDay ? 1 : 0,
dayChange = isHour || isMinute ? 1 : 0,
prevNextDay = isHour || isMinute ? day : 1,
prevDate = new Date(year - yearChange, month - monthChange, prevNextDay - dayChange, hour),
nextDate = new Date(year + yearChange, month + monthChange, prevNextDay + dayChange, hour),
prevLast = isYear ? new Date(Math.ceil(year / 10) * 10 - 9, 0, 0) :
isMonth ? new Date(year, 0, 0) : isDay ? new Date(year, month, 0) : new Date(year, month, day, -1),
nextFirst = isYear ? new Date(Math.ceil(year / 10) * 10 + 1, 0, 1) :
isMonth ? new Date(year + 1, 0, 1) : isDay ? new Date(year, month + 1, 1) : new Date(year, month, day + 1)
;

var tempMode = mode;
if (isDay && settings.showWeekNumbers){
Expand Down Expand Up @@ -809,7 +815,7 @@ $.fn.calendar = function(parameters) {
module.set.mode(newMode);
if (mode === 'hour' || (mode === 'day' && module.get.date())) {
//the user has chosen enough to consider a valid date/time has been chosen
module.set.date(date);
module.set.date(date, true, false);
} else {
module.set.focusDate(date);
}
Expand Down

0 comments on commit 6ba815e

Please sign in to comment.