diff options
Diffstat (limited to 'modules/luci-base/htdocs/luci-static/resources/luci.js')
-rw-r--r-- | modules/luci-base/htdocs/luci-static/resources/luci.js | 67 |
1 files changed, 60 insertions, 7 deletions
diff --git a/modules/luci-base/htdocs/luci-static/resources/luci.js b/modules/luci-base/htdocs/luci-static/resources/luci.js index 93ecbac15f..259679f137 100644 --- a/modules/luci-base/htdocs/luci-static/resources/luci.js +++ b/modules/luci-base/htdocs/luci-static/resources/luci.js @@ -23,6 +23,20 @@ }); } + /* 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 */ @@ -210,8 +224,7 @@ }); - var requestQueue = [], - rpcBaseURL = null; + var requestQueue = []; function isQueueableRequest(opt) { if (!classes.rpc) @@ -223,8 +236,7 @@ if (opt.nobatch === true) return false; - if (rpcBaseURL == null) - rpcBaseURL = Request.expandURL(classes.rpc.getBaseURL()); + var rpcBaseURL = Request.expandURL(classes.rpc.getBaseURL()); return (rpcBaseURL != null && opt.url.indexOf(rpcBaseURL) == 0); } @@ -331,7 +343,9 @@ 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; @@ -544,6 +558,7 @@ var dummyElem = null, domParser = null, originalCBIInit = null, + rpcBaseURL = null, classes = {}; var LuCI = Class.extend({ @@ -579,7 +594,9 @@ Promise.all([ domReady, this.require('ui'), - this.require('form') + this.require('rpc'), + this.require('form'), + this.probeRPCBaseURL() ]).then(this.setupDOM.bind(this)).catch(this.error); originalCBIInit = window.cbi_init; @@ -752,7 +769,43 @@ }, /* DOM setup */ - setupDOM: function(ev) { + probeRPCBaseURL: function() { + if (rpcBaseURL == null) { + try { + rpcBaseURL = window.sessionStorage.getItem('rpcBaseURL'); + } + catch (e) { } + } + + 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); + }, + + setupDOM: function(res) { + var domEv = res[0], + uiClass = res[1], + rpcClass = res[2], + formClass = res[3], + rpcBaseURL = res[4]; + + rpcClass.setBaseURL(rpcBaseURL); + Request.addInterceptor(function(res) { if (res.status != 403 || res.headers.get('X-LuCI-Login-Required') != 'yes') return; |