|
1 | | -// Burger menus |
2 | | -document.addEventListener('DOMContentLoaded', function () { |
3 | | - // open |
4 | | - const burger = document.querySelectorAll('.navbar-burger'); |
5 | | - const menu = document.querySelectorAll('.navbar-menu'); |
| 1 | +const download = document.querySelector("#download"); |
6 | 2 |
|
7 | | - if (burger.length && menu.length) { |
8 | | - for (var i = 0; i < burger.length; i++) { |
9 | | - burger[i].addEventListener('click', function () { |
10 | | - for (var j = 0; j < menu.length; j++) { |
11 | | - menu[j].classList.toggle('hidden'); |
12 | | - } |
13 | | - }); |
14 | | - } |
15 | | - } |
| 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 | +version: 'v0.2.1', |
| 7 | +windows: 'Lapce-windows.msi', |
| 8 | +linux: 'Lapce-linux.tar.gz', |
| 9 | +macos: 'Lapce-macos.dmg' |
| 10 | +} |
16 | 11 |
|
| 12 | +// 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 |
17 | 13 |
|
18 | | - // close |
19 | | - const close = document.querySelectorAll('.navbar-close'); |
20 | | - const backdrop = document.querySelectorAll('.navbar-backdrop'); |
| 14 | +// open |
| 15 | +const burger = document.querySelectorAll('.navbar-burger'); |
| 16 | +const menu = document.querySelectorAll('.navbar-menu'); |
21 | 17 |
|
22 | | - if (close.length) { |
23 | | - for (var i = 0; i < close.length; i++) { |
24 | | - close[i].addEventListener('click', function () { |
25 | | - for (var j = 0; j < menu.length; j++) { |
26 | | - menu[j].classList.toggle('hidden'); |
27 | | - } |
28 | | - }); |
29 | | - } |
30 | | - } |
| 18 | +if (burger.length && menu.length) { |
| 19 | + for (let i = 0; i < burger.length; i++) { |
| 20 | + burger[i].addEventListener('click', () => { |
| 21 | + for (let j = 0; j < menu.length; j++) { |
| 22 | + menu[j].classList.toggle('hidden'); |
| 23 | + } |
| 24 | + }); |
| 25 | + } |
| 26 | +} |
31 | 27 |
|
32 | | - if (backdrop.length) { |
33 | | - for (var i = 0; i < backdrop.length; i++) { |
34 | | - backdrop[i].addEventListener('click', function () { |
35 | | - for (var j = 0; j < menu.length; j++) { |
36 | | - menu[j].classList.toggle('hidden'); |
37 | | - } |
38 | | - }); |
39 | | - } |
40 | | - } |
| 28 | +// close |
| 29 | +const close = document.querySelectorAll('.navbar-close'); |
| 30 | +const backdrop = document.querySelectorAll('.navbar-backdrop'); |
41 | 31 |
|
42 | | - var OSName = "mac"; |
43 | | - const navApp = navigator.userAgent.toLowerCase(); |
44 | | - switch (true) { |
45 | | - case (navApp.indexOf("win") != -1): |
46 | | - OSName = "win"; |
47 | | - break; |
48 | | - case (navApp.indexOf("mac") != -1): |
49 | | - OSName = "mac"; |
50 | | - break; |
51 | | - case (navApp.indexOf("linux") != -1): |
52 | | - OSName = "linux"; |
53 | | - break; |
54 | | - case (navApp.indexOf("x11") != -1): |
55 | | - OSName = "linux"; |
56 | | - break; |
57 | | - } |
58 | | - const download = document.getElementById("download"); |
59 | | - switch (OSName) { |
60 | | - case "win": |
61 | | - download.innerText = "Download for Windows"; |
62 | | - download.setAttribute("href", "https://github.com/lapce/lapce/releases/download/v0.2.1/Lapce-windows.msi") |
63 | | - break; |
64 | | - case "mac": |
65 | | - download.innerText = "Download for macOS"; |
66 | | - download.setAttribute("href", "https://github.com/lapce/lapce/releases/download/v0.2.1/Lapce-macos.dmg") |
67 | | - break; |
68 | | - case "linux": |
69 | | - download.innerText = "Download for Linux"; |
70 | | - download.setAttribute("href", "https://github.com/lapce/lapce/releases/download/v0.2.1/Lapce-linux.tar.gz") |
71 | | - break; |
72 | | - } |
73 | | -}); |
| 32 | +if (close.length) { |
| 33 | + for (let i = 0; i < close.length; i++) { |
| 34 | + close[i].addEventListener('click', () => { |
| 35 | + for (let j = 0; j < menu.length; j++) { |
| 36 | + menu[j].classList.toggle('hidden'); |
| 37 | + } |
| 38 | + }); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +if (backdrop.length) { |
| 43 | + for (let i = 0; i < backdrop.length; i++) { |
| 44 | + backdrop[i].addEventListener('click', () => { |
| 45 | + for (let j = 0; j < menu.length; j++) { |
| 46 | + menu[j].classList.toggle('hidden'); |
| 47 | + } |
| 48 | + }); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// 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 |
| 53 | +let OSName = "mac"; |
| 54 | +const navApp = navigator.userAgent.toLowerCase(); |
| 55 | + |
| 56 | +switch (true) { |
| 57 | + case (navApp.indexOf("win") != -1): |
| 58 | + OSName = "win"; |
| 59 | + break; |
| 60 | + case (navApp.indexOf("mac") != -1): |
| 61 | + OSName = "mac"; |
| 62 | + break; |
| 63 | + case (navApp.indexOf("linux") != -1): |
| 64 | + OSName = "linux"; |
| 65 | + break; |
| 66 | + case (navApp.indexOf("x11") != -1): |
| 67 | + OSName = "linux"; |
| 68 | + break; |
| 69 | +} |
| 70 | + |
| 71 | +switch (OSName) { |
| 72 | + case "win": |
| 73 | + download.innerText = "Download for Windows"; |
| 74 | + download.setAttribute("href", `${github.repo}/releases/download/${github.version}/${github.windows}`) |
| 75 | + break; |
| 76 | + case "mac": |
| 77 | + download.innerText = "Download for macOS"; |
| 78 | + download.setAttribute("href", `${github.repo}/releases/download/${github.version}/${github.macos}`) |
| 79 | + break; |
| 80 | + case "linux": |
| 81 | + download.innerText = "Download for Linux"; |
| 82 | + download.setAttribute("href", `${github.repo}/releases/download/${github.version}/${github.linux}`) |
| 83 | + break; |
| 84 | +} |
0 commit comments