aboutsummaryrefslogtreecommitdiffstats
path: root/web/js/nms-ui-boxes.js
blob: 6a89fcd919ac217335e9e7c9d784bda31f43cd67 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
"use strict";

/*
 * An attempt at breaking up nms-info-box into something that works better.
 * The idea is to retain the ability to fiddle with html, but solve common
 * problems such as "I need to show a box that looks good" and "I need to
 * update the content of this field in this box when something happens".
 *
 * Seeing as how it's 2019, I figured I'd try to use this whole "class"
 * thing :D
 *
 * Eventually, everything in nms-info-box.js should use this, but more over,
 * if a need to write some code that needs a UI element arises, the general
 * problem should be solved here, while the implementation: elsewhere.
 *
 * Example: Adding a new switch or network should not be closely tied in
 * here.
 *
 * I expect this code to change dramatically as I start using it.
 */

class nmsBox {
  constructor(type, settings = null) {
    this.type = type;
    this._boxes = [];
    this.html = document.createElement(type);
    this.html.nmsBox = this;
    if (settings) {
      this.applySettings(settings);
    }
  }
  get value() {
    var values = [];
    for (var x in this._boxes) {
      var tmp = this._boxes[x].value;
      if (tmp != undefined && !(tmp instanceof Array && tmp.length == 0)) {
        values.push(tmp);
      }
    }
    return values;
  }

  applySettings(settings) {
    /*
     * This really should go deeper, but the problem is that
     * I'm lazy. You can't just use Object.assign() either because
     * stuff like html.style isn't just any element and we want
     * to be able to do html:{style:{display:"none"}} instead
     * of html:{style:new (style object thing with display:none)}
     *
     * So far this works.
     *
     * Note that this breaks forr classList: This is because
     * classList is just an accessor for className, so you
     * should set className instead initially.
     */
    if (settings.html) {
      for (var x in settings.html) {
        if (settings.html[x] instanceof Object) {
          Object.assign(this.html[x], settings.html[x]);
        } else {
          this.html[x] = settings.html[x];
        }
      }
    }
  }
  /* Should rename these to push() and unshift(), really, to be
   * more consistent.
   */
  add(box) {
    this._boxes.push(box);
    box.attach(this.html);
    box.show();
  }
  insert(box) {
    this._boxes.unshift(box);
    box.attach(this.html);
    box.show(true, -1);
  }

  /* This is provided so as to allow attaching to non-nmsBox-objects.
   * E.g.: attach the root to a HTML element directly.
   * If you just use foo = document.getElement...,
   * foo.appendChild(box.html), then .hide() will work fine,
   * but there's no way to do .show() afterwards.
   *
   * Be aware:
   * - we want to AVOID the child knowing too much about the parent
   * - HTML elements can only ever be part of a single parent
   *   element. This means that it is safe to attach(root1), .show(),
   *   atach(root2), show() ... The DOM object wil be removed from root1.
   * - Due to the above, we should probably actually adress that, since
   *   there are valid reasons for showing the same nmsBox twice (e.g.:
   *   showing a list of tons of stuff, and also a "top 5")
   */
  attach(root) {
    if (!(root instanceof HTMLElement)) {
      root = document.getElementById(root);
    }
    console.assert(root instanceof HTMLElement);
    this._root = root;
  }
  show(show = true, where = 1) {
    if (!show) {
      this.hide();
      return;
    }
    if (this._root instanceof HTMLElement) {
      if (where > 0) {
        this._root.appendChild(this.html);
      } else {
        this._root.insertBefore(this.html, this._root.firstElementChild);
      }
    }
  }
  _remove(source, box) {
    source.html.removeChild(box.html);
    if (box._root == source) {
      box._root = undefined;
    }
    var x = source._boxes.indexOf(box);
    delete source._boxes[x];
  }
  _includes(source, box) {
    if (source._boxes.indexOf(box) >= 0) {
      return true;
    } else {
      return false;
    }
  }
  /*
   * This redirect is to make it easier for subclasses that
   * override add() to make it eaiser to add logical
   * childs, e.g. adding to a table really adds to ._tbody,
   * not the true top-element of the table.
   */
  remove(box) {
    this._remove(this, box);
  }
  includes(box) {
    return this._includes(this, box);
  }
  hide() {
    if (this.html.parentElement instanceof HTMLElement) {
      this.html.parentElement.removeChild(this.html);
    }
  }
  destroy() {
    this.hide();
    for (var i in this._boxes) {
      var x = this._boxes[i];
      x.hide();
      x.destroy();
      delete this._boxes[i];
    }
    delete this.html;
    delete this;
  }
  update() {
    for (var x in this._boxes) {
      this._boxes[x].update();
    }
  }
}

