luci-base: luci.js: share environment object among LuCI base classes

This allows us to get rid of global `L` references in luci.js itself.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
This commit is contained in:
Jo-Philipp Wich 2020-04-16 10:46:45 +02:00
parent 237db822bd
commit e385640505

View file

@ -12,6 +12,8 @@
(function(window, document, undefined) { (function(window, document, undefined) {
'use strict'; 'use strict';
var env = {};
/* Object.assign polyfill for IE */ /* Object.assign polyfill for IE */
if (typeof Object.assign !== 'function') { if (typeof Object.assign !== 'function') {
Object.defineProperty(Object, 'assign', { Object.defineProperty(Object, 'assign', {
@ -1069,7 +1071,7 @@
*/ */
add: function(fn, interval) { add: function(fn, interval) {
if (interval == null || interval <= 0) if (interval == null || interval <= 0)
interval = window.L ? window.L.env.pollinterval : null; interval = env.pollinterval || null;
if (isNaN(interval) || typeof(fn) != 'function') if (isNaN(interval) || typeof(fn) != 'function')
throw new TypeError('Invalid argument to LuCI.poll.add()'); throw new TypeError('Invalid argument to LuCI.poll.add()');
@ -2101,24 +2103,24 @@
var LuCI = Class.extend(/** @lends LuCI.prototype */ { var LuCI = Class.extend(/** @lends LuCI.prototype */ {
__name__: 'LuCI', __name__: 'LuCI',
__init__: function(env) { __init__: function(setenv) {
document.querySelectorAll('script[src*="/luci.js"]').forEach(function(s) { document.querySelectorAll('script[src*="/luci.js"]').forEach(function(s) {
if (env.base_url == null || env.base_url == '') { if (setenv.base_url == null || setenv.base_url == '') {
var m = (s.getAttribute('src') || '').match(/^(.*)\/luci\.js(?:\?v=([^?]+))?$/); var m = (s.getAttribute('src') || '').match(/^(.*)\/luci\.js(?:\?v=([^?]+))?$/);
if (m) { if (m) {
env.base_url = m[1]; setenv.base_url = m[1];
env.resource_version = m[2]; setenv.resource_version = m[2];
} }
} }
}); });
if (env.base_url == null) if (setenv.base_url == null)
this.error('InternalError', 'Cannot find url of luci.js'); this.error('InternalError', 'Cannot find url of luci.js');
env.cgi_base = env.scriptname.replace(/\/[^\/]+$/, ''); setenv.cgi_base = setenv.scriptname.replace(/\/[^\/]+$/, '');
Object.assign(this.env, env); Object.assign(env, setenv);
var domReady = new Promise(function(resolveFn, rejectFn) { var domReady = new Promise(function(resolveFn, rejectFn) {
document.addEventListener('DOMContentLoaded', resolveFn); document.addEventListener('DOMContentLoaded', resolveFn);
@ -2319,7 +2321,7 @@
return Promise.resolve(classes[name]); return Promise.resolve(classes[name]);
} }
url = '%s/%s.js%s'.format(L.env.base_url, name.replace(/\./g, '/'), (L.env.resource_version ? '?v=' + L.env.resource_version : '')); url = '%s/%s.js%s'.format(env.base_url, name.replace(/\./g, '/'), (env.resource_version ? '?v=' + env.resource_version : ''));
from = [ name ].concat(from); from = [ name ].concat(from);
var compileClass = function(res) { var compileClass = function(res) {
@ -2422,8 +2424,8 @@
if (rpcBaseURL == null) { if (rpcBaseURL == null) {
var rpcFallbackURL = this.url('admin/ubus'); var rpcFallbackURL = this.url('admin/ubus');
rpcBaseURL = Request.get(this.env.ubuspath).then(function(res) { rpcBaseURL = Request.get(env.ubuspath).then(function(res) {
return (rpcBaseURL = (res.status == 400) ? L.env.ubuspath : rpcFallbackURL); return (rpcBaseURL = (res.status == 400) ? env.ubuspath : rpcFallbackURL);
}, function() { }, function() {
return (rpcBaseURL = rpcFallbackURL); return (rpcBaseURL = rpcFallbackURL);
}).then(function(url) { }).then(function(url) {
@ -2653,7 +2655,7 @@
* @instance * @instance
* @memberof LuCI * @memberof LuCI
*/ */
env: {}, env: env,
/** /**
* Construct an absolute filesystem path relative to the server * Construct an absolute filesystem path relative to the server
@ -2669,7 +2671,7 @@
* Return the joined path. * Return the joined path.
*/ */
fspath: function(/* ... */) { fspath: function(/* ... */) {
var path = this.env.documentroot; var path = env.documentroot;
for (var i = 0; i < arguments.length; i++) for (var i = 0; i < arguments.length; i++)
path += '/' + arguments[i]; path += '/' + arguments[i];
@ -2738,7 +2740,7 @@
* Returns the resulting URL path. * Returns the resulting URL path.
*/ */
url: function() { url: function() {
return this.path(this.env.scriptname, arguments); return this.path(env.scriptname, arguments);
}, },
/** /**
@ -2760,7 +2762,7 @@
* Returns the resulting URL path. * Returns the resulting URL path.
*/ */
resource: function() { resource: function() {
return this.path(this.env.resource, arguments); return this.path(env.resource, arguments);
}, },
/** /**
@ -2782,7 +2784,7 @@
* Returns the resulting URL path. * Returns the resulting URL path.
*/ */
media: function() { media: function() {
return this.path(this.env.media, arguments); return this.path(env.media, arguments);
}, },
/** /**
@ -2795,7 +2797,7 @@
* Returns the URL path to the current view. * Returns the URL path to the current view.
*/ */
location: function() { location: function() {
return this.path(this.env.scriptname, this.env.requestpath); return this.path(env.scriptname, env.requestpath);
}, },
@ -3039,9 +3041,9 @@
*/ */
poll: function(interval, url, args, cb, post) { poll: function(interval, url, args, cb, post) {
if (interval !== null && interval <= 0) if (interval !== null && interval <= 0)
interval = this.env.pollinterval; interval = env.pollinterval;
var data = post ? { token: this.env.token } : null, var data = post ? { token: env.token } : null,
method = post ? 'POST' : 'GET'; method = post ? 'POST' : 'GET';
if (!/^(?:\/|\S+:\/\/)/.test(url)) if (!/^(?:\/|\S+:\/\/)/.test(url))