From: Jo-Philipp Wich Date: Thu, 16 Apr 2020 08:46:45 +0000 (+0200) Subject: luci-base: luci.js: share environment object among LuCI base classes X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=bf4dd8e335b8b6046ca21e4d9f6c0ed33b6d3090;p=oweals%2Fluci.git 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 (cherry picked from commit e385640505352efe8e470d5b723e8b467a0d6fb6) --- diff --git a/modules/luci-base/htdocs/luci-static/resources/luci.js b/modules/luci-base/htdocs/luci-static/resources/luci.js index 6c9bec8b6..2245e0fca 100644 --- a/modules/luci-base/htdocs/luci-static/resources/luci.js +++ b/modules/luci-base/htdocs/luci-static/resources/luci.js @@ -12,6 +12,8 @@ (function(window, document, undefined) { 'use strict'; + var env = {}; + /* Object.assign polyfill for IE */ if (typeof Object.assign !== 'function') { Object.defineProperty(Object, 'assign', { @@ -1069,7 +1071,7 @@ */ add: function(fn, interval) { if (interval == null || interval <= 0) - interval = window.L ? window.L.env.pollinterval : null; + interval = env.pollinterval || null; if (isNaN(interval) || typeof(fn) != 'function') throw new TypeError('Invalid argument to LuCI.poll.add()'); @@ -2207,24 +2209,24 @@ var LuCI = Class.extend(/** @lends LuCI.prototype */ { __name__: 'LuCI', - __init__: function(env) { + __init__: function(setenv) { 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=([^?]+))?$/); if (m) { - env.base_url = m[1]; - env.resource_version = m[2]; + setenv.base_url = m[1]; + setenv.resource_version = m[2]; } } }); - if (env.base_url == null) + if (setenv.base_url == null) 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); document.addEventListener('poll-start', function(ev) { document.querySelectorAll('[id^="xhr_poll_status"]').forEach(function(e) { @@ -2437,7 +2439,7 @@ 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); var compileClass = function(res) { @@ -2536,8 +2538,8 @@ if (rpcBaseURL == null) { var rpcFallbackURL = this.url('admin/ubus'); - rpcBaseURL = Request.get(this.env.ubuspath).then(function(res) { - return (rpcBaseURL = (res.status == 400) ? L.env.ubuspath : rpcFallbackURL); + rpcBaseURL = Request.get(env.ubuspath).then(function(res) { + return (rpcBaseURL = (res.status == 400) ? env.ubuspath : rpcFallbackURL); }, function() { return (rpcBaseURL = rpcFallbackURL); }).then(function(url) { @@ -2721,7 +2723,7 @@ * @instance * @memberof LuCI */ - env: {}, + env: env, /** * Construct an absolute filesystem path relative to the server @@ -2737,7 +2739,7 @@ * Return the joined path. */ fspath: function(/* ... */) { - var path = this.env.documentroot; + var path = env.documentroot; for (var i = 0; i < arguments.length; i++) path += '/' + arguments[i]; @@ -2806,7 +2808,7 @@ * Returns the resulting URL path. */ url: function() { - return this.path(this.env.scriptname, arguments); + return this.path(env.scriptname, arguments); }, /** @@ -2828,7 +2830,7 @@ * Returns the resulting URL path. */ resource: function() { - return this.path(this.env.resource, arguments); + return this.path(env.resource, arguments); }, /** @@ -2850,7 +2852,7 @@ * Returns the resulting URL path. */ media: function() { - return this.path(this.env.media, arguments); + return this.path(env.media, arguments); }, /** @@ -2863,7 +2865,7 @@ * Returns the URL path to the current view. */ location: function() { - return this.path(this.env.scriptname, this.env.requestpath); + return this.path(env.scriptname, env.requestpath); }, @@ -3107,9 +3109,9 @@ */ poll: function(interval, url, args, cb, post) { 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'; if (!/^(?:\/|\S+:\/\/)/.test(url))