From 727e4ab31aa6d1a754711d4cd29dbcefae2e952a Mon Sep 17 00:00:00 2001 From: Kristian Lyngstol Date: Mon, 21 Mar 2016 20:11:55 +0100 Subject: NMS: NMS Public --- web/nms-public.gathering.org/js/nms-info-box.js | 521 ++++++++++++++++++++++++ 1 file changed, 521 insertions(+) create mode 100644 web/nms-public.gathering.org/js/nms-info-box.js (limited to 'web/nms-public.gathering.org/js/nms-info-box.js') diff --git a/web/nms-public.gathering.org/js/nms-info-box.js b/web/nms-public.gathering.org/js/nms-info-box.js new file mode 100644 index 0000000..5fdf044 --- /dev/null +++ b/web/nms-public.gathering.org/js/nms-info-box.js @@ -0,0 +1,521 @@ +"use strict"; + +/* + * NMS info window controller + * + * Interface: nmsInfoBox.showWindow(windowType,optionalParameter), nmsInfoBox.hide(), nmsInfoBox.refresh() + * + * Any windowTypes should at a minimum implement load, unload, getTitle, getContent, getChildContent + * + * TODO: Implement useful update methods on windowTypes + * + */ + +var nmsInfoBox = nmsInfoBox || { + stats: {}, + _container: false, //Container window + _window: false, //Active window (reference to _windowTypes object or false) + _windowTypes: [], //List of all avaliable window types +} + +/* + * Shows a window from the _windowTypes list + */ +nmsInfoBox.showWindow = function (windowName,argument) { + if(windowName == "switchInfo" && argument != '' && argument == this._window.sw) { + nmsInfoBox.hide(); + return; + } + nmsInfoBox.hide(); + for(var win in this._windowTypes) { + if(windowName == win) { + this._window = this._windowTypes[win]; + this._show(argument); + return; + } + } +} + +/* + * Refresh the active window + * + * Todo: Could use a less aggressive refresh that doesn't hide-show everything + * + */ +nmsInfoBox.refresh = function() { + nmsInfoBox._show(); +} + +/* + * Internal function to show the active _window and pass along any arguments + */ +nmsInfoBox._show = function(argument) { + nmsData.addHandler("comments","switchshower",nmsInfoBox.update,argument); + nmsData.addHandler("switches","switchshower",nmsInfoBox.update,argument); + nmsData.addHandler("smanagement","switchshower",nmsInfoBox.update,argument); + nmsData.addHandler("snmp","switchshower",nmsInfoBox.update,argument); + + this._window.load(argument); + + this._container = document.getElementById("info-panel-container"); + var panel = document.createElement("div"); + panel.classList.add("panel", "panel-default"); + var title = document.createElement("div"); + title.classList.add("panel-heading"); + var body = document.createElement("div"); + body.classList.add("panel-body"); + + title.innerHTML = this._window.getTitle() + ''; + var content = this._window.getContent(); + if(!content.nodeName) { + body.innerHTML = this._window.content; + } else { + body.appendChild(content); + } + var childContent = this._window.getChildContent(); + if(childContent != false) { + body.appendChild(childContent); + } + + panel.appendChild(title); + panel.appendChild(body); + while(this._container.firstChild) { + this._container.removeChild(this._container.firstChild); + } + this._container.appendChild(panel); + this._container.style.display = "block"; +} + +/* + * Hide the active window and tell it to unload + */ +nmsInfoBox.hide = function() { + if(this._container == false || this._window == false) + return; + this._container.style.display = "none"; + this._window.unload(); + this._window = false; + nmsData.unregisterHandler("comments","switchshower"); + nmsData.unregisterHandler("switches","switchshower"); + nmsData.unregisterHandler("smanagement","switchshower"); + nmsData.unregisterHandler("snmp","switchshower"); +} + +/* + * Window type: Add Switch + * + * Basic window that lets you create a new switch + * + */ +nmsInfoBox._windowTypes.addSwitch = { + title: 'Add new switch', + content: '', + childContent: false, + getTitle: function() { + return this.title; + }, + getContent: function() { + return this.content; + }, + getChildContent: function() { + return this.childContent; + }, + load: function(argument) { + }, + unload: function() { + }, + save: function() { + var sysname = document.getElementById('create-sysname').value; + var myData = JSON.stringify([{'sysname':sysname}]); + $.ajax({ + type: "POST", + url: "/api/private/switch-add", + dataType: "text", + data:myData, + success: function (data, textStatus, jqXHR) { + var result = JSON.parse(data); + if(result.switches_addded.length > 0) { + nmsInfoBox.hide(); + } + nmsData.invalidate("switches"); + nmsData.invalidate("smanagement"); + } + }); + } +}; + +/* + * Window type: Switch info + * + * Advanced window with information about a specific switch, and basic editing options + * + * Custom interfaces: showComments, showSNMP, showEdit, save + * + */ +nmsInfoBox._windowTypes.switchInfo = { + title: '', + content: '', + childContent: false, + sw: '', + swi: '', + swm: '', + load: function(sw) { + if(sw) { + this.sw = sw; + } + this.swi = nmsData.switches["switches"][this.sw]; + try { + this.swm = nmsData.smanagement.switches[this.sw]; + } catch(e) { + this.swm = []; + } + + + var content = []; + + for (var v in this.swi) { + if (v == "placement") { + var place = JSON.stringify(this.swi[v]); + content.push([v,place]); + continue; + } + content.push([v, this.swi[v]]); + } + + for (var v in this.swm) { + content.push([v, this.swm[v]]); + } + content.sort(); + + var infotable = nmsInfoBox._makeTable(content); + infotable.id = "info-switch-table"; + + this.content = infotable; + + }, + getTitle: function() { + return ' ' + this.sw + ''; + }, + getContent: function() { + return this.content; + }, + getChildContent: function() { + return this.childContent; + }, + showComments: function() { + var domObj = document.createElement("div"); + var comments = []; + if (nmsData.comments.comments != undefined && nmsData.comments.comments[this.sw] != undefined) { + for (var c in nmsData.comments.comments[this.sw]["comments"]) { + var comment = nmsData.comments.comments[this.sw]["comments"][c]; + if (comment["state"] == "active" || comment["state"] == "persist" || comment["state"] == "inactive") { + comments.push(comment); + } + } + } + + if (comments.length > 0) { + var commenttable = nmsInfoBox._makeCommentTable(comments); + commenttable.id = "info-switch-comments-table"; + domObj.appendChild(commenttable); + $(function () { $('[data-toggle="popover"]').popover({placement:"top",continer:'body'}) }) + } + var commentbox = document.createElement("div"); + commentbox.id = "commentbox"; + commentbox.className = "panel-body"; + commentbox.style.width = "100%"; + commentbox.innerHTML = '
'; + domObj.appendChild(commentbox); + + this.childContent = domObj; + nmsInfoBox.refresh(); + }, + showEdit: function() { + var domObj = document.createElement("div"); + var template = {}; + + nmsInfoBox._editValues = {}; + for (var v in this.swi) { + if (v == "placement") { + var place = JSON.stringify(this.swi[v]); + template[v] = place; + continue; + } + template[v] = nmsInfoBox._nullBlank(this.swi[v]); + } + for (var v in this.swm) { + template[v] = nmsInfoBox._nullBlank(this.swm[v]); + } + var content = []; + for (v in template) { + var tmpsw = '\'' + this.sw + '\''; + var tmpv = '\'' + v + '\''; + var tmphandler = '"nmsInfoBox._editChange(' + tmpsw + ',' + tmpv + ');"'; + var html = "'; + content.push([v, html]); + } + + var table = nmsInfoBox._makeTable(content, "edit"); + domObj.appendChild(table); + + var submit = document.createElement("button"); + submit.innerHTML = "Save changes"; + submit.classList.add("btn", "btn-primary"); + submit.id = "edit-submit-" + this.sw; + submit.onclick = function(e) { nmsInfoBox._windowTypes.switchInfo.save(); }; + domObj.appendChild(submit); + + var output = document.createElement("output"); + output.id = "edit-output"; + domObj.appendChild(output); + + if (place) { + var pval = document.getElementById("edit-" + this.sw + "-placement"); + if (pval) { + pval.value = place; + } + } + + this.childContent = domObj; + nmsInfoBox.refresh(); + }, + showSNMP: function(tree) { + var domObj = document.createElement("div"); + + var output = document.createElement("output"); + output.id = "edit-output"; + output.style = "white-space: pre;"; + try { + output.value = JSON.stringify(nmsData.snmp.snmp[this.sw][tree],null,4); + } catch(e) { + output.value = "(no recent data (yet)?)"; + } + domObj.appendChild(output); + + this.childContent = domObj; + nmsInfoBox.refresh(); + }, + unload: function() { + this.childContent = false; + }, + save: function() { + var myData = nmsInfoBox._editStringify(this.sw); + console.log(myData); + $.ajax({ + type: "POST", + url: "/api/private/switch-update", + dataType: "text", + data:myData, + success: function (data, textStatus, jqXHR) { + nmsData.invalidate("switches"); + nmsData.invalidate("smanagement"); + } + }); + nmsInfoBox.hide(); + } +}; + +/* + * Click a switch and display it + * it. + */ +nmsInfoBox.click = function(sw) +{ + this.showWindow("switchInfo",sw); +} + +/* + * General-purpose table-maker? + * + * Takes an array of arrays as input, and an optional caption. + * + * E.g.: _makeTable([["name","Kjell"],["Age","five"]], "Age list"); + */ +nmsInfoBox._makeTable = function(content, caption) { + var table = document.createElement("table"); + var tr; + var td1; + var td2; + table.className = "table"; + table.classList.add("table"); + table.classList.add("table-condensed"); + if (caption != undefined) { + var cap = document.createElement("caption"); + cap.textContent = caption; + table.appendChild(cap); + } + for (var v in content) { + tr = table.insertRow(-1); + td1 = tr.insertCell(0); + td2 = tr.insertCell(1); + td1.innerHTML = content[v][0]; + td2.innerHTML = content[v][1]; + } + return table; +} + +/* + * Create and return a table for comments. + * + * Input is an array of comments. + */ +nmsInfoBox._makeCommentTable = function(content) { + var table = document.createElement("table"); + table.className = "table"; + table.classList.add("table"); + table.classList.add("table-condensed"); + var cap = document.createElement("caption"); + cap.textContent = "Comments" + table.appendChild(cap); + for (var commentid in content) { + var tr; + var td1; + var td2; + var comment = content[commentid]; + var col; + if (comment["state"] == "active") + col = "danger"; + else if (comment["state"] == "inactive") + col = false; + else + col = "info"; + tr = table.insertRow(-1); + tr.id = "commentRow" + comment["id"]; + tr.className = col; + + td1 = tr.insertCell(0); + td1.style.whiteSpace = "nowrap"; + td1.style.width = "8em"; + td2 = tr.insertCell(1); + var txt = '
'; + txt += ''; + txt += ''; + txt += '
'; + td1.innerHTML = txt; + td2.innerHTML = comment["comment"]; + } + return table; +} + +nmsInfoBox._searchSmart = function(id, sw) { + try { + if (nmsData.smanagement.switches[sw].distro == id) { + return true; + } + if (id.match("active")) { + var limit = id; + limit = limit.replace("active>",""); + limit = limit.replace("active<",""); + limit = limit.replace("active=",""); + var operator = id.replace("active","")[0]; + if (limit == parseInt(limit)) { + if (operator == ">" ) { + if (nmsData.switchstate.switches[sw]['totals'].live > limit) { + return true; + } + } else if (operator == "<") { + if (nmsData.switchstate.switches[sw]['totals'].live < limit) { + return true; + } + } else if (operator == "=") { + if (nmsData.switchstate.switches[sw]['totals'].live == limit) { + return true; + } + } + } + } + if (nmsData.snmp.snmp[sw].misc.sysDescr[0].match(id)) { + return true; + } + } catch (e) { + return false; + } + return false; +} + +/* + * FIXME: Not sure this belongs here, it's really part of the "Core" ui, + * not just the infobox. + */ +nmsInfoBox._search = function() { + var el = document.getElementById("searchbox"); + var id = false; + var matches = []; + if (el) { + id = el.value; + } + if(id) { + for(var sw in nmsData.switches.switches) { + if (id[0] == "/") { + if (nmsInfoBox._searchSmart(id.slice(1),sw)) { + matches.push(sw); + nmsMap.setSwitchHighlight(sw,true); + } else { + nmsMap.setSwitchHighlight(sw,false); + } + } else { + if(sw.indexOf(id) > -1) { + matches.push(sw); + nmsMap.setSwitchHighlight(sw,true); + } else { + nmsMap.setSwitchHighlight(sw,false); + } + } + } + } else { + nmsMap.removeAllSwitchHighlights(); + } + if(matches.length == 1) { + document.getElementById("searchbox-submit").classList.add("btn-primary"); + document.getElementById("searchbox").dataset.match = matches[0]; + document.getElementById("searchbox").addEventListener("keydown",nmsInfoBox._searchKeyListener,false); + } else { + document.getElementById("searchbox-submit").classList.remove("btn-primary"); + document.getElementById("searchbox").dataset.match = ''; + document.getElementById("searchbox").removeEventListener("keydown",nmsInfoBox._searchKeyListener,false); + } +} + +nmsInfoBox._searchKeyListener = function(e) { + if(e.keyCode == 13) { + var sw = document.getElementById("searchbox").dataset.match; + nmsInfoBox.showWindow("switchInfo",sw); + } +} + + +nmsInfoBox._nullBlank = function(x) { + if (x == null || x == false || x == undefined) + return ""; + return x; +} + + +nmsInfoBox._editChange = function(sw, v) { + var el = document.getElementById("edit-" + sw + "-" + v); + var val = el.value; + if (v == "placement") { + try { + val = JSON.parse(val); + el.parentElement.classList.remove("has-error"); + el.parentElement.classList.add("has-success"); + } catch (e) { + el.parentElement.classList.add("has-error"); + return; + } + } + nmsInfoBox._editValues[v] = val; + el.classList.add("has-warning"); + var myData = nmsInfoBox._editStringify(sw); + var out = document.getElementById("edit-output"); + out.value = myData; +} + +nmsInfoBox._editStringify = function(sw) { + for (var key in nmsInfoBox._editValues) { + var val = nmsInfoBox._editValues[key]; + } + nmsInfoBox._editValues['sysname'] = sw; + var myData = JSON.stringify([nmsInfoBox._editValues]); + return myData; +} -- cgit v1.2.3 From 8e2fb5f3c7fbfbb13224f0cb8a610837bfef364a Mon Sep 17 00:00:00 2001 From: Kristian Lyngstol Date: Tue, 22 Mar 2016 19:54:00 +0100 Subject: Update nms-public code --- web/nms-public.gathering.org/js/nms-info-box.js | 198 +++++++++++++++--------- 1 file changed, 123 insertions(+), 75 deletions(-) (limited to 'web/nms-public.gathering.org/js/nms-info-box.js') diff --git a/web/nms-public.gathering.org/js/nms-info-box.js b/web/nms-public.gathering.org/js/nms-info-box.js index 5fdf044..2d12d56 100644 --- a/web/nms-public.gathering.org/js/nms-info-box.js +++ b/web/nms-public.gathering.org/js/nms-info-box.js @@ -15,8 +15,8 @@ var nmsInfoBox = nmsInfoBox || { stats: {}, _container: false, //Container window _window: false, //Active window (reference to _windowTypes object or false) - _windowTypes: [], //List of all avaliable window types -} + _windowTypes: [] //List of all avaliable window types +}; /* * Shows a window from the _windowTypes list @@ -34,26 +34,30 @@ nmsInfoBox.showWindow = function (windowName,argument) { return; } } -} +}; /* * Refresh the active window - * - * Todo: Could use a less aggressive refresh that doesn't hide-show everything - * */ nmsInfoBox.refresh = function() { + if(!nmsInfoBox._window) + return; nmsInfoBox._show(); +}; +nmsInfoBox.update = function(argument) { + if(!nmsInfoBox._window) + return; + nmsInfoBox._window.update(argument); } /* * Internal function to show the active _window and pass along any arguments */ nmsInfoBox._show = function(argument) { - nmsData.addHandler("comments","switchshower",nmsInfoBox.update,argument); - nmsData.addHandler("switches","switchshower",nmsInfoBox.update,argument); - nmsData.addHandler("smanagement","switchshower",nmsInfoBox.update,argument); - nmsData.addHandler("snmp","switchshower",nmsInfoBox.update,argument); + nmsData.addHandler("comments","switchshower",nmsInfoBox.update,'comments'); + nmsData.addHandler("switches","switchshower",nmsInfoBox.update,'switches'); + nmsData.addHandler("smanagement","switchshower",nmsInfoBox.update,'smanagement'); + nmsData.addHandler("snmp","switchshower",nmsInfoBox.update,'snmp'); this._window.load(argument); @@ -84,13 +88,13 @@ nmsInfoBox._show = function(argument) { } this._container.appendChild(panel); this._container.style.display = "block"; -} +}; /* * Hide the active window and tell it to unload */ nmsInfoBox.hide = function() { - if(this._container == false || this._window == false) + if(!this._container || !this._window) return; this._container.style.display = "none"; this._window.unload(); @@ -99,7 +103,7 @@ nmsInfoBox.hide = function() { nmsData.unregisterHandler("switches","switchshower"); nmsData.unregisterHandler("smanagement","switchshower"); nmsData.unregisterHandler("snmp","switchshower"); -} +}; /* * Window type: Add Switch @@ -122,6 +126,8 @@ nmsInfoBox._windowTypes.addSwitch = { }, load: function(argument) { }, + update: function(type) { + }, unload: function() { }, save: function() { @@ -129,12 +135,12 @@ nmsInfoBox._windowTypes.addSwitch = { var myData = JSON.stringify([{'sysname':sysname}]); $.ajax({ type: "POST", - url: "/api/private/switch-add", + url: "/api/write/switch-add", dataType: "text", data:myData, success: function (data, textStatus, jqXHR) { var result = JSON.parse(data); - if(result.switches_addded.length > 0) { + if(result.switches_addded.length > 0) { // FIXME unresolved variable switches_addded nmsInfoBox.hide(); } nmsData.invalidate("switches"); @@ -159,17 +165,18 @@ nmsInfoBox._windowTypes.switchInfo = { sw: '', swi: '', swm: '', + commentsHash: false, + activeView: '', load: function(sw) { if(sw) { this.sw = sw; } this.swi = nmsData.switches["switches"][this.sw]; - try { - this.swm = nmsData.smanagement.switches[this.sw]; - } catch(e) { - this.swm = []; - } - + try { + this.swm = nmsData.smanagement.switches[this.sw]; + } catch(e) { + this.swm = []; + } var content = []; @@ -193,6 +200,15 @@ nmsInfoBox._windowTypes.switchInfo = { this.content = infotable; }, + update: function(type) { + switch (type) { + case 'comments': + if(this.activeView == "comments" && this.commentsHash != nmsData.comments.hash) { + nmsInfoBox._windowTypes.switchInfo.showComments(); + } + break; + } + }, getTitle: function() { return ' ' + this.sw + ''; }, @@ -203,41 +219,60 @@ nmsInfoBox._windowTypes.switchInfo = { return this.childContent; }, showComments: function() { + this.activeView = "comments"; var domObj = document.createElement("div"); var comments = []; - if (nmsData.comments.comments != undefined && nmsData.comments.comments[this.sw] != undefined) { - for (var c in nmsData.comments.comments[this.sw]["comments"]) { - var comment = nmsData.comments.comments[this.sw]["comments"][c]; - if (comment["state"] == "active" || comment["state"] == "persist" || comment["state"] == "inactive") { - comments.push(comment); - } - } - } - if (comments.length > 0) { - var commenttable = nmsInfoBox._makeCommentTable(comments); - commenttable.id = "info-switch-comments-table"; - domObj.appendChild(commenttable); - $(function () { $('[data-toggle="popover"]').popover({placement:"top",continer:'body'}) }) - } var commentbox = document.createElement("div"); commentbox.id = "commentbox"; commentbox.className = "panel-body"; commentbox.style.width = "100%"; - commentbox.innerHTML = '
'; - domObj.appendChild(commentbox); + commentbox.innerHTML = '
'; + + // If we have no switch data, so just show comment form + if(!nmsData.comments || !nmsData.comments.comments) { + this.commentsHash = false; + + // We have data, but its old, so don't change data + } else if(this.commentsHash != false && this.commentsHash == nmsData.comments.hash) { + return; + + // We have data, refresh + } else if(nmsData.comments.comments[this.sw]) { + this.commentsHash = nmsData.comments.hash; + for (var c in nmsData.comments.comments[this.sw]["comments"]) { + var comment = nmsData.comments.comments[this.sw]["comments"][c]; + if (comment["state"] == "active" || comment["state"] == "persist" || comment["state"] == "inactive") { + comments.push(comment); + } + } + + if (comments.length > 0) { + var commenttable = nmsInfoBox._makeCommentTable(comments); + commenttable.id = "info-switch-comments-table"; + domObj.appendChild(commenttable); + $(function () { $('[data-toggle="popover"]').popover({placement:"top",continer:'body'}) }) + } + + // We have no data for this switch, but its still correct + } else { + this.commentsHash = nmsData.comments.hash; + } + domObj.appendChild(commentbox); this.childContent = domObj; nmsInfoBox.refresh(); }, showEdit: function() { + this.activeView = "edit"; var domObj = document.createElement("div"); var template = {}; nmsInfoBox._editValues = {}; + var place; for (var v in this.swi) { if (v == "placement") { - var place = JSON.stringify(this.swi[v]); + place = JSON.stringify(this.swi[v]); template[v] = place; continue; } @@ -255,6 +290,8 @@ nmsInfoBox._windowTypes.switchInfo = { content.push([v, html]); } + content.sort(); + var table = nmsInfoBox._makeTable(content, "edit"); domObj.appendChild(table); @@ -280,6 +317,7 @@ nmsInfoBox._windowTypes.switchInfo = { nmsInfoBox.refresh(); }, showSNMP: function(tree) { + this.activeView = "snmp"; var domObj = document.createElement("div"); var output = document.createElement("output"); @@ -297,13 +335,14 @@ nmsInfoBox._windowTypes.switchInfo = { }, unload: function() { this.childContent = false; + this.commentsHash = false; + this.activeView = ""; }, save: function() { var myData = nmsInfoBox._editStringify(this.sw); - console.log(myData); $.ajax({ type: "POST", - url: "/api/private/switch-update", + url: "/api/write/switch-update", dataType: "text", data:myData, success: function (data, textStatus, jqXHR) { @@ -311,7 +350,6 @@ nmsInfoBox._windowTypes.switchInfo = { nmsData.invalidate("smanagement"); } }); - nmsInfoBox.hide(); } }; @@ -322,7 +360,8 @@ nmsInfoBox._windowTypes.switchInfo = { nmsInfoBox.click = function(sw) { this.showWindow("switchInfo",sw); -} + this._windowTypes.switchInfo.showComments(); +}; /* * General-purpose table-maker? @@ -352,7 +391,7 @@ nmsInfoBox._makeTable = function(content, caption) { td2.innerHTML = content[v][1]; } return table; -} +}; /* * Create and return a table for comments. @@ -395,7 +434,7 @@ nmsInfoBox._makeCommentTable = function(content) { td2.innerHTML = comment["comment"]; } return table; -} +}; nmsInfoBox._searchSmart = function(id, sw) { try { @@ -424,6 +463,15 @@ nmsInfoBox._searchSmart = function(id, sw) { } } } + if (nmsData.smanagement.switches[sw].ip.match(id)) { + return true; + } + if (nmsData.smanagement.switches[sw].subnet4.match(id)) { + return true; + } + if (nmsData.smanagement.switches[sw].subnet6.match(id)) { + return true; + } if (nmsData.snmp.snmp[sw].misc.sysDescr[0].match(id)) { return true; } @@ -431,7 +479,7 @@ nmsInfoBox._searchSmart = function(id, sw) { return false; } return false; -} +}; /* * FIXME: Not sure this belongs here, it's really part of the "Core" ui, @@ -445,50 +493,54 @@ nmsInfoBox._search = function() { id = el.value; } if(id) { + nmsMap.enableHighlights(); for(var sw in nmsData.switches.switches) { - if (id[0] == "/") { - if (nmsInfoBox._searchSmart(id.slice(1),sw)) { - matches.push(sw); - nmsMap.setSwitchHighlight(sw,true); - } else { - nmsMap.setSwitchHighlight(sw,false); - } + if(sw.indexOf(id) > -1) { + matches.push(sw); + nmsMap.setSwitchHighlight(sw,true); + } else if (nmsInfoBox._searchSmart(id,sw)) { + matches.push(sw); + nmsMap.setSwitchHighlight(sw,true); } else { - if(sw.indexOf(id) > -1) { - matches.push(sw); - nmsMap.setSwitchHighlight(sw,true); - } else { - nmsMap.setSwitchHighlight(sw,false); - } + nmsMap.setSwitchHighlight(sw,false); } } } else { - nmsMap.removeAllSwitchHighlights(); + nmsMap.disableHighlights(); } if(matches.length == 1) { document.getElementById("searchbox-submit").classList.add("btn-primary"); document.getElementById("searchbox").dataset.match = matches[0]; - document.getElementById("searchbox").addEventListener("keydown",nmsInfoBox._searchKeyListener,false); } else { document.getElementById("searchbox-submit").classList.remove("btn-primary"); document.getElementById("searchbox").dataset.match = ''; - document.getElementById("searchbox").removeEventListener("keydown",nmsInfoBox._searchKeyListener,false); } -} +}; nmsInfoBox._searchKeyListener = function(e) { - if(e.keyCode == 13) { - var sw = document.getElementById("searchbox").dataset.match; - nmsInfoBox.showWindow("switchInfo",sw); + switch (e.keyCode) { + case 13: + var sw = document.getElementById("searchbox").dataset.match; + if(sw != '') { + nmsInfoBox.showWindow("switchInfo",sw); + this._windowTypes.switchInfo.showComments(); + } + break; + case 27: + document.getElementById("searchbox").dataset.match = ''; + document.getElementById("searchbox").value = ''; + nmsInfoBox._search(); + nmsInfoBox.hide(); + break; } -} +}; nmsInfoBox._nullBlank = function(x) { if (x == null || x == false || x == undefined) return ""; return x; -} +}; nmsInfoBox._editChange = function(sw, v) { @@ -509,13 +561,9 @@ nmsInfoBox._editChange = function(sw, v) { var myData = nmsInfoBox._editStringify(sw); var out = document.getElementById("edit-output"); out.value = myData; -} +}; nmsInfoBox._editStringify = function(sw) { - for (var key in nmsInfoBox._editValues) { - var val = nmsInfoBox._editValues[key]; - } - nmsInfoBox._editValues['sysname'] = sw; - var myData = JSON.stringify([nmsInfoBox._editValues]); - return myData; -} + nmsInfoBox._editValues['sysname'] = sw; + return JSON.stringify([nmsInfoBox._editValues]); +}; -- cgit v1.2.3 From 904196acc73603ab48b7f4d5a8ba276f00d64222 Mon Sep 17 00:00:00 2001 From: Kristian Lyngstol Date: Wed, 23 Mar 2016 17:52:31 +0100 Subject: NMS: Public temp map --- web/nms-public.gathering.org/js/nms-info-box.js | 54 +++++++++++++++---------- 1 file changed, 33 insertions(+), 21 deletions(-) (limited to 'web/nms-public.gathering.org/js/nms-info-box.js') diff --git a/web/nms-public.gathering.org/js/nms-info-box.js b/web/nms-public.gathering.org/js/nms-info-box.js index 2d12d56..0481420 100644 --- a/web/nms-public.gathering.org/js/nms-info-box.js +++ b/web/nms-public.gathering.org/js/nms-info-box.js @@ -5,9 +5,7 @@ * * Interface: nmsInfoBox.showWindow(windowType,optionalParameter), nmsInfoBox.hide(), nmsInfoBox.refresh() * - * Any windowTypes should at a minimum implement load, unload, getTitle, getContent, getChildContent - * - * TODO: Implement useful update methods on windowTypes + * Any windowTypes should at a minimum implement load, update, unload, getTitle, getContent, getChildContent * */ @@ -88,6 +86,7 @@ nmsInfoBox._show = function(argument) { } this._container.appendChild(panel); this._container.style.display = "block"; + $('[data-toggle="popover"]').popover({placement:"top",container:'body'}); }; /* @@ -210,7 +209,7 @@ nmsInfoBox._windowTypes.switchInfo = { } }, getTitle: function() { - return ' ' + this.sw + ''; + return '

' + this.sw + '

'; }, getContent: function() { return this.content; @@ -219,6 +218,7 @@ nmsInfoBox._windowTypes.switchInfo = { return this.childContent; }, showComments: function() { + var oldView = this.activeView; this.activeView = "comments"; var domObj = document.createElement("div"); var comments = []; @@ -233,10 +233,6 @@ nmsInfoBox._windowTypes.switchInfo = { if(!nmsData.comments || !nmsData.comments.comments) { this.commentsHash = false; - // We have data, but its old, so don't change data - } else if(this.commentsHash != false && this.commentsHash == nmsData.comments.hash) { - return; - // We have data, refresh } else if(nmsData.comments.comments[this.sw]) { this.commentsHash = nmsData.comments.hash; @@ -251,7 +247,6 @@ nmsInfoBox._windowTypes.switchInfo = { var commenttable = nmsInfoBox._makeCommentTable(comments); commenttable.id = "info-switch-comments-table"; domObj.appendChild(commenttable); - $(function () { $('[data-toggle="popover"]').popover({placement:"top",continer:'body'}) }) } // We have no data for this switch, but its still correct @@ -334,9 +329,14 @@ nmsInfoBox._windowTypes.switchInfo = { nmsInfoBox.refresh(); }, unload: function() { - this.childContent = false; + this.title = ''; + this.content = ''; + this.childContent = false; + this.sw = ''; + this.swi = ''; + this.swm = ''; this.commentsHash = false; - this.activeView = ""; + this.activeView = ''; }, save: function() { var myData = nmsInfoBox._editStringify(this.sw); @@ -346,6 +346,10 @@ nmsInfoBox._windowTypes.switchInfo = { dataType: "text", data:myData, success: function (data, textStatus, jqXHR) { + var result = JSON.parse(data); + if(result.switches_updated.length > 0) { // FIXME unresolved variable switches_addded + nmsInfoBox.hide(); + } nmsData.invalidate("switches"); nmsData.invalidate("smanagement"); } @@ -385,6 +389,7 @@ nmsInfoBox._makeTable = function(content, caption) { } for (var v in content) { tr = table.insertRow(-1); + tr.className = content[v][0].toLowerCase(); td1 = tr.insertCell(0); td2 = tr.insertCell(1); td1.innerHTML = content[v][0]; @@ -438,7 +443,7 @@ nmsInfoBox._makeCommentTable = function(content) { nmsInfoBox._searchSmart = function(id, sw) { try { - if (nmsData.smanagement.switches[sw].distro == id) { + if (nmsData.switches.switches[sw].distro_name == id) { return true; } if (id.match("active")) { @@ -463,15 +468,22 @@ nmsInfoBox._searchSmart = function(id, sw) { } } } - if (nmsData.smanagement.switches[sw].ip.match(id)) { - return true; - } - if (nmsData.smanagement.switches[sw].subnet4.match(id)) { - return true; - } - if (nmsData.smanagement.switches[sw].subnet6.match(id)) { - return true; - } + try { + if (nmsData.smanagement.switches[sw].mgmt_v4_addr.match(id)) { + return true; + } + if (nmsData.smanagement.switches[sw].mgmt_v6_addr.match(id)) { + return true; + } + } catch (e) {} + try { + if (nmsData.smanagement.switches[sw].subnet4.match(id)) { + return true; + } + if (nmsData.smanagement.switches[sw].subnet6.match(id)) { + return true; + } + } catch (e) {} if (nmsData.snmp.snmp[sw].misc.sysDescr[0].match(id)) { return true; } -- cgit v1.2.3 From 7ac4551ed94c1f1393bc69e595a90dbe15bc8f6c Mon Sep 17 00:00:00 2001 From: Kristian Lyngstol Date: Thu, 24 Mar 2016 21:15:50 +0100 Subject: NMS: Update public api --- web/nms-public.gathering.org/js/nms-info-box.js | 154 ++++++++++++++++++++++-- 1 file changed, 143 insertions(+), 11 deletions(-) (limited to 'web/nms-public.gathering.org/js/nms-info-box.js') diff --git a/web/nms-public.gathering.org/js/nms-info-box.js b/web/nms-public.gathering.org/js/nms-info-box.js index 0481420..a718f78 100644 --- a/web/nms-public.gathering.org/js/nms-info-box.js +++ b/web/nms-public.gathering.org/js/nms-info-box.js @@ -37,10 +37,10 @@ nmsInfoBox.showWindow = function (windowName,argument) { /* * Refresh the active window */ -nmsInfoBox.refresh = function() { +nmsInfoBox.refresh = function(argument) { if(!nmsInfoBox._window) return; - nmsInfoBox._show(); + nmsInfoBox._show(argument); }; nmsInfoBox.update = function(argument) { if(!nmsInfoBox._window) @@ -57,7 +57,8 @@ nmsInfoBox._show = function(argument) { nmsData.addHandler("smanagement","switchshower",nmsInfoBox.update,'smanagement'); nmsData.addHandler("snmp","switchshower",nmsInfoBox.update,'snmp'); - this._window.load(argument); + if(argument != "soft") + this._window.load(argument); this._container = document.getElementById("info-panel-container"); var panel = document.createElement("div"); @@ -209,7 +210,17 @@ nmsInfoBox._windowTypes.switchInfo = { } }, getTitle: function() { - return '

' + this.sw + '

'; + var sshButton = ''; + try { + var mgmt = nmsInfoBox._window.swm.mgmt_v4_addr; + sshButton = mgmt.split("/")[0]; + } catch(e) { + console.log(e); + } + if(sshButton != null && sshButton != undefined && sshButton != '') { + sshButton = ' '; + } + return '

' + this.sw + '

' + sshButton; }, getContent: function() { return this.content; @@ -357,6 +368,126 @@ nmsInfoBox._windowTypes.switchInfo = { } }; +/* + * Window type: Show inventory listing + * + * Basic window that displays a list of all devices with simple summary information + * + * TODO: Set up more complex views with more columns, sorting, etc. + * + */ +nmsInfoBox._windowTypes.inventoryListing = { + content: '', + childContent: false, + activeView: '', + activeFilter: '', + getTitle: function() { + return '

Inventory listing

'; + }, + getContent: function() { + return this.content; + }, + getChildContent: function() { + return this.childContent; + }, + setFilter: function(filter) { + this.activeFilter = filter.toLowerCase(); + nmsInfoBox._windowTypes.inventoryListing.load("refresh"); + }, + getFilter: function() { + return this.activeFilter; + }, + load: function(list) { + var hasSnmp = false; + var targetArray = []; + var listTitle = ''; + var needRefresh = false; + var needSnmp = false; + var contentObj = document.createElement("div"); + var inputObj = document.createElement("div"); + inputObj.innerHTML = '
'; + contentObj.appendChild(inputObj); + + + if(!nmsData.switches || !nmsData.switches.switches) + return; + if(!(!nmsData.snmp || !nmsData.snmp.snmp)) { + hasSnmp = true; + } + if(list == "refresh") { + list = this.activeView; + needRefresh = true; + } + + switch (list) { + case 'distro_name': + listTitle = 'Distro names'; + break; + case 'sysDescr': + if(hasSnmp) + listTitle = 'System description'; + needSnmp = true; + break; + default: + listTitle = 'Distro names'; + list = 'distro_name'; + } + this.activeView = list; + + if(needSnmp && !hasSnmp) { + this.content = "No SNMP data loaded. Reloading shortly."; + nmsData.addHandler("snmp","inventoryListing",nmsInfoBox._windowTypes.inventoryListing.update,"snmp-request"); + return; + } + + var resultArray = []; + for(var sw in nmsData.switches.switches) { + var value = ''; + if(this.activeFilter != '') { + if(sw.toLowerCase().indexOf(this.activeFilter) == -1 && !nmsInfoBox._searchSmart(this.activeFilter,sw)) + continue; + } + try { + switch (list) { + case 'distro_name': + value = nmsData.switches.switches[sw]["distro_name"]; + break; + case 'sysDescr': + value = nmsData.snmp.snmp[sw]["misc"]["sysDescr"][0]; + break; + } + } catch (e) { + //console.log(e); + } + resultArray.push([sw, value]); + } + + resultArray.sort(); + + var infotable = nmsInfoBox._makeTable(resultArray,listTitle); + infotable.id = "inventory-table"; + + contentObj.appendChild(infotable); + this.content = contentObj; + if(needRefresh) + nmsInfoBox.refresh("soft"); + }, + update: function(type) { + if(type == "snmp-request") { + nmsData.unregisterHandler("snmp","inventoryListing"); + nmsInfoBox._windowTypes.inventoryListing.load("refresh"); + } + }, + unload: function() { + nmsData.unregisterHandler("snmp","inventoryListing"); + this.content = ''; + this.activeView = ''; + this.activeFilter = ''; + }, + save: function() { + } +}; + /* * Click a switch and display it * it. @@ -443,9 +574,11 @@ nmsInfoBox._makeCommentTable = function(content) { nmsInfoBox._searchSmart = function(id, sw) { try { - if (nmsData.switches.switches[sw].distro_name == id) { - return true; - } + try { + if (nmsData.switches.switches[sw].distro_name.toLowerCase() == id) { + return true; + } + } catch (e) {} if (id.match("active")) { var limit = id; limit = limit.replace("active>",""); @@ -484,7 +617,7 @@ nmsInfoBox._searchSmart = function(id, sw) { return true; } } catch (e) {} - if (nmsData.snmp.snmp[sw].misc.sysDescr[0].match(id)) { + if (nmsData.snmp.snmp[sw].misc.sysDescr[0].toLowerCase().match(id)) { return true; } } catch (e) { @@ -502,12 +635,12 @@ nmsInfoBox._search = function() { var id = false; var matches = []; if (el) { - id = el.value; + id = el.value.toLowerCase(); } if(id) { nmsMap.enableHighlights(); for(var sw in nmsData.switches.switches) { - if(sw.indexOf(id) > -1) { + if(sw.toLowerCase().indexOf(id) > -1) { matches.push(sw); nmsMap.setSwitchHighlight(sw,true); } else if (nmsInfoBox._searchSmart(id,sw)) { @@ -547,7 +680,6 @@ nmsInfoBox._searchKeyListener = function(e) { } }; - nmsInfoBox._nullBlank = function(x) { if (x == null || x == false || x == undefined) return ""; -- cgit v1.2.3