Ticket #4574: calendar_css_today_and_selected.diff

File calendar_css_today_and_selected.diff, 5.7 KB (added by Silver_Ghost, 13 years ago)

add selected and today CSS classes to calendar cells (previous patch completely does not work)

  • django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js

    diff --git a/django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js b/django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js
    a b  
    247247        format = format.replace('\n', '\\n');
    248248        format = format.replace('\t', '\\t');
    249249        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('');
     250        return ["function(y, m, d) { selected = new Date(y, m-1, d);",
     251                "DateTimeShortcuts.calendarInputs[",
     252                num,
     253                "].value = selected.strftime('",
     254                format,
     255                "');DateTimeShortcuts.calendarInputs[",
     256                num,
     257                "].focus();document.getElementById(DateTimeShortcuts.calendarDivName1+",
     258                num,
     259                ").style.display='none';",
     260                "DateTimeShortcuts.calendars[",
     261                num,
     262                "].selected = selected;}"].join('');
    259263    },
    260264    handleCalendarQuickLink: function(num, offset) {
    261265       var d = new Date();
  • django/contrib/admin/static/admin/js/calendar.js

    diff --git a/django/contrib/admin/static/admin/js/calendar.js b/django/contrib/admin/static/admin/js/calendar.js
    a b  
    4646        return days;
    4747    },
    4848    draw: function(month, year, div_id, callback, selected) { // 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         var is_selected_month = false;
    55         if (typeof selected != 'undefined') {
    56             is_selected_month = (selected.getFullYear() == year && (selected.getMonth()+1) == month);
    57         }
    5849        month = parseInt(month);
    5950        year = parseInt(year);
     51       
    6052        var calDiv = document.getElementById(div_id);
    6153        removeChildren(calDiv);
    6254        var calTable = document.createElement('table');
    63         quickElement('caption', calTable, CalendarNamespace.monthsOfYear[month-1] + ' ' + year);
     55        var caption = CalendarNamespace.monthsOfYear[month-1] + ' ' + year;
     56        quickElement('caption', calTable, caption);
    6457        var tableBody = quickElement('tbody', calTable);
    6558
    6659        // Draw days-of-week header
    6760        var tableRow = quickElement('tr', tableBody);
    6861        for (var i = 0; i < 7; i++) {
    69             quickElement('th', tableRow, CalendarNamespace.daysOfWeek[(i + CalendarNamespace.firstDayOfWeek) % 7]);
     62            var dayNum = (i + CalendarNamespace.firstDayOfWeek) % 7
     63            var dayName = CalendarNamespace.daysOfWeek[dayNum]
     64            quickElement('th', tableRow, dayName);
    7065        }
    7166
    72         var startingPos = new Date(year, month-1, 1 - CalendarNamespace.firstDayOfWeek).getDay();
     67        var startingPos = new Date(year, month-1,
     68                                   1-CalendarNamespace.firstDayOfWeek).getDay();
    7369        var days = CalendarNamespace.getDaysInMonth(month, year);
    7470
    7571        // Draw blanks before first of month
     
    8177        }
    8278
    8379        // Draw days of month
     80        var is_selected_month = false;
     81        if (typeof selected != 'undefined') {
     82            is_selected_month = (selected.getFullYear() == year &&
     83                                 (selected.getMonth() + 1) == month);
     84        }
     85        var today = new Date();
     86        var todayDay = today.getDate();
     87        var todayMonth = today.getMonth() + 1;
     88        var todayYear = today.getFullYear();
    8489        var currentDay = 1;
     90        console.log(selected)
    8591        for (var i = startingPos; currentDay <= days; i++) {
    86             if (i%7 == 0 && currentDay != 1) {
     92            if (i % 7 == 0 && currentDay != 1) {
    8793                tableRow = quickElement('tr', tableBody);
    8894            }
    89             todayClass='';
    90             if ((currentDay==todayDay) && (month==todayMonth) && (year==todayYear)) {
    91                 todayClass='today';
     95
     96            var currentClass = '';
     97            if ((currentDay == todayDay) && (month == todayMonth) && (year == todayYear)) {
     98                currentClass = 'today';
    9299            }
     100
    93101            if (is_selected_month && currentDay == selected.getDate()) {
    94                 if (todayclass != '') todayclass += " ";
    95                 todayclass += "selected"; /* e.g: class="today selected" */
     102                if (currentClass != '') currentClass += " ";
     103                currentClass += "selected"; /* e.g: class="today selected" */
    96104            }
    97             var cell = quickElement('td', tableRow, '', 'class', todayClass);
     105            var cell = quickElement('td', tableRow, '', 'class', currentClass);
    98106
    99107            quickElement('a', cell, currentDay, 'href', 'javascript:void(' + callback + '('+year+','+month+','+currentDay+'));');
    100108            currentDay++;
     
    123131    this.today = new Date();
    124132    this.currentMonth = this.today.getMonth() + 1;
    125133    this.currentYear = this.today.getFullYear();
    126     if (typeof selected == 'undefined') {
    127             this.selected = this.today;
    128         }
    129         else {
    130             this.selected = selected;
    131         }
     134    this.selected = typeof selected == 'undefined' ? this.today : selected;
    132135}
    133136Calendar.prototype = {
    134137    drawCurrent: function() {
Back to Top