From 3c5a586ce9bef4203f3f62b9ec9f449cfafad3bf Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Tue, 5 May 2020 17:17:12 -0700 Subject: [PATCH] =?UTF-8?q?Fixed=20#31542=20--=20Simplified=20admin=20Java?= =?UTF-8?q?Script=20with=20"for=20=E2=80=A6=20of"=20statements.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/static/admin/js/SelectBox.js | 40 +++++-------------- .../admin/static/admin/js/SelectFilter2.js | 11 +++-- .../admin/js/admin/DateTimeShortcuts.js | 4 +- .../admin/static/admin/js/change_form.js | 3 +- .../contrib/admin/static/admin/js/collapse.js | 10 ++--- .../admin/static/admin/js/collapse.min.js | 4 +- .../contrib/admin/static/admin/js/urlify.js | 16 ++------ 7 files changed, 26 insertions(+), 62 deletions(-) diff --git a/django/contrib/admin/static/admin/js/SelectBox.js b/django/contrib/admin/static/admin/js/SelectBox.js index 9fd93c7a8c..6716e34530 100644 --- a/django/contrib/admin/static/admin/js/SelectBox.js +++ b/django/contrib/admin/static/admin/js/SelectBox.js @@ -6,10 +6,7 @@ const box = document.getElementById(id); SelectBox.cache[id] = []; const cache = SelectBox.cache[id]; - const boxOptions = box.options; - const boxOptionsLength = boxOptions.length; - for (let i = 0, j = boxOptionsLength; i < j; i++) { - const node = boxOptions[i]; + for (const node of box.options) { cache.push({value: node.value, text: node.text, displayed: 1}); } }, @@ -17,9 +14,7 @@ // Repopulate HTML select box from cache const box = document.getElementById(id); box.innerHTML = ''; - const cache = SelectBox.cache[id]; - for (let i = 0, j = cache.length; i < j; i++) { - const node = cache[i]; + for (const node of SelectBox.cache[id]) { if (node.displayed) { const new_option = new Option(node.text, node.value, false, false); // Shows a tooltip when hovering over the option @@ -32,14 +27,10 @@ // Redisplay the HTML select box, displaying only the choices containing ALL // the words in text. (It's an AND search.) const tokens = text.toLowerCase().split(/\s+/); - const cache = SelectBox.cache[id]; - for (let i = 0, j = cache.length; i < j; i++) { - const node = cache[i]; + for (const node of SelectBox.cache[id]) { node.displayed = 1; const node_text = node.text.toLowerCase(); - const numTokens = tokens.length; - for (let k = 0; k < numTokens; k++) { - const token = tokens[k]; + for (const token of tokens) { if (node_text.indexOf(token) === -1) { node.displayed = 0; break; // Once the first token isn't found we're done @@ -51,8 +42,7 @@ delete_from_cache: function(id, value) { let delete_index = null; const cache = SelectBox.cache[id]; - for (let i = 0, j = cache.length; i < j; i++) { - const node = cache[i]; + for (const [i, node] of cache.entries()) { if (node.value === value) { delete_index = i; break; @@ -65,9 +55,7 @@ }, cache_contains: function(id, value) { // Check if an item is contained in the cache - const cache = SelectBox.cache[id]; - for (let i = 0, j = cache.length; i < j; i++) { - const node = cache[i]; + for (const node of SelectBox.cache[id]) { if (node.value === value) { return true; } @@ -76,10 +64,7 @@ }, move: function(from, to) { const from_box = document.getElementById(from); - const boxOptions = from_box.options; - const boxOptionsLength = boxOptions.length; - for (let i = 0, j = boxOptionsLength; i < j; i++) { - const option = boxOptions[i]; + for (const option of from_box.options) { const option_value = option.value; if (option.selected && SelectBox.cache_contains(from, option_value)) { SelectBox.add_to_cache(to, {value: option_value, text: option.text, displayed: 1}); @@ -91,10 +76,7 @@ }, move_all: function(from, to) { const from_box = document.getElementById(from); - const boxOptions = from_box.options; - const boxOptionsLength = boxOptions.length; - for (let i = 0, j = boxOptionsLength; i < j; i++) { - const option = boxOptions[i]; + for (const option of from_box.options) { const option_value = option.value; if (SelectBox.cache_contains(from, option_value)) { SelectBox.add_to_cache(to, {value: option_value, text: option.text, displayed: 1}); @@ -119,10 +101,8 @@ }, select_all: function(id) { const box = document.getElementById(id); - const boxOptions = box.options; - const boxOptionsLength = boxOptions.length; - for (let i = 0; i < boxOptionsLength; i++) { - boxOptions[i].selected = 'selected'; + for (const option of box.options) { + option.selected = 'selected'; } } }; diff --git a/django/contrib/admin/static/admin/js/SelectFilter2.js b/django/contrib/admin/static/admin/js/SelectFilter2.js index 83ddd55a9e..6c709a08c2 100644 --- a/django/contrib/admin/static/admin/js/SelectFilter2.js +++ b/django/contrib/admin/static/admin/js/SelectFilter2.js @@ -16,16 +16,15 @@ Requires core.js and SelectBox.js. from_box.id += '_from'; // change its ID from_box.className = 'filtered'; - const ps = from_box.parentNode.getElementsByTagName('p'); - for (let i = 0; i < ps.length; i++) { - if (ps[i].classList.contains("info")) { + for (const p of from_box.parentNode.getElementsByTagName('p')) { + if (p.classList.contains("info")) { // Remove

