|
| 1 | +function generateHTML(devicesJson) { |
| 2 | + let html = ` |
| 3 | +<table style="font-size: 14px !important;"> |
| 4 | + <tbody>`; |
| 5 | + let first = true; |
| 6 | + for (const generation in devicesJson) { |
| 7 | + let devices = devicesJson[generation]; |
| 8 | + if (devices.isExperimental === true && !window.hasOwnProperty("showExperimental")) continue; |
| 9 | + devices.devices.forEach((device) => { |
| 10 | + //set defaults |
| 11 | + if (device.windows === undefined) |
| 12 | + device.windows = devices.default_windows; |
| 13 | + if (device.linux === undefined) device.linux = devices.default_linux; |
| 14 | + if (device.mac === undefined) device.mac = devices.default_mac; |
| 15 | + if (device.wpMethod === undefined) |
| 16 | + device.wpMethod = devices.default_wpmethod; |
| 17 | + if (device.fullrom === undefined) |
| 18 | + device.fullrom = devices.default_fullrom; |
| 19 | + if (device.rwLegacy === undefined) |
| 20 | + device.rwLegacy = devices.default_rwLegacy; |
| 21 | + }); |
| 22 | + if (first) { |
| 23 | + first = false; |
| 24 | + } else { |
| 25 | + html += ` |
| 26 | + <tr> |
| 27 | + <td colspan="8"></td> |
| 28 | + </tr>`; |
| 29 | + } |
| 30 | + html += ` |
| 31 | + <tr> |
| 32 | + <th colspan="8" style="text-align:left;"> <i>${generation}</i></th> |
| 33 | + </tr> |
| 34 | + <tr> |
| 35 | + <th scope="col"> Device Name</th> |
| 36 | + <th scope="col"> Board Name</th> |
| 37 | + <th scope="col"> MrChromebox Firmware</th> |
| 38 | + <th scope="col"> Windows Notes</th> |
| 39 | + <th scope="col"> Linux Notes</th> |
| 40 | + <th scope="col"> MacOS Notes</th> |
| 41 | + </tr>`; |
| 42 | + |
| 43 | + let windows; |
| 44 | + let linux; |
| 45 | + let mac; |
| 46 | + |
| 47 | + devices.devices.forEach((device, index) => { |
| 48 | + let devicename = device.device.join("<br>"); |
| 49 | + let has_firmware = (device.fullrom || device.rwLegacy) ? "✅" : ""; |
| 50 | + |
| 51 | + let win_out = ""; |
| 52 | + let linux_out = ""; |
| 53 | + let mac_out = ""; |
| 54 | + if (windows !== device.windows) { |
| 55 | + let length = 0; |
| 56 | + windows = device.windows; |
| 57 | + for (let i = index; i < devices.devices.length; i++) { |
| 58 | + if (devices.devices[i].windows === windows) length++; |
| 59 | + else break; |
| 60 | + } |
| 61 | + win_out = `\n <td rowspan="${length}" style=\"text-align:center;\">${windows}</td>`; |
| 62 | + } |
| 63 | + if (linux !== device.linux) { |
| 64 | + let length = 0; |
| 65 | + linux = device.linux; |
| 66 | + for (let i = index; i < devices.devices.length; i++) { |
| 67 | + if (devices.devices[i].linux === linux) length++; |
| 68 | + else break; |
| 69 | + } |
| 70 | + if (!linux) linux = devices.default_linux; |
| 71 | + linux_out = `\n <td rowspan="${length}" style=\"text-align:center;\">${linux}</td>`; |
| 72 | + } |
| 73 | + if (mac !== device.mac) { |
| 74 | + let length = 0; |
| 75 | + mac = device.mac; |
| 76 | + for (let i = index; i < devices.devices.length; i++) { |
| 77 | + if (devices.devices[i].mac === mac) length++; |
| 78 | + else break; |
| 79 | + } |
| 80 | + if (!mac) mac = devices.default_mac; |
| 81 | + mac_out = `\n <td rowspan="${length}" style=\"text-align:center;\">${mac}</td>`; |
| 82 | + } |
| 83 | + |
| 84 | + html += ` |
| 85 | + <tr> |
| 86 | + <td>${devicename}</td> |
| 87 | + <td style="text-align:center;"> ${device.boardname}</td> |
| 88 | + <td style="text-align:center;"> ${has_firmware}</td> |
| 89 | + ${win_out}${linux_out}${mac_out} |
| 90 | + </tr>`; |
| 91 | + }); |
| 92 | + } |
| 93 | + html += ` |
| 94 | + </tbody> |
| 95 | +</table>`; |
| 96 | + return html; |
| 97 | +} |
| 98 | + |
| 99 | +(async () => { |
| 100 | + const table = document.querySelector(".deviceTable"); |
| 101 | + const searchbox = document.querySelector(".deviceSearch"); |
| 102 | + let devices = []; |
| 103 | + try { |
| 104 | + devices = JSON.parse(await (await fetch("../../devices.json")).text()); |
| 105 | + } catch(e) { |
| 106 | + console.warn(e); |
| 107 | + searchbox.parentElement.remove(); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + table.innerHTML = generateHTML(JSON.parse(JSON.stringify(devices))); |
| 112 | + |
| 113 | + function search(keyword) { |
| 114 | + let dv = JSON.parse(JSON.stringify(devices)); |
| 115 | + keyword = keyword.toLowerCase().trim(); |
| 116 | + if (keyword === "show hidden!") { |
| 117 | + searchbox.value = ""; |
| 118 | + keyword = ""; |
| 119 | + window.showExperimental = true; |
| 120 | + } |
| 121 | + if (!keyword) { |
| 122 | + table.innerHTML = generateHTML(dv); |
| 123 | + return; |
| 124 | + } |
| 125 | + for (const k in dv) { |
| 126 | + for (let i=0; i<dv[k].devices.length; i++) { |
| 127 | + let hasTerm = dv[k].devices[i].device.filter(e => e.toLowerCase().includes(keyword)).length !== 0 || dv[k].devices[i].boardname.toLowerCase().includes(keyword); |
| 128 | + if (!hasTerm) { |
| 129 | + dv[k].devices.splice(i, 1); |
| 130 | + i--; |
| 131 | + } |
| 132 | + } |
| 133 | + if (dv[k].devices.length === 0) { |
| 134 | + delete dv[k]; |
| 135 | + } |
| 136 | + } |
| 137 | + if (Object.keys(dv).length === 0) { |
| 138 | + table.innerHTML = ""; |
| 139 | + table.innerText = "Device not found. Did you make a typo?"; |
| 140 | + return; |
| 141 | + } |
| 142 | + table.innerHTML = generateHTML(dv); |
| 143 | + } |
| 144 | + searchbox.addEventListener("keyup", (e) => search(e.target.value)); |
| 145 | +})(); |
0 commit comments