From afc06b56256f78ab832ff8066ac6f34b7443de22 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Fri, 25 Aug 2017 00:07:20 +0200 Subject: [PATCH] Fixed #28536 -- Removed old JS cross-browser utilities --- .../admin/static/admin/js/SelectFilter2.js | 40 ++++++++++++------ .../admin/js/admin/DateTimeShortcuts.js | 42 +++++++++---------- .../contrib/admin/static/admin/js/calendar.js | 4 +- django/contrib/admin/static/admin/js/core.js | 38 ----------------- 4 files changed, 51 insertions(+), 73 deletions(-) diff --git a/django/contrib/admin/static/admin/js/SelectFilter2.js b/django/contrib/admin/static/admin/js/SelectFilter2.js index 0f9a188d4b..52471d9472 100644 --- a/django/contrib/admin/static/admin/js/SelectFilter2.js +++ b/django/contrib/admin/static/admin/js/SelectFilter2.js @@ -1,4 +1,4 @@ -/*global SelectBox, addEvent, gettext, interpolate, quickElement, SelectFilter*/ +/*global SelectBox, gettext, interpolate, quickElement, SelectFilter*/ /* SelectFilter2 - Turns a multiple-select box into a filter interface. @@ -118,19 +118,33 @@ Requires jQuery, core.js, and SelectBox.js. } e.preventDefault(); }; - addEvent(choose_all, 'click', function(e) { move_selection(e, this, SelectBox.move_all, field_id + '_from', field_id + '_to'); }); - addEvent(add_link, 'click', function(e) { move_selection(e, this, SelectBox.move, field_id + '_from', field_id + '_to'); }); - addEvent(remove_link, 'click', function(e) { move_selection(e, this, SelectBox.move, field_id + '_to', field_id + '_from'); }); - addEvent(clear_all, 'click', function(e) { move_selection(e, this, SelectBox.move_all, field_id + '_to', field_id + '_from'); }); - addEvent(filter_input, 'keypress', function(e) { SelectFilter.filter_key_press(e, field_id); }); - addEvent(filter_input, 'keyup', function(e) { SelectFilter.filter_key_up(e, field_id); }); - addEvent(filter_input, 'keydown', function(e) { SelectFilter.filter_key_down(e, field_id); }); - addEvent(selector_div, 'change', function(e) { + choose_all.addEventListener('click', function(e) { + move_selection(e, this, SelectBox.move_all, field_id + '_from', field_id + '_to'); + }); + add_link.addEventListener('click', function(e) { + move_selection(e, this, SelectBox.move, field_id + '_from', field_id + '_to'); + }); + remove_link.addEventListener('click', function(e) { + move_selection(e, this, SelectBox.move, field_id + '_to', field_id + '_from'); + }); + clear_all.addEventListener('click', function(e) { + move_selection(e, this, SelectBox.move_all, field_id + '_to', field_id + '_from'); + }); + filter_input.addEventListener('keypress', function(e) { + SelectFilter.filter_key_press(e, field_id); + }); + filter_input.addEventListener('keyup', function(e) { + SelectFilter.filter_key_up(e, field_id); + }); + filter_input.addEventListener('keydown', function(e) { + SelectFilter.filter_key_down(e, field_id); + }); + selector_div.addEventListener('change', function(e) { if (e.target.tagName === 'SELECT') { SelectFilter.refresh_icons(field_id); } }); - addEvent(selector_div, 'dblclick', function(e) { + selector_div.addEventListener('dblclick', function(e) { if (e.target.tagName === 'OPTION') { if (e.target.closest('select').id === field_id + '_to') { SelectBox.move(field_id + '_to', field_id + '_from'); @@ -140,7 +154,9 @@ Requires jQuery, core.js, and SelectBox.js. SelectFilter.refresh_icons(field_id); } }); - addEvent(findForm(from_box), 'submit', function() { SelectBox.select_all(field_id + '_to'); }); + findForm(from_box).addEventListener('submit', function() { + SelectBox.select_all(field_id + '_to'); + }); SelectBox.init(field_id + '_from'); SelectBox.init(field_id + '_to'); // Move selected from_box options to to_box @@ -225,7 +241,7 @@ Requires jQuery, core.js, and SelectBox.js. } }; - addEvent(window, 'load', function(e) { + window.addEventListener('load', function(e) { $('select.selectfilter, select.selectfilterstacked').each(function() { var $el = $(this), data = $el.data(); diff --git a/django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js b/django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js index af6502e49b..0a57c2279b 100644 --- a/django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js +++ b/django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js @@ -1,4 +1,4 @@ -/*global addEvent, Calendar, cancelEventPropagation, findPosX, findPosY, getStyle, get_format, gettext, interpolate, ngettext, quickElement, removeEvent*/ +/*global Calendar, findPosX, findPosY, getStyle, get_format, gettext, interpolate, ngettext, quickElement*/ // Inserts shortcut buttons after all of the following: // // @@ -116,14 +116,14 @@ var now_link = document.createElement('a'); now_link.setAttribute('href', "#"); now_link.appendChild(document.createTextNode(gettext('Now'))); - addEvent(now_link, 'click', function(e) { + now_link.addEventListener('click', function(e) { e.preventDefault(); DateTimeShortcuts.handleClockQuicklink(num, -1); }); var clock_link = document.createElement('a'); clock_link.setAttribute('href', '#'); clock_link.id = DateTimeShortcuts.clockLinkName + num; - addEvent(clock_link, 'click', function(e) { + clock_link.addEventListener('click', function(e) { e.preventDefault(); // avoid triggering the document click handler to dismiss the clock e.stopPropagation(); @@ -161,7 +161,7 @@ clock_box.className = 'clockbox module'; clock_box.setAttribute('id', DateTimeShortcuts.clockDivName + num); document.body.appendChild(clock_box); - addEvent(clock_box, 'click', cancelEventPropagation); + clock_box.addEventListener('click', function(e) { e.stopPropagation(); }); quickElement('h2', clock_box, gettext('Choose a time')); var time_list = quickElement('ul', clock_box); @@ -172,7 +172,7 @@ var name = typeof DateTimeShortcuts.clockHours[inp.name] === 'undefined' ? 'default_' : inp.name; DateTimeShortcuts.clockHours[name].forEach(function(element) { var time_link = quickElement('a', quickElement('li', time_list), gettext(element[0]), 'href', '#'); - addEvent(time_link, 'click', function(e) { + time_link.addEventListener('click', function(e) { e.preventDefault(); DateTimeShortcuts.handleClockQuicklink(num, element[1]); }); @@ -181,12 +181,12 @@ var cancel_p = quickElement('p', clock_box); cancel_p.className = 'calendar-cancel'; var cancel_link = quickElement('a', cancel_p, gettext('Cancel'), 'href', '#'); - addEvent(cancel_link, 'click', function(e) { + cancel_link.addEventListener('click', function(e) { e.preventDefault(); DateTimeShortcuts.dismissClock(num); }); - django.jQuery(document).bind('keyup', function(event) { + document.addEventListener('keyup', function(event) { if (event.which === 27) { // ESC key closes popup DateTimeShortcuts.dismissClock(num); @@ -214,11 +214,11 @@ // Show the clock box clock_box.style.display = 'block'; - addEvent(document, 'click', DateTimeShortcuts.dismissClockFunc[num]); + document.addEventListener('click', DateTimeShortcuts.dismissClockFunc[num]); }, dismissClock: function(num) { document.getElementById(DateTimeShortcuts.clockDivName + num).style.display = 'none'; - removeEvent(document, 'click', DateTimeShortcuts.dismissClockFunc[num]); + document.removeEventListener('click', DateTimeShortcuts.dismissClockFunc[num]); }, handleClockQuicklink: function(num, val) { var d; @@ -246,14 +246,14 @@ var today_link = document.createElement('a'); today_link.setAttribute('href', '#'); today_link.appendChild(document.createTextNode(gettext('Today'))); - addEvent(today_link, 'click', function(e) { + today_link.addEventListener('click', function(e) { e.preventDefault(); DateTimeShortcuts.handleCalendarQuickLink(num, 0); }); var cal_link = document.createElement('a'); cal_link.setAttribute('href', '#'); cal_link.id = DateTimeShortcuts.calendarLinkName + num; - addEvent(cal_link, 'click', function(e) { + cal_link.addEventListener('click', function(e) { e.preventDefault(); // avoid triggering the document click handler to dismiss the calendar e.stopPropagation(); @@ -292,20 +292,20 @@ cal_box.className = 'calendarbox module'; cal_box.setAttribute('id', DateTimeShortcuts.calendarDivName1 + num); document.body.appendChild(cal_box); - addEvent(cal_box, 'click', cancelEventPropagation); + cal_box.addEventListener('click', function(e) { e.stopPropagation(); }); // next-prev links var cal_nav = quickElement('div', cal_box); var cal_nav_prev = quickElement('a', cal_nav, '<', 'href', '#'); cal_nav_prev.className = 'calendarnav-previous'; - addEvent(cal_nav_prev, 'click', function(e) { + cal_nav_prev.addEventListener('click', function(e) { e.preventDefault(); DateTimeShortcuts.drawPrev(num); }); var cal_nav_next = quickElement('a', cal_nav, '>', 'href', '#'); cal_nav_next.className = 'calendarnav-next'; - addEvent(cal_nav_next, 'click', function(e) { + cal_nav_next.addEventListener('click', function(e) { e.preventDefault(); DateTimeShortcuts.drawNext(num); }); @@ -320,19 +320,19 @@ var shortcuts = quickElement('div', cal_box); shortcuts.className = 'calendar-shortcuts'; var day_link = quickElement('a', shortcuts, gettext('Yesterday'), 'href', '#'); - addEvent(day_link, 'click', function(e) { + day_link.addEventListener('click', function(e) { e.preventDefault(); DateTimeShortcuts.handleCalendarQuickLink(num, -1); }); shortcuts.appendChild(document.createTextNode('\u00A0|\u00A0')); day_link = quickElement('a', shortcuts, gettext('Today'), 'href', '#'); - addEvent(day_link, 'click', function(e) { + day_link.addEventListener('click', function(e) { e.preventDefault(); DateTimeShortcuts.handleCalendarQuickLink(num, 0); }); shortcuts.appendChild(document.createTextNode('\u00A0|\u00A0')); day_link = quickElement('a', shortcuts, gettext('Tomorrow'), 'href', '#'); - addEvent(day_link, 'click', function(e) { + day_link.addEventListener('click', function(e) { e.preventDefault(); DateTimeShortcuts.handleCalendarQuickLink(num, +1); }); @@ -341,7 +341,7 @@ var cancel_p = quickElement('p', cal_box); cancel_p.className = 'calendar-cancel'; var cancel_link = quickElement('a', cancel_p, gettext('Cancel'), 'href', '#'); - addEvent(cancel_link, 'click', function(e) { + cancel_link.addEventListener('click', function(e) { e.preventDefault(); DateTimeShortcuts.dismissCalendar(num); }); @@ -386,11 +386,11 @@ cal_box.style.top = Math.max(0, findPosY(cal_link) - 75) + 'px'; cal_box.style.display = 'block'; - addEvent(document, 'click', DateTimeShortcuts.dismissCalendarFunc[num]); + document.addEventListener('click', DateTimeShortcuts.dismissCalendarFunc[num]); }, dismissCalendar: function(num) { document.getElementById(DateTimeShortcuts.calendarDivName1 + num).style.display = 'none'; - removeEvent(document, 'click', DateTimeShortcuts.dismissCalendarFunc[num]); + document.removeEventListener('click', DateTimeShortcuts.dismissCalendarFunc[num]); }, drawPrev: function(num) { DateTimeShortcuts.calendars[num].drawPreviousMonth(); @@ -421,6 +421,6 @@ } }; - addEvent(window, 'load', DateTimeShortcuts.init); + window.addEventListener('load', DateTimeShortcuts.init); window.DateTimeShortcuts = DateTimeShortcuts; })(); diff --git a/django/contrib/admin/static/admin/js/calendar.js b/django/contrib/admin/static/admin/js/calendar.js index 36480994cc..a4c047aa72 100644 --- a/django/contrib/admin/static/admin/js/calendar.js +++ b/django/contrib/admin/static/admin/js/calendar.js @@ -1,4 +1,4 @@ -/*global gettext, pgettext, get_format, quickElement, removeChildren, addEvent*/ +/*global gettext, pgettext, get_format, quickElement, removeChildren*/ /* calendar.js - Calendar functions by Adrian Holovaty depends on core.js for utility functions like removeChildren or quickElement @@ -130,7 +130,7 @@ depends on core.js for utility functions like removeChildren or quickElement var cell = quickElement('td', tableRow, '', 'class', todayClass); var link = quickElement('a', cell, currentDay, 'href', '#'); - addEvent(link, 'click', calendarMonth(year, month)); + link.addEventListener('click', calendarMonth(year, month)); currentDay++; } diff --git a/django/contrib/admin/static/admin/js/core.js b/django/contrib/admin/static/admin/js/core.js index edccdc0217..2989a94c51 100644 --- a/django/contrib/admin/static/admin/js/core.js +++ b/django/contrib/admin/static/admin/js/core.js @@ -4,44 +4,6 @@ var isOpera = (navigator.userAgent.indexOf("Opera") >= 0) && parseFloat(navigator.appVersion); var isIE = ((document.all) && (!isOpera)) && parseFloat(navigator.appVersion.split("MSIE ")[1].split(";")[0]); -// Cross-browser event handlers. -function addEvent(obj, evType, fn) { - 'use strict'; - if (obj.addEventListener) { - obj.addEventListener(evType, fn, false); - return true; - } else if (obj.attachEvent) { - var r = obj.attachEvent("on" + evType, fn); - return r; - } else { - return false; - } -} - -function removeEvent(obj, evType, fn) { - 'use strict'; - if (obj.removeEventListener) { - obj.removeEventListener(evType, fn, false); - return true; - } else if (obj.detachEvent) { - obj.detachEvent("on" + evType, fn); - return true; - } else { - return false; - } -} - -function cancelEventPropagation(e) { - 'use strict'; - if (!e) { - e = window.event; - } - e.cancelBubble = true; - if (e.stopPropagation) { - e.stopPropagation(); - } -} - // quickElement(tagType, parentReference [, textInChildNode, attribute, attributeValue ...]); function quickElement() { 'use strict';