treewide: switch to shared isObject(), toArray() and sortedKeys() helpers
[oweals/luci.git] / modules / luci-base / htdocs / luci-static / resources / tools / widgets.js
1 'use strict';
2 'require ui';
3 'require form';
4 'require network';
5 'require firewall';
6
7 var CBIZoneSelect = form.ListValue.extend({
8         __name__: 'CBI.ZoneSelect',
9
10         load: function(section_id) {
11                 return Promise.all([ firewall.getZones(), network.getNetworks() ]).then(L.bind(function(zn) {
12                         this.zones = zn[0];
13                         this.networks = zn[1];
14
15                         return this.super('load', section_id);
16                 }, this));
17         },
18
19         filter: function(section_id, value) {
20                 return true;
21         },
22
23         lookupZone: function(name) {
24                 return this.zones.filter(function(zone) { return zone.getName() == name })[0];
25         },
26
27         lookupNetwork: function(name) {
28                 return this.networks.filter(function(network) { return network.getName() == name })[0];
29         },
30
31         renderWidget: function(section_id, option_index, cfgvalue) {
32                 var values = L.toArray((cfgvalue != null) ? cfgvalue : this.default),
33                     choices = {};
34
35                 if (this.allowlocal) {
36                         choices[''] = E('span', {
37                                 'class': 'zonebadge',
38                                 'style': 'background-color:' + firewall.getColorForName(null)
39                         }, [
40                                 E('strong', _('Device')),
41                                 (this.allowany || this.allowlocal)
42                                         ? ' (%s)'.format(this.alias != 'dest' ? _('output') : _('input')) : ''
43                         ]);
44                 }
45                 else if (!this.multiple && (this.rmempty || this.optional)) {
46                         choices[''] = E('span', {
47                                 'class': 'zonebadge',
48                                 'style': 'background-color:' + firewall.getColorForName(null)
49                         }, E('em', _('unspecified')));
50                 }
51
52                 if (this.allowany) {
53                         choices['*'] = E('span', {
54                                 'class': 'zonebadge',
55                                 'style': 'background-color:' + firewall.getColorForName(null)
56                         }, [
57                                 E('strong', _('Any zone')),
58                                 (this.allowany && this.allowlocal) ? ' (%s)'.format(_('forward')) : ''
59                         ]);
60                 }
61
62                 for (var i = 0; i < this.zones.length; i++) {
63                         var zone = this.zones[i],
64                             name = zone.getName(),
65                             networks = zone.getNetworks(),
66                             ifaces = [];
67
68                         if (!this.filter(section_id, name))
69                                 continue;
70
71                         for (var j = 0; j < networks.length; j++) {
72                                 var network = this.lookupNetwork(networks[j]);
73
74                                 if (!network)
75                                         continue;
76
77                                 var span = E('span', {
78                                         'class': 'ifacebadge' + (network.getName() == this.network ? ' ifacebadge-active' : '')
79                                 }, network.getName() + ': ');
80
81                                 var devices = network.isBridge() ? network.getDevices() : L.toArray(network.getDevice());
82
83                                 for (var k = 0; k < devices.length; k++) {
84                                         span.appendChild(E('img', {
85                                                 'title': devices[k].getI18n(),
86                                                 'src': L.resource('icons/%s%s.png'.format(devices[k].getType(), devices[k].isUp() ? '' : '_disabled'))
87                                         }));
88                                 }
89
90                                 if (!devices.length)
91                                         span.appendChild(E('em', _('(empty)')));
92
93                                 ifaces.push(span);
94                         }
95
96                         if (!ifaces.length)
97                                 ifaces.push(E('em', _('(empty)')));
98
99                         choices[name] = E('span', {
100                                 'class': 'zonebadge',
101                                 'style': 'background-color:' + zone.getColor()
102                         }, [ E('strong', name) ].concat(ifaces));
103                 }
104
105                 var widget = new ui.Dropdown(values, choices, {
106                         id: this.cbid(section_id),
107                         sort: true,
108                         multiple: this.multiple,
109                         optional: this.optional || this.rmempty,
110                         select_placeholder: E('em', _('unspecified')),
111                         display_items: this.display_size || this.size || 3,
112                         dropdown_items: this.dropdown_size || this.size || 5,
113                         validate: L.bind(this.validate, this, section_id),
114                         create: !this.nocreate,
115                         create_markup: '' +
116                                 '<li data-value="{{value}}">' +
117                                         '<span class="zonebadge" style="background:repeating-linear-gradient(45deg,rgba(204,204,204,0.5),rgba(204,204,204,0.5) 5px,rgba(255,255,255,0.5) 5px,rgba(255,255,255,0.5) 10px)">' +
118                                                 '<strong>{{value}}:</strong> <em>('+_('create')+')</em>' +
119                                         '</span>' +
120                                 '</li>'
121                 });
122
123                 return widget.render();
124         },
125 });
126
127 var CBIZoneForwards = form.DummyValue.extend({
128         __name__: 'CBI.ZoneForwards',
129
130         load: function(section_id) {
131                 return Promise.all([ firewall.getDefaults(), firewall.getZones(), network.getNetworks() ]).then(L.bind(function(dzn) {
132                         this.defaults = dzn[0];
133                         this.zones = dzn[1];
134                         this.networks = dzn[2];
135
136                         return this.super('load', section_id);
137                 }, this));
138         },
139
140         renderZone: function(zone) {
141                 var name = zone.getName(),
142                     networks = zone.getNetworks(),
143                     ifaces = [];
144
145                 for (var j = 0; j < networks.length; j++) {
146                         var network = this.networks.filter(function(net) { return net.getName() == networks[j] })[0];
147
148                         if (!network)
149                                 continue;
150
151                         var span = E('span', {
152                                 'class': 'ifacebadge' + (network.getName() == this.network ? ' ifacebadge-active' : '')
153                         }, network.getName() + ': ');
154
155                         var devices = network.isBridge() ? network.getDevices() : L.toArray(network.getDevice());
156
157                         for (var k = 0; k < devices.length && devices[k]; k++) {
158                                 span.appendChild(E('img', {
159                                         'title': devices[k].getI18n(),
160                                         'src': L.resource('icons/%s%s.png'.format(devices[k].getType(), devices[k].isUp() ? '' : '_disabled'))
161                                 }));
162                         }
163
164                         if (!devices.length)
165                                 span.appendChild(E('em', _('(empty)')));
166
167                         ifaces.push(span);
168                 }
169
170                 if (!ifaces.length)
171                         ifaces.push(E('span', { 'class': 'ifacebadge' }, E('em', _('(empty)'))));
172
173                 return E('label', {
174                         'class': 'zonebadge cbi-tooltip-container',
175                         'style': 'background-color:' + zone.getColor()
176                 }, [
177                         E('strong', name),
178                         E('div', { 'class': 'cbi-tooltip' }, ifaces)
179                 ]);
180         },
181
182         renderWidget: function(section_id, option_index, cfgvalue) {
183                 var value = (cfgvalue != null) ? cfgvalue : this.default,
184                     zone = this.zones.filter(function(z) { return z.getName() == value })[0];
185
186                 if (!zone)
187                         return E([]);
188
189                 var forwards = zone.getForwardingsBy('src'),
190                     dzones = [];
191
192                 for (var i = 0; i < forwards.length; i++) {
193                         var dzone = forwards[i].getDestinationZone();
194
195                         if (!dzone)
196                                 continue;
197
198                         dzones.push(this.renderZone(dzone));
199                 }
200
201                 if (!dzones.length)
202                         dzones.push(E('label', { 'class': 'zonebadge zonebadge-empty' },
203                                 E('strong', this.defaults.getForward())));
204
205                 return E('div', { 'class': 'zone-forwards' }, [
206                         E('div', { 'class': 'zone-src' }, this.renderZone(zone)),
207                         E('span', '⇒'),
208                         E('div', { 'class': 'zone-dest' }, dzones)
209                 ]);
210         },
211 });
212
213 var CBINetworkSelect = form.ListValue.extend({
214         __name__: 'CBI.NetworkSelect',
215
216         load: function(section_id) {
217                 return network.getNetworks().then(L.bind(function(networks) {
218                         this.networks = networks;
219
220                         return this.super('load', section_id);
221                 }, this));
222         },
223
224         filter: function(section_id, value) {
225                 return true;
226         },
227
228         renderIfaceBadge: function(network) {
229                 var span = E('span', { 'class': 'ifacebadge' }, network.getName() + ': '),
230                     devices = network.isBridge() ? network.getDevices() : L.toArray(network.getDevice());
231
232                 for (var j = 0; j < devices.length && devices[j]; j++) {
233                         span.appendChild(E('img', {
234                                 'title': devices[j].getI18n(),
235                                 'src': L.resource('icons/%s%s.png'.format(devices[j].getType(), devices[j].isUp() ? '' : '_disabled'))
236                         }));
237                 }
238
239                 if (!devices.length) {
240                         span.appendChild(E('em', { 'class': 'hide-close' }, _('(no interfaces attached)')));
241                         span.appendChild(E('em', { 'class': 'hide-open' }, '-'));
242                 }
243
244                 return span;
245         },
246
247         renderWidget: function(section_id, option_index, cfgvalue) {
248                 var values = L.toArray((cfgvalue != null) ? cfgvalue : this.default),
249                     choices = {},
250                     checked = {};
251
252                 for (var i = 0; i < values.length; i++)
253                         checked[values[i]] = true;
254
255                 values = [];
256
257                 if (!this.multiple && (this.rmempty || this.optional))
258                         choices[''] = E('em', _('unspecified'));
259
260                 for (var i = 0; i < this.networks.length; i++) {
261                         var network = this.networks[i],
262                             name = network.getName();
263
264                         if (name == 'loopback' || !this.filter(section_id, name))
265                                 continue;
266
267                         if (this.novirtual && network.isVirtual())
268                                 continue;
269
270                         if (checked[name])
271                                 values.push(name);
272
273                         choices[name] = this.renderIfaceBadge(network);
274                 }
275
276                 var widget = new ui.Dropdown(this.multiple ? values : values[0], choices, {
277                         id: this.cbid(section_id),
278                         sort: true,
279                         multiple: this.multiple,
280                         optional: this.optional || this.rmempty,
281                         select_placeholder: E('em', _('unspecified')),
282                         display_items: this.display_size || this.size || 3,
283                         dropdown_items: this.dropdown_size || this.size || 5,
284                         validate: L.bind(this.validate, this, section_id),
285                         create: !this.nocreate,
286                         create_markup: '' +
287                                 '<li data-value="{{value}}">' +
288                                         '<span class="ifacebadge" style="background:repeating-linear-gradient(45deg,rgba(204,204,204,0.5),rgba(204,204,204,0.5) 5px,rgba(255,255,255,0.5) 5px,rgba(255,255,255,0.5) 10px)">' +
289                                                 '{{value}}: <em>('+_('create')+')</em>' +
290                                         '</span>' +
291                                 '</li>'
292                 });
293
294                 return widget.render();
295         },
296
297         textvalue: function(section_id) {
298                 var cfgvalue = this.cfgvalue(section_id),
299                     values = L.toArray((cfgvalue != null) ? cfgvalue : this.default),
300                     rv = E([]);
301
302                 for (var i = 0; i < (this.networks || []).length; i++) {
303                         var network = this.networks[i],
304                             name = network.getName();
305
306                         if (values.indexOf(name) == -1)
307                                 continue;
308
309                         if (rv.length)
310                                 L.dom.append(rv, ' ');
311
312                         L.dom.append(rv, this.renderIfaceBadge(network));
313                 }
314
315                 if (!rv.firstChild)
316                         rv.appendChild(E('em', _('unspecified')));
317
318                 return rv;
319         },
320 });
321
322
323 return L.Class.extend({
324         ZoneSelect: CBIZoneSelect,
325         ZoneForwards: CBIZoneForwards,
326         NetworkSelect: CBINetworkSelect
327 });