js: switch to ubus rpc login instead of luci-login
[project/luci2/ui.git] / luci2 / htdocs / luci2 / luci2.js
1 /*
2         LuCI2 - OpenWrt Web Interface
3
4         Copyright 2013 Jo-Philipp Wich <jow@openwrt.org>
5
6         Licensed under the Apache License, Version 2.0 (the "License");
7         you may not use this file except in compliance with the License.
8         You may obtain a copy of the License at
9
10                 http://www.apache.org/licenses/LICENSE-2.0
11 */
12
13 String.prototype.format = function()
14 {
15         var html_esc = [/&/g, '&#38;', /"/g, '&#34;', /'/g, '&#39;', /</g, '&#60;', />/g, '&#62;'];
16         var quot_esc = [/"/g, '&#34;', /'/g, '&#39;'];
17
18         function esc(s, r) {
19                 for( var i = 0; i < r.length; i += 2 )
20                         s = s.replace(r[i], r[i+1]);
21                 return s;
22         }
23
24         var str = this;
25         var out = '';
26         var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X|q|h|j|t|m))/;
27         var a = b = [], numSubstitutions = 0, numMatches = 0;
28
29         while ((a = re.exec(str)) != null)
30         {
31                 var m = a[1];
32                 var leftpart = a[2], pPad = a[3], pJustify = a[4], pMinLength = a[5];
33                 var pPrecision = a[6], pType = a[7];
34
35                 numMatches++;
36
37                 if (pType == '%')
38                 {
39                         subst = '%';
40                 }
41                 else
42                 {
43                         if (numSubstitutions < arguments.length)
44                         {
45                                 var param = arguments[numSubstitutions++];
46
47                                 var pad = '';
48                                 if (pPad && pPad.substr(0,1) == "'")
49                                         pad = leftpart.substr(1,1);
50                                 else if (pPad)
51                                         pad = pPad;
52
53                                 var justifyRight = true;
54                                 if (pJustify && pJustify === "-")
55                                         justifyRight = false;
56
57                                 var minLength = -1;
58                                 if (pMinLength)
59                                         minLength = parseInt(pMinLength);
60
61                                 var precision = -1;
62                                 if (pPrecision && pType == 'f')
63                                         precision = parseInt(pPrecision.substring(1));
64
65                                 var subst = param;
66
67                                 switch(pType)
68                                 {
69                                         case 'b':
70                                                 subst = (parseInt(param) || 0).toString(2);
71                                                 break;
72
73                                         case 'c':
74                                                 subst = String.fromCharCode(parseInt(param) || 0);
75                                                 break;
76
77                                         case 'd':
78                                                 subst = (parseInt(param) || 0);
79                                                 break;
80
81                                         case 'u':
82                                                 subst = Math.abs(parseInt(param) || 0);
83                                                 break;
84
85                                         case 'f':
86                                                 subst = (precision > -1)
87                                                         ? ((parseFloat(param) || 0.0)).toFixed(precision)
88                                                         : (parseFloat(param) || 0.0);
89                                                 break;
90
91                                         case 'o':
92                                                 subst = (parseInt(param) || 0).toString(8);
93                                                 break;
94
95                                         case 's':
96                                                 subst = param;
97                                                 break;
98
99                                         case 'x':
100                                                 subst = ('' + (parseInt(param) || 0).toString(16)).toLowerCase();
101                                                 break;
102
103                                         case 'X':
104                                                 subst = ('' + (parseInt(param) || 0).toString(16)).toUpperCase();
105                                                 break;
106
107                                         case 'h':
108                                                 subst = esc(param, html_esc);
109                                                 break;
110
111                                         case 'q':
112                                                 subst = esc(param, quot_esc);
113                                                 break;
114
115                                         case 'j':
116                                                 subst = String.serialize(param);
117                                                 break;
118
119                                         case 't':
120                                                 var td = 0;
121                                                 var th = 0;
122                                                 var tm = 0;
123                                                 var ts = (param || 0);
124
125                                                 if (ts > 60) {
126                                                         tm = Math.floor(ts / 60);
127                                                         ts = (ts % 60);
128                                                 }
129
130                                                 if (tm > 60) {
131                                                         th = Math.floor(tm / 60);
132                                                         tm = (tm % 60);
133                                                 }
134
135                                                 if (th > 24) {
136                                                         td = Math.floor(th / 24);
137                                                         th = (th % 24);
138                                                 }
139
140                                                 subst = (td > 0)
141                                                         ? '%dd %dh %dm %ds'.format(td, th, tm, ts)
142                                                         : '%dh %dm %ds'.format(th, tm, ts);
143
144                                                 break;
145
146                                         case 'm':
147                                                 var mf = pMinLength ? parseInt(pMinLength) : 1000;
148                                                 var pr = pPrecision ? Math.floor(10*parseFloat('0'+pPrecision)) : 2;
149
150                                                 var i = 0;
151                                                 var val = parseFloat(param || 0);
152                                                 var units = [ '', 'K', 'M', 'G', 'T', 'P', 'E' ];
153
154                                                 for (i = 0; (i < units.length) && (val > mf); i++)
155                                                         val /= mf;
156
157                                                 subst = val.toFixed(pr) + ' ' + units[i];
158                                                 break;
159                                 }
160
161                                 subst = (typeof(subst) == 'undefined') ? '' : subst.toString();
162
163                                 if (minLength > 0 && pad.length > 0)
164                                         for (var i = 0; i < (minLength - subst.length); i++)
165                                                 subst = justifyRight ? (pad + subst) : (subst + pad);
166                         }
167                 }
168
169                 out += leftpart + subst;
170                 str = str.substr(m.length);
171         }
172
173         return out + str;
174 }
175
176 function LuCI2()
177 {
178         var _luci2 = this;
179
180         var alphacmp = function(a, b)
181         {
182                 if (a < b)
183                         return -1;
184                 else if (a > b)
185                         return 1;
186                 else
187                         return 0;
188         };
189
190         var retcb = function(cb, rv)
191         {
192                 if (typeof(cb) == 'function')
193                         cb(rv);
194
195                 return rv;
196         };
197
198         var isa = function(x, t)
199         {
200                 if (typeof(x) != 'string' && typeof(t) == 'string')
201                         return (Object.prototype.toString.call(x) == '[object ' + t + ']');
202
203                 return (Object.prototype.toString.call(x) == Object.prototype.toString.call(t));
204         };
205
206         var rcall = function(obj, func, params, res_attr, res_default, cb, filter)
207         {
208                 if (typeof(params) == 'undefined')
209                         params = { };
210
211                 return _luci2.rpc.call(obj, func, params).then(function(res) {
212                         if (res[0] != 0 || typeof(res[1]) == 'undefined')
213                                 return retcb(cb, res_default);
214
215                         var rv = (typeof(res_attr) != 'undefined') ? res[1][res_attr] : res[1];
216                         if (typeof(rv) == 'undefined' || (typeof(res_default) != 'undefined' && !isa(rv, res_default)))
217                                 return retcb(cb, res_default);
218
219                         if (typeof(filter) == 'function')
220                                 rv = filter(rv);
221
222                         return retcb(cb, rv);
223                 });
224         };
225
226         var Class = function() { };
227
228         Class.extend = function(properties)
229         {
230                 Class.initializing = true;
231
232                 var prototype = new this();
233                 var superprot = this.prototype;
234
235                 Class.initializing = false;
236
237                 $.extend(prototype, properties, {
238                         callSuper: function() {
239                                 var args = [ ];
240                                 var meth = arguments[0];
241
242                                 if (typeof(superprot[meth]) != 'function')
243                                         return undefined;
244
245                                 for (var i = 1; i < arguments.length; i++)
246                                         args.push(arguments[i]);
247
248                                 return superprot[meth].apply(this, args);
249                         }
250                 });
251
252                 function _class()
253                 {
254                         this.options = arguments[0] || { };
255
256                         if (!Class.initializing && typeof(this.init) == 'function')
257                                 this.init.apply(this, arguments);
258                 }
259
260                 _class.prototype = prototype;
261                 _class.prototype.constructor = _class;
262
263                 _class.extend = arguments.callee;
264
265                 return _class;
266         };
267
268         this.defaults = function(obj, def)
269         {
270                 for (var key in def)
271                         if (typeof(obj[key]) == 'undefined')
272                                 obj[key] = def[key];
273
274                 return obj;
275         };
276
277         this.deferred = function(x)
278         {
279                 return (typeof(x) == 'object' &&
280                         typeof(x.then) == 'function' &&
281                         typeof(x.promise) == 'function');
282         };
283
284         this.deferrable = function()
285         {
286                 if (this.deferred(arguments[0]))
287                         return arguments[0];
288
289                 var d = $.Deferred();
290                     d.resolve.apply(d, arguments);
291
292                 return d.promise();
293         };
294
295         this.i18n = {
296
297                 loaded: false,
298                 catalog: { },
299                 plural:  function(n) { return 0 + (n != 1) },
300
301                 init: function() {
302                         if (_luci2.i18n.loaded)
303                                 return;
304
305                         var lang = (navigator.userLanguage || navigator.language || 'en').toLowerCase();
306                         var langs = (lang.indexOf('-') > -1) ? [ lang, lang.split(/-/)[0] ] : [ lang ];
307
308                         for (var i = 0; i < langs.length; i++)
309                                 $.ajax('%s/i18n/base.%s.json'.format(_luci2.globals.resource, langs[i]), {
310                                         async:    false,
311                                         cache:    true,
312                                         dataType: 'json',
313                                         success:  function(data) {
314                                                 $.extend(_luci2.i18n.catalog, data);
315
316                                                 var pe = _luci2.i18n.catalog[''];
317                                                 if (pe)
318                                                 {
319                                                         delete _luci2.i18n.catalog[''];
320                                                         try {
321                                                                 var pf = new Function('n', 'return 0 + (' + pe + ')');
322                                                                 _luci2.i18n.plural = pf;
323                                                         } catch (e) { };
324                                                 }
325                                         }
326                                 });
327
328                         _luci2.i18n.loaded = true;
329                 }
330
331         };
332
333         this.tr = function(msgid)
334         {
335                 _luci2.i18n.init();
336
337                 var msgstr = _luci2.i18n.catalog[msgid];
338
339                 if (typeof(msgstr) == 'undefined')
340                         return msgid;
341                 else if (typeof(msgstr) == 'string')
342                         return msgstr;
343                 else
344                         return msgstr[0];
345         };
346
347         this.trp = function(msgid, msgid_plural, count)
348         {
349                 _luci2.i18n.init();
350
351                 var msgstr = _luci2.i18n.catalog[msgid];
352
353                 if (typeof(msgstr) == 'undefined')
354                         return (count == 1) ? msgid : msgid_plural;
355                 else if (typeof(msgstr) == 'string')
356                         return msgstr;
357                 else
358                         return msgstr[_luci2.i18n.plural(count)];
359         };
360
361         this.trc = function(msgctx, msgid)
362         {
363                 _luci2.i18n.init();
364
365                 var msgstr = _luci2.i18n.catalog[msgid + '\u0004' + msgctx];
366
367                 if (typeof(msgstr) == 'undefined')
368                         return msgid;
369                 else if (typeof(msgstr) == 'string')
370                         return msgstr;
371                 else
372                         return msgstr[0];
373         };
374
375         this.trcp = function(msgctx, msgid, msgid_plural, count)
376         {
377                 _luci2.i18n.init();
378
379                 var msgstr = _luci2.i18n.catalog[msgid + '\u0004' + msgctx];
380
381                 if (typeof(msgstr) == 'undefined')
382                         return (count == 1) ? msgid : msgid_plural;
383                 else if (typeof(msgstr) == 'string')
384                         return msgstr;
385                 else
386                         return msgstr[_luci2.i18n.plural(count)];
387         };
388
389         this.setHash = function(key, value)
390         {
391                 var h = '';
392                 var data = this.getHash(undefined);
393
394                 if (typeof(value) == 'undefined')
395                         delete data[key];
396                 else
397                         data[key] = value;
398
399                 var keys = [ ];
400                 for (var k in data)
401                         keys.push(k);
402
403                 keys.sort();
404
405                 for (var i = 0; i < keys.length; i++)
406                 {
407                         if (i > 0)
408                                 h += ',';
409
410                         h += keys[i] + ':' + data[keys[i]];
411                 }
412
413                 if (h)
414                         location.hash = '#' + h;
415         };
416
417         this.getHash = function(key)
418         {
419                 var data = { };
420                 var tuples = (location.hash || '#').substring(1).split(/,/);
421
422                 for (var i = 0; i < tuples.length; i++)
423                 {
424                         var tuple = tuples[i].split(/:/);
425                         if (tuple.length == 2)
426                                 data[tuple[0]] = tuple[1];
427                 }
428
429                 if (typeof(key) != 'undefined')
430                         return data[key];
431
432                 return data;
433         };
434
435         this.globals = {
436                 resource: '/luci2',
437                 sid:      '00000000000000000000000000000000'
438         };
439
440         this.rpc = {
441
442                 _msg_id: 1,
443
444                 _wrap_msg: function(method, object, func, args)
445                 {
446                         if (typeof(args) != 'object')
447                                 args = { };
448
449                         return {
450                                 id:      _luci2.rpc._msg_id++,
451                                 jsonrpc: "2.0",
452                                 method:  method,
453                                 params:  (method == 'call') ? [ _luci2.globals.sid, object, func, args ] : object
454                         };
455                 },
456
457                 _parse_response: function(keys, priv)
458                 {
459                         return function(data) {
460                                 var obj;
461                                 try {
462                                         obj = $.parseJSON(data);
463                                 } catch(e) { }
464
465                                 if (typeof(obj) != 'object')
466                                         return undefined;
467
468                                 /* is a batched response */
469                                 if (keys)
470                                 {
471                                         var rv = { };
472                                         for (var i = 0; i < obj.length; i++)
473                                         {
474                                                 var p = (typeof(priv) != 'undefined') ? priv[i] : undefined;
475
476                                                 if ($.isArray(obj[i].result) && typeof(priv) != 'undefined')
477                                                         obj[i].result[2] = p;
478
479                                                 if (obj[i].jsonrpc != '2.0' || obj[i].error || !obj[i].result)
480                                                         rv[keys[i]] = [ 4 /* UBUS_STATUS_NO_DATA */, undefined, p ];
481                                                 else
482                                                         rv[keys[i]] = obj[i].result;
483                                         }
484                                         return rv;
485                                 }
486
487                                 if (obj.jsonrpc != '2.0' || obj.error || !obj.result)
488                                         return [ 4 /* UBUS_STATUS_NO_DATA */, undefined, priv ];
489
490                                 if ($.isArray(obj.result) && typeof(priv) != 'undefined')
491                                         obj.result[2] = priv;
492
493                                 return obj.result;
494                         };
495                 },
496
497                 _post_msg: function(message, cb, keys, priv)
498                 {
499                         return $.ajax('/ubus', {
500                                 cache:       false,
501                                 contentType: 'application/json',
502                                 data:        JSON.stringify(message),
503                                 dataFilter:  _luci2.rpc._parse_response(keys, priv),
504                                 dataType:    'text',
505                                 success:     cb,
506                                 type:        'POST'
507                         });
508                 },
509
510                 _post_single: function(object, method, args, cb, priv)
511                 {
512                         var msg = _luci2.rpc._wrap_msg('call', object, method, args, priv);
513                         return _luci2.rpc._post_msg(msg, cb, undefined, priv);
514                 },
515
516                 _post_batch: function(methods, cb)
517                 {
518                         if (typeof(methods) != 'object')
519                                 return undefined;
520
521                         var msgs = [ ];
522                         var keys = [ ];
523                         var priv = [ ];
524
525                         for (var k in methods)
526                         {
527                                 if (typeof(methods[k]) != 'object' || methods[k].length < 2)
528                                         continue;
529
530                                 keys.push(k);
531                                 priv.push(methods[k][3]);
532                                 msgs.push(_luci2.rpc._wrap_msg('call', methods[k][0], methods[k][1], methods[k][2]));
533                         }
534
535                         if (msgs.length > 0)
536                                 return _luci2.rpc._post_msg(msgs, cb, keys, priv);
537
538                         return _luci2.deferrable([ ]);
539                 },
540
541                 call: function()
542                 {
543                         var a = arguments;
544                         if (typeof a[0] == 'string')
545                                 return _luci2.rpc._post_single(a[0], a[1], a[2], a[3], a[4]);
546                         else
547                                 return _luci2.rpc._post_batch(a[0], a[1]);
548                 },
549
550                 list: function(objects)
551                 {
552                         var msg = _luci2.rpc._wrap_msg('list', objects);
553                         return _luci2.rpc._post_msg(msg);
554                 },
555
556                 access: function(scope, object, method, cb)
557                 {
558                         return _luci2.rpc._post_single('session', 'access', {
559                                 'sid':      _luci2.globals.sid,
560                                 'scope':        scope,
561                                 'object':   object,
562                                 'function': method
563                         }, function(rv) {
564                                 return retcb(cb, (rv[0] == 0 && rv[1] && rv[1].access == true));
565                         });
566                 }
567         };
568
569         this.uci = {
570
571                 writable: function(cb)
572                 {
573                         return _luci2.rpc.access('ubus', 'uci', 'commit', cb);
574                 },
575
576                 add: function(config, type, cb)
577                 {
578                         return rcall('uci', 'add', { config: config, type: type }, 'section', '', cb);
579                 },
580
581                 apply: function()
582                 {
583
584                 },
585
586                 changes: function(config)
587                 {
588                         return rcall('uci', 'changes', { config: config }, 'changes', [ ], cb);
589                 },
590
591                 commit: function(config)
592                 {
593                         return rcall('uci', 'commit', { config: config }, undefined, undefined, cb);
594                 },
595
596                 'delete': function(config, section, option)
597                 {
598                         var req = { config: config, section: section };
599
600                         if (isa(option, 'Array'))
601                                 req.options = option;
602                         else
603                                 req.option = option;
604
605                         return rcall('uci', 'delete', req, undefined, undefined, cb);
606                 },
607
608                 delete_all: function(config, type, matches)
609                 {
610                         return rcall('uci', 'delete', { config: config, type: type, match: matches }, undefined, undefined, cb);
611                 },
612
613                 foreach: function(config, type, cb)
614                 {
615                         return rcall('uci', 'get', { config: config, type: type }, 'values', { }, function(sections) {
616                                 for (var s in sections)
617                                         cb(sections[s]);
618                         });
619                 },
620
621                 get: function(config, section, option, cb)
622                 {
623                         return rcall('uci', 'get', { config: config, section: section, option: option }, undefined, { }, function(res) {
624                                 if (typeof(option) == 'undefined')
625                                         return retcb(cb, (res.values && res.values['.type']) ? res.values['.type'] : undefined);
626
627                                 return retcb(cb, res.value);
628                         });
629                 },
630
631                 get_all: function(config, section, cb)
632                 {
633                         return rcall('uci', 'get', { config: config, section: section }, 'values', { }, cb);
634                 },
635
636                 get_first: function(config, type, option, cb)
637                 {
638                         return rcall('uci', 'get', { config: config, type: type }, 'values', { }, function(sections) {
639                                 for (var s in sections)
640                                 {
641                                         var val = (typeof(option) == 'string') ? sections[s][option] : sections[s]['.name'];
642
643                                         if (typeof(val) != 'undefined')
644                                                 return retcb(cb, val);
645                                 }
646
647                                 return retcb(cb, undefined);
648                         });
649                 },
650
651                 section: function(config, type, name, values, cb)
652                 {
653                         return rcall('uci', 'add', { config: config, type: type, name: name, values: values }, 'section', undefined, cb);
654                 },
655
656                 set: function(config, section, option, value, cb)
657                 {
658                         if (typeof(value) == 'undefined' && typeof(option) == 'string')
659                                 return rcall('uci', 'add', { config: config, section: section, type: option }, undefined, undefined, cb);
660                         else if (isa(option, 'Object'))
661                                 return rcall('uci', 'set', { config: config, section: section, values: option }, undefined, undefined, cb);
662                         else
663
664                         var values = { };
665                             values[option] = value;
666
667                         return rcall('uci', 'set', { config: config, section: section, values: values }, undefined, undefined, cb);
668                 },
669
670                 order: function(config, sections, cb)
671                 {
672                         return rcall('uci', 'order', { config: config, sections: sections }, undefined, undefined, cb);
673                 }
674         };
675
676         this.network = {
677                 getNetworkStatus: function(cb)
678                 {
679                         var ifaces = [ ];
680                         var assign = function(target, key)
681                         {
682                                 return function(value) {
683                                         if (typeof(value) != 'undefined' && !$.isEmptyObject(value))
684                                                 target[key] = value;
685                                 };
686                         };
687
688                         return _luci2.rpc.list().then(function(data) {
689                                 var requests = [ ];
690
691                                 for (var i = 0; i < data.length; i++)
692                                 {
693                                         if (data[i].indexOf('network.interface.') != 0)
694                                                 continue;
695
696                                         var ifname = data[i].substring(18);
697                                         if (ifname == 'loopback')
698                                                 continue;
699
700                                         var iface = { 'name': ifname };
701
702                                         ifaces.push(iface);
703                                         requests.push(['network.interface', 'status', { 'interface': ifname }, iface]);
704                                 }
705
706                                 return _luci2.rpc.call(requests, function(responses) {
707                                         for (var key in responses)
708                                                 if (responses[key][0] == 0 && responses[key][1] && responses[key][2])
709                                                         $.extend(responses[key][2], responses[key][1]);
710                                 });
711                         }).then(function() {
712                                 var requests = [ ];
713
714                                 for (var i = 0; i < ifaces.length; i++)
715                                 {
716                                         var iface = ifaces[i];
717
718                                         var dev = iface.l3_device || iface.l2_device;
719                                         if (!dev)
720                                                 continue;
721
722                                         iface.device = { 'name': dev };
723                                         requests[dev] = ['network.device', 'status', { 'name': dev }, iface.device];
724                                 }
725
726                                 return _luci2.rpc.call(requests, function(responses) {
727                                         for (var key in responses)
728                                                 if (responses[key][0] == 0 && responses[key][1] && responses[key][2])
729                                                         $.extend(responses[key][2], responses[key][1]);
730                                 });
731                         }).then(function() {
732                                 var requests = [ ];
733
734                                 for (var i = 0; i < ifaces.length; i++)
735                                 {
736                                         var iface = ifaces[i];
737                                         if (!iface.device)
738                                                 continue;
739
740                                         var subdevs = iface.device['bridge-members'];
741                                         if (!subdevs)
742                                                 continue;
743
744                                         iface.subdevices = [ ];
745                                         for (var j = 0; j < subdevs.length; j++)
746                                         {
747                                                 iface.subdevices[j] = { 'name': subdevs[j] };
748                                                 requests.push(['network.device', 'status', { 'name': subdevs[j] }, iface.subdevices[j]]);
749                                         }
750                                 }
751
752                                 return _luci2.rpc.call(requests, function(responses) {
753                                         for (var key in responses)
754                                                 if (responses[key][0] == 0 && responses[key][1] && responses[key][2])
755                                                         $.extend(responses[key][2], responses[key][1]);
756                                 });
757                         }).then(function() {
758                                 var requests = [ ];
759
760                                 for (var i = 0; i < ifaces.length; i++)
761                                 {
762                                         var iface = ifaces[i];
763
764                                         if (iface.device)
765                                                 requests.push(['iwinfo', 'info', { 'device': iface.device.name }, iface.device]);
766
767                                         if (iface.subdevices)
768                                                 for (var j = 0; j < iface.subdevices.length; j++)
769                                                         requests.push(['iwinfo', 'info', { 'device': iface.subdevices[j].name }, iface.subdevices[j]]);
770                                 }
771
772                                 return _luci2.rpc.call(requests, function(responses) {
773                                         for (var key in responses)
774                                                 if (responses[key][0] == 0 && responses[key][1] && responses[key][2])
775                                                         if (!$.isEmptyObject(responses[key][1]))
776                                                                 responses[key][2].wireless = responses[key][1];
777                                 });
778                         }).then(function() {
779                                 ifaces.sort(function(a, b) {
780                                         if (a['interface'] < b['interface'])
781                                                 return -1;
782                                         else if (a['interface'] > b['interface'])
783                                                 return 1;
784                                         else
785                                                 return 0;
786                                 });
787                                 return retcb(cb, ifaces);
788                         });
789                 },
790
791                 findWanInterfaces: function(cb)
792                 {
793                         return _luci2.rpc.list().then(function(data) {
794                                 var requests = { };
795                                 for (var i = 0; i < data.length; i++)
796                                 {
797                                         if (data[i].indexOf('network.interface.') == 0)
798                                         {
799                                                 var ifname = data[i].substring(18);
800                                                 requests[ifname] = ['network.interface', 'status', { 'interface': ifname }];
801                                         }
802                                 }
803                                 return _luci2.rpc.call(requests);
804                         }).then(function(responses) {
805                                 var rv = [ ];
806                                 for (var ifname in responses)
807                                 {
808                                         var response = responses[ifname];
809
810                                         if (response[0] != 0 || !response[1] || !response[1].route)
811                                                 continue;
812
813                                         for (var rn = 0, rt = response[1].route[rn];
814                                                  rn < response[1].route.length;
815                                                  rn++, rt = response[1].route[rn])
816                                         {
817                                                 if (typeof(rt.table) != 'undefined')
818                                                         continue;
819
820                                                 if (rt.target == '0.0.0.0' && rt.mask == 0)
821                                                         rv[0] = response[1];
822                                                 else if (rt.target == '::' && rt.mask == 0)
823                                                         rv[1] = response[1];
824                                         }
825                                 }
826
827                                 return retcb(cb, rv);
828                         });
829                 },
830
831                 getDHCPLeases: function(cb)
832                 {
833                         return rcall('luci2.network', 'dhcp_leases', undefined, 'leases', [ ], cb);
834                 },
835
836                 getDHCPv6Leases: function(cb)
837                 {
838                         return rcall('luci2.network', 'dhcp6_leases', undefined, 'leases', [ ], cb);
839                 },
840
841                 getRoutes: function(cb)
842                 {
843                         return rcall('luci2.network', 'routes', undefined, 'routes', [ ], cb);
844                 },
845
846                 getIPv6Routes: function(cb)
847                 {
848                         return rcall('luci2.network', 'routes6', undefined, 'routes', [ ], cb);
849                 },
850
851                 getARPTable: function(cb)
852                 {
853                         return rcall('luci2.network', 'arp_table', undefined, 'entries', [ ], cb);
854                 },
855
856                 getInterfaceStatus: function(iface, cb)
857                 {
858                         return rcall('network.interface', 'status', { 'interface': iface }, undefined, { }, cb, function(rv) {
859                                 rv['interface'] = iface;
860                                 rv['l2_device'] = rv['device'];
861                                 return rv;
862                         });
863                 },
864
865                 getDeviceStatus: function(dev, cb)
866                 {
867                         return rcall('network.device', 'status', { name: dev }, undefined, { }, cb, function(rv) {
868                                 if (typeof(dev) == 'string')
869                                         rv.device = dev;
870                                 return rv;
871                         });
872                 },
873
874                 getConntrackCount: function(cb)
875                 {
876                         return rcall('luci2.network', 'conntrack_count', undefined, undefined, {
877                                 count: 0,
878                                 limit: 0
879                         }, cb);
880                 }
881         };
882
883         this.wireless = {
884                 getDevices: function(cb) {
885                         return rcall('iwinfo', 'devices', undefined, 'devices', [ ], cb, function(rv) {
886                                 rv.sort();
887                                 return rv;
888                         });
889                 },
890
891                 getInfo: function(dev, cb) {
892                         var parse_info = function(device, info, rv)
893                         {
894                                 if (!rv[info.phy])
895                                         rv[info.phy] = {
896                                                 networks: [ ]
897                                         };
898
899                                 var phy = rv[info.phy];
900
901                                 var phy_attrs = [
902                                         'country', 'channel', 'frequency', 'frequency_offset',
903                                         'txpower', 'txpower_offset', 'hwmodes', 'hardware', 'phy'
904                                 ];
905
906                                 var net_attrs = [
907                                         'ssid', 'bssid', 'mode', 'quality', 'quality_max',
908                                         'signal', 'noise', 'bitrate', 'encryption'
909                                 ];
910
911                                 for (var i = 0; i < phy_attrs.length; i++)
912                                         phy[phy_attrs[i]] = info[phy_attrs[i]];
913
914                                 var net = {
915                                         device: device
916                                 };
917
918                                 for (var i = 0; i < net_attrs.length; i++)
919                                         net[net_attrs[i]] = info[net_attrs[i]];
920
921                                 phy.networks.push(net);
922
923                                 return phy;
924                         };
925
926                         if (!dev)
927                         {
928                                 return _luci2.wireless.getDevices().then(function(devices) {
929                                         var requests = [ ];
930
931                                         for (var i = 0; i < devices.length; i++)
932                                         {
933                                                 if (devices[i].indexOf('.sta') >= 0)
934                                                         continue;
935
936                                                 requests[devices[i]] = [ 'iwinfo', 'info', { device: devices[i] } ];
937                                         }
938
939                                         return _luci2.rpc.call(requests);
940                                 }).then(function(responses) {
941                                         var rv = { };
942
943                                         for (var device in responses)
944                                         {
945                                                 var response = responses[device];
946
947                                                 if (response[0] != 0 || !response[1])
948                                                         continue;
949
950                                                 parse_info(device, response[1], rv);
951                                         }
952
953                                         return retcb(cb, rv);
954                                 });
955                         }
956
957                         return _luci2.rpc.call('iwinfo', 'info', { device: dev }).then(function(response) {
958                                 if (response[0] != 0 || !response[1])
959                                         return retcb(cb, { });
960
961                                 return retcb(cb, parse_info(dev, response[1], { }));
962                         });
963                 },
964
965                 getAssocList: function(dev, cb)
966                 {
967                         if (!dev)
968                         {
969                                 return _luci2.wireless.getDevices().then(function(devices) {
970                                         var requests = { };
971
972                                         for (var i = 0; i < devices.length; i++)
973                                         {
974                                                 if (devices[i].indexOf('.sta') >= 0)
975                                                         continue;
976
977                                                 requests[devices[i]] = [ 'iwinfo', 'assoclist', { device: devices[i] } ];
978                                         }
979
980                                         return _luci2.rpc.call(requests);
981                                 }).then(function(responses) {
982                                         var rv = [ ];
983
984                                         for (var device in responses)
985                                         {
986                                                 var response = responses[device];
987
988                                                 if (response[0] != 0 || !response[1] || !response[1].results)
989                                                         continue;
990
991                                                 for (var i = 0; i < response[1].results.length; i++)
992                                                 {
993                                                         var station = response[1].results[i];
994
995                                                         station.device = device;
996                                                         rv.push(station);
997                                                 }
998                                         }
999
1000                                         rv.sort(function(a, b) {
1001                                                 return (a.device == b.device)
1002                                                         ? (a.bssid < b.bssid)
1003                                                         : (a.device > b.device)
1004                                                 ;
1005                                         });
1006
1007                                         return retcb(cb, rv);
1008                                 });
1009                         }
1010
1011                         return _luci2.rpc.call('iwinfo', 'assoclist', { device: dev }).then(function(response) {
1012                                 var rv = [ ];
1013
1014                                 if (response[0] != 0 || !response[1] || !response[1].results)
1015                                         return retcb(cb, rv);
1016
1017                                 for (var i = 0; i < response[1].results.length; i++)
1018                                 {
1019                                         var station = response[1].results[i];
1020
1021                                         station.device = dev;
1022                                         rv.push(station);
1023                                 }
1024
1025                                 rv.sort(function(a, b) {
1026                                         return (a.bssid < b.bssid);
1027                                 });
1028
1029                                 return retcb(cb, rv);
1030                         });
1031                 },
1032
1033                 formatEncryption: function(enc)
1034                 {
1035                         var format_list = function(l, s)
1036                         {
1037                                 var rv = [ ];
1038                                 for (var i = 0; i < l.length; i++)
1039                                         rv.push(l[i].toUpperCase());
1040                                 return rv.join(s ? s : ', ');
1041                         }
1042
1043                         if (!enc || !enc.enabled)
1044                                 return _luci2.tr('None');
1045
1046                         if (enc.wep)
1047                         {
1048                                 if (enc.wep.length == 2)
1049                                         return _luci2.tr('WEP Open/Shared') + ' (%s)'.format(format_list(enc.ciphers, ', '));
1050                                 else if (enc.wep[0] == 'shared')
1051                                         return _luci2.tr('WEP Shared Auth') + ' (%s)'.format(format_list(enc.ciphers, ', '));
1052                                 else
1053                                         return _luci2.tr('WEP Open System') + ' (%s)'.format(format_list(enc.ciphers, ', '));
1054                         }
1055                         else if (enc.wpa)
1056                         {
1057                                 if (enc.wpa.length == 2)
1058                                         return _luci2.tr('mixed WPA/WPA2') + ' %s (%s)'.format(
1059                                                 format_list(enc.authentication, '/'),
1060                                                 format_list(enc.ciphers, ', ')
1061                                         );
1062                                 else if (enc.wpa[0] == 2)
1063                                         return 'WPA2 %s (%s)'.format(
1064                                                 format_list(enc.authentication, '/'),
1065                                                 format_list(enc.ciphers, ', ')
1066                                         );
1067                                 else
1068                                         return 'WPA %s (%s)'.format(
1069                                                 format_list(enc.authentication, '/'),
1070                                                 format_list(enc.ciphers, ', ')
1071                                         );
1072                         }
1073
1074                         return _luci2.tr('Unknown');
1075                 }
1076         };
1077
1078         this.system = {
1079                 getInfo: function(cb)
1080                 {
1081                         return _luci2.rpc.call({
1082                                 info:  [ 'system', 'info',  { } ],
1083                                 board: [ 'system', 'board', { } ],
1084                                 disk:  [ 'luci2.system', 'diskfree', { } ]
1085                         }).then(function(responses) {
1086                                 var rv = { };
1087
1088                                 if (responses.info[0] == 0)
1089                                         $.extend(rv, responses.info[1]);
1090
1091                                 if (responses.board[0] == 0)
1092                                         $.extend(rv, responses.board[1]);
1093
1094                                 if (responses.disk[0] == 0)
1095                                         $.extend(rv, responses.disk[1]);
1096
1097                                 return retcb(cb, rv);
1098                         });
1099                 },
1100
1101                 getProcessList: function(cb)
1102                 {
1103                         return rcall('luci2.system', 'process_list', undefined, 'processes', [ ], cb, function(rv) {
1104                                 rv.sort(function(a, b) { return a.pid - b.pid });
1105                                 return rv;
1106                         });
1107                 },
1108
1109                 getSystemLog: function(cb)
1110                 {
1111                         return rcall('luci2.system', 'syslog', undefined, 'log', '', cb);
1112                 },
1113
1114                 getKernelLog: function(cb)
1115                 {
1116                         return rcall('luci2.system', 'dmesg', undefined, 'log', '', cb);
1117                 },
1118
1119                 getZoneInfo: function(cb)
1120                 {
1121                         return $.getJSON(_luci2.globals.resource + '/zoneinfo.json', cb);
1122                 },
1123
1124                 canSendSignal: function(cb)
1125                 {
1126                         return _luci2.rpc.access('ubus', 'luci2.system', 'process_signal', cb);
1127                 },
1128
1129                 sendSignal: function(pid, sig, cb)
1130                 {
1131                         return _luci2.rpc.call('luci2.system', 'process_signal', { pid: pid, signal: sig }).then(function(response) {
1132                                 return retcb(cb, response[0] == 0);
1133                         });
1134                 },
1135
1136                 initList: function(cb)
1137                 {
1138                         return rcall('luci2.system', 'init_list', undefined, 'initscripts', [ ], cb, function(rv) {
1139                                 rv.sort(function(a, b) { return (a.start || 0) - (b.start || 0) });
1140                                 return rv;
1141                         });
1142                 },
1143
1144                 initEnabled: function(init, cb)
1145                 {
1146                         return this.initList(function(list) {
1147                                 for (var i = 0; i < list.length; i++)
1148                                         if (list[i].name == init)
1149                                                 return retcb(cb, !!list[i].enabled);
1150
1151                                 return retcb(cb, false);
1152                         });
1153                 },
1154
1155                 initRun: function(init, action, cb)
1156                 {
1157                         return _luci2.rpc.call('luci2.system', 'init_action', { name: init, action: action }).then(function(response) {
1158                                 return retcb(cb, response[0] == 0);
1159                         });
1160                 },
1161
1162                 canInitRun: function(cb)
1163                 {
1164                         return _luci2.rpc.access('ubus', 'luci2.system', 'init_action', cb);
1165                 },
1166
1167                 initStart:   function(init, cb) { return _luci2.system.initRun(init, 'start',   cb) },
1168                 initStop:    function(init, cb) { return _luci2.system.initRun(init, 'stop',    cb) },
1169                 initRestart: function(init, cb) { return _luci2.system.initRun(init, 'restart', cb) },
1170                 initReload:  function(init, cb) { return _luci2.system.initRun(init, 'reload',  cb) },
1171                 initEnable:  function(init, cb) { return _luci2.system.initRun(init, 'enable',  cb) },
1172                 initDisable: function(init, cb) { return _luci2.system.initRun(init, 'disable', cb) },
1173
1174
1175                 getRcLocal: function(cb)
1176                 {
1177                         return rcall('luci2.system', 'rclocal_get', undefined, 'data', '', cb);
1178                 },
1179
1180                 setRcLocal: function(data, cb)
1181                 {
1182                         return rcall('luci2.system', 'rclocal_set', { data: data }, undefined, undefined, cb);
1183                 },
1184
1185                 canSetRcLocal: function(cb)
1186                 {
1187                         return _luci2.rpc.access('ubus', 'luci2.system', 'rclocal_set', cb);
1188                 },
1189
1190
1191                 getCrontab: function(cb)
1192                 {
1193                         return rcall('luci2.system', 'crontab_get', undefined, 'data', '', cb);
1194                 },
1195
1196                 setCrontab: function(data, cb)
1197                 {
1198                         return rcall('luci2.system', 'crontab_set', { data: data }, undefined, undefined, cb);
1199                 },
1200
1201                 canSetCrontab: function(cb)
1202                 {
1203                         return _luci2.rpc.access('ubus', 'luci2.system', 'crontab_set', cb);
1204                 },
1205
1206
1207                 getSSHKeys: function(cb)
1208                 {
1209                         return rcall('luci2.system', 'sshkeys_get', undefined, 'keys', [ ], cb);
1210                 },
1211
1212                 setSSHKeys: function(keys, cb)
1213                 {
1214                         return rcall('luci2.system', 'sshkeys_set', { keys: keys }, undefined, undefined, cb);
1215                 },
1216
1217                 canSetSSHKeys: function(cb)
1218                 {
1219                         return _luci2.rpc.access('ubus', 'luci2.system', 'sshkeys_set', cb);
1220                 },
1221
1222
1223                 setPassword: function(user, pass, cb)
1224                 {
1225                         return rcall('luci2.system', 'password_set', { user: user, password: pass }, undefined, undefined, cb);
1226                 },
1227
1228                 canSetPassword: function(cb)
1229                 {
1230                         return _luci2.rpc.access('ubus', 'luci2.system', 'password_set', cb);
1231                 },
1232
1233
1234                 listLEDs: function(cb)
1235                 {
1236                         return rcall('luci2.system', 'led_list', undefined, 'leds', [ ], cb);
1237                 },
1238
1239                 listUSBDevices: function(cb)
1240                 {
1241                         return rcall('luci2.system', 'usb_list', undefined, 'devices', [ ], cb);
1242                 },
1243
1244
1245                 testUpgrade: function(cb)
1246                 {
1247                         return rcall('luci2.system', 'upgrade_test', undefined, undefined, { }, cb);
1248                 },
1249
1250                 startUpgrade: function(keep, cb)
1251                 {
1252                         return rcall('luci2.system', 'upgrade_start', { keep: !!keep }, undefined, undefined, cb);
1253                 },
1254
1255                 cleanUpgrade: function(cb)
1256                 {
1257                         return rcall('luci2.system', 'upgrade_clean', undefined, undefined, undefined, cb);
1258                 },
1259
1260                 canUpgrade: function(cb)
1261                 {
1262                         return _luci2.rpc.access('ubus', 'luci2.system', 'upgrade_start', cb);
1263                 },
1264
1265
1266                 restoreBackup: function(cb)
1267                 {
1268                         return rcall('luci2.system', 'backup_restore', undefined, undefined, undefined, cb);
1269                 },
1270
1271                 cleanBackup: function(cb)
1272                 {
1273                         return rcall('luci2.system', 'backup_clean', undefined, undefined, undefined, cb);
1274                 },
1275
1276                 canRestoreBackup: function(cb)
1277                 {
1278                         return _luci2.rpc.access('ubus', 'luci2.system', 'backup_restore', cb);
1279                 },
1280
1281
1282                 getBackupConfig: function(cb)
1283                 {
1284                         return rcall('luci2.system', 'backup_config_get', undefined, 'config', '', cb);
1285                 },
1286
1287                 setBackupConfig: function(data, cb)
1288                 {
1289                         return rcall('luci2.system', 'backup_config_set', { data: data }, undefined, undefined, cb);
1290                 },
1291
1292                 canSetBackupConfig: function(cb)
1293                 {
1294                         return _luci2.rpc.access('ubus', 'luci2.system', 'backup_config_set', cb);
1295                 },
1296
1297
1298                 listBackup: function(cb)
1299                 {
1300                         return rcall('luci2.system', 'backup_list', undefined, 'files', [ ], cb);
1301                 },
1302
1303
1304                 performReboot: function(cb)
1305                 {
1306                         return rcall('luci2.system', 'reboot', undefined, undefined, undefined, cb);
1307                 },
1308
1309                 canPerformReboot: function(cb)
1310                 {
1311                         return _luci2.rpc.access('ubus', 'luci2.system', 'reboot', cb);
1312                 }
1313         };
1314
1315         this.opkg = {
1316                 updateLists: function(cb)
1317                 {
1318                         return rcall('luci2.opkg', 'update', undefined, undefined, { }, cb);
1319                 },
1320
1321                 _fetchPackages: function(action, offset, limit, pattern, cb)
1322                 {
1323                         var packages = [ ];
1324                         var reqlimit = Math.min(limit, 100);
1325
1326                         if (reqlimit <= 0)
1327                                 reqlimit = 100;
1328
1329                         return _luci2.rpc.call('luci2.opkg', action, { offset: offset, limit: reqlimit, pattern: pattern }).then(function(response) {
1330                                 if (response[0] != 0 || !response[1] || !response[1].total)
1331                                         return retcb(cb, { length: 0, total: 0 });
1332
1333                                 packages.push.apply(packages, response[1].packages);
1334                                 packages.total = response[1].total;
1335
1336                                 if (limit <= 0)
1337                                         limit = response[1].total;
1338
1339                                 if (packages.length >= limit)
1340                                         return retcb(cb, packages);
1341
1342                                 var requests = [ ];
1343                                 for (var i = offset + packages.length; i < limit; i += 100)
1344                                         requests.push(['luci2.opkg', action, { offset: i, limit: (Math.min(i + 100, limit) % 100) || 100, pattern: pattern }]);
1345
1346                                 return _luci2.rpc.call(requests);
1347                         }).then(function(responses) {
1348                                 for (var key in responses)
1349                                 {
1350                                         var response = responses[key];
1351
1352                                         if (response[0] != 0 || !response[1] || !response[1].packages)
1353                                                 continue;
1354
1355                                         packages.push.apply(packages, response[1].packages);
1356                                         packages.total = response[1].total;
1357                                 }
1358
1359                                 return retcb(cb, packages);
1360                         });
1361                 },
1362
1363                 listPackages: function(offset, limit, pattern, cb)
1364                 {
1365                         return _luci2.opkg._fetchPackages('list', offset, limit, pattern, cb);
1366                 },
1367
1368                 installedPackages: function(offset, limit, pattern, cb)
1369                 {
1370                         return _luci2.opkg._fetchPackages('list_installed', offset, limit, pattern, cb);
1371                 },
1372
1373                 findPackages: function(offset, limit, pattern, cb)
1374                 {
1375                         return _luci2.opkg._fetchPackages('find', offset, limit, pattern, cb);
1376                 },
1377
1378                 installPackage: function(name, cb)
1379                 {
1380                         return rcall('luci2.opkg', 'install', { 'package': name }, undefined, { }, cb);
1381                 },
1382
1383                 removePackage: function(name, cb)
1384                 {
1385                         return rcall('luci2.opkg', 'remove', { 'package': name }, undefined, { }, cb);
1386                 },
1387
1388                 getConfig: function(cb)
1389                 {
1390                         return rcall('luci2.opkg', 'config_get', undefined, 'config', '', cb);
1391                 },
1392
1393                 setConfig: function(data, cb)
1394                 {
1395                         return rcall('luci2.opkg', 'config_set', { data: data }, undefined, undefined, cb);
1396                 },
1397
1398                 canInstallPackage: function(cb)
1399                 {
1400                         return _luci2.rpc.access('ubus', 'luci2.opkg', 'install', cb);
1401                 },
1402
1403                 canRemovePackage: function(cb)
1404                 {
1405                         return _luci2.rpc.access('ubus', 'luci2.opkg', 'remove', cb);
1406                 },
1407
1408                 canSetConfig: function(cb)
1409                 {
1410                         return _luci2.rpc.access('ubus', 'luci2.opkg', 'config_set', cb);
1411                 }
1412         };
1413
1414         this.ui = {
1415
1416                 loading: function(enable)
1417                 {
1418                         var win = $(window);
1419                         var body = $('body');
1420                         var div = _luci2._modal || (
1421                                 _luci2._modal = $('<div />')
1422                                         .addClass('cbi-modal-loader')
1423                                         .append($('<div />').text(_luci2.tr('Loading data...')))
1424                                         .appendTo(body)
1425                         );
1426
1427                         if (enable)
1428                         {
1429                                 body.css('overflow', 'hidden');
1430                                 body.css('padding', 0);
1431                                 body.css('width', win.width());
1432                                 body.css('height', win.height());
1433                                 div.css('width', win.width());
1434                                 div.css('height', win.height());
1435                                 div.show();
1436                         }
1437                         else
1438                         {
1439                                 div.hide();
1440                                 body.css('overflow', '');
1441                                 body.css('padding', '');
1442                                 body.css('width', '');
1443                                 body.css('height', '');
1444                         }
1445                 },
1446
1447                 dialog: function(title, content, options)
1448                 {
1449                         var win = $(window);
1450                         var body = $('body');
1451                         var div = _luci2._dialog || (
1452                                 _luci2._dialog = $('<div />')
1453                                         .addClass('cbi-modal-dialog')
1454                                         .append($('<div />')
1455                                                 .append($('<div />')
1456                                                         .addClass('cbi-modal-dialog-header'))
1457                                                 .append($('<div />')
1458                                                         .addClass('cbi-modal-dialog-body'))
1459                                                 .append($('<div />')
1460                                                         .addClass('cbi-modal-dialog-footer')
1461                                                         .append($('<button />')
1462                                                                 .addClass('cbi-button')
1463                                                                 .text(_luci2.tr('Close'))
1464                                                                 .click(function() {
1465                                                                         $('body')
1466                                                                                 .css('overflow', '')
1467                                                                                 .css('padding', '')
1468                                                                                 .css('width', '')
1469                                                                                 .css('height', '');
1470
1471                                                                         $(this).parent().parent().parent().hide();
1472                                                                 }))))
1473                                         .appendTo(body)
1474                         );
1475
1476                         if (typeof(options) != 'object')
1477                                 options = { };
1478
1479                         if (title === false)
1480                         {
1481                                 body
1482                                         .css('overflow', '')
1483                                         .css('padding', '')
1484                                         .css('width', '')
1485                                         .css('height', '');
1486
1487                                 _luci2._dialog.hide();
1488
1489                                 return;
1490                         }
1491
1492                         var cnt = div.children().children('div.cbi-modal-dialog-body');
1493                         var ftr = div.children().children('div.cbi-modal-dialog-footer');
1494
1495                         ftr.empty();
1496
1497                         if (options.style == 'confirm')
1498                         {
1499                                 ftr.append($('<button />')
1500                                         .addClass('cbi-button')
1501                                         .text(_luci2.tr('Ok'))
1502                                         .click(options.confirm || function() { _luci2.ui.dialog(false) }));
1503
1504                                 ftr.append($('<button />')
1505                                         .addClass('cbi-button')
1506                                         .text(_luci2.tr('Cancel'))
1507                                         .click(options.cancel || function() { _luci2.ui.dialog(false) }));
1508                         }
1509                         else if (options.style == 'close')
1510                         {
1511                                 ftr.append($('<button />')
1512                                         .addClass('cbi-button')
1513                                         .text(_luci2.tr('Close'))
1514                                         .click(options.close || function() { _luci2.ui.dialog(false) }));
1515                         }
1516                         else if (options.style == 'wait')
1517                         {
1518                                 ftr.append($('<button />')
1519                                         .addClass('cbi-button')
1520                                         .text(_luci2.tr('Close'))
1521                                         .attr('disabled', true));
1522                         }
1523
1524                         div.find('div.cbi-modal-dialog-header').text(title);
1525                         div.show();
1526
1527                         cnt
1528                                 .css('max-height', Math.floor(win.height() * 0.70) + 'px')
1529                                 .empty()
1530                                 .append(content);
1531
1532                         div.children()
1533                                 .css('margin-top', -Math.floor(div.children().height() / 2) + 'px');
1534
1535                         body.css('overflow', 'hidden');
1536                         body.css('padding', 0);
1537                         body.css('width', win.width());
1538                         body.css('height', win.height());
1539                         div.css('width', win.width());
1540                         div.css('height', win.height());
1541                 },
1542
1543                 upload: function(title, content, options)
1544                 {
1545                         var form = _luci2._upload || (
1546                                 _luci2._upload = $('<form />')
1547                                         .attr('method', 'post')
1548                                         .attr('action', '/cgi-bin/luci-upload')
1549                                         .attr('enctype', 'multipart/form-data')
1550                                         .attr('target', 'cbi-fileupload-frame')
1551                                         .append($('<p />'))
1552                                         .append($('<input />')
1553                                                 .attr('type', 'hidden')
1554                                                 .attr('name', 'sessionid')
1555                                                 .attr('value', _luci2.globals.sid))
1556                                         .append($('<input />')
1557                                                 .attr('type', 'hidden')
1558                                                 .attr('name', 'filename')
1559                                                 .attr('value', options.filename))
1560                                         .append($('<input />')
1561                                                 .attr('type', 'file')
1562                                                 .attr('name', 'filedata')
1563                                                 .addClass('cbi-input-file'))
1564                                         .append($('<div />')
1565                                                 .css('width', '100%')
1566                                                 .addClass('progressbar')
1567                                                 .addClass('intermediate')
1568                                                 .append($('<div />')
1569                                                         .css('width', '100%')))
1570                                         .append($('<iframe />')
1571                                                 .attr('name', 'cbi-fileupload-frame')
1572                                                 .css('width', '1px')
1573                                                 .css('height', '1px')
1574                                                 .css('visibility', 'hidden'))
1575                         );
1576
1577                         var finish = _luci2._upload_finish_cb || (
1578                                 _luci2._upload_finish_cb = function(ev) {
1579                                         $(this).off('load');
1580
1581                                         var body = (this.contentDocument || this.contentWindow.document).body;
1582                                         if (body.firstChild.tagName.toLowerCase() == 'pre')
1583                                                 body = body.firstChild;
1584
1585                                         var json;
1586                                         try {
1587                                                 json = $.parseJSON(body.innerHTML);
1588                                         } catch(e) {
1589                                                 json = {
1590                                                         message: _luci2.tr('Invalid server response received'),
1591                                                         error: [ -1, _luci2.tr('Invalid data') ]
1592                                                 };
1593                                         };
1594
1595                                         if (json.error)
1596                                         {
1597                                                 L.ui.dialog(L.tr('File upload'), [
1598                                                         $('<p />').text(_luci2.tr('The file upload failed with the server response below:')),
1599                                                         $('<pre />').addClass('alert-message').text(json.message || json.error[1]),
1600                                                         $('<p />').text(_luci2.tr('In case of network problems try uploading the file again.'))
1601                                                 ], { style: 'close' });
1602                                         }
1603                                         else if (typeof(ev.data.cb) == 'function')
1604                                         {
1605                                                 ev.data.cb(json);
1606                                         }
1607                                 }
1608                         );
1609
1610                         var confirm = _luci2._upload_confirm_cb || (
1611                                 _luci2._upload_confirm_cb = function() {
1612                                         var d = _luci2._upload;
1613                                         var f = d.find('.cbi-input-file');
1614                                         var b = d.find('.progressbar');
1615                                         var p = d.find('p');
1616
1617                                         if (!f.val())
1618                                                 return;
1619
1620                                         d.find('iframe').on('load', { cb: options.success }, finish);
1621                                         d.submit();
1622
1623                                         f.hide();
1624                                         b.show();
1625                                         p.text(_luci2.tr('File upload in progress â€¦'));
1626
1627                                         _luci2._dialog.find('button').prop('disabled', true);
1628                                 }
1629                         );
1630
1631                         _luci2._upload.find('.progressbar').hide();
1632                         _luci2._upload.find('.cbi-input-file').val('').show();
1633                         _luci2._upload.find('p').text(content || _luci2.tr('Select the file to upload and press "%s" to proceed.').format(_luci2.tr('Ok')));
1634
1635                         _luci2.ui.dialog(title || _luci2.tr('File upload'), _luci2._upload, {
1636                                 style: 'confirm',
1637                                 confirm: confirm
1638                         });
1639                 },
1640
1641                 reconnect: function()
1642                 {
1643                         var protocols = (location.protocol == 'https:') ? [ 'http', 'https' ] : [ 'http' ];
1644                         var ports     = (location.protocol == 'https:') ? [ 80, location.port || 443 ] : [ location.port || 80 ];
1645                         var address   = location.hostname.match(/^[A-Fa-f0-9]*:[A-Fa-f0-9:]+$/) ? '[' + location.hostname + ']' : location.hostname;
1646                         var images    = $();
1647                         var interval, timeout;
1648
1649                         _luci2.ui.dialog(
1650                                 _luci2.tr('Waiting for device'), [
1651                                         $('<p />').text(_luci2.tr('Please stand by while the device is reconfiguring â€¦')),
1652                                         $('<div />')
1653                                                 .css('width', '100%')
1654                                                 .addClass('progressbar')
1655                                                 .addClass('intermediate')
1656                                                 .append($('<div />')
1657                                                         .css('width', '100%'))
1658                                 ], { style: 'wait' }
1659                         );
1660
1661                         for (var i = 0; i < protocols.length; i++)
1662                                 images = images.add($('<img />').attr('url', protocols[i] + '://' + address + ':' + ports[i]));
1663
1664                         //_luci2.network.getNetworkStatus(function(s) {
1665                         //      for (var i = 0; i < protocols.length; i++)
1666                         //      {
1667                         //              for (var j = 0; j < s.length; j++)
1668                         //              {
1669                         //                      for (var k = 0; k < s[j]['ipv4-address'].length; k++)
1670                         //                              images = images.add($('<img />').attr('url', protocols[i] + '://' + s[j]['ipv4-address'][k].address + ':' + ports[i]));
1671                         //
1672                         //                      for (var l = 0; l < s[j]['ipv6-address'].length; l++)
1673                         //                              images = images.add($('<img />').attr('url', protocols[i] + '://[' + s[j]['ipv6-address'][l].address + ']:' + ports[i]));
1674                         //              }
1675                         //      }
1676                         //}).then(function() {
1677                                 images.on('load', function() {
1678                                         var url = this.getAttribute('url');
1679                                         _luci2.rpc.access('ubus', 'session', 'access').then(function(response) {
1680                                                 if (response[0] == 0)
1681                                                 {
1682                                                         window.clearTimeout(timeout);
1683                                                         window.clearInterval(interval);
1684                                                         _luci2.ui.dialog(false);
1685                                                         images = null;
1686                                                 }
1687                                                 else
1688                                                 {
1689                                                         location.href = url;
1690                                                 }
1691                                         });
1692                                 });
1693
1694                                 interval = window.setInterval(function() {
1695                                         images.each(function() {
1696                                                 this.setAttribute('src', this.getAttribute('url') + _luci2.globals.resource + '/icons/loading.gif?r=' + Math.random());
1697                                         });
1698                                 }, 5000);
1699
1700                                 timeout = window.setTimeout(function() {
1701                                         window.clearInterval(interval);
1702                                         images.off('load');
1703
1704                                         _luci2.ui.dialog(
1705                                                 _luci2.tr('Device not responding'),
1706                                                 _luci2.tr('The device was not responding within 180 seconds, you might need to manually reconnect your computer or use SSH to regain access.'),
1707                                                 { style: 'close' }
1708                                         );
1709                                 }, 180000);
1710                         //});
1711                 },
1712
1713                 login: function(invalid)
1714                 {
1715                         if (!_luci2._login_deferred || _luci2._login_deferred.state() != 'pending')
1716                                 _luci2._login_deferred = $.Deferred();
1717
1718                         /* try to find sid from hash */
1719                         var sid = _luci2.getHash('id');
1720                         if (sid && sid.match(/^[a-f0-9]{32}$/))
1721                         {
1722                                 _luci2.globals.sid = sid;
1723                                 _luci2.rpc.access('ubus', 'session', 'access').then(function(response) {
1724                                         if (response[0] == 0)
1725                                         {
1726                                                 _luci2._login_deferred.resolve();
1727                                         }
1728                                         else
1729                                         {
1730                                                 _luci2.setHash('id', undefined);
1731                                                 _luci2.ui.login();
1732                                         }
1733                                 });
1734
1735                                 return _luci2._login_deferred;
1736                         }
1737
1738                         var form = _luci2._login || (
1739                                 _luci2._login = $('<div />')
1740                                         .append($('<p />')
1741                                                 .addClass('alert-message')
1742                                                 .text(_luci2.tr('Wrong username or password given!')))
1743                                         .append($('<p />')
1744                                                 .append($('<label />')
1745                                                         .text(_luci2.tr('Username'))
1746                                                         .append($('<br />'))
1747                                                         .append($('<input />')
1748                                                                 .attr('type', 'text')
1749                                                                 .attr('name', 'username')
1750                                                                 .attr('value', 'root')
1751                                                                 .addClass('cbi-input-text'))))
1752                                         .append($('<p />')
1753                                                 .append($('<label />')
1754                                                         .text(_luci2.tr('Password'))
1755                                                         .append($('<br />'))
1756                                                         .append($('<input />')
1757                                                                 .attr('type', 'password')
1758                                                                 .attr('name', 'password')
1759                                                                 .addClass('cbi-input-password'))))
1760                                         .append($('<p />')
1761                                                 .text(_luci2.tr('Enter your username and password above, then click "%s" to proceed.').format(_luci2.tr('Ok'))))
1762                         );
1763
1764                         var response_cb = _luci2._login_response_cb || (
1765                                 _luci2._login_response_cb = function(response) {
1766                                         if (!response.sid)
1767                                         {
1768                                                 _luci2.ui.login(true);
1769                                         }
1770                                         else
1771                                         {
1772                                                 _luci2.globals.sid = response.sid;
1773                                                 _luci2.setHash('id', _luci2.globals.sid);
1774
1775                                                 _luci2.ui.dialog(false);
1776                                                 _luci2._login_deferred.resolve();
1777                                         }
1778                                 }
1779                         );
1780
1781                         var confirm_cb = _luci2._login_confirm_cb || (
1782                                 _luci2._login_confirm_cb = function() {
1783                                         var d = _luci2._login;
1784                                         var u = d.find('[name=username]').val();
1785                                         var p = d.find('[name=password]').val();
1786
1787                                         if (!u)
1788                                                 return;
1789
1790                                         _luci2.ui.dialog(
1791                                                 _luci2.tr('Logging in'), [
1792                                                         $('<p />').text(_luci2.tr('Log in in progress â€¦')),
1793                                                         $('<div />')
1794                                                                 .css('width', '100%')
1795                                                                 .addClass('progressbar')
1796                                                                 .addClass('intermediate')
1797                                                                 .append($('<div />')
1798                                                                         .css('width', '100%'))
1799                                                 ], { style: 'wait' }
1800                                         );
1801
1802                                         rcall('session', 'login', { username: u, password: p }, undefined, { }, response_cb);
1803                                 }
1804                         );
1805
1806                         if (invalid)
1807                                 form.find('.alert-message').show();
1808                         else
1809                                 form.find('.alert-message').hide();
1810
1811                         _luci2.ui.dialog(_luci2.tr('Authorization Required'), form, {
1812                                 style: 'confirm',
1813                                 confirm: confirm_cb
1814                         });
1815
1816                         return _luci2._login_deferred;
1817                 },
1818
1819                 renderMainMenu: function()
1820                 {
1821                         return rcall('luci2.ui', 'menu', undefined, 'menu', { }, function(entries) {
1822                                 _luci2.globals.mainMenu = new _luci2.ui.menu();
1823                                 _luci2.globals.mainMenu.entries(entries);
1824
1825                                 $('#mainmenu')
1826                                         .empty()
1827                                         .append(_luci2.globals.mainMenu.render(0, 1));
1828                         });
1829                 },
1830
1831                 renderViewMenu: function()
1832                 {
1833                         $('#viewmenu')
1834                                 .empty()
1835                                 .append(_luci2.globals.mainMenu.render(2, 900));
1836                 },
1837
1838                 renderView: function(node)
1839                 {
1840                         var name = node.view.split(/\//).join('.');
1841
1842                         _luci2.ui.renderViewMenu();
1843
1844                         if (!_luci2._views)
1845                                 _luci2._views = { };
1846
1847                         _luci2.setHash('view', node.view);
1848
1849                         if (_luci2._views[name] instanceof _luci2.ui.view)
1850                                 return _luci2._views[name].render();
1851
1852                         return $.ajax(_luci2.globals.resource + '/view/' + name + '.js', {
1853                                 method: 'GET',
1854                                 cache: true,
1855                                 dataType: 'text'
1856                         }).then(function(data) {
1857                                 try {
1858                                         var viewConstructor = (new Function(['L', '$'], 'return ' + data))(_luci2, $);
1859
1860                                         _luci2._views[name] = new viewConstructor({
1861                                                 name: name,
1862                                                 acls: node.write || { }
1863                                         });
1864
1865                                         return _luci2._views[name].render();
1866                                 }
1867                                 catch(e) { };
1868
1869                                 return $.Deferred().resolve();
1870                         });
1871                 },
1872
1873                 init: function()
1874                 {
1875                         _luci2.ui.loading(true);
1876
1877                         $.when(
1878                                 _luci2.ui.renderMainMenu()
1879                         ).then(function() {
1880                                 _luci2.ui.renderView(_luci2.globals.defaultNode).then(function() {
1881                                         _luci2.ui.loading(false);
1882                                 })
1883                         });
1884                 }
1885         };
1886
1887         var AbstractWidget = Class.extend({
1888                 i18n: function(text) {
1889                         return text;
1890                 },
1891
1892                 toString: function() {
1893                         var x = document.createElement('div');
1894                                 x.appendChild(this.render());
1895
1896                         return x.innerHTML;
1897                 },
1898
1899                 insertInto: function(id) {
1900                         return $(id).empty().append(this.render());
1901                 }
1902         });
1903
1904         this.ui.view = AbstractWidget.extend({
1905                 _fetch_template: function()
1906                 {
1907                         return $.ajax(_luci2.globals.resource + '/template/' + this.options.name + '.htm', {
1908                                 method: 'GET',
1909                                 cache: true,
1910                                 dataType: 'text',
1911                                 success: function(data) {
1912                                         data = data.replace(/<%([#:=])?(.+?)%>/g, function(match, p1, p2) {
1913                                                 p2 = p2.replace(/^\s+/, '').replace(/\s+$/, '');
1914                                                 switch (p1)
1915                                                 {
1916                                                 case '#':
1917                                                         return '';
1918
1919                                                 case ':':
1920                                                         return _luci2.tr(p2);
1921
1922                                                 case '=':
1923                                                         return _luci2.globals[p2] || '';
1924
1925                                                 default:
1926                                                         return '(?' + match + ')';
1927                                                 }
1928                                         });
1929
1930                                         $('#maincontent').append(data);
1931                                 }
1932                         });
1933                 },
1934
1935                 execute: function()
1936                 {
1937                         throw "Not implemented";
1938                 },
1939
1940                 render: function()
1941                 {
1942                         var container = $('#maincontent');
1943
1944                         container.empty();
1945
1946                         if (this.title)
1947                                 container.append($('<h2 />').append(this.title));
1948
1949                         if (this.description)
1950                                 container.append($('<div />').addClass('cbi-map-descr').append(this.description));
1951
1952                         var self = this;
1953                         return this._fetch_template().then(function() {
1954                                 return _luci2.deferrable(self.execute());
1955                         });
1956                 }
1957         });
1958
1959         this.ui.menu = AbstractWidget.extend({
1960                 init: function() {
1961                         this._nodes = { };
1962                 },
1963
1964                 entries: function(entries)
1965                 {
1966                         for (var entry in entries)
1967                         {
1968                                 var path = entry.split(/\//);
1969                                 var node = this._nodes;
1970
1971                                 for (i = 0; i < path.length; i++)
1972                                 {
1973                                         if (!node.childs)
1974                                                 node.childs = { };
1975
1976                                         if (!node.childs[path[i]])
1977                                                 node.childs[path[i]] = { };
1978
1979                                         node = node.childs[path[i]];
1980                                 }
1981
1982                                 $.extend(node, entries[entry]);
1983                         }
1984                 },
1985
1986                 _indexcmp: function(a, b)
1987                 {
1988                         var x = a.index || 0;
1989                         var y = b.index || 0;
1990                         return (x - y);
1991                 },
1992
1993                 firstChildView: function(node)
1994                 {
1995                         if (node.view)
1996                                 return node;
1997
1998                         var nodes = [ ];
1999                         for (var child in (node.childs || { }))
2000                                 nodes.push(node.childs[child]);
2001
2002                         nodes.sort(this._indexcmp);
2003
2004                         for (var i = 0; i < nodes.length; i++)
2005                         {
2006                                 var child = this.firstChildView(nodes[i]);
2007                                 if (child)
2008                                 {
2009                                         $.extend(node, child);
2010                                         return node;
2011                                 }
2012                         }
2013
2014                         return undefined;
2015                 },
2016
2017                 _onclick: function(ev)
2018                 {
2019                         _luci2.ui.loading(true);
2020                         _luci2.ui.renderView(ev.data).then(function() {
2021                                 _luci2.ui.loading(false);
2022                         });
2023
2024                         ev.preventDefault();
2025                         this.blur();
2026                 },
2027
2028                 _render: function(childs, level, min, max)
2029                 {
2030                         var nodes = [ ];
2031                         for (var node in childs)
2032                         {
2033                                 var child = this.firstChildView(childs[node]);
2034                                 if (child)
2035                                         nodes.push(childs[node]);
2036                         }
2037
2038                         nodes.sort(this._indexcmp);
2039
2040                         var list = $('<ul />');
2041
2042                         if (level == 0)
2043                                 list.addClass('nav');
2044                         else if (level == 1)
2045                                 list.addClass('dropdown-menu');
2046
2047                         for (var i = 0; i < nodes.length; i++)
2048                         {
2049                                 if (!_luci2.globals.defaultNode)
2050                                 {
2051                                         var v = _luci2.getHash('view');
2052                                         if (!v || v == nodes[i].view)
2053                                                 _luci2.globals.defaultNode = nodes[i];
2054                                 }
2055
2056                                 var item = $('<li />')
2057                                         .append($('<a />')
2058                                                 .attr('href', '#')
2059                                                 .text(_luci2.tr(nodes[i].title))
2060                                                 .click(nodes[i], this._onclick))
2061                                         .appendTo(list);
2062
2063                                 if (nodes[i].childs && level < max)
2064                                 {
2065                                         item.addClass('dropdown');
2066                                         item.find('a').addClass('menu');
2067                                         item.append(this._render(nodes[i].childs, level + 1));
2068                                 }
2069                         }
2070
2071                         return list.get(0);
2072                 },
2073
2074                 render: function(min, max)
2075                 {
2076                         var top = min ? this.getNode(_luci2.globals.defaultNode.view, min) : this._nodes;
2077                         return this._render(top.childs, 0, min, max);
2078                 },
2079
2080                 getNode: function(path, max)
2081                 {
2082                         var p = path.split(/\//);
2083                         var n = this._nodes;
2084
2085                         if (typeof(max) == 'undefined')
2086                                 max = p.length;
2087
2088                         for (var i = 0; i < max; i++)
2089                         {
2090                                 if (!n.childs[p[i]])
2091                                         return undefined;
2092
2093                                 n = n.childs[p[i]];
2094                         }
2095
2096                         return n;
2097                 }
2098         });
2099
2100         this.ui.table = AbstractWidget.extend({
2101                 init: function()
2102                 {
2103                         this._rows = [ ];
2104                 },
2105
2106                 row: function(values)
2107                 {
2108                         if (isa(values, 'Array'))
2109                         {
2110                                 this._rows.push(values);
2111                         }
2112                         else if (isa(values, 'Object'))
2113                         {
2114                                 var v = [ ];
2115                                 for (var i = 0; i < this.options.columns.length; i++)
2116                                 {
2117                                         var col = this.options.columns[i];
2118
2119                                         if (typeof col.key == 'string')
2120                                                 v.push(values[col.key]);
2121                                         else
2122                                                 v.push(null);
2123                                 }
2124                                 this._rows.push(v);
2125                         }
2126                 },
2127
2128                 rows: function(rows)
2129                 {
2130                         for (var i = 0; i < rows.length; i++)
2131                                 this.row(rows[i]);
2132                 },
2133
2134                 render: function(id)
2135                 {
2136                         var fieldset = document.createElement('fieldset');
2137                                 fieldset.className = 'cbi-section';
2138
2139                         if (this.options.caption)
2140                         {
2141                                 var legend = document.createElement('legend');
2142                                 $(legend).append(this.options.caption);
2143                                 fieldset.appendChild(legend);
2144                         }
2145
2146                         var table = document.createElement('table');
2147                                 table.className = 'cbi-section-table';
2148
2149                         var has_caption = false;
2150                         var has_description = false;
2151
2152                         for (var i = 0; i < this.options.columns.length; i++)
2153                                 if (this.options.columns[i].caption)
2154                                 {
2155                                         has_caption = true;
2156                                         break;
2157                                 }
2158                                 else if (this.options.columns[i].description)
2159                                 {
2160                                         has_description = true;
2161                                         break;
2162                                 }
2163
2164                         if (has_caption)
2165                         {
2166                                 var tr = table.insertRow(-1);
2167                                         tr.className = 'cbi-section-table-titles';
2168
2169                                 for (var i = 0; i < this.options.columns.length; i++)
2170                                 {
2171                                         var col = this.options.columns[i];
2172                                         var th = document.createElement('th');
2173                                                 th.className = 'cbi-section-table-cell';
2174
2175                                         tr.appendChild(th);
2176
2177                                         if (col.width)
2178                                                 th.style.width = col.width;
2179
2180                                         if (col.align)
2181                                                 th.style.textAlign = col.align;
2182
2183                                         if (col.caption)
2184                                                 $(th).append(col.caption);
2185                                 }
2186                         }
2187
2188                         if (has_description)
2189                         {
2190                                 var tr = table.insertRow(-1);
2191                                         tr.className = 'cbi-section-table-descr';
2192
2193                                 for (var i = 0; i < this.options.columns.length; i++)
2194                                 {
2195                                         var col = this.options.columns[i];
2196                                         var th = document.createElement('th');
2197                                                 th.className = 'cbi-section-table-cell';
2198
2199                                         tr.appendChild(th);
2200
2201                                         if (col.width)
2202                                                 th.style.width = col.width;
2203
2204                                         if (col.align)
2205                                                 th.style.textAlign = col.align;
2206
2207                                         if (col.description)
2208                                                 $(th).append(col.description);
2209                                 }
2210                         }
2211
2212                         if (this._rows.length == 0)
2213                         {
2214                                 if (this.options.placeholder)
2215                                 {
2216                                         var tr = table.insertRow(-1);
2217                                         var td = tr.insertCell(-1);
2218                                                 td.className = 'cbi-section-table-cell';
2219
2220                                         td.colSpan = this.options.columns.length;
2221                                         $(td).append(this.options.placeholder);
2222                                 }
2223                         }
2224                         else
2225                         {
2226                                 for (var i = 0; i < this._rows.length; i++)
2227                                 {
2228                                         var tr = table.insertRow(-1);
2229
2230                                         for (var j = 0; j < this.options.columns.length; j++)
2231                                         {
2232                                                 var col = this.options.columns[j];
2233                                                 var td = tr.insertCell(-1);
2234
2235                                                 var val = this._rows[i][j];
2236
2237                                                 if (typeof(val) == 'undefined')
2238                                                         val = col.placeholder;
2239
2240                                                 if (typeof(val) == 'undefined')
2241                                                         val = '';
2242
2243                                                 if (col.width)
2244                                                         td.style.width = col.width;
2245
2246                                                 if (col.align)
2247                                                         td.style.textAlign = col.align;
2248
2249                                                 if (typeof col.format == 'string')
2250                                                         $(td).append(col.format.format(val));
2251                                                 else if (typeof col.format == 'function')
2252                                                         $(td).append(col.format(val, i));
2253                                                 else
2254                                                         $(td).append(val);
2255                                         }
2256                                 }
2257                         }
2258
2259                         this._rows = [ ];
2260                         fieldset.appendChild(table);
2261
2262                         return fieldset;
2263                 }
2264         });
2265
2266         this.ui.progress = AbstractWidget.extend({
2267                 render: function()
2268                 {
2269                         var vn = parseInt(this.options.value) || 0;
2270                         var mn = parseInt(this.options.max) || 100;
2271                         var pc = Math.floor((100 / mn) * vn);
2272
2273                         var bar = document.createElement('div');
2274                                 bar.className = 'progressbar';
2275
2276                         bar.appendChild(document.createElement('div'));
2277                         bar.lastChild.appendChild(document.createElement('div'));
2278                         bar.lastChild.style.width = pc + '%';
2279
2280                         if (typeof(this.options.format) == 'string')
2281                                 $(bar.lastChild.lastChild).append(this.options.format.format(this.options.value, this.options.max, pc));
2282                         else if (typeof(this.options.format) == 'function')
2283                                 $(bar.lastChild.lastChild).append(this.options.format(pc));
2284                         else
2285                                 $(bar.lastChild.lastChild).append('%.2f%%'.format(pc));
2286
2287                         return bar;
2288                 }
2289         });
2290
2291         this.ui.devicebadge = AbstractWidget.extend({
2292                 render: function()
2293                 {
2294                         var dev = this.options.l3_device || this.options.device || '?';
2295
2296                         var span = document.createElement('span');
2297                                 span.className = 'ifacebadge';
2298
2299                         if (typeof(this.options.signal) == 'number' ||
2300                                 typeof(this.options.noise) == 'number')
2301                         {
2302                                 var r = 'none';
2303                                 if (typeof(this.options.signal) != 'undefined' &&
2304                                         typeof(this.options.noise) != 'undefined')
2305                                 {
2306                                         var q = (-1 * (this.options.noise - this.options.signal)) / 5;
2307                                         if (q < 1)
2308                                                 r = '0';
2309                                         else if (q < 2)
2310                                                 r = '0-25';
2311                                         else if (q < 3)
2312                                                 r = '25-50';
2313                                         else if (q < 4)
2314                                                 r = '50-75';
2315                                         else
2316                                                 r = '75-100';
2317                                 }
2318
2319                                 span.appendChild(document.createElement('img'));
2320                                 span.lastChild.src = _luci2.globals.resource + '/icons/signal-' + r + '.png';
2321
2322                                 if (r == 'none')
2323                                         span.title = _luci2.tr('No signal');
2324                                 else
2325                                         span.title = '%s: %d %s / %s: %d %s'.format(
2326                                                 _luci2.tr('Signal'), this.options.signal, _luci2.tr('dBm'),
2327                                                 _luci2.tr('Noise'), this.options.noise, _luci2.tr('dBm')
2328                                         );
2329                         }
2330                         else
2331                         {
2332                                 var type = 'ethernet';
2333                                 var desc = _luci2.tr('Ethernet device');
2334
2335                                 if (this.options.l3_device != this.options.device)
2336                                 {
2337                                         type = 'tunnel';
2338                                         desc = _luci2.tr('Tunnel interface');
2339                                 }
2340                                 else if (dev.indexOf('br-') == 0)
2341                                 {
2342                                         type = 'bridge';
2343                                         desc = _luci2.tr('Bridge');
2344                                 }
2345                                 else if (dev.indexOf('.') > 0)
2346                                 {
2347                                         type = 'vlan';
2348                                         desc = _luci2.tr('VLAN interface');
2349                                 }
2350                                 else if (dev.indexOf('wlan') == 0 ||
2351                                                  dev.indexOf('ath') == 0 ||
2352                                                  dev.indexOf('wl') == 0)
2353                                 {
2354                                         type = 'wifi';
2355                                         desc = _luci2.tr('Wireless Network');
2356                                 }
2357
2358                                 span.appendChild(document.createElement('img'));
2359                                 span.lastChild.src = _luci2.globals.resource + '/icons/' + type + (this.options.up ? '' : '_disabled') + '.png';
2360                                 span.title = desc;
2361                         }
2362
2363                         $(span).append(' ');
2364                         $(span).append(dev);
2365
2366                         return span;
2367                 }
2368         });
2369
2370         var type = function(f, l)
2371         {
2372                 f.message = l;
2373                 return f;
2374         };
2375
2376         this.cbi = {
2377                 validation: {
2378                         i18n: function(msg)
2379                         {
2380                                 _luci2.cbi.validation.message = _luci2.tr(msg);
2381                         },
2382
2383                         compile: function(code)
2384                         {
2385                                 var pos = 0;
2386                                 var esc = false;
2387                                 var depth = 0;
2388                                 var types = _luci2.cbi.validation.types;
2389                                 var stack = [ ];
2390
2391                                 code += ',';
2392
2393                                 for (var i = 0; i < code.length; i++)
2394                                 {
2395                                         if (esc)
2396                                         {
2397                                                 esc = false;
2398                                                 continue;
2399                                         }
2400
2401                                         switch (code.charCodeAt(i))
2402                                         {
2403                                         case 92:
2404                                                 esc = true;
2405                                                 break;
2406
2407                                         case 40:
2408                                         case 44:
2409                                                 if (depth <= 0)
2410                                                 {
2411                                                         if (pos < i)
2412                                                         {
2413                                                                 var label = code.substring(pos, i);
2414                                                                         label = label.replace(/\\(.)/g, '$1');
2415                                                                         label = label.replace(/^[ \t]+/g, '');
2416                                                                         label = label.replace(/[ \t]+$/g, '');
2417
2418                                                                 if (label && !isNaN(label))
2419                                                                 {
2420                                                                         stack.push(parseFloat(label));
2421                                                                 }
2422                                                                 else if (label.match(/^(['"]).*\1$/))
2423                                                                 {
2424                                                                         stack.push(label.replace(/^(['"])(.*)\1$/, '$2'));
2425                                                                 }
2426                                                                 else if (typeof types[label] == 'function')
2427                                                                 {
2428                                                                         stack.push(types[label]);
2429                                                                         stack.push(null);
2430                                                                 }
2431                                                                 else
2432                                                                 {
2433                                                                         throw "Syntax error, unhandled token '"+label+"'";
2434                                                                 }
2435                                                         }
2436                                                         pos = i+1;
2437                                                 }
2438                                                 depth += (code.charCodeAt(i) == 40);
2439                                                 break;
2440
2441                                         case 41:
2442                                                 if (--depth <= 0)
2443                                                 {
2444                                                         if (typeof stack[stack.length-2] != 'function')
2445                                                                 throw "Syntax error, argument list follows non-function";
2446
2447                                                         stack[stack.length-1] =
2448                                                                 arguments.callee(code.substring(pos, i));
2449
2450                                                         pos = i+1;
2451                                                 }
2452                                                 break;
2453                                         }
2454                                 }
2455
2456                                 return stack;
2457                         }
2458                 }
2459         };
2460
2461         var validation = this.cbi.validation;
2462
2463         validation.types = {
2464                 'integer': function()
2465                 {
2466                         if (this.match(/^-?[0-9]+$/) != null)
2467                                 return true;
2468
2469                         validation.i18n('Must be a valid integer');
2470                         return false;
2471                 },
2472
2473                 'uinteger': function()
2474                 {
2475                         if (validation.types['integer'].apply(this) && (this >= 0))
2476                                 return true;
2477
2478                         validation.i18n('Must be a positive integer');
2479                         return false;
2480                 },
2481
2482                 'float': function()
2483                 {
2484                         if (!isNaN(parseFloat(this)))
2485                                 return true;
2486
2487                         validation.i18n('Must be a valid number');
2488                         return false;
2489                 },
2490
2491                 'ufloat': function()
2492                 {
2493                         if (validation.types['float'].apply(this) && (this >= 0))
2494                                 return true;
2495
2496                         validation.i18n('Must be a positive number');
2497                         return false;
2498                 },
2499
2500                 'ipaddr': function()
2501                 {
2502                         if (validation.types['ip4addr'].apply(this) ||
2503                                 validation.types['ip6addr'].apply(this))
2504                                 return true;
2505
2506                         validation.i18n('Must be a valid IP address');
2507                         return false;
2508                 },
2509
2510                 'ip4addr': function()
2511                 {
2512                         if (this.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})(\/(\S+))?$/))
2513                         {
2514                                 if ((RegExp.$1 >= 0) && (RegExp.$1 <= 255) &&
2515                                     (RegExp.$2 >= 0) && (RegExp.$2 <= 255) &&
2516                                     (RegExp.$3 >= 0) && (RegExp.$3 <= 255) &&
2517                                     (RegExp.$4 >= 0) && (RegExp.$4 <= 255) &&
2518                                     ((RegExp.$6.indexOf('.') < 0)
2519                                       ? ((RegExp.$6 >= 0) && (RegExp.$6 <= 32))
2520                                       : (validation.types['ip4addr'].apply(RegExp.$6))))
2521                                         return true;
2522                         }
2523
2524                         validation.i18n('Must be a valid IPv4 address');
2525                         return false;
2526                 },
2527
2528                 'ip6addr': function()
2529                 {
2530                         if (this.match(/^([a-fA-F0-9:.]+)(\/(\d+))?$/))
2531                         {
2532                                 if (!RegExp.$2 || ((RegExp.$3 >= 0) && (RegExp.$3 <= 128)))
2533                                 {
2534                                         var addr = RegExp.$1;
2535
2536                                         if (addr == '::')
2537                                         {
2538                                                 return true;
2539                                         }
2540
2541                                         if (addr.indexOf('.') > 0)
2542                                         {
2543                                                 var off = addr.lastIndexOf(':');
2544
2545                                                 if (!(off && validation.types['ip4addr'].apply(addr.substr(off+1))))
2546                                                 {
2547                                                         validation.i18n('Must be a valid IPv6 address');
2548                                                         return false;
2549                                                 }
2550
2551                                                 addr = addr.substr(0, off) + ':0:0';
2552                                         }
2553
2554                                         if (addr.indexOf('::') >= 0)
2555                                         {
2556                                                 var colons = 0;
2557                                                 var fill = '0';
2558
2559                                                 for (var i = 1; i < (addr.length-1); i++)
2560                                                         if (addr.charAt(i) == ':')
2561                                                                 colons++;
2562
2563                                                 if (colons > 7)
2564                                                 {
2565                                                         validation.i18n('Must be a valid IPv6 address');
2566                                                         return false;
2567                                                 }
2568
2569                                                 for (var i = 0; i < (7 - colons); i++)
2570                                                         fill += ':0';
2571
2572                                                 if (addr.match(/^(.*?)::(.*?)$/))
2573                                                         addr = (RegExp.$1 ? RegExp.$1 + ':' : '') + fill +
2574                                                                    (RegExp.$2 ? ':' + RegExp.$2 : '');
2575                                         }
2576
2577                                         if (addr.match(/^(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}$/) != null)
2578                                                 return true;
2579
2580                                         validation.i18n('Must be a valid IPv6 address');
2581                                         return false;
2582                                 }
2583                         }
2584
2585                         return false;
2586                 },
2587
2588                 'port': function()
2589                 {
2590                         if (validation.types['integer'].apply(this) &&
2591                                 (this >= 0) && (this <= 65535))
2592                                 return true;
2593
2594                         validation.i18n('Must be a valid port number');
2595                         return false;
2596                 },
2597
2598                 'portrange': function()
2599                 {
2600                         if (this.match(/^(\d+)-(\d+)$/))
2601                         {
2602                                 var p1 = RegExp.$1;
2603                                 var p2 = RegExp.$2;
2604
2605                                 if (validation.types['port'].apply(p1) &&
2606                                     validation.types['port'].apply(p2) &&
2607                                     (parseInt(p1) <= parseInt(p2)))
2608                                         return true;
2609                         }
2610                         else if (validation.types['port'].apply(this))
2611                         {
2612                                 return true;
2613                         }
2614
2615                         validation.i18n('Must be a valid port range');
2616                         return false;
2617                 },
2618
2619                 'macaddr': function()
2620                 {
2621                         if (this.match(/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/) != null)
2622                                 return true;
2623
2624                         validation.i18n('Must be a valid MAC address');
2625                         return false;
2626                 },
2627
2628                 'host': function()
2629                 {
2630                         if (validation.types['hostname'].apply(this) ||
2631                             validation.types['ipaddr'].apply(this))
2632                                 return true;
2633
2634                         validation.i18n('Must be a valid hostname or IP address');
2635                         return false;
2636                 },
2637
2638                 'hostname': function()
2639                 {
2640                         if ((this.length <= 253) &&
2641                             ((this.match(/^[a-zA-Z0-9]+$/) != null ||
2642                              (this.match(/^[a-zA-Z0-9_][a-zA-Z0-9_\-.]*[a-zA-Z0-9]$/) &&
2643                               this.match(/[^0-9.]/)))))
2644                                 return true;
2645
2646                         validation.i18n('Must be a valid host name');
2647                         return false;
2648                 },
2649
2650                 'network': function()
2651                 {
2652                         if (validation.types['uciname'].apply(this) ||
2653                             validation.types['host'].apply(this))
2654                                 return true;
2655
2656                         validation.i18n('Must be a valid network name');
2657                         return false;
2658                 },
2659
2660                 'wpakey': function()
2661                 {
2662                         var v = this;
2663
2664                         if ((v.length == 64)
2665                               ? (v.match(/^[a-fA-F0-9]{64}$/) != null)
2666                                   : ((v.length >= 8) && (v.length <= 63)))
2667                                 return true;
2668
2669                         validation.i18n('Must be a valid WPA key');
2670                         return false;
2671                 },
2672
2673                 'wepkey': function()
2674                 {
2675                         var v = this;
2676
2677                         if (v.substr(0,2) == 's:')
2678                                 v = v.substr(2);
2679
2680                         if (((v.length == 10) || (v.length == 26))
2681                               ? (v.match(/^[a-fA-F0-9]{10,26}$/) != null)
2682                               : ((v.length == 5) || (v.length == 13)))
2683                                 return true;
2684
2685                         validation.i18n('Must be a valid WEP key');
2686                         return false;
2687                 },
2688
2689                 'uciname': function()
2690                 {
2691                         if (this.match(/^[a-zA-Z0-9_]+$/) != null)
2692                                 return true;
2693
2694                         validation.i18n('Must be a valid UCI identifier');
2695                         return false;
2696                 },
2697
2698                 'range': function(min, max)
2699                 {
2700                         var val = parseFloat(this);
2701
2702                         if (validation.types['integer'].apply(this) &&
2703                             !isNaN(min) && !isNaN(max) && ((val >= min) && (val <= max)))
2704                                 return true;
2705
2706                         validation.i18n('Must be a number between %d and %d');
2707                         return false;
2708                 },
2709
2710                 'min': function(min)
2711                 {
2712                         var val = parseFloat(this);
2713
2714                         if (validation.types['integer'].apply(this) &&
2715                             !isNaN(min) && !isNaN(val) && (val >= min))
2716                                 return true;
2717
2718                         validation.i18n('Must be a number greater or equal to %d');
2719                         return false;
2720                 },
2721
2722                 'max': function(max)
2723                 {
2724                         var val = parseFloat(this);
2725
2726                         if (validation.types['integer'].apply(this) &&
2727                             !isNaN(max) && !isNaN(val) && (val <= max))
2728                                 return true;
2729
2730                         validation.i18n('Must be a number lower or equal to %d');
2731                         return false;
2732                 },
2733
2734                 'rangelength': function(min, max)
2735                 {
2736                         var val = '' + this;
2737
2738                         if (!isNaN(min) && !isNaN(max) &&
2739                             (val.length >= min) && (val.length <= max))
2740                                 return true;
2741
2742                         validation.i18n('Must be between %d and %d characters');
2743                         return false;
2744                 },
2745
2746                 'minlength': function(min)
2747                 {
2748                         var val = '' + this;
2749
2750                         if (!isNaN(min) && (val.length >= min))
2751                                 return true;
2752
2753                         validation.i18n('Must be at least %d characters');
2754                         return false;
2755                 },
2756
2757                 'maxlength': function(max)
2758                 {
2759                         var val = '' + this;
2760
2761                         if (!isNaN(max) && (val.length <= max))
2762                                 return true;
2763
2764                         validation.i18n('Must be at most %d characters');
2765                         return false;
2766                 },
2767
2768                 'or': function()
2769                 {
2770                         var msgs = [ ];
2771
2772                         for (var i = 0; i < arguments.length; i += 2)
2773                         {
2774                                 delete validation.message;
2775
2776                                 if (typeof(arguments[i]) != 'function')
2777                                 {
2778                                         if (arguments[i] == this)
2779                                                 return true;
2780                                         i--;
2781                                 }
2782                                 else if (arguments[i].apply(this, arguments[i+1]))
2783                                 {
2784                                         return true;
2785                                 }
2786
2787                                 if (validation.message)
2788                                         msgs.push(validation.message.format.apply(validation.message, arguments[i+1]));
2789                         }
2790
2791                         validation.message = msgs.join( _luci2.tr(' - or - '));
2792                         return false;
2793                 },
2794
2795                 'and': function()
2796                 {
2797                         var msgs = [ ];
2798
2799                         for (var i = 0; i < arguments.length; i += 2)
2800                         {
2801                                 delete validation.message;
2802
2803                                 if (typeof arguments[i] != 'function')
2804                                 {
2805                                         if (arguments[i] != this)
2806                                                 return false;
2807                                         i--;
2808                                 }
2809                                 else if (!arguments[i].apply(this, arguments[i+1]))
2810                                 {
2811                                         return false;
2812                                 }
2813
2814                                 if (validation.message)
2815                                         msgs.push(validation.message.format.apply(validation.message, arguments[i+1]));
2816                         }
2817
2818                         validation.message = msgs.join(', ');
2819                         return true;
2820                 },
2821
2822                 'neg': function()
2823                 {
2824                         return validation.types['or'].apply(
2825                                 this.replace(/^[ \t]*![ \t]*/, ''), arguments);
2826                 },
2827
2828                 'list': function(subvalidator, subargs)
2829                 {
2830                         if (typeof subvalidator != 'function')
2831                                 return false;
2832
2833                         var tokens = this.match(/[^ \t]+/g);
2834                         for (var i = 0; i < tokens.length; i++)
2835                                 if (!subvalidator.apply(tokens[i], subargs))
2836                                         return false;
2837
2838                         return true;
2839                 },
2840
2841                 'phonedigit': function()
2842                 {
2843                         if (this.match(/^[0-9\*#!\.]+$/) != null)
2844                                 return true;
2845
2846                         validation.i18n('Must be a valid phone number digit');
2847                         return false;
2848                 },
2849
2850                 'string': function()
2851                 {
2852                         return true;
2853                 }
2854         };
2855
2856
2857         var AbstractValue = AbstractWidget.extend({
2858                 init: function(name, options)
2859                 {
2860                         this.name = name;
2861                         this.instance = { };
2862                         this.dependencies = [ ];
2863                         this.rdependency = { };
2864
2865                         this.options = _luci2.defaults(options, {
2866                                 placeholder: '',
2867                                 datatype: 'string',
2868                                 optional: false,
2869                                 keep: true
2870                         });
2871                 },
2872
2873                 id: function(sid)
2874                 {
2875                         return this.section.id('field', sid || '__unknown__', this.name);
2876                 },
2877
2878                 render: function(sid)
2879                 {
2880                         var i = this.instance[sid] = { };
2881
2882                         i.top = $('<div />').addClass('cbi-value');
2883
2884                         if (typeof(this.options.caption) == 'string')
2885                                 $('<label />')
2886                                         .addClass('cbi-value-title')
2887                                         .attr('for', this.id(sid))
2888                                         .text(this.options.caption)
2889                                         .appendTo(i.top);
2890
2891                         i.widget = $('<div />').addClass('cbi-value-field').append(this.widget(sid)).appendTo(i.top);
2892                         i.error = $('<div />').addClass('cbi-value-error').appendTo(i.top);
2893
2894                         if (typeof(this.options.description) == 'string')
2895                                 $('<div />')
2896                                         .addClass('cbi-value-description')
2897                                         .text(this.options.description)
2898                                         .appendTo(i.top);
2899
2900                         return i.top;
2901                 },
2902
2903                 ucipath: function(sid)
2904                 {
2905                         return {
2906                                 config:  (this.options.uci_package || this.map.uci_package),
2907                                 section: (this.options.uci_section || sid),
2908                                 option:  (this.options.uci_option  || this.name)
2909                         };
2910                 },
2911
2912                 ucivalue: function(sid)
2913                 {
2914                         var uci = this.ucipath(sid);
2915                         var val = this.map.get(uci.config, uci.section, uci.option);
2916
2917                         if (typeof(val) == 'undefined')
2918                                 return this.options.initial;
2919
2920                         return val;
2921                 },
2922
2923                 formvalue: function(sid)
2924                 {
2925                         var v = $('#' + this.id(sid)).val();
2926                         return (v === '') ? undefined : v;
2927                 },
2928
2929                 textvalue: function(sid)
2930                 {
2931                         var v = this.formvalue(sid);
2932
2933                         if (typeof(v) == 'undefined' || ($.isArray(v) && !v.length))
2934                                 v = this.ucivalue(sid);
2935
2936                         if (typeof(v) == 'undefined' || ($.isArray(v) && !v.length))
2937                                 v = this.options.placeholder;
2938
2939                         if (typeof(v) == 'undefined' || v === '')
2940                                 return undefined;
2941
2942                         if (typeof(v) == 'string' && $.isArray(this.choices))
2943                         {
2944                                 for (var i = 0; i < this.choices.length; i++)
2945                                         if (v === this.choices[i][0])
2946  &nbs