5 * This is the LuCI base class. It is automatically instantiated and
6 * accessible using the global `L` variable.
9 * The environment settings to use for the LuCI runtime.
12 (function(window
, document
, undefined) {
15 /* Object.assign polyfill for IE */
16 if (typeof Object
.assign
!== 'function') {
17 Object
.defineProperty(Object
, 'assign', {
18 value
: function assign(target
, varArgs
) {
20 throw new TypeError('Cannot convert undefined or null to object');
22 var to
= Object(target
);
24 for (var index
= 1; index
< arguments
.length
; index
++)
25 if (arguments
[index
] != null)
26 for (var nextKey
in arguments
[index
])
27 if (Object
.prototype.hasOwnProperty
.call(arguments
[index
], nextKey
))
28 to
[nextKey
] = arguments
[index
][nextKey
];
37 /* Promise.finally polyfill */
38 if (typeof Promise
.prototype.finally !== 'function') {
39 Promise
.prototype.finally = function(fn
) {
40 var onFinally = function(cb
) {
41 return Promise
.resolve(fn
.call(this)).then(cb
);
45 function(result
) { return onFinally
.call(this, function() { return result
}) },
46 function(reason
) { return onFinally
.call(this, function() { return Promise
.reject(reason
) }) }
52 * Class declaration and inheritance helper
55 var toCamelCase = function(s
) {
56 return s
.replace(/(?:^|[\. -])(.)/g, function(m0
, m1
) { return m1
.toUpperCase() });
65 * `LuCI.Class` is the abstract base class all LuCI classes inherit from.
67 * It provides simple means to create subclasses of given classes and
68 * implements prototypal inheritance.
70 var superContext
= {}, classIndex
= 0, Class
= Object
.assign(function() {}, {
72 * Extends this base class with the properties described in
73 * `properties` and returns a new subclassed Class instance
75 * @memberof LuCI.Class
77 * @param {Object<string, *>} properties
78 * An object describing the properties to add to the new
81 * @returns {LuCI.Class}
82 * Returns a new LuCI.Class sublassed from this class, extended
83 * by the given properties and with its prototype set to this base
84 * class to enable inheritance. The resulting value represents a
85 * class constructor and can be instantiated with `new`.
87 extend: function(properties
) {
89 __id__
: { value
: classIndex
},
90 __base__
: { value
: this.prototype },
91 __name__
: { value
: properties
.__name__
|| 'anonymous' + classIndex
++ }
94 var ClassConstructor = function() {
95 if (!(this instanceof ClassConstructor
))
96 throw new TypeError('Constructor must not be called without "new"');
98 if (Object
.getPrototypeOf(this).hasOwnProperty('__init__')) {
99 if (typeof(this.__init__
) != 'function')
100 throw new TypeError('Class __init__ member is not a function');
102 this.__init__
.apply(this, arguments
)
105 this.super('__init__', arguments
);
109 for (var key
in properties
)
110 if (!props
[key
] && properties
.hasOwnProperty(key
))
111 props
[key
] = { value
: properties
[key
], writable
: true };
113 ClassConstructor
.prototype = Object
.create(this.prototype, props
);
114 ClassConstructor
.prototype.constructor = ClassConstructor
;
115 Object
.assign(ClassConstructor
, this);
116 ClassConstructor
.displayName
= toCamelCase(props
.__name__
.value
+ 'Class');
118 return ClassConstructor
;
122 * Extends this base class with the properties described in
123 * `properties`, instantiates the resulting subclass using
124 * the additional optional arguments passed to this function
125 * and returns the resulting subclassed Class instance.
127 * This function serves as a convenience shortcut for
128 * {@link LuCI.Class.extend Class.extend()} and subsequent
131 * @memberof LuCI.Class
133 * @param {Object<string, *>} properties
134 * An object describing the properties to add to the new
137 * @param {...*} [new_args]
138 * Specifies arguments to be passed to the subclass constructor
139 * as-is in order to instantiate the new subclass.
141 * @returns {LuCI.Class}
142 * Returns a new LuCI.Class instance extended by the given
143 * properties with its prototype set to this base class to
144 * enable inheritance.
146 singleton: function(properties
/*, ... */) {
147 return Class
.extend(properties
)
148 .instantiate(Class
.prototype.varargs(arguments
, 1));
152 * Calls the class constructor using `new` with the given argument
153 * array being passed as variadic parameters to the constructor.
155 * @memberof LuCI.Class
157 * @param {Array<*>} params
158 * An array of arbitrary values which will be passed as arguments
159 * to the constructor function.
161 * @param {...*} [new_args]
162 * Specifies arguments to be passed to the subclass constructor
163 * as-is in order to instantiate the new subclass.
165 * @returns {LuCI.Class}
166 * Returns a new LuCI.Class instance extended by the given
167 * properties with its prototype set to this base class to
168 * enable inheritance.
170 instantiate: function(args
) {
171 return new (Function
.prototype.bind
.apply(this,
172 Class
.prototype.varargs(args
, 0, null)))();
176 call: function(self
, method
) {
177 if (typeof(this.prototype[method
]) != 'function')
178 throw new ReferenceError(method
+ ' is not defined in class');
180 return this.prototype[method
].apply(self
, self
.varargs(arguments
, 1));
184 * Checks whether the given class value is a subclass of this class.
186 * @memberof LuCI.Class
188 * @param {LuCI.Class} classValue
189 * The class object to test.
192 * Returns `true` when the given `classValue` is a subclass of this
193 * class or `false` if the given value is not a valid class or not
194 * a subclass of this class'.
196 isSubclass: function(classValue
) {
197 return (classValue
!= null &&
198 typeof(classValue
) == 'function' &&
199 classValue
.prototype instanceof this);
204 * Extract all values from the given argument array beginning from
205 * `offset` and prepend any further given optional parameters to
206 * the beginning of the resulting array copy.
208 * @memberof LuCI.Class
211 * @param {Array<*>} args
212 * The array to extract the values from.
214 * @param {number} offset
215 * The offset from which to extract the values. An offset of `0`
216 * would copy all values till the end.
218 * @param {...*} [extra_args]
219 * Extra arguments to add to prepend to the resultung array.
221 * @returns {Array<*>}
222 * Returns a new array consisting of the optional extra arguments
223 * and the values extracted from the `args` array beginning with
226 varargs: function(args
, offset
/*, ... */) {
227 return Array
.prototype.slice
.call(arguments
, 2)
228 .concat(Array
.prototype.slice
.call(args
, offset
));
232 * Walks up the parent class chain and looks for a class member
233 * called `key` in any of the parent classes this class inherits
234 * from. Returns the member value of the superclass or calls the
235 * member as function and returns its return value when the
236 * optional `callArgs` array is given.
238 * This function has two signatures and is sensitive to the
239 * amount of arguments passed to it:
241 * Returns the value of `key` when found within one of the
243 * - `super('key', ['arg1', 'arg2'])` -
244 * Calls the `key()` method with parameters `arg1` and `arg2`
245 * when found within one of the parent classes.
247 * @memberof LuCI.Class
250 * @param {string} key
251 * The name of the superclass member to retrieve.
253 * @param {Array<*>} [callArgs]
254 * An optional array of function call parameters to use. When
255 * this parameter is specified, the found member value is called
256 * as function using the values of this array as arguments.
258 * @throws {ReferenceError}
259 * Throws a `ReferenceError` when `callArgs` are specified and
260 * the found member named by `key` is not a function value.
263 * Returns the value of the found member or the return value of
264 * the call to the found method. Returns `null` when no member
265 * was found in the parent class chain or when the call to the
266 * superclass method returned `null`.
268 super: function(key
, callArgs
) {
272 var slotIdx
= this.__id__
+ '.' + key
,
273 symStack
= superContext
[slotIdx
],
276 for (protoCtx
= Object
.getPrototypeOf(symStack
? symStack
[0] : Object
.getPrototypeOf(this));
277 protoCtx
!= null && !protoCtx
.hasOwnProperty(key
);
278 protoCtx
= Object
.getPrototypeOf(protoCtx
)) {}
280 if (protoCtx
== null)
283 var res
= protoCtx
[key
];
285 if (arguments
.length
> 1) {
286 if (typeof(res
) != 'function')
287 throw new ReferenceError(key
+ ' is not a function in base class');
289 if (typeof(callArgs
) != 'object')
290 callArgs
= this.varargs(arguments
, 1);
293 symStack
.unshift(protoCtx
);
295 superContext
[slotIdx
] = [ protoCtx
];
297 res
= res
.apply(this, callArgs
);
299 if (symStack
&& symStack
.length
> 1)
300 symStack
.shift(protoCtx
);
302 delete superContext
[slotIdx
];
309 * Returns a string representation of this class.
312 * Returns a string representation of this class containing the
313 * constructor functions `displayName` and describing the class
314 * members and their respective types.
316 toString: function() {
317 var s
= '[' + this.constructor.displayName
+ ']', f
= true;
318 for (var k
in this) {
319 if (this.hasOwnProperty(k
)) {
320 s
+= (f
? ' {\n' : '') + ' ' + k
+ ': ' + typeof(this[k
]) + '\n';
324 return s
+ (f
? '' : '}');
336 * The `Headers` class is an internal utility class exposed in HTTP
337 * response objects using the `response.headers` property.
339 var Headers
= Class
.extend(/** @lends LuCI.Headers.prototype */ {
340 __name__
: 'LuCI.XHR.Headers',
341 __init__: function(xhr
) {
342 var hdrs
= this.headers
= {};
343 xhr
.getAllResponseHeaders().split(/\r\n/).forEach(function(line
) {
344 var m
= /^([^:]+):(.*)$/.exec(line
);
346 hdrs
[m
[1].trim().toLowerCase()] = m
[2].trim();
351 * Checks whether the given header name is present.
352 * Note: Header-Names are case-insensitive.
355 * @memberof LuCI.Headers
356 * @param {string} name
357 * The header name to check
360 * Returns `true` if the header name is present, `false` otherwise
362 has: function(name
) {
363 return this.headers
.hasOwnProperty(String(name
).toLowerCase());
367 * Returns the value of the given header name.
368 * Note: Header-Names are case-insensitive.
371 * @memberof LuCI.Headers
372 * @param {string} name
373 * The header name to read
375 * @returns {string|null}
376 * The value of the given header name or `null` if the header isn't present.
378 get: function(name
) {
379 var key
= String(name
).toLowerCase();
380 return this.headers
.hasOwnProperty(key
) ? this.headers
[key
] : null;
390 * The `Response` class is an internal utility class representing HTTP responses.
392 var Response
= Class
.extend({
393 __name__
: 'LuCI.XHR.Response',
394 __init__: function(xhr
, url
, duration
, headers
, content
) {
396 * Describes whether the response is successful (status codes `200..299`) or not
398 * @memberof LuCI.Response
402 this.ok
= (xhr
.status
>= 200 && xhr
.status
<= 299);
405 * The numeric HTTP status code of the response
407 * @memberof LuCI.Response
411 this.status
= xhr
.status
;
414 * The HTTP status description message of the response
416 * @memberof LuCI.Response
420 this.statusText
= xhr
.statusText
;
423 * The HTTP headers of the response
425 * @memberof LuCI.Response
427 * @type {LuCI.Headers}
429 this.headers
= (headers
!= null) ? headers
: new Headers(xhr
);
432 * The total duration of the HTTP request in milliseconds
434 * @memberof LuCI.Response
438 this.duration
= duration
;
441 * The final URL of the request, i.e. after following redirects.
443 * @memberof LuCI.Response
452 if (content
!= null && typeof(content
) == 'object') {
453 this.responseJSON
= content
;
454 this.responseText
= null;
456 else if (content
!= null) {
457 this.responseJSON
= null;
458 this.responseText
= String(content
);
461 this.responseJSON
= null;
462 this.responseText
= xhr
.responseText
;
467 * Clones the given response object, optionally overriding the content
468 * of the cloned instance.
471 * @memberof LuCI.Response
472 * @param {*} [content]
473 * Override the content of the cloned response. Object values will be
474 * treated as JSON response data, all other types will be converted
475 * using `String()` and treated as response text.
477 * @returns {LuCI.Response}
478 * The cloned `Response` instance.
480 clone: function(content
) {
481 var copy
= new Response(this.xhr
, this.url
, this.duration
, this.headers
, content
);
484 copy
.status
= this.status
;
485 copy
.statusText
= this.statusText
;
491 * Access the response content as JSON data.
494 * @memberof LuCI.Response
495 * @throws {SyntaxError}
496 * Throws `SyntaxError` if the content isn't valid JSON.
499 * The parsed JSON data.
502 if (this.responseJSON
== null)
503 this.responseJSON
= JSON
.parse(this.responseText
);
505 return this.responseJSON
;
509 * Access the response content as string.
512 * @memberof LuCI.Response
514 * The response content.
517 if (this.responseText
== null && this.responseJSON
!= null)
518 this.responseText
= JSON
.stringify(this.responseJSON
);
520 return this.responseText
;
525 var requestQueue
= [];
527 function isQueueableRequest(opt
) {
531 if (opt
.method
!= 'POST' || typeof(opt
.content
) != 'object')
534 if (opt
.nobatch
=== true)
537 var rpcBaseURL
= Request
.expandURL(classes
.rpc
.getBaseURL());
539 return (rpcBaseURL
!= null && opt
.url
.indexOf(rpcBaseURL
) == 0);
542 function flushRequestQueue() {
543 if (!requestQueue
.length
)
546 var reqopt
= Object
.assign({}, requestQueue
[0][0], { content
: [], nobatch
: true }),
549 for (var i
= 0; i
< requestQueue
.length
; i
++) {
550 batch
[i
] = requestQueue
[i
];
551 reqopt
.content
[i
] = batch
[i
][0].content
;
554 requestQueue
.length
= 0;
556 Request
.request(rpcBaseURL
, reqopt
).then(function(reply
) {
557 var json
= null, req
= null;
559 try { json
= reply
.json() }
562 while ((req
= batch
.shift()) != null)
563 if (Array
.isArray(json
) && json
.length
)
564 req
[2].call(reqopt
, reply
.clone(json
.shift()));
566 req
[1].call(reqopt
, new Error('No related RPC reply'));
567 }).catch(function(error
) {
570 while ((req
= batch
.shift()) != null)
571 req
[1].call(reqopt
, error
);
581 * The `Request` class allows initiating HTTP requests and provides utilities
582 * for dealing with responses.
584 var Request
= Class
.singleton(/** @lends LuCI.Request.prototype */ {
585 __name__
: 'LuCI.Request',
590 * Turn the given relative URL into an absolute URL if necessary.
593 * @memberof LuCI.Request
594 * @param {string} url
595 * The URL to convert.
598 * The absolute URL derived from the given one, or the original URL
599 * if it already was absolute.
601 expandURL: function(url
) {
602 if (!/^(?:[^/]+:)?\/\//.test(url
))
603 url
= location
.protocol
+ '//' + location
.host
+ url
;
609 * @typedef {Object} RequestOptions
610 * @memberof LuCI.Request
612 * @property {string} [method=GET]
613 * The HTTP method to use, e.g. `GET` or `POST`.
615 * @property {Object<string, Object|string>} [query]
616 * Query string data to append to the URL. Non-string values of the
617 * given object will be converted to JSON.
619 * @property {boolean} [cache=false]
620 * Specifies whether the HTTP response may be retrieved from cache.
622 * @property {string} [username]
623 * Provides a username for HTTP basic authentication.
625 * @property {string} [password]
626 * Provides a password for HTTP basic authentication.
628 * @property {number} [timeout]
629 * Specifies the request timeout in seconds.
631 * @property {boolean} [credentials=false]
632 * Whether to include credentials such as cookies in the request.
634 * @property {*} [content]
635 * Specifies the HTTP message body to send along with the request.
636 * If the value is a function, it is invoked and the return value
637 * used as content, if it is a FormData instance, it is used as-is,
638 * if it is an object, it will be converted to JSON, in all other
639 * cases it is converted to a string.
641 * @property {Object<string, string>} [header]
642 * Specifies HTTP headers to set for the request.
644 * @property {function} [progress]
645 * An optional request callback function which receives ProgressEvent
646 * instances as sole argument during the HTTP request transfer.
650 * Initiate an HTTP request to the given target.
653 * @memberof LuCI.Request
654 * @param {string} target
655 * The URL to request.
657 * @param {LuCI.Request.RequestOptions} [options]
658 * Additional options to configure the request.
660 * @returns {Promise<LuCI.Response>}
661 * The resulting HTTP response.
663 request: function(target
, options
) {
664 var state
= { xhr
: new XMLHttpRequest(), url
: this.expandURL(target
), start
: Date
.now() },
665 opt
= Object
.assign({}, options
, state
),
668 callback
= this.handleReadyStateChange
;
670 return new Promise(function(resolveFn
, rejectFn
) {
671 opt
.xhr
.onreadystatechange
= callback
.bind(opt
, resolveFn
, rejectFn
);
672 opt
.method
= String(opt
.method
|| 'GET').toUpperCase();
674 if ('query' in opt
) {
675 var q
= (opt
.query
!= null) ? Object
.keys(opt
.query
).map(function(k
) {
676 if (opt
.query
[k
] != null) {
677 var v
= (typeof(opt
.query
[k
]) == 'object')
678 ? JSON
.stringify(opt
.query
[k
])
679 : String(opt
.query
[k
]);
681 return '%s=%s'.format(encodeURIComponent(k
), encodeURIComponent(v
));
684 return encodeURIComponent(k
);
689 switch (opt
.method
) {
693 opt
.url
+= ((/\?/).test(opt
.url
) ? '&' : '?') + q
;
697 if (content
== null) {
699 contenttype
= 'application/x-www-form-urlencoded';
706 opt
.url
+= ((/\?/).test(opt
.url
) ? '&' : '?') + (new Date()).getTime();
708 if (isQueueableRequest(opt
)) {
709 requestQueue
.push([opt
, rejectFn
, resolveFn
]);
710 requestAnimationFrame(flushRequestQueue
);
714 if ('username' in opt
&& 'password' in opt
)
715 opt
.xhr
.open(opt
.method
, opt
.url
, true, opt
.username
, opt
.password
);
717 opt
.xhr
.open(opt
.method
, opt
.url
, true);
719 opt
.xhr
.responseType
= 'text';
721 if ('overrideMimeType' in opt
.xhr
)
722 opt
.xhr
.overrideMimeType('application/octet-stream');
724 if ('timeout' in opt
)
725 opt
.xhr
.timeout
= +opt
.timeout
;
727 if ('credentials' in opt
)
728 opt
.xhr
.withCredentials
= !!opt
.credentials
;
730 if (opt
.content
!= null) {
731 switch (typeof(opt
.content
)) {
733 content
= opt
.content(xhr
);
737 if (!(opt
.content
instanceof FormData
)) {
738 content
= JSON
.stringify(opt
.content
);
739 contenttype
= 'application/json';
742 content
= opt
.content
;
747 content
= String(opt
.content
);
751 if ('headers' in opt
)
752 for (var header
in opt
.headers
)
753 if (opt
.headers
.hasOwnProperty(header
)) {
754 if (header
.toLowerCase() != 'content-type')
755 opt
.xhr
.setRequestHeader(header
, opt
.headers
[header
]);
757 contenttype
= opt
.headers
[header
];
760 if ('progress' in opt
&& 'upload' in opt
.xhr
)
761 opt
.xhr
.upload
.addEventListener('progress', opt
.progress
);
763 if (contenttype
!= null)
764 opt
.xhr
.setRequestHeader('Content-Type', contenttype
);
767 opt
.xhr
.send(content
);
770 rejectFn
.call(opt
, e
);
775 handleReadyStateChange: function(resolveFn
, rejectFn
, ev
) {
777 duration
= Date
.now() - this.start
;
779 if (xhr
.readyState
!== 4)
782 if (xhr
.status
=== 0 && xhr
.statusText
=== '') {
783 if (duration
>= this.timeout
)
784 rejectFn
.call(this, new Error('XHR request timed out'));
786 rejectFn
.call(this, new Error('XHR request aborted by browser'));
789 var response
= new Response(
790 xhr
, xhr
.responseURL
|| this.url
, duration
);
792 Promise
.all(Request
.interceptors
.map(function(fn
) { return fn(response
) }))
793 .then(resolveFn
.bind(this, response
))
794 .catch(rejectFn
.bind(this));
799 * Initiate an HTTP GET request to the given target.
802 * @memberof LuCI.Request
803 * @param {string} target
804 * The URL to request.
806 * @param {LuCI.Request.RequestOptions} [options]
807 * Additional options to configure the request.
809 * @returns {Promise<LuCI.Response>}
810 * The resulting HTTP response.
812 get: function(url
, options
) {
813 return this.request(url
, Object
.assign({ method
: 'GET' }, options
));
817 * Initiate an HTTP POST request to the given target.
820 * @memberof LuCI.Request
821 * @param {string} target
822 * The URL to request.
825 * The request data to send, see {@link LuCI.Request.RequestOptions} for details.
827 * @param {LuCI.Request.RequestOptions} [options]
828 * Additional options to configure the request.
830 * @returns {Promise<LuCI.Response>}
831 * The resulting HTTP response.
833 post: function(url
, data
, options
) {
834 return this.request(url
, Object
.assign({ method
: 'POST', content
: data
}, options
));
838 * Interceptor functions are invoked whenever an HTTP reply is received, in the order
839 * these functions have been registered.
840 * @callback LuCI.Request.interceptorFn
841 * @param {LuCI.Response} res
842 * The HTTP response object
846 * Register an HTTP response interceptor function. Interceptor
847 * functions are useful to perform default actions on incoming HTTP
848 * responses, such as checking for expired authentication or for
849 * implementing request retries before returning a failure.
852 * @memberof LuCI.Request
853 * @param {LuCI.Request.interceptorFn} interceptorFn
854 * The interceptor function to register.
856 * @returns {LuCI.Request.interceptorFn}
857 * The registered function.
859 addInterceptor: function(interceptorFn
) {
860 if (typeof(interceptorFn
) == 'function')
861 this.interceptors
.push(interceptorFn
);
862 return interceptorFn
;
866 * Remove an HTTP response interceptor function. The passed function
867 * value must be the very same value that was used to register the
871 * @memberof LuCI.Request
872 * @param {LuCI.Request.interceptorFn} interceptorFn
873 * The interceptor function to remove.
876 * Returns `true` if any function has been removed, else `false`.
878 removeInterceptor: function(interceptorFn
) {
879 var oldlen
= this.interceptors
.length
, i
= oldlen
;
881 if (this.interceptors
[i
] === interceptorFn
)
882 this.interceptors
.splice(i
, 1);
883 return (this.interceptors
.length
< oldlen
);
888 * @memberof LuCI.Request
892 * The `Request.poll` class provides some convience wrappers around
893 * {@link LuCI.Poll} mainly to simplify registering repeating HTTP
894 * request calls as polling functions.
898 * The callback function is invoked whenever an HTTP reply to a
899 * polled request is received or when the polled request timed
902 * @callback LuCI.Request.poll~callbackFn
903 * @param {LuCI.Response} res
904 * The HTTP response object.
907 * The response JSON if the response could be parsed as such,
910 * @param {number} duration
911 * The total duration of the request in milliseconds.
915 * Register a repeating HTTP request with an optional callback
916 * to invoke whenever a response for the request is received.
919 * @memberof LuCI.Request.poll
920 * @param {number} interval
921 * The poll interval in seconds.
923 * @param {string} url
924 * The URL to request on each poll.
926 * @param {LuCI.Request.RequestOptions} [options]
927 * Additional options to configure the request.
929 * @param {LuCI.Request.poll~callbackFn} [callback]
930 * {@link LuCI.Request.poll~callbackFn Callback} function to
931 * invoke for each HTTP reply.
933 * @throws {TypeError}
934 * Throws `TypeError` when an invalid interval was passed.
936 * @returns {function}
937 * Returns the internally created poll function.
939 add: function(interval
, url
, options
, callback
) {
940 if (isNaN(interval
) || interval
<= 0)
941 throw new TypeError('Invalid poll interval');
943 var ival
= interval
>>> 0,
944 opts
= Object
.assign({}, options
, { timeout
: ival
* 1000 - 5 });
946 var fn = function() {
947 return Request
.request(url
, options
).then(function(res
) {
952 callback(res
, res
.json(), res
.duration
);
955 callback(res
, null, res
.duration
);
960 return (Poll
.add(fn
, ival
) ? fn
: null);
964 * Remove a polling request that has been previously added using `add()`.
965 * This function is essentially a wrapper around
966 * {@link LuCI.Poll.remove LuCI.Poll.remove()}.
969 * @memberof LuCI.Request.poll
970 * @param {function} entry
971 * The poll function returned by {@link LuCI.Request.poll#add add()}.
974 * Returns `true` if any function has been removed, else `false`.
976 remove: function(entry
) { return Poll
.remove(entry
) },
979 * Alias for {@link LuCI.Poll.start LuCI.Poll.start()}.
982 * @memberof LuCI.Request.poll
984 start: function() { return Poll
.start() },
987 * Alias for {@link LuCI.Poll.stop LuCI.Poll.stop()}.
990 * @memberof LuCI.Request.poll
992 stop: function() { return Poll
.stop() },
995 * Alias for {@link LuCI.Poll.active LuCI.Poll.active()}.
998 * @memberof LuCI.Request.poll
1000 active: function() { return Poll
.active() }
1010 * The `Poll` class allows registering and unregistering poll actions,
1011 * as well as starting, stopping and querying the state of the polling
1014 var Poll
= Class
.singleton(/** @lends LuCI.Poll.prototype */ {
1015 __name__
: 'LuCI.Poll',
1020 * Add a new operation to the polling loop. If the polling loop is not
1021 * already started at this point, it will be implicitely started.
1024 * @memberof LuCI.Poll
1025 * @param {function} fn
1026 * The function to invoke on each poll interval.
1028 * @param {number} interval
1029 * The poll interval in seconds.
1031 * @throws {TypeError}
1032 * Throws `TypeError` when an invalid interval was passed.
1034 * @returns {boolean}
1035 * Returns `true` if the function has been added or `false` if it
1036 * already is registered.
1038 add: function(fn
, interval
) {
1039 if (interval
== null || interval
<= 0)
1040 interval
= window
.L
? window
.L
.env
.pollinterval
: null;
1042 if (isNaN(interval
) || typeof(fn
) != 'function')
1043 throw new TypeError('Invalid argument to LuCI.Poll.add()');
1045 for (var i
= 0; i
< this.queue
.length
; i
++)
1046 if (this.queue
[i
].fn
=== fn
)
1057 if (this.tick
!= null && !this.active())
1064 * Remove an operation from the polling loop. If no further operatons
1065 * are registered, the polling loop is implicitely stopped.
1068 * @memberof LuCI.Poll
1069 * @param {function} fn
1070 * The function to remove.
1072 * @throws {TypeError}
1073 * Throws `TypeError` when the given argument isn't a function.
1075 * @returns {boolean}
1076 * Returns `true` if the function has been removed or `false` if it
1079 remove: function(fn
) {
1080 if (typeof(fn
) != 'function')
1081 throw new TypeError('Invalid argument to LuCI.Poll.remove()');
1083 var len
= this.queue
.length
;
1085 for (var i
= len
; i
> 0; i
--)
1086 if (this.queue
[i
-1].fn
=== fn
)
1087 this.queue
.splice(i
-1, 1);
1089 if (!this.queue
.length
&& this.stop())
1092 return (this.queue
.length
!= len
);
1096 * (Re)start the polling loop. Dispatches a custom `poll-start` event
1097 * to the `document` object upon successful start.
1100 * @memberof LuCI.Poll
1101 * @returns {boolean}
1102 * Returns `true` if polling has been started (or if no functions
1103 * where registered) or `false` when the polling loop already runs.
1111 if (this.queue
.length
) {
1112 this.timer
= window
.setInterval(this.step
, 1000);
1114 document
.dispatchEvent(new CustomEvent('poll-start'));
1121 * Stop the polling loop. Dispatches a custom `poll-stop` event
1122 * to the `document` object upon successful stop.
1125 * @memberof LuCI.Poll
1126 * @returns {boolean}
1127 * Returns `true` if polling has been stopped or `false` if it din't
1128 * run to begin with.
1134 document
.dispatchEvent(new CustomEvent('poll-stop'));
1135 window
.clearInterval(this.timer
);
1143 for (var i
= 0, e
= null; (e
= Poll
.queue
[i
]) != null; i
++) {
1144 if ((Poll
.tick
% e
.i
) != 0)
1152 Promise
.resolve(e
.fn()).finally((function() { this.r
= true }).bind(e
));
1155 Poll
.tick
= (Poll
.tick
+ 1) % Math
.pow(2, 32);
1159 * Test whether the polling loop is running.
1162 * @memberof LuCI.Poll
1163 * @returns {boolean} - Returns `true` if polling is active, else `false`.
1165 active: function() {
1166 return (this.timer
!= null);
1171 var dummyElem
= null,
1173 originalCBIInit
= null,
1178 var LuCI
= Class
.extend(/** @lends LuCI.prototype */ {
1180 __init__: function(env
) {
1182 document
.querySelectorAll('script[src*="/luci.js"]').forEach(function(s
) {
1183 if (env
.base_url
== null || env
.base_url
== '') {
1184 var m
= (s
.getAttribute('src') || '').match(/^(.*)\/luci\.js(?:\?v=([^?]+))?$/);
1186 env
.base_url
= m
[1];
1187 env
.resource_version
= m
[2];
1192 if (env
.base_url
== null)
1193 this.error('InternalError', 'Cannot find url of luci.js');
1195 Object
.assign(this.env
, env
);
1197 document
.addEventListener('poll-start', function(ev
) {
1198 document
.querySelectorAll('[id^="xhr_poll_status"]').forEach(function(e
) {
1199 e
.style
.display
= (e
.id
== 'xhr_poll_status_off') ? 'none' : '';
1203 document
.addEventListener('poll-stop', function(ev
) {
1204 document
.querySelectorAll('[id^="xhr_poll_status"]').forEach(function(e
) {
1205 e
.style
.display
= (e
.id
== 'xhr_poll_status_on') ? 'none' : '';
1209 var domReady
= new Promise(function(resolveFn
, rejectFn
) {
1210 document
.addEventListener('DOMContentLoaded', resolveFn
);
1216 this.require('rpc'),
1217 this.require('form'),
1218 this.probeRPCBaseURL()
1219 ]).then(this.setupDOM
.bind(this)).catch(this.error
);
1221 originalCBIInit
= window
.cbi_init
;
1222 window
.cbi_init = function() {};
1226 * Captures the current stack trace and throws an error of the
1227 * specified type as a new exception. Also logs the exception as
1228 * error to the debug console if it is available.
1233 * @param {Error|string} [type=Error]
1234 * Either a string specifying the type of the error to throw or an
1235 * existing `Error` instance to copy.
1237 * @param {string} [fmt=Unspecified error]
1238 * A format string which is used to form the error message, together
1239 * with all subsequent optional arguments.
1241 * @param {...*} [args]
1242 * Zero or more variable arguments to the supplied format string.
1245 * Throws the created error object with the captured stack trace
1246 * appended to the message and the type set to the given type
1247 * argument or copied from the given error instance.
1249 raise: function(type
, fmt
/*, ...*/) {
1251 msg
= fmt
? String
.prototype.format
.apply(fmt
, this.varargs(arguments
, 2)) : null,
1254 if (type
instanceof Error
) {
1258 e
.message
= msg
+ ': ' + e
.message
;
1261 try { throw new Error('stacktrace') }
1262 catch (e2
) { stack
= (e2
.stack
|| '').split(/\n/) }
1264 e
= new (window
[type
|| 'Error'] || Error
)(msg
|| 'Unspecified error');
1265 e
.name
= type
|| 'Error';
1268 stack
= (stack
|| []).map(function(frame
) {
1269 frame
= frame
.replace(/(.*?)@(.+):(\d+):(\d+)/g, 'at $1 ($2:$3:$4)').trim();
1270 return frame
? ' ' + frame
: '';
1273 if (!/^ at /.test(stack
[0]))
1276 if (/\braise /.test(stack
[0]))
1279 if (/\berror /.test(stack
[0]))
1283 e
.message
+= '\n' + stack
.join('\n');
1285 if (window
.console
&& console
.debug
)
1292 * A wrapper around {@link LuCI#raise raise()} which also renders
1293 * the error either as modal overlay when `ui.js` is already loaed
1294 * or directly into the view body.
1299 * @param {Error|string} [type=Error]
1300 * Either a string specifying the type of the error to throw or an
1301 * existing `Error` instance to copy.
1303 * @param {string} [fmt=Unspecified error]
1304 * A format string which is used to form the error message, together
1305 * with all subsequent optional arguments.
1307 * @param {...*} [args]
1308 * Zero or more variable arguments to the supplied format string.
1311 * Throws the created error object with the captured stack trace
1312 * appended to the message and the type set to the given type
1313 * argument or copied from the given error instance.
1315 error: function(type
, fmt
/*, ...*/) {
1317 L
.raise
.apply(L
, Array
.prototype.slice
.call(arguments
));
1322 L
.ui
.addNotification(e
.name
|| _('Runtime error'),
1323 E('pre', {}, e
.message
), 'danger');
1325 L
.dom
.content(document
.querySelector('#maincontent'),
1326 E('pre', { 'class': 'alert-message error' }, e
.message
));
1336 * Return a bound function using the given `self` as `this` context
1337 * and any further arguments as parameters to the bound function.
1342 * @param {function} fn
1343 * The function to bind.
1346 * The value to bind as `this` context to the specified function.
1348 * @param {...*} [args]
1349 * Zero or more variable arguments which are bound to the function
1352 * @returns {function}
1353 * Returns the bound function.
1355 bind: function(fn
, self
/*, ... */) {
1356 return Function
.prototype.bind
.apply(fn
, this.varargs(arguments
, 2, self
));
1360 * Load an additional LuCI JavaScript class and its dependencies,
1361 * instantiate it and return the resulting class instance. Each
1362 * class is only loaded once. Subsequent attempts to load the same
1363 * class will return the already instantiated class.
1368 * @param {string} name
1369 * The name of the class to load in dotted notation. Dots will
1370 * be replaced by spaces and joined with the runtime-determined
1371 * base URL of LuCI.js to form an absolute URL to load the class
1374 * @throws {DependencyError}
1375 * Throws a `DependencyError` when the class to load includes
1376 * circular dependencies.
1378 * @throws {NetworkError}
1379 * Throws `NetworkError` when the underlying {@link LuCI.Request}
1382 * @throws {SyntaxError}
1383 * Throws `SyntaxError` when the loaded class file code cannot
1384 * be interpreted by `eval`.
1386 * @throws {TypeError}
1387 * Throws `TypeError` when the class file could be loaded and
1388 * interpreted, but when invoking its code did not yield a valid
1391 * @returns {Promise<LuCI#Class>}
1392 * Returns the instantiated class.
1394 require: function(name
, from) {
1395 var L
= this, url
= null, from = from || [];
1397 /* Class already loaded */
1398 if (classes
[name
] != null) {
1399 /* Circular dependency */
1400 if (from.indexOf(name
) != -1)
1401 L
.raise('DependencyError',
1402 'Circular dependency: class "%s" depends on "%s"',
1403 name
, from.join('" which depends on "'));
1405 return Promise
.resolve(classes
[name
]);
1408 url
= '%s/%s.js%s'.format(L
.env
.base_url
, name
.replace(/\./g, '/'), (L.env.resource_version ? '?v
=' + L.env.resource_version : ''));
1409 from = [ name ].concat(from);
1411 var compileClass = function(res) {
1413 L.raise('NetworkError
',
1414 'HTTP error
%d
while loading
class file
"%s"', res.status, url);
1416 var source = res.text(),
1417 requirematch = /^require[ \t]+(\S+)(?:[ \t]+as[ \t]+([a-zA-Z_]\S*))?$/,
1418 strictmatch = /^use[ \t]+strict$/,
1422 /* find require statements in source */
1423 for (var i = 0, off = -1, quote = -1, esc = false; i < source.length; i++) {
1424 var chr = source.charCodeAt(i);
1429 else if (chr == 92) {
1432 else if (chr == quote) {
1433 var s = source.substring(off, i),
1434 m = requirematch.exec(s);
1437 var dep = m[1], as = m[2] || dep.replace(/[^a-zA-Z0-9_]/g, '_
');
1438 depends.push(L.require(dep, from));
1441 else if (!strictmatch.exec(s)) {
1448 else if (quote == -1 && (chr == 34 || chr == 39)) {
1454 /* load dependencies and instantiate class */
1455 return Promise.all(depends).then(function(instances) {
1456 var _factory, _class;
1460 '(function(window
, document
, L
%s
) { %s
})\n\n//# sourceURL=%s\n'
1461 .format(args
, source
, res
.url
));
1464 L
.raise('SyntaxError', '%s\n in %s:%s',
1465 error
.message
, res
.url
, error
.lineNumber
|| '?');
1468 _factory
.displayName
= toCamelCase(name
+ 'ClassFactory');
1469 _class
= _factory
.apply(_factory
, [window
, document
, L
].concat(instances
));
1471 if (!Class
.isSubclass(_class
))
1472 L
.error('TypeError', '"%s" factory yields invalid constructor', name
);
1474 if (_class
.displayName
== 'AnonymousClass')
1475 _class
.displayName
= toCamelCase(name
+ 'Class');
1477 var ptr
= Object
.getPrototypeOf(L
),
1478 parts
= name
.split(/\./),
1479 instance
= new _class();
1481 for (var i
= 0; ptr
&& i
< parts
.length
- 1; i
++)
1482 ptr
= ptr
[parts
[i
]];
1485 ptr
[parts
[i
]] = instance
;
1487 classes
[name
] = instance
;
1493 /* Request class file */
1494 classes
[name
] = Request
.get(url
, { cache
: true }).then(compileClass
);
1496 return classes
[name
];
1500 probeRPCBaseURL: function() {
1501 if (rpcBaseURL
== null) {
1503 rpcBaseURL
= window
.sessionStorage
.getItem('rpcBaseURL');
1508 if (rpcBaseURL
== null) {
1509 var rpcFallbackURL
= this.url('admin/ubus');
1511 rpcBaseURL
= Request
.get('/ubus/').then(function(res
) {
1512 return (rpcBaseURL
= (res
.status
== 400) ? '/ubus/' : rpcFallbackURL
);
1514 return (rpcBaseURL
= rpcFallbackURL
);
1515 }).then(function(url
) {
1517 window
.sessionStorage
.setItem('rpcBaseURL', url
);
1525 return Promise
.resolve(rpcBaseURL
);
1528 probeSystemFeatures: function() {
1529 var sessionid
= classes
.rpc
.getSessionID();
1531 if (sysFeatures
== null) {
1533 var data
= JSON
.parse(window
.sessionStorage
.getItem('sysFeatures'));
1535 if (this.isObject(data
) && this.isObject(data
[sessionid
]))
1536 sysFeatures
= data
[sessionid
];
1541 if (!this.isObject(sysFeatures
)) {
1542 sysFeatures
= classes
.rpc
.declare({
1544 method
: 'getFeatures',
1546 })().then(function(features
) {
1549 data
[sessionid
] = features
;
1551 window
.sessionStorage
.setItem('sysFeatures', JSON
.stringify(data
));
1555 sysFeatures
= features
;
1561 return Promise
.resolve(sysFeatures
);
1565 * Test whether a particular system feature is available, such as
1566 * hostapd SAE support or an installed firewall. The features are
1567 * queried once at the beginning of the LuCI session and cached in
1568 * `SessionStorage` throughout the lifetime of the associated tab or
1574 * @param {string} feature
1575 * The feature to test. For detailed list of known feature flags,
1576 * see `/modules/luci-base/root/usr/libexec/rpcd/luci`.
1578 * @param {string} [subfeature]
1579 * Some feature classes like `hostapd` provide sub-feature flags,
1580 * such as `sae` or `11w` support. The `subfeature` argument can
1581 * be used to query these.
1583 * @return {boolean|null}
1584 * Return `true` if the queried feature (and sub-feature) is available
1585 * or `false` if the requested feature isn't present or known.
1586 * Return `null` when a sub-feature was queried for a feature which
1587 * has no sub-features.
1589 hasSystemFeature: function() {
1590 var ft
= sysFeatures
[arguments
[0]];
1592 if (arguments
.length
== 2)
1593 return this.isObject(ft
) ? ft
[arguments
[1]] : null;
1595 return (ft
!= null && ft
!= false);
1599 notifySessionExpiry: function() {
1602 L
.ui
.showModal(_('Session expired'), [
1603 E('div', { class: 'alert-message warning' },
1604 _('A new login is required since the authentication session expired.')),
1605 E('div', { class: 'right' },
1607 class: 'btn primary',
1609 var loc
= window
.location
;
1610 window
.location
= loc
.protocol
+ '//' + loc
.host
+ loc
.pathname
+ loc
.search
;
1612 }, _('To login…')))
1615 L
.raise('SessionError', 'Login session is expired');
1619 setupDOM: function(res
) {
1624 rpcBaseURL
= res
[4];
1626 rpcClass
.setBaseURL(rpcBaseURL
);
1628 rpcClass
.addInterceptor(function(msg
, req
) {
1629 if (!L
.isObject(msg
) || !L
.isObject(msg
.error
) || msg
.error
.code
!= -32002)
1632 if (!L
.isObject(req
) || (req
.object
== 'session' && req
.method
== 'access'))
1635 return rpcClass
.declare({
1636 'object': 'session',
1638 'params': [ 'scope', 'object', 'function' ],
1639 'expect': { access
: true }
1640 })('uci', 'luci', 'read').catch(L
.notifySessionExpiry
);
1643 Request
.addInterceptor(function(res
) {
1644 var isDenied
= false;
1646 if (res
.status
== 403 && res
.headers
.get('X-LuCI-Login-Required') == 'yes')
1652 L
.notifySessionExpiry();
1655 return this.probeSystemFeatures().finally(this.initDOM
);
1659 initDOM: function() {
1662 document
.dispatchEvent(new CustomEvent('luci-loaded'));
1666 * The `env` object holds environment settings used by LuCI, such
1667 * as request timeouts, base URLs etc.
1675 * Construct a relative URL path from the given prefix and parts.
1676 * The resulting URL is guaranteed to only contain the characters
1677 * `a-z`, `A-Z`, `0-9`, `_`, `.`, `%`, `,`, `;`, and `-` as well
1678 * as `/` for the path separator.
1683 * @param {string} [prefix]
1684 * The prefix to join the given parts with. If the `prefix` is
1685 * omitted, it defaults to an empty string.
1687 * @param {string[]} [parts]
1688 * An array of parts to join into an URL path. Parts may contain
1689 * slashes and any of the other characters mentioned above.
1692 * Return the joined URL path.
1694 path: function(prefix
, parts
) {
1695 var url
= [ prefix
|| '' ];
1697 for (var i
= 0; i
< parts
.length
; i
++)
1698 if (/^(?:[a-zA-Z0-9_.%,;-]+\/)*[a-zA-Z0-9_.%,;-]+$/.test(parts
[i
]))
1699 url
.push('/', parts
[i
]);
1701 if (url
.length
=== 1)
1704 return url
.join('');
1708 * Construct an URL pathrelative to the script path of the server
1709 * side LuCI application (usually `/cgi-bin/luci`).
1711 * The resulting URL is guaranteed to only contain the characters
1712 * `a-z`, `A-Z`, `0-9`, `_`, `.`, `%`, `,`, `;`, and `-` as well
1713 * as `/` for the path separator.
1718 * @param {string[]} [parts]
1719 * An array of parts to join into an URL path. Parts may contain
1720 * slashes and any of the other characters mentioned above.
1723 * Returns the resulting URL path.
1726 return this.path(this.env
.scriptname
, arguments
);
1730 * Construct an URL path relative to the global static resource path
1731 * of the LuCI ui (usually `/luci-static/resources`).
1733 * The resulting URL is guaranteed to only contain the characters
1734 * `a-z`, `A-Z`, `0-9`, `_`, `.`, `%`, `,`, `;`, and `-` as well
1735 * as `/` for the path separator.
1740 * @param {string[]} [parts]
1741 * An array of parts to join into an URL path. Parts may contain
1742 * slashes and any of the other characters mentioned above.
1745 * Returns the resulting URL path.
1747 resource: function() {
1748 return this.path(this.env
.resource
, arguments
);
1752 * Return the complete URL path to the current view.
1758 * Returns the URL path to the current view.
1760 location: function() {
1761 return this.path(this.env
.scriptname
, this.env
.requestpath
);
1766 * Tests whether the passed argument is a JavaScript object.
1767 * This function is meant to be an object counterpart to the
1768 * standard `Array.isArray()` function.
1777 * Returns `true` if the given value is of type object and
1778 * not `null`, else returns `false`.
1780 isObject: function(val
) {
1781 return (val
!= null && typeof(val
) == 'object');
1785 * Return an array of sorted object keys, optionally sorted by
1786 * a different key or a different sorting mode.
1791 * @param {object} obj
1792 * The object to extract the keys from. If the given value is
1793 * not an object, the function will return an empty array.
1795 * @param {string} [key]
1796 * Specifies the key to order by. This is mainly useful for
1797 * nested objects of objects or objects of arrays when sorting
1798 * shall not be performed by the primary object keys but by
1799 * some other key pointing to a value within the nested values.
1801 * @param {string} [sortmode]
1802 * May be either `addr` or `num` to override the natural
1803 * lexicographic sorting with a sorting suitable for IP/MAC style
1804 * addresses or numeric values respectively.
1806 * @return {string[]}
1807 * Returns an array containing the sorted keys of the given object.
1809 sortedKeys: function(obj
, key
, sortmode
) {
1810 if (obj
== null || typeof(obj
) != 'object')
1813 return Object
.keys(obj
).map(function(e
) {
1814 var v
= (key
!= null) ? obj
[e
][key
] : e
;
1818 v
= (v
!= null) ? v
.replace(/(?:^|[.:])([0-9a-fA-F]{1,4})/g,
1819 function(m0
, m1
) { return ('000' + m1
.toLowerCase()).substr(-4) }) : null;
1823 v
= (v
!= null) ? +v
: null;
1828 }).filter(function(e
) {
1829 return (e
[1] != null);
1830 }).sort(function(a
, b
) {
1831 return (a
[1] > b
[1]);
1832 }).map(function(e
) {
1838 * Converts the given value to an array. If the given value is of
1839 * type array, it is returned as-is, values of type object are
1840 * returned as one-element array containing the object, empty
1841 * strings and `null` values are returned as empty array, all other
1842 * values are converted using `String()`, trimmed, split on white
1843 * space and returned as array.
1849 * The value to convert into an array.
1851 * @return {Array<*>}
1852 * Returns the resulting array.
1854 toArray: function(val
) {
1857 else if (Array
.isArray(val
))
1859 else if (typeof(val
) == 'object')
1862 var s
= String(val
).trim();
1867 return s
.split(/\s+/);
1871 * Returns a promise resolving with either the given value or or with
1872 * the given default in case the input value is a rejecting promise.
1878 * The value to resolve the promise with.
1880 * @param {*} defvalue
1881 * The default value to resolve the promise with in case the given
1882 * input value is a rejecting promise.
1884 * @returns {Promise<*>}
1885 * Returns a new promise resolving either to the given input value or
1886 * to the given default value on error.
1888 resolveDefault: function(value
, defvalue
) {
1889 return Promise
.resolve(value
).catch(function() { return defvalue
});
1893 * The request callback function is invoked whenever an HTTP
1894 * reply to a request made using the `L.get()`, `L.post()` or
1895 * `L.poll()` function is timed out or received successfully.
1900 * @callback LuCI.requestCallbackFn
1901 * @param {XMLHTTPRequest} xhr
1902 * The XMLHTTPRequest instance used to make the request.
1905 * The response JSON if the response could be parsed as such,
1908 * @param {number} duration
1909 * The total duration of the request in milliseconds.
1913 * Issues a GET request to the given url and invokes the specified
1914 * callback function. The function is a wrapper around
1915 * {@link LuCI.Request#request Request.request()}.
1921 * @param {string} url
1922 * The URL to request.
1924 * @param {Object<string, string>} [args]
1925 * Additional query string arguments to append to the URL.
1927 * @param {LuCI.requestCallbackFn} cb
1928 * The callback function to invoke when the request finishes.
1930 * @return {Promise<null>}
1931 * Returns a promise resolving to `null` when concluded.
1933 get: function(url
, args
, cb
) {
1934 return this.poll(null, url
, args
, cb
, false);
1938 * Issues a POST request to the given url and invokes the specified
1939 * callback function. The function is a wrapper around
1940 * {@link LuCI.Request#request Request.request()}. The request is
1941 * sent using `application/x-www-form-urlencoded` encoding and will
1942 * contain a field `token` with the current value of `LuCI.env.token`
1949 * @param {string} url
1950 * The URL to request.
1952 * @param {Object<string, string>} [args]
1953 * Additional post arguments to append to the request body.
1955 * @param {LuCI.requestCallbackFn} cb
1956 * The callback function to invoke when the request finishes.
1958 * @return {Promise<null>}
1959 * Returns a promise resolving to `null` when concluded.
1961 post: function(url
, args
, cb
) {
1962 return this.poll(null, url
, args
, cb
, true);
1966 * Register a polling HTTP request that invokes the specified
1967 * callback function. The function is a wrapper around
1968 * {@link LuCI.Request.poll#add Request.poll.add()}.
1974 * @param {number} interval
1975 * The poll interval to use. If set to a value less than or equal
1976 * to `0`, it will default to the global poll interval configured
1977 * in `LuCI.env.pollinterval`.
1979 * @param {string} url
1980 * The URL to request.
1982 * @param {Object<string, string>} [args]
1983 * Specifies additional arguments for the request. For GET requests,
1984 * the arguments are appended to the URL as query string, for POST
1985 * requests, they'll be added to the request body.
1987 * @param {LuCI.requestCallbackFn} cb
1988 * The callback function to invoke whenever a request finishes.
1990 * @param {boolean} [post=false]
1991 * When set to `false` or not specified, poll requests will be made
1992 * using the GET method. When set to `true`, POST requests will be
1993 * issued. In case of POST requests, the request body will contain
1994 * an argument `token` with the current value of `LuCI.env.token` by
1995 * default, regardless of the parameters specified with `args`.
1997 * @return {function}
1998 * Returns the internally created function that has been passed to
1999 * {@link LuCI.Request.poll#add Request.poll.add()}. This value can
2000 * be passed to {@link LuCI.Poll.remove Poll.remove()} to remove the
2003 poll: function(interval
, url
, args
, cb
, post
) {
2004 if (interval
!== null && interval
<= 0)
2005 interval
= this.env
.pollinterval
;
2007 var data
= post
? { token
: this.env
.token
} : null,
2008 method
= post
? 'POST' : 'GET';
2010 if (!/^(?:\/|\S+:\/\/)/.test(url
))
2011 url
= this.url(url
);
2014 data
= Object
.assign(data
|| {}, args
);
2016 if (interval
!== null)
2017 return Request
.poll
.add(interval
, url
, { method
: method
, query
: data
}, cb
);
2019 return Request
.request(url
, { method
: method
, query
: data
})
2020 .then(function(res
) {
2022 if (/^application\/json\b/.test(res
.headers
.get('Content-Type')))
2023 try { json
= res
.json() } catch(e
) {}
2024 cb(res
.xhr
, json
, res
.duration
);
2029 * Deprecated wrapper around {@link LuCI.Poll.remove Poll.remove()}.
2035 * @param {function} entry
2036 * The polling function to remove.
2039 * Returns `true` when the function has been removed or `false` if
2040 * it could not be found.
2042 stop: function(entry
) { return Poll
.remove(entry
) },
2045 * Deprecated wrapper around {@link LuCI.Poll.stop Poll.stop()}.
2052 * Returns `true` when the polling loop has been stopped or `false`
2053 * when it didn't run to begin with.
2055 halt: function() { return Poll
.stop() },
2058 * Deprecated wrapper around {@link LuCI.Poll.start Poll.start()}.
2065 * Returns `true` when the polling loop has been started or `false`
2066 * when it was already running.
2068 run: function() { return Poll
.start() },
2077 * The `dom` class provides convenience method for creating and
2078 * manipulating DOM elements.
2080 dom
: Class
.singleton(/* @lends LuCI.dom.prototype */ {
2081 __name__
: 'LuCI.DOM',
2084 * Tests whether the given argument is a valid DOM `Node`.
2087 * @memberof LuCI.dom
2089 * The value to test.
2091 * @returns {boolean}
2092 * Returns `true` if the value is a DOM `Node`, else `false`.
2095 return (e
!= null && typeof(e
) == 'object' && 'nodeType' in e
);
2099 * Parses a given string as HTML and returns the first child node.
2102 * @memberof LuCI.dom
2104 * A string containing an HTML fragment to parse. Note that only
2105 * the first result of the resulting structure is returned, so an
2106 * input value of `<div>foo</div> <div>bar</div>` will only return
2107 * the first `div` element node.
2110 * Returns the first DOM `Node` extracted from the HTML fragment or
2111 * `null` on parsing failures or if no element could be found.
2113 parse: function(s
) {
2117 domParser
= domParser
|| new DOMParser();
2118 elem
= domParser
.parseFromString(s
, 'text/html').body
.firstChild
;
2124 dummyElem
= dummyElem
|| document
.createElement('div');
2125 dummyElem
.innerHTML
= s
;
2126 elem
= dummyElem
.firstChild
;
2131 return elem
|| null;
2135 * Tests whether a given `Node` matches the given query selector.
2137 * This function is a convenience wrapper around the standard
2138 * `Node.matches("selector")` function with the added benefit that
2139 * the `node` argument may be a non-`Node` value, in which case
2140 * this function simply returns `false`.
2143 * @memberof LuCI.dom
2145 * The `Node` argument to test the selector against.
2147 * @param {string} [selector]
2148 * The query selector expression to test against the given node.
2150 * @returns {boolean}
2151 * Returns `true` if the given node matches the specified selector
2152 * or `false` when the node argument is no valid DOM `Node` or the
2153 * selector didn't match.
2155 matches: function(node
, selector
) {
2156 var m
= this.elem(node
) ? node
.matches
|| node
.msMatchesSelector
: null;
2157 return m
? m
.call(node
, selector
) : false;
2161 * Returns the closest parent node that matches the given query
2162 * selector expression.
2164 * This function is a convenience wrapper around the standard
2165 * `Node.closest("selector")` function with the added benefit that
2166 * the `node` argument may be a non-`Node` value, in which case
2167 * this function simply returns `null`.
2170 * @memberof LuCI.dom
2172 * The `Node` argument to find the closest parent for.
2174 * @param {string} [selector]
2175 * The query selector expression to test against each parent.
2177 * @returns {Node|null}
2178 * Returns the closest parent node matching the selector or
2179 * `null` when the node argument is no valid DOM `Node` or the
2180 * selector didn't match any parent.
2182 parent: function(node
, selector
) {
2183 if (this.elem(node
) && node
.closest
)
2184 return node
.closest(selector
);
2186 while (this.elem(node
))
2187 if (this.matches(node
, selector
))
2190 node
= node
.parentNode
;
2196 * Appends the given children data to the given node.
2199 * @memberof LuCI.dom
2201 * The `Node` argument to append the children to.
2203 * @param {*} [children]
2204 * The childrens to append to the given node.
2206 * When `children` is an array, then each item of the array
2207 * will be either appended as child element or text node,
2208 * depending on whether the item is a DOM `Node` instance or
2209 * some other non-`null` value. Non-`Node`, non-`null` values
2210 * will be converted to strings first before being passed as
2211 * argument to `createTextNode()`.
2213 * When `children` is a function, it will be invoked with
2214 * the passed `node` argument as sole parameter and the `append`
2215 * function will be invoked again, with the given `node` argument
2216 * as first and the return value of the `children` function as
2219 * When `children` is is a DOM `Node` instance, it will be
2220 * appended to the given `node`.
2222 * When `children` is any other non-`null` value, it will be
2223 * converted to a string and appened to the `innerHTML` property
2224 * of the given `node`.
2226 * @returns {Node|null}
2227 * Returns the last children `Node` appended to the node or `null`
2228 * if either the `node` argument was no valid DOM `node` or if the
2229 * `children` was `null` or didn't result in further DOM nodes.
2231 append: function(node
, children
) {
2232 if (!this.elem(node
))
2235 if (Array
.isArray(children
)) {
2236 for (var i
= 0; i
< children
.length
; i
++)
2237 if (this.elem(children
[i
]))
2238 node
.appendChild(children
[i
]);
2239 else if (children
!== null && children
!== undefined)
2240 node
.appendChild(document
.createTextNode('' + children
[i
]));
2242 return node
.lastChild
;
2244 else if (typeof(children
) === 'function') {
2245 return this.append(node
, children(node
));
2247 else if (this.elem(children
)) {
2248 return node
.appendChild(children
);
2250 else if (children
!== null && children
!== undefined) {
2251 node
.innerHTML
= '' + children
;
2252 return node
.lastChild
;
2259 * Replaces the content of the given node with the given children.
2261 * This function first removes any children of the given DOM
2262 * `Node` and then adds the given given children following the
2263 * rules outlined below.
2266 * @memberof LuCI.dom
2268 * The `Node` argument to replace the children of.
2270 * @param {*} [children]
2271 * The childrens to replace into the given node.
2273 * When `children` is an array, then each item of the array
2274 * will be either appended as child element or text node,
2275 * depending on whether the item is a DOM `Node` instance or
2276 * some other non-`null` value. Non-`Node`, non-`null` values
2277 * will be converted to strings first before being passed as
2278 * argument to `createTextNode()`.
2280 * When `children` is a function, it will be invoked with
2281 * the passed `node` argument as sole parameter and the `append`
2282 * function will be invoked again, with the given `node` argument
2283 * as first and the return value of the `children` function as
2286 * When `children` is is a DOM `Node` instance, it will be
2287 * appended to the given `node`.
2289 * When `children` is any other non-`null` value, it will be
2290 * converted to a string and appened to the `innerHTML` property
2291 * of the given `node`.
2293 * @returns {Node|null}
2294 * Returns the last children `Node` appended to the node or `null`
2295 * if either the `node` argument was no valid DOM `node` or if the
2296 * `children` was `null` or didn't result in further DOM nodes.
2298 content: function(node
, children
) {
2299 if (!this.elem(node
))
2302 var dataNodes
= node
.querySelectorAll('[data-idref]');
2304 for (var i
= 0; i
< dataNodes
.length
; i
++)
2305 delete this.registry
[dataNodes
[i
].getAttribute('data-idref')];
2307 while (node
.firstChild
)
2308 node
.removeChild(node
.firstChild
);
2310 return this.append(node
, children
);
2314 * Sets attributes or registers event listeners on element nodes.
2317 * @memberof LuCI.dom
2319 * The `Node` argument to set the attributes or add the event
2320 * listeners for. When the given `node` value is not a valid
2321 * DOM `Node`, the function returns and does nothing.
2323 * @param {string|Object<string, *>} key
2324 * Specifies either the attribute or event handler name to use,
2325 * or an object containing multiple key, value pairs which are
2326 * each added to the node as either attribute or event handler,
2327 * depending on the respective value.
2330 * Specifies the attribute value or event handler function to add.
2331 * If the `key` parameter is an `Object`, this parameter will be
2334 * When `val` is of type function, it will be registered as event
2335 * handler on the given `node` with the `key` parameter being the
2338 * When `val` is of type object, it will be serialized as JSON and
2339 * added as attribute to the given `node`, using the given `key`
2340 * as attribute name.
2342 * When `val` is of any other type, it will be added as attribute
2343 * to the given `node` as-is, with the underlying `setAttribute()`
2344 * call implicitely turning it into a string.
2346 attr: function(node
, key
, val
) {
2347 if (!this.elem(node
))
2352 if (typeof(key
) === 'object' && key
!== null)
2354 else if (typeof(key
) === 'string')
2355 attr
= {}, attr
[key
] = val
;
2358 if (!attr
.hasOwnProperty(key
) || attr
[key
] == null)
2361 switch (typeof(attr
[key
])) {
2363 node
.addEventListener(key
, attr
[key
]);
2367 node
.setAttribute(key
, JSON
.stringify(attr
[key
]));
2371 node
.setAttribute(key
, attr
[key
]);
2377 * Creates a new DOM `Node` from the given `html`, `attr` and
2378 * `data` parameters.
2380 * This function has multiple signatures, it can be either invoked
2381 * in the form `create(html[, attr[, data]])` or in the form
2382 * `create(html[, data])`. The used variant is determined from the
2383 * type of the second argument.
2386 * @memberof LuCI.dom
2388 * Describes the node to create.
2390 * When the value of `html` is of type array, a `DocumentFragment`
2391 * node is created and each item of the array is first converted
2392 * to a DOM `Node` by passing it through `create()` and then added
2393 * as child to the fragment.
2395 * When the value of `html` is a DOM `Node` instance, no new
2396 * element will be created but the node will be used as-is.
2398 * When the value of `html` is a string starting with `<`, it will
2399 * be passed to `dom.parse()` and the resulting value is used.
2401 * When the value of `html` is any other string, it will be passed
2402 * to `document.createElement()` for creating a new DOM `Node` of
2405 * @param {Object<string, *>} [attr]
2406 * Specifies an Object of key, value pairs to set as attributes
2407 * or event handlers on the created node. Refer to
2408 * {@link LuCI.dom#attr dom.attr()} for details.
2411 * Specifies children to append to the newly created element.
2412 * Refer to {@link LuCI.dom#append dom.append()} for details.
2414 * @throws {InvalidCharacterError}
2415 * Throws an `InvalidCharacterError` when the given `html`
2416 * argument contained malformed markup (such as not escaped
2417 * `&` characters in XHTML mode) or when the given node name
2418 * in `html` contains characters which are not legal in DOM
2419 * element names, such as spaces.
2422 * Returns the newly created `Node`.
2424 create: function() {
2425 var html
= arguments
[0],
2426 attr
= arguments
[1],
2427 data
= arguments
[2],
2430 if (!(attr
instanceof Object
) || Array
.isArray(attr
))
2431 data
= attr
, attr
= null;
2433 if (Array
.isArray(html
)) {
2434 elem
= document
.createDocumentFragment();
2435 for (var i
= 0; i
< html
.length
; i
++)
2436 elem
.appendChild(this.create(html
[i
]));
2438 else if (this.elem(html
)) {
2441 else if (html
.charCodeAt(0) === 60) {
2442 elem
= this.parse(html
);
2445 elem
= document
.createElement(html
);
2451 this.attr(elem
, attr
);
2452 this.append(elem
, data
);
2460 * Attaches or detaches arbitrary data to and from a DOM `Node`.
2462 * This function is useful to attach non-string values or runtime
2463 * data that is not serializable to DOM nodes. To decouple data
2464 * from the DOM, values are not added directly to nodes, but
2465 * inserted into a registry instead which is then referenced by a
2466 * string key stored as `data-idref` attribute in the node.
2468 * This function has multiple signatures and is sensitive to the
2469 * number of arguments passed to it.
2471 * - `dom.data(node)` -
2472 * Fetches all data associated with the given node.
2473 * - `dom.data(node, key)` -
2474 * Fetches a specific key associated with the given node.
2475 * - `dom.data(node, key, val)` -
2476 * Sets a specific key to the given value associated with the
2478 * - `dom.data(node, null)` -
2479 * Clears any data associated with the node.
2480 * - `dom.data(node, key, null)` -
2481 * Clears the given key associated with the node.
2484 * @memberof LuCI.dom
2485 * @param {Node} node
2486 * The DOM `Node` instance to set or retrieve the data for.
2488 * @param {string|null} [key]
2489 * This is either a string specifying the key to retrieve, or
2490 * `null` to unset the entire node data.
2492 * @param {*|null} [val]
2493 * This is either a non-`null` value to set for a given key or
2494 * `null` to remove the given `key` from the specified node.
2497 * Returns the get or set value, or `null` when no value could
2500 data: function(node
, key
, val
) {
2501 var id
= node
.getAttribute('data-idref');
2503 /* clear all data */
2504 if (arguments
.length
> 1 && key
== null) {
2506 node
.removeAttribute('data-idref');
2507 val
= this.registry
[id
]
2508 delete this.registry
[id
];
2516 else if (arguments
.length
> 2 && key
!= null && val
== null) {
2518 val
= this.registry
[id
][key
];
2519 delete this.registry
[id
][key
];
2527 else if (arguments
.length
> 2 && key
!= null && val
!= null) {
2529 do { id
= Math
.floor(Math
.random() * 0xffffffff).toString(16) }
2530 while (this.registry
.hasOwnProperty(id
));
2532 node
.setAttribute('data-idref', id
);
2533 this.registry
[id
] = {};
2536 return (this.registry
[id
][key
] = val
);
2540 else if (arguments
.length
== 1) {
2542 return this.registry
[id
];
2548 else if (arguments
.length
== 2) {
2550 return this.registry
[id
][key
];
2557 * Binds the given class instance ot the specified DOM `Node`.
2559 * This function uses the `dom.data()` facility to attach the
2560 * passed instance of a Class to a node. This is needed for
2561 * complex widget elements or similar where the corresponding
2562 * class instance responsible for the element must be retrieved
2563 * from DOM nodes obtained by `querySelector()` or similar means.
2566 * @memberof LuCI.dom
2567 * @param {Node} node
2568 * The DOM `Node` instance to bind the class to.
2570 * @param {Class} inst
2571 * The Class instance to bind to the node.
2573 * @throws {TypeError}
2574 * Throws a `TypeError` when the given instance argument isn't
2575 * a valid Class instance.
2578 * Returns the bound class instance.
2580 bindClassInstance: function(node
, inst
) {
2581 if (!(inst
instanceof Class
))
2582 L
.error('TypeError', 'Argument must be a class instance');
2584 return this.data(node
, '_class', inst
);
2588 * Finds a bound class instance on the given node itself or the
2589 * first bound instance on its closest parent node.
2592 * @memberof LuCI.dom
2593 * @param {Node} node
2594 * The DOM `Node` instance to start from.
2596 * @returns {Class|null}
2597 * Returns the founds class instance if any or `null` if no bound
2598 * class could be found on the node itself or any of its parents.
2600 findClassInstance: function(node
) {
2604 inst
= this.data(node
, '_class');
2605 node
= node
.parentNode
;
2607 while (!(inst
instanceof Class
) && node
!= null);
2613 * Finds a bound class instance on the given node itself or the
2614 * first bound instance on its closest parent node and invokes
2615 * the specified method name on the found class instance.
2618 * @memberof LuCI.dom
2619 * @param {Node} node
2620 * The DOM `Node` instance to start from.
2622 * @param {string} method
2623 * The name of the method to invoke on the found class instance.
2625 * @param {...*} params
2626 * Additional arguments to pass to the invoked method as-is.
2629 * Returns the return value of the invoked method if a class
2630 * instance and method has been found. Returns `null` if either
2631 * no bound class instance could be found, or if the found
2632 * instance didn't have the requested `method`.
2634 callClassMethod: function(node
, method
/*, ... */) {
2635 var inst
= this.findClassInstance(node
);
2637 if (inst
== null || typeof(inst
[method
]) != 'function')
2640 return inst
[method
].apply(inst
, inst
.varargs(arguments
, 2));
2644 * The ignore callback function is invoked by `isEmpty()` for each
2645 * child node to decide whether to ignore a child node or not.
2647 * When this function returns `false`, the node passed to it is
2648 * ignored, else not.
2650 * @callback LuCI.dom~ignoreCallbackFn
2651 * @param {Node} node
2652 * The child node to test.
2654 * @returns {boolean}
2655 * Boolean indicating whether to ignore the node or not.
2659 * Tests whether a given DOM `Node` instance is empty or appears
2662 * Any element child nodes which have the CSS class `hidden` set
2663 * or for which the optionally passed `ignoreFn` callback function
2664 * returns `false` are ignored.
2667 * @memberof LuCI.dom
2668 * @param {Node} node
2669 * The DOM `Node` instance to test.
2671 * @param {LuCI.dom~ignoreCallbackFn} [ignoreFn]
2672 * Specifies an optional function which is invoked for each child
2673 * node to decide whether the child node should be ignored or not.
2675 * @returns {boolean}
2676 * Returns `true` if the node does not have any children or if
2677 * any children node either has a `hidden` CSS class or a `false`
2678 * result when testing it using the given `ignoreFn`.
2680 isEmpty: function(node
, ignoreFn
) {
2681 for (var child
= node
.firstElementChild
; child
!= null; child
= child
.nextElementSibling
)
2682 if (!child
.classList
.contains('hidden') && (!ignoreFn
|| !ignoreFn(child
)))
2699 * The `view` class forms the basis of views and provides a standard
2700 * set of methods to inherit from.
2702 view
: Class
.extend(/* @lends LuCI.view.prototype */ {
2703 __name__
: 'LuCI.View',
2705 __init__: function() {
2706 var vp
= document
.getElementById('view');
2708 L
.dom
.content(vp
, E('div', { 'class': 'spinning' }, _('Loading view…')));
2710 return Promise
.resolve(this.load())
2711 .then(L
.bind(this.render
, this))
2712 .then(L
.bind(function(nodes
) {
2713 var vp
= document
.getElementById('view');
2715 L
.dom
.content(vp
, nodes
);
2716 L
.dom
.append(vp
, this.addFooter());
2717 }, this)).catch(L
.error
);
2721 * The load function is invoked before the view is rendered.
2723 * The invocation of this function is wrapped by
2724 * `Promise.resolve()` so it may return Promises if needed.
2726 * The return value of the function (or the resolved values
2727 * of the promise returned by it) will be passed as first
2728 * argument to `render()`.
2730 * This function is supposed to be overwritten by subclasses,
2731 * the default implementation does nothing.
2735 * @memberof LuCI.view
2737 * @returns {*|Promise<*>}
2738 * May return any value or a Promise resolving to any value.
2740 load: function() {},
2743 * The render function is invoked after the
2744 * {@link LuCI.view#load load()} function and responsible
2745 * for setting up the view contents. It must return a DOM
2746 * `Node` or `DocumentFragment` holding the contents to
2747 * insert into the view area.
2749 * The invocation of this function is wrapped by
2750 * `Promise.resolve()` so it may return Promises if needed.
2752 * The return value of the function (or the resolved values
2753 * of the promise returned by it) will be inserted into the
2754 * main content area using
2755 * {@link LuCI.dom#append dom.append()}.
2757 * This function is supposed to be overwritten by subclasses,
2758 * the default implementation does nothing.
2762 * @memberof LuCI.view
2763 * @param {*|null} load_results
2764 * This function will receive the return value of the
2765 * {@link LuCI.view#load view.load()} function as first
2768 * @returns {Node|Promise<Node>}
2769 * Should return a DOM `Node` value or a `Promise` resolving
2770 * to a `Node` value.
2772 render: function() {},
2775 * The handleSave function is invoked when the user clicks
2776 * the `Save` button in the page action footer.
2778 * The default implementation should be sufficient for most
2779 * views using {@link form#Map form.Map()} based forms - it
2780 * will iterate all forms present in the view and invoke
2781 * the {@link form#Map#save Map.save()} method on each form.
2783 * Views not using `Map` instances or requiring other special
2784 * logic should overwrite `handleSave()` with a custom
2787 * To disable the `Save` page footer button, views extending
2788 * this base class should overwrite the `handleSave` function
2791 * The invocation of this function is wrapped by
2792 * `Promise.resolve()` so it may return Promises if needed.
2795 * @memberof LuCI.view
2797 * The DOM event that triggered the function.
2799 * @returns {*|Promise<*>}
2800 * Any return values of this function are discarded, but
2801 * passed through `Promise.resolve()` to ensure that any
2802 * returned promise runs to completion before the button
2805 handleSave: function(ev
) {
2808 document
.getElementById('maincontent')
2809 .querySelectorAll('.cbi-map').forEach(function(map
) {
2810 tasks
.push(L
.dom
.callClassMethod(map
, 'save'));
2813 return Promise
.all(tasks
);
2817 * The handleSaveApply function is invoked when the user clicks
2818 * the `Save & Apply` button in the page action footer.
2820 * The default implementation should be sufficient for most
2821 * views using {@link form#Map form.Map()} based forms - it
2823 * {@link LuCI.view.handleSave view.handleSave()} and then
2824 * call {@link ui#changes#apply ui.changes.apply()} to start the
2825 * modal config apply and page reload flow.
2827 * Views not using `Map` instances or requiring other special
2828 * logic should overwrite `handleSaveApply()` with a custom
2831 * To disable the `Save & Apply` page footer button, views
2832 * extending this base class should overwrite the
2833 * `handleSaveApply` function with `null`.
2835 * The invocation of this function is wrapped by
2836 * `Promise.resolve()` so it may return Promises if needed.
2839 * @memberof LuCI.view
2841 * The DOM event that triggered the function.
2843 * @returns {*|Promise<*>}
2844 * Any return values of this function are discarded, but
2845 * passed through `Promise.resolve()` to ensure that any
2846 * returned promise runs to completion before the button
2849 handleSaveApply: function(ev
, mode
) {
2850 return this.handleSave(ev
).then(function() {
2851 L
.ui
.changes
.apply(mode
== '0');
2856 * The handleReset function is invoked when the user clicks
2857 * the `Reset` button in the page action footer.
2859 * The default implementation should be sufficient for most
2860 * views using {@link form#Map form.Map()} based forms - it
2861 * will iterate all forms present in the view and invoke
2862 * the {@link form#Map#save Map.reset()} method on each form.
2864 * Views not using `Map` instances or requiring other special
2865 * logic should overwrite `handleReset()` with a custom
2868 * To disable the `Reset` page footer button, views extending
2869 * this base class should overwrite the `handleReset` function
2872 * The invocation of this function is wrapped by
2873 * `Promise.resolve()` so it may return Promises if needed.
2876 * @memberof LuCI.view
2878 * The DOM event that triggered the function.
2880 * @returns {*|Promise<*>}
2881 * Any return values of this function are discarded, but
2882 * passed through `Promise.resolve()` to ensure that any
2883 * returned promise runs to completion before the button
2886 handleReset: function(ev
) {
2889 document
.getElementById('maincontent')
2890 .querySelectorAll('.cbi-map').forEach(function(map
) {
2891 tasks
.push(L
.dom
.callClassMethod(map
, 'reset'));
2894 return Promise
.all(tasks
);
2898 * Renders a standard page action footer if any of the
2899 * `handleSave()`, `handleSaveApply()` or `handleReset()`
2900 * functions are defined.
2902 * The default implementation should be sufficient for most
2903 * views - it will render a standard page footer with action
2904 * buttons labeled `Save`, `Save & Apply` and `Reset`
2905 * triggering the `handleSave()`, `handleSaveApply()` and
2906 * `handleReset()` functions respectively.
2908 * When any of these `handle*()` functions is overwritten
2909 * with `null` by a view extending this class, the
2910 * corresponding button will not be rendered.
2913 * @memberof LuCI.view
2914 * @returns {DocumentFragment}
2915 * Returns a `DocumentFragment` containing the footer bar
2916 * with buttons for each corresponding `handle*()` action
2917 * or an empty `DocumentFragment` if all three `handle*()`
2918 * methods are overwritten with `null`.
2920 addFooter: function() {
2923 var saveApplyBtn
= this.handleSaveApply
? new L
.ui
.ComboButton('0', {
2924 0: [ _('Save & Apply') ],
2925 1: [ _('Apply unchecked') ]
2928 0: 'cbi-button cbi-button-apply important',
2929 1: 'cbi-button cbi-button-negative important'
2931 click
: L
.ui
.createHandlerFn(this, 'handleSaveApply')
2932 }).render() : E([]);
2934 if (this.handleSaveApply
|| this.handleSave
|| this.handleReset
) {
2935 footer
.appendChild(E('div', { 'class': 'cbi-page-actions' }, [
2937 this.handleSave
? E('button', {
2938 'class': 'cbi-button cbi-button-save',
2939 'click': L
.ui
.createHandlerFn(this, 'handleSave')
2940 }, [ _('Save') ]) : '', ' ',
2941 this.handleReset
? E('button', {
2942 'class': 'cbi-button cbi-button-reset',
2943 'click': L
.ui
.createHandlerFn(this, 'handleReset')
2944 }, [ _('Reset') ]) : ''
2959 * The `LuCI.XHR` class is a legacy compatibility shim for the
2960 * functionality formerly provided by `xhr.js`. It is registered as global
2961 * `window.XHR` symbol for compatibility with legacy code.
2963 * New code should use {@link LuCI.Request} instead to implement HTTP
2966 var XHR
= Class
.extend(/** @lends LuCI.XHR.prototype */ {
2967 __name__
: 'LuCI.XHR',
2968 __init__: function() {
2969 if (window
.console
&& console
.debug
)
2970 console
.debug('Direct use XHR() is deprecated, please use L.Request instead');
2973 _response: function(cb
, res
, json
, duration
) {
2975 cb(res
, json
, duration
);
2980 * This function is a legacy wrapper around
2981 * {@link LuCI#get LuCI.get()}.
2985 * @memberof LuCI.XHR
2987 * @param {string} url
2988 * The URL to request
2990 * @param {Object} [data]
2991 * Additional query string data
2993 * @param {LuCI.requestCallbackFn} [callback]
2994 * Callback function to invoke on completion
2996 * @param {number} [timeout]
2997 * Request timeout to use
2999 * @return {Promise<null>}
3001 get: function(url
, data
, callback
, timeout
) {
3003 L
.get(url
, data
, this._response
.bind(this, callback
), timeout
);
3007 * This function is a legacy wrapper around
3008 * {@link LuCI#post LuCI.post()}.
3012 * @memberof LuCI.XHR
3014 * @param {string} url
3015 * The URL to request
3017 * @param {Object} [data]
3018 * Additional data to append to the request body.
3020 * @param {LuCI.requestCallbackFn} [callback]
3021 * Callback function to invoke on completion
3023 * @param {number} [timeout]
3024 * Request timeout to use
3026 * @return {Promise<null>}
3028 post: function(url
, data
, callback
, timeout
) {
3030 L
.post(url
, data
, this._response
.bind(this, callback
), timeout
);
3034 * Cancels a running request.
3036 * This function does not actually cancel the underlying
3037 * `XMLHTTPRequest` request but it sets a flag which prevents the
3038 * invocation of the callback function when the request eventually
3039 * finishes or timed out.
3043 * @memberof LuCI.XHR
3045 cancel: function() { delete this.active
},
3048 * Checks the running state of the request.
3052 * @memberof LuCI.XHR
3054 * @returns {boolean}
3055 * Returns `true` if the request is still running or `false` if it
3056 * already completed.
3058 busy: function() { return (this.active
=== true) },
3061 * Ignored for backwards compatibility.
3063 * This function does nothing.
3067 * @memberof LuCI.XHR
3069 abort: function() {},
3072 * Existing for backwards compatibility.
3074 * This function simply throws an `InternalError` when invoked.
3078 * @memberof LuCI.XHR
3080 * @throws {InternalError}
3081 * Throws an `InternalError` with the message `Not implemented`
3084 send_form: function() { L
.error('InternalError', 'Not implemented') },
3087 XHR
.get = function() { return window
.L
.get.apply(window
.L
, arguments
) };
3088 XHR
.post = function() { return window
.L
.post
.apply(window
.L
, arguments
) };
3089 XHR
.poll = function() { return window
.L
.poll
.apply(window
.L
, arguments
) };
3090 XHR
.stop
= Request
.poll
.remove
.bind(Request
.poll
);
3091 XHR
.halt
= Request
.poll
.stop
.bind(Request
.poll
);
3092 XHR
.run
= Request
.poll
.start
.bind(Request
.poll
);
3093 XHR
.running
= Request
.poll
.active
.bind(Request
.poll
);
3097 })(window
, document
);