|
| 1 | +import ejs from "ejs"; |
| 2 | +import { i18n } from "../localization"; |
| 3 | +import GUI, { TABS } from "../gui"; |
| 4 | +import { mspHelper } from "../msp/MSPHelper"; |
| 5 | +import MSP from "../msp"; |
| 6 | +import MSPCodes from "../msp/MSPCodes"; |
| 7 | +import $ from "jquery"; |
| 8 | + |
| 9 | +// Discover config sources (text-based) |
| 10 | +const templateFiles = import.meta.glob("../../norn-configs/template.ejs", { eager: true, as: "raw" }); |
| 11 | + |
| 12 | +const norn_config = { |
| 13 | + analyticsChanges: {}, |
| 14 | +}; |
| 15 | + |
| 16 | +norn_config.initialize = function (callback) { |
| 17 | + const self = this; |
| 18 | + |
| 19 | + GUI.active_tab = "norn_config"; |
| 20 | + |
| 21 | + function load_configuration_from_fc() { |
| 22 | + // Load any prerequisite data from FC, then load the HTML |
| 23 | + mspHelper.readFullConfiguration?.(() => { |
| 24 | + $("#content").load("./tabs/norn_config.html", on_tab_loaded_handler); |
| 25 | + }) || $("#content").load("./tabs/norn_config.html", on_tab_loaded_handler); |
| 26 | + } |
| 27 | + |
| 28 | + function update_ui() { |
| 29 | + i18n.localizePage(); |
| 30 | + |
| 31 | + // Populate Manticore models (allow None) with explicit options |
| 32 | + const manticoreSelect = $("select[name='norn_manticore']"); |
| 33 | + if (manticoreSelect.length) { |
| 34 | + manticoreSelect.empty(); |
| 35 | + manticoreSelect.append(`<option value="">${i18n.getMessage("nornNone")}</option>`); |
| 36 | + manticoreSelect.append(`<option value="uart">UART</option>`); |
| 37 | + manticoreSelect.append(`<option value="gpio">GPIO</option>`); |
| 38 | + manticoreSelect.on("change", function () { |
| 39 | + self.analyticsChanges["NornManticore"] = $(this).val() || null; |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + // Populate VTX profiles (allow None) with explicit options |
| 44 | + const vtxSelect = $("select[name='norn_vtx']"); |
| 45 | + if (vtxSelect.length) { |
| 46 | + vtxSelect.empty(); |
| 47 | + vtxSelect.append(`<option value="">${i18n.getMessage("nornNone")}</option>`); |
| 48 | + vtxSelect.append(`<option value="3.3_vtx">3.3GHz</option>`); |
| 49 | + vtxSelect.append(`<option value="5.8_vtx">5.8GHz</option>`); |
| 50 | + vtxSelect.append(`<option value="optica">Optica</option>`); |
| 51 | + vtxSelect.on("change", function () { |
| 52 | + self.analyticsChanges["NornVtx"] = $(this).val() || null; |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + // Example dropdown wiring |
| 57 | + const exampleSelect = $("select[name='norn_mode']"); |
| 58 | + if (exampleSelect.length) { |
| 59 | + exampleSelect.on("change", function () { |
| 60 | + const value = $(this).val(); |
| 61 | + self.analyticsChanges["NornMode"] = value; |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + // Example button wiring |
| 66 | + $("a.generate").on("click", on_generate_handler); |
| 67 | + } |
| 68 | + |
| 69 | + function on_tab_loaded_handler() { |
| 70 | + update_ui(); |
| 71 | + |
| 72 | + GUI.interval_add( |
| 73 | + "status_pull", |
| 74 | + function status_pull() { |
| 75 | + MSP.send_message(MSPCodes.MSP_STATUS); |
| 76 | + }, |
| 77 | + 500, |
| 78 | + true, |
| 79 | + ); |
| 80 | + |
| 81 | + GUI.content_ready(callback); |
| 82 | + } |
| 83 | + |
| 84 | + // No save/copy handlers for now (kept minimal per request) |
| 85 | + |
| 86 | + function readFileRaw(pathMap, path) { |
| 87 | + if (!path) return ""; |
| 88 | + try { |
| 89 | + return pathMap[path] || ""; |
| 90 | + } catch (e) { |
| 91 | + console.error("Cannot read", path, e); |
| 92 | + return ""; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + function getSelectedKeys() { |
| 97 | + const manticoreKey = $("select[name='norn_manticore']").val() || ""; |
| 98 | + const vtxKey = $("select[name='norn_vtx']").val() || ""; |
| 99 | + return { manticoreKey, vtxKey }; |
| 100 | + } |
| 101 | + |
| 102 | + function on_generate_handler(e) { |
| 103 | + e?.preventDefault?.(); |
| 104 | + |
| 105 | + const templatePath = Object.keys(templateFiles)[0]; |
| 106 | + let result; |
| 107 | + if (templatePath) { |
| 108 | + const tpl = readFileRaw(templateFiles, templatePath); |
| 109 | + result = ejs.render(tpl, getSelectedKeys()); |
| 110 | + } else { |
| 111 | + // Fallback to old behavior if template missing |
| 112 | + const parts = []; |
| 113 | + const genericPath = Object.keys(genericFiles)[0]; |
| 114 | + parts.push(readFileRaw(genericFiles, genericPath)); |
| 115 | + const manticorePath = $("select[name='norn_manticore']").val(); |
| 116 | + if (manticorePath) parts.push(readFileRaw(manticoreFiles, manticorePath)); |
| 117 | + const vtxPath = $("select[name='norn_vtx']").val(); |
| 118 | + if (vtxPath) parts.push(readFileRaw(vtxFiles, vtxPath)); |
| 119 | + result = parts.filter(Boolean).join("\n\n").trim(); |
| 120 | + } |
| 121 | + |
| 122 | + $("#norn_config_output").val(result); |
| 123 | + } |
| 124 | + |
| 125 | + load_configuration_from_fc(); |
| 126 | +}; |
| 127 | + |
| 128 | +norn_config.cleanup = function (callback) { |
| 129 | + if (callback) callback(); |
| 130 | +}; |
| 131 | + |
| 132 | +TABS.norn_config = norn_config; |
| 133 | +export { norn_config }; |
0 commit comments