1 /*global addEvent, Calendar, cancelEventPropagation, findPosX, findPosY, getStyle, get_format, gettext, interpolate, ngettext, quickElement, removeEvent*/
2 // Inserts shortcut buttons after all of the following:
3 // <input type="text" class="vDateField">
4 // <input type="text" class="vTimeField">
7 var DateTimeShortcuts = {
12 dismissCalendarFunc: [],
13 calendarDivName1: 'calendarbox', // name of calendar <div> that gets toggled
14 calendarDivName2: 'calendarin', // name of <div> that contains calendar
15 calendarLinkName: 'calendarlink',// name of the link that is used to toggle
16 clockDivName: 'clockbox', // name of clock <div> that gets toggled
17 clockLinkName: 'clocklink', // name of the link that is used to toggle
18 shortCutsClass: 'datetimeshortcuts', // class of the clock and cal shortcuts
19 timezoneWarningClass: 'timezonewarning', // class of the warning for timezone mismatch
22 var body = document.getElementsByTagName('body')[0];
23 var serverOffset = body.getAttribute('data-admin-utc-offset');
25 var localOffset = new Date().getTimezoneOffset() * -60;
26 DateTimeShortcuts.timezoneOffset = localOffset - serverOffset;
29 var inputs = document.getElementsByTagName('input');
30 for (var i = 0; i < inputs.length; i++) {
32 if (inp.getAttribute('type') === 'text' && inp.className.match(/vTimeField/)) {
33 DateTimeShortcuts.addClock(inp);
34 DateTimeShortcuts.addTimezoneWarning(inp);
36 else if (inp.getAttribute('type') === 'text' && inp.className.match(/vDateField/)) {
37 DateTimeShortcuts.addCalendar(inp);
38 DateTimeShortcuts.addTimezoneWarning(inp);
42 // Return the current time while accounting for the server timezone.
44 var body = document.getElementsByTagName('body')[0];
45 var serverOffset = body.getAttribute('data-admin-utc-offset');
47 var localNow = new Date();
48 var localOffset = localNow.getTimezoneOffset() * -60;
49 localNow.setTime(localNow.getTime() + 1000 * (serverOffset - localOffset));
55 // Add a warning when the time zone in the browser and backend do not match.
56 addTimezoneWarning: function(inp) {
57 var $ = django.jQuery;
58 var warningClass = DateTimeShortcuts.timezoneWarningClass;
59 var timezoneOffset = DateTimeShortcuts.timezoneOffset / 3600;
61 // Only warn if there is a time zone mismatch.
62 if (!timezoneOffset) {
66 // Check if warning is already there.
67 if ($(inp).siblings('.' + warningClass).length) {
72 if (timezoneOffset > 0) {
74 'Note: You are %s hour ahead of server time.',
75 'Note: You are %s hours ahead of server time.',
82 'Note: You are %s hour behind server time.',
83 'Note: You are %s hours behind server time.',
87 message = interpolate(message, [timezoneOffset]);
89 var $warning = $('<span>');
90 $warning.attr('class', warningClass);
91 $warning.text(message);
97 // Add clock widget to a given field
98 addClock: function(inp) {
99 var num = DateTimeShortcuts.clockInputs.length;
100 DateTimeShortcuts.clockInputs[num] = inp;
101 DateTimeShortcuts.dismissClockFunc[num] = function() { DateTimeShortcuts.dismissClock(num); return true; };
103 // Shortcut links (clock icon and "Now" link)
104 var shortcuts_span = document.createElement('span');
105 shortcuts_span.className = DateTimeShortcuts.shortCutsClass;
106 inp.parentNode.insertBefore(shortcuts_span, inp.nextSibling);
107 var now_link = document.createElement('a');
108 now_link.setAttribute('href', "#");
109 now_link.appendChild(document.createTextNode(gettext('Now')));
110 addEvent(now_link, 'click', function(e) {
112 DateTimeShortcuts.handleClockQuicklink(num, -1);
114 var clock_link = document.createElement('a');
115 clock_link.setAttribute('href', '#');
116 clock_link.id = DateTimeShortcuts.clockLinkName + num;
117 addEvent(clock_link, 'click', function(e) {
119 // avoid triggering the document click handler to dismiss the clock
121 DateTimeShortcuts.openClock(num);
125 'span', clock_link, '',
126 'class', 'clock-icon',
127 'title', gettext('Choose a Time')
129 shortcuts_span.appendChild(document.createTextNode('\u00A0'));
130 shortcuts_span.appendChild(now_link);
131 shortcuts_span.appendChild(document.createTextNode('\u00A0|\u00A0'));
132 shortcuts_span.appendChild(clock_link);
134 // Create clock link div
136 // Markup looks like:
137 // <div id="clockbox1" class="clockbox module">
138 // <h2>Choose a time</h2>
139 // <ul class="timelist">
140 // <li><a href="#">Now</a></li>
141 // <li><a href="#">Midnight</a></li>
142 // <li><a href="#">6 a.m.</a></li>
143 // <li><a href="#">Noon</a></li>
144 // <li><a href="#">6 p.m.</a></li>
146 // <p class="calendar-cancel"><a href="#">Cancel</a></p>
149 var clock_box = document.createElement('div');
150 clock_box.style.display = 'none';
151 clock_box.style.position = 'absolute';
152 clock_box.className = 'clockbox module';
153 clock_box.setAttribute('id', DateTimeShortcuts.clockDivName + num);
154 document.body.appendChild(clock_box);
155 addEvent(clock_box, 'click', cancelEventPropagation);
157 quickElement('h2', clock_box, gettext('Choose a time'));
158 var time_list = quickElement('ul', clock_box);
159 time_list.className = 'timelist';
160 var time_link = quickElement("a", quickElement("li", time_list), gettext("Now"), "href", "#");
161 addEvent(time_link, 'click', function(e) {
163 DateTimeShortcuts.handleClockQuicklink(num, -1);
165 time_link = quickElement("a", quickElement("li", time_list), gettext("Midnight"), "href", "#");
166 addEvent(time_link, 'click', function(e) {
168 DateTimeShortcuts.handleClockQuicklink(num, 0);
170 time_link = quickElement("a", quickElement("li", time_list), gettext("6 a.m."), "href", "#");
171 addEvent(time_link, 'click', function(e) {
173 DateTimeShortcuts.handleClockQuicklink(num, 6);
175 time_link = quickElement("a", quickElement("li", time_list), gettext("Noon"), "href", "#");
176 addEvent(time_link, 'click', function(e) {
178 DateTimeShortcuts.handleClockQuicklink(num, 12);
180 time_link = quickElement("a", quickElement("li", time_list), gettext("6 p.m."), "href", "#");
181 addEvent(time_link, 'click', function(e) {
183 DateTimeShortcuts.handleClockQuicklink(num, 18);
186 var cancel_p = quickElement('p', clock_box);
187 cancel_p.className = 'calendar-cancel';
188 var cancel_link = quickElement('a', cancel_p, gettext('Cancel'), 'href', '#');
189 addEvent(cancel_link, 'click', function(e) {
191 DateTimeShortcuts.dismissClock(num);
194 django.jQuery(document).bind('keyup', function(event) {
195 if (event.which === 27) {
196 // ESC key closes popup
197 DateTimeShortcuts.dismissClock(num);
198 event.preventDefault();
202 openClock: function(num) {
203 var clock_box = document.getElementById(DateTimeShortcuts.clockDivName + num);
204 var clock_link = document.getElementById(DateTimeShortcuts.clockLinkName + num);
206 // Recalculate the clockbox position
207 // is it left-to-right or right-to-left layout ?
208 if (getStyle(document.body, 'direction') !== 'rtl') {
209 clock_box.style.left = findPosX(clock_link) + 17 + 'px';
212 // since style's width is in em, it'd be tough to calculate
213 // px value of it. let's use an estimated px for now
214 // TODO: IE returns wrong value for findPosX when in rtl mode
215 // (it returns as it was left aligned), needs to be fixed.
216 clock_box.style.left = findPosX(clock_link) - 110 + 'px';
218 clock_box.style.top = Math.max(0, findPosY(clock_link) - 30) + 'px';
220 // Show the clock box
221 clock_box.style.display = 'block';
222 addEvent(document, 'click', DateTimeShortcuts.dismissClockFunc[num]);
224 dismissClock: function(num) {
225 document.getElementById(DateTimeShortcuts.clockDivName + num).style.display = 'none';
226 removeEvent(document, 'click', DateTimeShortcuts.dismissClockFunc[num]);
228 handleClockQuicklink: function(num, val) {
231 d = DateTimeShortcuts.now();
234 d = new Date(1970, 1, 1, val, 0, 0, 0);
236 DateTimeShortcuts.clockInputs[num].value = d.strftime(get_format('TIME_INPUT_FORMATS')[0]);
237 DateTimeShortcuts.clockInputs[num].focus();
238 DateTimeShortcuts.dismissClock(num);
240 // Add calendar widget to a given field.
241 addCalendar: function(inp) {
242 var num = DateTimeShortcuts.calendars.length;
244 DateTimeShortcuts.calendarInputs[num] = inp;
245 DateTimeShortcuts.dismissCalendarFunc[num] = function() { DateTimeShortcuts.dismissCalendar(num); return true; };
247 // Shortcut links (calendar icon and "Today" link)
248 var shortcuts_span = document.createElement('span');
249 shortcuts_span.className = DateTimeShortcuts.shortCutsClass;
250 inp.parentNode.insertBefore(shortcuts_span, inp.nextSibling);
251 var today_link = document.createElement('a');
252 today_link.setAttribute('href', '#');
253 today_link.appendChild(document.createTextNode(gettext('Today')));
254 addEvent(today_link, 'click', function(e) {
256 DateTimeShortcuts.handleCalendarQuickLink(num, 0);
258 var cal_link = document.createElement('a');
259 cal_link.setAttribute('href', '#');
260 cal_link.id = DateTimeShortcuts.calendarLinkName + num;
261 addEvent(cal_link, 'click', function(e) {
263 // avoid triggering the document click handler to dismiss the calendar
265 DateTimeShortcuts.openCalendar(num);
268 'span', cal_link, '',
269 'class', 'date-icon',
270 'title', gettext('Choose a Date')
272 shortcuts_span.appendChild(document.createTextNode('\u00A0'));
273 shortcuts_span.appendChild(today_link);
274 shortcuts_span.appendChild(document.createTextNode('\u00A0|\u00A0'));
275 shortcuts_span.appendChild(cal_link);
277 // Create calendarbox div.
279 // Markup looks like:
281 // <div id="calendarbox3" class="calendarbox module">
283 // <a href="#" class="link-previous">‹</a>
284 // <a href="#" class="link-next">›</a> February 2003
286 // <div class="calendar" id="calendarin3">
289 // <div class="calendar-shortcuts">
290 // <a href="#">Yesterday</a> | <a href="#">Today</a> | <a href="#">Tomorrow</a>
292 // <p class="calendar-cancel"><a href="#">Cancel</a></p>
294 var cal_box = document.createElement('div');
295 cal_box.style.display = 'none';
296 cal_box.style.position = 'absolute';
297 cal_box.className = 'calendarbox module';
298 cal_box.setAttribute('id', DateTimeShortcuts.calendarDivName1 + num);
299 document.body.appendChild(cal_box);
300 addEvent(cal_box, 'click', cancelEventPropagation);
303 var cal_nav = quickElement('div', cal_box);
304 var cal_nav_prev = quickElement('a', cal_nav, '<', 'href', '#');
305 cal_nav_prev.className = 'calendarnav-previous';
306 addEvent(cal_nav_prev, 'click', function(e) {
308 DateTimeShortcuts.drawPrev(num);
311 var cal_nav_next = quickElement('a', cal_nav, '>', 'href', '#');
312 cal_nav_next.className = 'calendarnav-next';
313 addEvent(cal_nav_next, 'click', function(e) {
315 DateTimeShortcuts.drawNext(num);
319 var cal_main = quickElement('div', cal_box, '', 'id', DateTimeShortcuts.calendarDivName2 + num);
320 cal_main.className = 'calendar';
321 DateTimeShortcuts.calendars[num] = new Calendar(DateTimeShortcuts.calendarDivName2 + num, DateTimeShortcuts.handleCalendarCallback(num));
322 DateTimeShortcuts.calendars[num].drawCurrent();
324 // calendar shortcuts
325 var shortcuts = quickElement('div', cal_box);
326 shortcuts.className = 'calendar-shortcuts';
327 var day_link = quickElement('a', shortcuts, gettext('Yesterday'), 'href', '#');
328 addEvent(day_link, 'click', function(e) {
330 DateTimeShortcuts.handleCalendarQuickLink(num, -1);
332 shortcuts.appendChild(document.createTextNode('\u00A0|\u00A0'));
333 day_link = quickElement('a', shortcuts, gettext('Today'), 'href', '#');
334 addEvent(day_link, 'click', function(e) {
336 DateTimeShortcuts.handleCalendarQuickLink(num, 0);
338 shortcuts.appendChild(document.createTextNode('\u00A0|\u00A0'));
339 day_link = quickElement('a', shortcuts, gettext('Tomorrow'), 'href', '#');
340 addEvent(day_link, 'click', function(e) {
342 DateTimeShortcuts.handleCalendarQuickLink(num, +1);
346 var cancel_p = quickElement('p', cal_box);
347 cancel_p.className = 'calendar-cancel';
348 var cancel_link = quickElement('a', cancel_p, gettext('Cancel'), 'href', '#');
349 addEvent(cancel_link, 'click', function(e) {
351 DateTimeShortcuts.dismissCalendar(num);
353 django.jQuery(document).bind('keyup', function(event) {
354 if (event.which === 27) {
355 // ESC key closes popup
356 DateTimeShortcuts.dismissCalendar(num);
357 event.preventDefault();
361 openCalendar: function(num) {
362 var cal_box = document.getElementById(DateTimeShortcuts.calendarDivName1 + num);
363 var cal_link = document.getElementById(DateTimeShortcuts.calendarLinkName + num);
364 var inp = DateTimeShortcuts.calendarInputs[num];
366 // Determine if the current value in the input has a valid date.
367 // If so, draw the calendar with that date's year and month.
369 var format = get_format('DATE_INPUT_FORMATS')[0];
370 var selected = inp.value.strptime(format);
371 var year = selected.getUTCFullYear();
372 var month = selected.getUTCMonth() + 1;
374 if (re.test(year.toString()) && month >= 1 && month <= 12) {
375 DateTimeShortcuts.calendars[num].drawDate(month, year, selected);
379 // Recalculate the clockbox position
380 // is it left-to-right or right-to-left layout ?
381 if (getStyle(document.body, 'direction') !== 'rtl') {
382 cal_box.style.left = findPosX(cal_link) + 17 + 'px';
385 // since style's width is in em, it'd be tough to calculate
386 // px value of it. let's use an estimated px for now
387 // TODO: IE returns wrong value for findPosX when in rtl mode
388 // (it returns as it was left aligned), needs to be fixed.
389 cal_box.style.left = findPosX(cal_link) - 180 + 'px';
391 cal_box.style.top = Math.max(0, findPosY(cal_link) - 75) + 'px';
393 cal_box.style.display = 'block';
394 addEvent(document, 'click', DateTimeShortcuts.dismissCalendarFunc[num]);
396 dismissCalendar: function(num) {
397 document.getElementById(DateTimeShortcuts.calendarDivName1 + num).style.display = 'none';
398 removeEvent(document, 'click', DateTimeShortcuts.dismissCalendarFunc[num]);
400 drawPrev: function(num) {
401 DateTimeShortcuts.calendars[num].drawPreviousMonth();
403 drawNext: function(num) {
404 DateTimeShortcuts.calendars[num].drawNextMonth();
406 handleCalendarCallback: function(num) {
407 var format = get_format('DATE_INPUT_FORMATS')[0];
408 // the format needs to be escaped a little
409 format = format.replace('\\', '\\\\');
410 format = format.replace('\r', '\\r');
411 format = format.replace('\n', '\\n');
412 format = format.replace('\t', '\\t');
413 format = format.replace("'", "\\'");
414 return function(y, m, d) {
415 DateTimeShortcuts.calendarInputs[num].value = new Date(y, m - 1, d).strftime(format);
416 DateTimeShortcuts.calendarInputs[num].focus();
417 document.getElementById(DateTimeShortcuts.calendarDivName1 + num).style.display = 'none';
420 handleCalendarQuickLink: function(num, offset) {
421 var d = DateTimeShortcuts.now();
422 d.setDate(d.getDate() + offset);
423 DateTimeShortcuts.calendarInputs[num].value = d.strftime(get_format('DATE_INPUT_FORMATS')[0]);
424 DateTimeShortcuts.calendarInputs[num].focus();
425 DateTimeShortcuts.dismissCalendar(num);
429 addEvent(window, 'load', DateTimeShortcuts.init);
430 window.DateTimeShortcuts = DateTimeShortcuts;