Skip to content

Commit 90520dc

Browse files
fix: restore custom menu functionality after refactoring
- Initialize this.customMenu in node_helper start() - Create dedicated nav element for custom menus in createDynamicMenu() - Add dynamic menu title support in updateHeaderTitle() - Copy custom_menu.example.json to config directory in demo setup The custom menu feature was broken after refactoring because items were inserted into the wrong nav container and customMenu object was not initialized, causing menu items to not display correctly.
1 parent 8f3dc99 commit 90520dc

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

demo.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ let config = {
9292
position: "bottom_right",
9393
config: {
9494
secureEndpoints: false,
95+
customMenu: "custom_menu.example.json",
9596
classes: {
9697
"Toggle Weather": {
9798
toggle: ["weather"]

node_helper.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ module.exports = NodeHelper.create({
7272
/* API EXTENSION - Added v2.0.0 */
7373
this.externalApiRoutes = {};
7474
this.moduleApiMenu = {};
75+
this.customMenu = {};
7576
},
7677

7778
stop () {
@@ -1281,6 +1282,7 @@ module.exports = NodeHelper.create({
12811282
}
12821283
fs.readFile(menuPath, (error, data) => {
12831284
if (error) {
1285+
Log.error(`Error reading custom menu: ${error}`);
12841286
return;
12851287
} else {
12861288
this.customMenu = {...this.customMenu, ...JSON.parse(this.translate(data.toString()))};

remote.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,13 @@ const Remote = {
464464
titleText = classesButton?.querySelector(".text")?.textContent || this.translate("TITLE");
465465
}
466466

467+
// For custom/dynamic menus, use button text
468+
if (!titleText && key.endsWith("-menu")) {
469+
const buttonId = key.replace("-menu", "-button");
470+
const button = document.getElementById(buttonId);
471+
titleText = button?.querySelector(".text")?.textContent;
472+
}
473+
467474
if (titleText) {
468475
headerTitleElement.textContent = titleText;
469476
}
@@ -2083,7 +2090,29 @@ const Remote = {
20832090
globalThis.location.hash = "main-menu";
20842091
}
20852092
}
2086-
this.createMenuElement(content, "main", document.querySelector("#alert-button"));
2093+
2094+
// Create button in main menu
2095+
const button = this.createMenuElement(content, "main", document.querySelector("#alert-button"));
2096+
2097+
// Create dedicated nav element for this menu
2098+
if (button && content.items && content.items.length > 0) {
2099+
const mainElement = document.querySelector("main");
2100+
const nav = document.createElement("nav");
2101+
nav.className = `menu-nav menu-element hidden ${content.id}-menu`;
2102+
2103+
// Insert nav after other nav elements
2104+
const lastNav = mainElement.querySelector("nav:last-of-type");
2105+
if (lastNav) {
2106+
lastNav.parentNode.insertBefore(nav, lastNav.nextSibling);
2107+
} else {
2108+
mainElement.append(nav);
2109+
}
2110+
2111+
// Create menu items inside the new nav
2112+
for (const item of content.items) {
2113+
this.createMenuElement(item, content.id, nav.lastChild || nav);
2114+
}
2115+
}
20872116
}
20882117
};
20892118

0 commit comments

Comments
 (0)