diff options
-rwxr-xr-x | web/api/write/switches | 24 | ||||
-rw-r--r-- | web/js/nms-info-box.js | 183 | ||||
-rw-r--r-- | web/js/nms-ui-boxes.js | 6 | ||||
-rw-r--r-- | web/js/nms-ui-switch.js | 35 |
4 files changed, 84 insertions, 164 deletions
diff --git a/web/api/write/switches b/web/api/write/switches index 947404d..e42c4b3 100755 --- a/web/api/write/switches +++ b/web/api/write/switches @@ -63,28 +63,22 @@ foreach my $tmp2 (@tmp) { } else { %placement = %{convertplace($switch{'placement'})}; } - if (not defined($switch{'ip'}) and defined($switch{'mgtmt4'})) { - $switch{'ip'} = $switch{'mgtmt4'}; - } - if (not defined($switch{'secondary_ip'}) and defined($switch{'mgtmt6'})) { - $switch{'secondary_ip'} = $switch{'mgtmt6'}; - } my ($x1,$x2,$y1,$y2); $x1 = $placement{'x1'}; $y1 = $placement{'y1'}; $x2 = $placement{'xx'}; $y2 = $placement{'yy'}; $switch{'placement'} = "(($x1,$y1),($x2,$y2))"; - + my @names; + my @values; map { if (defined ($template{$_})) { - $template{$_} = $dbh->quote($switch{$_}); + push @names,$_; + push @values,$dbh->quote($switch{$_}); } } keys %switch; - - - $nms::web::dbh->do("INSERT INTO SWITCHES (ip, sysname, last_updated, locked, poll_frequency, tags, community, secondary_ip, placement,subnet4,subnet6,distro) VALUES ($template{'ip'}, $template{'sysname'}, $template{'last_updated'}, $template{'locked'}, $template{'poll_frequency'}, $template{'tags'}, $template{'community'}, $template{'secondary_ip'}, $template{'placement'},$template{'subnet4'},$template{'subnet6'},$template{'distro'});"); - push @added, $switch{'sysname'}; + $nms::web::dbh->do("INSERT INTO SWITCHES (" . join(",",@names) . ") VALUES (". join(",",@values).");"); + push @added, $switch{'sysname'}; oplog("\"" . $switch{'sysname'} . "\"", "Switch added: " . $switch{'sysname'}); } else { if (defined($switch{'placement'})) { @@ -105,12 +99,6 @@ foreach my $tmp2 (@tmp) { if (defined($switch{'tags'})) { $switch{'tags'} =~ s/'/"/g; } - if (not defined($switch{'ip'}) and defined($switch{'mgtmt4'})) { - $switch{'ip'} = $switch{'mgtmt4'}; - } - if (not defined($switch{'secondary_ip'}) and defined($switch{'mgtmt6'})) { - $switch{'secondary_ip'} = $switch{'mgtmt6'}; - } my @set; map { if (defined($template{$_})) { diff --git a/web/js/nms-info-box.js b/web/js/nms-info-box.js index c42c7ae..0d18930 100644 --- a/web/js/nms-info-box.js +++ b/web/js/nms-info-box.js @@ -978,151 +978,54 @@ nmsInfoBox.addPanelType("inventoryListing",inventoryListingPanel); var switchEditPanel = function () { nmsInfoPanel.call(this,"switchEdit"); this.refresh = function (reason) { - var swi = []; - var swm = []; - try { - swi = nmsData.switches["switches"][this.sw]; - } catch(e) {} - try { - swm = nmsData.smanagement.switches[this.sw]; - } catch(e) {} - - var domObj = document.createElement("div"); - var template = {}; - - nmsInfoBox._editValues = {}; - var place; - var tags; - for (var v in swi) { - /* - * Placement and tags needs to be sent and edited - * as plain JSON... - */ - if (v == "placement") { - place = JSON.stringify(swi[v]); - template[v] = place; - continue; - } - if (v == "tags") { - tags = JSON.stringify(swi[v]); - template[v] = tags; - continue; - } - template[v] = nmsInfoBox._nullBlank(swi[v]); - } - for (var v in swm) { - template[v] = nmsInfoBox._nullBlank(swm[v]); - } - var content = []; - for (v in template) { - var tmpsw = '\'' + this.sw + '\''; - var tmpv = '\'' + v + '\''; - var tmphandler = '"nmsInfoBox._editChange(' + tmpsw + ',' + tmpv + ');"'; - if(v == 'community') { - var html = '<input type="password" autocomplete="off" onfocus="this.type = \'text\'" class="form-control" value="' + template[v] + '" id="edit-' + this.sw + '-' + v + '" onchange=' + tmphandler + ' oninput=' + tmphandler + '>'; - } - else { - var html = '<input type="text" class="form-control" value="' + template[v] + '" id="edit-' + this.sw + '-' + v + '" onchange=' + tmphandler + ' oninput=' + tmphandler + ' ' + (v == 'sysname' || v == 'subnet4' || v == 'subnet6' || v == 'mgmt_v4_gw' || v == 'mgmt_v6_gw' ? "readonly" : "") + '>'; - } - if (v == "placement") { - v = "placement <a onclick='var _x = document.getElementById(\"edit-" + this.sw + "-placement\"); _x.value = \"\\\"reset\\\"\"; _x.oninput();' class='pull-right'>Reset</a>"; - } - content.push([v, html]); - } - - content.sort(); - - var table = nmsInfoBox._makeTable(content); - domObj.appendChild(table); - - var outputCont = document.createElement("div"); - outputCont.id = "edit-output-cont"; - outputCont.classList.add("collapse"); - outputCont.innerHTML = "<h5>Request preview</h5>"; - var output = document.createElement("output"); - output.id = "edit-output"; - outputCont.appendChild(output); - domObj.appendChild(outputCont); - - var nav = document.createElement("nav"); - nav.classList.add("nav","nav-pills"); - - var submit = document.createElement("button"); - submit.innerHTML = "Save changes"; - submit.classList.add("btn", "btn-primary"); - submit.id = "edit-submit-" + this.sw; - submit.setAttribute("onclick","nmsInfoBox._windowHandler.doInPanel('" + this.id + "','save');"); - submit.style = "margin-right: 5px"; - nav.appendChild(submit); - - var deleteButton = document.createElement("button"); - deleteButton.innerHTML = "Delete switch"; - deleteButton.classList.add("btn", "btn-danger"); - deleteButton.id = "delete-submit-" + this.sw; - deleteButton.setAttribute("onclick","nmsInfoBox._windowHandler.doInPanel('" + this.id + "','deleteSwitch');"); - nav.appendChild(deleteButton); - - var toggleDetails = document.createElement("button"); - toggleDetails.innerHTML = '<span class="glyphicon glyphicon-menu-hamburger" aria-hidden="true"></span>'; - toggleDetails.classList.add("btn", "btn-default", "pull-right"); - toggleDetails.dataset.toggle = "collapse"; - toggleDetails.dataset.target = "#edit-output-cont"; - toggleDetails.title = "Show request preview"; - toggleDetails.id = "edit-toggle-details-" + this.sw; - nav.appendChild(toggleDetails); - - domObj.appendChild(nav); - - this._render(domObj); - if (place) { - var pval = document.getElementById("edit-" + this.sw + "-placement"); - if (pval) { - pval.value = place; + if (this.box) { return; } + this.box = new nmsModSwitch(this.sw); + var save = new nmsButton("Save","btn-primary"); + save.panel = this; + save.html.onclick = function(e) { + var diff = this.nmsBox.panel.box.diff() + console.log(diff) + + if (diff != undefined) { + $.ajax({ + type: "POST", + url: "/api/write/switches", + dataType: "text", + data:JSON.stringify([diff]), + success: function (data, textStatus, jqXHR) { + var result = JSON.parse(data); + console.log("hei...") + nmsData.invalidate("switches"); + nmsData.invalidate("smanagement"); + } + }); } } - if (tags) { - var ptags = document.getElementById("edit-" + this.sw + "-tags"); - if (ptags) { - ptags.value = tags; - } + this.box.add(save) + var del = new nmsButton("Delete","btn btn-danger"); + del.panel = this + del.html.onclick = function(e) { + if(confirm("This will delete the switch: " + this.nmsBox.panel.sw)) { + var myData = [{'sysname': this.nmsBox.panel.sw, 'deleted': true}]; + myData = JSON.stringify(myData); + $.ajax({ + type: "POST", + url: "/api/write/switches", + dataType: "text", + data:myData, + success: function (data, textStatus, jqXHR) { + nmsInfoBox.hide(); + nmsData.invalidate("switches"); + nmsData.invalidate("smanagement"); + } + }); + }; } - - }; - this.save = function () { - var myData = nmsInfoBox._editStringify(this.sw); - $.ajax({ - type: "POST", - url: "/api/write/switches", - 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"); - } - }); + this.box.add(del) + this.box.attach(this.me) + this.box.show() }; - this.deleteSwitch = function () { - if(confirm("This will delete the switch: " + this.sw)) { - var myData = [{'sysname': this.sw, 'deleted': true}]; - myData = JSON.stringify(myData); - $.ajax({ - type: "POST", - url: "/api/write/switches", - dataType: "text", - data:myData, - success: function (data, textStatus, jqXHR) { - nmsInfoBox.hide(); - nmsData.invalidate("switches"); - nmsData.invalidate("smanagement"); - } - }); - }; - }; }; nmsInfoBox.addPanelType("switchEdit",switchEditPanel); diff --git a/web/js/nms-ui-boxes.js b/web/js/nms-ui-boxes.js index 9a3330c..dda77c9 100644 --- a/web/js/nms-ui-boxes.js +++ b/web/js/nms-ui-boxes.js @@ -176,6 +176,12 @@ class nmsString extends nmsBox { return this.html.textContent; } } + +class nmsButton extends nmsBox { + constructor(text,type = "btn-default") { + super("button",{html:{textContent:text,className:"btn "+type,type:"button"}}) + } +} /* * A general-purpose table. It can be created in one go, or * as-you-go. diff --git a/web/js/nms-ui-switch.js b/web/js/nms-ui-switch.js index 3e6dd47..571f215 100644 --- a/web/js/nms-ui-switch.js +++ b/web/js/nms-ui-switch.js @@ -10,7 +10,7 @@ * it to avoid complicating things. * */ -class nmsModSwitch extends nmsPanel { +class nmsModSwitch extends nmsBox { constructor(sw) { var title; if (sw == undefined) { @@ -18,9 +18,9 @@ class nmsModSwitch extends nmsPanel { } else { title = "Edit " + sw; } - super(title) + super("div",{html:{className: "panel-body"}}); this._sw = sw; - this.nav.add(new nmsString("Adding and editing stuff has immediate effects and blah blah blah, insert sensible help-text here.")); + //this.nav.add(new nmsString("Adding and editing stuff has immediate effects and blah blah blah, insert sensible help-text here.")); this.generateBaseTemplate() this.populate() } @@ -76,14 +76,18 @@ class nmsModSwitch extends nmsPanel { var template = {} for (var v in swi) { console.assert(this._template[v] instanceof nmsType) - this._template[v].value = swi[v]; + if (swi[v] != null) { + this._template[v].value = swi[v]; + } } for (var v in swm) { if (v == "last_updated") { continue; } console.assert(this._template[v] instanceof nmsType) - this._template[v].value = swm[v]; + if (swm[v] != null) { + this._template[v].value = swm[v]; + } } } populate() { @@ -103,7 +107,26 @@ class nmsModSwitch extends nmsPanel { this.title = "saw row change on " + row.name + " to " + row.value; } get value() { - return this.table.value; + var ret = {}; + for (var idx in this.rows) { + ret[idx] = this.rows[idx].value; + } + return ret; + } + diff() { + var ret = {}; + var changed = 0; + for (var idx in this.rows) { + if (this.rows[idx].value != this.rows[idx].original) { + ret[idx] = this.rows[idx].value; + changed++; + } + } + if (!changed) { + return undefined; + } + ret["sysname"] = this.rows["sysname"].value; + return ret; } } |