luci-base: luci.js: support registering request progress handlers
[oweals/luci.git] / modules / luci-base / htdocs / luci-static / resources / luci.js
1 (function(window, document, undefined) {
2         'use strict';
3
4         /* Object.assign polyfill for IE */
5         if (typeof Object.assign !== 'function') {
6                 Object.defineProperty(Object, 'assign', {
7                         value: function assign(target, varArgs) {
8                                 if (target == null)
9                                         throw new TypeError('Cannot convert undefined or null to object');
10
11                                 var to = Object(target);
12
13                                 for (var index = 1; index < arguments.length; index++)
14                                         if (arguments[index] != null)
15                                                 for (var nextKey in arguments[index])
16                                                         if (Object.prototype.hasOwnProperty.call(arguments[index], nextKey))
17                                                                 to[nextKey] = arguments[index][nextKey];
18
19                                 return to;
20                         },
21                         writable: true,
22                         configurable: true
23                 });
24         }
25
26         /* Promise.finally polyfill */
27         if (typeof Promise.prototype.finally !== 'function') {
28                 Promise.prototype.finally = function(fn) {
29                         var onFinally = function(cb) {
30                                 return Promise.resolve(fn.call(this)).then(cb);
31                         };
32
33                         return this.then(
34                                 function(result) { return onFinally.call(this, function() { return result }) },
35                                 function(reason) { return onFinally.call(this, function() { return Promise.reject(reason) }) }
36                         );
37                 };
38         }
39
40         /*
41          * Class declaration and inheritance helper
42          */
43
44         var toCamelCase = function(s) {
45                 return s.replace(/(?:^|[\. -])(.)/g, function(m0, m1) { return m1.toUpperCase() });
46         };
47
48         var superContext = null, Class = Object.assign(function() {}, {
49                 extend: function(properties) {
50                         var props = {
51                                 __base__: { value: this.prototype },
52                                 __name__: { value: properties.__name__ || 'anonymous' }
53                         };
54
55                         var ClassConstructor = function() {
56                                 if (!(this instanceof ClassConstructor))
57                                         throw new TypeError('Constructor must not be called without "new"');
58
59                                 if (Object.getPrototypeOf(this).hasOwnProperty('__init__')) {
60                                         if (typeof(this.__init__) != 'function')
61                                                 throw new TypeError('Class __init__ member is not a function');
62
63                                         this.__init__.apply(this, arguments)
64                                 }
65                                 else {
66                                         this.super('__init__', arguments);
67                                 }
68                         };
69
70                         for (var key in properties)
71                                 if (!props[key] && properties.hasOwnProperty(key))
72                                         props[key] = { value: properties[key], writable: true };
73
74                         ClassConstructor.prototype = Object.create(this.prototype, props);
75                         ClassConstructor.prototype.constructor = ClassConstructor;
76                         Object.assign(ClassConstructor, this);
77                         ClassConstructor.displayName = toCamelCase(props.__name__.value + 'Class');
78
79                         return ClassConstructor;
80                 },
81
82                 singleton: function(properties /*, ... */) {
83                         return Class.extend(properties)
84                                 .instantiate(Class.prototype.varargs(arguments, 1));
85                 },
86
87                 instantiate: function(args) {
88                         return new (Function.prototype.bind.apply(this,
89                                 Class.prototype.varargs(args, 0, null)))();
90                 },
91
92                 call: function(self, method) {
93                         if (typeof(this.prototype[method]) != 'function')
94                                 throw new ReferenceError(method + ' is not defined in class');
95
96                         return this.prototype[method].apply(self, self.varargs(arguments, 1));
97                 },
98
99                 isSubclass: function(_class) {
100                         return (_class != null &&
101                                 typeof(_class) == 'function' &&
102                                 _class.prototype instanceof this);
103                 },
104
105                 prototype: {
106                         varargs: function(args, offset /*, ... */) {
107                                 return Array.prototype.slice.call(arguments, 2)
108                                         .concat(Array.prototype.slice.call(args, offset));
109                         },
110
111                         super: function(key, callArgs) {
112                                 for (superContext = Object.getPrototypeOf(superContext ||
113                                                                           Object.getPrototypeOf(this));
114                                      superContext && !superContext.hasOwnProperty(key);
115                                      superContext = Object.getPrototypeOf(superContext)) { }
116
117                                 if (!superContext)
118                                         return null;
119
120                                 var res = superContext[key];
121
122                                 if (arguments.length > 1) {
123                                         if (typeof(res) != 'function')
124                                                 throw new ReferenceError(key + ' is not a function in base class');
125
126                                         if (typeof(callArgs) != 'object')
127                                                 callArgs = this.varargs(arguments, 1);
128
129                                         res = res.apply(this, callArgs);
130                                 }
131
132                                 superContext = null;
133
134                                 return res;
135                         },
136
137                         toString: function() {
138                                 var s = '[' + this.constructor.displayName + ']', f = true;
139                                 for (var k in this) {
140                                         if (this.hasOwnProperty(k)) {
141                                                 s += (f ? ' {\n' : '') + '  ' + k + ': ' + typeof(this[k]) + '\n';
142                                                 f = false;
143                                         }
144                                 }
145                                 return s + (f ? '' : '}');
146                         }
147                 }
148         });
149
150
151         /*
152          * HTTP Request helper
153          */
154
155         var Headers = Class.extend({
156                 __name__: 'LuCI.XHR.Headers',
157                 __init__: function(xhr) {
158                         var hdrs = this.headers = {};
159                         xhr.getAllResponseHeaders().split(/\r\n/).forEach(function(line) {
160                                 var m = /^([^:]+):(.*)$/.exec(line);
161                                 if (m != null)
162                                         hdrs[m[1].trim().toLowerCase()] = m[2].trim();
163                         });
164                 },
165
166                 has: function(name) {
167                         return this.headers.hasOwnProperty(String(name).toLowerCase());
168                 },
169
170                 get: function(name) {
171                         var key = String(name).toLowerCase();
172                         return this.headers.hasOwnProperty(key) ? this.headers[key] : null;
173                 }
174         });
175
176         var Response = Class.extend({
177                 __name__: 'LuCI.XHR.Response',
178                 __init__: function(xhr, url, duration, headers, content) {
179                         this.ok = (xhr.status >= 200 && xhr.status <= 299);
180                         this.status = xhr.status;
181                         this.statusText = xhr.statusText;
182                         this.headers = (headers != null) ? headers : new Headers(xhr);
183                         this.duration = duration;
184                         this.url = url;
185                         this.xhr = xhr;
186
187                         if (content != null && typeof(content) == 'object') {
188                                 this.responseJSON = content;
189                                 this.responseText = null;
190                         }
191                         else if (content != null) {
192                                 this.responseJSON = null;
193                                 this.responseText = String(content);
194                         }
195                         else {
196                                 this.responseJSON = null;
197                                 this.responseText = xhr.responseText;
198                         }
199                 },
200
201                 clone: function(content) {
202                         var copy = new Response(this.xhr, this.url, this.duration, this.headers, content);
203
204                         copy.ok = this.ok;
205                         copy.status = this.status;
206                         copy.statusText = this.statusText;
207
208                         return copy;
209                 },
210
211                 json: function() {
212                         if (this.responseJSON == null)
213                                 this.responseJSON = JSON.parse(this.responseText);
214
215                         return this.responseJSON;
216                 },
217
218                 text: function() {
219                         if (this.responseText == null && this.responseJSON != null)
220                                 this.responseText = JSON.stringify(this.responseJSON);
221
222                         return this.responseText;
223                 }
224         });
225
226
227         var requestQueue = [];
228
229         function isQueueableRequest(opt) {
230                 if (!classes.rpc)
231                         return false;
232
233                 if (opt.method != 'POST' || typeof(opt.content) != 'object')
234                         return false;
235
236                 if (opt.nobatch === true)
237                         return false;
238
239                 var rpcBaseURL = Request.expandURL(classes.rpc.getBaseURL());
240
241                 return (rpcBaseURL != null && opt.url.indexOf(rpcBaseURL) == 0);
242         }
243
244         function flushRequestQueue() {
245                 if (!requestQueue.length)
246                         return;
247
248                 var reqopt = Object.assign({}, requestQueue[0][0], { content: [], nobatch: true }),
249                     batch = [];
250
251                 for (var i = 0; i < requestQueue.length; i++) {
252                         batch[i] = requestQueue[i];
253                         reqopt.content[i] = batch[i][0].content;
254                 }
255
256                 requestQueue.length = 0;
257
258                 Request.request(rpcBaseURL, reqopt).then(function(reply) {
259                         var json = null, req = null;
260
261                         try { json = reply.json() }
262                         catch(e) { }
263
264                         while ((req = batch.shift()) != null)
265                                 if (Array.isArray(json) && json.length)
266                                         req[2].call(reqopt, reply.clone(json.shift()));
267                                 else
268                                         req[1].call(reqopt, new Error('No related RPC reply'));
269                 }).catch(function(error) {
270                         var req = null;
271
272                         while ((req = batch.shift()) != null)
273                                 req[1].call(reqopt, error);
274                 });
275         }
276
277         var Request = Class.singleton({
278                 __name__: 'LuCI.Request',
279
280                 interceptors: [],
281
282                 expandURL: function(url) {
283                         if (!/^(?:[^/]+:)?\/\//.test(url))
284                                 url = location.protocol + '//' + location.host + url;
285
286                         return url;
287                 },
288
289                 request: function(target, options) {
290                         var state = { xhr: new XMLHttpRequest(), url: this.expandURL(target), start: Date.now() },
291                             opt = Object.assign({}, options, state),
292                             content = null,
293                             contenttype = null,
294                             callback = this.handleReadyStateChange;
295
296                         return new Promise(function(resolveFn, rejectFn) {
297                                 opt.xhr.onreadystatechange = callback.bind(opt, resolveFn, rejectFn);
298                                 opt.method = String(opt.method || 'GET').toUpperCase();
299
300                                 if ('query' in opt) {
301                                         var q = (opt.query != null) ? Object.keys(opt.query).map(function(k) {
302                                                 if (opt.query[k] != null) {
303                                                         var v = (typeof(opt.query[k]) == 'object')
304                                                                 ? JSON.stringify(opt.query[k])
305                                                                 : String(opt.query[k]);
306
307                                                         return '%s=%s'.format(encodeURIComponent(k), encodeURIComponent(v));
308                                                 }
309                                                 else {
310                                                         return encodeURIComponent(k);
311                                                 }
312                                         }).join('&') : '';
313
314                                         if (q !== '') {
315                                                 switch (opt.method) {
316                                                 case 'GET':
317                                                 case 'HEAD':
318                                                 case 'OPTIONS':
319                                                         opt.url += ((/\?/).test(opt.url) ? '&' : '?') + q;
320                                                         break;
321
322                                                 default:
323                                                         if (content == null) {
324                                                                 content = q;
325                                                                 contenttype = 'application/x-www-form-urlencoded';
326                                                         }
327                                                 }
328                                         }
329                                 }
330
331                                 if (!opt.cache)
332                                         opt.url += ((/\?/).test(opt.url) ? '&' : '?') + (new Date()).getTime();
333
334                                 if (isQueueableRequest(opt)) {
335                                         requestQueue.push([opt, rejectFn, resolveFn]);
336                                         requestAnimationFrame(flushRequestQueue);
337                                         return;
338                                 }
339
340                                 if ('username' in opt && 'password' in opt)
341                                         opt.xhr.open(opt.method, opt.url, true, opt.username, opt.password);
342                                 else
343                                         opt.xhr.open(opt.method, opt.url, true);
344
345                                 opt.xhr.responseType = 'text';
346
347                                 if ('overrideMimeType' in opt.xhr)
348                                         opt.xhr.overrideMimeType('application/octet-stream');
349
350                                 if ('timeout' in opt)
351                                         opt.xhr.timeout = +opt.timeout;
352
353                                 if ('credentials' in opt)
354                                         opt.xhr.withCredentials = !!opt.credentials;
355
356                                 if (opt.content != null) {
357                                         switch (typeof(opt.content)) {
358                                         case 'function':
359                                                 content = opt.content(xhr);
360                                                 break;
361
362                                         case 'object':
363                                                 content = JSON.stringify(opt.content);
364                                                 contenttype = 'application/json';
365                                                 break;
366
367                                         default:
368                                                 content = String(opt.content);
369                                         }
370                                 }
371
372                                 if ('headers' in opt)
373                                         for (var header in opt.headers)
374                                                 if (opt.headers.hasOwnProperty(header)) {
375                                                         if (header.toLowerCase() != 'content-type')
376                                                                 opt.xhr.setRequestHeader(header, opt.headers[header]);
377                                                         else
378                                                                 contenttype = opt.headers[header];
379                                                 }
380
381                                 if ('progress' in opt && 'upload' in opt.xhr)
382                                         opt.xhr.upload.addEventListener('progress', opt.progress);
383
384                                 if (contenttype != null)
385                                         opt.xhr.setRequestHeader('Content-Type', contenttype);
386
387                                 try {
388                                         opt.xhr.send(content);
389                                 }
390                                 catch (e) {
391                                         rejectFn.call(opt, e);
392                                 }
393                         });
394                 },
395
396                 handleReadyStateChange: function(resolveFn, rejectFn, ev) {
397                         var xhr = this.xhr;
398
399                         if (xhr.readyState !== 4)
400                                 return;
401
402                         if (xhr.status === 0 && xhr.statusText === '') {
403                                 rejectFn.call(this, new Error('XHR request aborted by browser'));
404                         }
405                         else {
406                                 var response = new Response(
407                                         xhr, xhr.responseURL || this.url, Date.now() - this.start);
408
409                                 Promise.all(Request.interceptors.map(function(fn) { return fn(response) }))
410                                         .then(resolveFn.bind(this, response))
411                                         .catch(rejectFn.bind(this));
412                         }
413                 },
414
415                 get: function(url, options) {
416                         return this.request(url, Object.assign({ method: 'GET' }, options));
417                 },
418
419                 post: function(url, data, options) {
420                         return this.request(url, Object.assign({ method: 'POST', content: data }, options));
421                 },
422
423                 addInterceptor: function(interceptorFn) {
424                         if (typeof(interceptorFn) == 'function')
425                                 this.interceptors.push(interceptorFn);
426                         return interceptorFn;
427                 },
428
429                 removeInterceptor: function(interceptorFn) {
430                         var oldlen = this.interceptors.length, i = oldlen;
431                         while (i--)
432                                 if (this.interceptors[i] === interceptorFn)
433                                         this.interceptors.splice(i, 1);
434                         return (this.interceptors.length < oldlen);
435                 },
436
437                 poll: {
438                         add: function(interval, url, options, callback) {
439                                 if (isNaN(interval) || interval <= 0)
440                                         throw new TypeError('Invalid poll interval');
441
442                                 var ival = interval >>> 0,
443                                     opts = Object.assign({}, options, { timeout: ival * 1000 - 5 });
444
445                                 return Poll.add(function() {
446                                         return Request.request(url, options).then(function(res) {
447                                                 if (!Poll.active())
448                                                         return;
449
450                                                 try {
451                                                         callback(res, res.json(), res.duration);
452                                                 }
453                                                 catch (err) {
454                                                         callback(res, null, res.duration);
455                                                 }
456                                         });
457                                 }, ival);
458                         },
459
460                         remove: function(entry) { return Poll.remove(entry) },
461                         start: function() { return Poll.start() },
462                         stop: function() { return Poll.stop() },
463                         active: function() { return Poll.active() }
464                 }
465         });
466
467         var Poll = Class.singleton({
468                 __name__: 'LuCI.Poll',
469
470                 queue: [],
471
472                 add: function(fn, interval) {
473                         if (interval == null || interval <= 0)
474                                 interval = window.L ? window.L.env.pollinterval : null;
475
476                         if (isNaN(interval) || typeof(fn) != 'function')
477                                 throw new TypeError('Invalid argument to LuCI.Poll.add()');
478
479                         for (var i = 0; i < this.queue.length; i++)
480                                 if (this.queue[i].fn === fn)
481                                         return false;
482
483                         var e = {
484                                 r: true,
485                                 i: interval >>> 0,
486                                 fn: fn
487                         };
488
489                         this.queue.push(e);
490
491                         if (this.tick != null && !this.active())
492                                 this.start();
493
494                         return true;
495                 },
496
497                 remove: function(fn) {
498                         if (typeof(fn) != 'function')
499                                 throw new TypeError('Invalid argument to LuCI.Poll.remove()');
500
501                         var len = this.queue.length;
502
503                         for (var i = len; i > 0; i--)
504                                 if (this.queue[i-1].fn === fn)
505                                         this.queue.splice(i-1, 1);
506
507                         if (!this.queue.length && this.stop())
508                                 this.tick = 0;
509
510                         return (this.queue.length != len);
511                 },
512
513                 start: function() {
514                         if (this.active())
515                                 return false;
516
517                         this.tick = 0;
518
519                         if (this.queue.length) {
520                                 this.timer = window.setInterval(this.step, 1000);
521                                 this.step();
522                                 document.dispatchEvent(new CustomEvent('poll-start'));
523                         }
524
525                         return true;
526                 },
527
528                 stop: function() {
529                         if (!this.active())
530                                 return false;
531
532                         document.dispatchEvent(new CustomEvent('poll-stop'));
533                         window.clearInterval(this.timer);
534                         delete this.timer;
535                         delete this.tick;
536                         return true;
537                 },
538
539                 step: function() {
540                         for (var i = 0, e = null; (e = Poll.queue[i]) != null; i++) {
541                                 if ((Poll.tick % e.i) != 0)
542                                         continue;
543
544                                 if (!e.r)
545                                         continue;
546
547                                 e.r = false;
548
549                                 Promise.resolve(e.fn()).finally((function() { this.r = true }).bind(e));
550                         }
551
552                         Poll.tick = (Poll.tick + 1) % Math.pow(2, 32);
553                 },
554
555                 active: function() {
556                         return (this.timer != null);
557                 }
558         });
559
560
561         var dummyElem = null,
562             domParser = null,
563             originalCBIInit = null,
564             rpcBaseURL = null,
565             sysFeatures = null,
566             classes = {};
567
568         var LuCI = Class.extend({
569                 __name__: 'LuCI',
570                 __init__: function(env) {
571
572                         document.querySelectorAll('script[src*="/luci.js"]').forEach(function(s) {
573                                 if (env.base_url == null || env.base_url == '')
574                                         env.base_url = s.getAttribute('src').replace(/\/luci\.js(?:\?v=[^?]+)?$/, '');
575                         });
576
577                         if (env.base_url == null)
578                                 this.error('InternalError', 'Cannot find url of luci.js');
579
580                         Object.assign(this.env, env);
581
582                         document.addEventListener('poll-start', function(ev) {
583                                 document.querySelectorAll('[id^="xhr_poll_status"]').forEach(function(e) {
584                                         e.style.display = (e.id == 'xhr_poll_status_off') ? 'none' : '';
585                                 });
586                         });
587
588                         document.addEventListener('poll-stop', function(ev) {
589                                 document.querySelectorAll('[id^="xhr_poll_status"]').forEach(function(e) {
590                                         e.style.display = (e.id == 'xhr_poll_status_on') ? 'none' : '';
591                                 });
592                         });
593
594                         var domReady = new Promise(function(resolveFn, rejectFn) {
595                                 document.addEventListener('DOMContentLoaded', resolveFn);
596                         });
597
598                         Promise.all([
599                                 domReady,
600                                 this.require('ui'),
601                                 this.require('rpc'),
602                                 this.require('form'),
603                                 this.probeRPCBaseURL()
604                         ]).then(this.setupDOM.bind(this)).catch(this.error);
605
606                         originalCBIInit = window.cbi_init;
607                         window.cbi_init = function() {};
608                 },
609
610                 raise: function(type, fmt /*, ...*/) {
611                         var e = null,
612                             msg = fmt ? String.prototype.format.apply(fmt, this.varargs(arguments, 2)) : null,
613                             stack = null;
614
615                         if (type instanceof Error) {
616                                 e = type;
617                                 stack = (e.stack || '').split(/\n/);
618
619                                 if (msg)
620                                         e.message = msg + ': ' + e.message;
621                         }
622                         else {
623                                 e = new (window[type || 'Error'] || Error)(msg || 'Unspecified error');
624                                 e.name = type || 'Error';
625                         }
626
627                         if (window.console && console.debug)
628                                 console.debug(e);
629
630                         throw e;
631                 },
632
633                 error: function(type, fmt /*, ...*/) {
634                         try {
635                                 L.raise.apply(L, Array.prototype.slice.call(arguments));
636                         }
637                         catch (e) {
638                                 var stack = (e.stack || '').split(/\n/).map(function(frame) {
639                                         frame = frame.replace(/(.*?)@(.+):(\d+):(\d+)/g, 'at $1 ($2:$3:$4)').trim();
640                                         return frame ? '  ' + frame : '';
641                                 });
642
643                                 if (!/^  at /.test(stack[0]))
644                                         stack.shift();
645
646                                 if (/\braise /.test(stack[0]))
647                                         stack.shift();
648
649                                 if (/\berror /.test(stack[0]))
650                                         stack.shift();
651
652                                 stack = stack.length ? '\n' + stack.join('\n') : '';
653
654                                 if (L.ui)
655                                         L.ui.showModal(e.name || _('Runtime error'),
656                                                 E('pre', { 'class': 'alert-message error' }, e.message + stack));
657                                 else
658                                         L.dom.content(document.querySelector('#maincontent'),
659                                                 E('pre', { 'class': 'alert-message error' }, e + stack));
660
661                                 throw e;
662                         }
663                 },
664
665                 bind: function(fn, self /*, ... */) {
666                         return Function.prototype.bind.apply(fn, this.varargs(arguments, 2, self));
667                 },
668
669                 /* Class require */
670                 require: function(name, from) {
671                         var L = this, url = null, from = from || [];
672
673                         /* Class already loaded */
674                         if (classes[name] != null) {
675                                 /* Circular dependency */
676                                 if (from.indexOf(name) != -1)
677                                         L.raise('DependencyError',
678                                                 'Circular dependency: class "%s" depends on "%s"',
679                                                 name, from.join('" which depends on "'));
680
681                                 return classes[name];
682                         }
683
684                         url = '%s/%s.js'.format(L.env.base_url, name.replace(/\./g, '/'));
685                         from = [ name ].concat(from);
686
687                         var compileClass = function(res) {
688                                 if (!res.ok)
689                                         L.raise('NetworkError',
690                                                 'HTTP error %d while loading class file "%s"', res.status, url);
691
692                                 var source = res.text(),
693                                     requirematch = /^require[ \t]+(\S+)(?:[ \t]+as[ \t]+([a-zA-Z_]\S*))?$/,
694                                     strictmatch = /^use[ \t]+strict$/,
695                                     depends = [],
696                                     args = '';
697
698                                 /* find require statements in source */
699                                 for (var i = 0, off = -1, quote = -1, esc = false; i < source.length; i++) {
700                                         var chr = source.charCodeAt(i);
701
702                                         if (esc) {
703                                                 esc = false;
704                                         }
705                                         else if (chr == 92) {
706                                                 esc = true;
707                                         }
708                                         else if (chr == quote) {
709                                                 var s = source.substring(off, i),
710                                                     m = requirematch.exec(s);
711
712                                                 if (m) {
713                                                         var dep = m[1], as = m[2] || dep.replace(/[^a-zA-Z0-9_]/g, '_');
714                                                         depends.push(L.require(dep, from));
715                                                         args += ', ' + as;
716                                                 }
717                                                 else if (!strictmatch.exec(s)) {
718                                                         break;
719                                                 }
720
721                                                 off = -1;
722                                                 quote = -1;
723                                         }
724                                         else if (quote == -1 && (chr == 34 || chr == 39)) {
725                                                 off = i + 1;
726                                                 quote = chr;
727                                         }
728                                 }
729
730                                 /* load dependencies and instantiate class */
731                                 return Promise.all(depends).then(function(instances) {
732                                         var _factory, _class;
733
734                                         try {
735                                                 _factory = eval(
736                                                         '(function(window, document, L%s) { %s })\n\n//# sourceURL=%s\n'
737                                                                 .format(args, source, res.url));
738                                         }
739                                         catch (error) {
740                                                 L.raise('SyntaxError', '%s\n  in %s:%s',
741                                                         error.message, res.url, error.lineNumber || '?');
742                                         }
743
744                                         _factory.displayName = toCamelCase(name + 'ClassFactory');
745                                         _class = _factory.apply(_factory, [window, document, L].concat(instances));
746
747                                         if (!Class.isSubclass(_class))
748                                             L.error('TypeError', '"%s" factory yields invalid constructor', name);
749
750                                         if (_class.displayName == 'AnonymousClass')
751                                                 _class.displayName = toCamelCase(name + 'Class');
752
753                                         var ptr = Object.getPrototypeOf(L),
754                                             parts = name.split(/\./),
755                                             instance = new _class();
756
757                                         for (var i = 0; ptr && i < parts.length - 1; i++)
758                                                 ptr = ptr[parts[i]];
759
760                                         if (ptr)
761                                                 ptr[parts[i]] = instance;
762
763                                         classes[name] = instance;
764
765                                         return instance;
766                                 });
767                         };
768
769                         /* Request class file */
770                         classes[name] = Request.get(url, { cache: true }).then(compileClass);
771
772                         return classes[name];
773                 },
774
775                 /* DOM setup */
776                 probeRPCBaseURL: function() {
777                         if (rpcBaseURL == null) {
778                                 try {
779                                         rpcBaseURL = window.sessionStorage.getItem('rpcBaseURL');
780                                 }
781                                 catch (e) { }
782                         }
783
784                         if (rpcBaseURL == null) {
785                                 var rpcFallbackURL = this.url('admin/ubus');
786
787                                 rpcBaseURL = Request.get('/ubus/').then(function(res) {
788                                         return (rpcBaseURL = (res.status == 400) ? '/ubus/' : rpcFallbackURL);
789                                 }, function() {
790                                         return (rpcBaseURL = rpcFallbackURL);
791                                 }).then(function(url) {
792                                         try {
793                                                 window.sessionStorage.setItem('rpcBaseURL', url);
794                                         }
795                                         catch (e) { }
796
797                                         return url;
798                                 });
799                         }
800
801                         return Promise.resolve(rpcBaseURL);
802                 },
803
804                 probeSystemFeatures: function() {
805                         if (sysFeatures == null) {
806                                 try {
807                                         sysFeatures = JSON.parse(window.sessionStorage.getItem('sysFeatures'));
808                                 }
809                                 catch (e) {}
810                         }
811
812                         if (!this.isObject(sysFeatures)) {
813                                 sysFeatures = classes.rpc.declare({
814                                         object: 'luci',
815                                         method: 'getFeatures',
816                                         expect: { '': {} }
817                                 })().then(function(features) {
818                                         try {
819                                                 window.sessionStorage.setItem('sysFeatures', JSON.stringify(features));
820                                         }
821                                         catch (e) {}
822
823                                         sysFeatures = features;
824
825                                         return features;
826                                 });
827                         }
828
829                         return Promise.resolve(sysFeatures);
830                 },
831
832                 hasSystemFeature: function() {
833                         var ft = sysFeatures[arguments[0]];
834
835                         if (arguments.length == 2)
836                                 return this.isObject(ft) ? ft[arguments[1]] : null;
837
838                         return (ft != null && ft != false);
839                 },
840
841                 setupDOM: function(res) {
842                         var domEv = res[0],
843                             uiClass = res[1],
844                             rpcClass = res[2],
845                             formClass = res[3],
846                             rpcBaseURL = res[4];
847
848                         rpcClass.setBaseURL(rpcBaseURL);
849
850                         Request.addInterceptor(function(res) {
851                                 if (res.status != 403 || res.headers.get('X-LuCI-Login-Required') != 'yes')
852                                         return;
853
854                                 Poll.stop();
855
856                                 L.ui.showModal(_('Session expired'), [
857                                         E('div', { class: 'alert-message warning' },
858                                                 _('A new login is required since the authentication session expired.')),
859                                         E('div', { class: 'right' },
860                                                 E('div', {
861                                                         class: 'btn primary',
862                                                         click: function() {
863                                                                 var loc = window.location;
864                                                                 window.location = loc.protocol + '//' + loc.host + loc.pathname + loc.search;
865                                                         }
866                                                 }, _('To login…')))
867                                 ]);
868
869                                 throw 'Session expired';
870                         });
871
872                         return this.probeSystemFeatures().finally(this.initDOM);
873                 },
874
875                 initDOM: function() {
876                         originalCBIInit();
877                         Poll.start();
878                         document.dispatchEvent(new CustomEvent('luci-loaded'));
879                 },
880
881                 env: {},
882
883                 /* URL construction helpers */
884                 path: function(prefix, parts) {
885                         var url = [ prefix || '' ];
886
887                         for (var i = 0; i < parts.length; i++)
888                                 if (/^(?:[a-zA-Z0-9_.%,;-]+\/)*[a-zA-Z0-9_.%,;-]+$/.test(parts[i]))
889                                         url.push('/', parts[i]);
890
891                         if (url.length === 1)
892                                 url.push('/');
893
894                         return url.join('');
895                 },
896
897                 url: function() {
898                         return this.path(this.env.scriptname, arguments);
899                 },
900
901                 resource: function() {
902                         return this.path(this.env.resource, arguments);
903                 },
904
905                 location: function() {
906                         return this.path(this.env.scriptname, this.env.requestpath);
907                 },
908
909
910                 /* Data helpers */
911                 isObject: function(val) {
912                         return (val != null && typeof(val) == 'object');
913                 },
914
915                 sortedKeys: function(obj, key, sortmode) {
916                         if (obj == null || typeof(obj) != 'object')
917                                 return [];
918
919                         return Object.keys(obj).map(function(e) {
920                                 var v = (key != null) ? obj[e][key] : e;
921
922                                 switch (sortmode) {
923                                 case 'addr':
924                                         v = (v != null) ? v.replace(/(?:^|[.:])([0-9a-fA-F]{1,4})/g,
925                                                 function(m0, m1) { return ('000' + m1.toLowerCase()).substr(-4) }) : null;
926                                         break;
927
928                                 case 'num':
929                                         v = (v != null) ? +v : null;
930                                         break;
931                                 }
932
933                                 return [ e, v ];
934                         }).filter(function(e) {
935                                 return (e[1] != null);
936                         }).sort(function(a, b) {
937                                 return (a[1] > b[1]);
938                         }).map(function(e) {
939                                 return e[0];
940                         });
941                 },
942
943                 toArray: function(val) {
944                         if (val == null)
945                                 return [];
946                         else if (Array.isArray(val))
947                                 return val;
948                         else if (typeof(val) == 'object')
949                                 return [ val ];
950
951                         var s = String(val).trim();
952
953                         if (s == '')
954                                 return [];
955
956                         return s.split(/\s+/);
957                 },
958
959
960                 /* HTTP resource fetching */
961                 get: function(url, args, cb) {
962                         return this.poll(null, url, args, cb, false);
963                 },
964
965                 post: function(url, args, cb) {
966                         return this.poll(null, url, args, cb, true);
967                 },
968
969                 poll: function(interval, url, args, cb, post) {
970                         if (interval !== null && interval <= 0)
971                                 interval = this.env.pollinterval;
972
973                         var data = post ? { token: this.env.token } : null,
974                             method = post ? 'POST' : 'GET';
975
976                         if (!/^(?:\/|\S+:\/\/)/.test(url))
977                                 url = this.url(url);
978
979                         if (args != null)
980                                 data = Object.assign(data || {}, args);
981
982                         if (interval !== null)
983                                 return Request.poll.add(interval, url, { method: method, query: data }, cb);
984                         else
985                                 return Request.request(url, { method: method, query: data })
986                                         .then(function(res) {
987                                                 var json = null;
988                                                 if (/^application\/json\b/.test(res.headers.get('Content-Type')))
989                                                         try { json = res.json() } catch(e) {}
990                                                 cb(res.xhr, json, res.duration);
991                                         });
992                 },
993
994                 stop: function(entry) { return Poll.remove(entry) },
995                 halt: function() { return Poll.stop() },
996                 run: function() { return Poll.start() },
997
998                 /* DOM manipulation */
999                 dom: Class.singleton({
1000                         __name__: 'LuCI.DOM',
1001
1002                         elem: function(e) {
1003                                 return (e != null && typeof(e) == 'object' && 'nodeType' in e);
1004                         },
1005
1006                         parse: function(s) {
1007                                 var elem;
1008
1009                                 try {
1010                                         domParser = domParser || new DOMParser();
1011                                         elem = domParser.parseFromString(s, 'text/html').body.firstChild;
1012                                 }
1013                                 catch(e) {}
1014
1015                                 if (!elem) {
1016                                         try {
1017                                                 dummyElem = dummyElem || document.createElement('div');
1018                                                 dummyElem.innerHTML = s;
1019                                                 elem = dummyElem.firstChild;
1020                                         }
1021                                         catch (e) {}
1022                                 }
1023
1024                                 return elem || null;
1025                         },
1026
1027                         matches: function(node, selector) {
1028                                 var m = this.elem(node) ? node.matches || node.msMatchesSelector : null;
1029                                 return m ? m.call(node, selector) : false;
1030                         },
1031
1032                         parent: function(node, selector) {
1033                                 if (this.elem(node) && node.closest)
1034                                         return node.closest(selector);
1035
1036                                 while (this.elem(node))
1037                                         if (this.matches(node, selector))
1038                                                 return node;
1039                                         else
1040                                                 node = node.parentNode;
1041
1042                                 return null;
1043                         },
1044
1045                         append: function(node, children) {
1046                                 if (!this.elem(node))
1047                                         return null;
1048
1049                                 if (Array.isArray(children)) {
1050                                         for (var i = 0; i < children.length; i++)
1051                                                 if (this.elem(children[i]))
1052                                                         node.appendChild(children[i]);
1053                                                 else if (children !== null && children !== undefined)
1054                                                         node.appendChild(document.createTextNode('' + children[i]));
1055
1056                                         return node.lastChild;
1057                                 }
1058                                 else if (typeof(children) === 'function') {
1059                                         return this.append(node, children(node));
1060                                 }
1061                                 else if (this.elem(children)) {
1062                                         return node.appendChild(children);
1063                                 }
1064                                 else if (children !== null && children !== undefined) {
1065                                         node.innerHTML = '' + children;
1066                                         return node.lastChild;
1067                                 }
1068
1069                                 return null;
1070                         },
1071
1072                         content: function(node, children) {
1073                                 if (!this.elem(node))
1074                                         return null;
1075
1076                                 var dataNodes = node.querySelectorAll('[data-idref]');
1077
1078                                 for (var i = 0; i < dataNodes.length; i++)
1079                                         delete this.registry[dataNodes[i].getAttribute('data-idref')];
1080
1081                                 while (node.firstChild)
1082                                         node.removeChild(node.firstChild);
1083
1084                                 return this.append(node, children);
1085                         },
1086
1087                         attr: function(node, key, val) {
1088                                 if (!this.elem(node))
1089                                         return null;
1090
1091                                 var attr = null;
1092
1093                                 if (typeof(key) === 'object' && key !== null)
1094                                         attr = key;
1095                                 else if (typeof(key) === 'string')
1096                                         attr = {}, attr[key] = val;
1097
1098                                 for (key in attr) {
1099                                         if (!attr.hasOwnProperty(key) || attr[key] == null)
1100                                                 continue;
1101
1102                                         switch (typeof(attr[key])) {
1103                                         case 'function':
1104                                                 node.addEventListener(key, attr[key]);
1105                                                 break;
1106
1107                                         case 'object':
1108                                                 node.setAttribute(key, JSON.stringify(attr[key]));
1109                                                 break;
1110
1111                                         default:
1112                                                 node.setAttribute(key, attr[key]);
1113                                         }
1114                                 }
1115                         },
1116
1117                         create: function() {
1118                                 var html = arguments[0],
1119                                     attr = arguments[1],
1120                                     data = arguments[2],
1121                                     elem;
1122
1123                                 if (!(attr instanceof Object) || Array.isArray(attr))
1124                                         data = attr, attr = null;
1125
1126                                 if (Array.isArray(html)) {
1127                                         elem = document.createDocumentFragment();
1128                                         for (var i = 0; i < html.length; i++)
1129                                                 elem.appendChild(this.create(html[i]));
1130                                 }
1131                                 else if (this.elem(html)) {
1132                                         elem = html;
1133                                 }
1134                                 else if (html.charCodeAt(0) === 60) {
1135                                         elem = this.parse(html);
1136                                 }
1137                                 else {
1138                                         elem = document.createElement(html);
1139                                 }
1140
1141                                 if (!elem)
1142                                         return null;
1143
1144                                 this.attr(elem, attr);
1145                                 this.append(elem, data);
1146
1147                                 return elem;
1148                         },
1149
1150                         registry: {},
1151
1152                         data: function(node, key, val) {
1153                                 var id = node.getAttribute('data-idref');
1154
1155                                 /* clear all data */
1156                                 if (arguments.length > 1 && key == null) {
1157                                         if (id != null) {
1158                                                 node.removeAttribute('data-idref');
1159                                                 val = this.registry[id]
1160                                                 delete this.registry[id];
1161                                                 return val;
1162                                         }
1163
1164                                         return null;
1165                                 }
1166
1167                                 /* clear a key */
1168                                 else if (arguments.length > 2 && key != null && val == null) {
1169                                         if (id != null) {
1170                                                 val = this.registry[id][key];
1171                                                 delete this.registry[id][key];
1172                                                 return val;
1173                                         }
1174
1175                                         return null;
1176                                 }
1177
1178                                 /* set a key */
1179                                 else if (arguments.length > 2 && key != null && val != null) {
1180                                         if (id == null) {
1181                                                 do { id = Math.floor(Math.random() * 0xffffffff).toString(16) }
1182                                                 while (this.registry.hasOwnProperty(id));
1183
1184                                                 node.setAttribute('data-idref', id);
1185                                                 this.registry[id] = {};
1186                                         }
1187
1188                                         return (this.registry[id][key] = val);
1189                                 }
1190
1191                                 /* get all data */
1192                                 else if (arguments.length == 1) {
1193                                         if (id != null)
1194                                                 return this.registry[id];
1195
1196                                         return null;
1197                                 }
1198
1199                                 /* get a key */
1200                                 else if (arguments.length == 2) {
1201                                         if (id != null)
1202                                                 return this.registry[id][key];
1203                                 }
1204
1205                                 return null;
1206                         },
1207
1208                         bindClassInstance: function(node, inst) {
1209                                 if (!(inst instanceof Class))
1210                                         L.error('TypeError', 'Argument must be a class instance');
1211
1212                                 return this.data(node, '_class', inst);
1213                         },
1214
1215                         findClassInstance: function(node) {
1216                                 var inst = null;
1217
1218                                 do {
1219                                         inst = this.data(node, '_class');
1220                                         node = node.parentNode;
1221                                 }
1222                                 while (!(inst instanceof Class) && node != null);
1223
1224                                 return inst;
1225                         },
1226
1227                         callClassMethod: function(node, method /*, ... */) {
1228                                 var inst = this.findClassInstance(node);
1229
1230                                 if (inst == null || typeof(inst[method]) != 'function')
1231                                         return null;
1232
1233                                 return inst[method].apply(inst, inst.varargs(arguments, 2));
1234                         },
1235
1236                         isEmpty: function(node, ignoreFn) {
1237                                 for (var child = node.firstElementChild; child != null; child = child.nextElementSibling)
1238                                         if (!child.classList.contains('hidden') && (!ignoreFn || !ignoreFn(child)))
1239                                                 return false;
1240
1241                                 return true;
1242                         }
1243                 }),
1244
1245                 Poll: Poll,
1246                 Class: Class,
1247                 Request: Request,
1248
1249                 view: Class.extend({
1250                         __name__: 'LuCI.View',
1251
1252                         __init__: function() {
1253                                 var vp = document.getElementById('view');
1254
1255                                 L.dom.content(vp, E('div', { 'class': 'spinning' }, _('Loading view…')));
1256
1257                                 return Promise.resolve(this.load())
1258                                         .then(L.bind(this.render, this))
1259                                         .then(L.bind(function(nodes) {
1260                                                 var vp = document.getElementById('view');
1261
1262                                                 L.dom.content(vp, nodes);
1263                                                 L.dom.append(vp, this.addFooter());
1264                                         }, this)).catch(L.error);
1265                         },
1266
1267                         load: function() {},
1268                         render: function() {},
1269
1270                         handleSave: function(ev) {
1271                                 var tasks = [];
1272
1273                                 document.getElementById('maincontent')
1274                                         .querySelectorAll('.cbi-map').forEach(function(map) {
1275                                                 tasks.push(L.dom.callClassMethod(map, 'save'));
1276                                         });
1277
1278                                 return Promise.all(tasks);
1279                         },
1280
1281                         handleSaveApply: function(ev) {
1282                                 return this.handleSave(ev).then(function() {
1283                                         L.ui.changes.apply(true);
1284                                 });
1285                         },
1286
1287                         handleReset: function(ev) {
1288                                 var tasks = [];
1289
1290                                 document.getElementById('maincontent')
1291                                         .querySelectorAll('.cbi-map').forEach(function(map) {
1292                                                 tasks.push(L.dom.callClassMethod(map, 'reset'));
1293                                         });
1294
1295                                 return Promise.all(tasks);
1296                         },
1297
1298                         addFooter: function() {
1299                                 var footer = E([]),
1300                                     mc = document.getElementById('maincontent');
1301
1302                                 if (mc.querySelector('.cbi-map')) {
1303                                         footer.appendChild(E('div', { 'class': 'cbi-page-actions' }, [
1304                                                 E('input', {
1305                                                         'class': 'cbi-button cbi-button-apply',
1306                                                         'type': 'button',
1307                                                         'value': _('Save & Apply'),
1308                                                         'click': L.bind(this.handleSaveApply, this)
1309                                                 }), ' ',
1310                                                 E('input', {
1311                                                         'class': 'cbi-button cbi-button-save',
1312                                                         'type': 'submit',
1313                                                         'value': _('Save'),
1314                                                         'click': L.bind(this.handleSave, this)
1315                                                 }), ' ',
1316                                                 E('input', {
1317                                                         'class': 'cbi-button cbi-button-reset',
1318                                                         'type': 'button',
1319                                                         'value': _('Reset'),
1320                                                         'click': L.bind(this.handleReset, this)
1321                                                 })
1322                                         ]));
1323                                 }
1324
1325                                 return footer;
1326                         }
1327                 })
1328         });
1329
1330         var XHR = Class.extend({
1331                 __name__: 'LuCI.XHR',
1332                 __init__: function() {
1333                         if (window.console && console.debug)
1334                                 console.debug('Direct use XHR() is deprecated, please use L.Request instead');
1335                 },
1336
1337                 _response: function(cb, res, json, duration) {
1338                         if (this.active)
1339                                 cb(res, json, duration);
1340                         delete this.active;
1341                 },
1342
1343                 get: function(url, data, callback, timeout) {
1344                         this.active = true;
1345                         L.get(url, data, this._response.bind(this, callback), timeout);
1346                 },
1347
1348                 post: function(url, data, callback, timeout) {
1349                         this.active = true;
1350                         L.post(url, data, this._response.bind(this, callback), timeout);
1351                 },
1352
1353                 cancel: function() { delete this.active },
1354                 busy: function() { return (this.active === true) },
1355                 abort: function() {},
1356                 send_form: function() { L.error('InternalError', 'Not implemented') },
1357         });
1358
1359         XHR.get = function() { return window.L.get.apply(window.L, arguments) };
1360         XHR.post = function() { return window.L.post.apply(window.L, arguments) };
1361         XHR.poll = function() { return window.L.poll.apply(window.L, arguments) };
1362         XHR.stop = Request.poll.remove.bind(Request.poll);
1363         XHR.halt = Request.poll.stop.bind(Request.poll);
1364         XHR.run = Request.poll.start.bind(Request.poll);
1365         XHR.running = Request.poll.active.bind(Request.poll);
1366
1367         window.XHR = XHR;
1368         window.LuCI = LuCI;
1369 })(window, document);