From bbd969b40c0e5d05dd74dee2ea0093fb4915cc8e Mon Sep 17 00:00:00 2001 From: ObserverOfTime Date: Wed, 21 Dec 2022 18:35:32 +0200 Subject: [PATCH] Reformat scripts & update icon font --- .editorconfig | 1 + MangAdventure/templates/layout.html | 4 ++-- static/scripts/bookmark.js | 13 ++++++++---- static/scripts/chapter.js | 25 +++++++++++++--------- static/scripts/search.js | 31 +++++++++++++++------------ static/scripts/tinymce-init.js | 33 ++++++++++++++--------------- 6 files changed, 60 insertions(+), 47 deletions(-) diff --git a/.editorconfig b/.editorconfig index 554aeb10..f65c3382 100644 --- a/.editorconfig +++ b/.editorconfig @@ -18,6 +18,7 @@ quote_type = double [*.js] indent_size = 2 +max_line_length = 80 quote_type = single curly_bracket_next_line = false diff --git a/MangAdventure/templates/layout.html b/MangAdventure/templates/layout.html index 6d893cac..70221ab0 100644 --- a/MangAdventure/templates/layout.html +++ b/MangAdventure/templates/layout.html @@ -48,8 +48,8 @@ - {% endcache %} diff --git a/static/scripts/bookmark.js b/static/scripts/bookmark.js index 6eb06b06..8871c433 100644 --- a/static/scripts/bookmark.js +++ b/static/scripts/bookmark.js @@ -6,10 +6,15 @@ data.append('series', btn.dataset.series); xhr.open('POST', btn.dataset.target, true); xhr.onload = function() { - switch(xhr.status) { - case 201: btn.className = 'mi mi-bookmark bookmark-btn'; break; - case 204: btn.className = 'mi mi-bookmark-o bookmark-btn'; break; - default: console.error(xhr.statusText); + switch (xhr.status) { + case 201: + btn.className = 'mi mi-bookmark bookmark-btn'; + break; + case 204: + btn.className = 'mi mi-bookmark-o bookmark-btn'; + break; + default: + console.error(xhr.statusText); } }; xhr.send(data); diff --git a/static/scripts/chapter.js b/static/scripts/chapter.js index ad1a4129..e4eb01e3 100644 --- a/static/scripts/chapter.js +++ b/static/scripts/chapter.js @@ -4,7 +4,7 @@ const img = ph.nextElementSibling; - if(img.complete) ph.remove(); + if (img.complete) ph.remove(); else img.addEventListener('load', () => ph.remove(), true); img.addEventListener('click', function(evt) { @@ -18,20 +18,25 @@ document.getElementById('controls').removeAttribute('class'); document.querySelector('.curr-page input') .addEventListener('keyup', function(evt) { - if(evt.keyCode !== 13) return; + if (evt.code !== 'Enter') return; const n = Number(this.value); - if(!n || !Number.isInteger(n)) return; - if(n === Number(this.placeholder)) return; - if(n > Number(this.dataset.max)) return; - if(n > 0) location.href = `../${n}/`; + if (!n || !Number.isInteger(n)) return; + if (n === Number(this.placeholder)) return; + if (n > Number(this.dataset.max)) return; + if (n > 0) location.href = `../${n}/`; }); }); document.body.addEventListener('keyup', evt => { - switch(evt.keyCode) { - case 37: location.href = changePage('prev'); break; - case 39: location.href = changePage('next'); break; - default: return; + switch (evt.code) { + case 'ArrowLeft': + location.href = changePage('prev'); + break; + case 'ArrowRight': + location.href = changePage('next'); + break; + default: + return; } }); })(document.getElementById('placeholder')); diff --git a/static/scripts/search.js b/static/scripts/search.js index 491238aa..31817cf3 100644 --- a/static/scripts/search.js +++ b/static/scripts/search.js @@ -12,10 +12,10 @@ } function matchQuery(q) { - if(q.matches) { + if (q.matches) { document.querySelectorAll('td.s-hidden').forEach((elem, i) => { const n = (i / 6 >> 0) + 1; - switch(elem.className) { + switch (elem.className) { case 'result-people s-hidden': appendInfo(n, 'people', 'Author/Artist', elem.innerHTML); break; @@ -46,29 +46,30 @@ const form = document.getElementById('search-form'); const table = document.getElementById('result-table'); - if(table && window.Tablesort) { + if (table && window.Tablesort) { new window.Tablesort(table); table.querySelector('th').removeAttribute('data-sort-default'); } matchQuery(query); - form['categories[]'].forEach(c => {c.indeterminate = true}); + form['categories[]'].forEach(c => { c.indeterminate = true }); (url.searchParams.get('categories') || '').split(',') .filter(e => e !== '').forEach(c => { c = c.trim().toLowerCase(); const val = c[0] === '-' ? c.slice(1) : c; - const input = Array.from(form['categories[]']).find(e => e.value === val); + const input = Array.from(form['categories[]']) + .find(e => e.value === val); input.indeterminate = false; input.checked = c[0] !== '-'; }); form.elements[5].lastChild.addEventListener('click', evt => { let el = evt.target; - if(el.tagName === 'I' || el.nodeType === 3) + if (el.tagName === 'I' || el.nodeType === 3) el = el.parentNode; - if(el.className !== 'tooltip category') return; + if (el.className !== 'tooltip category') return; const [state, input] = el.children; - switch(state.className) { + switch (state.className) { case 'mi mi-circle': state.className = 'mi mi-ok-circle'; input.indeterminate = false; @@ -92,19 +93,19 @@ evt.stopPropagation(); form.categories.value = Array.from(form['categories[]']) .reduce((acc, cur) => { - if(cur.indeterminate) return acc; - if(acc !== '') acc += ','; - if(!cur.checked) acc += '-'; + if (cur.indeterminate) return acc; + if (acc !== '') acc += ','; + if (!cur.checked) acc += '-'; return acc + cur.value; }, ''); Array.from(form.elements).slice(0, 5).concat(form.categories) - .forEach(el => {el.disabled = !el.value}); + .forEach(el => { el.disabled = !el.value }); const search = form.action + '?' + new URLSearchParams(new FormData(form)).toString(); const xhr = new XMLHttpRequest(); xhr.open(form.method, search, true); xhr.onload = function() { - if(this.status !== 200) { + if (this.status !== 200) { document.open(); document.write(this.responseText); document.close(); @@ -123,7 +124,9 @@ history.replaceState({name: 'search'}, document.title, search); initialize(); }; - xhr.onerror = function() {console.error(this.statusText)}; + xhr.onerror = function() { + console.error(this.statusText); + }; xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); xhr.send(null); }); diff --git a/static/scripts/tinymce-init.js b/static/scripts/tinymce-init.js index cb9ffecc..db0a8ac9 100644 --- a/static/scripts/tinymce-init.js +++ b/static/scripts/tinymce-init.js @@ -3,28 +3,27 @@ window.addEventListener('DOMContentLoaded', () => { document.querySelectorAll('.tinymce').forEach(el => { const mce_conf = JSON.parse(el.dataset.tinymceConfig); - if('replace_icons' in mce_conf) { + if ('replace_icons' in mce_conf) { const old_setup = mce_conf.setup; mce_conf.setup = editor => { - if(typeof old_setup === 'function') old_setup(editor); + if (typeof old_setup === 'function') old_setup(editor); editor.on('init', evt => { - evt.target.editorContainer.querySelectorAll('i') - .forEach(ico => { - if(ico.className === 'mce-caret') { - ico.className = 'mi mi-down'; - } else if(ico.className.endsWith('save')) { - ico.className = 'mi mi-send'; - ico.nextElementSibling.innerHTML = - mce_conf.submit_text || 'Send'; - } else { - ico.className = ico.className - .replace('mce-ico', 'mi') - .replace('mce-i-', 'mi-'); - } - }); + evt.target.editorContainer.querySelectorAll('i').forEach(ico => { + if (ico.className === 'mce-caret') { + ico.className = 'mi mi-down'; + } else if (ico.className.endsWith('save')) { + ico.className = 'mi mi-send'; + ico.nextElementSibling.innerHTML = + mce_conf.submit_text || 'Send'; + } else { + ico.className = ico.className + .replace('mce-ico', 'mi') + .replace('mce-i-', 'mi-'); + } + }); }); }; } - if(!tinyMCE.editors[el.id]) tinyMCE.init(mce_conf); + if (!tinyMCE.editors[el.id]) tinyMCE.init(mce_conf); }); });