Fixed #31542 -- Simplified admin JavaScript with "for … of" statements.

This commit is contained in:
Jon Dufresne 2020-05-05 17:17:12 -07:00 committed by Mariusz Felisiak
parent 007f9f9a4c
commit 3c5a586ce9
7 changed files with 26 additions and 62 deletions

View File

@ -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';
}
}
};

View File

@ -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 <p class="info">, 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);
}
}

View File

@ -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);

View File

@ -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) {

View File

@ -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);
});
});
}

View File

@ -1,2 +1,2 @@
'use strict';window.addEventListener("load",function(){var d=document.querySelectorAll("fieldset.collapse");for(var c=0;c<d.length;c++){var a=d[c];if(0===a.querySelectorAll("div.errors, ul.errorlist").length){a.classList.add("collapsed");a=a.querySelector("h2");const b=document.createElement("a");b.id="fieldsetcollapser"+c;b.className="collapse-toggle";b.href="#";b.textContent=gettext("Show");a.appendChild(document.createTextNode(" ("));a.appendChild(b);a.appendChild(document.createTextNode(")"))}}d=
function(b){if(b.target.matches(".collapse-toggle")){b.preventDefault();b.stopPropagation();const a=b.target.closest("fieldset");a.classList.contains("collapsed")?(b.target.textContent=gettext("Hide"),a.classList.remove("collapsed")):(b.target.textContent=gettext("Show"),a.classList.add("collapsed"))}};c=document.querySelectorAll("fieldset.module");for(a=0;a<c.length;a++)c[a].addEventListener("click",d)});
'use strict';window.addEventListener("load",function(){var c=document.querySelectorAll("fieldset.collapse");for(const [a,b]of c.entries())if(0===b.querySelectorAll("div.errors, ul.errorlist").length){b.classList.add("collapsed");c=b.querySelector("h2");const d=document.createElement("a");d.id="fieldsetcollapser"+a;d.className="collapse-toggle";d.href="#";d.textContent=gettext("Show");c.appendChild(document.createTextNode(" ("));c.appendChild(d);c.appendChild(document.createTextNode(")"))}const e=
function(a){if(a.target.matches(".collapse-toggle")){a.preventDefault();a.stopPropagation();const b=a.target.closest("fieldset");b.classList.contains("collapsed")?(a.target.textContent=gettext("Hide"),b.classList.remove("collapsed")):(a.target.textContent=gettext("Show"),b.classList.add("collapsed"))}};document.querySelectorAll("fieldset.module").forEach(function(a){a.addEventListener("click",e)})});

View File

@ -131,20 +131,10 @@
return;
}
Downcoder.map = {};
Downcoder.chars = [];
for (let i = 0; i < ALL_DOWNCODE_MAPS.length; i++) {
const lookup = ALL_DOWNCODE_MAPS[i];
for (const c in lookup) {
if (lookup.hasOwnProperty(c)) {
Downcoder.map[c] = lookup[c];
}
}
}
for (const k in Downcoder.map) {
if (Downcoder.map.hasOwnProperty(k)) {
Downcoder.chars.push(k);
}
for (const lookup of ALL_DOWNCODE_MAPS) {
Object.assign(Downcoder.map, lookup);
}
Downcoder.chars = Object.keys(Downcoder.map);
Downcoder.regex = new RegExp(Downcoder.chars.join('|'), 'g');
}
};