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

added disable masks: supports business days and even/odd weeks; completed nl_NL translations #884

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
71 changes: 65 additions & 6 deletions lib/picker.date.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,34 @@ DatePicker.prototype.create = function( type, value, options ) {
} //DatePicker.prototype.create


// bluppfisk: This function calculates a number of working days (days) from a given date (dateUnit)

function addWorkingDays(dateUnit, days) {
days = parseInt(days);
var wks = Math.floor(days/5);
// var dys = days.mod(5);
var dys = ((days%5)+5)%5;
var dy = dateUnit.getDay();
var newDate = new Date();

if (dy === 6 && dys > -1) {
if (dys === 0) {dys-=2; dy+=2;}
dys++; dy -= 6;}
if (dy === 0 && dys < 1) {
if (dys === 0) {dys+=2; dy-=2;}
dys--; dy += 6;}
if (dy + dys > 5) dys += 2;
if (dy + dys < 1) dys -= 2;
newDate.setDate(dateUnit.getDate()+wks*7+dys);
return newDate;
}

/**
* Create a range limit object using an array, date object,
* literal “true”, or integer relative to another time.
* bluppfisk: added the boolean toWorkingDays parameter
*/
DatePicker.prototype.createRange = function( from, to ) {
DatePicker.prototype.createRange = function( from, to, toWorkingDays ) {

var calendar = this,
createDate = function( date ) {
Expand All @@ -277,7 +300,11 @@ DatePicker.prototype.createRange = function( from, to ) {
from = [ to.year, to.month, to.date + from ];
}
else if ( _.isInteger( to ) && $.isPlainObject( from ) ) {
to = [ from.year, from.month, from.date + to ];
if (toWorkingDays) { // bluppfisk: add working days instead of normal days (i.e. exclude Saturday and Sunday)
to = addWorkingDays(createDate( [from.year, from.month, from.date ] ).obj, to);
} else {
to = [ from.year, from.month, from.date + to ];
}
}

return {
Expand All @@ -286,15 +313,26 @@ DatePicker.prototype.createRange = function( from, to ) {
}
} //DatePicker.prototype.createRange


/**
* Check if a date unit falls within a date range object.
*/
DatePicker.prototype.withinRange = function( range, dateUnit ) {
range = this.createRange(range.from, range.to)
workingDays = range.toWorkingDays || null; // bluppfisk: count working days instead of normal days if this is set
range = this.createRange(range.from, range.to, workingDays);
return dateUnit.pick >= range.from.pick && dateUnit.pick <= range.to.pick
}

/**
* Bluppfisk: Check if a date falls within an even or odd week's disabled range.
*/
DatePicker.prototype.inEvenOddWeeks = function ( evenOddWeek, dateUnit, firstDay ) {
oddWeeks = evenOddWeek.oddWeeks;
evenWeeks = evenOddWeek.evenWeeks;
day = new Date(dateUnit.pick);
weekday = (day.getDay() + (firstDay ? 0 : 1)) || 7; //getDay is safer than mod 7, apparently
return ( jQuery.inArray( weekday, ((getWeekNumber(day, firstDay) % 2 === 0) ? evenWeeks : oddWeeks )) != -1);
}


/**
* Check if two date range objects overlap.
Expand Down Expand Up @@ -546,7 +584,20 @@ DatePicker.prototype.validate = function( type, dateObject, options ) {
return dateObject
} //DatePicker.prototype.validate


// bluppfisk: get week number function
function getWeekNumber(d, firstDay) {
// Copy date so don't modify original
d = new Date(+d);
d.setHours(0,0,0);
// Set to nearest Thursday: current date + 4 - current day number (depending on the firstDay setting)
d.setDate(d.getDate() + (firstDay ? 4 : 3) - (firstDay ? (d.getDay()||7) : (d.getDay())));
// Get first day of year
var yearStart = new Date(d.getFullYear(),0,1);
// Calculate full weeks to nearest Thursday
var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7);
// Return week number
return weekNo;
}
/**
* Check if a date is disabled.
*/
Expand All @@ -570,7 +621,15 @@ DatePicker.prototype.disabled = function( dateToVerify ) {

// If it’s an object, match a date within the “from” and “to” range.
if ( $.isPlainObject( dateToDisable ) ) {
return calendar.withinRange( dateToDisable, dateToVerify )

// bluppfisk: verify whether it's a to-from range or an odd/even week weekday group
if ( 'to' in dateToDisable || 'from' in dateToDisable ) {
return calendar.withinRange( dateToDisable, dateToVerify )
}
if ( 'oddWeeks' in dateToDisable || 'evenWeeks' in dateToDisable ) {
return calendar.inEvenOddWeeks( dateToDisable, dateToVerify, calendar.settings.firstDay );
}

}
})

Expand Down
6 changes: 5 additions & 1 deletion lib/translations/nl_NL.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ jQuery.extend( jQuery.fn.pickadate.defaults, {
monthsShort: [ 'jan', 'feb', 'maa', 'apr', 'mei', 'jun', 'jul', 'aug', 'sep', 'okt', 'nov', 'dec' ],
weekdaysFull: [ 'zondag', 'maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag' ],
weekdaysShort: [ 'zo', 'ma', 'di', 'wo', 'do', 'vr', 'za' ],
labelMonthNext: 'Volgende maand',
labelMonthPrev: 'Vorige maand',
labelMonthSelect: 'Kies de maand',
labelYearSelect: 'Kies het jaar',
today: 'vandaag',
clear: 'verwijderen',
close: 'sluiten',
Expand All @@ -15,4 +19,4 @@ jQuery.extend( jQuery.fn.pickadate.defaults, {

jQuery.extend( jQuery.fn.pickatime.defaults, {
clear: 'verwijderen'
});
});