comparison media/django/js/calendar.js @ 1:0dcfcdf50c62

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