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 = {
|
44 | 44 | } |
45 | 45 | return days; |
46 | 46 | }, |
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 |
48 | 53 | month = parseInt(month); |
49 | 54 | year = parseInt(year); |
50 | 55 | var calDiv = document.getElementById(div_id); |
… |
… |
var CalendarNamespace = {
|
66 | 71 | tableRow = quickElement('tr', tableBody); |
67 | 72 | for (var i = 0; i < startingPos; i++) { |
68 | 73 | var _cell = quickElement('td', tableRow, ' '); |
69 | | _cell.style.backgroundColor = '#f3f3f3'; |
| 74 | _cell.className = "nonday"; |
70 | 75 | } |
71 | 76 | |
72 | 77 | // Draw days of month |
73 | 78 | 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 | } |
74 | 94 | for (var i = startingPos; currentDay <= days; i++) { |
75 | 95 | if (i%7 == 0 && currentDay != 1) { |
76 | 96 | tableRow = quickElement('tr', tableBody); |
77 | 97 | } |
78 | 98 | 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); |
80 | 120 | currentDay++; |
81 | 121 | } |
82 | 122 | |
83 | 123 | // Draw blanks after end of month (optional, but makes for valid code) |
84 | 124 | while (tableRow.childNodes.length < 7) { |
85 | 125 | var _cell = quickElement('td', tableRow, ' '); |
86 | | _cell.style.backgroundColor = '#f3f3f3'; |
| 126 | _cell.className = "nonday"; |
87 | 127 | } |
88 | 128 | |
89 | 129 | calDiv.appendChild(calTable); |
… |
… |
var CalendarNamespace = {
|
91 | 131 | } |
92 | 132 | |
93 | 133 | // Calendar -- A calendar instance |
94 | | function Calendar(div_id, callback) { |
| 134 | function Calendar(div_id, callback, selected, enable_from) { |
95 | 135 | // div_id (string) is the ID of the element in which the calendar will |
96 | 136 | // be displayed |
97 | 137 | // callback (string) is the name of a JavaScript function that will be |
… |
… |
function Calendar(div_id, callback) {
|
102 | 142 | this.today = new Date(); |
103 | 143 | this.currentMonth = this.today.getMonth() + 1; |
104 | 144 | 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; |
105 | 152 | } |
106 | 153 | Calendar.prototype = { |
107 | 154 | 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); |
109 | 156 | }, |
110 | 157 | drawDate: function(month, year) { |
111 | 158 | this.currentMonth = month; |