aboutsummaryrefslogtreecommitdiffstats
path: root/web/js/nms-ui-switch.js
blob: fe4bbe9fe301bec7e67b2cd9746950a88db3db4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"use strict";

class nmsUiSwitch extends nmsPanel {
	constructor(sw) {
		var title;
		if (sw == undefined) {
			title = "Add new switch"
		} else {
			title = "Edit " + sw;
		}
		super(title)
		this._sw = sw;
		this.populate()
	}
	/*
	 * We really should base this on a backend-API exposing relevant fields...
	 */
	getTemplate(sw) {
		if (sw == undefined) {
			return { 
				mgmt_v4_addr: null, 
				mgmt_v6_addr: null,
				community: null,
				placement: null,
				mgmt_vlan: null,
				poll_frequency: null,
				tags: null
			};
		}
		var swi = [];
		var swm = [];
		try {
			swi = nmsData.switches["switches"][this._sw];
		} catch(e) {}
		try {
			swm = nmsData.smanagement.switches[this._sw];
		} catch(e) {}

		var template = {}
		for (var v in swi) {
			template[v] = swi[v];
		}
		for (var v in swm) {
			if (v == "last_updated") {
				continue;
			}
			template[v] = swm[v];
		}
		return template;
	}
	populate() {
		var template = this.getTemplate(this._sw);
		this.table = new nmsTable();
		var first = new Array("sysname","distro_name","distro_phy_port","traffic_vlan")
		var sorted = new Array();
		for (var v in template) {
			if (!first.includes(v)) {
				sorted.push(v);
			}
		}
		sorted.sort();
		var finals = first.concat(sorted);
		this.rows = {}
		for (var i in finals) {
			var v = finals[i];
			this.rows[v] = new nmsEditRow(v, nmsInfoBox._nullBlank(template[v]));
			this.rows[v].parent = this;
			this.table.add(this.rows[v]);
		}
		this.add(this.table);
	}
	changed(row) {
		this.title = "saw row change on " + row.name + " to " + row.value;
	}
	get value() {
		return this.table.value;
	}
}

class nmsEditRow extends nmsBox {
	constructor(text,value) {
		super("tr")
		// This should/could be smarter in the future.
		if (value instanceof Object) {
			value = JSON.stringify(value);
		}
		this.name = text;
		this._value = value;
		this.original = value;
		var td1 = new nmsBox("td")
		td1.add(new nmsString(text))
		this.add(td1);

		var td2 = new nmsBox("td")
		var input = new nmsBox("input")
		input.html.value = value;
		input.html.className = "form-control";
		input.html.type = "text";
		input.row = this;
		input.html.onchange = function() {
			this.nmsBox.row.value = this.value
		}
		input.html.oninput = function() {
			this.nmsBox.row.value = this.value
		}
		this._input = input;
		this._td2 = td2;
		td2.add(input)
		this.add(td2)
	}
	get value() {
		return this._value;
	}
	set value(value) {
		this._value = value;
		if (this._input.html.value != value) {
			this._input.html.value = value
		}
		if (this._value != this.original) {
			this._td2.html.classList.add("has-warning");
		} else {
			this._td2.html.classList.remove("has-warning");
		}
		this.parent.changed(this) 
	}
}