/ Published in: JavaScript
Released into the public domain.
Expand |
Embed | Plain Text
function find_index(array, string) { var i = 0; for(i=0;i<array.length;i++) { if(array[i]==string) break; } return array[i] == string ? i : -1; } function days_in_month(month, year) { if(month!=2) { if(month==9||month==4||month==6||month==11) return 30; else return 31; } else return year%4==""&&year%100!="" ? 29 : 28; } months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; day_names = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']; function render_month(for_month, for_year, direction, direction_to_change, marked_days) { // for direction, 0/undefined = specified month, 1 = next month, -1 = last month if(typeof for_month=='undefined'||typeof for_year=='undefined') currentDate = new Date(); if(typeof for_month=='undefined') for_month = currentDate.getMonth() + 1; if(typeof for_year=='undefined') for_year = currentDate.getFullYear(); if(typeof direction=='undefined') direction = 0; date_for_calendar_for_month = new Date(for_year, for_month-1, 1); if(direction_to_change=='month') date_for_calendar_for_month.setMonth(for_month-1+direction); else if(direction_to_change=='year') date_for_calendar_for_month.setYear(for_year+direction); if(direction!=0) { for_month = date_for_calendar_for_month.getMonth() + 1; for_year = date_for_calendar_for_month.getFullYear(); } thead_tr = document.createElement('tr'); // render the day headings thead = document.createElement('thead'); for(day in day_names) thead_tr.appendChild(generateElement('th', null, null, day_names[day])); thead.appendChild(thead_tr); tbody = document.createElement('tbody'); this_month = months[for_month-1]; calendar_for_month = generateElement('table', [['class', 'calendarMonth bm'+for_month+'y'+for_year+'e']], [generateElement('caption', null, null, this_month + ' ' + for_year), thead, tbody]); day_of_the_week = date_for_calendar_for_month.getDay(); // get the day the week starts on (0 = Sun, 6 = Sat) date_for_calendar_for_month.setMonth(date_for_calendar_for_month.getMonth()-1); days_in_last_month = days_in_month(date_for_calendar_for_month.getMonth() + 1, date_for_calendar_for_month.getFullYear()); if(day_of_the_week==0) day_of_the_week = 7; // convert to format used by PHP (1 = Mon, 7 = Sun) week_tr = document.createElement('tr'); last_month = months[date_for_calendar_for_month.getMonth()]; year_last_month = date_for_calendar_for_month.getFullYear(); for(var n=1;n<day_of_the_week;n++) { day_of_the_month = days_in_last_month - (day_of_the_week - n - 1); week_tr.appendChild(generateElement('td', [find_index(marked_days, year_last_month + 'm' + (date_for_calendar_for_month.getMonth()+1) + 'd' + day_of_the_month) != -1 ? ['style', 'background: #fcf;'] : null, ['class', 'previousMonth'], ['title', last_month + ' ' + day_of_the_month + ', ' + year_last_month]], null, day_of_the_month)); // pad row with empty cells until the first day } days = days_in_month(for_month, for_year); // get the number of days in the month day = 0; while(day<days) { if(day_of_the_week==8) { if(day != 0) { tbody.appendChild(week_tr); // add the previous week to the tbody week_tr = document.createElement('tr'); // create the tr for the new week } day_of_the_week = 1; // reset day of the week to Monday } week_tr.appendChild(generateElement('td', [find_index(marked_days, for_year + 'm' + for_month + 'd' + (day+1)) != -1 ? ['style', 'background: #fcf;'] : null, ['title', this_month + ' ' + (day+1) + ', ' + for_year]], null, day+1)); // add the cell for the day day_of_the_week++; // increment day of the month day++; // increment date } date_for_calendar_for_month.setMonth(date_for_calendar_for_month.getMonth() + 2); // increment month next_month = months[date_for_calendar_for_month.getMonth()]; year_next_month = date_for_calendar_for_month.getFullYear(); for(var n=day_of_the_week;n<=7;n++) { day_of_the_month = n + 1 - day_of_the_week; week_tr.appendChild(generateElement('td', [find_index(marked_days, year_next_month + 'm' + (date_for_calendar_for_month.getMonth()+1) + 'd' + day_of_the_month) != -1 ? ['style', 'background: #fcf;'] : null, ['class', 'nextMonth'], ['title', next_month + ' ' + day_of_the_month + ', ' + year_next_month]], null, day_of_the_month)); // pad row with empty cells until the end of the row } tbody.appendChild(week_tr); // add last week to tbody calendar_for_month.appendChild(tbody); // add tbody return calendar_for_month; // return table for month } function change_calendar_date(navigationButton, direction_to_change, back, marked_days) { thisCalendar = navigationButton.parentNode.parentNode.nextSibling; thisCalendarDate = thisCalendar.className.match(/bm[0-9]{1,2}?y[0-9]{4}?e/)[0].substring(1).split('y'); newCalendar = render_month(parseInt(thisCalendarDate[0].substring(1)), parseInt(thisCalendarDate[1].substring(0, thisCalendarDate[1].length-1)), typeof back != 'undefined' && back == true ? -1 : 1, direction_to_change, marked_days); thisCalendar.parentNode.replaceChild(newCalendar, thisCalendar); return false; } function render_calendar(start_month, start_year, marked_days) { // marked_days is an array of strings representing dates in the form YYYYmMdD (e.g. April 8, 2011 is 2011m4d8; October 26, 2011 is 2011m10d26 -- basically, leave off leading zeroes) if(typeof start_month=='undefined'||start_month==null||typeof start_year=='undefined'||start_year==null) currentDate = new Date(); if(typeof start_month=='undefined'||start_month==null) start_month = currentDate.getMonth() + 1; if(typeof start_year=='undefined'||start_year==null) start_year = currentDate.getFullYear(); calendarNavigation = document.createElement('ul'); marked_days = '\'' + marked_days.join('\',\'').replace(/[^0-9\,\'md]/g, '') + '\''; navigationButtons = [generateElement('a', [['href', '#'], ['onclick', 'return change_calendar_date(this, \'month\', false, ['+marked_days+']);']], null, '›'), generateElement('a', [['href', '#'], ['onclick', 'return change_calendar_date(this, \'year\', false, ['+marked_days+']);']], null, '»'), generateElement('a', [['href', '#'], ['onclick', 'return change_calendar_date(this, \'year\', true, ['+marked_days+']);']], null, '«'), generateElement('a', [['href', '#'], ['onclick', 'return change_calendar_date(this, \'month\', true, ['+marked_days+']);']], null, '‹')]; for(navigationButton in navigationButtons) calendarNavigation.appendChild(generateElement('li', [['class', 'navigationButton'+navigationButton]], [navigationButtons[navigationButton]])); calendar = generateElement('div', [['class', 'calendar']], [calendarNavigation, render_month(start_month, start_year, null, null, marked_days)]); return calendar.outerHTML; }
You need to login to post a comment.
