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