11 * The `LuCI.uci` class utilizes {@link LuCI.rpc} to declare low level
12 * remote UCI `ubus` procedures and implements a local caching and data
13 * manipulation layer on top to allow for synchroneous operations on
14 * UCI configuration data.
16 return baseclass.extend(/** @lends LuCI.uci.prototype */ {
17 __init__: function() {
30 callLoad: rpc.declare({
34 expect: { values: { } }
38 callOrder: rpc.declare({
41 params: [ 'config', 'sections' ]
44 callAdd: rpc.declare({
47 params: [ 'config', 'type', 'name', 'values' ],
48 expect: { section: '' }
51 callSet: rpc.declare({
54 params: [ 'config', 'section', 'values' ]
57 callDelete: rpc.declare({
60 params: [ 'config', 'section', 'options' ]
63 callApply: rpc.declare({
66 params: [ 'timeout', 'rollback' ]
69 callConfirm: rpc.declare({
76 * Generates a new, unique section ID for the given configuration.
78 * Note that the generated ID is temporary, it will get replaced by an
79 * identifier in the form `cfgXXXXXX` once the configuration is saved
80 * by the remote `ubus` UCI api.
82 * @param {string} config
83 * The configuration to generate the new section ID for.
86 * A newly generated, unique section ID in the form `newXXXXXX`
87 * where `X` denotes a hexadecimal digit.
89 createSID: function(conf) {
90 var v = this.state.values,
91 n = this.state.creates,
95 sid = "new%06x".format(Math.random() * 0xFFFFFF);
96 } while ((n[conf] && n[conf][sid]) || (v[conf] && v[conf][sid]));
102 * Resolves a given section ID in extended notation to the internal
105 * @param {string} config
106 * The configuration to resolve the section ID for.
108 * @param {string} sid
109 * The section ID to resolve. If the ID is in the form `@typename[#]`,
110 * it will get resolved to an internal anonymous ID in the forms
111 * `cfgXXXXXX`/`newXXXXXX` or to the name of a section in case it points
112 * to a named section. When the given ID is not in extended notation,
113 * it will be returned as-is.
115 * @returns {string|null}
116 * Returns the resolved section ID or the original given ID if it was
117 * not in extended notation. Returns `null` when an extended ID could
118 * not be resolved to existing section ID.
120 resolveSID: function(conf, sid) {
121 if (typeof(sid) != 'string')
124 var m = /^@([a-zA-Z0-9_-]+)\[(-?[0-9]+)\]$/.exec(sid);
129 sections = this.sections(conf, type),
130 section = sections[pos >= 0 ? pos : sections.length + pos];
132 return section ? section['.name'] : null;
139 reorderSections: function() {
140 var v = this.state.values,
141 n = this.state.creates,
142 r = this.state.reorder,
145 if (Object.keys(r).length === 0)
146 return Promise.resolve();
149 gather all created and existing sections, sort them according
150 to their index value and issue an uci order call
163 o.sort(function(a, b) {
164 return (a['.index'] - b['.index']);
169 for (var i = 0; i < o.length; i++)
170 sids.push(o[i]['.name']);
172 tasks.push(this.callOrder(c, sids));
176 this.state.reorder = { };
177 return Promise.all(tasks);
181 loadPackage: function(packageName) {
182 if (this.loaded[packageName] == null)
183 return (this.loaded[packageName] = this.callLoad(packageName));
185 return Promise.resolve(this.loaded[packageName]);
189 * Loads the given UCI configurations from the remote `ubus` api.
191 * Loaded configurations are cached and only loaded once. Subsequent
192 * load operations of the same configurations will return the cached
195 * To force reloading a configuration, it has to be unloaded with
196 * {@link LuCI.uci#unload uci.unload()} first.
198 * @param {string|string[]} config
199 * The name of the configuration or an array of configuration
202 * @returns {Promise<string[]>}
203 * Returns a promise resolving to the names of the configurations
204 * that have been successfully loaded.
206 load: function(packages) {
211 if (!Array.isArray(packages))
212 packages = [ packages ];
214 for (var i = 0; i < packages.length; i++)
215 if (!self.state.values[packages[i]]) {
216 pkgs.push(packages[i]);
217 tasks.push(self.loadPackage(packages[i]));
220 return Promise.all(tasks).then(function(responses) {
221 for (var i = 0; i < responses.length; i++)
222 self.state.values[pkgs[i]] = responses[i];
224 if (responses.length)
225 document.dispatchEvent(new CustomEvent('uci-loaded'));
232 * Unloads the given UCI configurations from the local cache.
234 * @param {string|string[]} config
235 * The name of the configuration or an array of configuration
238 unload: function(packages) {
239 if (!Array.isArray(packages))
240 packages = [ packages ];
242 for (var i = 0; i < packages.length; i++) {
243 delete this.state.values[packages[i]];
244 delete this.state.creates[packages[i]];
245 delete this.state.changes[packages[i]];
246 delete this.state.deletes[packages[i]];
248 delete this.loaded[packages[i]];
253 * Adds a new section of the given type to the given configuration,
254 * optionally named according to the given name.
256 * @param {string} config
257 * The name of the configuration to add the section to.
259 * @param {string} type
260 * The type of the section to add.
262 * @param {string} [name]
263 * The name of the section to add. If the name is omitted, an anonymous
264 * section will be added instead.
267 * Returns the section ID of the newly added section which is equivalent
268 * to the given name for non-anonymous sections.
270 add: function(conf, type, name) {
271 var n = this.state.creates,
272 sid = name || this.createSID(conf);
282 '.index': 1000 + this.state.newidx++
289 * Removes the section with the given ID from the given configuration.
291 * @param {string} config
292 * The name of the configuration to remove the section from.
294 * @param {string} sid
295 * The ID of the section to remove.
297 remove: function(conf, sid) {
298 var n = this.state.creates,
299 c = this.state.changes,
300 d = this.state.deletes;
302 /* requested deletion of a just created section */
303 if (n[conf] && n[conf][sid]) {
318 * A section object represents the options and their corresponding values
319 * enclosed within a configuration section, as well as some additional
320 * meta data such as sort indexes and internal ID.
322 * Any internal metadata fields are prefixed with a dot which is isn't
323 * an allowed character for normal option names.
325 * @typedef {Object<string, boolean|number|string|string[]>} SectionObject
328 * @property {boolean} .anonymous
329 * The `.anonymous` property specifies whether the configuration is
330 * anonymous (`true`) or named (`false`).
332 * @property {number} .index
333 * The `.index` property specifes the sort order of the section.
335 * @property {string} .name
336 * The `.name` property holds the name of the section object. It may be
337 * either an anonymous ID in the form `cfgXXXXXX` or `newXXXXXX` with `X`
338 * being a hexadecimal digit or a string holding the name of the section.
340 * @property {string} .type
341 * The `.type` property contains the type of the corresponding uci
344 * @property {string|string[]} *
345 * A section object may contain an arbitrary number of further properties
346 * representing the uci option enclosed in the section.
348 * All option property names will be in the form `[A-Za-z0-9_]+` and
349 * either contain a string value or an array of strings, in case the
350 * underlying option is an UCI list.
354 * The sections callback is invoked for each section found within
355 * the given configuration and receives the section object and its
356 * associated name as arguments.
358 * @callback LuCI.uci~sectionsFn
360 * @param {LuCI.uci.SectionObject} section
361 * The section object.
363 * @param {string} sid
364 * The name or ID of the section.
368 * Enumerates the sections of the given configuration, optionally
371 * @param {string} config
372 * The name of the configuration to enumerate the sections for.
374 * @param {string} [type]
375 * Enumerate only sections of the given type. If omitted, enumerate
378 * @param {LuCI.uci~sectionsFn} [cb]
379 * An optional callback to invoke for each enumerated section.
381 * @returns {Array<LuCI.uci.SectionObject>}
382 * Returns a sorted array of the section objects within the given
383 * configuration, filtered by type of a type has been specified.
385 sections: function(conf, type, cb) {
387 v = this.state.values[conf],
388 n = this.state.creates[conf],
389 c = this.state.changes[conf],
390 d = this.state.deletes[conf];
396 if (!d || d[s] !== true)
397 if (!type || v[s]['.type'] == type)
398 sa.push(Object.assign({ }, v[s], c ? c[s] : undefined));
402 if (!type || n[s]['.type'] == type)
403 sa.push(Object.assign({ }, n[s]));
405 sa.sort(function(a, b) {
406 return a['.index'] - b['.index'];
409 for (var i = 0; i < sa.length; i++)
412 if (typeof(cb) == 'function')
413 for (var i = 0; i < sa.length; i++)
414 cb.call(this, sa[i], sa[i]['.name']);
420 * Gets the value of the given option within the specified section
421 * of the given configuration or the entire section object if the
422 * option name is omitted.
424 * @param {string} config
425 * The name of the configuration to read the value from.
427 * @param {string} sid
428 * The name or ID of the section to read.
430 * @param {string} [option]
431 * The option name to read the value from. If the option name is
432 * omitted or `null`, the entire section is returned instead.
434 * @returns {null|string|string[]|LuCI.uci.SectionObject}
435 * - Returns a string containing the option value in case of a
437 * - Returns an array of strings containing the option values in
438 * case of `option` pointing to an UCI list.
439 * - Returns a {@link LuCI.uci.SectionObject section object} if
440 * the `option` argument has been omitted or is `null`.
441 * - Returns `null` if the config, section or option has not been
442 * found or if the corresponding configuration is not loaded.
444 get: function(conf, sid, opt) {
445 var v = this.state.values,
446 n = this.state.creates,
447 c = this.state.changes,
448 d = this.state.deletes;
450 sid = this.resolveSID(conf, sid);
455 /* requested option in a just created section */
456 if (n[conf] && n[conf][sid]) {
463 return n[conf][sid][opt];
466 /* requested an option value */
468 /* check whether option was deleted */
469 if (d[conf] && d[conf][sid]) {
470 if (d[conf][sid] === true)
473 for (var i = 0; i < d[conf][sid].length; i++)
474 if (d[conf][sid][i] == opt)
478 /* check whether option was changed */
479 if (c[conf] && c[conf][sid] && c[conf][sid][opt] != null)
480 return c[conf][sid][opt];
482 /* return base value */
483 if (v[conf] && v[conf][sid])
484 return v[conf][sid][opt];
489 /* requested an entire section */
497 * Sets the value of the given option within the specified section
498 * of the given configuration.
500 * If either config, section or option is null, or if `option` begins
501 * with a dot, the function will do nothing.
503 * @param {string} config
504 * The name of the configuration to set the option value in.
506 * @param {string} sid
507 * The name or ID of the section to set the option value in.
509 * @param {string} option
510 * The option name to set the value for.
512 * @param {null|string|string[]} value
513 * The option value to set. If the value is `null` or an empty string,
514 * the option will be removed, otherwise it will be set or overwritten
515 * with the given value.
517 set: function(conf, sid, opt, val) {
518 var v = this.state.values,
519 n = this.state.creates,
520 c = this.state.changes,
521 d = this.state.deletes;
523 sid = this.resolveSID(conf, sid);
525 if (sid == null || opt == null || opt.charAt(0) == '.')
528 if (n[conf] && n[conf][sid]) {
530 n[conf][sid][opt] = val;
532 delete n[conf][sid][opt];
534 else if (val != null && val !== '') {
535 /* do not set within deleted section */
536 if (d[conf] && d[conf][sid] === true)
539 /* only set in existing sections */
540 if (!v[conf] || !v[conf][sid])
549 /* undelete option */
550 if (d[conf] && d[conf][sid]) {
551 d[conf][sid] = d[conf][sid].filter(function(o) { return o !== opt });
553 if (d[conf][sid].length == 0)
557 c[conf][sid][opt] = val;
560 /* only delete in existing sections */
561 if (!(v[conf] && v[conf][sid] && v[conf][sid].hasOwnProperty(opt)) &&
562 !(c[conf] && c[conf][sid] && c[conf][sid].hasOwnProperty(opt)))
571 if (d[conf][sid] !== true)
572 d[conf][sid].push(opt);
577 * Remove the given option within the specified section of the given
580 * This function is a convenience wrapper around
581 * `uci.set(config, section, option, null)`.
583 * @param {string} config
584 * The name of the configuration to remove the option from.
586 * @param {string} sid
587 * The name or ID of the section to remove the option from.
589 * @param {string} option
590 * The name of the option to remove.
592 unset: function(conf, sid, opt) {
593 return this.set(conf, sid, opt, null);
597 * Gets the value of the given option or the entire section object of
598 * the first found section of the specified type or the first found
599 * section of the entire configuration if no type is specfied.
601 * @param {string} config
602 * The name of the configuration to read the value from.
604 * @param {string} [type]
605 * The type of the first section to find. If it is `null`, the first
606 * section of the entire config is read, otherwise the first section
607 * matching the given type.
609 * @param {string} [option]
610 * The option name to read the value from. If the option name is
611 * omitted or `null`, the entire section is returned instead.
613 * @returns {null|string|string[]|LuCI.uci.SectionObject}
614 * - Returns a string containing the option value in case of a
616 * - Returns an array of strings containing the option values in
617 * case of `option` pointing to an UCI list.
618 * - Returns a {@link LuCI.uci.SectionObject section object} if
619 * the `option` argument has been omitted or is `null`.
620 * - Returns `null` if the config, section or option has not been
621 * found or if the corresponding configuration is not loaded.
623 get_first: function(conf, type, opt) {
626 this.sections(conf, type, function(s) {
631 return this.get(conf, sid, opt);
635 * Sets the value of the given option within the first found section
636 * of the given configuration matching the specified type or within
637 * the first section of the entire config when no type has is specified.
639 * If either config, type or option is null, or if `option` begins
640 * with a dot, the function will do nothing.
642 * @param {string} config
643 * The name of the configuration to set the option value in.
645 * @param {string} [type]
646 * The type of the first section to find. If it is `null`, the first
647 * section of the entire config is written to, otherwise the first
648 * section matching the given type is used.
650 * @param {string} option
651 * The option name to set the value for.
653 * @param {null|string|string[]} value
654 * The option value to set. If the value is `null` or an empty string,
655 * the option will be removed, otherwise it will be set or overwritten
656 * with the given value.
658 set_first: function(conf, type, opt, val) {
661 this.sections(conf, type, function(s) {
666 return this.set(conf, sid, opt, val);
670 * Removes the given option within the first found section of the given
671 * configuration matching the specified type or within the first section
672 * of the entire config when no type has is specified.
674 * This function is a convenience wrapper around
675 * `uci.set_first(config, type, option, null)`.
677 * @param {string} config
678 * The name of the configuration to set the option value in.
680 * @param {string} [type]
681 * The type of the first section to find. If it is `null`, the first
682 * section of the entire config is written to, otherwise the first
683 * section matching the given type is used.
685 * @param {string} option
686 * The option name to set the value for.
688 unset_first: function(conf, type, opt) {
689 return this.set_first(conf, type, opt, null);
693 * Move the first specified section within the given configuration
694 * before or after the second specified section.
696 * @param {string} config
697 * The configuration to move the section within.
699 * @param {string} sid1
700 * The ID of the section to move within the configuration.
702 * @param {string} [sid2]
703 * The ID of the target section for the move operation. If the
704 * `after` argument is `false` or not specified, the section named by
705 * `sid1` will be moved before this target section, if the `after`
706 * argument is `true`, the `sid1` section will be moved after this
709 * When the `sid2` argument is `null`, the section specified by `sid1`
710 * is moved to the end of the configuration.
712 * @param {boolean} [after=false]
713 * When `true`, the section `sid1` is moved after the section `sid2`,
714 * when `false`, the section `sid1` is moved before `sid2`.
716 * If `sid2` is null, then this parameter has no effect and the section
717 * `sid1` is moved to the end of the configuration instead.
720 * Returns `true` when the section was successfully moved, or `false`
721 * when either the section specified by `sid1` or by `sid2` is not found.
723 move: function(conf, sid1, sid2, after) {
724 var sa = this.sections(conf),
725 s1 = null, s2 = null;
727 sid1 = this.resolveSID(conf, sid1);
728 sid2 = this.resolveSID(conf, sid2);
730 for (var i = 0; i < sa.length; i++) {
731 if (sa[i]['.name'] != sid1)
746 for (var i = 0; i < sa.length; i++) {
747 if (sa[i]['.name'] != sid2)
751 sa.splice(i + !!after, 0, s1);
759 for (var i = 0; i < sa.length; i++)
760 this.get(conf, sa[i]['.name'])['.index'] = i;
762 this.state.reorder[conf] = true;
768 * Submits all local configuration changes to the remove `ubus` api,
769 * adds, removes and reorders remote sections as needed and reloads
770 * all loaded configurations to resynchronize the local state with
771 * the remote configuration values.
773 * @returns {string[]}
774 * Returns a promise resolving to an array of configuration names which
775 * have been reloaded by the save operation.
778 var v = this.state.values,
779 n = this.state.creates,
780 c = this.state.changes,
781 d = this.state.deletes,
782 r = this.state.reorder,
789 for (var conf in n) {
790 for (var sid in n[conf]) {
796 for (var k in n[conf][sid]) {
798 p.type = n[conf][sid][k];
799 else if (k == '.create')
800 p.name = n[conf][sid][k];
801 else if (k.charAt(0) != '.')
802 p.values[k] = n[conf][sid][k];
805 snew.push(n[conf][sid]);
806 tasks.push(self.callAdd(p.config, p.type, p.name, p.values));
813 for (var conf in c) {
814 for (var sid in c[conf])
815 tasks.push(self.callSet(conf, sid, c[conf][sid]));
821 for (var conf in d) {
822 for (var sid in d[conf]) {
823 var o = d[conf][sid];
824 tasks.push(self.callDelete(conf, sid, (o === true) ? null : o));
834 return Promise.all(tasks).then(function(responses) {
836 array "snew" holds references to the created uci sections,
837 use it to assign the returned names of the new sections
839 for (var i = 0; i < snew.length; i++)
840 snew[i]['.name'] = responses[i];
842 return self.reorderSections();
844 pkgs = Object.keys(pkgs);
848 return self.load(pkgs);
853 * Instructs the remote `ubus` UCI api to commit all saved changes with
854 * rollback protection and attempts to confirm the pending commit
855 * operation to cancel the rollback timer.
857 * @param {number} [timeout=10]
858 * Override the confirmation timeout after which a rollback is triggered.
860 * @returns {Promise<number>}
861 * Returns a promise resolving/rejecting with the `ubus` RPC status code.
863 apply: function(timeout) {
867 if (typeof(timeout) != 'number' || timeout < 1)
870 return self.callApply(timeout, true).then(function(rv) {
872 return Promise.reject(rv);
874 var try_deadline = date.getTime() + 1000 * timeout;
875 var try_confirm = function() {
876 return self.callConfirm().then(function(rv) {
878 if (date.getTime() < try_deadline)
879 window.setTimeout(try_confirm, 250);
881 return Promise.reject(rv);
888 window.setTimeout(try_confirm, 1000);
893 * An UCI change record is a plain array containing the change operation
894 * name as first element, the affected section ID as second argument
895 * and an optional third and fourth argument whose meanings depend on
898 * @typedef {string[]} ChangeRecord
901 * @property {string} 0
902 * The operation name - may be one of `add`, `set`, `remove`, `order`,
903 * `list-add`, `list-del` or `rename`.
905 * @property {string} 1
906 * The section ID targeted by the operation.
908 * @property {string} 2
909 * The meaning of the third element depends on the operation.
910 * - For `add` it is type of the section that has been added
911 * - For `set` it either is the option name if a fourth element exists,
912 * or the type of a named section which has been added when the change
913 * entry only contains three elements.
914 * - For `remove` it contains the name of the option that has been
916 * - For `order` it specifies the new sort index of the section.
917 * - For `list-add` it contains the name of the list option a new value
919 * - For `list-del` it contains the name of the list option a value has
921 * - For `rename` it contains the name of the option that has been
922 * renamed if a fourth element exists, else it contains the new name
923 * a section has been renamed to if the change entry only contains
926 * @property {string} 4
927 * The meaning of the fourth element depends on the operation.
928 * - For `set` it is the value an option has been set to.
929 * - For `list-add` it is the new value that has been added to a
931 * - For `rename` it is the new name of an option that has been
936 * Fetches uncommitted UCI changes from the remote `ubus` RPC api.
939 * @returns {Promise<Object<string, Array<LuCI.uci.ChangeRecord>>>}
940 * Returns a promise resolving to an object containing the configuration
941 * names as keys and arrays of related change records as values.
943 changes: rpc.declare({
946 expect: { changes: { } }