diff options
-rwxr-xr-x | web/nms.gathering.org/api/private/switch-add | 27 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/private/switches-management | 2 | ||||
-rw-r--r-- | web/nms.gathering.org/js/nms-info-box.js | 101 |
3 files changed, 123 insertions, 7 deletions
diff --git a/web/nms.gathering.org/api/private/switch-add b/web/nms.gathering.org/api/private/switch-add index 93416ff..64e5e58 100755 --- a/web/nms.gathering.org/api/private/switch-add +++ b/web/nms.gathering.org/api/private/switch-add @@ -24,6 +24,22 @@ my $sth = $nms::web::dbh->prepare("SELECT sysname FROM switches WHERE sysname=?" my @fields = ('ip', 'sysname', 'switchtype', 'last_updated', 'locked', 'poll_frequency', 'community', 'lldp_chassis_id', 'secondary_ip', 'placement'); +sub convertplace +{ + my %in = %{$_[0]}; + my %out = (); + + if (not defined $in{'x1'} and defined($in{'x'})) { + $out{'x1'} = $in{'x'}; + $out{'y1'} = $in{'y'}; + $out{'xx'} = $in{'x'} + $in{'width'}; + $out{'yy'} = $in{'y'} + $in{'height'}; + } else { + return \%in; + } + return \%out; +} + foreach my $tmp2 (@tmp) { my %switch = %{$tmp2}; my $affected = 0; @@ -43,7 +59,7 @@ foreach my $tmp2 (@tmp) { if (not defined ($switch{'placement'})) { %placement = guess_placement($switch{'sysname'}); } else { - %placement = %{$switch{'placement'}}; + %placement = %{convertplace($switch{'placement'})}; } if (not defined($switch{'ip'}) and defined($switch{'mgtmt4'})) { $switch{'ip'} = $switch{'mgtmt4'}; @@ -66,11 +82,12 @@ foreach my $tmp2 (@tmp) { push @added, $switch{'sysname'}; } else { if (defined($switch{'placement'})) { + my %placement = %{convertplace($switch{'placement'})}; my ($x1,$x2,$y1,$y2); - $x1 = $switch{'placement'}{'x1'}; - $y1 = $switch{'placement'}{'y1'}; - $x2 = $switch{'placement'}{'xx'}; - $y2 = $switch{'placement'}{'yy'}; + $x1 = $placement{'x1'}; + $y1 = $placement{'y1'}; + $x2 = $placement{'xx'}; + $y2 = $placement{'yy'}; $switch{'placement'} = "(($x1,$y1),($x2,$y2))"; } if (not defined($switch{'ip'}) and defined($switch{'mgtmt4'})) { diff --git a/web/nms.gathering.org/api/private/switches-management b/web/nms.gathering.org/api/private/switches-management index 474f674..a598824 100755 --- a/web/nms.gathering.org/api/private/switches-management +++ b/web/nms.gathering.org/api/private/switches-management @@ -12,7 +12,7 @@ use Data::Dumper; $nms::web::cc{'max-age'} = "60"; -my $q2 = $nms::web::dbh->prepare('select sysname,ip,switchtype,poll_frequency,community,last_updated from switches '); +my $q2 = $nms::web::dbh->prepare('select sysname,ip,poll_frequency,community,last_updated from switches '); $q2->execute(); while (my $ref = $q2->fetchrow_hashref()) { diff --git a/web/nms.gathering.org/js/nms-info-box.js b/web/nms.gathering.org/js/nms-info-box.js index cf77d1e..94792ee 100644 --- a/web/nms.gathering.org/js/nms-info-box.js +++ b/web/nms.gathering.org/js/nms-info-box.js @@ -59,6 +59,7 @@ nmsInfoBox._hide = function() commentbox.parentNode.removeChild(commentbox); swtop.style.display = 'none'; nmsInfoBox._showing = ""; + nmsInfoBox._editHide(); } /* @@ -151,7 +152,7 @@ nmsInfoBox._show = function(x) nmsInfoBox._hide(); nmsInfoBox._showing = x; - swtitle.innerHTML = x + '<button type="button" class="close" aria-labe="Close" onclick="nmsInfoBox.hide();" style="float: right;"><span aria-hidden="true">×</span></button>'; + swtitle.innerHTML = ' <button type="button" class="edit btn btn-xs btn-warning" onclick="nmsInfoBox._edit(\'' + x + '\');">Edit</button> ' + x + ' <button type="button" class="close" aria-label="Close" onclick="nmsInfoBox.hide();" style="float: right;"><span aria-hidden="true">×</span></button>'; for (var v in sw) { if (v == "placement") { @@ -195,3 +196,101 @@ nmsInfoBox._show = function(x) swtop.style.display = 'block'; } +nmsInfoBox._nullBlank = function(x) { + if (x == null || x == false || x == undefined) + return ""; + return x; +} + +nmsInfoBox._editHide = function() { + var container = document.getElementById("nmsInfoBox-edit-box"); + if (container != undefined) + container.parentNode.removeChild(container); +} + +nmsInfoBox._edit = function(sw) { + var template = {}; + var place = false; + nmsInfoBox._editHide(); + var container = document.createElement("div"); + container.id = "nmsInfoBox-edit-box"; + + nmsInfoBox._editValues = {}; + if (nmsData.switches.switches[sw] != undefined) { + for (var v in nmsData.switches.switches[sw]) { + if (v == "placement") { + place = JSON.stringify(nmsData.switches.switches[sw][v]); + template[v] = ""; + continue; + } + template[v] = this._nullBlank(nmsData.switches.switches[sw][v]); + } + } + if (nmsData.smanagement.switches[sw] != undefined) { + for (var v in nmsData.smanagement.switches[sw]) { + template[v] = this._nullBlank(nmsData.smanagement.switches[sw][v]); + } + } + var content = []; + for (v in template) { + var tmpsw = '\'' + sw + '\''; + var tmpv = '\'' + v + '\''; + var tmphandler = '"nmsInfoBox._editChange(' + tmpsw + ',' + tmpv + ');"'; + var html = "<input type=\"text\" class=\"form-control\" value=\"" + template[v] + "\" id=\"edit-"+ sw + "-" + v + '" onchange=' + tmphandler + ' oninput=' + tmphandler + '/>'; + content.push([v, html]); + } + var table = nmsInfoBox._makeTable(content, "edit"); + var swtop = document.getElementById("info-switch-parent"); + container.appendChild(table); + var submit = document.createElement("button"); + submit.innerHTML = "Save changes"; + submit.classList.add("btn", "btn-primary"); + submit.id = "edit-submit-" + sw; + submit.onclick = function(e) { nmsInfoBox._editSave(sw, e); }; + container.appendChild(submit); + var output = document.createElement("output"); + output.id = "edit-output"; + container.appendChild(output); + swtop.appendChild(container); + if (place) { + var pval = document.getElementById("edit-" + sw + "-placement"); + if (pval) { + pval.value = place; + } + } +} + +nmsInfoBox._editChange = function(sw, v) { + var el = document.getElementById("edit-" + sw + "-" + v); + var val = el.value; + if (v == "placement") + val = JSON.parse(val); + nmsInfoBox._editValues[v] = val; + el.classList.add("bg-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; +} +nmsInfoBox._editSave = function(sw, e) { + var myData = nmsInfoBox._editStringify(sw); + $.ajax({ + type: "POST", + url: "/api/private/switch-add", + dataType: "text", + data:myData, + success: function (data, textStatus, jqXHR) { + nmsData.invalidate("switches"); + nmsData.invalidate("smanagement"); + } + }); + nmsInfoBox._editHide(); +} |