Skip to content

Commit

Permalink
feat(calendar): disabledhours
Browse files Browse the repository at this point in the history
This PR allow some hours to be disabled trough a new parameter called disabledHours.
  • Loading branch information
prudho authored Jul 9, 2021
1 parent 32be6c7 commit d77cc99
Showing 1 changed file with 100 additions and 3 deletions.
103 changes: 100 additions & 3 deletions src/definitions/modules/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,19 @@ $.fn.calendar = function(parameters) {
cell.attr("data-variation", disabledDate[metadata.variation]);
}
}
if (mode === 'hour') {
var disabledHour = module.helper.findHourAsObject(cellDate, mode, settings.disabledHours);
if (disabledHour !== null && disabledHour[metadata.message]) {
cell.attr("data-tooltip", disabledHour[metadata.message]);
cell.attr("data-position", disabledHour[metadata.position] || tooltipPosition);
if(disabledHour[metadata.inverted] || (isInverted && disabledHour[metadata.inverted] === undefined)) {
cell.attr("data-inverted", '');
}
if(disabledHour[metadata.variation]) {
cell.attr("data-variation", disabledHour[metadata.variation]);
}
}
}
} else {
eventDate = module.helper.findDayAsObject(cellDate, mode, settings.eventDates);
if (eventDate !== null) {
Expand Down Expand Up @@ -975,7 +988,7 @@ $.fn.calendar = function(parameters) {

helper: {
isDisabled: function(date, mode) {
return (mode === 'day' || mode === 'month' || mode === 'year') && ((mode === 'day' && settings.disabledDaysOfWeek.indexOf(date.getDay()) !== -1) || settings.disabledDates.some(function(d){
return (mode === 'day' || mode === 'month' || mode === 'year' || mode === 'hour') && (((mode === 'day' && settings.disabledDaysOfWeek.indexOf(date.getDay()) !== -1) || settings.disabledDates.some(function(d){
if(typeof d === 'string') {
d = module.helper.sanitiseDate(d);
}
Expand Down Expand Up @@ -1008,7 +1021,45 @@ $.fn.calendar = function(parameters) {
}
}
}
}));
})) || (mode === 'hour' && settings.disabledHours.some(function(d){
if (typeof d === 'string') {
d = module.helper.sanitiseDate(d);
}
if (d instanceof Date) {
return module.helper.dateEqual(date, d, mode);
} else if (typeof d === 'number') {
return date.getHours() === d;
}
if (d !== null && typeof d === 'object') {
var blocked = true;

if (d[metadata.date]) {
if (d[metadata.date] instanceof Date) {
blocked = module.helper.dateEqual(date, module.helper.sanitiseDate(d[metadata.date]));
} else if (Array.isArray(d[metadata.date])) {
return d[metadata.date].some(function(idate) {
blocked = module.helper.dateEqual(date, idate, mode);
});
}
}

if (d[metadata.days]) {
if (typeof d[metadata.days] === 'number') {
blocked = date.getDay() == d[metadata.days];
} else if (Array.isArray(d[metadata.days])) {
blocked = d[metadata.days].indexOf(date.getDay()) > -1;
}
}

if (d[metadata.hours]) {
if (typeof d[metadata.hours] === 'number') {
return blocked && date.getHours() == d[metadata.hours];
} else if (Array.isArray(d[metadata.hours])) {
return blocked && d[metadata.hours].indexOf(date.getHours()) > -1;
}
}
}
})));
},
isEnabled: function(date, mode) {
if (mode === 'day') {
Expand Down Expand Up @@ -1076,6 +1127,49 @@ $.fn.calendar = function(parameters) {
}
return null;
},
findHourAsObject: function(date, mode, hours) {
if (mode === 'hour') {
var d;
var hourCheck = function(date, d) {
if (d[metadata.hours]) {
if (typeof d[metadata.hours] === 'number' && date.getHours() == d[metadata.hours]) {
return d;
} else if (Array.isArray(d[metadata.hours])) {
if (d[metadata.hours].indexOf(date.getHours()) > -1) {
return d;
}
}
}
}
for (var i = 0; i < hours.length; i++) {
d = hours[i];
if (typeof d === 'number' && date.getHours() == d) {
return null;
} else if (d !== null && typeof d === 'object') {
if (d[metadata.days] && hourCheck(date,d)) {
if (typeof d[metadata.days] === 'number' && date.getDay() == d[metadata.days]) {
return d;
} else if (Array.isArray(d[metadata.days])) {
if (d[metadata.days].indexOf(date.getDay()) > -1) {
return d;
}
}
} else if (d[metadata.date] && hourCheck(date,d)) {
if (d[metadata.date] instanceof Date && module.helper.dateEqual(date, module.helper.sanitiseDate(d[metadata.date]))) {
return d;
} else if (Array.isArray(d[metadata.date])) {
if (d[metadata.date].some(function(idate) { return module.helper.dateEqual(date, idate, mode); })) {
return d;
}
}
} else if (hourCheck(date,d)) {
return d;
}
}
}
}
return null;
},
sanitiseDate: function (date) {
if (!(date instanceof Date)) {
date = parser.date('' + date, settings);
Expand Down Expand Up @@ -1361,6 +1455,7 @@ $.fn.calendar.settings = {
multiMonth : 1, // show multiple months when in 'day' mode
minTimeGap : 5,
showWeekNumbers : null, // show Number of Week at the very first column of a dayView
disabledHours : [], // specific hour(s) which won't be selectable and contain additional information.
disabledDates : [], // specific day(s) which won't be selectable and contain additional information.
disabledDaysOfWeek : [], // day(s) which won't be selectable(s) (0 = Sunday)
enabledDates : [], // specific day(s) which will be selectable, all other days will be disabled
Expand Down Expand Up @@ -1766,7 +1861,9 @@ $.fn.calendar.settings = {
variation: 'variation',
position: 'position',
month: 'month',
year: 'year'
year: 'year',
hours: 'hours',
days: 'days'
},

eventClass: 'blue'
Expand Down

0 comments on commit d77cc99

Please sign in to comment.