Code

Ticket #4574: calendar_css_today_and_selected.diff

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

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

Line 
1diff --git a/django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js b/django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js
2--- a/django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js
3+++ b/django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js
4@@ -247,15 +247,19 @@
5         format = format.replace('\n', '\\n');
6         format = format.replace('\t', '\\t');
7         format = format.replace("'", "\\'");
8-        return ["function(y, m, d) { DateTimeShortcuts.calendarInputs[",
9-               num,
10-               "].value = new Date(y, m-1, d).strftime('",
11-               format,
12-               "');DateTimeShortcuts.calendarInputs[",
13-               num,
14-               "].focus();document.getElementById(DateTimeShortcuts.calendarDivName1+",
15-               num,
16-               ").style.display='none';}"].join('');
17+        return ["function(y, m, d) { selected = new Date(y, m-1, d);",
18+                "DateTimeShortcuts.calendarInputs[",
19+                num,
20+                "].value = selected.strftime('",
21+                format,
22+                "');DateTimeShortcuts.calendarInputs[",
23+                num,
24+                "].focus();document.getElementById(DateTimeShortcuts.calendarDivName1+",
25+                num,
26+                ").style.display='none';",
27+                "DateTimeShortcuts.calendars[",
28+                num,
29+                "].selected = selected;}"].join('');
30     },
31     handleCalendarQuickLink: function(num, offset) {
32        var d = new Date();
33diff --git a/django/contrib/admin/static/admin/js/calendar.js b/django/contrib/admin/static/admin/js/calendar.js
34--- a/django/contrib/admin/static/admin/js/calendar.js
35+++ b/django/contrib/admin/static/admin/js/calendar.js
36@@ -46,30 +46,26 @@
37         return days;
38     },
39     draw: function(month, year, div_id, callback, selected) { // month = 1-12, year = 1-9999
40-        var today = new Date();
41-        var todayDay = today.getDate();
42-        var todayMonth = today.getMonth()+1;
43-        var todayYear = today.getFullYear();
44-        var todayClass = '';
45-        var is_selected_month = false;
46-        if (typeof selected != 'undefined') {
47-            is_selected_month = (selected.getFullYear() == year && (selected.getMonth()+1) == month);
48-        }
49         month = parseInt(month);
50         year = parseInt(year);
51+       
52         var calDiv = document.getElementById(div_id);
53         removeChildren(calDiv);
54         var calTable = document.createElement('table');
55-        quickElement('caption', calTable, CalendarNamespace.monthsOfYear[month-1] + ' ' + year);
56+        var caption = CalendarNamespace.monthsOfYear[month-1] + ' ' + year;
57+        quickElement('caption', calTable, caption);
58         var tableBody = quickElement('tbody', calTable);
59 
60         // Draw days-of-week header
61         var tableRow = quickElement('tr', tableBody);
62         for (var i = 0; i < 7; i++) {
63-            quickElement('th', tableRow, CalendarNamespace.daysOfWeek[(i + CalendarNamespace.firstDayOfWeek) % 7]);
64+            var dayNum = (i + CalendarNamespace.firstDayOfWeek) % 7
65+            var dayName = CalendarNamespace.daysOfWeek[dayNum]
66+            quickElement('th', tableRow, dayName);
67         }
68 
69-        var startingPos = new Date(year, month-1, 1 - CalendarNamespace.firstDayOfWeek).getDay();
70+        var startingPos = new Date(year, month-1,
71+                                   1-CalendarNamespace.firstDayOfWeek).getDay();
72         var days = CalendarNamespace.getDaysInMonth(month, year);
73 
74         // Draw blanks before first of month
75@@ -81,20 +77,32 @@
76         }
77 
78         // Draw days of month
79+        var is_selected_month = false;
80+        if (typeof selected != 'undefined') {
81+            is_selected_month = (selected.getFullYear() == year &&
82+                                 (selected.getMonth() + 1) == month);
83+        }
84+        var today = new Date();
85+        var todayDay = today.getDate();
86+        var todayMonth = today.getMonth() + 1;
87+        var todayYear = today.getFullYear();
88         var currentDay = 1;
89+        console.log(selected)
90         for (var i = startingPos; currentDay <= days; i++) {
91-            if (i%7 == 0 && currentDay != 1) {
92+            if (i % 7 == 0 && currentDay != 1) {
93                 tableRow = quickElement('tr', tableBody);
94             }
95-            todayClass='';
96-            if ((currentDay==todayDay) && (month==todayMonth) && (year==todayYear)) {
97-                todayClass='today';
98+
99+            var currentClass = '';
100+            if ((currentDay == todayDay) && (month == todayMonth) && (year == todayYear)) {
101+                currentClass = 'today';
102             }
103+
104             if (is_selected_month && currentDay == selected.getDate()) {
105-                if (todayclass != '') todayclass += " ";
106-                todayclass += "selected"; /* e.g: class="today selected" */
107+                if (currentClass != '') currentClass += " ";
108+                currentClass += "selected"; /* e.g: class="today selected" */
109             }
110-            var cell = quickElement('td', tableRow, '', 'class', todayClass);
111+            var cell = quickElement('td', tableRow, '', 'class', currentClass);
112 
113             quickElement('a', cell, currentDay, 'href', 'javascript:void(' + callback + '('+year+','+month+','+currentDay+'));');
114             currentDay++;
115@@ -123,12 +131,7 @@
116     this.today = new Date();
117     this.currentMonth = this.today.getMonth() + 1;
118     this.currentYear = this.today.getFullYear();
119-    if (typeof selected == 'undefined') {
120-            this.selected = this.today;
121-        }
122-        else {
123-            this.selected = selected;
124-        }
125+    this.selected = typeof selected == 'undefined' ? this.today : selected;
126 }
127 Calendar.prototype = {
128     drawCurrent: function() {