mirror of
https://github.com/django/django.git
synced 2025-06-05 11:39:13 +00:00
Fixed #31542 -- Simplified admin JavaScript with "for … of" statements.
This commit is contained in:
parent
007f9f9a4c
commit
3c5a586ce9
@ -6,10 +6,7 @@
|
|||||||
const box = document.getElementById(id);
|
const box = document.getElementById(id);
|
||||||
SelectBox.cache[id] = [];
|
SelectBox.cache[id] = [];
|
||||||
const cache = SelectBox.cache[id];
|
const cache = SelectBox.cache[id];
|
||||||
const boxOptions = box.options;
|
for (const node of box.options) {
|
||||||
const boxOptionsLength = boxOptions.length;
|
|
||||||
for (let i = 0, j = boxOptionsLength; i < j; i++) {
|
|
||||||
const node = boxOptions[i];
|
|
||||||
cache.push({value: node.value, text: node.text, displayed: 1});
|
cache.push({value: node.value, text: node.text, displayed: 1});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -17,9 +14,7 @@
|
|||||||
// Repopulate HTML select box from cache
|
// Repopulate HTML select box from cache
|
||||||
const box = document.getElementById(id);
|
const box = document.getElementById(id);
|
||||||
box.innerHTML = '';
|
box.innerHTML = '';
|
||||||
const cache = SelectBox.cache[id];
|
for (const node of SelectBox.cache[id]) {
|
||||||
for (let i = 0, j = cache.length; i < j; i++) {
|
|
||||||
const node = cache[i];
|
|
||||||
if (node.displayed) {
|
if (node.displayed) {
|
||||||
const new_option = new Option(node.text, node.value, false, false);
|
const new_option = new Option(node.text, node.value, false, false);
|
||||||
// Shows a tooltip when hovering over the option
|
// Shows a tooltip when hovering over the option
|
||||||
@ -32,14 +27,10 @@
|
|||||||
// Redisplay the HTML select box, displaying only the choices containing ALL
|
// Redisplay the HTML select box, displaying only the choices containing ALL
|
||||||
// the words in text. (It's an AND search.)
|
// the words in text. (It's an AND search.)
|
||||||
const tokens = text.toLowerCase().split(/\s+/);
|
const tokens = text.toLowerCase().split(/\s+/);
|
||||||
const cache = SelectBox.cache[id];
|
for (const node of SelectBox.cache[id]) {
|
||||||
for (let i = 0, j = cache.length; i < j; i++) {
|
|
||||||
const node = cache[i];
|
|
||||||
node.displayed = 1;
|
node.displayed = 1;
|
||||||
const node_text = node.text.toLowerCase();
|
const node_text = node.text.toLowerCase();
|
||||||
const numTokens = tokens.length;
|
for (const token of tokens) {
|
||||||
for (let k = 0; k < numTokens; k++) {
|
|
||||||
const token = tokens[k];
|
|
||||||
if (node_text.indexOf(token) === -1) {
|
if (node_text.indexOf(token) === -1) {
|
||||||
node.displayed = 0;
|
node.displayed = 0;
|
||||||
break; // Once the first token isn't found we're done
|
break; // Once the first token isn't found we're done
|
||||||
@ -51,8 +42,7 @@
|
|||||||
delete_from_cache: function(id, value) {
|
delete_from_cache: function(id, value) {
|
||||||
let delete_index = null;
|
let delete_index = null;
|
||||||
const cache = SelectBox.cache[id];
|
const cache = SelectBox.cache[id];
|
||||||
for (let i = 0, j = cache.length; i < j; i++) {
|
for (const [i, node] of cache.entries()) {
|
||||||
const node = cache[i];
|
|
||||||
if (node.value === value) {
|
if (node.value === value) {
|
||||||
delete_index = i;
|
delete_index = i;
|
||||||
break;
|
break;
|
||||||
@ -65,9 +55,7 @@
|
|||||||
},
|
},
|
||||||
cache_contains: function(id, value) {
|
cache_contains: function(id, value) {
|
||||||
// Check if an item is contained in the cache
|
// Check if an item is contained in the cache
|
||||||
const cache = SelectBox.cache[id];
|
for (const node of SelectBox.cache[id]) {
|
||||||
for (let i = 0, j = cache.length; i < j; i++) {
|
|
||||||
const node = cache[i];
|
|
||||||
if (node.value === value) {
|
if (node.value === value) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -76,10 +64,7 @@
|
|||||||
},
|
},
|
||||||
move: function(from, to) {
|
move: function(from, to) {
|
||||||
const from_box = document.getElementById(from);
|
const from_box = document.getElementById(from);
|
||||||
const boxOptions = from_box.options;
|
for (const option of from_box.options) {
|
||||||
const boxOptionsLength = boxOptions.length;
|
|
||||||
for (let i = 0, j = boxOptionsLength; i < j; i++) {
|
|
||||||
const option = boxOptions[i];
|
|
||||||
const option_value = option.value;
|
const option_value = option.value;
|
||||||
if (option.selected && SelectBox.cache_contains(from, option_value)) {
|
if (option.selected && SelectBox.cache_contains(from, option_value)) {
|
||||||
SelectBox.add_to_cache(to, {value: option_value, text: option.text, displayed: 1});
|
SelectBox.add_to_cache(to, {value: option_value, text: option.text, displayed: 1});
|
||||||
@ -91,10 +76,7 @@
|
|||||||
},
|
},
|
||||||
move_all: function(from, to) {
|
move_all: function(from, to) {
|
||||||
const from_box = document.getElementById(from);
|
const from_box = document.getElementById(from);
|
||||||
const boxOptions = from_box.options;
|
for (const option of from_box.options) {
|
||||||
const boxOptionsLength = boxOptions.length;
|
|
||||||
for (let i = 0, j = boxOptionsLength; i < j; i++) {
|
|
||||||
const option = boxOptions[i];
|
|
||||||
const option_value = option.value;
|
const option_value = option.value;
|
||||||
if (SelectBox.cache_contains(from, option_value)) {
|
if (SelectBox.cache_contains(from, option_value)) {
|
||||||
SelectBox.add_to_cache(to, {value: option_value, text: option.text, displayed: 1});
|
SelectBox.add_to_cache(to, {value: option_value, text: option.text, displayed: 1});
|
||||||
@ -119,10 +101,8 @@
|
|||||||
},
|
},
|
||||||
select_all: function(id) {
|
select_all: function(id) {
|
||||||
const box = document.getElementById(id);
|
const box = document.getElementById(id);
|
||||||
const boxOptions = box.options;
|
for (const option of box.options) {
|
||||||
const boxOptionsLength = boxOptions.length;
|
option.selected = 'selected';
|
||||||
for (let i = 0; i < boxOptionsLength; i++) {
|
|
||||||
boxOptions[i].selected = 'selected';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -16,16 +16,15 @@ Requires core.js and SelectBox.js.
|
|||||||
from_box.id += '_from'; // change its ID
|
from_box.id += '_from'; // change its ID
|
||||||
from_box.className = 'filtered';
|
from_box.className = 'filtered';
|
||||||
|
|
||||||
const ps = from_box.parentNode.getElementsByTagName('p');
|
for (const p of from_box.parentNode.getElementsByTagName('p')) {
|
||||||
for (let i = 0; i < ps.length; i++) {
|
if (p.classList.contains("info")) {
|
||||||
if (ps[i].classList.contains("info")) {
|
|
||||||
// Remove <p class="info">, because it just gets in the way.
|
// Remove <p class="info">, because it just gets in the way.
|
||||||
from_box.parentNode.removeChild(ps[i]);
|
from_box.parentNode.removeChild(p);
|
||||||
} else if (ps[i].classList.contains("help")) {
|
} else if (p.classList.contains("help")) {
|
||||||
// Move help text up to the top so it isn't below the select
|
// 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
|
// boxes or wrapped off on the side to the right of the add
|
||||||
// button:
|
// button:
|
||||||
from_box.parentNode.insertBefore(ps[i], from_box.parentNode.firstChild);
|
from_box.parentNode.insertBefore(p, from_box.parentNode.firstChild);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,9 +35,7 @@
|
|||||||
DateTimeShortcuts.timezoneOffset = localOffset - serverOffset;
|
DateTimeShortcuts.timezoneOffset = localOffset - serverOffset;
|
||||||
}
|
}
|
||||||
|
|
||||||
const inputs = document.getElementsByTagName('input');
|
for (const inp of document.getElementsByTagName('input')) {
|
||||||
for (let i = 0; i < inputs.length; i++) {
|
|
||||||
const inp = inputs[i];
|
|
||||||
if (inp.type === 'text' && inp.classList.contains('vTimeField')) {
|
if (inp.type === 'text' && inp.classList.contains('vTimeField')) {
|
||||||
DateTimeShortcuts.addClock(inp);
|
DateTimeShortcuts.addClock(inp);
|
||||||
DateTimeShortcuts.addTimezoneWarning(inp);
|
DateTimeShortcuts.addTimezoneWarning(inp);
|
||||||
|
@ -4,8 +4,7 @@
|
|||||||
const modelName = document.getElementById('django-admin-form-add-constants').dataset.modelName;
|
const modelName = document.getElementById('django-admin-form-add-constants').dataset.modelName;
|
||||||
if (modelName) {
|
if (modelName) {
|
||||||
const form = document.getElementById(modelName + '_form');
|
const form = document.getElementById(modelName + '_form');
|
||||||
for (let i = 0; i < form.elements.length; i++) {
|
for (const element of form.elements) {
|
||||||
const element = form.elements[i];
|
|
||||||
// HTMLElement.offsetParent returns null when the element is not
|
// HTMLElement.offsetParent returns null when the element is not
|
||||||
// rendered.
|
// rendered.
|
||||||
if (inputTags.includes(element.tagName) && !element.disabled && element.offsetParent) {
|
if (inputTags.includes(element.tagName) && !element.disabled && element.offsetParent) {
|
||||||
|
@ -4,8 +4,7 @@
|
|||||||
window.addEventListener('load', function() {
|
window.addEventListener('load', function() {
|
||||||
// Add anchor tag for Show/Hide link
|
// Add anchor tag for Show/Hide link
|
||||||
const fieldsets = document.querySelectorAll('fieldset.collapse');
|
const fieldsets = document.querySelectorAll('fieldset.collapse');
|
||||||
for (let i = 0; i < fieldsets.length; i++) {
|
for (const [i, elem] of fieldsets.entries()) {
|
||||||
const elem = fieldsets[i];
|
|
||||||
// Don't hide if fields in this fieldset have errors
|
// Don't hide if fields in this fieldset have errors
|
||||||
if (elem.querySelectorAll('div.errors, ul.errorlist').length === 0) {
|
if (elem.querySelectorAll('div.errors, ul.errorlist').length === 0) {
|
||||||
elem.classList.add('collapsed');
|
elem.classList.add('collapsed');
|
||||||
@ -37,9 +36,8 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const inlineDivs = document.querySelectorAll('fieldset.module');
|
document.querySelectorAll('fieldset.module').forEach(function(el) {
|
||||||
for (let i = 0; i < inlineDivs.length; i++) {
|
el.addEventListener('click', toggleFunc);
|
||||||
inlineDivs[i].addEventListener('click', toggleFunc);
|
});
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -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=
|
'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(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)});
|
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)})});
|
||||||
|
@ -131,20 +131,10 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Downcoder.map = {};
|
Downcoder.map = {};
|
||||||
Downcoder.chars = [];
|
for (const lookup of ALL_DOWNCODE_MAPS) {
|
||||||
for (let i = 0; i < ALL_DOWNCODE_MAPS.length; i++) {
|
Object.assign(Downcoder.map, lookup);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Downcoder.chars = Object.keys(Downcoder.map);
|
||||||
Downcoder.regex = new RegExp(Downcoder.chars.join('|'), 'g');
|
Downcoder.regex = new RegExp(Downcoder.chars.join('|'), 'g');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user