|
1 |
| -document.addEventListener('DOMContentLoaded', function () { |
| 1 | +// there is no need to check for dom content loaded if you use defer inside html. Defer makes sure the script pauses until the dom loaded |
2 | 2 |
|
3 |
| - // rather than looking for it all the time, you could just create an enum and change this once than updating every line |
4 |
| - const github = { |
5 |
| - repo: 'https://github.com/lapce/lapce', |
6 |
| - windows: 'Lapce-windows.msi', |
7 |
| - linux: 'Lapce-linux.tar.gz', |
8 |
| - macos: 'Lapce-macos.dmg' |
9 |
| - } |
| 3 | +document.addEventListener("DOMContentLoaded", function () { |
| 4 | + // rather than looking for it all the time, you could just create an enum and change this once than updating every line |
10 | 5 |
|
11 |
| - // there is no need to check for dom content loaded if you use defer inside html. Defer makes sure the script pauses until the dom loaded |
| 6 | + const github = { |
| 7 | + repo: "https://github.com/lapce/lapce", |
| 8 | + windows: "Lapce-windows.msi", |
| 9 | + linux: "Lapce-linux.tar.gz", |
| 10 | + macos: "Lapce-macos.dmg", |
| 11 | + }; |
12 | 12 |
|
13 |
| - // open |
14 |
| - const burger = document.querySelectorAll('.navbar-burger'); |
15 |
| - const menu = document.querySelectorAll('.navbar-menu'); |
| 13 | + const burger = document.querySelectorAll(".navbar-burger"); |
| 14 | + const menu = document.querySelectorAll(".navbar-menu"); |
| 15 | + const close = document.querySelectorAll(".navbar-close"); |
| 16 | + const backdrop = document.querySelectorAll(".navbar-backdrop"); |
| 17 | + const download = document.querySelector("#download"); |
16 | 18 |
|
17 |
| - if (burger.length && menu.length) { |
18 |
| - for (let i = 0; i < burger.length; i++) { |
19 |
| - burger[i].addEventListener('click', () => { |
20 |
| - for (let j = 0; j < menu.length; j++) { |
21 |
| - menu[j].classList.toggle('hidden'); |
22 |
| - } |
23 |
| - }); |
24 |
| - } |
25 |
| - } |
| 19 | + if (burger.length && menu.length) { |
| 20 | + burger.forEach((burgerElement) => { |
| 21 | + burgerElement.addEventListener("click", () => { |
| 22 | + menu.forEach((menuElement) => { |
| 23 | + menuElement.classList.toggle("hidden"); |
| 24 | + }); |
| 25 | + }); |
| 26 | + }); |
| 27 | + } |
26 | 28 |
|
27 |
| - // close |
28 |
| - const close = document.querySelectorAll('.navbar-close'); |
29 |
| - const backdrop = document.querySelectorAll('.navbar-backdrop'); |
| 29 | + const closeMenu = () => { |
| 30 | + menu.forEach((menuElement) => { |
| 31 | + menuElement.classList.toggle("hidden"); |
| 32 | + }); |
| 33 | + }; |
30 | 34 |
|
31 |
| - if (close.length) { |
32 |
| - for (let i = 0; i < close.length; i++) { |
33 |
| - close[i].addEventListener('click', () => { |
34 |
| - for (let j = 0; j < menu.length; j++) { |
35 |
| - menu[j].classList.toggle('hidden'); |
36 |
| - } |
37 |
| - }); |
38 |
| - } |
39 |
| - } |
| 35 | + if (close.length) { |
| 36 | + close.forEach((closeElement) => { |
| 37 | + closeElement.addEventListener("click", closeMenu); |
| 38 | + }); |
| 39 | + } |
40 | 40 |
|
41 |
| - if (backdrop.length) { |
42 |
| - for (let i = 0; i < backdrop.length; i++) { |
43 |
| - backdrop[i].addEventListener('click', () => { |
44 |
| - for (let j = 0; j < menu.length; j++) { |
45 |
| - menu[j].classList.toggle('hidden'); |
46 |
| - } |
47 |
| - }); |
48 |
| - } |
49 |
| - } |
| 41 | + if (backdrop.length) { |
| 42 | + backdrop.forEach((backdropElement) => { |
| 43 | + backdropElement.addEventListener("click", closeMenu); |
| 44 | + }); |
| 45 | + } |
50 | 46 |
|
51 |
| - // to avoid variables being reached outside of scope, ES6 guides recommend the use of let and const for variables. "var" is only available for backwards compatibility |
52 |
| - let OSName = "mac"; |
53 |
| - const navApp = navigator.userAgent.toLowerCase(); |
| 47 | + const navApp = navigator.userAgent.toLowerCase(); |
54 | 48 |
|
55 |
| - switch (true) { |
56 |
| - case (navApp.indexOf("win") != -1): |
57 |
| - OSName = "win"; |
58 |
| - break; |
59 |
| - case (navApp.indexOf("mac") != -1): |
60 |
| - OSName = "mac"; |
61 |
| - break; |
62 |
| - case (navApp.indexOf("linux") != -1): |
63 |
| - OSName = "linux"; |
64 |
| - break; |
65 |
| - case (navApp.indexOf("x11") != -1): |
66 |
| - OSName = "linux"; |
67 |
| - break; |
68 |
| - } |
| 49 | + const setDownload = (innerText, release) => { |
| 50 | + download.innerText = innerText; |
| 51 | + download.setAttribute( |
| 52 | + "href", |
| 53 | + `${github.repo}/releases/latest/download/${release}` |
| 54 | + ); |
| 55 | + }; |
69 | 56 |
|
70 |
| - const download = document.querySelector("#download"); |
71 |
| - switch (OSName) { |
72 |
| - case "win": |
73 |
| - download.innerText = "Download for Windows"; |
74 |
| - download.setAttribute("href", `${github.repo}/releases/latest/download/${github.windows}`) |
75 |
| - break; |
76 |
| - case "mac": |
77 |
| - download.innerText = "Download for macOS"; |
78 |
| - download.setAttribute("href", `${github.repo}/releases/latest/download/${github.macos}`) |
79 |
| - break; |
80 |
| - case "linux": |
81 |
| - download.innerText = "Download for Linux"; |
82 |
| - download.setAttribute("href", `${github.repo}/releases/latest/download/${github.linux}`) |
83 |
| - break; |
84 |
| - } |
| 57 | + switch (true) { |
| 58 | + case navApp.indexOf("win") !== -1: |
| 59 | + setDownload("Download for Windows", github.windows); |
| 60 | + break; |
| 61 | + case navApp.indexOf("mac") !== -1: |
| 62 | + setDownload("Download for macOS", github.macos); |
| 63 | + break; |
| 64 | + case navApp.indexOf("linux") !== -1 || navApp.indexOf("x11") !== -1: |
| 65 | + setDownload("Download for Linux", github.linux); |
| 66 | + break; |
| 67 | + } |
85 | 68 | });
|
0 commit comments