code refactor: add $.remove() and $$.remove()

This commit is contained in:
tophf 2017-12-02 19:54:54 +03:00
parent d4c0930723
commit 0de69e306d
4 changed files with 23 additions and 18 deletions

View File

@ -289,9 +289,7 @@ function initCodeMirror() {
}
}
if (index >= total) {
if (progress) {
progress.remove();
}
$.remove(progress);
return;
}
if (!progress &&
@ -1015,9 +1013,7 @@ function setupGlobalSearch() {
const dlg = $('.CodeMirror-dialog', cm.getWrapperElement());
if (!dlg || cmp === 0 || wrapAround && CodeMirror.cmpPos(cm.getCursor(), origPos) >= 0) {
if (dlg) {
dlg.remove();
}
$.remove(dlg);
doReplace();
}
});
@ -2061,7 +2057,7 @@ function setGlobalProgress(done, total) {
setTimeout(() => {
progressElement.title = progress + '%';
});
} else if (progressElement) {
progressElement.remove();
} else {
$.remove(progressElement);
}
}

View File

@ -112,7 +112,7 @@
$('.header').classList.add('meta-init');
$('.header').classList.remove('meta-init-error');
setTimeout(() => $('.lds-spinner') && $('.lds-spinner').remove(), 1000);
setTimeout(() => $.remove('.lds-spinner'), 1000);
showError('');
requestAnimationFrame(adjustCodeHeight);
@ -195,8 +195,7 @@
function install(style) {
installed = style;
$$('.warning')
.forEach(el => el.remove());
$$.remove('.warning');
$('button.install').disabled = true;
$('button.install').classList.add('installed');
$('h2.installed').classList.add('active');

View File

@ -11,6 +11,19 @@ for (const type of [NodeList, NamedNodeMap, HTMLCollection, HTMLAllCollection])
}
}
$.remove = (selector, base = document) => {
const el = selector instanceof Node ? selector : $(selector, base);
if (el) {
el.remove();
}
};
$$.remove = (selector, base = document) => {
for (const el of base.querySelectorAll(selector)) {
el.remove();
}
};
{
// display a full text tooltip on buttons with ellipsis overflow and no inherent title
const addTooltipsToEllipsized = () => {
@ -41,10 +54,7 @@ for (const type of [NodeList, NamedNodeMap, HTMLCollection, HTMLAllCollection])
}
onDOMready().then(() => {
const el = $('#firefox-transitions-bug-suppressor');
if (el) {
el.remove();
}
$.remove('#firefox-transitions-bug-suppressor');
});
if (!chrome.app && chrome.windows) {

View File

@ -348,7 +348,7 @@ Object.assign(handleEvent, {
indicator(event) {
const entry = handleEvent.getClickedStyleElement(event);
const info = template.regexpProblemExplanation.cloneNode(true);
$$('#' + info.id).forEach(el => el.remove());
$.remove('#' + info.id);
$$('a', info).forEach(el => (el.onclick = handleEvent.openURLandHide));
$$('button', info).forEach(el => (el.onclick = handleEvent.closeExplanation));
entry.appendChild(info);
@ -420,14 +420,14 @@ function handleUpdate(style) {
// Add an entry when a new style for the current url is installed
if (tabURL && BG.getApplicableSections({style, matchUrl: tabURL, stopOnFirst: true}).length) {
document.body.classList.remove('blocked');
$$('.blocked-info, #no-styles').forEach(el => el.remove());
$$.remove('.blocked-info, #no-styles');
createStyleElement({style});
}
}
function handleDelete(id) {
$$(ENTRY_ID_PREFIX + id).forEach(el => el.remove());
$.remove(ENTRY_ID_PREFIX + id);
}