From f4c660e1a85853acdadab61c0aa0009fa10d47a7 Mon Sep 17 00:00:00 2001 From: Gusted Date: Sun, 10 Jan 2021 18:27:20 +0100 Subject: [PATCH] Cleanup Javascript - Remove sortable.js(not used). - Cleanup redirects, I couldn't properly look at it. - Made main.js ECMA6 ready. - Made event functions into a declaration to ensure compatibility with possible older browser version. - Removed useless `=== true/false`. - tempSrc uses declaration now. --- assets/js/main.js | 36 +++++----- assets/js/redirects.js | 71 ++++++++++---------- assets/js/sortable.js | 145 ----------------------------------------- 3 files changed, 56 insertions(+), 196 deletions(-) delete mode 100644 assets/js/sortable.js diff --git a/assets/js/main.js b/assets/js/main.js index 5b9f01f2..6cc8b207 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -1,8 +1,7 @@ -document.querySelectorAll(".onclick-select").forEach(element => { +document.querySelectorAll(".onclick-select").forEach((element) => { element.addEventListener("click", element.select); }); - // Navbar dropdowns const navSections = document.querySelectorAll(".nav-details"); @@ -11,23 +10,25 @@ navSections.forEach(navSection => { }); document.addEventListener("click", navSectionsClose); -function navSectionsToggle() { +const navSectionsToggle = () => { // When opening next dropdown, hide previous if (this.open) { - navSections.forEach(navSection => { - if (navSection != this && navSection.open) navSection.open = !open; + navSections.forEach((navSection) => { + if (navSection != this && navSection.open) { + navSection.open = !open; + } }); } } -function navSectionsClose(event) { +const navSectionsClose = (event) => { // Hide all dropdowns when clicking in different place if ( event.target.matches(".nav-summary") || event.target.parentNode.matches(".nav-summary") ) return; - navSections.forEach(navSection => { + navSections.forEach((navSection) => { navSection.open = !open; }); } @@ -37,11 +38,11 @@ function navSectionsClose(event) { document.querySelector("#nav-switch-theme").style.display = "inline"; document.querySelector("#nav-switch-theme").addEventListener("click", changeColorScheme); -function changeColorScheme() { +const changeColorScheme = () => { // Use whatever users want if (localStorage.getItem("colorScheme") === "dark") { // Change to light theme - if (window.matchMedia("(prefers-color-scheme: dark)").matches === false) { + if (!window.matchMedia("(prefers-color-scheme: dark)").matches) { document.querySelector("#dark-css").setAttribute("media", "(prefers-color-scheme: dark)"); localStorage.removeItem("colorScheme"); } else { @@ -52,7 +53,7 @@ function changeColorScheme() { } // Change to dark theme else if (localStorage.getItem("colorScheme") === "light") { - if (window.matchMedia("(prefers-color-scheme: dark)").matches === true) { + if (window.matchMedia("(prefers-color-scheme: dark)").matches) { document.querySelector("#dark-css").setAttribute("media", "(prefers-color-scheme: dark)"); localStorage.removeItem("colorScheme"); } else { @@ -63,7 +64,7 @@ function changeColorScheme() { } // Just use whatever browsers want - else if (window.matchMedia("(prefers-color-scheme: dark)").matches === true) { + else if (window.matchMedia("(prefers-color-scheme: dark)").matches) { // Change to light Theme document.querySelector("#dark-css").setAttribute("media", "invalid"); localStorage.setItem("colorScheme", "light"); @@ -78,16 +79,15 @@ function changeColorScheme() { // Fix images in dark theme function fixThemeImages() { - document.querySelectorAll('[data-theme-src]').forEach(function(image) { - tempSrc = image.src; + document.querySelectorAll('[data-theme-src]').forEach((image) => { + const tempSrc = image.src; image.src = image.getAttribute("data-theme-src"); image.setAttribute("data-theme-src", tempSrc); }); } -if ( - (localStorage.getItem("colorScheme") === "dark") || - (window.matchMedia("(prefers-color-scheme: dark)").matches ^ - localStorage.getItem("colorScheme") === "light") -) { +if (localStorage.getItem("colorScheme") === "dark" || + window.matchMedia("(prefers-color-scheme: dark)").matches ^ + localStorage.getItem("colorScheme") === "light" + ) { fixThemeImages(); } diff --git a/assets/js/redirects.js b/assets/js/redirects.js index ef9ebca8..c5a51078 100644 --- a/assets/js/redirects.js +++ b/assets/js/redirects.js @@ -1,33 +1,38 @@ -if (window.location == 'https://www.privacytools.io/#ukusa') { window.location = 'https://www.privacytools.io/providers/';} -if (window.location == 'https://www.privacytools.io/#vpn') { window.location = 'https://www.privacytools.io/providers/vpn/';} -if (window.location == 'https://www.privacytools.io/#email') { window.location = 'https://www.privacytools.io/providers/email/';} -if (window.location == 'https://www.privacytools.io/#cloud') { window.location = 'https://www.privacytools.io/providers/cloud-storage/';} -if (window.location == 'https://www.privacytools.io/#social') { window.location = 'https://www.privacytools.io/providers/social-networks/';} -if (window.location == 'https://www.privacytools.io/#dns') { window.location = 'https://www.privacytools.io/providers/dns/';} -if (window.location == 'https://www.privacytools.io/#search') { window.location = 'https://www.privacytools.io/providers/search-engines/';} -if (window.location == 'https://www.privacytools.io/#host') { window.location = 'https://www.privacytools.io/providers/hosting/';} -if (window.location == 'https://www.privacytools.io/#paste') { window.location = 'https://www.privacytools.io/providers/paste';} -if (window.location == 'https://www.privacytools.io/#browser') { window.location = 'https://www.privacytools.io/browsers/';} -if (window.location == 'https://www.privacytools.io/#fingerprint') { window.location = 'https://www.privacytools.io/browsers/#fingerprint';} -if (window.location == 'https://www.privacytools.io/#webrtc') { window.location = 'https://www.privacytools.io/browsers/#webrtc';} -if (window.location == 'https://www.privacytools.io/#addons') { window.location = 'https://www.privacytools.io/browsers/#addons';} -if (window.location == 'https://www.privacytools.io/#about_config') { window.location = 'https://www.privacytools.io/browsers/#about_config';} -if (window.location == 'https://www.privacytools.io/#clients') { window.location = 'https://www.privacytools.io/software/email/';} -if (window.location == 'https://www.privacytools.io/#messaging') { window.location = 'https://www.privacytools.io/software/email/#messaging';} -if (window.location == 'https://www.privacytools.io/#im') { window.location = 'https://www.privacytools.io/software/im/';} -if (window.location == 'https://www.privacytools.io/#voip') { window.location = 'https://www.privacytools.io/software/voip/';} -if (window.location == 'https://www.privacytools.io/#filesharing') { window.location = 'https://www.privacytools.io/software/file-sharing/';} -if (window.location == 'https://www.privacytools.io/#mycloud') { window.location = 'https://www.privacytools.io/software/cloud/';} -if (window.location == 'https://www.privacytools.io/#sync') { window.location = 'https://www.privacytools.io/software/file-sync/';} -if (window.location == 'https://www.privacytools.io/#pw') { window.location = 'https://www.privacytools.io/software/passwords/';} -if (window.location == 'https://www.privacytools.io/#calendar_contacts') { window.location = 'https://www.privacytools.io/software/calendar-contacts/';} -if (window.location == 'https://www.privacytools.io/#encrypt') { window.location = 'https://www.privacytools.io/software/encryption-tools/';} -if (window.location == 'https://www.privacytools.io/#darknets') { window.location = 'https://www.privacytools.io/software/networks/';} -if (window.location == 'https://www.privacytools.io/#notebook') { window.location = 'https://www.privacytools.io/software/notebooks/';} -if (window.location == 'https://www.privacytools.io/#productivity') { window.location = 'https://www.privacytools.io/software/productivity/';} -if (window.location == 'https://www.privacytools.io/#os') { window.location = 'https://www.privacytools.io/operating-systems/';} -if (window.location == 'https://www.privacytools.io/#live_os') { window.location = 'https://www.privacytools.io/operating-systems/#live_os';} -if (window.location == 'https://www.privacytools.io/#mobile_os') { window.location = 'https://www.privacytools.io/operating-systems/#mobile_os';} -if (window.location == 'https://www.privacytools.io/#aaddons') { window.location = 'https://www.privacytools.io/operating-systems/#aaddons';} -if (window.location == 'https://www.privacytools.io/#firmware') { window.location = 'https://www.privacytools.io/operating-systems/#firmware';} -if (window.location == 'https://www.privacytools.io/#win10') { window.location = 'https://www.privacytools.io/operating-systems/#win10';} +const redirectLinks = { + 'https://www.privacytools.io/#ukusa': 'https://www.privacytools.io/providers/', + 'https://www.privacytools.io/#vpn': 'https://www.privacytools.io/providers/vpn/', + 'https://www.privacytools.io/#email': 'https://www.privacytools.io/providers/email/', + 'https://www.privacytools.io/#cloud': 'https://www.privacytools.io/providers/cloud-storage/', + 'https://www.privacytools.io/#social': 'https://www.privacytools.io/providers/social-networks/', + 'https://www.privacytools.io/#dns': 'https://www.privacytools.io/providers/dns/', + 'https://www.privacytools.io/#search': 'https://www.privacytools.io/providers/search-engines/', + 'https://www.privacytools.io/#host': 'https://www.privacytools.io/providers/hosting/', + 'https://www.privacytools.io/#paste': 'https://www.privacytools.io/providers/paste', + 'https://www.privacytools.io/#browser': 'https://www.privacytools.io/browsers/', + 'https://www.privacytools.io/#fingerprint': 'https://www.privacytools.io/browsers/#fingerprint', + 'https://www.privacytools.io/#webrtc': 'https://www.privacytools.io/browsers/#webrtc', + 'https://www.privacytools.io/#addons': 'https://www.privacytools.io/browsers/#addons', + 'https://www.privacytools.io/#about_config': 'https://www.privacytools.io/browsers/#about_config', + 'https://www.privacytools.io/#clients': 'https://www.privacytools.io/software/email/', + 'https://www.privacytools.io/#messaging': 'https://www.privacytools.io/software/email/#messaging', + 'https://www.privacytools.io/#im': 'https://www.privacytools.io/software/im/', + 'https://www.privacytools.io/#voip': 'https://www.privacytools.io/software/voip/', + 'https://www.privacytools.io/#filesharing': 'https://www.privacytools.io/software/file-sharing/', + 'https://www.privacytools.io/#mycloud': 'https://www.privacytools.io/software/cloud/', + 'https://www.privacytools.io/#sync': 'https://www.privacytools.io/software/file-sync/', + 'https://www.privacytools.io/#pw': 'https://www.privacytools.io/software/passwords/', + 'https://www.privacytools.io/#calendar_contacts': 'https://www.privacytools.io/software/calendar-contacts/', + 'https://www.privacytools.io/#encrypt': 'https://www.privacytools.io/software/encryption-tools/', + 'https://www.privacytools.io/#darknets': 'https://www.privacytools.io/software/networks/', + 'https://www.privacytools.io/#notebook': 'https://www.privacytools.io/software/notebooks/', + 'https://www.privacytools.io/#productivity': 'https://www.privacytools.io/software/productivity/', + 'https://www.privacytools.io/#os': 'https://www.privacytools.io/operating-systems/', + 'https://www.privacytools.io/#live_os': 'https://www.privacytools.io/operating-systems/#live_os', + 'https://www.privacytools.io/#mobile_os': 'https://www.privacytools.io/operating-systems/#mobile_os', + 'https://www.privacytools.io/#aaddons': 'https://www.privacytools.io/operating-systems/#aaddons', + 'https://www.privacytools.io/#firmware': 'https://www.privacytools.io/operating-systems/#firmware', + 'https://www.privacytools.io/#win10': 'https://www.privacytools.io/operating-systems/#win10' +} +if (redirectLinks[window.location]) { + window.location = redirectLinks[window.location]; +} \ No newline at end of file diff --git a/assets/js/sortable.js b/assets/js/sortable.js deleted file mode 100644 index 1e638aeb..00000000 --- a/assets/js/sortable.js +++ /dev/null @@ -1,145 +0,0 @@ -(function() { - var SELECTOR, clickEvent, numberRegExp, sortable, touchDevice, trimRegExp; - - SELECTOR = 'table[data-sortable]'; - - numberRegExp = /^-?[£$¤]?[\d,.]+%?$/; - - trimRegExp = /^\s+|\s+$/g; - - touchDevice = 'ontouchstart' in document.documentElement; - - clickEvent = touchDevice ? 'touchstart' : 'click'; - - sortable = { - init: function() { - var table, tables, _i, _len, _results; - tables = document.querySelectorAll(SELECTOR); - _results = []; - for (_i = 0, _len = tables.length; _i < _len; _i++) { - table = tables[_i]; - _results.push(sortable.initTable(table)); - } - return _results; - }, - initTable: function(table) { - var i, th, ths, _i, _len; - if (table.tHead.rows.length !== 1) { - return; - } - if (table.getAttribute('data-sortable-initialized') === 'true') { - return; - } - table.setAttribute('data-sortable-initialized', 'true'); - ths = table.querySelectorAll('th'); - for (i = _i = 0, _len = ths.length; _i < _len; i = ++_i) { - th = ths[i]; - if (th.getAttribute('data-sortable') !== 'false') { - sortable.setupClickableTH(table, th, i); - } - } - return table; - }, - setupClickableTH: function(table, th, i) { - var type; - type = sortable.getColumnType(table, i); - return th.addEventListener(clickEvent, function(e) { - var newSortedDirection, row, rowArray, rowArrayObject, sorted, sortedDirection, tBody, ths, _i, _j, _k, _len, _len1, _len2, _ref, _results; - sorted = this.getAttribute('data-sorted') === 'true'; - sortedDirection = this.getAttribute('data-sorted-direction'); - if (sorted) { - newSortedDirection = sortedDirection === 'ascending' ? 'descending' : 'ascending'; - } else { - newSortedDirection = type.defaultSortDirection; - } - ths = this.parentNode.querySelectorAll('th'); - for (_i = 0, _len = ths.length; _i < _len; _i++) { - th = ths[_i]; - th.setAttribute('data-sorted', 'false'); - th.removeAttribute('data-sorted-direction'); - } - this.setAttribute('data-sorted', 'true'); - this.setAttribute('data-sorted-direction', newSortedDirection); - tBody = table.tBodies[0]; - rowArray = []; - _ref = tBody.rows; - for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) { - row = _ref[_j]; - rowArray.push([sortable.getNodeValue(row.cells[i]), row]); - } - if (sorted) { - rowArray.reverse(); - } else { - rowArray.sort(type.compare); - } - _results = []; - for (_k = 0, _len2 = rowArray.length; _k < _len2; _k++) { - rowArrayObject = rowArray[_k]; - _results.push(tBody.appendChild(rowArrayObject[1])); - } - return _results; - }); - }, - getColumnType: function(table, i) { - var row, text, _i, _len, _ref; - _ref = table.tBodies[0].rows; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - row = _ref[_i]; - text = sortable.getNodeValue(row.cells[i]); - if (text !== '' && text.match(numberRegExp)) { - return sortable.types.numeric; - } - } - return sortable.types.alpha; - }, - getNodeValue: function(node) { - if (!node) { - return ''; - } - if (node.getAttribute('data-value') !== null) { - return node.getAttribute('data-value'); - } - if (typeof node.innerText !== 'undefined') { - return node.innerText.replace(trimRegExp, ''); - } - return node.textContent.replace(trimRegExp, ''); - }, - types: { - numeric: { - defaultSortDirection: 'descending', - compare: function(a, b) { - var aa, bb; - aa = parseFloat(a[0].replace(/[^0-9.-]/g, '')); - bb = parseFloat(b[0].replace(/[^0-9.-]/g, '')); - if (isNaN(aa)) { - aa = 0; - } - if (isNaN(bb)) { - bb = 0; - } - return bb - aa; - } - }, - alpha: { - defaultSortDirection: 'ascending', - compare: function(a, b) { - var aa, bb; - aa = a[0].toLowerCase(); - bb = b[0].toLowerCase(); - if (aa === bb) { - return 0; - } - if (aa < bb) { - return -1; - } - return 1; - } - } - } - }; - - setTimeout(sortable.init, 0); - - window.Sortable = sortable; - -}).call(this);