~ubuntu-branches/ubuntu/quantal/python-django/quantal-security

« back to all changes in this revision

Viewing changes to django/contrib/admin/media/js/admin/DateTimeShortcuts.js

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2010-05-21 07:52:55 UTC
  • mfrom: (1.3.6 upstream)
  • mto: This revision was merged to the branch mainline in revision 28.
  • Revision ID: james.westby@ubuntu.com-20100521075255-ii78v1dyfmyu3uzx
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
    calendarLinkName: 'calendarlink',// name of the link that is used to toggle
12
12
    clockDivName: 'clockbox',        // name of clock <div> that gets toggled
13
13
    clockLinkName: 'clocklink',      // name of the link that is used to toggle
 
14
    shortCutsClass: 'datetimeshortcuts', // class of the clock and cal shortcuts
14
15
    admin_media_prefix: '',
15
16
    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
 
            }
 
17
        // Get admin_media_prefix by grabbing it off the window object. It's
 
18
        // set in the admin/base.html template, so if it's not there, someone's
 
19
        // overridden the template. In that case, we'll set a clearly-invalid
 
20
        // value in the hopes that someone will examine HTTP requests and see it.
 
21
        if (window.__admin_media_prefix__ != undefined) {
 
22
            DateTimeShortcuts.admin_media_prefix = window.__admin_media_prefix__;
 
23
        } else {
 
24
            DateTimeShortcuts.admin_media_prefix = '/missing-admin-media-prefix/';
25
25
        }
26
26
 
27
27
        var inputs = document.getElementsByTagName('input');
42
42
 
43
43
        // Shortcut links (clock icon and "Now" link)
44
44
        var shortcuts_span = document.createElement('span');
 
45
        shortcuts_span.className = DateTimeShortcuts.shortCutsClass;
45
46
        inp.parentNode.insertBefore(shortcuts_span, inp.nextSibling);
46
47
        var now_link = document.createElement('a');
47
 
        now_link.setAttribute('href', "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", new Date().getHourMinuteSecond());");
 
48
        now_link.setAttribute('href', "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", new Date().strftime('" + get_format('TIME_INPUT_FORMATS')[0] + "'));");
48
49
        now_link.appendChild(document.createTextNode(gettext('Now')));
49
50
        var clock_link = document.createElement('a');
50
51
        clock_link.setAttribute('href', 'javascript:DateTimeShortcuts.openClock(' + num + ');');
80
81
        quickElement('h2', clock_box, gettext('Choose a time'));
81
82
        time_list = quickElement('ul', clock_box, '');
82
83
        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');")
 
84
        time_format = get_format('TIME_INPUT_FORMATS')[0];
 
85
        quickElement("a", quickElement("li", time_list, ""), gettext("Now"), "href", "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", new Date().strftime('" + time_format + "'));");
 
86
        quickElement("a", quickElement("li", time_list, ""), gettext("Midnight"), "href", "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", new Date(1970,1,1,0,0,0,0).strftime('" + time_format + "'));");
 
87
        quickElement("a", quickElement("li", time_list, ""), gettext("6 a.m."), "href", "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", new Date(1970,1,1,6,0,0,0).strftime('" + time_format + "'));");
 
88
        quickElement("a", quickElement("li", time_list, ""), gettext("Noon"), "href", "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", new Date(1970,1,1,12,0,0,0).strftime('" + time_format + "'));");
87
89
 
88
90
        cancel_p = quickElement('p', clock_box, '');
89
91
        cancel_p.className = 'calendar-cancel';
117
119
    },
118
120
    handleClockQuicklink: function(num, val) {
119
121
       DateTimeShortcuts.clockInputs[num].value = val;
 
122
       DateTimeShortcuts.clockInputs[num].focus();
120
123
       DateTimeShortcuts.dismissClock(num);
121
124
    },
122
125
    // Add calendar widget to a given field.
127
130
 
128
131
        // Shortcut links (calendar icon and "Today" link)
129
132
        var shortcuts_span = document.createElement('span');
 
133
        shortcuts_span.className = DateTimeShortcuts.shortCutsClass;
130
134
        inp.parentNode.insertBefore(shortcuts_span, inp.nextSibling);
131
135
        var today_link = document.createElement('a');
132
136
        today_link.setAttribute('href', 'javascript:DateTimeShortcuts.handleCalendarQuickLink(' + num + ', 0);');
195
199
    openCalendar: function(num) {
196
200
        var cal_box = document.getElementById(DateTimeShortcuts.calendarDivName1+num)
197
201
        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
 
    
 
202
        var inp = DateTimeShortcuts.calendarInputs[num];
 
203
 
 
204
        // Determine if the current value in the input has a valid date.
 
205
        // If so, draw the calendar with that date's year and month.
 
206
        if (inp.value) {
 
207
            var date_parts = inp.value.split('-');
 
208
            var year = date_parts[0];
 
209
            var month = parseFloat(date_parts[1]);
 
210
            if (year.match(/\d\d\d\d/) && month >= 1 && month <= 12) {
 
211
                DateTimeShortcuts.calendars[num].drawDate(month, year);
 
212
            }
 
213
        }
 
214
 
212
215
        // Recalculate the clockbox position
213
216
        // is it left-to-right or right-to-left layout ?
214
217
        if (getStyle(document.body,'direction')!='rtl') {
237
240
        DateTimeShortcuts.calendars[num].drawNextMonth();
238
241
    },
239
242
    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';}";
 
243
        format = get_format('DATE_INPUT_FORMATS')[0];
 
244
        // the format needs to be escaped a little
 
245
        format = format.replace('\\', '\\\\');
 
246
        format = format.replace('\r', '\\r');
 
247
        format = format.replace('\n', '\\n');
 
248
        format = format.replace('\t', '\\t');
 
249
        format = format.replace("'", "\\'");
 
250
        return ["function(y, m, d) { DateTimeShortcuts.calendarInputs[",
 
251
               num,
 
252
               "].value = new Date(y, m-1, d).strftime('",
 
253
               format,
 
254
               "');DateTimeShortcuts.calendarInputs[",
 
255
               num,
 
256
               "].focus();document.getElementById(DateTimeShortcuts.calendarDivName1+",
 
257
               num,
 
258
               ").style.display='none';}"].join('');
241
259
    },
242
260
    handleCalendarQuickLink: function(num, offset) {
243
261
       var d = new Date();
244
262
       d.setDate(d.getDate() + offset)
245
 
       DateTimeShortcuts.calendarInputs[num].value = d.getISODate();
 
263
       DateTimeShortcuts.calendarInputs[num].value = d.strftime(get_format('DATE_INPUT_FORMATS')[0]);
 
264
       DateTimeShortcuts.calendarInputs[num].focus();
246
265
       DateTimeShortcuts.dismissCalendar(num);
247
266
    },
248
267
    cancelEventPropagation: function(e) {