class nmsString extends nmsBox {
  constructor(content, type) {
    type = type ? type : "p";
    super(type, { html: { textContent: content } });
  }
  set value(input) {
    this.html.textContent = input;
  }
  get value() {
    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.
 */
class nmsTable extends nmsBox {
  constructor(content, caption) {
    super("table", { html: { className: "table table-sm table-transparent" } });
    if (caption != undefined) {
      var cap = new nmsBox("caption");
      cap.html.textContent = caption;
      super.add(cap);
    }
    this._tbody = new nmsBox("tbody");
    super.add(this._tbody);
    for (var v in content) {
      this.add(content[v]);
    }
  }
  remove(box) {
    this._remove(this._tbody, box);
  }
  includes(box) {
    return this._tbody.includes(box);
  }
  _makeBox(content) {
    if (content instanceof nmsBox) {
      return content;
    }
    var tr;
    tr = new nmsBox("tr");
    for (var x in content) {
      var td = new nmsBox("td");
      var child = content[x];
      if (child instanceof nmsBox) {
        td.add(child);
      } else {
        td.add(new nmsString(child));
      }
      tr.add(td);
    }
    return tr;
  }
  /* Can take either a single nmsBox-object that will be added as-is,
   * or an array of items that will be added as individual cells
   */
  add(content) {
    this._tbody.add(this._makeBox(content));
  }
  insert(box) {
    this._tbody.insert(this._makeBox(box));
  }
}

/*
 * Basic panel. Rooted at the 'metaContainer' as of now, but that MIGHT CHANGE.
 * Draws a nice box with a background, title, close-button and whatnot.
 * Usage:
 * var myThing = new nmsPanel("initial title");
 * myThing.add(nmsString("whatever"))
 * myThing.title = "blah...."
 */
class nmsPanel extends nmsBox {
  constructor(title) {
    super("div", { html: { style: { gridColumn: 2 } } });
    this._topBox = new nmsBox("div", {
      html: { className: "panel panel-default" },
    });
    this._body = new nmsBox("div", { html: { className: "panel-body" } });
    this.nav = new nmsBox("div", { html: { className: "panel-body" } });
    this._topBox.add(this.makeHeading(title));
    this._topBox.add(this.nav);
    this._topBox.add(this._body);
    super.add(this._topBox);
  }
  attach(root = document.getElementById("genericPanelContainer")) {
    try {
      var x = parseInt(root.lastElementChild.style.gridColumnStart);
      this.html.style.gridColumnStart = x + 1;
    } catch (e) {}
    super.attach(root);
  }
  show() {
    if (!this._root) {
      this.attach();
    }
    super.show();
  }
  /* Mainly just to make the constructor more readable. */
  makeHeading(title) {
    var titleObject = new nmsBox("div", {
      html: { className: "panel-heading" },
    });
    this._titleText = new nmsBox("h4", { html: { textContent: title } });
    var closeButton = new nmsBox("button");
    closeButton.html.className = "close";
    closeButton.panel = this;
    closeButton.html.onclick = function () {
      this.nmsBox.panel.destroy();
    };
    closeButton.html.style = "float: right;";
    closeButton.add(new nmsString("X", "span"));
    titleObject.add(closeButton);
    titleObject.add(this._titleText);
    return titleObject;
  }
  add(item) {
    this._body.add(item);
  }
  set title(input) {
    this._titleText.html.textContent = input;
  }
  get title() {
    this._titleText.html.textContent;
  }
}