Sort interfaces:

- interfaces that do not depend on others (and have no children) are placed at the top of the treeview
 - interfaces that do have children get them directly assigned below them

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER
2025-03-06 04:32:19 +01:00
parent 97d3a22559
commit 4ee9c83d9c

View File

@@ -28,7 +28,8 @@ $(function () {
gateways.add(inet6.gateway); gateways.add(inet6.gateway);
} }
var json = []; var interfaces = {};
var masterInterfaces = {};
// For each interface in data.interface, create a new object and push it to json // For each interface in data.interface, create a new object and push it to json
data.interfaces.forEach(function (interface) { data.interfaces.forEach(function (interface) {
@@ -46,21 +47,32 @@ $(function () {
const status = `<span class="${carrierColor}">${stateText}</span>`; const status = `<span class="${carrierColor}">${stateText}</span>`;
let master = null;
if (interface.master !== undefined) {
// Find interface.master in data.interfaces
master = data.interfaces.find(obj => obj.index === interface.master).name;
}
// Show an icon for indenting slave interfaces
const indentIcon = master === null ? "" : "<i class='fa fa-diagram-project fa-fw'></i> ";
var obj = { var obj = {
text: interface.name + " - " + status, text: indentIcon + interface.name + " - " + status,
class: gateways.has(interface.name) ? "text-bold" : null, class: gateways.has(interface.name) ? "text-bold" : null,
icon: "fa fa-network-wired fa-fw", icon: "fa fa-network-wired fa-fw",
nodes: [], nodes: [],
}; };
if (interface.master !== undefined) { if (master !== null) {
// Find interface.master in data.interfaces
const master = data.interfaces.find(obj => obj.index === interface.master);
if (master !== undefined) {
obj.nodes.push({ obj.nodes.push({
text: "Master interface: <code>" + utils.escapeHtml(master.name) + "</code>", text: "Master interface: <code>" + utils.escapeHtml(master) + "</code>",
icon: "fa fa-network-wired fa-fw", icon: "fa fa-network-wired fa-fw",
}); });
if (master in masterInterfaces) {
masterInterfaces[master].push(interface.name);
} else {
masterInterfaces[master] = [interface.name];
} }
} }
@@ -403,9 +415,37 @@ $(function () {
obj.nodes.push(furtherDetails); obj.nodes.push(furtherDetails);
} }
json.push(obj); interfaces[interface.name] = obj;
}); });
// Sort interfaces based on masterInterfaces. If an item is found in
// masterInterfaces, it should be placed after the master interface
const ifaces = Object.keys(interfaces);
const interfaceList = Object.keys(masterInterfaces);
// Add slave interfaces next to master interfaces
for (const master of interfaceList) {
if (master in masterInterfaces) {
for (const slave of masterInterfaces[master]) {
ifaces.splice(ifaces.indexOf(slave), 1);
interfaceList.splice(interfaceList.indexOf(master) + 1, 0, slave);
}
}
}
// Add interfaces that are not slaves at the top of the list (in reverse order)
for (const iface of ifaces.reverse()) {
if (!interfaceList.includes(iface)) {
interfaceList.unshift(iface);
}
}
// Build the tree view
const json = [];
for (const iface of interfaceList) {
json.push(interfaces[iface]);
}
$("#tree").bstreeview({ $("#tree").bstreeview({
data: json, data: json,
expandIcon: "fa fa-angle-down fa-fw", expandIcon: "fa fa-angle-down fa-fw",