bgneal@1
|
1 /*
|
bgneal@1
|
2 calendar.js - Calendar functions by Adrian Holovaty
|
bgneal@1
|
3 */
|
bgneal@1
|
4
|
bgneal@1
|
5 function removeChildren(a) { // "a" is reference to an object
|
bgneal@1
|
6 while (a.hasChildNodes()) a.removeChild(a.lastChild);
|
bgneal@1
|
7 }
|
bgneal@1
|
8
|
bgneal@1
|
9 // quickElement(tagType, parentReference, textInChildNode, [, attribute, attributeValue ...]);
|
bgneal@1
|
10 function quickElement() {
|
bgneal@1
|
11 var obj = document.createElement(arguments[0]);
|
bgneal@1
|
12 if (arguments[2] != '' && arguments[2] != null) {
|
bgneal@1
|
13 var textNode = document.createTextNode(arguments[2]);
|
bgneal@1
|
14 obj.appendChild(textNode);
|
bgneal@1
|
15 }
|
bgneal@1
|
16 var len = arguments.length;
|
bgneal@1
|
17 for (var i = 3; i < len; i += 2) {
|
bgneal@1
|
18 obj.setAttribute(arguments[i], arguments[i+1]);
|
bgneal@1
|
19 }
|
bgneal@1
|
20 arguments[1].appendChild(obj);
|
bgneal@1
|
21 return obj;
|
bgneal@1
|
22 }
|
bgneal@1
|
23
|
bgneal@1
|
24 // CalendarNamespace -- Provides a collection of HTML calendar-related helper functions
|
bgneal@1
|
25 var CalendarNamespace = {
|
bgneal@1
|
26 monthsOfYear: gettext('January February March April May June July August September October November December').split(' '),
|
bgneal@1
|
27 daysOfWeek: gettext('S M T W T F S').split(' '),
|
bgneal@1
|
28 isLeapYear: function(year) {
|
bgneal@1
|
29 return (((year % 4)==0) && ((year % 100)!=0) || ((year % 400)==0));
|
bgneal@1
|
30 },
|
bgneal@1
|
31 getDaysInMonth: function(month,year) {
|
bgneal@1
|
32 var days;
|
bgneal@1
|
33 if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) {
|
bgneal@1
|
34 days = 31;
|
bgneal@1
|
35 }
|
bgneal@1
|
36 else if (month==4 || month==6 || month==9 || month==11) {
|
bgneal@1
|
37 days = 30;
|
bgneal@1
|
38 }
|
bgneal@1
|
39 else if (month==2 && CalendarNamespace.isLeapYear(year)) {
|
bgneal@1
|
40 days = 29;
|
bgneal@1
|
41 }
|
bgneal@1
|
42 else {
|
bgneal@1
|
43 days = 28;
|
bgneal@1
|
44 }
|
bgneal@1
|
45 return days;
|
bgneal@1
|
46 },
|
bgneal@1
|
47 draw: function(month, year, div_id, callback) { // month = 1-12, year = 1-9999
|
bgneal@1
|
48 month = parseInt(month);
|
bgneal@1
|
49 year = parseInt(year);
|
bgneal@1
|
50 var calDiv = document.getElementById(div_id);
|
bgneal@1
|
51 removeChildren(calDiv);
|
bgneal@1
|
52 var calTable = document.createElement('table');
|
bgneal@1
|
53 quickElement('caption', calTable, CalendarNamespace.monthsOfYear[month-1] + ' ' + year);
|
bgneal@1
|
54 var tableBody = quickElement('tbody', calTable);
|
bgneal@1
|
55
|
bgneal@1
|
56 // Draw days-of-week header
|
bgneal@1
|
57 var tableRow = quickElement('tr', tableBody);
|
bgneal@1
|
58 for (var i = 0; i < 7; i++) {
|
bgneal@1
|
59 quickElement('th', tableRow, CalendarNamespace.daysOfWeek[i]);
|
bgneal@1
|
60 }
|
bgneal@1
|
61
|
bgneal@1
|
62 var startingPos = new Date(year, month-1, 1).getDay();
|
bgneal@1
|
63 var days = CalendarNamespace.getDaysInMonth(month, year);
|
bgneal@1
|
64
|
bgneal@1
|
65 // Draw blanks before first of month
|
bgneal@1
|
66 tableRow = quickElement('tr', tableBody);
|
bgneal@1
|
67 for (var i = 0; i < startingPos; i++) {
|
bgneal@1
|
68 var _cell = quickElement('td', tableRow, ' ');
|
bgneal@1
|
69 _cell.style.backgroundColor = '#f3f3f3';
|
bgneal@1
|
70 }
|
bgneal@1
|
71
|
bgneal@1
|
72 // Draw days of month
|
bgneal@1
|
73 var currentDay = 1;
|
bgneal@1
|
74 for (var i = startingPos; currentDay <= days; i++) {
|
bgneal@1
|
75 if (i%7 == 0 && currentDay != 1) {
|
bgneal@1
|
76 tableRow = quickElement('tr', tableBody);
|
bgneal@1
|
77 }
|
bgneal@1
|
78 var cell = quickElement('td', tableRow, '');
|
bgneal@1
|
79 quickElement('a', cell, currentDay, 'href', 'javascript:void(' + callback + '('+year+','+month+','+currentDay+'));');
|
bgneal@1
|
80 currentDay++;
|
bgneal@1
|
81 }
|
bgneal@1
|
82
|
bgneal@1
|
83 // Draw blanks after end of month (optional, but makes for valid code)
|
bgneal@1
|
84 while (tableRow.childNodes.length < 7) {
|
bgneal@1
|
85 var _cell = quickElement('td', tableRow, ' ');
|
bgneal@1
|
86 _cell.style.backgroundColor = '#f3f3f3';
|
bgneal@1
|
87 }
|
bgneal@1
|
88
|
bgneal@1
|
89 calDiv.appendChild(calTable);
|
bgneal@1
|
90 }
|
bgneal@1
|
91 }
|
bgneal@1
|
92
|
bgneal@1
|
93 // Calendar -- A calendar instance
|
bgneal@1
|
94 function Calendar(div_id, callback) {
|
bgneal@1
|
95 // div_id (string) is the ID of the element in which the calendar will
|
bgneal@1
|
96 // be displayed
|
bgneal@1
|
97 // callback (string) is the name of a JavaScript function that will be
|
bgneal@1
|
98 // called with the parameters (year, month, day) when a day in the
|
bgneal@1
|
99 // calendar is clicked
|
bgneal@1
|
100 this.div_id = div_id;
|
bgneal@1
|
101 this.callback = callback;
|
bgneal@1
|
102 this.today = new Date();
|
bgneal@1
|
103 this.currentMonth = this.today.getMonth() + 1;
|
bgneal@1
|
104 this.currentYear = this.today.getFullYear();
|
bgneal@1
|
105 }
|
bgneal@1
|
106 Calendar.prototype = {
|
bgneal@1
|
107 drawCurrent: function() {
|
bgneal@1
|
108 CalendarNamespace.draw(this.currentMonth, this.currentYear, this.div_id, this.callback);
|
bgneal@1
|
109 },
|
bgneal@1
|
110 drawDate: function(month, year) {
|
bgneal@1
|
111 this.currentMonth = month;
|
bgneal@1
|
112 this.currentYear = year;
|
bgneal@1
|
113 this.drawCurrent();
|
bgneal@1
|
114 },
|
bgneal@1
|
115 drawPreviousMonth: function() {
|
bgneal@1
|
116 if (this.currentMonth == 1) {
|
bgneal@1
|
117 this.currentMonth = 12;
|
bgneal@1
|
118 this.currentYear--;
|
bgneal@1
|
119 }
|
bgneal@1
|
120 else {
|
bgneal@1
|
121 this.currentMonth--;
|
bgneal@1
|
122 }
|
bgneal@1
|
123 this.drawCurrent();
|
bgneal@1
|
124 },
|
bgneal@1
|
125 drawNextMonth: function() {
|
bgneal@1
|
126 if (this.currentMonth == 12) {
|
bgneal@1
|
127 this.currentMonth = 1;
|
bgneal@1
|
128 this.currentYear++;
|
bgneal@1
|
129 }
|
bgneal@1
|
130 else {
|
bgneal@1
|
131 this.currentMonth++;
|
bgneal@1
|
132 }
|
bgneal@1
|
133 this.drawCurrent();
|
bgneal@1
|
134 },
|
bgneal@1
|
135 drawPreviousYear: function() {
|
bgneal@1
|
136 this.currentYear--;
|
bgneal@1
|
137 this.drawCurrent();
|
bgneal@1
|
138 },
|
bgneal@1
|
139 drawNextYear: function() {
|
bgneal@1
|
140 this.currentYear++;
|
bgneal@1
|
141 this.drawCurrent();
|
bgneal@1
|
142 }
|
bgneal@1
|
143 }
|