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 c[conf][sid][opt] = val;
556 /* only delete in existing sections */
557 if (!(v[conf] && v[conf][sid] && v[conf][sid].hasOwnProperty(opt)) &&
558 !(c[conf] && c[conf][sid] && c[conf][sid].hasOwnProperty(opt)))
567 if (d[conf][sid] !== true)
568 d[conf][sid].push(opt);
573 * Remove the given option within the specified section of the given
576 * This function is a convenience wrapper around
577 * `uci.set(config, section, option, null)`.
579 * @param {string} config
580 * The name of the configuration to remove the option from.
582 * @param {string} sid
583 * The name or ID of the section to remove the option from.
585 * @param {string} option
586 * The name of the option to remove.
588 unset: function(conf, sid, opt) {
589 return this.set(conf, sid, opt, null);
593 * Gets the value of the given option or the entire section object of
594 * the first found section of the specified type or the first found
595 * section of the entire configuration if no type is specfied.
597 * @param {string} config
598 * The name of the configuration to read the value from.
600 * @param {string} [type]
601 * The type of the first section to find. If it is `null`, the first
602 * section of the entire config is read, otherwise the first section
603 * matching the given type.
605 * @param {string} [option]
606 * The option name to read the value from. If the option name is
607 * omitted or `null`, the entire section is returned instead.
609 * @returns {null|string|string[]|LuCI.uci.SectionObject}
610 * - Returns a string containing the option value in case of a
612 * - Returns an array of strings containing the option values in
613 * case of `option` pointing to an UCI list.
614 * - Returns a {@link LuCI.uci.SectionObject section object} if
615 * the `option` argument has been omitted or is `null`.
616 * - Returns `null` if the config, section or option has not been
617 * found or if the corresponding configuration is not loaded.
619 get_first: function(conf, type, opt) {
622 this.sections(conf, type, function(s) {
627 return this.get(conf, sid, opt);
631 * Sets the value of the given option within the first found section
632 * of the given configuration matching the specified type or within
633 * the first section of the entire config when no type has is specified.
635 * If either config, type or option is null, or if `option` begins
636 * with a dot, the function will do nothing.
638 * @param {string} config
639 * The name of the configuration to set the option value in.
641 * @param {string} [type]
642 * The type of the first section to find. If it is `null`, the first
643 * section of the entire config is written to, otherwise the first
644 * section matching the given type is used.
646 * @param {string} option
647 * The option name to set the value for.
649 * @param {null|string|string[]} value
650 * The option value to set. If the value is `null` or an empty string,
651 * the option will be removed, otherwise it will be set or overwritten
652 * with the given value.
654 set_first: function(conf, type, opt, val) {
657 this.sections(conf, type, function(s) {
662 return this.set(conf, sid, opt, val);
666 * Removes the given option within the first found section of the given
667 * configuration matching the specified type or within the first section
668 * of the entire config when no type has is specified.
670 * This function is a convenience wrapper around
671 * `uci.set_first(config, type, option, null)`.
673 * @param {string} config
674 * The name of the configuration to set the option value in.
676 * @param {string} [type]
677 * The type of the first section to find. If it is `null`, the first
678 * section of the entire config is written to, otherwise the first
679 * section matching the given type is used.
681 * @param {string} option
682 * The option name to set the value for.
684 unset_first: function(conf, type, opt) {
685 return this.set_first(conf, type, opt, null);
689 * Move the first specified section within the given configuration
690 * before or after the second specified section.
692 * @param {string} config
693 * The configuration to move the section within.
695 * @param {string} sid1
696 * The ID of the section to move within the configuration.
698 * @param {string} [sid2]
699 * The ID of the target section for the move operation. If the
700 * `after` argument is `false` or not specified, the section named by
701 * `sid1` will be moved before this target section, if the `after`
702 * argument is `true`, the `sid1` section will be moved after this
705 * When the `sid2` argument is `null`, the section specified by `sid1`
706 * is moved to the end of the configuration.
708 * @param {boolean} [after=false]
709 * When `true`, the section `sid1` is moved after the section `sid2`,
710 * when `false`, the section `sid1` is moved before `sid2`.
712 * If `sid2` is null, then this parameter has no effect and the section
713 * `sid1` is moved to the end of the configuration instead.
716 * Returns `true` when the section was successfully moved, or `false`
717 * when either the section specified by `sid1` or by `sid2` is not found.
719 move: function(conf, sid1, sid2, after) {
720 var sa = this.sections(conf),
721 s1 = null, s2 = null;
723 sid1 = this.resolveSID(conf, sid1);
724 sid2 = this.resolveSID(conf, sid2);
726 for (var i = 0; i < sa.length; i++) {
727 if (sa[i]['.name'] != sid1)
742 for (var i = 0; i < sa.length; i++) {
743 if (sa[i]['.name'] != sid2)
747 sa.splice(i + !!after, 0, s1);
755 for (var i = 0; i < sa.length; i++)
756 this.get(conf, sa[i]['.name'])['.index'] = i;
758 this.state.reorder[conf] = true;
764 * Submits all local configuration changes to the remove `ubus` api,
765 * adds, removes and reorders remote sections as needed and reloads
766 * all loaded configurations to resynchronize the local state with
767 * the remote configuration values.
769 * @returns {string[]}
770 * Returns a promise resolving to an array of configuration names which
771 * have been reloaded by the save operation.
774 var v = this.state.values,
775 n = this.state.creates,
776 c = this.state.changes,
777 d = this.state.deletes,
778 r = this.state.reorder,
785 for (var conf in n) {
786 for (var sid in n[conf]) {
792 for (var k in n[conf][sid]) {
794 r.type = n[conf][sid][k];
795 else if (k == '.create')
796 r.name = n[conf][sid][k];
797 else if (k.charAt(0) != '.')
798 r.values[k] = n[conf][sid][k];
801 snew.push(n[conf][sid]);
802 tasks.push(self.callAdd(r.config, r.type, r.name, r.values));
809 for (var conf in c) {
810 for (var sid in c[conf])
811 tasks.push(self.callSet(conf, sid, c[conf][sid]));
817 for (var conf in d) {
818 for (var sid in d[conf]) {
819 var o = d[conf][sid];
820 tasks.push(self.callDelete(conf, sid, (o === true) ? null : o));
830 return Promise.all(tasks).then(function(responses) {
832 array "snew" holds references to the created uci sections,
833 use it to assign the returned names of the new sections
835 for (var i = 0; i < snew.length; i++)
836 snew[i]['.name'] = responses[i];
838 return self.reorderSections();
840 pkgs = Object.keys(pkgs);
844 return self.load(pkgs);
849 * Instructs the remote `ubus` UCI api to commit all saved changes with
850 * rollback protection and attempts to confirm the pending commit
851 * operation to cancel the rollback timer.
853 * @param {number} [timeout=10]
854 * Override the confirmation timeout after which a rollback is triggered.
856 * @returns {Promise<number>}
857 * Returns a promise resolving/rejecting with the `ubus` RPC status code.
859 apply: function(timeout) {
863 if (typeof(timeout) != 'number' || timeout < 1)
866 return self.callApply(timeout, true).then(function(rv) {
868 return Promise.reject(rv);
870 var try_deadline = date.getTime() + 1000 * timeout;
871 var try_confirm = function() {
872 return self.callConfirm().then(function(rv) {
874 if (date.getTime() < try_deadline)
875 window.setTimeout(try_confirm, 250);
877 return Promise.reject(rv);
884 window.setTimeout(try_confirm, 1000);
889 * An UCI change record is a plain array containing the change operation
890 * name as first element, the affected section ID as second argument
891 * and an optional third and fourth argument whose meanings depend on
894 * @typedef {string[]} ChangeRecord
897 * @property {string} 0
898 * The operation name - may be one of `add`, `set`, `remove`, `order`,
899 * `list-add`, `list-del` or `rename`.
901 * @property {string} 1
902 * The section ID targeted by the operation.
904 * @property {string} 2
905 * The meaning of the third element depends on the operation.
906 * - For `add` it is type of the section that has been added
907 * - For `set` it either is the option name if a fourth element exists,
908 * or the type of a named section which has been added when the change
909 * entry only contains three elements.
910 * - For `remove` it contains the name of the option that has been
912 * - For `order` it specifies the new sort index of the section.
913 * - For `list-add` it contains the name of the list option a new value
915 * - For `list-del` it contains the name of the list option a value has
917 * - For `rename` it contains the name of the option that has been
918 * renamed if a fourth element exists, else it contains the new name
919 * a section has been renamed to if the change entry only contains
922 * @property {string} 4
923 * The meaning of the fourth element depends on the operation.
924 * - For `set` it is the value an option has been set to.
925 * - For `list-add` it is the new value that has been added to a
927 * - For `rename` it is the new name of an option that has been
932 * Fetches uncommitted UCI changes from the remote `ubus` RPC api.
935 * @returns {Promise<Object<string, Array<LuCI.uci.ChangeRecord>>>}
936 * Returns a promise resolving to an object containing the configuration
937 * names as keys and arrays of related change records as values.
939 changes: rpc.declare({
942 expect: { changes: { } }