Première version en Python-Django.
[auf_paf.git] / static / admin / js / calendar.js
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 firstDayOfWeek: parseInt(get_format('FIRST_DAY_OF_WEEK')),
29 isLeapYear: function(year) {
30 return (((year % 4)==0) && ((year % 100)!=0) || ((year % 400)==0));
31 },
32 getDaysInMonth: function(month,year) {
33 var days;
34 if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) {
35 days = 31;
36 }
37 else if (month==4 || month==6 || month==9 || month==11) {
38 days = 30;
39 }
40 else if (month==2 && CalendarNamespace.isLeapYear(year)) {
41 days = 29;
42 }
43 else {
44 days = 28;
45 }
46 return days;
47 },
48 draw: function(month, year, div_id, callback) { // month = 1-12, year = 1-9999
49 var today = new Date();
50 var todayDay = today.getDate();
51 var todayMonth = today.getMonth()+1;
52 var todayYear = today.getFullYear();
53 var todayClass = '';
54
55 month = parseInt(month);
56 year = parseInt(year);
57 var calDiv = document.getElementById(div_id);
58 removeChildren(calDiv);
59 var calTable = document.createElement('table');
60 quickElement('caption', calTable, CalendarNamespace.monthsOfYear[month-1] + ' ' + year);
61 var tableBody = quickElement('tbody', calTable);
62
63 // Draw days-of-week header
64 var tableRow = quickElement('tr', tableBody);
65 for (var i = 0; i < 7; i++) {
66 quickElement('th', tableRow, CalendarNamespace.daysOfWeek[(i + CalendarNamespace.firstDayOfWeek) % 7]);
67 }
68
69 var startingPos = new Date(year, month-1, 1 - CalendarNamespace.firstDayOfWeek).getDay();
70 var days = CalendarNamespace.getDaysInMonth(month, year);
71
72 // Draw blanks before first of month
73 tableRow = quickElement('tr', tableBody);
74 for (var i = 0; i < startingPos; i++) {
75 var _cell = quickElement('td', tableRow, ' ');
76 _cell.style.backgroundColor = '#f3f3f3';
77 }
78
79 // Draw days of month
80 var currentDay = 1;
81 for (var i = startingPos; currentDay <= days; i++) {
82 if (i%7 == 0 && currentDay != 1) {
83 tableRow = quickElement('tr', tableBody);
84 }
85 if ((currentDay==todayDay) && (month==todayMonth) && (year==todayYear)) {
86 todayClass='today';
87 } else {
88 todayClass='';
89 }
90 var cell = quickElement('td', tableRow, '', 'class', todayClass);
91
92 quickElement('a', cell, currentDay, 'href', 'javascript:void(' + callback + '('+year+','+month+','+currentDay+'));');
93 currentDay++;
94 }
95
96 // Draw blanks after end of month (optional, but makes for valid code)
97 while (tableRow.childNodes.length < 7) {
98 var _cell = quickElement('td', tableRow, ' ');
99 _cell.style.backgroundColor = '#f3f3f3';
100 }
101
102 calDiv.appendChild(calTable);
103 }
104 }
105
106 // Calendar -- A calendar instance
107 function Calendar(div_id, callback) {
108 // div_id (string) is the ID of the element in which the calendar will
109 // be displayed
110 // callback (string) is the name of a JavaScript function that will be
111 // called with the parameters (year, month, day) when a day in the
112 // calendar is clicked
113 this.div_id = div_id;
114 this.callback = callback;
115 this.today = new Date();
116 this.currentMonth = this.today.getMonth() + 1;
117 this.currentYear = this.today.getFullYear();
118 }
119 Calendar.prototype = {
120 drawCurrent: function() {
121 CalendarNamespace.draw(this.currentMonth, this.currentYear, this.div_id, this.callback);
122 },
123 drawDate: function(month, year) {
124 this.currentMonth = month;
125 this.currentYear = year;
126 this.drawCurrent();
127 },
128 drawPreviousMonth: function() {
129 if (this.currentMonth == 1) {
130 this.currentMonth = 12;
131 this.currentYear--;
132 }
133 else {
134 this.currentMonth--;
135 }
136 this.drawCurrent();
137 },
138 drawNextMonth: function() {
139 if (this.currentMonth == 12) {
140 this.currentMonth = 1;
141 this.currentYear++;
142 }
143 else {
144 this.currentMonth++;
145 }
146 this.drawCurrent();
147 },
148 drawPreviousYear: function() {
149 this.currentYear--;
150 this.drawCurrent();
151 },
152 drawNextYear: function() {
153 this.currentYear++;
154 this.drawCurrent();
155 }
156 }