// sample json response
// [{"date":"2011-4-01","type":"free"}]
// declare freeDays global arrays
var freeDays = [];
$(document).ready(function() {
// perform initial json request for day usage
fetchDayUsage();
});
// query for day usage in datepicker - year, month are passed by the datepicker object
function fetchDayUsage(year, month)
{
var start_date = '';
// if a month and year were supplied, build a start_date in yyyy-mm-dd format
if (year != undefined && month != undefined) {
start_date = year +'-';
start_date += month +'-';
start_date += '01';
}
$.ajax({
async: false,
url: "ajax.todos?action=fetchDayUsage&start_date="+ start_date,
dataType: "json",
success: function(data) {
// loop over json response
$.each(data, function(index, value) {
// add to freeDays array
switch (value.type) {
case 'free':
freeDays.push(value.date); // add this date to the freeDays array
break;
}
});
// datepicker popout for date due on create new to-do
$("#datepicker").datepicker({
changeMonth: true,
changeYear: true,
showOtherMonths: true,
selectOtherMonths: true,
//numberOfMonths: 2,
dateFormat: 'DD, d MM, yy',
altField: '#date_due',
altFormat: 'yy-mm-dd',
beforeShowDay: highlightDays,
onChangeMonthYear: fetchDayUsage,
firstDay: 1 // rows starts on Monday
});
}
});
}
// runs for every day displayed in datepicker, adds class and tooltip if matched to days in freeDays array
function highlightDays(date)
{
// loop free days
for (var a=0; a<freeDays.length; a++) {
if (new Date(freeDays[a]).toString() == date.toString()) {
return [true, 'free-day', 'no to-do items due']; // [0] = true | false if this day is selectable, [1] = class to add, [2] = tooltip to display
}
}
return [true, ''];
}
Bookmarks