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

Add blockWeekDays option to date field. #1079

Merged
merged 1 commit into from
Oct 28, 2015
Merged
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
Add blockWeekDays option to date field.
To be able to block selection of some days of week (for example, weekends)
I added blockWeekDays options to date field. It's array of week day
numbers. (1 - monday, 2 - tuesday, 3 - wensday, 4 - thursday, 5 - friday,
6 - saturday, 0 - sunday)
  • Loading branch information
MikeZhur committed Oct 23, 2015
commit 456499fedea983d6edab2d8f7710909908d42882
30 changes: 23 additions & 7 deletions src/w2fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,14 @@

case 'date':
Copy link
Owner

Choose a reason for hiding this comment

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

this should also be implemented for "datetime" type

defaults = {
format : w2utils.settings.dateFormat, // date format
keyboard : true,
silent : true,
start : '', // string or jquery object
end : '', // string or jquery object
blocked : {}, // { '4/11/2011': 'yes' }
colored : {} // { '4/11/2011': 'red:white' }
format : w2utils.settings.dateFormat, // date format
keyboard : true,
silent : true,
start : '', // string or jquery object
end : '', // string or jquery object
blocked : {}, // { '4/11/2011': 'yes' }
colored : {}, // { '4/11/2011': 'red:white' }
blockWeekDays : null // array of numbers of weekday to block
Copy link
Owner

Choose a reason for hiding this comment

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

I think it should be empty array by default

Choose a reason for hiding this comment

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

Ok. I don't mind. Let in be an empty array. I thought it must work if this option was not specified (for backward compatibility), so I added this checks. But default empty array seems to be ok.

};
this.options = $.extend(true, {}, defaults, options);
options = this.options; // since object is re-created, need to re-assign
Expand Down Expand Up @@ -1863,6 +1864,21 @@
}
// block predefined dates
if (this.options.blocked && $.inArray(str, this.options.blocked) != -1) inRange = false;

/*
clockWeekDay - type: array or integers. every element - number of week day.
number of weekday (1 - monday, 2 - tuesday, 3 - wensday, 4 - thursday, 5 - friday, 6 - saturday, 0 - sunday)
for block in calendar (for example, block all sundays so user can't choose sunday in calendar)
*/
if (this.options.blockWeekDays != null && this.options.blockWeekDays != undefined
Copy link
Owner

Choose a reason for hiding this comment

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

if (this.options.blockWeekDays != null) 

is the same as

if (this.options.blockWeekDays != null && this.options.blockWeekDays != undefined)

&& this.options.blockWeekDays.length != undefined){
Copy link
Owner

Choose a reason for hiding this comment

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

I think instead of

this.options.blockWeekDays.length != undefined

we should use

Array.isArray(this.options.blockWeekDays)

var l = this.options.blockWeekDays.length;
for (var i=0; i<l; i++){
if (dt.getDay() == this.options.blockWeekDays[i]){
inRange = false;
}
}
}
}
}
else if (this.type == 'time') {
Expand Down
7 changes: 7 additions & 0 deletions test/fields.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
items.push(item);
}
$('.date').w2field('date', { silent: false, format: 'dd/mm/yyyy' });
$('.date-with-blocked-weekends').w2field('date', { silent: false, format: 'yyyy-m-d', blockWeekDays: [0,6] })
$('.datetime').w2field('datetime', {});
$('.time').w2field('time', { start: '8:15am', end: '4:30pm' });
$('.color').w2field('color');
Expand Down Expand Up @@ -243,6 +244,12 @@
<input id="date1" class="w2ui-input"/> - <input id="date2" class="w2ui-input"/>
</div>
</div>
<div class="w2ui-field">
<label>date with blocked weekends:</label>
<div>
<input class="date-with-blocked-weekends" type="text" size="20"/>
</div>
</div>
<div class="w2ui-field">
<label>time:</label>
<div>
Expand Down