(function(window, document, undefined) {
+ 'use strict';
+
/* Object.assign polyfill for IE */
if (typeof Object.assign !== 'function') {
Object.defineProperty(Object, 'assign', {
});
}
+ /* Promise.finally polyfill */
+ if (typeof Promise.prototype.finally !== 'function') {
+ Promise.prototype.finally = function(fn) {
+ var onFinally = function(cb) {
+ return Promise.resolve(fn.call(this)).then(cb);
+ };
+
+ return this.then(
+ function(result) { return onFinally.call(this, function() { return result }) },
+ function(reason) { return onFinally.call(this, function() { return Promise.reject(reason) }) }
+ );
+ };
+ }
+
/*
* Class declaration and inheritance helper
*/
* HTTP Request helper
*/
- Headers = Class.extend({
+ var Headers = Class.extend({
__name__: 'LuCI.XHR.Headers',
__init__: function(xhr) {
var hdrs = this.headers = {};
}
});
- Response = Class.extend({
+ var Response = Class.extend({
__name__: 'LuCI.XHR.Response',
- __init__: function(xhr, url, duration) {
+ __init__: function(xhr, url, duration, headers, content) {
this.ok = (xhr.status >= 200 && xhr.status <= 299);
this.status = xhr.status;
this.statusText = xhr.statusText;
- this.responseText = xhr.responseText;
- this.headers = new Headers(xhr);
+ this.headers = (headers != null) ? headers : new Headers(xhr);
this.duration = duration;
this.url = url;
this.xhr = xhr;
+
+ if (content != null && typeof(content) == 'object') {
+ this.responseJSON = content;
+ this.responseText = null;
+ }
+ else if (content != null) {
+ this.responseJSON = null;
+ this.responseText = String(content);
+ }
+ else {
+ this.responseJSON = null;
+ this.responseText = xhr.responseText;
+ }
+ },
+
+ clone: function(content) {
+ var copy = new Response(this.xhr, this.url, this.duration, this.headers, content);
+
+ copy.ok = this.ok;
+ copy.status = this.status;
+ copy.statusText = this.statusText;
+
+ return copy;
},
json: function() {
- return JSON.parse(this.responseText);
+ if (this.responseJSON == null)
+ this.responseJSON = JSON.parse(this.responseText);
+
+ return this.responseJSON;
},
text: function() {
+ if (this.responseText == null && this.responseJSON != null)
+ this.responseText = JSON.stringify(this.responseJSON);
+
return this.responseText;
}
});
- Request = Class.singleton({
+
+ var requestQueue = [];
+
+ function isQueueableRequest(opt) {
+ if (!classes.rpc)
+ return false;
+
+ if (opt.method != 'POST' || typeof(opt.content) != 'object')
+ return false;
+
+ if (opt.nobatch === true)
+ return false;
+
+ var rpcBaseURL = Request.expandURL(classes.rpc.getBaseURL());
+
+ return (rpcBaseURL != null && opt.url.indexOf(rpcBaseURL) == 0);
+ }
+
+ function flushRequestQueue() {
+ if (!requestQueue.length)
+ return;
+
+ var reqopt = Object.assign({}, requestQueue[0][0], { content: [], nobatch: true }),
+ batch = [];
+
+ for (var i = 0; i < requestQueue.length; i++) {
+ batch[i] = requestQueue[i];
+ reqopt.content[i] = batch[i][0].content;
+ }
+
+ requestQueue.length = 0;
+
+ Request.request(rpcBaseURL, reqopt).then(function(reply) {
+ var json = null, req = null;
+
+ try { json = reply.json() }
+ catch(e) { }
+
+ while ((req = batch.shift()) != null)
+ if (Array.isArray(json) && json.length)
+ req[2].call(reqopt, reply.clone(json.shift()));
+ else
+ req[1].call(reqopt, new Error('No related RPC reply'));
+ }).catch(function(error) {
+ var req = null;
+
+ while ((req = batch.shift()) != null)
+ req[1].call(reqopt, error);
+ });
+ }
+
+ var Request = Class.singleton({
__name__: 'LuCI.Request',
interceptors: [],
+ expandURL: function(url) {
+ if (!/^(?:[^/]+:)?\/\//.test(url))
+ url = location.protocol + '//' + location.host + url;
+
+ return url;
+ },
+
request: function(target, options) {
- var state = { xhr: new XMLHttpRequest(), url: target, start: Date.now() },
+ var state = { xhr: new XMLHttpRequest(), url: this.expandURL(target), start: Date.now() },
opt = Object.assign({}, options, state),
content = null,
contenttype = null,
if (!opt.cache)
opt.url += ((/\?/).test(opt.url) ? '&' : '?') + (new Date()).getTime();
- if (!/^(?:[^/]+:)?\/\//.test(opt.url))
- opt.url = location.protocol + '//' + location.host + opt.url;
+ if (isQueueableRequest(opt)) {
+ requestQueue.push([opt, rejectFn, resolveFn]);
+ requestAnimationFrame(flushRequestQueue);
+ return;
+ }
if ('username' in opt && 'password' in opt)
opt.xhr.open(opt.method, opt.url, true, opt.username, opt.password);
opt.xhr.open(opt.method, opt.url, true);
opt.xhr.responseType = 'text';
- opt.xhr.overrideMimeType('application/octet-stream');
+
+ if ('overrideMimeType' in opt.xhr)
+ opt.xhr.overrideMimeType('application/octet-stream');
if ('timeout' in opt)
opt.xhr.timeout = +opt.timeout;
break;
case 'object':
- content = JSON.stringify(opt.content);
- contenttype = 'application/json';
+ if (!(opt.content instanceof FormData)) {
+ content = JSON.stringify(opt.content);
+ contenttype = 'application/json';
+ }
+ else {
+ content = opt.content;
+ }
break;
default:
contenttype = opt.headers[header];
}
+ if ('progress' in opt && 'upload' in opt.xhr)
+ opt.xhr.upload.addEventListener('progress', opt.progress);
+
if (contenttype != null)
opt.xhr.setRequestHeader('Content-Type', contenttype);
},
handleReadyStateChange: function(resolveFn, rejectFn, ev) {
- var xhr = this.xhr;
+ var xhr = this.xhr,
+ duration = Date.now() - this.start;
if (xhr.readyState !== 4)
return;
if (xhr.status === 0 && xhr.statusText === '') {
- rejectFn.call(this, new Error('XHR request aborted by browser'));
+ if (duration >= this.timeout)
+ rejectFn.call(this, new Error('XHR request timed out'));
+ else
+ rejectFn.call(this, new Error('XHR request aborted by browser'));
}
else {
var response = new Response(
- xhr, xhr.responseURL || this.url, Date.now() - this.start);
+ xhr, xhr.responseURL || this.url, duration);
Promise.all(Request.interceptors.map(function(fn) { return fn(response) }))
.then(resolveFn.bind(this, response))
.catch(rejectFn.bind(this));
}
-
- try {
- xhr.abort();
- }
- catch(e) {}
},
get: function(url, options) {
return (this.interceptors.length < oldlen);
},
- poll: Class.singleton({
- __name__: 'LuCI.Request.Poll',
-
- queue: [],
-
+ poll: {
add: function(interval, url, options, callback) {
if (isNaN(interval) || interval <= 0)
throw new TypeError('Invalid poll interval');
- var e = {
- interval: interval,
- url: url,
- options: options,
- callback: callback
- };
+ var ival = interval >>> 0,
+ opts = Object.assign({}, options, { timeout: ival * 1000 - 5 });
- this.queue.push(e);
- return e;
+ return Poll.add(function() {
+ return Request.request(url, options).then(function(res) {
+ if (!Poll.active())
+ return;
+
+ try {
+ callback(res, res.json(), res.duration);
+ }
+ catch (err) {
+ callback(res, null, res.duration);
+ }
+ });
+ }, ival);
},
- remove: function(entry) {
- var oldlen = this.queue.length, i = oldlen;
+ remove: function(entry) { return Poll.remove(entry) },
+ start: function() { return Poll.start() },
+ stop: function() { return Poll.stop() },
+ active: function() { return Poll.active() }
+ }
+ });
- while (i--)
- if (this.queue[i] === entry) {
- delete this.queue[i].running;
- this.queue.splice(i, 1);
- }
+ var Poll = Class.singleton({
+ __name__: 'LuCI.Poll',
- if (!this.queue.length)
- this.stop();
+ queue: [],
- return (this.queue.length < oldlen);
- },
+ add: function(fn, interval) {
+ if (interval == null || interval <= 0)
+ interval = window.L ? window.L.env.pollinterval : null;
+
+ if (isNaN(interval) || typeof(fn) != 'function')
+ throw new TypeError('Invalid argument to LuCI.Poll.add()');
- start: function() {
- if (!this.queue.length || this.active())
+ for (var i = 0; i < this.queue.length; i++)
+ if (this.queue[i].fn === fn)
return false;
+ var e = {
+ r: true,
+ i: interval >>> 0,
+ fn: fn
+ };
+
+ this.queue.push(e);
+
+ if (this.tick != null && !this.active())
+ this.start();
+
+ return true;
+ },
+
+ remove: function(fn) {
+ if (typeof(fn) != 'function')
+ throw new TypeError('Invalid argument to LuCI.Poll.remove()');
+
+ var len = this.queue.length;
+
+ for (var i = len; i > 0; i--)
+ if (this.queue[i-1].fn === fn)
+ this.queue.splice(i-1, 1);
+
+ if (!this.queue.length && this.stop())
this.tick = 0;
+
+ return (this.queue.length != len);
+ },
+
+ start: function() {
+ if (this.active())
+ return false;
+
+ this.tick = 0;
+
+ if (this.queue.length) {
this.timer = window.setInterval(this.step, 1000);
this.step();
document.dispatchEvent(new CustomEvent('poll-start'));
- return true;
- },
+ }
- stop: function() {
- if (!this.active())
- return false;
+ return true;
+ },
- document.dispatchEvent(new CustomEvent('poll-stop'));
- window.clearInterval(this.timer);
- delete this.timer;
- delete this.tick;
- return true;
- },
+ stop: function() {
+ if (!this.active())
+ return false;
- step: function() {
- Request.poll.queue.forEach(function(e) {
- if ((Request.poll.tick % e.interval) != 0)
- return;
+ document.dispatchEvent(new CustomEvent('poll-stop'));
+ window.clearInterval(this.timer);
+ delete this.timer;
+ delete this.tick;
+ return true;
+ },
- if (e.running)
- return;
+ step: function() {
+ for (var i = 0, e = null; (e = Poll.queue[i]) != null; i++) {
+ if ((Poll.tick % e.i) != 0)
+ continue;
- var opts = Object.assign({}, e.options,
- { timeout: e.interval * 1000 - 5 });
+ if (!e.r)
+ continue;
- e.running = true;
- Request.request(e.url, opts)
- .then(function(res) {
- if (!e.running || !Request.poll.active())
- return;
+ e.r = false;
- try {
- e.callback(res, res.json(), res.duration);
- }
- catch (err) {
- e.callback(res, null, res.duration);
- }
- })
- .finally(function() { delete e.running });
- });
+ Promise.resolve(e.fn()).finally((function() { this.r = true }).bind(e));
+ }
- Request.poll.tick = (Request.poll.tick + 1) % Math.pow(2, 32);
- },
+ Poll.tick = (Poll.tick + 1) % Math.pow(2, 32);
+ },
- active: function() {
- return (this.timer != null);
- }
- })
+ active: function() {
+ return (this.timer != null);
+ }
});
- var modalDiv = null,
- tooltipDiv = null,
- tooltipTimeout = null,
- dummyElem = null,
+ var dummyElem = null,
domParser = null,
originalCBIInit = null,
+ rpcBaseURL = null,
+ sysFeatures = null,
classes = {};
- LuCI = Class.extend({
+ var LuCI = Class.extend({
__name__: 'LuCI',
__init__: function(env) {
- Object.assign(this.env, env);
- modalDiv = document.body.appendChild(
- this.dom.create('div', { id: 'modal_overlay' },
- this.dom.create('div', { class: 'modal', role: 'dialog', 'aria-modal': true })));
-
- tooltipDiv = document.body.appendChild(this.dom.create('div', { class: 'cbi-tooltip' }));
+ document.querySelectorAll('script[src*="/luci.js"]').forEach(function(s) {
+ if (env.base_url == null || env.base_url == '') {
+ var m = (s.getAttribute('src') || '').match(/^(.*)\/luci\.js(?:\?v=([^?]+))?$/);
+ if (m) {
+ env.base_url = m[1];
+ env.resource_version = m[2];
+ }
+ }
+ });
- document.addEventListener('mouseover', this.showTooltip.bind(this), true);
- document.addEventListener('mouseout', this.hideTooltip.bind(this), true);
- document.addEventListener('focus', this.showTooltip.bind(this), true);
- document.addEventListener('blur', this.hideTooltip.bind(this), true);
+ if (env.base_url == null)
+ this.error('InternalError', 'Cannot find url of luci.js');
- document.addEventListener('DOMContentLoaded', this.setupDOM.bind(this));
+ Object.assign(this.env, env);
document.addEventListener('poll-start', function(ev) {
document.querySelectorAll('[id^="xhr_poll_status"]').forEach(function(e) {
});
});
+ var domReady = new Promise(function(resolveFn, rejectFn) {
+ document.addEventListener('DOMContentLoaded', resolveFn);
+ });
+
+ Promise.all([
+ domReady,
+ this.require('ui'),
+ this.require('rpc'),
+ this.require('form'),
+ this.probeRPCBaseURL()
+ ]).then(this.setupDOM.bind(this)).catch(this.error);
+
originalCBIInit = window.cbi_init;
window.cbi_init = function() {};
},
+ raise: function(type, fmt /*, ...*/) {
+ var e = null,
+ msg = fmt ? String.prototype.format.apply(fmt, this.varargs(arguments, 2)) : null,
+ stack = null;
+
+ if (type instanceof Error) {
+ e = type;
+
+ if (msg)
+ e.message = msg + ': ' + e.message;
+ }
+ else {
+ try { throw new Error('stacktrace') }
+ catch (e2) { stack = (e2.stack || '').split(/\n/) }
+
+ e = new (window[type || 'Error'] || Error)(msg || 'Unspecified error');
+ e.name = type || 'Error';
+ }
+
+ stack = (stack || []).map(function(frame) {
+ frame = frame.replace(/(.*?)@(.+):(\d+):(\d+)/g, 'at $1 ($2:$3:$4)').trim();
+ return frame ? ' ' + frame : '';
+ });
+
+ if (!/^ at /.test(stack[0]))
+ stack.shift();
+
+ if (/\braise /.test(stack[0]))
+ stack.shift();
+
+ if (/\berror /.test(stack[0]))
+ stack.shift();
+
+ if (stack.length)
+ e.message += '\n' + stack.join('\n');
+
+ if (window.console && console.debug)
+ console.debug(e);
+
+ throw e;
+ },
+
+ error: function(type, fmt /*, ...*/) {
+ try {
+ L.raise.apply(L, Array.prototype.slice.call(arguments));
+ }
+ catch (e) {
+ if (!e.reported) {
+ if (L.ui)
+ L.ui.addNotification(e.name || _('Runtime error'),
+ E('pre', {}, e.message), 'danger');
+ else
+ L.dom.content(document.querySelector('#maincontent'),
+ E('pre', { 'class': 'alert-message error' }, e.message));
+
+ e.reported = true;
+ }
+
+ throw e;
+ }
+ },
+
+ bind: function(fn, self /*, ... */) {
+ return Function.prototype.bind.apply(fn, this.varargs(arguments, 2, self));
+ },
+
/* Class require */
require: function(name, from) {
var L = this, url = null, from = from || [];
if (classes[name] != null) {
/* Circular dependency */
if (from.indexOf(name) != -1)
- throw new Error('Circular dependency: class "%s" depends on "%s"'
- .format(name, from.join('" which depends on "')));
+ L.raise('DependencyError',
+ 'Circular dependency: class "%s" depends on "%s"',
+ name, from.join('" which depends on "'));
return classes[name];
}
- document.querySelectorAll('script[src$="/luci.js"]').forEach(function(s) {
- url = '%s/%s.js'.format(
- s.getAttribute('src').replace(/\/luci\.js$/, ''),
- name.replace(/\./g, '/'));
- });
-
- if (url == null)
- throw new Error('Cannot find url of luci.js');
-
+ url = '%s/%s.js%s'.format(L.env.base_url, name.replace(/\./g, '/'), (L.env.resource_version ? '?v=' + L.env.resource_version : ''));
from = [ name ].concat(from);
var compileClass = function(res) {
if (!res.ok)
- throw new Error('HTTP error %d while loading class file "%s"'
- .format(res.status, url));
+ L.raise('NetworkError',
+ 'HTTP error %d while loading class file "%s"', res.status, url);
var source = res.text(),
- reqmatch = /(?:^|\n)[ \t]*(?:["']require[ \t]+(\S+)(?:[ \t]+as[ \t]+([a-zA-Z_]\S*))?["']);/g,
+ requirematch = /^require[ \t]+(\S+)(?:[ \t]+as[ \t]+([a-zA-Z_]\S*))?$/,
+ strictmatch = /^use[ \t]+strict$/,
depends = [],
args = '';
/* find require statements in source */
- for (var m = reqmatch.exec(source); m; m = reqmatch.exec(source)) {
- var dep = m[1], as = m[2] || dep.replace(/[^a-zA-Z0-9_]/g, '_');
- depends.push(L.require(dep, from));
- args += ', ' + as;
+ for (var i = 0, off = -1, quote = -1, esc = false; i < source.length; i++) {
+ var chr = source.charCodeAt(i);
+
+ if (esc) {
+ esc = false;
+ }
+ else if (chr == 92) {
+ esc = true;
+ }
+ else if (chr == quote) {
+ var s = source.substring(off, i),
+ m = requirematch.exec(s);
+
+ if (m) {
+ var dep = m[1], as = m[2] || dep.replace(/[^a-zA-Z0-9_]/g, '_');
+ depends.push(L.require(dep, from));
+ args += ', ' + as;
+ }
+ else if (!strictmatch.exec(s)) {
+ break;
+ }
+
+ off = -1;
+ quote = -1;
+ }
+ else if (quote == -1 && (chr == 34 || chr == 39)) {
+ off = i + 1;
+ quote = chr;
+ }
}
/* load dependencies and instantiate class */
return Promise.all(depends).then(function(instances) {
+ var _factory, _class;
+
try {
_factory = eval(
'(function(window, document, L%s) { %s })\n\n//# sourceURL=%s\n'
.format(args, source, res.url));
}
catch (error) {
- throw new SyntaxError('%s\n in %s:%s'
- .format(error.message, res.url, error.lineNumber || '?'));
+ L.raise('SyntaxError', '%s\n in %s:%s',
+ error.message, res.url, error.lineNumber || '?');
}
_factory.displayName = toCamelCase(name + 'ClassFactory');
_class = _factory.apply(_factory, [window, document, L].concat(instances));
if (!Class.isSubclass(_class))
- throw new TypeError('"%s" factory yields invalid constructor'
- .format(name));
+ L.error('TypeError', '"%s" factory yields invalid constructor', name);
if (_class.displayName == 'AnonymousClass')
_class.displayName = toCamelCase(name + 'Class');
for (var i = 0; ptr && i < parts.length - 1; i++)
ptr = ptr[parts[i]];
- if (!ptr)
- throw new Error('Parent "%s" for class "%s" is missing'
- .format(parts.slice(0, i).join('.'), name));
+ if (ptr)
+ ptr[parts[i]] = instance;
- classes[name] = ptr[parts[i]] = instance;
+ classes[name] = instance;
return instance;
});
},
/* DOM setup */
- setupDOM: function(ev) {
- this.tabs.init();
+ probeRPCBaseURL: function() {
+ if (rpcBaseURL == null) {
+ try {
+ rpcBaseURL = window.sessionStorage.getItem('rpcBaseURL');
+ }
+ catch (e) { }
+ }
- Request.addInterceptor(function(res) {
- if (res.status != 403 || res.headers.get('X-LuCI-Login-Required') != 'yes')
+ if (rpcBaseURL == null) {
+ var rpcFallbackURL = this.url('admin/ubus');
+
+ rpcBaseURL = Request.get('/ubus/').then(function(res) {
+ return (rpcBaseURL = (res.status == 400) ? '/ubus/' : rpcFallbackURL);
+ }, function() {
+ return (rpcBaseURL = rpcFallbackURL);
+ }).then(function(url) {
+ try {
+ window.sessionStorage.setItem('rpcBaseURL', url);
+ }
+ catch (e) { }
+
+ return url;
+ });
+ }
+
+ return Promise.resolve(rpcBaseURL);
+ },
+
+ probeSystemFeatures: function() {
+ var sessionid = classes.rpc.getSessionID();
+
+ if (sysFeatures == null) {
+ try {
+ var data = JSON.parse(window.sessionStorage.getItem('sysFeatures'));
+
+ if (this.isObject(data) && this.isObject(data[sessionid]))
+ sysFeatures = data[sessionid];
+ }
+ catch (e) {}
+ }
+
+ if (!this.isObject(sysFeatures)) {
+ sysFeatures = classes.rpc.declare({
+ object: 'luci',
+ method: 'getFeatures',
+ expect: { '': {} }
+ })().then(function(features) {
+ try {
+ var data = {};
+ data[sessionid] = features;
+
+ window.sessionStorage.setItem('sysFeatures', JSON.stringify(data));
+ }
+ catch (e) {}
+
+ sysFeatures = features;
+
+ return features;
+ });
+ }
+
+ return Promise.resolve(sysFeatures);
+ },
+
+ hasSystemFeature: function() {
+ var ft = sysFeatures[arguments[0]];
+
+ if (arguments.length == 2)
+ return this.isObject(ft) ? ft[arguments[1]] : null;
+
+ return (ft != null && ft != false);
+ },
+
+ notifySessionExpiry: function() {
+ Poll.stop();
+
+ L.ui.showModal(_('Session expired'), [
+ E('div', { class: 'alert-message warning' },
+ _('A new login is required since the authentication session expired.')),
+ E('div', { class: 'right' },
+ E('div', {
+ class: 'btn primary',
+ click: function() {
+ var loc = window.location;
+ window.location = loc.protocol + '//' + loc.host + loc.pathname + loc.search;
+ }
+ }, _('To login…')))
+ ]);
+
+ L.raise('SessionError', 'Login session is expired');
+ },
+
+ setupDOM: function(res) {
+ var domEv = res[0],
+ uiClass = res[1],
+ rpcClass = res[2],
+ formClass = res[3],
+ rpcBaseURL = res[4];
+
+ rpcClass.setBaseURL(rpcBaseURL);
+
+ rpcClass.addInterceptor(function(msg, req) {
+ if (!L.isObject(msg) || !L.isObject(msg.error) || msg.error.code != -32002)
return;
- Request.poll.stop();
-
- L.showModal(_('Session expired'), [
- E('div', { class: 'alert-message warning' },
- _('A new login is required since the authentication session expired.')),
- E('div', { class: 'right' },
- E('div', {
- class: 'btn primary',
- click: function() {
- var loc = window.location;
- window.location = loc.protocol + '//' + loc.host + loc.pathname + loc.search;
- }
- }, _('To login…')))
- ]);
+ if (!L.isObject(req) || (req.object == 'session' && req.method == 'access'))
+ return;
+
+ return rpcClass.declare({
+ 'object': 'session',
+ 'method': 'access',
+ 'params': [ 'scope', 'object', 'function' ],
+ 'expect': { access: true }
+ })('uci', 'luci', 'read').catch(L.notifySessionExpiry);
+ });
+
+ Request.addInterceptor(function(res) {
+ var isDenied = false;
+
+ if (res.status == 403 && res.headers.get('X-LuCI-Login-Required') == 'yes')
+ isDenied = true;
+
+ if (!isDenied)
+ return;
- return Promise.reject(new Error('Session expired'));
+ L.notifySessionExpiry();
});
+ return this.probeSystemFeatures().finally(this.initDOM);
+ },
+
+ initDOM: function() {
originalCBIInit();
- Request.poll.start();
+ Poll.start();
+ document.dispatchEvent(new CustomEvent('luci-loaded'));
},
env: {},
},
+ /* Data helpers */
+ isObject: function(val) {
+ return (val != null && typeof(val) == 'object');
+ },
+
+ sortedKeys: function(obj, key, sortmode) {
+ if (obj == null || typeof(obj) != 'object')
+ return [];
+
+ return Object.keys(obj).map(function(e) {
+ var v = (key != null) ? obj[e][key] : e;
+
+ switch (sortmode) {
+ case 'addr':
+ v = (v != null) ? v.replace(/(?:^|[.:])([0-9a-fA-F]{1,4})/g,
+ function(m0, m1) { return ('000' + m1.toLowerCase()).substr(-4) }) : null;
+ break;
+
+ case 'num':
+ v = (v != null) ? +v : null;
+ break;
+ }
+
+ return [ e, v ];
+ }).filter(function(e) {
+ return (e[1] != null);
+ }).sort(function(a, b) {
+ return (a[1] > b[1]);
+ }).map(function(e) {
+ return e[0];
+ });
+ },
+
+ toArray: function(val) {
+ if (val == null)
+ return [];
+ else if (Array.isArray(val))
+ return val;
+ else if (typeof(val) == 'object')
+ return [ val ];
+
+ var s = String(val).trim();
+
+ if (s == '')
+ return [];
+
+ return s.split(/\s+/);
+ },
+
+
/* HTTP resource fetching */
get: function(url, args, cb) {
return this.poll(null, url, args, cb, false);
});
},
- stop: function(entry) { return Request.poll.remove(entry) },
- halt: function() { return Request.poll.stop() },
- run: function() { return Request.poll.start() },
-
+ stop: function(entry) { return Poll.remove(entry) },
+ halt: function() { return Poll.stop() },
+ run: function() { return Poll.start() },
- /* Modal dialog */
- showModal: function(title, children) {
- var dlg = modalDiv.firstElementChild;
+ /* DOM manipulation */
+ dom: Class.singleton({
+ __name__: 'LuCI.DOM',
- dlg.setAttribute('class', 'modal');
+ elem: function(e) {
+ return (e != null && typeof(e) == 'object' && 'nodeType' in e);
+ },
- this.dom.content(dlg, this.dom.create('h4', {}, title));
- this.dom.append(dlg, children);
+ parse: function(s) {
+ var elem;
- document.body.classList.add('modal-overlay-active');
+ try {
+ domParser = domParser || new DOMParser();
+ elem = domParser.parseFromString(s, 'text/html').body.firstChild;
+ }
+ catch(e) {}
- return dlg;
- },
+ if (!elem) {
+ try {
+ dummyElem = dummyElem || document.createElement('div');
+ dummyElem.innerHTML = s;
+ elem = dummyElem.firstChild;
+ }
+ catch (e) {}
+ }
- hideModal: function() {
- document.body.classList.remove('modal-overlay-active');
- },
+ return elem || null;
+ },
+ matches: function(node, selector) {
+ var m = this.elem(node) ? node.matches || node.msMatchesSelector : null;
+ return m ? m.call(node, selector) : false;
+ },
- /* Tooltip */
- showTooltip: function(ev) {
- var target = findParent(ev.target, '[data-tooltip]');
+ parent: function(node, selector) {
+ if (this.elem(node) && node.closest)
+ return node.closest(selector);
- if (!target)
- return;
+ while (this.elem(node))
+ if (this.matches(node, selector))
+ return node;
+ else
+ node = node.parentNode;
- if (tooltipTimeout !== null) {
- window.clearTimeout(tooltipTimeout);
- tooltipTimeout = null;
- }
+ return null;
+ },
- var rect = target.getBoundingClientRect(),
- x = rect.left + window.pageXOffset,
- y = rect.top + rect.height + window.pageYOffset;
+ append: function(node, children) {
+ if (!this.elem(node))
+ return null;
- tooltipDiv.className = 'cbi-tooltip';
- tooltipDiv.innerHTML = '▲ ';
- tooltipDiv.firstChild.data += target.getAttribute('data-tooltip');
+ if (Array.isArray(children)) {
+ for (var i = 0; i < children.length; i++)
+ if (this.elem(children[i]))
+ node.appendChild(children[i]);
+ else if (children !== null && children !== undefined)
+ node.appendChild(document.createTextNode('' + children[i]));
- if (target.hasAttribute('data-tooltip-style'))
- tooltipDiv.classList.add(target.getAttribute('data-tooltip-style'));
+ return node.lastChild;
+ }
+ else if (typeof(children) === 'function') {
+ return this.append(node, children(node));
+ }
+ else if (this.elem(children)) {
+ return node.appendChild(children);
+ }
+ else if (children !== null && children !== undefined) {
+ node.innerHTML = '' + children;
+ return node.lastChild;
+ }
- if ((y + tooltipDiv.offsetHeight) > (window.innerHeight + window.pageYOffset)) {
- y -= (tooltipDiv.offsetHeight + target.offsetHeight);
- tooltipDiv.firstChild.data = '▼ ' + tooltipDiv.firstChild.data.substr(2);
- }
+ return null;
+ },
- tooltipDiv.style.top = y + 'px';
- tooltipDiv.style.left = x + 'px';
- tooltipDiv.style.opacity = 1;
+ content: function(node, children) {
+ if (!this.elem(node))
+ return null;
- tooltipDiv.dispatchEvent(new CustomEvent('tooltip-open', {
- bubbles: true,
- detail: { target: target }
- }));
- },
+ var dataNodes = node.querySelectorAll('[data-idref]');
- hideTooltip: function(ev) {
- if (ev.target === tooltipDiv || ev.relatedTarget === tooltipDiv ||
- tooltipDiv.contains(ev.target) || tooltipDiv.contains(ev.relatedTarget))
- return;
+ for (var i = 0; i < dataNodes.length; i++)
+ delete this.registry[dataNodes[i].getAttribute('data-idref')];
- if (tooltipTimeout !== null) {
- window.clearTimeout(tooltipTimeout);
- tooltipTimeout = null;
- }
+ while (node.firstChild)
+ node.removeChild(node.firstChild);
- tooltipDiv.style.opacity = 0;
- tooltipTimeout = window.setTimeout(function() { tooltipDiv.removeAttribute('style'); }, 250);
+ return this.append(node, children);
+ },
- tooltipDiv.dispatchEvent(new CustomEvent('tooltip-close', { bubbles: true }));
- },
+ attr: function(node, key, val) {
+ if (!this.elem(node))
+ return null;
+ var attr = null;
- /* Widget helper */
- itemlist: function(node, items, separators) {
- var children = [];
+ if (typeof(key) === 'object' && key !== null)
+ attr = key;
+ else if (typeof(key) === 'string')
+ attr = {}, attr[key] = val;
- if (!Array.isArray(separators))
- separators = [ separators || E('br') ];
+ for (key in attr) {
+ if (!attr.hasOwnProperty(key) || attr[key] == null)
+ continue;
- for (var i = 0; i < items.length; i += 2) {
- if (items[i+1] !== null && items[i+1] !== undefined) {
- var sep = separators[(i/2) % separators.length],
- cld = [];
+ switch (typeof(attr[key])) {
+ case 'function':
+ node.addEventListener(key, attr[key]);
+ break;
- children.push(E('span', { class: 'nowrap' }, [
- items[i] ? E('strong', items[i] + ': ') : '',
- items[i+1]
- ]));
+ case 'object':
+ node.setAttribute(key, JSON.stringify(attr[key]));
+ break;
- if ((i+2) < items.length)
- children.push(this.dom.elem(sep) ? sep.cloneNode(true) : sep);
+ default:
+ node.setAttribute(key, attr[key]);
+ }
}
- }
-
- this.dom.content(node, children);
-
- return node;
- },
-
- Class: Class,
- Request: Request
- });
-
- /* Tabs */
- LuCI.prototype.tabs = {
- init: function() {
- var groups = [], prevGroup = null, currGroup = null;
-
- document.querySelectorAll('[data-tab]').forEach(function(tab) {
- var parent = tab.parentNode;
-
- if (!parent.hasAttribute('data-tab-group'))
- parent.setAttribute('data-tab-group', groups.length);
+ },
- currGroup = +parent.getAttribute('data-tab-group');
+ create: function() {
+ var html = arguments[0],
+ attr = arguments[1],
+ data = arguments[2],
+ elem;
- if (currGroup !== prevGroup) {
- prevGroup = currGroup;
+ if (!(attr instanceof Object) || Array.isArray(attr))
+ data = attr, attr = null;
- if (!groups[currGroup])
- groups[currGroup] = [];
+ if (Array.isArray(html)) {
+ elem = document.createDocumentFragment();
+ for (var i = 0; i < html.length; i++)
+ elem.appendChild(this.create(html[i]));
+ }
+ else if (this.elem(html)) {
+ elem = html;
+ }
+ else if (html.charCodeAt(0) === 60) {
+ elem = this.parse(html);
+ }
+ else {
+ elem = document.createElement(html);
}
- groups[currGroup].push(tab);
- });
-
- for (var i = 0; i < groups.length; i++)
- this.initTabGroup(groups[i]);
-
- document.addEventListener('dependency-update', this.updateTabs.bind(this));
-
- this.updateTabs();
-
- if (!groups.length)
- this.setActiveTabId(-1, -1);
- },
-
- initTabGroup: function(panes) {
- if (!Array.isArray(panes) || panes.length === 0)
- return;
-
- var menu = E('ul', { 'class': 'cbi-tabmenu' }),
- group = panes[0].parentNode,
- groupId = +group.getAttribute('data-tab-group'),
- selected = null;
-
- for (var i = 0, pane; pane = panes[i]; i++) {
- var name = pane.getAttribute('data-tab'),
- title = pane.getAttribute('data-tab-title'),
- active = pane.getAttribute('data-tab-active') === 'true';
-
- menu.appendChild(E('li', {
- 'class': active ? 'cbi-tab' : 'cbi-tab-disabled',
- 'data-tab': name
- }, E('a', {
- 'href': '#',
- 'click': this.switchTab.bind(this)
- }, title)));
-
- if (active)
- selected = i;
- }
-
- group.parentNode.insertBefore(menu, group);
+ if (!elem)
+ return null;
- if (selected === null) {
- selected = this.getActiveTabId(groupId);
+ this.attr(elem, attr);
+ this.append(elem, data);
- if (selected < 0 || selected >= panes.length)
- selected = 0;
+ return elem;
+ },
- menu.childNodes[selected].classList.add('cbi-tab');
- menu.childNodes[selected].classList.remove('cbi-tab-disabled');
- panes[selected].setAttribute('data-tab-active', 'true');
+ registry: {},
- this.setActiveTabId(groupId, selected);
- }
- },
+ data: function(node, key, val) {
+ var id = node.getAttribute('data-idref');
- getActiveTabState: function() {
- var page = document.body.getAttribute('data-page');
+ /* clear all data */
+ if (arguments.length > 1 && key == null) {
+ if (id != null) {
+ node.removeAttribute('data-idref');
+ val = this.registry[id]
+ delete this.registry[id];
+ return val;
+ }
- try {
- var val = JSON.parse(window.sessionStorage.getItem('tab'));
- if (val.page === page && Array.isArray(val.groups))
- return val;
- }
- catch(e) {}
+ return null;
+ }
- window.sessionStorage.removeItem('tab');
- return { page: page, groups: [] };
- },
+ /* clear a key */
+ else if (arguments.length > 2 && key != null && val == null) {
+ if (id != null) {
+ val = this.registry[id][key];
+ delete this.registry[id][key];
+ return val;
+ }
- getActiveTabId: function(groupId) {
- return +this.getActiveTabState().groups[groupId] || 0;
- },
+ return null;
+ }
- setActiveTabId: function(groupId, tabIndex) {
- try {
- var state = this.getActiveTabState();
- state.groups[groupId] = tabIndex;
+ /* set a key */
+ else if (arguments.length > 2 && key != null && val != null) {
+ if (id == null) {
+ do { id = Math.floor(Math.random() * 0xffffffff).toString(16) }
+ while (this.registry.hasOwnProperty(id));
- window.sessionStorage.setItem('tab', JSON.stringify(state));
- }
- catch (e) { return false; }
+ node.setAttribute('data-idref', id);
+ this.registry[id] = {};
+ }
- return true;
- },
+ return (this.registry[id][key] = val);
+ }
- updateTabs: function(ev) {
- document.querySelectorAll('[data-tab-title]').forEach(function(pane) {
- var menu = pane.parentNode.previousElementSibling,
- tab = menu.querySelector('[data-tab="%s"]'.format(pane.getAttribute('data-tab'))),
- n_errors = pane.querySelectorAll('.cbi-input-invalid').length;
+ /* get all data */
+ else if (arguments.length == 1) {
+ if (id != null)
+ return this.registry[id];
- if (!pane.firstElementChild) {
- tab.style.display = 'none';
- tab.classList.remove('flash');
- }
- else if (tab.style.display === 'none') {
- tab.style.display = '';
- requestAnimationFrame(function() { tab.classList.add('flash') });
+ return null;
}
- if (n_errors) {
- tab.setAttribute('data-errors', n_errors);
- tab.setAttribute('data-tooltip', _('%d invalid field(s)').format(n_errors));
- tab.setAttribute('data-tooltip-style', 'error');
+ /* get a key */
+ else if (arguments.length == 2) {
+ if (id != null)
+ return this.registry[id][key];
}
- else {
- tab.removeAttribute('data-errors');
- tab.removeAttribute('data-tooltip');
- }
- });
- },
-
- switchTab: function(ev) {
- var tab = ev.target.parentNode,
- name = tab.getAttribute('data-tab'),
- menu = tab.parentNode,
- group = menu.nextElementSibling,
- groupId = +group.getAttribute('data-tab-group'),
- index = 0;
-
- ev.preventDefault();
- if (!tab.classList.contains('cbi-tab-disabled'))
- return;
-
- menu.querySelectorAll('[data-tab]').forEach(function(tab) {
- tab.classList.remove('cbi-tab');
- tab.classList.remove('cbi-tab-disabled');
- tab.classList.add(
- tab.getAttribute('data-tab') === name ? 'cbi-tab' : 'cbi-tab-disabled');
- });
-
- group.childNodes.forEach(function(pane) {
- if (L.dom.matches(pane, '[data-tab]')) {
- if (pane.getAttribute('data-tab') === name) {
- pane.setAttribute('data-tab-active', 'true');
- L.tabs.setActiveTabId(groupId, index);
- }
- else {
- pane.setAttribute('data-tab-active', 'false');
- }
-
- index++;
- }
- });
- }
- };
+ return null;
+ },
- /* DOM manipulation */
- LuCI.prototype.dom = {
- elem: function(e) {
- return (typeof(e) === 'object' && e !== null && 'nodeType' in e);
- },
+ bindClassInstance: function(node, inst) {
+ if (!(inst instanceof Class))
+ L.error('TypeError', 'Argument must be a class instance');
- parse: function(s) {
- var elem;
+ return this.data(node, '_class', inst);
+ },
- try {
- domParser = domParser || new DOMParser();
- elem = domParser.parseFromString(s, 'text/html').body.firstChild;
- }
- catch(e) {}
+ findClassInstance: function(node) {
+ var inst = null;
- if (!elem) {
- try {
- dummyElem = dummyElem || document.createElement('div');
- dummyElem.innerHTML = s;
- elem = dummyElem.firstChild;
+ do {
+ inst = this.data(node, '_class');
+ node = node.parentNode;
}
- catch (e) {}
- }
-
- return elem || null;
- },
+ while (!(inst instanceof Class) && node != null);
- matches: function(node, selector) {
- var m = this.elem(node) ? node.matches || node.msMatchesSelector : null;
- return m ? m.call(node, selector) : false;
- },
-
- parent: function(node, selector) {
- if (this.elem(node) && node.closest)
- return node.closest(selector);
+ return inst;
+ },
- while (this.elem(node))
- if (this.matches(node, selector))
- return node;
- else
- node = node.parentNode;
+ callClassMethod: function(node, method /*, ... */) {
+ var inst = this.findClassInstance(node);
- return null;
- },
+ if (inst == null || typeof(inst[method]) != 'function')
+ return null;
- append: function(node, children) {
- if (!this.elem(node))
- return null;
+ return inst[method].apply(inst, inst.varargs(arguments, 2));
+ },
- if (Array.isArray(children)) {
- for (var i = 0; i < children.length; i++)
- if (this.elem(children[i]))
- node.appendChild(children[i]);
- else if (children !== null && children !== undefined)
- node.appendChild(document.createTextNode('' + children[i]));
+ isEmpty: function(node, ignoreFn) {
+ for (var child = node.firstElementChild; child != null; child = child.nextElementSibling)
+ if (!child.classList.contains('hidden') && (!ignoreFn || !ignoreFn(child)))
+ return false;
- return node.lastChild;
- }
- else if (typeof(children) === 'function') {
- return this.append(node, children(node));
- }
- else if (this.elem(children)) {
- return node.appendChild(children);
- }
- else if (children !== null && children !== undefined) {
- node.innerHTML = '' + children;
- return node.lastChild;
+ return true;
}
+ }),
- return null;
- },
+ Poll: Poll,
+ Class: Class,
+ Request: Request,
- content: function(node, children) {
- if (!this.elem(node))
- return null;
+ view: Class.extend({
+ __name__: 'LuCI.View',
- while (node.firstChild)
- node.removeChild(node.firstChild);
+ __init__: function() {
+ var vp = document.getElementById('view');
- return this.append(node, children);
- },
+ L.dom.content(vp, E('div', { 'class': 'spinning' }, _('Loading view…')));
- attr: function(node, key, val) {
- if (!this.elem(node))
- return null;
+ return Promise.resolve(this.load())
+ .then(L.bind(this.render, this))
+ .then(L.bind(function(nodes) {
+ var vp = document.getElementById('view');
- var attr = null;
+ L.dom.content(vp, nodes);
+ L.dom.append(vp, this.addFooter());
+ }, this)).catch(L.error);
+ },
- if (typeof(key) === 'object' && key !== null)
- attr = key;
- else if (typeof(key) === 'string')
- attr = {}, attr[key] = val;
+ load: function() {},
+ render: function() {},
- for (key in attr) {
- if (!attr.hasOwnProperty(key) || attr[key] === null || attr[key] === undefined)
- continue;
+ handleSave: function(ev) {
+ var tasks = [];
- switch (typeof(attr[key])) {
- case 'function':
- node.addEventListener(key, attr[key]);
- break;
+ document.getElementById('maincontent')
+ .querySelectorAll('.cbi-map').forEach(function(map) {
+ tasks.push(L.dom.callClassMethod(map, 'save'));
+ });
- case 'object':
- node.setAttribute(key, JSON.stringify(attr[key]));
- break;
+ return Promise.all(tasks);
+ },
- default:
- node.setAttribute(key, attr[key]);
- }
- }
- },
+ handleSaveApply: function(ev) {
+ return this.handleSave(ev).then(function() {
+ L.ui.changes.apply(true);
+ });
+ },
- create: function() {
- var html = arguments[0],
- attr = (arguments[1] instanceof Object && !Array.isArray(arguments[1])) ? arguments[1] : null,
- data = attr ? arguments[2] : arguments[1],
- elem;
+ handleReset: function(ev) {
+ var tasks = [];
- if (this.elem(html))
- elem = html;
- else if (html.charCodeAt(0) === 60)
- elem = this.parse(html);
- else
- elem = document.createElement(html);
+ document.getElementById('maincontent')
+ .querySelectorAll('.cbi-map').forEach(function(map) {
+ tasks.push(L.dom.callClassMethod(map, 'reset'));
+ });
- if (!elem)
- return null;
+ return Promise.all(tasks);
+ },
- this.attr(elem, attr);
- this.append(elem, data);
+ addFooter: function() {
+ var footer = E([]);
+
+ if (this.handleSaveApply || this.handleSave || this.handleReset) {
+ footer.appendChild(E('div', { 'class': 'cbi-page-actions' }, [
+ this.handleSaveApply ? E('button', {
+ 'class': 'cbi-button cbi-button-apply',
+ 'click': L.ui.createHandlerFn(this, 'handleSaveApply')
+ }, [ _('Save & Apply') ]) : '', ' ',
+ this.handleSave ? E('button', {
+ 'class': 'cbi-button cbi-button-save',
+ 'click': L.ui.createHandlerFn(this, 'handleSave')
+ }, [ _('Save') ]) : '', ' ',
+ this.handleReset ? E('button', {
+ 'class': 'cbi-button cbi-button-reset',
+ 'click': L.ui.createHandlerFn(this, 'handleReset')
+ }, [ _('Reset') ]) : ''
+ ]));
+ }
- return elem;
- }
- };
+ return footer;
+ }
+ })
+ });
- XHR = Class.extend({
+ var XHR = Class.extend({
__name__: 'LuCI.XHR',
__init__: function() {
if (window.console && console.debug)
cancel: function() { delete this.active },
busy: function() { return (this.active === true) },
abort: function() {},
- send_form: function() { throw 'Not implemented' },
+ send_form: function() { L.error('InternalError', 'Not implemented') },
});
XHR.get = function() { return window.L.get.apply(window.L, arguments) };