comparison media/django/js/admin/DateTimeShortcuts.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 // Inserts shortcut buttons after all of the following:
2 // <input type="text" class="vDateField">
3 // <input type="text" class="vTimeField">
4
5 var DateTimeShortcuts = {
6 calendars: [],
7 calendarInputs: [],
8 clockInputs: [],
9 calendarDivName1: 'calendarbox', // name of calendar <div> that gets toggled
10 calendarDivName2: 'calendarin', // name of <div> that contains calendar
11 calendarLinkName: 'calendarlink',// name of the link that is used to toggle
12 clockDivName: 'clockbox', // name of clock <div> that gets toggled
13 clockLinkName: 'clocklink', // name of the link that is used to toggle
14 admin_media_prefix: '',
15 init: function() {
16 // Deduce admin_media_prefix by looking at the <script>s in the
17 // current document and finding the URL of *this* module.
18 var scripts = document.getElementsByTagName('script');
19 for (var i=0; i<scripts.length; i++) {
20 if (scripts[i].src.match(/DateTimeShortcuts/)) {
21 var idx = scripts[i].src.indexOf('js/admin/DateTimeShortcuts');
22 DateTimeShortcuts.admin_media_prefix = scripts[i].src.substring(0, idx);
23 break;
24 }
25 }
26
27 var inputs = document.getElementsByTagName('input');
28 for (i=0; i<inputs.length; i++) {
29 var inp = inputs[i];
30 if (inp.getAttribute('type') == 'text' && inp.className.match(/vTimeField/)) {
31 DateTimeShortcuts.addClock(inp);
32 }
33 else if (inp.getAttribute('type') == 'text' && inp.className.match(/vDateField/)) {
34 DateTimeShortcuts.addCalendar(inp);
35 }
36 }
37 },
38 // Add clock widget to a given field
39 addClock: function(inp) {
40 var num = DateTimeShortcuts.clockInputs.length;
41 DateTimeShortcuts.clockInputs[num] = inp;
42
43 // Shortcut links (clock icon and "Now" link)
44 var shortcuts_span = document.createElement('span');
45 inp.parentNode.insertBefore(shortcuts_span, inp.nextSibling);
46 var now_link = document.createElement('a');
47 now_link.setAttribute('href', "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", new Date().getHourMinuteSecond());");
48 now_link.appendChild(document.createTextNode(gettext('Now')));
49 var clock_link = document.createElement('a');
50 clock_link.setAttribute('href', 'javascript:DateTimeShortcuts.openClock(' + num + ');');
51 clock_link.id = DateTimeShortcuts.clockLinkName + num;
52 quickElement('img', clock_link, '', 'src', DateTimeShortcuts.admin_media_prefix + 'img/admin/icon_clock.gif', 'alt', gettext('Clock'));
53 shortcuts_span.appendChild(document.createTextNode('\240'));
54 shortcuts_span.appendChild(now_link);
55 shortcuts_span.appendChild(document.createTextNode('\240|\240'));
56 shortcuts_span.appendChild(clock_link);
57
58 // Create clock link div
59 //
60 // Markup looks like:
61 // <div id="clockbox1" class="clockbox module">
62 // <h2>Choose a time</h2>
63 // <ul class="timelist">
64 // <li><a href="#">Now</a></li>
65 // <li><a href="#">Midnight</a></li>
66 // <li><a href="#">6 a.m.</a></li>
67 // <li><a href="#">Noon</a></li>
68 // </ul>
69 // <p class="calendar-cancel"><a href="#">Cancel</a></p>
70 // </div>
71
72 var clock_box = document.createElement('div');
73 clock_box.style.display = 'none';
74 clock_box.style.position = 'absolute';
75 clock_box.className = 'clockbox module';
76 clock_box.setAttribute('id', DateTimeShortcuts.clockDivName + num);
77 document.body.appendChild(clock_box);
78 addEvent(clock_box, 'click', DateTimeShortcuts.cancelEventPropagation);
79
80 quickElement('h2', clock_box, gettext('Choose a time'));
81 time_list = quickElement('ul', clock_box, '');
82 time_list.className = 'timelist';
83 quickElement("a", quickElement("li", time_list, ""), gettext("Now"), "href", "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", new Date().getHourMinuteSecond());")
84 quickElement("a", quickElement("li", time_list, ""), gettext("Midnight"), "href", "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", '00:00:00');")
85 quickElement("a", quickElement("li", time_list, ""), gettext("6 a.m."), "href", "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", '06:00:00');")
86 quickElement("a", quickElement("li", time_list, ""), gettext("Noon"), "href", "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", '12:00:00');")
87
88 cancel_p = quickElement('p', clock_box, '');
89 cancel_p.className = 'calendar-cancel';
90 quickElement('a', cancel_p, gettext('Cancel'), 'href', 'javascript:DateTimeShortcuts.dismissClock(' + num + ');');
91 },
92 openClock: function(num) {
93 var clock_box = document.getElementById(DateTimeShortcuts.clockDivName+num)
94 var clock_link = document.getElementById(DateTimeShortcuts.clockLinkName+num)
95
96 // Recalculate the clockbox position
97 // is it left-to-right or right-to-left layout ?
98 if (getStyle(document.body,'direction')!='rtl') {
99 clock_box.style.left = findPosX(clock_link) + 17 + 'px';
100 }
101 else {
102 // since style's width is in em, it'd be tough to calculate
103 // px value of it. let's use an estimated px for now
104 // TODO: IE returns wrong value for findPosX when in rtl mode
105 // (it returns as it was left aligned), needs to be fixed.
106 clock_box.style.left = findPosX(clock_link) - 110 + 'px';
107 }
108 clock_box.style.top = findPosY(clock_link) - 30 + 'px';
109
110 // Show the clock box
111 clock_box.style.display = 'block';
112 addEvent(window.document, 'click', function() { DateTimeShortcuts.dismissClock(num); return true; });
113 },
114 dismissClock: function(num) {
115 document.getElementById(DateTimeShortcuts.clockDivName + num).style.display = 'none';
116 window.document.onclick = null;
117 },
118 handleClockQuicklink: function(num, val) {
119 DateTimeShortcuts.clockInputs[num].value = val;
120 DateTimeShortcuts.dismissClock(num);
121 },
122 // Add calendar widget to a given field.
123 addCalendar: function(inp) {
124 var num = DateTimeShortcuts.calendars.length;
125
126 DateTimeShortcuts.calendarInputs[num] = inp;
127
128 // Shortcut links (calendar icon and "Today" link)
129 var shortcuts_span = document.createElement('span');
130 inp.parentNode.insertBefore(shortcuts_span, inp.nextSibling);
131 var today_link = document.createElement('a');
132 today_link.setAttribute('href', 'javascript:DateTimeShortcuts.handleCalendarQuickLink(' + num + ', 0);');
133 today_link.appendChild(document.createTextNode(gettext('Today')));
134 var cal_link = document.createElement('a');
135 cal_link.setAttribute('href', 'javascript:DateTimeShortcuts.openCalendar(' + num + ');');
136 cal_link.id = DateTimeShortcuts.calendarLinkName + num;
137 quickElement('img', cal_link, '', 'src', DateTimeShortcuts.admin_media_prefix + 'img/admin/icon_calendar.gif', 'alt', gettext('Calendar'));
138 shortcuts_span.appendChild(document.createTextNode('\240'));
139 shortcuts_span.appendChild(today_link);
140 shortcuts_span.appendChild(document.createTextNode('\240|\240'));
141 shortcuts_span.appendChild(cal_link);
142
143 // Create calendarbox div.
144 //
145 // Markup looks like:
146 //
147 // <div id="calendarbox3" class="calendarbox module">
148 // <h2>
149 // <a href="#" class="link-previous">&lsaquo;</a>
150 // <a href="#" class="link-next">&rsaquo;</a> February 2003
151 // </h2>
152 // <div class="calendar" id="calendarin3">
153 // <!-- (cal) -->
154 // </div>
155 // <div class="calendar-shortcuts">
156 // <a href="#">Yesterday</a> | <a href="#">Today</a> | <a href="#">Tomorrow</a>
157 // </div>
158 // <p class="calendar-cancel"><a href="#">Cancel</a></p>
159 // </div>
160 var cal_box = document.createElement('div');
161 cal_box.style.display = 'none';
162 cal_box.style.position = 'absolute';
163 cal_box.className = 'calendarbox module';
164 cal_box.setAttribute('id', DateTimeShortcuts.calendarDivName1 + num);
165 document.body.appendChild(cal_box);
166 addEvent(cal_box, 'click', DateTimeShortcuts.cancelEventPropagation);
167
168 // next-prev links
169 var cal_nav = quickElement('div', cal_box, '');
170 var cal_nav_prev = quickElement('a', cal_nav, '<', 'href', 'javascript:DateTimeShortcuts.drawPrev('+num+');');
171 cal_nav_prev.className = 'calendarnav-previous';
172 var cal_nav_next = quickElement('a', cal_nav, '>', 'href', 'javascript:DateTimeShortcuts.drawNext('+num+');');
173 cal_nav_next.className = 'calendarnav-next';
174
175 // main box
176 var cal_main = quickElement('div', cal_box, '', 'id', DateTimeShortcuts.calendarDivName2 + num);
177 cal_main.className = 'calendar';
178 DateTimeShortcuts.calendars[num] = new Calendar(DateTimeShortcuts.calendarDivName2 + num, DateTimeShortcuts.handleCalendarCallback(num));
179 DateTimeShortcuts.calendars[num].drawCurrent();
180
181 // calendar shortcuts
182 var shortcuts = quickElement('div', cal_box, '');
183 shortcuts.className = 'calendar-shortcuts';
184 quickElement('a', shortcuts, gettext('Yesterday'), 'href', 'javascript:DateTimeShortcuts.handleCalendarQuickLink(' + num + ', -1);');
185 shortcuts.appendChild(document.createTextNode('\240|\240'));
186 quickElement('a', shortcuts, gettext('Today'), 'href', 'javascript:DateTimeShortcuts.handleCalendarQuickLink(' + num + ', 0);');
187 shortcuts.appendChild(document.createTextNode('\240|\240'));
188 quickElement('a', shortcuts, gettext('Tomorrow'), 'href', 'javascript:DateTimeShortcuts.handleCalendarQuickLink(' + num + ', +1);');
189
190 // cancel bar
191 var cancel_p = quickElement('p', cal_box, '');
192 cancel_p.className = 'calendar-cancel';
193 quickElement('a', cancel_p, gettext('Cancel'), 'href', 'javascript:DateTimeShortcuts.dismissCalendar(' + num + ');');
194 },
195 openCalendar: function(num) {
196 var cal_box = document.getElementById(DateTimeShortcuts.calendarDivName1+num)
197 var cal_link = document.getElementById(DateTimeShortcuts.calendarLinkName+num)
198 var inp = DateTimeShortcuts.calendarInputs[num];
199
200 // Determine if the current value in the input has a valid date.
201 // If so, draw the calendar with that date's year and month.
202 if (inp.value) {
203 var date_parts = inp.value.split('-');
204 var year = date_parts[0];
205 var month = parseFloat(date_parts[1]);
206 if (year.match(/\d\d\d\d/) && month >= 1 && month <= 12) {
207 DateTimeShortcuts.calendars[num].drawDate(month, year);
208 }
209 }
210
211
212 // Recalculate the clockbox position
213 // is it left-to-right or right-to-left layout ?
214 if (getStyle(document.body,'direction')!='rtl') {
215 cal_box.style.left = findPosX(cal_link) + 17 + 'px';
216 }
217 else {
218 // since style's width is in em, it'd be tough to calculate
219 // px value of it. let's use an estimated px for now
220 // TODO: IE returns wrong value for findPosX when in rtl mode
221 // (it returns as it was left aligned), needs to be fixed.
222 cal_box.style.left = findPosX(cal_link) - 180 + 'px';
223 }
224 cal_box.style.top = findPosY(cal_link) - 75 + 'px';
225
226 cal_box.style.display = 'block';
227 addEvent(window.document, 'click', function() { DateTimeShortcuts.dismissCalendar(num); return true; });
228 },
229 dismissCalendar: function(num) {
230 document.getElementById(DateTimeShortcuts.calendarDivName1+num).style.display = 'none';
231 window.document.onclick = null;
232 },
233 drawPrev: function(num) {
234 DateTimeShortcuts.calendars[num].drawPreviousMonth();
235 },
236 drawNext: function(num) {
237 DateTimeShortcuts.calendars[num].drawNextMonth();
238 },
239 handleCalendarCallback: function(num) {
240 return "function(y, m, d) { DateTimeShortcuts.calendarInputs["+num+"].value = y+'-'+(m<10?'0':'')+m+'-'+(d<10?'0':'')+d; document.getElementById(DateTimeShortcuts.calendarDivName1+"+num+").style.display='none';}";
241 },
242 handleCalendarQuickLink: function(num, offset) {
243 var d = new Date();
244 d.setDate(d.getDate() + offset)
245 DateTimeShortcuts.calendarInputs[num].value = d.getISODate();
246 DateTimeShortcuts.dismissCalendar(num);
247 },
248 cancelEventPropagation: function(e) {
249 if (!e) e = window.event;
250 e.cancelBubble = true;
251 if (e.stopPropagation) e.stopPropagation();
252 }
253 }
254
255 addEvent(window, 'load', DateTimeShortcuts.init);