, because it just gets in the way. - from_box.parentNode.removeChild(ps[i]); - } else if (ps[i].classList.contains("help")) { + from_box.parentNode.removeChild(p); + } else if (p.classList.contains("help")) { // Move help text up to the top so it isn't below the select // boxes or wrapped off on the side to the right of the add // button: - from_box.parentNode.insertBefore(ps[i], from_box.parentNode.firstChild); + from_box.parentNode.insertBefore(p, from_box.parentNode.firstChild); } } diff --git a/django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js b/django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js index 07d0d0253a..28de479763 100644 --- a/django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js +++ b/django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js @@ -35,9 +35,7 @@ DateTimeShortcuts.timezoneOffset = localOffset - serverOffset; } - const inputs = document.getElementsByTagName('input'); - for (let i = 0; i < inputs.length; i++) { - const inp = inputs[i]; + for (const inp of document.getElementsByTagName('input')) { if (inp.type === 'text' && inp.classList.contains('vTimeField')) { DateTimeShortcuts.addClock(inp); DateTimeShortcuts.addTimezoneWarning(inp); diff --git a/django/contrib/admin/static/admin/js/change_form.js b/django/contrib/admin/static/admin/js/change_form.js index cb50120b1b..96a4c62ef4 100644 --- a/django/contrib/admin/static/admin/js/change_form.js +++ b/django/contrib/admin/static/admin/js/change_form.js @@ -4,8 +4,7 @@ const modelName = document.getElementById('django-admin-form-add-constants').dataset.modelName; if (modelName) { const form = document.getElementById(modelName + '_form'); - for (let i = 0; i < form.elements.length; i++) { - const element = form.elements[i]; + for (const element of form.elements) { // HTMLElement.offsetParent returns null when the element is not // rendered. if (inputTags.includes(element.tagName) && !element.disabled && element.offsetParent) { diff --git a/django/contrib/admin/static/admin/js/collapse.js b/django/contrib/admin/static/admin/js/collapse.js index e0e2ba9f87..c6c7b0f68a 100644 --- a/django/contrib/admin/static/admin/js/collapse.js +++ b/django/contrib/admin/static/admin/js/collapse.js @@ -4,8 +4,7 @@ window.addEventListener('load', function() { // Add anchor tag for Show/Hide link const fieldsets = document.querySelectorAll('fieldset.collapse'); - for (let i = 0; i < fieldsets.length; i++) { - const elem = fieldsets[i]; + for (const [i, elem] of fieldsets.entries()) { // Don't hide if fields in this fieldset have errors if (elem.querySelectorAll('div.errors, ul.errorlist').length === 0) { elem.classList.add('collapsed'); @@ -37,9 +36,8 @@ } } }; - const inlineDivs = document.querySelectorAll('fieldset.module'); - for (let i = 0; i < inlineDivs.length; i++) { - inlineDivs[i].addEventListener('click', toggleFunc); - } + document.querySelectorAll('fieldset.module').forEach(function(el) { + el.addEventListener('click', toggleFunc); + }); }); } diff --git a/django/contrib/admin/static/admin/js/collapse.min.js b/django/contrib/admin/static/admin/js/collapse.min.js index cd78b6984c..06201c597f 100644 --- a/django/contrib/admin/static/admin/js/collapse.min.js +++ b/django/contrib/admin/static/admin/js/collapse.min.js @@ -1,2 +1,2 @@ -'use strict';window.addEventListener("load",function(){var d=document.querySelectorAll("fieldset.collapse");for(var c=0;c