Skip to content

Commit

Permalink
feat(calendar): add ability to disable specific date(s)
Browse files Browse the repository at this point in the history
## Closes
#200
  • Loading branch information
TATSUNO Yasuhiro authored and Sean committed Nov 5, 2018
1 parent dd04a90 commit 3f7bc5d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
44 changes: 40 additions & 4 deletions src/definitions/modules/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ $.fn.calendar = function(parameters) {
var pages = isDay ? multiMonth : 1;

var container = $container;
var tooltipPosition = container.hasClass("left") ? "right center" : "left center";
container.empty();
if (pages > 1) {
pageGrid = $('<div/>').addClass(className.grid).appendTo(container);
Expand Down Expand Up @@ -315,7 +316,14 @@ $.fn.calendar = function(parameters) {
cell.text(cellText);
cell.data(metadata.date, cellDate);
var adjacent = isDay && cellDate.getMonth() !== ((month + 12) % 12);
var disabled = adjacent || !module.helper.isDateInRange(cellDate, mode) || settings.isDisabled(cellDate, mode) || (mode === 'day' && settings.disabledDaysOfWeek.includes(cellDate.getDay()));
var disabled = adjacent || !module.helper.isDateInRange(cellDate, mode) || settings.isDisabled(cellDate, mode) || module.helper.isDisabled(cellDate, mode);
if (disabled) {
var disabledReason = module.helper.disabledReason(cellDate, mode);
if (disabledReason !== null) {
cell.attr("data-tooltip", disabledReason[metadata.message]);
cell.attr("data-position", tooltipPosition);
}
}
var active = module.helper.dateEqual(cellDate, date, mode);
var isToday = module.helper.dateEqual(cellDate, today, mode);
cell.toggleClass(className.adjacentCell, adjacent);
Expand Down Expand Up @@ -378,7 +386,7 @@ $.fn.calendar = function(parameters) {
var inRange = !rangeDate ? false :
((!!startDate && module.helper.isDateInRange(cellDate, mode, startDate, rangeDate)) ||
(!!endDate && module.helper.isDateInRange(cellDate, mode, rangeDate, endDate)));
cell.toggleClass(className.focusCell, focused && (!isTouch || isTouchDown) && !adjacent);
cell.toggleClass(className.focusCell, focused && (!isTouch || isTouchDown) && !adjacent && !disabled);
cell.toggleClass(className.rangeCell, inRange && !active && !disabled);
});
}
Expand Down Expand Up @@ -446,6 +454,9 @@ $.fn.calendar = function(parameters) {
event.stopPropagation();
isTouchDown = false;
var target = $(event.target);
if (target.hasClass("disabled")) {
return;
}
var parent = target.parent();
if (parent.data(metadata.date) || parent.data(metadata.focusDate) || parent.data(metadata.mode)) {
//clicked on a child element, switch to parent (used when clicking directly on prev/next <i> icon element)
Expand Down Expand Up @@ -495,7 +506,7 @@ $.fn.calendar = function(parameters) {
//enter
var mode = module.get.mode();
var date = module.get.focusDate();
if (date && !settings.isDisabled(date, mode) && !(mode === 'day' && settings.disabledDaysOfWeek.includes(date.getDay())) ) {
if (date && !settings.isDisabled(date, mode) && !module.helper.isDisabled(date, mode)) {
module.selectDate(date);
}
//disable form submission:
Expand Down Expand Up @@ -789,6 +800,29 @@ $.fn.calendar = function(parameters) {
},

helper: {
isDisabled: function(date, mode) {
return mode === 'day' && ((settings.disabledDaysOfWeek.indexOf(date.getDay()) !== -1) || settings.disabledDates.some(function(d){
if (d instanceof Date) {
return module.helper.dateEqual(date, d, mode);
}
if (d !== null && typeof d === 'object') {
return module.helper.dateEqual(date, d[metadata.date], mode);
}
}));
},
disabledReason: function(date, mode) {
if (mode === 'day') {
for (var i = 0; i < settings.disabledDates.length; i++) {
var d = settings.disabledDates[i];
if (d !== null && typeof d === 'object' && module.helper.dateEqual(date, d[metadata.date], mode)) {
var reason = {};
reason[metadata.message] = d[metadata.message];
return reason;
}
}
}
return null;
},
sanitiseDate: function (date) {
if (!date) {
return undefined;
Expand Down Expand Up @@ -1073,6 +1107,7 @@ $.fn.calendar.settings = {
endCalendar : null, // jquery object or selector for another calendar that represents the end date of a date range
multiMonth : 1, // show multiple months when in 'day' mode
showWeekNumbers : null, // show Number of Week at the very first column of a dayView
disabledDates : [], // specific day(s) which won't be selectable and contain additional information.
disabledDaysOfWeek : [], // day(s) which won't be selectable(s) (0 = Sunday)
// popup options ('popup', 'on', 'hoverable', and show/hide callbacks are overridden)
popupOptions: {
Expand Down Expand Up @@ -1433,7 +1468,8 @@ $.fn.calendar.settings = {
minDate: 'minDate',
maxDate: 'maxDate',
mode: 'mode',
monthOffset: 'monthOffset'
monthOffset: 'monthOffset',
message: 'message'
}
};

Expand Down
3 changes: 2 additions & 1 deletion src/definitions/modules/calendar.less
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@
}

.ui.calendar .ui.table tr .disabled {
pointer-events: none;
pointer-events: auto;
cursor: default;
color: @disabledTextColor;
}

Expand Down

0 comments on commit 3f7bc5d

Please sign in to comment.