Ticket #6489: 0017-Add-selected-and-enabled_from-for-JS-calendar.patch

File 0017-Add-selected-and-enabled_from-for-JS-calendar.patch, 5.1 KB (added by Bastian Kleineidam <calvin@…>, 17 years ago)
  • django/contrib/admin/media/js/calendar.js

    From eda402c6331925093e916bfacb49e4e0b107ccf4 Mon Sep 17 00:00:00 2001
    From: Bastian Kleineidam <calvin@debian.org>
    Date: Fri, 25 Jan 2008 21:13:11 +0100
    Subject: Add selected and enabled_from for JS calendar
    
    Add two parameters to the JS calendar class: highlighting a
    selected date, and disabling dates before a specified one.
    
    Signed-off-by: Bastian Kleineidam <calvin@debian.org>
    
    diff --git a/django/contrib/admin/media/js/calendar.js b/django/contrib/admin/media/js/calendar.js
    index 9035176..0543ba6 100644
    a b var CalendarNamespace = {  
    4444        }
    4545        return days;
    4646    },
    47     draw: function(month, year, div_id, callback) { // month = 1-12, year = 1-9999
     47    draw: function(month, year, div_id, callback, selected, enable_from) {
     48        // month = 1-12, year = 1-9999
     49        // div_id - the calendar div id
     50        // callback - onclick callback
     51        // selected - highlight given date
     52        // enable_from - only display links from given date
    4853        month = parseInt(month);
    4954        year = parseInt(year);
    5055        var calDiv = document.getElementById(div_id);
    var CalendarNamespace = {  
    6671        tableRow = quickElement('tr', tableBody);
    6772        for (var i = 0; i < startingPos; i++) {
    6873            var _cell = quickElement('td', tableRow, ' ');
    69             _cell.style.backgroundColor = '#f3f3f3';
     74            _cell.className = "nonday";
    7075        }
    7176
    7277        // Draw days of month
    7378        var currentDay = 1;
     79        var today = new Date();
     80        var is_current_month = (today.getFullYear() == year && (today.getMonth()+1) == month);
     81        var is_selected_month = false;
     82        if (typeof selected != 'undefined') {
     83            is_selected_month = (selected.getFullYear() == year && (selected.getMonth()+1) == month);
     84        }
     85        var is_enabled_month = true;
     86        var is_enabled_day = true;
     87        var has_enabled_from = false;
     88        if (typeof enable_from != 'undefined') {
     89            is_enabled_month = (enable_from.getFullYear() < year ||
     90               (enable_from.getFullYear() == year && (enable_from.getMonth()+1) < month));
     91            is_enabled_day = (enable_from.getFullYear() == year && (enable_from.getMonth()+1) == month);
     92            has_enabled_from = true;
     93        }
    7494        for (var i = startingPos; currentDay <= days; i++) {
    7595            if (i%7 == 0 && currentDay != 1) {
    7696                tableRow = quickElement('tr', tableBody);
    7797            }
    7898            var cell = quickElement('td', tableRow, '');
    79             quickElement('a', cell, currentDay, 'href', 'javascript:void(' + callback + '('+year+','+month+','+currentDay+'));');
     99            var elemclass = '';
     100            if (is_current_month && currentDay == today.getDate()) {
     101                if (elemclass != '') elemclass += " ";
     102                elemclass += "today";
     103            }
     104            if (is_selected_month && currentDay == selected.getDate()) {
     105                if (elemclass != '') elemclass += " ";
     106                elemclass += "selected";
     107            }
     108            var href = "#";
     109            if (!has_enabled_from || is_enabled_month || (is_enabled_day && currentDay >= enable_from.getDate())) {
     110                href = 'javascript:void(' + callback + '('+year+','+month+','+currentDay+'));';
     111            }
     112            else {
     113                if (elemclass != '') elemclass += " ";
     114                elemclass += "disable";
     115            }
     116            if (elemclass != '') {
     117                cell.className = elemclass;
     118            }
     119            quickElement('a', cell, currentDay, 'href', href);
    80120            currentDay++;
    81121        }
    82122
    83123        // Draw blanks after end of month (optional, but makes for valid code)
    84124        while (tableRow.childNodes.length < 7) {
    85125            var _cell = quickElement('td', tableRow, ' ');
    86             _cell.style.backgroundColor = '#f3f3f3';
     126            _cell.className = "nonday";
    87127        }
    88128
    89129        calDiv.appendChild(calTable);
    var CalendarNamespace = {  
    91131}
    92132
    93133// Calendar -- A calendar instance
    94 function Calendar(div_id, callback) {
     134function Calendar(div_id, callback, selected, enable_from) {
    95135    // div_id (string) is the ID of the element in which the calendar will
    96136    //     be displayed
    97137    // callback (string) is the name of a JavaScript function that will be
    function Calendar(div_id, callback) {  
    102142    this.today = new Date();
    103143    this.currentMonth = this.today.getMonth() + 1;
    104144    this.currentYear = this.today.getFullYear();
     145    if (typeof selected == 'undefined') {
     146        this.selected = this.today;
     147    }
     148    else {
     149        this.selected = selected;
     150    }
     151    this.enable_from = enable_from;
    105152}
    106153Calendar.prototype = {
    107154    drawCurrent: function() {
    108         CalendarNamespace.draw(this.currentMonth, this.currentYear, this.div_id, this.callback);
     155        CalendarNamespace.draw(this.currentMonth, this.currentYear, this.div_id, this.callback, this.selected, this.enable_from);
    109156    },
    110157    drawDate: function(month, year) {
    111158        this.currentMonth = month;
Back to Top