7 var CBINode
= Class
.extend({
8 __init__: function(title
, description
) {
9 this.title
= title
|| '';
10 this.description
= description
|| '';
14 append: function(obj
) {
15 this.children
.push(obj
);
20 this.children
.forEach(function(child
) {
21 child
.parse
.apply(child
, args
);
26 L
.error('InternalError', 'Not implemented');
29 loadChildren: function(/* ... */) {
32 if (Array
.isArray(this.children
))
33 for (var i
= 0; i
< this.children
.length
; i
++)
34 if (!this.children
[i
].disable
)
35 tasks
.push(this.children
[i
].load
.apply(this.children
[i
], arguments
));
37 return Promise
.all(tasks
);
40 renderChildren: function(tab_name
/*, ... */) {
44 if (Array
.isArray(this.children
))
45 for (var i
= 0; i
< this.children
.length
; i
++)
46 if (tab_name
=== null || this.children
[i
].tab
=== tab_name
)
47 if (!this.children
[i
].disable
)
48 tasks
.push(this.children
[i
].render
.apply(
49 this.children
[i
], this.varargs(arguments
, 1, index
++)));
51 return Promise
.all(tasks
);
54 stripTags: function(s
) {
58 var x
= E('div', {}, s
);
59 return x
.textContent
|| x
.innerText
|| '';
63 var CBIMap
= CBINode
.extend({
64 __init__: function(config
/*, ... */) {
65 this.super('__init__', this.varargs(arguments
, 1));
68 this.parsechain
= [ config
];
71 findElements: function(/* ... */) {
74 if (arguments
.length
== 1)
76 else if (arguments
.length
== 2)
77 q
= '[%s="%s"]'.format(arguments
[0], arguments
[1]);
79 L
.error('InternalError', 'Expecting one or two arguments to findElements()');
81 return this.root
.querySelectorAll(q
);
84 findElement: function(/* ... */) {
85 var res
= this.findElements
.apply(this, arguments
);
86 return res
.length
? res
[0] : null;
89 chain: function(config
) {
90 if (this.parsechain
.indexOf(config
) == -1)
91 this.parsechain
.push(config
);
94 section: function(cbiClass
/*, ... */) {
95 if (!CBIAbstractSection
.isSubclass(cbiClass
))
96 L
.error('TypeError', 'Class must be a descendent of CBIAbstractSection');
98 var obj
= cbiClass
.instantiate(this.varargs(arguments
, 1, this));
104 return uci
.load(this.parsechain
|| [ this.config
])
105 .then(this.loadChildren
.bind(this));
111 if (Array
.isArray(this.children
))
112 for (var i
= 0; i
< this.children
.length
; i
++)
113 tasks
.push(this.children
[i
].parse());
115 return Promise
.all(tasks
);
120 .then(uci
.save
.bind(uci
))
121 .then(this.load
.bind(this))
122 .then(this.renderContents
.bind(this))
124 alert('Cannot save due to invalid values')
125 return Promise
.reject();
130 return this.renderContents();
134 return this.load().then(this.renderContents
.bind(this));
137 renderContents: function() {
138 var mapEl
= this.root
|| (this.root
= E('div', {
139 'id': 'cbi-%s'.format(this.config
),
141 'cbi-dependency-check': L
.bind(this.checkDepends
, this)
144 L
.dom
.bindClassInstance(mapEl
, this);
146 return this.renderChildren(null).then(L
.bind(function(nodes
) {
147 var initialRender
= !mapEl
.firstChild
;
149 L
.dom
.content(mapEl
, null);
151 if (this.title
!= null && this.title
!= '')
152 mapEl
.appendChild(E('h2', { 'name': 'content' }, this.title
));
154 if (this.description
!= null && this.description
!= '')
155 mapEl
.appendChild(E('div', { 'class': 'cbi-map-descr' }, this.description
));
157 L
.dom
.append(mapEl
, nodes
);
159 if (!initialRender
) {
160 mapEl
.classList
.remove('flash');
162 window
.setTimeout(function() {
163 mapEl
.classList
.add('flash');
173 lookupOption: function(name
, section_id
) {
174 var id
, elem
, sid
, inst
;
176 if (name
.indexOf('.') > -1)
177 id
= 'cbid.%s'.format(name
);
179 id
= 'cbid.%s.%s.%s'.format(this.config
, section_id
, name
);
181 elem
= this.findElement('data-field', id
);
182 sid
= elem
? id
.split(/\./)[2] : null;
183 inst
= elem
? L
.dom
.findClassInstance(elem
) : null;
185 return (inst
instanceof CBIAbstractValue
) ? [ inst
, sid
] : null;
188 checkDepends: function(ev
, n
) {
191 for (var i
= 0, s
= this.children
[0]; (s
= this.children
[i
]) != null; i
++)
192 if (s
.checkDepends(ev
, n
))
195 if (changed
&& (n
|| 0) < 10)
196 this.checkDepends(ev
, (n
|| 10) + 1);
200 var CBIAbstractSection
= CBINode
.extend({
201 __init__: function(map
, sectionType
/*, ... */) {
202 this.super('__init__', this.varargs(arguments
, 2));
204 this.sectiontype
= sectionType
;
206 this.config
= map
.config
;
208 this.optional
= true;
209 this.addremove
= false;
210 this.dynamic
= false;
213 cfgsections: function() {
214 L
.error('InternalError', 'Not implemented');
217 filter: function(section_id
) {
222 var section_ids
= this.cfgsections(),
225 if (Array
.isArray(this.children
))
226 for (var i
= 0; i
< section_ids
.length
; i
++)
227 tasks
.push(this.loadChildren(section_ids
[i
])
228 .then(Function
.prototype.bind
.call(function(section_id
, set_values
) {
229 for (var i
= 0; i
< set_values
.length
; i
++)
230 this.children
[i
].cfgvalue(section_id
, set_values
[i
]);
231 }, this, section_ids
[i
])));
233 return Promise
.all(tasks
);
237 var section_ids
= this.cfgsections(),
240 if (Array
.isArray(this.children
))
241 for (var i
= 0; i
< section_ids
.length
; i
++)
242 for (var j
= 0; j
< this.children
.length
; j
++)
243 tasks
.push(this.children
[j
].parse(section_ids
[i
]));
245 return Promise
.all(tasks
);
248 tab: function(name
, title
, description
) {
249 if (this.tabs
&& this.tabs
[name
])
250 throw 'Tab already declared';
255 description
: description
,
259 this.tabs
= this.tabs
|| [];
260 this.tabs
.push(entry
);
261 this.tabs
[name
] = entry
;
263 this.tab_names
= this.tab_names
|| [];
264 this.tab_names
.push(name
);
267 option: function(cbiClass
/*, ... */) {
268 if (!CBIAbstractValue
.isSubclass(cbiClass
))
269 throw L
.error('TypeError', 'Class must be a descendent of CBIAbstractValue');
271 var obj
= cbiClass
.instantiate(this.varargs(arguments
, 1, this.map
, this));
276 taboption: function(tabName
/*, ... */) {
277 if (!this.tabs
|| !this.tabs
[tabName
])
278 throw L
.error('ReferenceError', 'Associated tab not declared');
280 var obj
= this.option
.apply(this, this.varargs(arguments
, 1));
282 this.tabs
[tabName
].children
.push(obj
);
286 renderUCISection: function(section_id
) {
287 var renderTasks
= [];
290 return this.renderOptions(null, section_id
);
292 for (var i
= 0; i
< this.tab_names
.length
; i
++)
293 renderTasks
.push(this.renderOptions(this.tab_names
[i
], section_id
));
295 return Promise
.all(renderTasks
)
296 .then(this.renderTabContainers
.bind(this, section_id
));
299 renderTabContainers: function(section_id
, nodes
) {
300 var config_name
= this.uciconfig
|| this.map
.config
,
301 containerEls
= E([]);
303 for (var i
= 0; i
< nodes
.length
; i
++) {
304 var tab_name
= this.tab_names
[i
],
305 tab_data
= this.tabs
[tab_name
],
306 containerEl
= E('div', {
307 'id': 'container.%s.%s.%s'.format(config_name
, section_id
, tab_name
),
308 'data-tab': tab_name
,
309 'data-tab-title': tab_data
.title
,
310 'data-tab-active': tab_name
=== this.selected_tab
313 if (tab_data
.description
!= null && tab_data
.description
!= '')
314 containerEl
.appendChild(
315 E('div', { 'class': 'cbi-tab-descr' }, tab_data
.description
));
317 containerEl
.appendChild(nodes
[i
]);
318 containerEls
.appendChild(containerEl
);
324 renderOptions: function(tab_name
, section_id
) {
325 var in_table
= (this instanceof CBITableSection
);
326 return this.renderChildren(tab_name
, section_id
, in_table
).then(function(nodes
) {
327 var optionEls
= E([]);
328 for (var i
= 0; i
< nodes
.length
; i
++)
329 optionEls
.appendChild(nodes
[i
]);
334 checkDepends: function(ev
, n
) {
336 sids
= this.cfgsections();
338 for (var i
= 0, sid
= sids
[0]; (sid
= sids
[i
]) != null; i
++) {
339 for (var j
= 0, o
= this.children
[0]; (o
= this.children
[j
]) != null; j
++) {
340 var isActive
= o
.isActive(sid
),
341 isSatisified
= o
.checkDepends(sid
);
343 if (isActive
!= isSatisified
) {
344 o
.setActive(sid
, !isActive
);
349 o
.triggerValidation(sid
);
358 var isEqual = function(x
, y
) {
359 if (x
!= null && y
!= null && typeof(x
) != typeof(y
))
362 if ((x
== null && y
!= null) || (x
!= null && y
== null))
365 if (Array
.isArray(x
)) {
366 if (x
.length
!= y
.length
)
369 for (var i
= 0; i
< x
.length
; i
++)
370 if (!isEqual(x
[i
], y
[i
]))
373 else if (typeof(x
) == 'object') {
375 if (x
.hasOwnProperty(k
) && !y
.hasOwnProperty(k
))
378 if (!isEqual(x
[k
], y
[k
]))
383 if (y
.hasOwnProperty(k
) && !x
.hasOwnProperty(k
))
393 var CBIAbstractValue
= CBINode
.extend({
394 __init__: function(map
, section
, option
/*, ... */) {
395 this.super('__init__', this.varargs(arguments
, 3));
397 this.section
= section
;
398 this.option
= option
;
400 this.config
= map
.config
;
407 this.optional
= false;
410 depends: function(field
, value
) {
413 if (typeof(field
) === 'string')
414 deps
= {}, deps
[field
] = value
;
418 this.deps
.push(deps
);
421 transformDepList: function(section_id
, deplist
) {
422 var list
= deplist
|| this.deps
,
425 if (Array
.isArray(list
)) {
426 for (var i
= 0; i
< list
.length
; i
++) {
429 for (var k
in list
[i
]) {
430 if (list
[i
].hasOwnProperty(k
)) {
431 if (k
.charAt(0) === '!')
433 else if (k
.indexOf('.') !== -1)
434 dep
['cbid.%s'.format(k
)] = list
[i
][k
];
436 dep
['cbid.%s.%s.%s'.format(this.config
, this.ucisection
|| section_id
, k
)] = list
[i
][k
];
441 if (dep
.hasOwnProperty(k
)) {
452 transformChoices: function() {
453 if (!Array
.isArray(this.keylist
) || this.keylist
.length
== 0)
458 for (var i
= 0; i
< this.keylist
.length
; i
++)
459 choices
[this.keylist
[i
]] = this.vallist
[i
];
464 checkDepends: function(section_id
) {
467 if (!Array
.isArray(this.deps
) || !this.deps
.length
)
470 for (var i
= 0; i
< this.deps
.length
; i
++) {
474 for (var dep
in this.deps
[i
]) {
475 if (dep
== '!reverse') {
478 else if (dep
== '!default') {
483 var res
= this.map
.lookupOption(dep
, section_id
),
484 val
= res
? res
[0].formvalue(res
[1]) : null;
486 istat
= (istat
&& isEqual(val
, this.deps
[i
][dep
]));
497 cbid: function(section_id
) {
498 if (section_id
== null)
499 L
.error('TypeError', 'Section ID required');
501 return 'cbid.%s.%s.%s'.format(this.map
.config
, section_id
, this.option
);
504 load: function(section_id
) {
505 if (section_id
== null)
506 L
.error('TypeError', 'Section ID required');
509 this.uciconfig
|| this.map
.config
,
510 this.ucisection
|| section_id
,
511 this.ucioption
|| this.option
);
514 cfgvalue: function(section_id
, set_value
) {
515 if (section_id
== null)
516 L
.error('TypeError', 'Section ID required');
518 if (arguments
.length
== 2) {
519 this.data
= this.data
|| {};
520 this.data
[section_id
] = set_value
;
523 return this.data
? this.data
[section_id
] : null;
526 formvalue: function(section_id
) {
527 var node
= this.map
.findElement('id', this.cbid(section_id
));
528 return node
? L
.dom
.callClassMethod(node
, 'getValue') : null;
531 textvalue: function(section_id
) {
532 var cval
= this.cfgvalue(section_id
);
537 return (cval
!= null) ? '%h'.format(cval
) : null;
540 validate: function(section_id
, value
) {
544 isValid: function(section_id
) {
545 var node
= this.map
.findElement('id', this.cbid(section_id
));
546 return node
? L
.dom
.callClassMethod(node
, 'isValid') : true;
549 isActive: function(section_id
) {
550 var field
= this.map
.findElement('data-field', this.cbid(section_id
));
551 return (field
!= null && !field
.classList
.contains('hidden'));
554 setActive: function(section_id
, active
) {
555 var field
= this.map
.findElement('data-field', this.cbid(section_id
));
557 if (field
&& field
.classList
.contains('hidden') == active
) {
558 field
.classList
[active
? 'remove' : 'add']('hidden');
565 triggerValidation: function(section_id
) {
566 var node
= this.map
.findElement('id', this.cbid(section_id
));
567 return node
? L
.dom
.callClassMethod(node
, 'triggerValidation') : true;
570 parse: function(section_id
) {
571 var active
= this.isActive(section_id
),
572 cval
= this.cfgvalue(section_id
),
573 fval
= active
? this.formvalue(section_id
) : null;
575 if (active
&& !this.isValid(section_id
))
576 return Promise
.reject();
578 if (fval
!= '' && fval
!= null) {
579 if (this.forcewrite
|| !isEqual(cval
, fval
))
580 return Promise
.resolve(this.write(section_id
, fval
));
583 if (this.rmempty
|| this.optional
) {
584 return Promise
.resolve(this.remove(section_id
));
586 else if (!isEqual(cval
, fval
)) {
587 console
.log('This should have been catched by isValid()');
588 return Promise
.reject();
592 return Promise
.resolve();
595 write: function(section_id
, formvalue
) {
597 this.uciconfig
|| this.map
.config
,
598 this.ucisection
|| section_id
,
599 this.ucioption
|| this.option
,
603 remove: function(section_id
) {
605 this.uciconfig
|| this.map
.config
,
606 this.ucisection
|| section_id
,
607 this.ucioption
|| this.option
);
611 var CBITypedSection
= CBIAbstractSection
.extend({
612 __name__
: 'CBI.TypedSection',
614 cfgsections: function() {
615 return uci
.sections(this.uciconfig
|| this.map
.config
, this.sectiontype
)
616 .map(function(s
) { return s
['.name'] })
617 .filter(L
.bind(this.filter
, this));
620 handleAdd: function(ev
, name
) {
621 var config_name
= this.uciconfig
|| this.map
.config
;
623 uci
.add(config_name
, this.sectiontype
, name
);
627 handleRemove: function(section_id
, ev
) {
628 var config_name
= this.uciconfig
|| this.map
.config
;
630 uci
.remove(config_name
, section_id
);
634 renderSectionAdd: function(extra_class
) {
638 var createEl
= E('div', { 'class': 'cbi-section-create' }),
639 config_name
= this.uciconfig
|| this.map
.config
;
641 if (extra_class
!= null)
642 createEl
.classList
.add(extra_class
);
644 if (this.anonymous
) {
645 createEl
.appendChild(E('input', {
647 'class': 'cbi-button cbi-button-add',
650 'click': L
.bind(this.handleAdd
, this)
654 var nameEl
= E('input', {
656 'class': 'cbi-section-create-name'
659 L
.dom
.append(createEl
, [
660 E('div', {}, nameEl
),
662 'class': 'cbi-button cbi-button-add',
666 'click': L
.bind(function(ev
) {
667 if (nameEl
.classList
.contains('cbi-input-invalid'))
670 this.handleAdd(ev
, nameEl
.value
);
675 ui
.addValidator(nameEl
, 'uciname', true, 'blur', 'keyup');
681 renderContents: function(cfgsections
, nodes
) {
682 var section_id
= null,
683 config_name
= this.uciconfig
|| this.map
.config
,
684 sectionEl
= E('div', {
685 'id': 'cbi-%s-%s'.format(config_name
, this.sectiontype
),
686 'class': 'cbi-section'
689 if (this.title
!= null && this.title
!= '')
690 sectionEl
.appendChild(E('legend', {}, this.title
));
692 if (this.description
!= null && this.description
!= '')
693 sectionEl
.appendChild(E('div', { 'class': 'cbi-section-descr' }, this.description
));
695 for (var i
= 0; i
< nodes
.length
; i
++) {
696 if (this.addremove
) {
697 sectionEl
.appendChild(
698 E('div', { 'class': 'cbi-section-remove right' },
701 'class': 'cbi-button',
702 'name': 'cbi.rts.%s.%s'.format(config_name
, cfgsections
[i
]),
703 'value': _('Delete'),
704 'data-section-id': cfgsections
[i
],
705 'click': L
.bind(this.handleRemove
, this, cfgsections
[i
])
710 sectionEl
.appendChild(E('h3', cfgsections
[i
].toUpperCase()));
712 sectionEl
.appendChild(E('div', {
713 'id': 'cbi-%s-%s'.format(config_name
, cfgsections
[i
]),
715 ? 'cbi-section-node cbi-section-node-tabbed' : 'cbi-section-node'
719 ui
.tabs
.initTabGroup(sectionEl
.lastChild
.childNodes
);
722 if (nodes
.length
== 0)
723 L
.dom
.append(sectionEl
, [
724 E('em', _('This section contains no values yet')),
728 sectionEl
.appendChild(this.renderSectionAdd());
730 L
.dom
.bindClassInstance(sectionEl
, this);
736 var cfgsections
= this.cfgsections(),
739 for (var i
= 0; i
< cfgsections
.length
; i
++)
740 renderTasks
.push(this.renderUCISection(cfgsections
[i
]));
742 return Promise
.all(renderTasks
).then(this.renderContents
.bind(this, cfgsections
));
746 var CBITableSection
= CBITypedSection
.extend({
747 __name__
: 'CBI.TableSection',
750 throw 'Tabs are not supported by TableSection';
753 renderContents: function(cfgsections
, nodes
) {
754 var section_id
= null,
755 config_name
= this.uciconfig
|| this.map
.config
,
756 max_cols
= isNaN(this.max_cols
) ? this.children
.length
: this.max_cols
,
757 has_more
= max_cols
< this.children
.length
,
758 sectionEl
= E('div', {
759 'id': 'cbi-%s-%s'.format(config_name
, this.sectiontype
),
760 'class': 'cbi-section cbi-tblsection'
763 'class': 'table cbi-section-table'
766 if (this.title
!= null && this.title
!= '')
767 sectionEl
.appendChild(E('h3', {}, this.title
));
769 if (this.description
!= null && this.description
!= '')
770 sectionEl
.appendChild(E('div', { 'class': 'cbi-section-descr' }, this.description
));
772 tableEl
.appendChild(this.renderHeaderRows(max_cols
));
774 for (var i
= 0; i
< nodes
.length
; i
++) {
775 var sectionname
= this.stripTags((typeof(this.sectiontitle
) == 'function')
776 ? String(this.sectiontitle(cfgsections
[i
]) || '') : cfgsections
[i
]).trim();
778 var trEl
= E('div', {
779 'id': 'cbi-%s-%s'.format(config_name
, cfgsections
[i
]),
780 'class': 'tr cbi-section-table-row',
781 'data-sid': cfgsections
[i
],
782 'draggable': this.sortable
? true : null,
783 'mousedown': this.sortable
? L
.bind(this.handleDragInit
, this) : null,
784 'dragstart': this.sortable
? L
.bind(this.handleDragStart
, this) : null,
785 'dragover': this.sortable
? L
.bind(this.handleDragOver
, this) : null,
786 'dragenter': this.sortable
? L
.bind(this.handleDragEnter
, this) : null,
787 'dragleave': this.sortable
? L
.bind(this.handleDragLeave
, this) : null,
788 'dragend': this.sortable
? L
.bind(this.handleDragEnd
, this) : null,
789 'drop': this.sortable
? L
.bind(this.handleDrop
, this) : null,
790 'data-title': (sectionname
&& (!this.anonymous
|| this.sectiontitle
)) ? sectionname
: null
793 if (this.extedit
|| this.rowcolors
)
794 trEl
.classList
.add(!(tableEl
.childNodes
.length
% 2)
795 ? 'cbi-rowstyle-1' : 'cbi-rowstyle-2');
797 for (var j
= 0; j
< max_cols
&& nodes
[i
].firstChild
; j
++)
798 trEl
.appendChild(nodes
[i
].firstChild
);
800 trEl
.appendChild(this.renderRowActions(cfgsections
[i
], has_more
? _('More…') : null));
801 tableEl
.appendChild(trEl
);
804 if (nodes
.length
== 0)
805 tableEl
.appendChild(E('div', { 'class': 'tr cbi-section-table-row placeholder' },
806 E('div', { 'class': 'td' },
807 E('em', {}, _('This section contains no values yet')))));
809 sectionEl
.appendChild(tableEl
);
811 sectionEl
.appendChild(this.renderSectionAdd('cbi-tblsection-create'));
813 L
.dom
.bindClassInstance(sectionEl
, this);
818 renderHeaderRows: function(max_cols
) {
819 var has_titles
= false,
820 has_descriptions
= false,
821 anon_class
= (!this.anonymous
|| this.sectiontitle
) ? 'named' : 'anonymous',
824 for (var i
= 0, opt
; i
< max_cols
&& (opt
= this.children
[i
]) != null; i
++) {
825 if (opt
.optional
|| opt
.modalonly
)
828 has_titles
= has_titles
|| !!opt
.title
;
829 has_descriptions
= has_descriptions
|| !!opt
.description
;
833 var trEl
= E('div', {
834 'class': 'tr cbi-section-table-titles ' + anon_class
,
835 'data-title': (!this.anonymous
|| this.sectiontitle
) ? _('Name') : null
838 for (var i
= 0, opt
; i
< max_cols
&& (opt
= this.children
[i
]) != null; i
++) {
839 if (opt
.optional
|| opt
.modalonly
)
842 trEl
.appendChild(E('div', {
843 'class': 'th cbi-section-table-cell',
844 'data-type': opt
.__name__
847 if (opt
.width
!= null)
848 trEl
.lastElementChild
.style
.width
=
849 (typeof(opt
.width
) == 'number') ? opt
.width
+'px' : opt
.width
;
852 trEl
.lastElementChild
.appendChild(E('a', {
853 'href': opt
.titleref
,
854 'class': 'cbi-title-ref',
855 'title': this.titledesc
|| _('Go to relevant configuration page')
858 L
.dom
.content(trEl
.lastElementChild
, opt
.title
);
861 if (this.sortable
|| this.extedit
|| this.addremove
|| has_more
)
862 trEl
.appendChild(E('div', {
863 'class': 'th cbi-section-table-cell cbi-section-actions'
866 trEls
.appendChild(trEl
);
869 if (has_descriptions
) {
870 var trEl
= E('div', {
871 'class': 'tr cbi-section-table-descr ' + anon_class
874 for (var i
= 0, opt
; i
< max_cols
&& (opt
= this.children
[i
]) != null; i
++) {
875 if (opt
.optional
|| opt
.modalonly
)
878 trEl
.appendChild(E('div', {
879 'class': 'th cbi-section-table-cell',
880 'data-type': opt
.__name__
881 }, opt
.description
));
883 if (opt
.width
!= null)
884 trEl
.lastElementChild
.style
.width
=
885 (typeof(opt
.width
) == 'number') ? opt
.width
+'px' : opt
.width
;
888 if (this.sortable
|| this.extedit
|| this.addremove
|| has_more
)
889 trEl
.appendChild(E('div', {
890 'class': 'th cbi-section-table-cell cbi-section-actions'
893 trEls
.appendChild(trEl
);
899 renderRowActions: function(section_id
, more_label
) {
900 var config_name
= this.uciconfig
|| this.map
.config
;
902 if (!this.sortable
&& !this.extedit
&& !this.addremove
&& !more_label
)
905 var tdEl
= E('div', {
906 'class': 'td cbi-section-table-cell nowrap cbi-section-actions'
910 L
.dom
.append(tdEl
.lastElementChild
, [
912 'title': _('Drag to reorder'),
913 'class': 'cbi-button drag-handle center',
914 'style': 'cursor:move'
922 if (typeof(this.extedit
) == 'function')
923 evFn
= L
.bind(this.extedit
, this);
924 else if (typeof(this.extedit
) == 'string')
925 evFn
= L
.bind(function(sid
, ev
) {
926 location
.href
= this.extedit
.format(sid
);
927 }, this, section_id
);
929 L
.dom
.append(tdEl
.lastElementChild
,
934 'class': 'cbi-button cbi-button-edit',
941 L
.dom
.append(tdEl
.lastElementChild
,
946 'class': 'cbi-button cbi-button-edit',
947 'click': L
.bind(this.renderMoreOptionsModal
, this, section_id
)
952 if (this.addremove
) {
953 L
.dom
.append(tdEl
.lastElementChild
,
956 'value': _('Delete'),
957 'title': _('Delete'),
958 'class': 'cbi-button cbi-button-remove',
959 'click': L
.bind(function(sid
, ev
) {
960 uci
.remove(config_name
, sid
);
970 handleDragInit: function(ev
) {
971 scope
.dragState
= { node
: ev
.target
};
974 handleDragStart: function(ev
) {
975 if (!scope
.dragState
|| !scope
.dragState
.node
.classList
.contains('drag-handle')) {
976 scope
.dragState
= null;
981 scope
.dragState
.node
= L
.dom
.parent(scope
.dragState
.node
, '.tr');
982 ev
.dataTransfer
.setData('text', 'drag');
983 ev
.target
.style
.opacity
= 0.4;
986 handleDragOver: function(ev
) {
987 var n
= scope
.dragState
.targetNode
,
988 r
= scope
.dragState
.rect
,
989 t
= r
.top
+ r
.height
/ 2;
991 if (ev
.clientY
<= t
) {
992 n
.classList
.remove('drag-over-below');
993 n
.classList
.add('drag-over-above');
996 n
.classList
.remove('drag-over-above');
997 n
.classList
.add('drag-over-below');
1000 ev
.dataTransfer
.dropEffect
= 'move';
1001 ev
.preventDefault();
1005 handleDragEnter: function(ev
) {
1006 scope
.dragState
.rect
= ev
.currentTarget
.getBoundingClientRect();
1007 scope
.dragState
.targetNode
= ev
.currentTarget
;
1010 handleDragLeave: function(ev
) {
1011 ev
.currentTarget
.classList
.remove('drag-over-above');
1012 ev
.currentTarget
.classList
.remove('drag-over-below');
1015 handleDragEnd: function(ev
) {
1018 n
.style
.opacity
= '';
1019 n
.classList
.add('flash');
1020 n
.parentNode
.querySelectorAll('.drag-over-above, .drag-over-below')
1021 .forEach(function(tr
) {
1022 tr
.classList
.remove('drag-over-above');
1023 tr
.classList
.remove('drag-over-below');
1027 handleDrop: function(ev
) {
1028 var s
= scope
.dragState
;
1030 if (s
.node
&& s
.targetNode
) {
1031 var config_name
= this.uciconfig
|| this.map
.config
,
1032 ref_node
= s
.targetNode
,
1035 if (ref_node
.classList
.contains('drag-over-below')) {
1036 ref_node
= ref_node
.nextElementSibling
;
1040 var sid1
= s
.node
.getAttribute('data-sid'),
1041 sid2
= s
.targetNode
.getAttribute('data-sid');
1043 s
.node
.parentNode
.insertBefore(s
.node
, ref_node
);
1044 uci
.move(config_name
, sid1
, sid2
, after
);
1047 scope
.dragState
= null;
1048 ev
.target
.style
.opacity
= '';
1049 ev
.stopPropagation();
1050 ev
.preventDefault();
1054 handleModalCancel: function(modalMap
, ev
) {
1055 return Promise
.resolve(L
.ui
.hideModal());
1058 handleModalSave: function(modalMap
, ev
) {
1059 return modalMap
.save()
1060 .then(L
.bind(this.map
.reset
, this.map
))
1061 .then(L
.ui
.hideModal
)
1062 .catch(function() {});
1065 renderMoreOptionsModal: function(section_id
, ev
) {
1066 var parent
= this.map
,
1067 title
= parent
.title
,
1069 m
= new CBIMap(this.map
.config
, null, null),
1070 s
= m
.section(CBINamedSection
, section_id
, this.sectiontype
);
1072 s
.tab_names
= this.tab_names
;
1074 if (typeof(this.sectiontitle
) == 'function')
1075 name
= this.stripTags(String(this.sectiontitle(section_id
) || ''));
1076 else if (!this.anonymous
)
1080 title
+= ' - ' + name
;
1082 for (var i
= 0; i
< this.children
.length
; i
++) {
1083 var o1
= this.children
[i
];
1085 if (o1
.disabled
|| o1
.modalonly
=== false)
1088 var o2
= s
.option(o1
.constructor, o1
.option
, o1
.title
, o1
.description
);
1091 if (!o1
.hasOwnProperty(k
))
1108 //ev.target.classList.add('spinning');
1109 m
.render().then(L
.bind(function(nodes
) {
1110 //ev.target.classList.remove('spinning');
1111 L
.ui
.showModal(title
, [
1113 E('div', { 'class': 'right' }, [
1117 'click': L
.bind(this.handleModalCancel
, this, m
),
1118 'value': _('Dismiss')
1122 'class': 'cbi-button cbi-button-positive important',
1123 'click': L
.bind(this.handleModalSave
, this, m
),
1128 }, this)).catch(L
.error
);
1132 var CBIGridSection
= CBITableSection
.extend({
1133 tab: function(name
, title
, description
) {
1134 CBIAbstractSection
.prototype.tab
.call(this, name
, title
, description
);
1137 handleAdd: function(ev
) {
1138 var config_name
= this.uciconfig
|| this.map
.config
,
1139 section_id
= uci
.add(config_name
, this.sectiontype
);
1141 this.addedSection
= section_id
;
1142 this.renderMoreOptionsModal(section_id
);
1145 handleModalSave: function(/* ... */) {
1146 return this.super('handleModalSave', arguments
)
1147 .then(L
.bind(function() { this.addedSection
= null }, this));
1150 handleModalCancel: function(/* ... */) {
1151 var config_name
= this.uciconfig
|| this.map
.config
;
1153 if (this.addedSection
!= null) {
1154 uci
.remove(config_name
, this.addedSection
);
1155 this.addedSection
= null;
1158 return this.super('handleModalCancel', arguments
);
1161 renderUCISection: function(section_id
) {
1162 return this.renderOptions(null, section_id
);
1165 renderChildren: function(tab_name
, section_id
, in_table
) {
1166 var tasks
= [], index
= 0;
1168 for (var i
= 0, opt
; (opt
= this.children
[i
]) != null; i
++) {
1169 if (opt
.disable
|| opt
.modalonly
)
1173 tasks
.push(opt
.render(index
++, section_id
, in_table
));
1175 tasks
.push(this.renderTextValue(section_id
, opt
));
1178 return Promise
.all(tasks
);
1181 renderTextValue: function(section_id
, opt
) {
1182 var title
= this.stripTags(opt
.title
).trim(),
1183 descr
= this.stripTags(opt
.description
).trim(),
1184 value
= opt
.textvalue(section_id
);
1187 'class': 'td cbi-value-field',
1188 'data-title': (title
!= '') ? title
: opt
.option
,
1189 'data-description': (descr
!= '') ? descr
: null,
1190 'data-name': opt
.option
,
1191 'data-type': opt
.typename
|| opt
.__name__
1192 }, (value
!= null) ? value
: E('em', _('none')));
1195 renderRowActions: function(section_id
) {
1196 return this.super('renderRowActions', [ section_id
, _('Edit') ]);
1200 var section_ids
= this.cfgsections(),
1203 if (Array
.isArray(this.children
)) {
1204 for (var i
= 0; i
< section_ids
.length
; i
++) {
1205 for (var j
= 0; j
< this.children
.length
; j
++) {
1206 if (!this.children
[j
].editable
|| this.children
[j
].modalonly
)
1209 tasks
.push(this.children
[j
].parse(section_ids
[i
]));
1214 return Promise
.all(tasks
);
1218 var CBINamedSection
= CBIAbstractSection
.extend({
1219 __name__
: 'CBI.NamedSection',
1220 __init__: function(map
, section_id
/*, ... */) {
1221 this.super('__init__', this.varargs(arguments
, 2, map
));
1223 this.section
= section_id
;
1226 cfgsections: function() {
1227 return [ this.section
];
1230 handleAdd: function(ev
) {
1231 var section_id
= this.section
,
1232 config_name
= this.uciconfig
|| this.map
.config
;
1234 uci
.add(config_name
, this.sectiontype
, section_id
);
1238 handleRemove: function(ev
) {
1239 var section_id
= this.section
,
1240 config_name
= this.uciconfig
|| this.map
.config
;
1242 uci
.remove(config_name
, section_id
);
1246 renderContents: function(data
) {
1247 var ucidata
= data
[0], nodes
= data
[1],
1248 section_id
= this.section
,
1249 config_name
= this.uciconfig
|| this.map
.config
,
1250 sectionEl
= E('div', {
1251 'id': ucidata
? null : 'cbi-%s-%s'.format(config_name
, section_id
),
1252 'class': 'cbi-section'
1255 if (typeof(this.title
) === 'string' && this.title
!== '')
1256 sectionEl
.appendChild(E('legend', {}, this.title
));
1258 if (typeof(this.description
) === 'string' && this.description
!== '')
1259 sectionEl
.appendChild(E('div', { 'class': 'cbi-section-descr' }, this.description
));
1262 if (this.addremove
) {
1263 sectionEl
.appendChild(
1264 E('div', { 'class': 'cbi-section-remove right' },
1267 'class': 'cbi-button',
1268 'value': _('Delete'),
1269 'click': L
.bind(this.handleRemove
, this)
1273 sectionEl
.appendChild(E('div', {
1274 'id': 'cbi-%s-%s'.format(config_name
, section_id
),
1276 ? 'cbi-section-node cbi-section-node-tabbed' : 'cbi-section-node'
1280 ui
.tabs
.initTabGroup(sectionEl
.lastChild
.childNodes
);
1282 else if (this.addremove
) {
1283 sectionEl
.appendChild(
1286 'class': 'cbi-button cbi-button-add',
1288 'click': L
.bind(this.handleAdd
, this)
1292 L
.dom
.bindClassInstance(sectionEl
, this);
1297 render: function() {
1298 var config_name
= this.uciconfig
|| this.map
.config
,
1299 section_id
= this.section
;
1301 return Promise
.all([
1302 uci
.get(config_name
, section_id
),
1303 this.renderUCISection(section_id
)
1304 ]).then(this.renderContents
.bind(this));
1308 var CBIValue
= CBIAbstractValue
.extend({
1309 __name__
: 'CBI.Value',
1311 value: function(key
, val
) {
1312 this.keylist
= this.keylist
|| [];
1313 this.keylist
.push(String(key
));
1315 this.vallist
= this.vallist
|| [];
1316 this.vallist
.push(String(val
!= null ? val
: key
));
1319 render: function(option_index
, section_id
, in_table
) {
1320 return Promise
.resolve(this.cfgvalue(section_id
))
1321 .then(this.renderWidget
.bind(this, section_id
, option_index
))
1322 .then(this.renderFrame
.bind(this, section_id
, in_table
, option_index
));
1325 renderFrame: function(section_id
, in_table
, option_index
, nodes
) {
1326 var config_name
= this.uciconfig
|| this.map
.config
,
1327 depend_list
= this.transformDepList(section_id
),
1331 optionEl
= E('div', {
1332 'class': 'td cbi-value-field',
1333 'data-title': this.stripTags(this.title
).trim(),
1334 'data-description': this.stripTags(this.description
).trim(),
1335 'data-name': this.option
,
1336 'data-type': this.typename
|| (this.template
? this.template
.replace(/^.+\//, '') : null) || this.__name__
1338 'id': 'cbi-%s-%s-%s'.format(config_name
, section_id
, this.option
),
1339 'data-index': option_index
,
1340 'data-depends': depend_list
,
1341 'data-field': this.cbid(section_id
)
1345 optionEl
= E('div', {
1346 'class': 'cbi-value',
1347 'id': 'cbi-%s-%s-%s'.format(config_name
, section_id
, this.option
),
1348 'data-index': option_index
,
1349 'data-depends': depend_list
,
1350 'data-field': this.cbid(section_id
),
1351 'data-name': this.option
,
1352 'data-type': this.typename
|| (this.template
? this.template
.replace(/^.+\//, '') : null) || this.__name__
1355 if (this.last_child
)
1356 optionEl
.classList
.add('cbi-value-last');
1358 if (typeof(this.title
) === 'string' && this.title
!== '') {
1359 optionEl
.appendChild(E('label', {
1360 'class': 'cbi-value-title',
1361 'for': 'cbid.%s.%s.%s'.format(config_name
, section_id
, this.option
)
1363 this.titleref
? E('a', {
1364 'class': 'cbi-title-ref',
1365 'href': this.titleref
,
1366 'title': this.titledesc
|| _('Go to relevant configuration page')
1367 }, this.title
) : this.title
));
1369 optionEl
.appendChild(E('div', { 'class': 'cbi-value-field' }));
1374 (optionEl
.lastChild
|| optionEl
).appendChild(nodes
);
1376 if (!in_table
&& typeof(this.description
) === 'string' && this.description
!== '')
1377 L
.dom
.append(optionEl
.lastChild
|| optionEl
,
1378 E('div', { 'class': 'cbi-value-description' }, this.description
));
1380 if (depend_list
&& depend_list
.length
)
1381 optionEl
.classList
.add('hidden');
1383 optionEl
.addEventListener('widget-change',
1384 L
.bind(this.map
.checkDepends
, this.map
));
1386 L
.dom
.bindClassInstance(optionEl
, this);
1391 renderWidget: function(section_id
, option_index
, cfgvalue
) {
1392 var value
= (cfgvalue
!= null) ? cfgvalue
: this.default,
1393 choices
= this.transformChoices(),
1397 var placeholder
= (this.optional
|| this.rmempty
)
1398 ? E('em', _('unspecified')) : _('-- Please choose --');
1400 widget
= new ui
.Combobox(Array
.isArray(value
) ? value
.join(' ') : value
, choices
, {
1401 id
: this.cbid(section_id
),
1403 optional
: this.optional
|| this.rmempty
,
1404 datatype
: this.datatype
,
1405 select_placeholder
: this.placeholder
|| placeholder
,
1406 validate
: L
.bind(this.validate
, this, section_id
)
1410 widget
= new ui
.Textfield(Array
.isArray(value
) ? value
.join(' ') : value
, {
1411 id
: this.cbid(section_id
),
1412 password
: this.password
,
1413 optional
: this.optional
|| this.rmempty
,
1414 datatype
: this.datatype
,
1415 placeholder
: this.placeholder
,
1416 validate
: L
.bind(this.validate
, this, section_id
)
1420 return widget
.render();
1424 var CBIDynamicList
= CBIValue
.extend({
1425 __name__
: 'CBI.DynamicList',
1427 renderWidget: function(section_id
, option_index
, cfgvalue
) {
1428 var value
= (cfgvalue
!= null) ? cfgvalue
: this.default,
1429 choices
= this.transformChoices(),
1432 if (Array
.isArray(value
))
1434 else if (value
!= null)
1435 items
= String(value
).trim().split(/\s+/);
1437 var widget
= new ui
.DynamicList(items
, choices
, {
1438 id
: this.cbid(section_id
),
1440 optional
: this.optional
|| this.rmempty
,
1441 datatype
: this.datatype
,
1442 validate
: L
.bind(this.validate
, this, section_id
)
1445 return widget
.render();
1449 var CBIListValue
= CBIValue
.extend({
1450 __name__
: 'CBI.ListValue',
1452 __init__: function() {
1453 this.super('__init__', arguments
);
1454 this.widget
= 'select';
1458 renderWidget: function(section_id
, option_index
, cfgvalue
) {
1459 var choices
= this.transformChoices();
1460 var widget
= new ui
.Select((cfgvalue
!= null) ? cfgvalue
: this.default, choices
, {
1461 id
: this.cbid(section_id
),
1464 optional
: this.rmempty
|| this.optional
,
1465 validate
: L
.bind(this.validate
, this, section_id
)
1468 return widget
.render();
1472 var CBIFlagValue
= CBIValue
.extend({
1473 __name__
: 'CBI.FlagValue',
1475 __init__: function() {
1476 this.super('__init__', arguments
);
1479 this.disabled
= '0';
1480 this.default = this.disabled
;
1483 renderWidget: function(section_id
, option_index
, cfgvalue
) {
1484 var widget
= new ui
.Checkbox((cfgvalue
!= null) ? cfgvalue
: this.default, {
1485 id
: this.cbid(section_id
),
1486 value_enabled
: this.enabled
,
1487 value_disabled
: this.disabled
,
1488 validate
: L
.bind(this.validate
, this, section_id
)
1491 return widget
.render();
1494 formvalue: function(section_id
) {
1495 var node
= this.map
.findElement('id', this.cbid(section_id
)),
1496 checked
= node
? L
.dom
.callClassMethod(node
, 'isChecked') : false;
1498 return checked
? this.enabled
: this.disabled
;
1501 textvalue: function(section_id
) {
1502 var cval
= this.cfgvalue(section_id
);
1505 cval
= this.default;
1507 return (cval
== this.enabled
) ? _('Yes') : _('No');
1510 parse: function(section_id
) {
1511 if (this.isActive(section_id
)) {
1512 var fval
= this.formvalue(section_id
);
1514 if (!this.isValid(section_id
))
1515 return Promise
.reject();
1517 if (fval
== this.default && (this.optional
|| this.rmempty
))
1518 return Promise
.resolve(this.remove(section_id
));
1520 return Promise
.resolve(this.write(section_id
, fval
));
1523 return Promise
.resolve(this.remove(section_id
));
1528 var CBIMultiValue
= CBIDynamicList
.extend({
1529 __name__
: 'CBI.MultiValue',
1531 __init__: function() {
1532 this.super('__init__', arguments
);
1533 this.placeholder
= _('-- Please choose --');
1536 renderWidget: function(section_id
, option_index
, cfgvalue
) {
1537 var value
= (cfgvalue
!= null) ? cfgvalue
: this.default,
1538 choices
= this.transformChoices();
1540 if (!Array
.isArray(value
))
1541 value
= String(value
).split(/\s+/);
1543 var widget
= new ui
.Dropdown(value
, choices
, {
1544 id
: this.cbid(section_id
),
1547 optional
: this.optional
|| this.rmempty
,
1548 select_placeholder
: this.placeholder
,
1549 display_items
: this.display_size
|| this.size
|| 3,
1550 dropdown_items
: this.dropdown_size
|| this.size
|| -1,
1551 validate
: L
.bind(this.validate
, this, section_id
)
1554 return widget
.render();
1558 var CBIDummyValue
= CBIValue
.extend({
1559 __name__
: 'CBI.DummyValue',
1561 renderWidget: function(section_id
, option_index
, cfgvalue
) {
1562 var value
= (cfgvalue
!= null) ? cfgvalue
: this.default,
1563 hiddenEl
= new ui
.Hiddenfield(value
, { id
: this.cbid(section_id
) }),
1564 outputEl
= E('div');
1567 outputEl
.appendChild(E('a', { 'href': this.href
}));
1569 L
.dom
.append(outputEl
.lastChild
|| outputEl
,
1570 this.rawhtml
? value
: [ value
]);
1579 var CBIButtonValue
= CBIValue
.extend({
1580 __name__
: 'CBI.ButtonValue',
1582 renderWidget: function(section_id
, option_index
, cfgvalue
) {
1583 var value
= (cfgvalue
!= null) ? cfgvalue
: this.default;
1585 if (value
!== false)
1589 'id': this.cbid(section_id
)
1592 'class': 'cbi-button cbi-button-%s'.format(this.inputstyle
|| 'button'),
1594 //'id': this.cbid(section_id),
1595 //'name': this.cbid(section_id),
1596 'value': this.inputtitle
|| this.title
,
1597 'click': L
.bind(function(ev
) {
1598 ev
.target
.previousElementSibling
.value
= ev
.target
.value
;
1604 return document
.createTextNode(' - ');
1608 var CBIHiddenValue
= CBIValue
.extend({
1609 __name__
: 'CBI.HiddenValue',
1611 renderWidget: function(section_id
, option_index
, cfgvalue
) {
1612 var widget
= new ui
.Hiddenfield((cfgvalue
!= null) ? cfgvalue
: this.default, {
1613 id
: this.cbid(section_id
)
1616 return widget
.render();
1620 var CBISectionValue
= CBIValue
.extend({
1621 __name__
: 'CBI.ContainerValue',
1622 __init__: function(map
, section
, option
, cbiClass
/*, ... */) {
1623 this.super('__init__', [map
, section
, option
]);
1625 if (!CBIAbstractSection
.isSubclass(cbiClass
))
1626 throw 'Sub section must be a descendent of CBIAbstractSection';
1628 this.subsection
= cbiClass
.instantiate(this.varargs(arguments
, 4, this.map
));
1631 load: function(section_id
) {
1632 return this.subsection
.load();
1635 parse: function(section_id
) {
1636 return this.subsection
.parse();
1639 renderWidget: function(section_id
, option_index
, cfgvalue
) {
1640 return this.subsection
.render();
1643 checkDepends: function(section_id
) {
1644 this.subsection
.checkDepends();
1645 return this.super('checkDepends');
1648 write: function() {},
1649 remove: function() {},
1650 cfgvalue: function() { return null },
1651 formvalue: function() { return null }
1654 return L
.Class
.extend({
1656 AbstractSection
: CBIAbstractSection
,
1657 AbstractValue
: CBIAbstractValue
,
1659 TypedSection
: CBITypedSection
,
1660 TableSection
: CBITableSection
,
1661 GridSection
: CBIGridSection
,
1662 NamedSection
: CBINamedSection
,
1665 DynamicList
: CBIDynamicList
,
1666 ListValue
: CBIListValue
,
1668 MultiValue
: CBIMultiValue
,
1669 DummyValue
: CBIDummyValue
,
1670 Button
: CBIButtonValue
,
1671 HiddenValue
: CBIHiddenValue
,
1672 SectionValue
: CBISectionValue