"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
* - Add some movement/placement/size options to info window
*
*/
/*
* 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': ['switchSummary','switchComments']
},
'ports': {
'name': 'SNMP - Ports',
'panels': ['switchSNMP:ports']
},
'misc': {
'name': 'SNMP - Misc',
'panels': ['switchSNMP:misc']
},
'details': {
'name': 'Settings',
'panels': ['switchDetails']
},
'edit': {
'name': 'Edit',
'panels': ['switchEdit']
}
}
},
{
'id': 'addSwitch',
'title': 'Add new switch(es)',
'views': {
'initial': {
'name': 'Add switch',
'panels': ['switchAdd']
}
}
},
{
'id': 'searchHelp',
'title': 'Search help',
'views': {
'initial': {
'name': 'Search help',
'panels': ['searchHelp']
}
}
},
{
'id': 'inventoryListing',
'title': 'Inventory listing',
'views': {
'initial': {
'name': 'Distro names',
'panels': ['inventoryListing:distro_name']
},
'sysDescr': {
'name': 'System description',
'panels': ['inventoryListing:sysDescr']
},
'jnxBoxSerialNo': {
'name': 'Serial numbers',
'panels': ['inventoryListing:jnxBoxSerialNo']
}
}
}
],
_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)
this._container.style.display = "block";
};
/*
* Internal function to load and register the initial html objects
*/
nmsInfoBox._load = function() {
var infoBox = document.createElement("div");
infoBox.classList.add("panel", "panel-default");
var title = document.createElement("div");
title.id = "info-box-title";
title.classList.add("panel-heading");
var nav = document.createElement("div");
nav.id = "info-box-nav";
nav.classList.add("panel-body");
var body = document.createElement("div");
body.id = "info-box-body";
body.classList.add("panel-body");
infoBox.appendChild(title);
infoBox.appendChild(nav);
infoBox.appendChild(body);
this._container = document.getElementById("info-box-container");
this._container.appendChild(infoBox);
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._window = {};
this.windowTypes = false;
this.panelTypes = false;
this.argument = false;
this.show = function () {
this.containerObj.classList.remove("hidden");
};
this.hide = function () {
this.containerObj.classList.add("hidden");
};
this.setContainerObj = function (containerObj) {
this.containerObj = containerObj;
};
this.setTitleObj = function (titleObj) {
this.titleObj = 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 = '
';
var i = 0;
for(var view in this._window.views) {
var viewObj = this._window.views[view];
var active = '';
if(this._view == view)
active = ' class="active" ';
output += '