1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
|
'use strict';
'require rpc';
'require baseclass';
function isEmpty(object, ignore) {
for (var property in object)
if (object.hasOwnProperty(property) && property != ignore)
return false;
return true;
}
/**
* @class uci
* @memberof LuCI
* @hideconstructor
* @classdesc
*
* The `LuCI.uci` class utilizes {@link LuCI.rpc} to declare low level
* remote UCI `ubus` procedures and implements a local caching and data
* manipulation layer on top to allow for synchronous operations on
* UCI configuration data.
*/
return baseclass.extend(/** @lends LuCI.uci.prototype */ {
__init__: function() {
this.state = {
newidx: 0,
values: { },
creates: { },
changes: { },
deletes: { },
reorder: { }
};
this.loaded = {};
},
callLoad: rpc.declare({
object: 'uci',
method: 'get',
params: [ 'config' ],
expect: { values: { } },
reject: true
}),
callOrder: rpc.declare({
object: 'uci',
method: 'order',
params: [ 'config', 'sections' ],
reject: true
}),
callAdd: rpc.declare({
object: 'uci',
method: 'add',
params: [ 'config', 'type', 'name', 'values' ],
expect: { section: '' },
reject: true
}),
callSet: rpc.declare({
object: 'uci',
method: 'set',
params: [ 'config', 'section', 'values' ],
reject: true
}),
callDelete: rpc.declare({
object: 'uci',
method: 'delete',
params: [ 'config', 'section', 'options' ],
reject: true
}),
callApply: rpc.declare({
object: 'uci',
method: 'apply',
params: [ 'timeout', 'rollback' ],
reject: true
}),
callConfirm: rpc.declare({
object: 'uci',
method: 'confirm',
reject: true
}),
/**
* Generates a new, unique section ID for the given configuration.
*
* Note that the generated ID is temporary, it will get replaced by an
* identifier in the form `cfgXXXXXX` once the configuration is saved
* by the remote `ubus` UCI api.
*
* @param {string} conf
* The configuration to generate the new section ID for.
*
* @returns {string}
* A newly generated, unique section ID in the form `newXXXXXX`
* where `X` denotes a hexadecimal digit.
*/
createSID: function(conf) {
var v = this.state.values,
n = this.state.creates,
sid;
do {
sid = "new%06x".format(Math.random() * 0xFFFFFF);
} while ((n[conf] && n[conf][sid]) || (v[conf] && v[conf][sid]));
return sid;
},
/**
* Resolves a given section ID in extended notation to the internal
* section ID value.
*
* @param {string} conf
* The configuration to resolve the section ID for.
*
* @param {string} sid
* The section ID to resolve. If the ID is in the form `@typename[#]`,
* it will get resolved to an internal anonymous ID in the forms
* `cfgXXXXXX`/`newXXXXXX` or to the name of a section in case it points
* to a named section. When the given ID is not in extended notation,
* it will be returned as-is.
*
* @returns {string|null}
* Returns the resolved section ID or the original given ID if it was
* not in extended notation. Returns `null` when an extended ID could
* not be resolved to existing section ID.
*/
resolveSID: function(conf, sid) {
if (typeof(sid) != 'string')
return sid;
var m = /^@([a-zA-Z0-9_-]+)\[(-?[0-9]+)\]$/.exec(sid);
if (m) {
var type = m[1],
pos = +m[2],
sections = this.sections(conf, type),
section = sections[pos >= 0 ? pos : sections.length + pos];
return section ? section['.name'] : null;
}
return sid;
},
/* private */
reorderSections: function() {
var v = this.state.values,
n = this.state.creates,
r = this.state.reorder,
tasks = [];
if (Object.keys(r).length === 0)
return Promise.resolve();
/*
gather all created and existing sections, sort them according
to their index value and issue an uci order call
*/
for (var c in r) {
var o = [ ];
if (n[c])
for (var s in n[c])
o.push(n[c][s]);
for (var s in v[c])
o.push(v[c][s]);
if (o.length > 0) {
o.sort(function(a, b) {
return (a['.index'] - b['.index']);
});
var sids = [ ];
for (var i = 0; i < o.length; i++)
sids.push(o[i]['.name']);
tasks.push(this.callOrder(c, sids));
}
}
this.state.reorder = { };
return Promise.all(tasks);
},
/* private */
loadPackage: function(packageName) {
if (this.loaded[packageName] == null)
return (this.loaded[packageName] = this.callLoad(packageName));
return Promise.resolve(this.loaded[packageName]);
},
/**
* Loads the given UCI configurations from the remote `ubus` api.
*
* Loaded configurations are cached and only loaded once. Subsequent
* load operations of the same configurations will return the cached
* data.
*
* To force reloading a configuration, it has to be unloaded with
* {@link LuCI.uci#unload uci.unload()} first.
*
* @param {string|string[]} packages
* The name of the configuration or an array of configuration
* names to load.
*
* @returns {Promise<string[]>}
* Returns a promise resolving to the names of the configurations
* that have been successfully loaded.
*/
load: function(packages) {
var self = this,
pkgs = [ ],
tasks = [];
if (!Array.isArray(packages))
packages = [ packages ];
for (var i = 0; i < packages.length; i++)
if (!self.state.values[packages[i]]) {
pkgs.push(packages[i]);
tasks.push(self.loadPackage(packages[i]));
}
return Promise.all(tasks).then(function(responses) {
for (var i = 0; i < responses.length; i++)
self.state.values[pkgs[i]] = responses[i];
if (responses.length)
document.dispatchEvent(new CustomEvent('uci-loaded'));
return pkgs;
});
},
/**
* Unloads the given UCI configurations from the local cache.
*
* @param {string|string[]} packages
* The name of the configuration or an array of configuration
* names to unload.
*/
unload: function(packages) {
if (!Array.isArray(packages))
packages = [ packages ];
for (var i = 0; i < packages.length; i++) {
delete this.state.values[packages[i]];
delete this.state.creates[packages[i]];
delete this.state.changes[packages[i]];
delete this.state.deletes[packages[i]];
delete this.loaded[packages[i]];
}
},
/**
* Adds a new section of the given type to the given configuration,
* optionally named according to the given name.
*
* @param {string} conf
* The name of the configuration to add the section to.
*
* @param {string} type
* The type of the section to add.
*
* @param {string} [name]
* The name of the section to add. If the name is omitted, an anonymous
* section will be added instead.
*
* @returns {string}
* Returns the section ID of the newly added section which is equivalent
* to the given name for non-anonymous sections.
*/
add: function(conf, type, name) {
var n = this.state.creates,
sid = name || this.createSID(conf);
if (!n[conf])
n[conf] = { };
n[conf][sid] = {
'.type': type,
'.name': sid,
'.create': name,
'.anonymous': !name,
'.index': 1000 + this.state.newidx++
};
return sid;
},
/**
* Removes the section with the given ID from the given configuration.
*
* @param {string} conf
* The name of the configuration to remove the section from.
*
* @param {string} sid
* The ID of the section to remove.
*/
remove: function(conf, sid) {
var v = this.state.values,
n = this.state.creates,
c = this.state.changes,
d = this.state.deletes;
/* requested deletion of a just created section */
if (n[conf] && n[conf][sid]) {
delete n[conf][sid];
}
else if (v[conf] && v[conf][sid]) {
if (c[conf])
delete c[conf][sid];
if (!d[conf])
d[conf] = { };
d[conf][sid] = true;
}
},
/**
* A section object represents the options and their corresponding values
* enclosed within a configuration section, as well as some additional
* meta data such as sort indexes and internal ID.
*
* Any internal metadata fields are prefixed with a dot which isn't
* an allowed character for normal option names.
*
* @typedef {Object<string, boolean|number|string|string[]>} SectionObject
* @memberof LuCI.uci
*
* @property {boolean} .anonymous
* The `.anonymous` property specifies whether the configuration is
* anonymous (`true`) or named (`false`).
*
* @property {number} .index
* The `.index` property specifies the sort order of the section.
*
* @property {string} .name
* The `.name` property holds the name of the section object. It may be
* either an anonymous ID in the form `cfgXXXXXX` or `newXXXXXX` with `X`
* being a hexadecimal digit or a string holding the name of the section.
*
* @property {string} .type
* The `.type` property contains the type of the corresponding uci
* section.
*
* @property {string|string[]} *
* A section object may contain an arbitrary number of further properties
* representing the uci option enclosed in the section.
*
* All option property names will be in the form `[A-Za-z0-9_]+` and
* either contain a string value or an array of strings, in case the
* underlying option is an UCI list.
*/
/**
* The sections callback is invoked for each section found within
* the given configuration and receives the section object and its
* associated name as arguments.
*
* @callback LuCI.uci~sectionsFn
*
* @param {LuCI.uci.SectionObject} section
* The section object.
*
* @param {string} sid
* The name or ID of the section.
*/
/**
* Enumerates the sections of the given configuration, optionally
* filtered by type.
*
* @param {string} conf
* The name of the configuration to enumerate the sections for.
*
* @param {string} [type]
* Enumerate only sections of the given type. If omitted, enumerate
* all sections.
*
* @param {LuCI.uci~sectionsFn} [cb]
* An optional callback to invoke for each enumerated section.
*
* @returns {Array<LuCI.uci.SectionObject>}
* Returns a sorted array of the section objects within the given
* configuration, filtered by type of a type has been specified.
*/
sections: function(conf, type, cb) {
var sa = [ ],
v = this.state.values[conf],
n = this.state.creates[conf],
c = this.state.changes[conf],
d = this.state.deletes[conf];
if (!v)
return sa;
for (var s in v)
if (!d || d[s] !== true)
if (!type || v[s]['.type'] == type)
sa.push(Object.assign({ }, v[s], c ? c[s] : null));
if (n)
for (var s in n)
if (!type || n[s]['.type'] == type)
sa.push(Object.assign({ }, n[s]));
sa.sort(function(a, b) {
return a['.index'] - b['.index'];
});
for (var i = 0; i < sa.length; i++)
sa[i]['.index'] = i;
if (typeof(cb) == 'function')
for (var i = 0; i < sa.length; i++)
cb.call(this, sa[i], sa[i]['.name']);
return sa;
},
/**
* Gets the value of the given option within the specified section
* of the given configuration or the entire section object if the
* option name is omitted.
*
* @param {string} conf
* The name of the configuration to read the value from.
*
* @param {string} sid
* The name or ID of the section to read.
*
* @param {string} [opt]
* The option name to read the value from. If the option name is
* omitted or `null`, the entire section is returned instead.
*
* @returns {null|string|string[]|LuCI.uci.SectionObject}
* - Returns a string containing the option value in case of a
* plain UCI option.
* - Returns an array of strings containing the option values in
* case of `option` pointing to an UCI list.
* - Returns a {@link LuCI.uci.SectionObject section object} if
* the `option` argument has been omitted or is `null`.
* - Returns `null` if the config, section or option has not been
* found or if the corresponding configuration is not loaded.
*/
get: function(conf, sid, opt) {
var v = this.state.values,
n = this.state.creates,
c = this.state.changes,
d = this.state.deletes;
sid = this.resolveSID(conf, sid);
if (sid == null)
return null;
/* requested option in a just created section */
if (n[conf] && n[conf][sid]) {
if (!n[conf])
return null;
if (opt == null)
return n[conf][sid];
return n[conf][sid][opt];
}
/* requested an option value */
if (opt != null) {
/* check whether option was deleted */
if (d[conf] && d[conf][sid])
if (d[conf][sid] === true || d[conf][sid][opt])
return null;
/* check whether option was changed */
if (c[conf] && c[conf][sid] && c[conf][sid][opt] != null)
return c[conf][sid][opt];
/* return base value */
if (v[conf] && v[conf][sid])
return v[conf][sid][opt];
return null;
}
/* requested an entire section */
if (v[conf]) {
/* check whether entire section was deleted */
if (d[conf] && d[conf][sid] === true)
return null;
var s = v[conf][sid] || null;
if (s) {
/* merge changes */
if (c[conf] && c[conf][sid])
for (var opt in c[conf][sid])
if (c[conf][sid][opt] != null)
s[opt] = c[conf][sid][opt];
/* merge deletions */
if (d[conf] && d[conf][sid])
for (var opt in d[conf][sid])
delete s[opt];
}
return s;
}
return null;
},
/**
* Sets the value of the given option within the specified section
* of the given configuration.
*
* If either config, section or option is null, or if `option` begins
* with a dot, the function will do nothing.
*
* @param {string} conf
* The name of the configuration to set the option value in.
*
* @param {string} sid
* The name or ID of the section to set the option value in.
*
* @param {string} opt
* The option name to set the value for.
*
* @param {null|string|string[]} val
* The option value to set. If the value is `null` or an empty string,
* the option will be removed, otherwise it will be set or overwritten
* with the given value.
*/
set: function(conf, sid, opt, val) {
var v = this.state.values,
n = this.state.creates,
c = this.state.changes,
d = this.state.deletes;
sid = this.resolveSID(conf, sid);
if (sid == null || opt == null || opt.charAt(0) == '.')
return;
if (n[conf] && n[conf][sid]) {
if (val != null)
n[conf][sid][opt] = val;
else
delete n[conf][sid][opt];
}
else if (val != null && val !== '') {
/* do not set within deleted section */
if (d[conf] && d[conf][sid] === true)
return;
/* only set in existing sections */
if (!v[conf] || !v[conf][sid])
return;
if (!c[conf])
c[conf] = {};
if (!c[conf][sid])
c[conf][sid] = {};
/* undelete option */
if (d[conf] && d[conf][sid]) {
if (isEmpty(d[conf][sid], opt))
delete d[conf][sid];
else
delete d[conf][sid][opt];
}
c[conf][sid][opt] = val;
}
else {
/* revert any change for to-be-deleted option */
if (c[conf] && c[conf][sid]) {
if (isEmpty(c[conf][sid], opt))
delete c[conf][sid];
else
delete c[conf][sid][opt];
}
/* only delete existing options */
if (v[conf] && v[conf][sid] && v[conf][sid].hasOwnProperty(opt)) {
if (!d[conf])
d[conf] = { };
if (!d[conf][sid])
d[conf][sid] = { };
if (d[conf][sid] !== true)
d[conf][sid][opt] = true;
}
}
},
/**
* Remove the given option within the specified section of the given
* configuration.
*
* This function is a convenience wrapper around
* `uci.set(config, section, option, null)`.
*
* @param {string} conf
* The name of the configuration to remove the option from.
*
* @param {string} sid
* The name or ID of the section to remove the option from.
*
* @param {string} opt
* The name of the option to remove.
*/
unset: function(conf, sid, opt) {
return this.set(conf, sid, opt, null);
},
/**
* Gets the value of the given option or the entire section object of
* the first found section of the specified type or the first found
* section of the entire configuration if no type is specified.
*
* @param {string} conf
* The name of the configuration to read the value from.
*
* @param {string} [type]
* The type of the first section to find. If it is `null`, the first
* section of the entire config is read, otherwise the first section
* matching the given type.
*
* @param {string} [opt]
* The option name to read the value from. If the option name is
* omitted or `null`, the entire section is returned instead.
*
* @returns {null|string|string[]|LuCI.uci.SectionObject}
* - Returns a string containing the option value in case of a
* plain UCI option.
* - Returns an array of strings containing the option values in
* case of `option` pointing to an UCI list.
* - Returns a {@link LuCI.uci.SectionObject section object} if
* the `option` argument has been omitted or is `null`.
* - Returns `null` if the config, section or option has not been
* found or if the corresponding configuration is not loaded.
*/
get_first: function(conf, type, opt) {
var sid = null;
this.sections(conf, type, function(s) {
if (sid == null)
sid = s['.name'];
});
return this.get(conf, sid, opt);
},
/**
* Sets the value of the given option within the first found section
* of the given configuration matching the specified type or within
* the first section of the entire config when no type has is specified.
*
* If either config, type or option is null, or if `option` begins
* with a dot, the function will do nothing.
*
* @param {string} conf
* The name of the configuration to set the option value in.
*
* @param {string} [type]
* The type of the first section to find. If it is `null`, the first
* section of the entire config is written to, otherwise the first
* section matching the given type is used.
*
* @param {string} opt
* The option name to set the value for.
*
* @param {null|string|string[]} val
* The option value to set. If the value is `null` or an empty string,
* the option will be removed, otherwise it will be set or overwritten
* with the given value.
*/
set_first: function(conf, type, opt, val) {
var sid = null;
this.sections(conf, type, function(s) {
if (sid == null)
sid = s['.name'];
});
return this.set(conf, sid, opt, val);
},
/**
* Removes the given option within the first found section of the given
* configuration matching the specified type or within the first section
* of the entire config when no type has is specified.
*
* This function is a convenience wrapper around
* `uci.set_first(config, type, option, null)`.
*
* @param {string} conf
* The name of the configuration to set the option value in.
*
* @param {string} [type]
* The type of the first section to find. If it is `null`, the first
* section of the entire config is written to, otherwise the first
* section matching the given type is used.
*
* @param {string} opt
* The option name to set the value for.
*/
unset_first: function(conf, type, opt) {
return this.set_first(conf, type, opt, null);
},
/**
* Move the first specified section within the given configuration
* before or after the second specified section.
*
* @param {string} conf
* The configuration to move the section within.
*
* @param {string} sid1
* The ID of the section to move within the configuration.
*
* @param {string} [sid2]
* The ID of the target section for the move operation. If the
* `after` argument is `false` or not specified, the section named by
* `sid1` will be moved before this target section, if the `after`
* argument is `true`, the `sid1` section will be moved after this
* section.
*
* When the `sid2` argument is `null`, the section specified by `sid1`
* is moved to the end of the configuration.
*
* @param {boolean} [after=false]
* When `true`, the section `sid1` is moved after the section `sid2`,
* when `false`, the section `sid1` is moved before `sid2`.
*
* If `sid2` is null, then this parameter has no effect and the section
* `sid1` is moved to the end of the configuration instead.
*
* @returns {boolean}
* Returns `true` when the section was successfully moved, or `false`
* when either the section specified by `sid1` or by `sid2` is not found.
*/
move: function(conf, sid1, sid2, after) {
var sa = this.sections(conf),
s1 = null, s2 = null;
sid1 = this.resolveSID(conf, sid1);
sid2 = this.resolveSID(conf, sid2);
for (var i = 0; i < sa.length; i++) {
if (sa[i]['.name'] != sid1)
continue;
s1 = sa[i];
sa.splice(i, 1);
break;
}
if (s1 == null)
return false;
if (sid2 == null) {
sa.push(s1);
}
else {
for (var i = 0; i < sa.length; i++) {
if (sa[i]['.name'] != sid2)
continue;
s2 = sa[i];
sa.splice(i + !!after, 0, s1);
break;
}
if (s2 == null)
return false;
}
for (var i = 0; i < sa.length; i++)
this.get(conf, sa[i]['.name'])['.index'] = i;
this.state.reorder[conf] = true;
return true;
},
/**
* Submits all local configuration changes to the remove `ubus` api,
* adds, removes and reorders remote sections as needed and reloads
* all loaded configurations to resynchronize the local state with
* the remote configuration values.
*
* @returns {string[]}
* Returns a promise resolving to an array of configuration names which
* have been reloaded by the save operation.
*/
save: function() {
var v = this.state.values,
n = this.state.creates,
c = this.state.changes,
d = this.state.deletes,
r = this.state.reorder,
self = this,
snew = [ ],
pkgs = { },
tasks = [];
if (d)
for (var conf in d) {
for (var sid in d[conf]) {
var o = d[conf][sid];
if (o === true)
tasks.push(self.callDelete(conf, sid, null));
else
tasks.push(self.callDelete(conf, sid, Object.keys(o)));
}
pkgs[conf] = true;
}
if (n)
for (var conf in n) {
for (var sid in n[conf]) {
var p = {
config: conf,
values: { }
};
for (var k in n[conf][sid]) {
if (k == '.type')
p.type = n[conf][sid][k];
else if (k == '.create')
p.name = n[conf][sid][k];
else if (k.charAt(0) != '.')
p.values[k] = n[conf][sid][k];
}
snew.push(n[conf][sid]);
tasks.push(self.callAdd(p.config, p.type, p.name, p.values));
}
pkgs[conf] = true;
}
if (c)
for (var conf in c) {
for (var sid in c[conf])
tasks.push(self.callSet(conf, sid, c[conf][sid]));
pkgs[conf] = true;
}
if (r)
for (var conf in r)
pkgs[conf] = true;
return Promise.all(tasks).then(function(responses) {
/*
array "snew" holds references to the created uci sections,
use it to assign the returned names of the new sections
*/
for (var i = 0; i < snew.length; i++)
snew[i]['.name'] = responses[i];
return self.reorderSections();
}).then(function() {
pkgs = Object.keys(pkgs);
self.unload(pkgs);
return self.load(pkgs);
});
},
/**
* Instructs the remote `ubus` UCI api to commit all saved changes with
* rollback protection and attempts to confirm the pending commit
* operation to cancel the rollback timer.
*
* @param {number} [timeout=10]
* Override the confirmation timeout after which a rollback is triggered.
*
* @returns {Promise<number>}
* Returns a promise resolving/rejecting with the `ubus` RPC status code.
*/
apply: function(timeout) {
var self = this,
date = new Date();
if (typeof(timeout) != 'number' || timeout < 1)
timeout = 10;
return self.callApply(timeout, true).then(function(rv) {
if (rv != 0)
return Promise.reject(rv);
var try_deadline = date.getTime() + 1000 * timeout;
var try_confirm = function() {
return self.callConfirm().then(function(rv) {
if (rv != 0) {
if (date.getTime() < try_deadline)
window.setTimeout(try_confirm, 250);
else
return Promise.reject(rv);
}
return rv;
});
};
window.setTimeout(try_confirm, 1000);
});
},
/**
* An UCI change record is a plain array containing the change operation
* name as first element, the affected section ID as second argument
* and an optional third and fourth argument whose meanings depend on
* the operation.
*
* @typedef {string[]} ChangeRecord
* @memberof LuCI.uci
*
* @property {string} 0
* The operation name - may be one of `add`, `set`, `remove`, `order`,
* `list-add`, `list-del` or `rename`.
*
* @property {string} 1
* The section ID targeted by the operation.
*
* @property {string} 2
* The meaning of the third element depends on the operation.
* - For `add` it is type of the section that has been added
* - For `set` it either is the option name if a fourth element exists,
* or the type of a named section which has been added when the change
* entry only contains three elements.
* - For `remove` it contains the name of the option that has been
* removed.
* - For `order` it specifies the new sort index of the section.
* - For `list-add` it contains the name of the list option a new value
* has been added to.
* - For `list-del` it contains the name of the list option a value has
* been removed from.
* - For `rename` it contains the name of the option that has been
* renamed if a fourth element exists, else it contains the new name
* a section has been renamed to if the change entry only contains
* three elements.
*
* @property {string} 4
* The meaning of the fourth element depends on the operation.
* - For `set` it is the value an option has been set to.
* - For `list-add` it is the new value that has been added to a
* list option.
* - For `rename` it is the new name of an option that has been
* renamed.
*/
/**
* Fetches uncommitted UCI changes from the remote `ubus` RPC api.
*
* @method
* @returns {Promise<Object<string, Array<LuCI.uci.ChangeRecord>>>}
* Returns a promise resolving to an object containing the configuration
* names as keys and arrays of related change records as values.
*/
changes: rpc.declare({
object: 'uci',
method: 'changes',
expect: { changes: { } }
})
});
|