2f96ef11bbb2b875d50e99024371d9e40f53acf9
7 var CBIJSONConfig
= Class
.extend({
8 __init__: function(data
) {
9 data
= Object
.assign({}, data
);
16 for (var sectiontype
in data
) {
17 if (!data
.hasOwnProperty(sectiontype
))
20 if (L
.isObject(data
[sectiontype
])) {
21 this.data
[sectiontype
] = Object
.assign(data
[sectiontype
], {
27 section_ids
.push(sectiontype
);
30 else if (Array
.isArray(data
[sectiontype
])) {
31 for (var i
= 0, index
= 0; i
< data
[sectiontype
].length
; i
++) {
32 var item
= data
[sectiontype
][i
],
35 if (!L
.isObject(item
))
38 if (typeof(item
['.name']) == 'string') {
43 name
= sectiontype
+ num_sections
;
47 if (!this.data
.hasOwnProperty(name
))
48 section_ids
.push(name
);
50 this.data
[name
] = Object
.assign(item
, {
51 '.index': num_sections
++,
52 '.anonymous': anonymous
,
60 section_ids
.sort(L
.bind(function(a
, b
) {
61 var indexA
= (this.data
[a
]['.index'] != null) ? +this.data
[a
]['.index'] : 9999,
62 indexB
= (this.data
[b
]['.index'] != null) ? +this.data
[b
]['.index'] : 9999;
65 return (indexA
- indexB
);
70 for (var i
= 0; i
< section_ids
.length
; i
++)
71 this.data
[section_ids
[i
]]['.index'] = i
;
75 return Promise
.resolve(this.data
);
79 return Promise
.resolve();
82 get: function(config
, section
, option
) {
87 return this.data
[section
];
89 if (!this.data
.hasOwnProperty(section
))
92 var value
= this.data
[section
][option
];
94 if (Array
.isArray(value
))
103 set: function(config
, section
, option
, value
) {
104 if (section
== null || option
== null || option
.charAt(0) == '.')
107 if (!this.data
.hasOwnProperty(section
))
111 delete this.data
[section
][option
];
112 else if (Array
.isArray(value
))
113 this.data
[section
][option
] = value
;
115 this.data
[section
][option
] = String(value
);
118 unset: function(config
, section
, option
) {
119 return this.set(config
, section
, option
, null);
122 sections: function(config
, sectiontype
, callback
) {
125 for (var section_id
in this.data
)
126 if (sectiontype
== null || this.data
[section_id
]['.type'] == sectiontype
)
127 rv
.push(this.data
[section_id
]);
129 rv
.sort(function(a
, b
) { return a
['.index'] - b
['.index'] });
131 if (typeof(callback
) == 'function')
132 for (var i
= 0; i
< rv
.length
; i
++)
133 callback
.call(this, rv
[i
], rv
[i
]['.name']);
138 add: function(config
, sectiontype
, sectionname
) {
139 var num_sections_type
= 0, next_index
= 0;
141 for (var name
in this.data
) {
142 num_sections_type
+= (this.data
[name
]['.type'] == sectiontype
);
143 next_index
= Math
.max(next_index
, this.data
[name
]['.index']);
146 var section_id
= sectionname
|| sectiontype
+ num_sections_type
;
148 if (!this.data
.hasOwnProperty(section_id
)) {
149 this.data
[section_id
] = {
151 '.type': sectiontype
,
152 '.anonymous': (sectionname
== null),
153 '.index': next_index
+ 1
160 remove: function(config
, section
) {
161 if (this.data
.hasOwnProperty(section
))
162 delete this.data
[section
];
165 resolveSID: function(config
, section_id
) {
169 move: function(config
, section_id1
, section_id2
, after
) {
170 return uci
.move.apply(this, [config
, section_id1
, section_id2
, after
]);
174 var CBINode
= Class
.extend({
175 __init__: function(title
, description
) {
176 this.title
= title
|| '';
177 this.description
= description
|| '';
181 append: function(obj
) {
182 this.children
.push(obj
);
186 var args
= arguments
;
187 this.children
.forEach(function(child
) {
188 child
.parse
.apply(child
, args
);
193 L
.error('InternalError', 'Not implemented');
196 loadChildren: function(/* ... */) {
199 if (Array
.isArray(this.children
))
200 for (var i
= 0; i
< this.children
.length
; i
++)
201 if (!this.children
[i
].disable
)
202 tasks
.push(this.children
[i
].load
.apply(this.children
[i
], arguments
));
204 return Promise
.all(tasks
);
207 renderChildren: function(tab_name
/*, ... */) {
211 if (Array
.isArray(this.children
))
212 for (var i
= 0; i
< this.children
.length
; i
++)
213 if (tab_name
=== null || this.children
[i
].tab
=== tab_name
)
214 if (!this.children
[i
].disable
)
215 tasks
.push(this.children
[i
].render
.apply(
216 this.children
[i
], this.varargs(arguments
, 1, index
++)));
218 return Promise
.all(tasks
);
221 stripTags: function(s
) {
222 if (typeof(s
) == 'string' && !s
.match(/[<>]/))
225 var x
= E('div', {}, s
);
226 return x
.textContent
|| x
.innerText
|| '';
229 titleFn: function(attr
/*, ... */) {
232 if (typeof(this[attr
]) == 'function')
233 s
= this[attr
].apply(this, this.varargs(arguments
, 1));
234 else if (typeof(this[attr
]) == 'string')
235 s
= (arguments
.length
> 1) ? ''.format
.apply(this[attr
], this.varargs(arguments
, 1)) : this[attr
];
238 s
= this.stripTags(String(s
)).trim();
240 if (s
== null || s
== '')
247 var CBIMap
= CBINode
.extend({
248 __init__: function(config
/*, ... */) {
249 this.super('__init__', this.varargs(arguments
, 1));
251 this.config
= config
;
252 this.parsechain
= [ config
];
256 findElements: function(/* ... */) {
259 if (arguments
.length
== 1)
261 else if (arguments
.length
== 2)
262 q
= '[%s="%s"]'.format(arguments
[0], arguments
[1]);
264 L
.error('InternalError', 'Expecting one or two arguments to findElements()');
266 return this.root
.querySelectorAll(q
);
269 findElement: function(/* ... */) {
270 var res
= this.findElements
.apply(this, arguments
);
271 return res
.length
? res
[0] : null;
274 chain: function(config
) {
275 if (this.parsechain
.indexOf(config
) == -1)
276 this.parsechain
.push(config
);
279 section: function(cbiClass
/*, ... */) {
280 if (!CBIAbstractSection
.isSubclass(cbiClass
))
281 L
.error('TypeError', 'Class must be a descendent of CBIAbstractSection');
283 var obj
= cbiClass
.instantiate(this.varargs(arguments
, 1, this));
289 return this.data
.load(this.parsechain
|| [ this.config
])
290 .then(this.loadChildren
.bind(this));
296 if (Array
.isArray(this.children
))
297 for (var i
= 0; i
< this.children
.length
; i
++)
298 tasks
.push(this.children
[i
].parse());
300 return Promise
.all(tasks
);
303 save: function(cb
, silent
) {
308 .then(this.data
.save
.bind(this.data
))
309 .then(this.load
.bind(this))
312 alert('Cannot save due to invalid values');
314 return Promise
.reject();
315 }).finally(this.renderContents
.bind(this));
319 return this.renderContents();
323 return this.load().then(this.renderContents
.bind(this));
326 renderContents: function() {
327 var mapEl
= this.root
|| (this.root
= E('div', {
328 'id': 'cbi-%s'.format(this.config
),
330 'cbi-dependency-check': L
.bind(this.checkDepends
, this)
333 L
.dom
.bindClassInstance(mapEl
, this);
335 return this.renderChildren(null).then(L
.bind(function(nodes
) {
336 var initialRender
= !mapEl
.firstChild
;
338 L
.dom
.content(mapEl
, null);
340 if (this.title
!= null && this.title
!= '')
341 mapEl
.appendChild(E('h2', { 'name': 'content' }, this.title
));
343 if (this.description
!= null && this.description
!= '')
344 mapEl
.appendChild(E('div', { 'class': 'cbi-map-descr' }, this.description
));
347 L
.dom
.append(mapEl
, E('div', { 'class': 'cbi-map-tabbed' }, nodes
));
349 L
.dom
.append(mapEl
, nodes
);
351 if (!initialRender
) {
352 mapEl
.classList
.remove('flash');
354 window
.setTimeout(function() {
355 mapEl
.classList
.add('flash');
361 var tabGroups
= mapEl
.querySelectorAll('.cbi-map-tabbed, .cbi-section-node-tabbed');
363 for (var i
= 0; i
< tabGroups
.length
; i
++)
364 ui
.tabs
.initTabGroup(tabGroups
[i
].childNodes
);
370 lookupOption: function(name
, section_id
, config_name
) {
371 var id
, elem
, sid
, inst
;
373 if (name
.indexOf('.') > -1)
374 id
= 'cbid.%s'.format(name
);
376 id
= 'cbid.%s.%s.%s'.format(config_name
|| this.config
, section_id
, name
);
378 elem
= this.findElement('data-field', id
);
379 sid
= elem
? id
.split(/\./)[2] : null;
380 inst
= elem
? L
.dom
.findClassInstance(elem
) : null;
382 return (inst
instanceof CBIAbstractValue
) ? [ inst
, sid
] : null;
385 checkDepends: function(ev
, n
) {
388 for (var i
= 0, s
= this.children
[0]; (s
= this.children
[i
]) != null; i
++)
389 if (s
.checkDepends(ev
, n
))
392 if (changed
&& (n
|| 0) < 10)
393 this.checkDepends(ev
, (n
|| 10) + 1);
395 ui
.tabs
.updateTabs(ev
, this.root
);
398 isDependencySatisfied: function(depends
, config_name
, section_id
) {
401 if (!Array
.isArray(depends
) || !depends
.length
)
404 for (var i
= 0; i
< depends
.length
; i
++) {
408 for (var dep
in depends
[i
]) {
409 if (dep
== '!reverse') {
412 else if (dep
== '!default') {
417 var res
= this.lookupOption(dep
, section_id
, config_name
),
418 val
= (res
&& res
[0].isActive(res
[1])) ? res
[0].formvalue(res
[1]) : null;
420 istat
= (istat
&& isEqual(val
, depends
[i
][dep
]));
432 var CBIJSONMap
= CBIMap
.extend({
433 __init__: function(data
/*, ... */) {
434 this.super('__init__', this.varargs(arguments
, 1, 'json'));
436 this.config
= 'json';
437 this.parsechain
= [ 'json' ];
438 this.data
= new CBIJSONConfig(data
);
442 var CBIAbstractSection
= CBINode
.extend({
443 __init__: function(map
, sectionType
/*, ... */) {
444 this.super('__init__', this.varargs(arguments
, 2));
446 this.sectiontype
= sectionType
;
448 this.config
= map
.config
;
450 this.optional
= true;
451 this.addremove
= false;
452 this.dynamic
= false;
455 cfgsections: function() {
456 L
.error('InternalError', 'Not implemented');
459 filter: function(section_id
) {
464 var section_ids
= this.cfgsections(),
467 if (Array
.isArray(this.children
))
468 for (var i
= 0; i
< section_ids
.length
; i
++)
469 tasks
.push(this.loadChildren(section_ids
[i
])
470 .then(Function
.prototype.bind
.call(function(section_id
, set_values
) {
471 for (var i
= 0; i
< set_values
.length
; i
++)
472 this.children
[i
].cfgvalue(section_id
, set_values
[i
]);
473 }, this, section_ids
[i
])));
475 return Promise
.all(tasks
);
479 var section_ids
= this.cfgsections(),
482 if (Array
.isArray(this.children
))
483 for (var i
= 0; i
< section_ids
.length
; i
++)
484 for (var j
= 0; j
< this.children
.length
; j
++)
485 tasks
.push(this.children
[j
].parse(section_ids
[i
]));
487 return Promise
.all(tasks
);
490 tab: function(name
, title
, description
) {
491 if (this.tabs
&& this.tabs
[name
])
492 throw 'Tab already declared';
497 description
: description
,
501 this.tabs
= this.tabs
|| [];
502 this.tabs
.push(entry
);
503 this.tabs
[name
] = entry
;
505 this.tab_names
= this.tab_names
|| [];
506 this.tab_names
.push(name
);
509 option: function(cbiClass
/*, ... */) {
510 if (!CBIAbstractValue
.isSubclass(cbiClass
))
511 throw L
.error('TypeError', 'Class must be a descendent of CBIAbstractValue');
513 var obj
= cbiClass
.instantiate(this.varargs(arguments
, 1, this.map
, this));
518 taboption: function(tabName
/*, ... */) {
519 if (!this.tabs
|| !this.tabs
[tabName
])
520 throw L
.error('ReferenceError', 'Associated tab not declared');
522 var obj
= this.option
.apply(this, this.varargs(arguments
, 1));
524 this.tabs
[tabName
].children
.push(obj
);
528 renderUCISection: function(section_id
) {
529 var renderTasks
= [];
532 return this.renderOptions(null, section_id
);
534 for (var i
= 0; i
< this.tab_names
.length
; i
++)
535 renderTasks
.push(this.renderOptions(this.tab_names
[i
], section_id
));
537 return Promise
.all(renderTasks
)
538 .then(this.renderTabContainers
.bind(this, section_id
));
541 renderTabContainers: function(section_id
, nodes
) {
542 var config_name
= this.uciconfig
|| this.map
.config
,
543 containerEls
= E([]);
545 for (var i
= 0; i
< nodes
.length
; i
++) {
546 var tab_name
= this.tab_names
[i
],
547 tab_data
= this.tabs
[tab_name
],
548 containerEl
= E('div', {
549 'id': 'container.%s.%s.%s'.format(config_name
, section_id
, tab_name
),
550 'data-tab': tab_name
,
551 'data-tab-title': tab_data
.title
,
552 'data-tab-active': tab_name
=== this.selected_tab
555 if (tab_data
.description
!= null && tab_data
.description
!= '')
556 containerEl
.appendChild(
557 E('div', { 'class': 'cbi-tab-descr' }, tab_data
.description
));
559 containerEl
.appendChild(nodes
[i
]);
560 containerEls
.appendChild(containerEl
);
566 renderOptions: function(tab_name
, section_id
) {
567 var in_table
= (this instanceof CBITableSection
);
568 return this.renderChildren(tab_name
, section_id
, in_table
).then(function(nodes
) {
569 var optionEls
= E([]);
570 for (var i
= 0; i
< nodes
.length
; i
++)
571 optionEls
.appendChild(nodes
[i
]);
576 checkDepends: function(ev
, n
) {
578 sids
= this.cfgsections();
580 for (var i
= 0, sid
= sids
[0]; (sid
= sids
[i
]) != null; i
++) {
581 for (var j
= 0, o
= this.children
[0]; (o
= this.children
[j
]) != null; j
++) {
582 var isActive
= o
.isActive(sid
),
583 isSatisified
= o
.checkDepends(sid
);
585 if (isActive
!= isSatisified
) {
586 o
.setActive(sid
, !isActive
);
591 o
.triggerValidation(sid
);
600 var isEqual = function(x
, y
) {
601 if (x
!= null && y
!= null && typeof(x
) != typeof(y
))
604 if ((x
== null && y
!= null) || (x
!= null && y
== null))
607 if (Array
.isArray(x
)) {
608 if (x
.length
!= y
.length
)
611 for (var i
= 0; i
< x
.length
; i
++)
612 if (!isEqual(x
[i
], y
[i
]))
615 else if (typeof(x
) == 'object') {
617 if (x
.hasOwnProperty(k
) && !y
.hasOwnProperty(k
))
620 if (!isEqual(x
[k
], y
[k
]))
625 if (y
.hasOwnProperty(k
) && !x
.hasOwnProperty(k
))
635 var CBIAbstractValue
= CBINode
.extend({
636 __init__: function(map
, section
, option
/*, ... */) {
637 this.super('__init__', this.varargs(arguments
, 3));
639 this.section
= section
;
640 this.option
= option
;
642 this.config
= map
.config
;
649 this.optional
= false;
652 depends: function(field
, value
) {
655 if (typeof(field
) === 'string')
656 deps
= {}, deps
[field
] = value
;
660 this.deps
.push(deps
);
663 transformDepList: function(section_id
, deplist
) {
664 var list
= deplist
|| this.deps
,
667 if (Array
.isArray(list
)) {
668 for (var i
= 0; i
< list
.length
; i
++) {
671 for (var k
in list
[i
]) {
672 if (list
[i
].hasOwnProperty(k
)) {
673 if (k
.charAt(0) === '!')
675 else if (k
.indexOf('.') !== -1)
676 dep
['cbid.%s'.format(k
)] = list
[i
][k
];
678 dep
['cbid.%s.%s.%s'.format(
679 this.uciconfig
|| this.section
.uciconfig
|| this.map
.config
,
680 this.ucisection
|| section_id
,
687 if (dep
.hasOwnProperty(k
)) {
698 transformChoices: function() {
699 if (!Array
.isArray(this.keylist
) || this.keylist
.length
== 0)
704 for (var i
= 0; i
< this.keylist
.length
; i
++)
705 choices
[this.keylist
[i
]] = this.vallist
[i
];
710 checkDepends: function(section_id
) {
711 var config_name
= this.uciconfig
|| this.section
.uciconfig
|| this.map
.config
,
712 active
= this.map
.isDependencySatisfied(this.deps
, config_name
, section_id
);
715 this.updateDefaultValue(section_id
);
720 updateDefaultValue: function(section_id
) {
721 if (!L
.isObject(this.defaults
))
724 var config_name
= this.uciconfig
|| this.section
.uciconfig
|| this.map
.config
,
725 default_defval
= null, satisified_defval
= null;
727 for (var value
in this.defaults
) {
728 if (!this.defaults
[value
] || this.defaults
[value
].length
== 0) {
729 default_defval
= value
;
732 else if (this.map
.isDependencySatisfied(this.defaults
[value
], config_name
, section_id
)) {
733 satisified_defval
= value
;
738 if (satisified_defval
== null)
739 satisified_defval
= default_defval
;
741 var node
= this.map
.findElement('id', this.cbid(section_id
));
742 if (node
&& node
.getAttribute('data-changed') != 'true' && satisified_defval
!= null)
743 L
.dom
.callClassMethod(node
, 'setValue', satisified_defval
);
745 this.default = satisified_defval
;
748 cbid: function(section_id
) {
749 if (section_id
== null)
750 L
.error('TypeError', 'Section ID required');
752 return 'cbid.%s.%s.%s'.format(
753 this.uciconfig
|| this.section
.uciconfig
|| this.map
.config
,
754 section_id
, this.option
);
757 load: function(section_id
) {
758 if (section_id
== null)
759 L
.error('TypeError', 'Section ID required');
761 return this.map
.data
.get(
762 this.uciconfig
|| this.section
.uciconfig
|| this.map
.config
,
763 this.ucisection
|| section_id
,
764 this.ucioption
|| this.option
);
767 cfgvalue: function(section_id
, set_value
) {
768 if (section_id
== null)
769 L
.error('TypeError', 'Section ID required');
771 if (arguments
.length
== 2) {
772 this.data
= this.data
|| {};
773 this.data
[section_id
] = set_value
;
776 return this.data
? this.data
[section_id
] : null;
779 formvalue: function(section_id
) {
780 var node
= this.map
.findElement('id', this.cbid(section_id
));
781 return node
? L
.dom
.callClassMethod(node
, 'getValue') : null;
784 textvalue: function(section_id
) {
785 var cval
= this.cfgvalue(section_id
);
790 return (cval
!= null) ? '%h'.format(cval
) : null;
793 validate: function(section_id
, value
) {
797 isValid: function(section_id
) {
798 var node
= this.map
.findElement('id', this.cbid(section_id
));
799 return node
? L
.dom
.callClassMethod(node
, 'isValid') : true;
802 isActive: function(section_id
) {
803 var field
= this.map
.findElement('data-field', this.cbid(section_id
));
804 return (field
!= null && !field
.classList
.contains('hidden'));
807 setActive: function(section_id
, active
) {
808 var field
= this.map
.findElement('data-field', this.cbid(section_id
));
810 if (field
&& field
.classList
.contains('hidden') == active
) {
811 field
.classList
[active
? 'remove' : 'add']('hidden');
818 triggerValidation: function(section_id
) {
819 var node
= this.map
.findElement('id', this.cbid(section_id
));
820 return node
? L
.dom
.callClassMethod(node
, 'triggerValidation') : true;
823 parse: function(section_id
) {
824 var active
= this.isActive(section_id
),
825 cval
= this.cfgvalue(section_id
),
826 fval
= active
? this.formvalue(section_id
) : null;
828 if (active
&& !this.isValid(section_id
))
829 return Promise
.reject();
831 if (fval
!= '' && fval
!= null) {
832 if (this.forcewrite
|| !isEqual(cval
, fval
))
833 return Promise
.resolve(this.write(section_id
, fval
));
836 if (this.rmempty
|| this.optional
) {
837 return Promise
.resolve(this.remove(section_id
));
839 else if (!isEqual(cval
, fval
)) {
840 console
.log('This should have been catched by isValid()');
841 return Promise
.reject();
845 return Promise
.resolve();
848 write: function(section_id
, formvalue
) {
849 return this.map
.data
.set(
850 this.uciconfig
|| this.section
.uciconfig
|| this.map
.config
,
851 this.ucisection
|| section_id
,
852 this.ucioption
|| this.option
,
856 remove: function(section_id
) {
857 return this.map
.data
.unset(
858 this.uciconfig
|| this.section
.uciconfig
|| this.map
.config
,
859 this.ucisection
|| section_id
,
860 this.ucioption
|| this.option
);
864 var CBITypedSection
= CBIAbstractSection
.extend({
865 __name__
: 'CBI.TypedSection',
867 cfgsections: function() {
868 return this.map
.data
.sections(this.uciconfig
|| this.map
.config
, this.sectiontype
)
869 .map(function(s
) { return s
['.name'] })
870 .filter(L
.bind(this.filter
, this));
873 handleAdd: function(ev
, name
) {
874 var config_name
= this.uciconfig
|| this.map
.config
;
876 this.map
.data
.add(config_name
, this.sectiontype
, name
);
877 return this.map
.save(null, true);
880 handleRemove: function(section_id
, ev
) {
881 var config_name
= this.uciconfig
|| this.map
.config
;
883 this.map
.data
.remove(config_name
, section_id
);
884 return this.map
.save(null, true);
887 renderSectionAdd: function(extra_class
) {
891 var createEl
= E('div', { 'class': 'cbi-section-create' }),
892 config_name
= this.uciconfig
|| this.map
.config
,
893 btn_title
= this.titleFn('addbtntitle');
895 if (extra_class
!= null)
896 createEl
.classList
.add(extra_class
);
898 if (this.anonymous
) {
899 createEl
.appendChild(E('button', {
900 'class': 'cbi-button cbi-button-add',
901 'title': btn_title
|| _('Add'),
902 'click': L
.ui
.createHandlerFn(this, 'handleAdd')
903 }, [ btn_title
|| _('Add') ]));
906 var nameEl
= E('input', {
908 'class': 'cbi-section-create-name'
911 L
.dom
.append(createEl
, [
912 E('div', {}, nameEl
),
914 'class': 'cbi-button cbi-button-add',
916 'value': btn_title
|| _('Add'),
917 'title': btn_title
|| _('Add'),
918 'click': L
.ui
.createHandlerFn(this, function(ev
) {
919 if (nameEl
.classList
.contains('cbi-input-invalid'))
922 return this.handleAdd(ev
, nameEl
.value
);
927 ui
.addValidator(nameEl
, 'uciname', true, 'blur', 'keyup');
933 renderSectionPlaceholder: function() {
935 E('em', _('This section contains no values yet')),
940 renderContents: function(cfgsections
, nodes
) {
941 var section_id
= null,
942 config_name
= this.uciconfig
|| this.map
.config
,
943 sectionEl
= E('div', {
944 'id': 'cbi-%s-%s'.format(config_name
, this.sectiontype
),
945 'class': 'cbi-section',
946 'data-tab': this.map
.tabbed
? this.sectiontype
: null,
947 'data-tab-title': this.map
.tabbed
? this.title
|| this.sectiontype
: null
950 if (this.title
!= null && this.title
!= '')
951 sectionEl
.appendChild(E('legend', {}, this.title
));
953 if (this.description
!= null && this.description
!= '')
954 sectionEl
.appendChild(E('div', { 'class': 'cbi-section-descr' }, this.description
));
956 for (var i
= 0; i
< nodes
.length
; i
++) {
957 if (this.addremove
) {
958 sectionEl
.appendChild(
959 E('div', { 'class': 'cbi-section-remove right' },
961 'class': 'cbi-button',
962 'name': 'cbi.rts.%s.%s'.format(config_name
, cfgsections
[i
]),
963 'data-section-id': cfgsections
[i
],
964 'click': L
.ui
.createHandlerFn(this, 'handleRemove', cfgsections
[i
])
965 }, [ _('Delete') ])));
969 sectionEl
.appendChild(E('h3', cfgsections
[i
].toUpperCase()));
971 sectionEl
.appendChild(E('div', {
972 'id': 'cbi-%s-%s'.format(config_name
, cfgsections
[i
]),
974 ? 'cbi-section-node cbi-section-node-tabbed' : 'cbi-section-node',
975 'data-section-id': cfgsections
[i
]
979 if (nodes
.length
== 0)
980 sectionEl
.appendChild(this.renderSectionPlaceholder());
982 sectionEl
.appendChild(this.renderSectionAdd());
984 L
.dom
.bindClassInstance(sectionEl
, this);
990 var cfgsections
= this.cfgsections(),
993 for (var i
= 0; i
< cfgsections
.length
; i
++)
994 renderTasks
.push(this.renderUCISection(cfgsections
[i
]));
996 return Promise
.all(renderTasks
).then(this.renderContents
.bind(this, cfgsections
));
1000 var CBITableSection
= CBITypedSection
.extend({
1001 __name__
: 'CBI.TableSection',
1004 throw 'Tabs are not supported by TableSection';
1007 renderContents: function(cfgsections
, nodes
) {
1008 var section_id
= null,
1009 config_name
= this.uciconfig
|| this.map
.config
,
1010 max_cols
= isNaN(this.max_cols
) ? this.children
.length
: this.max_cols
,
1011 has_more
= max_cols
< this.children
.length
,
1012 sectionEl
= E('div', {
1013 'id': 'cbi-%s-%s'.format(config_name
, this.sectiontype
),
1014 'class': 'cbi-section cbi-tblsection',
1015 'data-tab': this.map
.tabbed
? this.sectiontype
: null,
1016 'data-tab-title': this.map
.tabbed
? this.title
|| this.sectiontype
: null
1018 tableEl
= E('div', {
1019 'class': 'table cbi-section-table'
1022 if (this.title
!= null && this.title
!= '')
1023 sectionEl
.appendChild(E('h3', {}, this.title
));
1025 if (this.description
!= null && this.description
!= '')
1026 sectionEl
.appendChild(E('div', { 'class': 'cbi-section-descr' }, this.description
));
1028 tableEl
.appendChild(this.renderHeaderRows(max_cols
));
1030 for (var i
= 0; i
< nodes
.length
; i
++) {
1031 var sectionname
= this.titleFn('sectiontitle', cfgsections
[i
]);
1033 var trEl
= E('div', {
1034 'id': 'cbi-%s-%s'.format(config_name
, cfgsections
[i
]),
1035 'class': 'tr cbi-section-table-row',
1036 'data-sid': cfgsections
[i
],
1037 'draggable': this.sortable
? true : null,
1038 'mousedown': this.sortable
? L
.bind(this.handleDragInit
, this) : null,
1039 'dragstart': this.sortable
? L
.bind(this.handleDragStart
, this) : null,
1040 'dragover': this.sortable
? L
.bind(this.handleDragOver
, this) : null,
1041 'dragenter': this.sortable
? L
.bind(this.handleDragEnter
, this) : null,
1042 'dragleave': this.sortable
? L
.bind(this.handleDragLeave
, this) : null,
1043 'dragend': this.sortable
? L
.bind(this.handleDragEnd
, this) : null,
1044 'drop': this.sortable
? L
.bind(this.handleDrop
, this) : null,
1045 'data-title': (sectionname
&& (!this.anonymous
|| this.sectiontitle
)) ? sectionname
: null,
1046 'data-section-id': cfgsections
[i
]
1049 if (this.extedit
|| this.rowcolors
)
1050 trEl
.classList
.add(!(tableEl
.childNodes
.length
% 2)
1051 ? 'cbi-rowstyle-1' : 'cbi-rowstyle-2');
1053 for (var j
= 0; j
< max_cols
&& nodes
[i
].firstChild
; j
++)
1054 trEl
.appendChild(nodes
[i
].firstChild
);
1056 trEl
.appendChild(this.renderRowActions(cfgsections
[i
], has_more
? _('More…') : null));
1057 tableEl
.appendChild(trEl
);
1060 if (nodes
.length
== 0)
1061 tableEl
.appendChild(E('div', { 'class': 'tr cbi-section-table-row placeholder' },
1062 E('div', { 'class': 'td' },
1063 E('em', {}, _('This section contains no values yet')))));
1065 sectionEl
.appendChild(tableEl
);
1067 sectionEl
.appendChild(this.renderSectionAdd('cbi-tblsection-create'));
1069 L
.dom
.bindClassInstance(sectionEl
, this);
1074 renderHeaderRows: function(max_cols
) {
1075 var has_titles
= false,
1076 has_descriptions
= false,
1077 anon_class
= (!this.anonymous
|| this.sectiontitle
) ? 'named' : 'anonymous',
1080 for (var i
= 0, opt
; i
< max_cols
&& (opt
= this.children
[i
]) != null; i
++) {
1081 if (opt
.optional
|| opt
.modalonly
)
1084 has_titles
= has_titles
|| !!opt
.title
;
1085 has_descriptions
= has_descriptions
|| !!opt
.description
;
1089 var trEl
= E('div', {
1090 'class': 'tr cbi-section-table-titles ' + anon_class
,
1091 'data-title': (!this.anonymous
|| this.sectiontitle
) ? _('Name') : null
1094 for (var i
= 0, opt
; i
< max_cols
&& (opt
= this.children
[i
]) != null; i
++) {
1095 if (opt
.optional
|| opt
.modalonly
)
1098 trEl
.appendChild(E('div', {
1099 'class': 'th cbi-section-table-cell',
1100 'data-type': opt
.__name__
1103 if (opt
.width
!= null)
1104 trEl
.lastElementChild
.style
.width
=
1105 (typeof(opt
.width
) == 'number') ? opt
.width
+'px' : opt
.width
;
1108 trEl
.lastElementChild
.appendChild(E('a', {
1109 'href': opt
.titleref
,
1110 'class': 'cbi-title-ref',
1111 'title': this.titledesc
|| _('Go to relevant configuration page')
1114 L
.dom
.content(trEl
.lastElementChild
, opt
.title
);
1117 if (this.sortable
|| this.extedit
|| this.addremove
|| has_more
)
1118 trEl
.appendChild(E('div', {
1119 'class': 'th cbi-section-table-cell cbi-section-actions'
1122 trEls
.appendChild(trEl
);
1125 if (has_descriptions
) {
1126 var trEl
= E('div', {
1127 'class': 'tr cbi-section-table-descr ' + anon_class
1130 for (var i
= 0, opt
; i
< max_cols
&& (opt
= this.children
[i
]) != null; i
++) {
1131 if (opt
.optional
|| opt
.modalonly
)
1134 trEl
.appendChild(E('div', {
1135 'class': 'th cbi-section-table-cell',
1136 'data-type': opt
.__name__
1137 }, opt
.description
));
1139 if (opt
.width
!= null)
1140 trEl
.lastElementChild
.style
.width
=
1141 (typeof(opt
.width
) == 'number') ? opt
.width
+'px' : opt
.width
;
1144 if (this.sortable
|| this.extedit
|| this.addremove
|| has_more
)
1145 trEl
.appendChild(E('div', {
1146 'class': 'th cbi-section-table-cell cbi-section-actions'
1149 trEls
.appendChild(trEl
);
1155 renderRowActions: function(section_id
, more_label
) {
1156 var config_name
= this.uciconfig
|| this.map
.config
;
1158 if (!this.sortable
&& !this.extedit
&& !this.addremove
&& !more_label
)
1161 var tdEl
= E('div', {
1162 'class': 'td cbi-section-table-cell nowrap cbi-section-actions'
1165 if (this.sortable
) {
1166 L
.dom
.append(tdEl
.lastElementChild
, [
1168 'title': _('Drag to reorder'),
1169 'class': 'cbi-button drag-handle center',
1170 'style': 'cursor:move'
1178 if (typeof(this.extedit
) == 'function')
1179 evFn
= L
.bind(this.extedit
, this);
1180 else if (typeof(this.extedit
) == 'string')
1181 evFn
= L
.bind(function(sid
, ev
) {
1182 location
.href
= this.extedit
.format(sid
);
1183 }, this, section_id
);
1185 L
.dom
.append(tdEl
.lastElementChild
,
1188 'class': 'cbi-button cbi-button-edit',
1195 L
.dom
.append(tdEl
.lastElementChild
,
1197 'title': more_label
,
1198 'class': 'cbi-button cbi-button-edit',
1199 'click': L
.ui
.createHandlerFn(this, 'renderMoreOptionsModal', section_id
)
1204 if (this.addremove
) {
1205 var btn_title
= this.titleFn('removebtntitle', section_id
);
1207 L
.dom
.append(tdEl
.lastElementChild
,
1209 'title': btn_title
|| _('Delete'),
1210 'class': 'cbi-button cbi-button-remove',
1211 'click': L
.ui
.createHandlerFn(this, function(sid
, ev
) {
1212 this.map
.data
.remove(config_name
, sid
);
1213 return this.map
.save(null, true);
1215 }, [ btn_title
|| _('Delete') ])
1222 handleDragInit: function(ev
) {
1223 scope
.dragState
= { node
: ev
.target
};
1226 handleDragStart: function(ev
) {
1227 if (!scope
.dragState
|| !scope
.dragState
.node
.classList
.contains('drag-handle')) {
1228 scope
.dragState
= null;
1229 ev
.preventDefault();
1233 scope
.dragState
.node
= L
.dom
.parent(scope
.dragState
.node
, '.tr');
1234 ev
.dataTransfer
.setData('text', 'drag');
1235 ev
.target
.style
.opacity
= 0.4;
1238 handleDragOver: function(ev
) {
1239 var n
= scope
.dragState
.targetNode
,
1240 r
= scope
.dragState
.rect
,
1241 t
= r
.top
+ r
.height
/ 2;
1243 if (ev
.clientY
<= t
) {
1244 n
.classList
.remove('drag-over-below');
1245 n
.classList
.add('drag-over-above');
1248 n
.classList
.remove('drag-over-above');
1249 n
.classList
.add('drag-over-below');
1252 ev
.dataTransfer
.dropEffect
= 'move';
1253 ev
.preventDefault();
1257 handleDragEnter: function(ev
) {
1258 scope
.dragState
.rect
= ev
.currentTarget
.getBoundingClientRect();
1259 scope
.dragState
.targetNode
= ev
.currentTarget
;
1262 handleDragLeave: function(ev
) {
1263 ev
.currentTarget
.classList
.remove('drag-over-above');
1264 ev
.currentTarget
.classList
.remove('drag-over-below');
1267 handleDragEnd: function(ev
) {
1270 n
.style
.opacity
= '';
1271 n
.classList
.add('flash');
1272 n
.parentNode
.querySelectorAll('.drag-over-above, .drag-over-below')
1273 .forEach(function(tr
) {
1274 tr
.classList
.remove('drag-over-above');
1275 tr
.classList
.remove('drag-over-below');
1279 handleDrop: function(ev
) {
1280 var s
= scope
.dragState
;
1282 if (s
.node
&& s
.targetNode
) {
1283 var config_name
= this.uciconfig
|| this.map
.config
,
1284 ref_node
= s
.targetNode
,
1287 if (ref_node
.classList
.contains('drag-over-below')) {
1288 ref_node
= ref_node
.nextElementSibling
;
1292 var sid1
= s
.node
.getAttribute('data-sid'),
1293 sid2
= s
.targetNode
.getAttribute('data-sid');
1295 s
.node
.parentNode
.insertBefore(s
.node
, ref_node
);
1296 this.map
.data
.move(config_name
, sid1
, sid2
, after
);
1299 scope
.dragState
= null;
1300 ev
.target
.style
.opacity
= '';
1301 ev
.stopPropagation();
1302 ev
.preventDefault();
1306 handleModalCancel: function(modalMap
, ev
) {
1307 return Promise
.resolve(L
.ui
.hideModal());
1310 handleModalSave: function(modalMap
, ev
) {
1311 return modalMap
.save()
1312 .then(L
.bind(this.map
.load
, this.map
))
1313 .then(L
.bind(this.map
.reset
, this.map
))
1314 .then(L
.ui
.hideModal
)
1315 .catch(function() {});
1318 addModalOptions: function(modalSection
, section_id
, ev
) {
1322 renderMoreOptionsModal: function(section_id
, ev
) {
1323 var parent
= this.map
,
1324 title
= parent
.title
,
1326 m
= new CBIMap(this.map
.config
, null, null),
1327 s
= m
.section(CBINamedSection
, section_id
, this.sectiontype
);
1332 s
.tab_names
= this.tab_names
;
1334 if ((name
= this.titleFn('modaltitle', section_id
)) != null)
1336 else if ((name
= this.titleFn('sectiontitle', section_id
)) != null)
1337 title
= '%s - %s'.format(parent
.title
, name
);
1338 else if (!this.anonymous
)
1339 title
= '%s - %s'.format(parent
.title
, section_id
);
1341 for (var i
= 0; i
< this.children
.length
; i
++) {
1342 var o1
= this.children
[i
];
1344 if (o1
.modalonly
=== false)
1347 var o2
= s
.option(o1
.constructor, o1
.option
, o1
.title
, o1
.description
);
1350 if (!o1
.hasOwnProperty(k
))
1367 return Promise
.resolve(this.addModalOptions(s
, section_id
, ev
)).then(L
.bind(m
.render
, m
)).then(L
.bind(function(nodes
) {
1368 L
.ui
.showModal(title
, [
1370 E('div', { 'class': 'right' }, [
1373 'click': L
.ui
.createHandlerFn(this, 'handleModalCancel', m
)
1374 }, [ _('Dismiss') ]), ' ',
1376 'class': 'cbi-button cbi-button-positive important',
1377 'click': L
.ui
.createHandlerFn(this, 'handleModalSave', m
)
1381 }, this)).catch(L
.error
);
1385 var CBIGridSection
= CBITableSection
.extend({
1386 tab: function(name
, title
, description
) {
1387 CBIAbstractSection
.prototype.tab
.call(this, name
, title
, description
);
1390 handleAdd: function(ev
) {
1391 var config_name
= this.uciconfig
|| this.map
.config
,
1392 section_id
= this.map
.data
.add(config_name
, this.sectiontype
);
1394 this.addedSection
= section_id
;
1395 return this.renderMoreOptionsModal(section_id
);
1398 handleModalSave: function(/* ... */) {
1399 return this.super('handleModalSave', arguments
)
1400 .then(L
.bind(function() { this.addedSection
= null }, this));
1403 handleModalCancel: function(/* ... */) {
1404 var config_name
= this.uciconfig
|| this.map
.config
;
1406 if (this.addedSection
!= null) {
1407 this.map
.data
.remove(config_name
, this.addedSection
);
1408 this.addedSection
= null;
1411 return this.super('handleModalCancel', arguments
);
1414 renderUCISection: function(section_id
) {
1415 return this.renderOptions(null, section_id
);
1418 renderChildren: function(tab_name
, section_id
, in_table
) {
1419 var tasks
= [], index
= 0;
1421 for (var i
= 0, opt
; (opt
= this.children
[i
]) != null; i
++) {
1422 if (opt
.disable
|| opt
.modalonly
)
1426 tasks
.push(opt
.render(index
++, section_id
, in_table
));
1428 tasks
.push(this.renderTextValue(section_id
, opt
));
1431 return Promise
.all(tasks
);
1434 renderTextValue: function(section_id
, opt
) {
1435 var title
= this.stripTags(opt
.title
).trim(),
1436 descr
= this.stripTags(opt
.description
).trim(),
1437 value
= opt
.textvalue(section_id
);
1440 'class': 'td cbi-value-field',
1441 'data-title': (title
!= '') ? title
: opt
.option
,
1442 'data-description': (descr
!= '') ? descr
: null,
1443 'data-name': opt
.option
,
1444 'data-type': opt
.typename
|| opt
.__name__
1445 }, (value
!= null) ? value
: E('em', _('none')));
1448 renderRowActions: function(section_id
) {
1449 return this.super('renderRowActions', [ section_id
, _('Edit') ]);
1453 var section_ids
= this.cfgsections(),
1456 if (Array
.isArray(this.children
)) {
1457 for (var i
= 0; i
< section_ids
.length
; i
++) {
1458 for (var j
= 0; j
< this.children
.length
; j
++) {
1459 if (!this.children
[j
].editable
|| this.children
[j
].modalonly
)
1462 tasks
.push(this.children
[j
].parse(section_ids
[i
]));
1467 return Promise
.all(tasks
);
1471 var CBINamedSection
= CBIAbstractSection
.extend({
1472 __name__
: 'CBI.NamedSection',
1473 __init__: function(map
, section_id
/*, ... */) {
1474 this.super('__init__', this.varargs(arguments
, 2, map
));
1476 this.section
= section_id
;
1479 cfgsections: function() {
1480 return [ this.section
];
1483 handleAdd: function(ev
) {
1484 var section_id
= this.section
,
1485 config_name
= this.uciconfig
|| this.map
.config
;
1487 this.map
.data
.add(config_name
, this.sectiontype
, section_id
);
1488 return this.map
.save(null, true);
1491 handleRemove: function(ev
) {
1492 var section_id
= this.section
,
1493 config_name
= this.uciconfig
|| this.map
.config
;
1495 this.map
.data
.remove(config_name
, section_id
);
1496 return this.map
.save(null, true);
1499 renderContents: function(data
) {
1500 var ucidata
= data
[0], nodes
= data
[1],
1501 section_id
= this.section
,
1502 config_name
= this.uciconfig
|| this.map
.config
,
1503 sectionEl
= E('div', {
1504 'id': ucidata
? null : 'cbi-%s-%s'.format(config_name
, section_id
),
1505 'class': 'cbi-section',
1506 'data-tab': this.map
.tabbed
? this.sectiontype
: null,
1507 'data-tab-title': this.map
.tabbed
? this.title
|| this.sectiontype
: null
1510 if (typeof(this.title
) === 'string' && this.title
!== '')
1511 sectionEl
.appendChild(E('legend', {}, this.title
));
1513 if (typeof(this.description
) === 'string' && this.description
!== '')
1514 sectionEl
.appendChild(E('div', { 'class': 'cbi-section-descr' }, this.description
));
1517 if (this.addremove
) {
1518 sectionEl
.appendChild(
1519 E('div', { 'class': 'cbi-section-remove right' },
1521 'class': 'cbi-button',
1522 'click': L
.ui
.createHandlerFn(this, 'handleRemove')
1523 }, [ _('Delete') ])));
1526 sectionEl
.appendChild(E('div', {
1527 'id': 'cbi-%s-%s'.format(config_name
, section_id
),
1529 ? 'cbi-section-node cbi-section-node-tabbed' : 'cbi-section-node',
1530 'data-section-id': section_id
1533 else if (this.addremove
) {
1534 sectionEl
.appendChild(
1536 'class': 'cbi-button cbi-button-add',
1537 'click': L
.ui
.createHandlerFn(this, 'handleAdd')
1541 L
.dom
.bindClassInstance(sectionEl
, this);
1546 render: function() {
1547 var config_name
= this.uciconfig
|| this.map
.config
,
1548 section_id
= this.section
;
1550 return Promise
.all([
1551 this.map
.data
.get(config_name
, section_id
),
1552 this.renderUCISection(section_id
)
1553 ]).then(this.renderContents
.bind(this));
1557 var CBIValue
= CBIAbstractValue
.extend({
1558 __name__
: 'CBI.Value',
1560 value: function(key
, val
) {
1561 this.keylist
= this.keylist
|| [];
1562 this.keylist
.push(String(key
));
1564 this.vallist
= this.vallist
|| [];
1565 this.vallist
.push(String(val
!= null ? val
: key
));
1568 render: function(option_index
, section_id
, in_table
) {
1569 return Promise
.resolve(this.cfgvalue(section_id
))
1570 .then(this.renderWidget
.bind(this, section_id
, option_index
))
1571 .then(this.renderFrame
.bind(this, section_id
, in_table
, option_index
));
1574 renderFrame: function(section_id
, in_table
, option_index
, nodes
) {
1575 var config_name
= this.uciconfig
|| this.section
.uciconfig
|| this.map
.config
,
1576 depend_list
= this.transformDepList(section_id
),
1580 optionEl
= E('div', {
1581 'class': 'td cbi-value-field',
1582 'data-title': this.stripTags(this.title
).trim(),
1583 'data-description': this.stripTags(this.description
).trim(),
1584 'data-name': this.option
,
1585 'data-type': this.typename
|| (this.template
? this.template
.replace(/^.+\//, '') : null) || this.__name__
1587 'id': 'cbi-%s-%s-%s'.format(config_name
, section_id
, this.option
),
1588 'data-index': option_index
,
1589 'data-depends': depend_list
,
1590 'data-field': this.cbid(section_id
)
1594 optionEl
= E('div', {
1595 'class': 'cbi-value',
1596 'id': 'cbi-%s-%s-%s'.format(config_name
, section_id
, this.option
),
1597 'data-index': option_index
,
1598 'data-depends': depend_list
,
1599 'data-field': this.cbid(section_id
),
1600 'data-name': this.option
,
1601 'data-type': this.typename
|| (this.template
? this.template
.replace(/^.+\//, '') : null) || this.__name__
1604 if (this.last_child
)
1605 optionEl
.classList
.add('cbi-value-last');
1607 if (typeof(this.title
) === 'string' && this.title
!== '') {
1608 optionEl
.appendChild(E('label', {
1609 'class': 'cbi-value-title',
1610 'for': 'widget.cbid.%s.%s.%s'.format(config_name
, section_id
, this.option
)
1612 this.titleref
? E('a', {
1613 'class': 'cbi-title-ref',
1614 'href': this.titleref
,
1615 'title': this.titledesc
|| _('Go to relevant configuration page')
1616 }, this.title
) : this.title
));
1618 optionEl
.appendChild(E('div', { 'class': 'cbi-value-field' }));
1623 (optionEl
.lastChild
|| optionEl
).appendChild(nodes
);
1625 if (!in_table
&& typeof(this.description
) === 'string' && this.description
!== '')
1626 L
.dom
.append(optionEl
.lastChild
|| optionEl
,
1627 E('div', { 'class': 'cbi-value-description' }, this.description
));
1629 if (depend_list
&& depend_list
.length
)
1630 optionEl
.classList
.add('hidden');
1632 optionEl
.addEventListener('widget-change',
1633 L
.bind(this.map
.checkDepends
, this.map
));
1635 L
.dom
.bindClassInstance(optionEl
, this);
1640 renderWidget: function(section_id
, option_index
, cfgvalue
) {
1641 var value
= (cfgvalue
!= null) ? cfgvalue
: this.default,
1642 choices
= this.transformChoices(),
1646 var placeholder
= (this.optional
|| this.rmempty
)
1647 ? E('em', _('unspecified')) : _('-- Please choose --');
1649 widget
= new ui
.Combobox(Array
.isArray(value
) ? value
.join(' ') : value
, choices
, {
1650 id
: this.cbid(section_id
),
1652 optional
: this.optional
|| this.rmempty
,
1653 datatype
: this.datatype
,
1654 select_placeholder
: this.placeholder
|| placeholder
,
1655 validate
: L
.bind(this.validate
, this, section_id
)
1659 widget
= new ui
.Textfield(Array
.isArray(value
) ? value
.join(' ') : value
, {
1660 id
: this.cbid(section_id
),
1661 password
: this.password
,
1662 optional
: this.optional
|| this.rmempty
,
1663 datatype
: this.datatype
,
1664 placeholder
: this.placeholder
,
1665 validate
: L
.bind(this.validate
, this, section_id
)
1669 return widget
.render();
1673 var CBIDynamicList
= CBIValue
.extend({
1674 __name__
: 'CBI.DynamicList',
1676 renderWidget: function(section_id
, option_index
, cfgvalue
) {
1677 var value
= (cfgvalue
!= null) ? cfgvalue
: this.default,
1678 choices
= this.transformChoices(),
1679 items
= L
.toArray(value
);
1681 var widget
= new ui
.DynamicList(items
, choices
, {
1682 id
: this.cbid(section_id
),
1684 optional
: this.optional
|| this.rmempty
,
1685 datatype
: this.datatype
,
1686 placeholder
: this.placeholder
,
1687 validate
: L
.bind(this.validate
, this, section_id
)
1690 return widget
.render();
1694 var CBIListValue
= CBIValue
.extend({
1695 __name__
: 'CBI.ListValue',
1697 __init__: function() {
1698 this.super('__init__', arguments
);
1699 this.widget
= 'select';
1703 renderWidget: function(section_id
, option_index
, cfgvalue
) {
1704 var choices
= this.transformChoices();
1705 var widget
= new ui
.Select((cfgvalue
!= null) ? cfgvalue
: this.default, choices
, {
1706 id
: this.cbid(section_id
),
1709 optional
: this.optional
,
1710 placeholder
: this.placeholder
,
1711 validate
: L
.bind(this.validate
, this, section_id
)
1714 return widget
.render();
1718 var CBIFlagValue
= CBIValue
.extend({
1719 __name__
: 'CBI.FlagValue',
1721 __init__: function() {
1722 this.super('__init__', arguments
);
1725 this.disabled
= '0';
1726 this.default = this.disabled
;
1729 renderWidget: function(section_id
, option_index
, cfgvalue
) {
1730 var widget
= new ui
.Checkbox((cfgvalue
!= null) ? cfgvalue
: this.default, {
1731 id
: this.cbid(section_id
),
1732 value_enabled
: this.enabled
,
1733 value_disabled
: this.disabled
,
1734 validate
: L
.bind(this.validate
, this, section_id
)
1737 return widget
.render();
1740 formvalue: function(section_id
) {
1741 var node
= this.map
.findElement('id', this.cbid(section_id
)),
1742 checked
= node
? L
.dom
.callClassMethod(node
, 'isChecked') : false;
1744 return checked
? this.enabled
: this.disabled
;
1747 textvalue: function(section_id
) {
1748 var cval
= this.cfgvalue(section_id
);
1751 cval
= this.default;
1753 return (cval
== this.enabled
) ? _('Yes') : _('No');
1756 parse: function(section_id
) {
1757 if (this.isActive(section_id
)) {
1758 var fval
= this.formvalue(section_id
);
1760 if (!this.isValid(section_id
))
1761 return Promise
.reject();
1763 if (fval
== this.default && (this.optional
|| this.rmempty
))
1764 return Promise
.resolve(this.remove(section_id
));
1766 return Promise
.resolve(this.write(section_id
, fval
));
1769 return Promise
.resolve(this.remove(section_id
));
1774 var CBIMultiValue
= CBIDynamicList
.extend({
1775 __name__
: 'CBI.MultiValue',
1777 __init__: function() {
1778 this.super('__init__', arguments
);
1779 this.placeholder
= _('-- Please choose --');
1782 renderWidget: function(section_id
, option_index
, cfgvalue
) {
1783 var value
= (cfgvalue
!= null) ? cfgvalue
: this.default,
1784 choices
= this.transformChoices();
1786 var widget
= new ui
.Dropdown(L
.toArray(value
), choices
, {
1787 id
: this.cbid(section_id
),
1790 optional
: this.optional
|| this.rmempty
,
1791 select_placeholder
: this.placeholder
,
1792 display_items
: this.display_size
|| this.size
|| 3,
1793 dropdown_items
: this.dropdown_size
|| this.size
|| -1,
1794 validate
: L
.bind(this.validate
, this, section_id
)
1797 return widget
.render();
1801 var CBITextValue
= CBIValue
.extend({
1802 __name__
: 'CBI.TextValue',
1806 renderWidget: function(section_id
, option_index
, cfgvalue
) {
1807 var value
= (cfgvalue
!= null) ? cfgvalue
: this.default;
1809 var widget
= new ui
.Textarea(value
, {
1810 id
: this.cbid(section_id
),
1811 optional
: this.optional
|| this.rmempty
,
1812 placeholder
: this.placeholder
,
1813 monospace
: this.monospace
,
1817 validate
: L
.bind(this.validate
, this, section_id
)
1820 return widget
.render();
1824 var CBIDummyValue
= CBIValue
.extend({
1825 __name__
: 'CBI.DummyValue',
1827 renderWidget: function(section_id
, option_index
, cfgvalue
) {
1828 var value
= (cfgvalue
!= null) ? cfgvalue
: this.default,
1829 hiddenEl
= new ui
.Hiddenfield(value
, { id
: this.cbid(section_id
) }),
1830 outputEl
= E('div');
1833 outputEl
.appendChild(E('a', { 'href': this.href
}));
1835 L
.dom
.append(outputEl
.lastChild
|| outputEl
,
1836 this.rawhtml
? value
: [ value
]);
1844 remove: function() {},
1845 write: function() {}
1848 var CBIButtonValue
= CBIValue
.extend({
1849 __name__
: 'CBI.ButtonValue',
1851 renderWidget: function(section_id
, option_index
, cfgvalue
) {
1852 var value
= (cfgvalue
!= null) ? cfgvalue
: this.default,
1853 hiddenEl
= new ui
.Hiddenfield(value
, { id
: this.cbid(section_id
) }),
1854 outputEl
= E('div'),
1855 btn_title
= this.titleFn('inputtitle', section_id
) || this.titleFn('title', section_id
);
1857 if (value
!== false)
1858 L
.dom
.content(outputEl
, [
1860 'class': 'cbi-button cbi-button-%s'.format(this.inputstyle
|| 'button'),
1861 'click': L
.ui
.createHandlerFn(this, this.onclick
|| function(ev
) {
1862 ev
.currentTarget
.parentNode
.nextElementSibling
.value
= value
;
1863 return this.map
.save();
1868 L
.dom
.content(outputEl
, ' - ');
1877 var CBIHiddenValue
= CBIValue
.extend({
1878 __name__
: 'CBI.HiddenValue',
1880 renderWidget: function(section_id
, option_index
, cfgvalue
) {
1881 var widget
= new ui
.Hiddenfield((cfgvalue
!= null) ? cfgvalue
: this.default, {
1882 id
: this.cbid(section_id
)
1885 return widget
.render();
1889 var CBIFileUpload
= CBIValue
.extend({
1890 __name__
: 'CBI.FileSelect',
1892 __init__: function(/* ... */) {
1893 this.super('__init__', arguments
);
1895 this.show_hidden
= false;
1896 this.enable_upload
= true;
1897 this.enable_remove
= true;
1898 this.root_directory
= '/etc/luci-uploads';
1901 renderWidget: function(section_id
, option_index
, cfgvalue
) {
1902 var browserEl
= new ui
.FileUpload((cfgvalue
!= null) ? cfgvalue
: this.default, {
1903 id
: this.cbid(section_id
),
1904 name
: this.cbid(section_id
),
1905 show_hidden
: this.show_hidden
,
1906 enable_upload
: this.enable_upload
,
1907 enable_remove
: this.enable_remove
,
1908 root_directory
: this.root_directory
1911 return browserEl
.render();
1915 var CBISectionValue
= CBIValue
.extend({
1916 __name__
: 'CBI.ContainerValue',
1917 __init__: function(map
, section
, option
, cbiClass
/*, ... */) {
1918 this.super('__init__', [map
, section
, option
]);
1920 if (!CBIAbstractSection
.isSubclass(cbiClass
))
1921 throw 'Sub section must be a descendent of CBIAbstractSection';
1923 this.subsection
= cbiClass
.instantiate(this.varargs(arguments
, 4, this.map
));
1926 load: function(section_id
) {
1927 return this.subsection
.load();
1930 parse: function(section_id
) {
1931 return this.subsection
.parse();
1934 renderWidget: function(section_id
, option_index
, cfgvalue
) {
1935 return this.subsection
.render();
1938 checkDepends: function(section_id
) {
1939 this.subsection
.checkDepends();
1940 return CBIValue
.prototype.checkDepends
.apply(this, [ section_id
]);
1943 write: function() {},
1944 remove: function() {},
1945 cfgvalue: function() { return null },
1946 formvalue: function() { return null }
1949 return L
.Class
.extend({
1951 JSONMap
: CBIJSONMap
,
1952 AbstractSection
: CBIAbstractSection
,
1953 AbstractValue
: CBIAbstractValue
,
1955 TypedSection
: CBITypedSection
,
1956 TableSection
: CBITableSection
,
1957 GridSection
: CBIGridSection
,
1958 NamedSection
: CBINamedSection
,
1961 DynamicList
: CBIDynamicList
,
1962 ListValue
: CBIListValue
,
1964 MultiValue
: CBIMultiValue
,
1965 TextValue
: CBITextValue
,
1966 DummyValue
: CBIDummyValue
,
1967 Button
: CBIButtonValue
,
1968 HiddenValue
: CBIHiddenValue
,
1969 FileUpload
: CBIFileUpload
,
1970 SectionValue
: CBISectionValue