"use strict"; /* * NMS info-window controller * * Interface: nmsInfoBox.showWindow(windowType,optionalParameter), nmsInfoBox.hide() * * Uses a basic hierarchy of window > views > panels, where each panel can work * independently, but gets loaded/unloaded when needed by window or view. * */ /* * * Currently broken or needs reimplementing: * - Handler unloading is not working correctly, and many are never removed * - SSH-management link, this should propably be a custom "view" of sorts * * General TODO: * - Move inventory into a separate tab. * - Add external windows (timetravel, etc) * - Take a critical look at what methods/variables should be marked as "_" * - Currently argument is assumed to be a switch, this should not be the case * - Add some basic styling to separate panels visually when in the same view * */ /* * Basic configuration */ var nmsInfoBox = nmsInfoBox || { stats: {}, _container: false, //Container window _windowHandler: false, //Window handler _sw: false, //Name of last switch opened, used for toggle-click _windowTypes: [ { id: "switchInfo", title: "Switch info", views: { initial: { name: "Summary", panels: ["switchLatency", "switchSummary", "switchLinks", "switchComments"], public: true, }, ports: { name: "Ports", panels: ["switchPorts"], }, misc: { name: "SNMP", panels: ["switchSNMP:misc"], }, edit: { name: "Settings", panels: ["switchEdit"], }, }, }, { id: "networkInfo", title: "Network info", views: { initial: { name: "Summary", panels: ["networkSummary"], }, edit: { name: "Settings", panels: ["networkEdit"], }, }, }, { id: "addSwitch", title: "Add new switch(es)", views: { initial: { name: "Add switch", panels: ["switchAdd"], }, }, }, { id: "addNetwork", title: "Add new network(s)", views: { initial: { name: "Add network", panels: ["networkAdd"], }, }, }, { id: "searchHelp", title: "Search help", views: { initial: { name: "Search help", panels: ["searchHelp"], }, }, }, { id: "searchResults", title: "Search Results", views: { initial: { name: "Search Results", panels: ["searchResults"], }, }, }, { id: "listNetwork", title: "Networks", views: { initial: { name: "List all networks", panels: ["listNetworks"], }, }, }, { id: "debug", title: "Debug", views: { initial: { name: "Debug", panels: ["debugInfo"], }, stats: { name: "Stats", panels: ["debugStats"], }, }, }, ], _panelTypes: {}, //Populate by using the nmsInfoBox.addPanelType method }; /* * Shows a window, and triggers initial load if needed */ nmsInfoBox.showWindow = function (windowName, argument) { if (windowName == "switchInfo" && argument != "" && argument == this._sw) { nmsInfoBox.hide(); return; } if (!this._container) this._load(); if (!windowName) windowName = "switchInfo"; this._sw = argument; this._windowHandler.showWindow(windowName, argument); }; /* * Internal function to load and register the initial html objects */ nmsInfoBox._load = function () { var modalBox = document.createElement("div"); modalBox.classList.add("modal", "show"); modalBox.style = "display: block;"; // did not work // modalBox.setAttribute('data-bs-backdrop', "false"); var dialogBox = document.createElement("div"); dialogBox.classList.add( "modal-dialog", "modal-lg", "modal-dialog-scrollable" ); var dialogContentBox = document.createElement("div"); dialogContentBox.classList.add("modal-content"); var dialogHeader = document.createElement("div"); dialogHeader.classList.add("modal-header"); modalBox.appendChild(dialogBox); dialogBox.appendChild(dialogContentBox); var title = document.createElement("div"); title.id = "info-box-title"; title.classList.add("modal-title"); var nav = document.createElement("div"); nav.id = "info-box-nav"; var body = document.createElement("div"); body.id = "info-box-body"; body.classList.add("modal-body"); var closeButton = document.createElement("button"); // closeButton.classList.add("btn-close"); closeButton.onclick = function () { nmsInfoBox.hide(); }; dialogContentBox.appendChild(dialogHeader); dialogContentBox.appendChild(body); dialogHeader.appendChild(title); dialogHeader.appendChild(nav); dialogHeader.appendChild(closeButton); this._container = document.getElementById("info-box-container"); this._container.appendChild(modalBox); this._windowHandler = new windowHandler(); this._windowHandler.setContainerObj( document.getElementById("info-box-container") ); this._windowHandler.setTitleObj(document.getElementById("info-box-title")); this._windowHandler.setBodyObj(document.getElementById("info-box-body")); this._windowHandler.setNavObj(document.getElementById("info-box-nav")); this._windowHandler.setPanelTypes(this._panelTypes); this._windowHandler.setWindowTypes(this._windowTypes); }; /* * Adds a panel type to _panelTypes for usage in windows and views */ nmsInfoBox.addPanelType = function (id, obj) { this._panelTypes[id] = obj; }; /* * Hide the active window and tell it to unload */ nmsInfoBox.hide = function () { this._sw = false; if ( this._windowHandler != undefined && this._windowHandler.hide != undefined ) { this._windowHandler.hide(); this._windowHandler.unloadWindow(); } }; /* * Click a switch and display it. */ nmsInfoBox.click = function (sw) { this.showWindow("switchInfo", sw); }; /* * Window handler * * Is based on a hierarchy of objects: Window (itself) > Views > Panels. Where * any object should not interact with any other directly. Panels are special * nmsInfoPanel-objects that handle their own rendering, refreshing, etc. The * window handler only makes sure these panels are loaded and unloaded when * needed in a window or view. * * Does primarily rely on an imported list of panel types and window types to * display stuff, but has methods for manual overrides if needed. * * Panels can use the doInPanel(panelId,functionName,arguments) method to pass * actions to themselves if needed. * */ var windowHandler = function () { this.containerObj = false; this.titleObj = false; this.navObj = false; this.bodyObj = false; this._panels = {}; this._view = ""; this._viewId = {}; this._window = {}; this.windowTypes = false; this.panelTypes = false; this.argument = false; this.show = function () { this.containerObj.classList.remove("d-none"); }; this.hide = function () { this.containerObj.classList.add("d-none"); }; this.setContainerObj = function (containerObj) { this.containerObj = containerObj; }; this.setTitleObj = function (titleObj) { this.titleObj = titleObj; }; this.getTitleObj = function (titleObj) { return this.titleObj; }; this.setNavObj = function (navObj) { this.navObj = navObj; }; this.setBodyObj = function (bodyObj) { this.bodyObj = bodyObj; }; this.setPanelTypes = function (panelTypes) { this.panelTypes = panelTypes; }; this.setWindowTypes = function (windowTypes) { this.windowTypes = {}; for (var i in windowTypes) { this.windowTypes[windowTypes[i].id] = windowTypes[i]; } }; this.setArgument = function (argument) { if (this.argument != argument) { this.argument = argument; this.showView(this._view); } }; this.showPanel = function (panelName) { var panelArray = panelName.split(":"); var panelName = panelArray[0]; var panelMode = panelArray[1]; if (this.panelTypes[panelName]) { var id = (Math.random().toString(36) + "00000000000000000").slice( 2, 10 + 2 ); var panel = new this.panelTypes[panelName](); panel.setId(id); if (!!panelMode) panel.setMode(panelMode); panel.load(this.bodyObj, this.argument); this._panels[id] = panel; } }; this.showTitle = function (title) { this.titleObj.innerHTML = '

' + title + "

"; }; this.showNav = function () { if (!this._window.views) this.navObj.innerHTML = ""; var output = '