luci-base: CBIFileUpload support file browser mode
[project/luci.git] / modules / luci-base / htdocs / luci-static / resources / ui.js
1 'use strict';
2 'require validation';
3 'require baseclass';
4 'require request';
5 'require session';
6 'require poll';
7 'require dom';
8 'require rpc';
9 'require uci';
10 'require fs';
11
12 var modalDiv = null,
13 tooltipDiv = null,
14 indicatorDiv = null,
15 tooltipTimeout = null;
16
17 /**
18 * @class AbstractElement
19 * @memberof LuCI.ui
20 * @hideconstructor
21 * @classdesc
22 *
23 * The `AbstractElement` class serves as abstract base for the different widgets
24 * implemented by `LuCI.ui`. It provides the common logic for getting and
25 * setting values, for checking the validity state and for wiring up required
26 * events.
27 *
28 * UI widget instances are usually not supposed to be created by view code
29 * directly, instead they're implicitly created by `LuCI.form` when
30 * instantiating CBI forms.
31 *
32 * This class is automatically instantiated as part of `LuCI.ui`. To use it
33 * in views, use `'require ui'` and refer to `ui.AbstractElement`. To import
34 * it in external JavaScript, use `L.require("ui").then(...)` and access the
35 * `AbstractElement` property of the class instance value.
36 */
37 var UIElement = baseclass.extend(/** @lends LuCI.ui.AbstractElement.prototype */ {
38 /**
39 * @typedef {Object} InitOptions
40 * @memberof LuCI.ui.AbstractElement
41 *
42 * @property {string} [id]
43 * Specifies the widget ID to use. It will be used as HTML `id` attribute
44 * on the toplevel widget DOM node.
45 *
46 * @property {string} [name]
47 * Specifies the widget name which is set as HTML `name` attribute on the
48 * corresponding `<input>` element.
49 *
50 * @property {boolean} [optional=true]
51 * Specifies whether the input field allows empty values.
52 *
53 * @property {string} [datatype=string]
54 * An expression describing the input data validation constraints.
55 * It defaults to `string` which will allow any value.
56 * See {@link LuCI.validation} for details on the expression format.
57 *
58 * @property {function} [validator]
59 * Specifies a custom validator function which is invoked after the
60 * standard validation constraints are checked. The function should return
61 * `true` to accept the given input value. Any other return value type is
62 * converted to a string and treated as validation error message.
63 *
64 * @property {boolean} [disabled=false]
65 * Specifies whether the widget should be rendered in disabled state
66 * (`true`) or not (`false`). Disabled widgets cannot be interacted with
67 * and are displayed in a slightly faded style.
68 */
69
70 /**
71 * Read the current value of the input widget.
72 *
73 * @instance
74 * @memberof LuCI.ui.AbstractElement
75 * @returns {string|string[]|null}
76 * The current value of the input element. For simple inputs like text
77 * fields or selects, the return value type will be a - possibly empty -
78 * string. Complex widgets such as `DynamicList` instances may result in
79 * an array of strings or `null` for unset values.
80 */
81 getValue: function() {
82 if (dom.matches(this.node, 'select') || dom.matches(this.node, 'input'))
83 return this.node.value;
84
85 return null;
86 },
87
88 /**
89 * Set the current value of the input widget.
90 *
91 * @instance
92 * @memberof LuCI.ui.AbstractElement
93 * @param {string|string[]|null} value
94 * The value to set the input element to. For simple inputs like text
95 * fields or selects, the value should be a - possibly empty - string.
96 * Complex widgets such as `DynamicList` instances may accept string array
97 * or `null` values.
98 */
99 setValue: function(value) {
100 if (dom.matches(this.node, 'select') || dom.matches(this.node, 'input'))
101 this.node.value = value;
102 },
103
104 /**
105 * Set the current placeholder value of the input widget.
106 *
107 * @instance
108 * @memberof LuCI.ui.AbstractElement
109 * @param {string|string[]|null} value
110 * The placeholder to set for the input element. Only applicable to text
111 * inputs, not to radio buttons, selects or similar.
112 */
113 setPlaceholder: function(value) {
114 var node = this.node ? this.node.querySelector('input,textarea') : null;
115 if (node) {
116 switch (node.getAttribute('type') || 'text') {
117 case 'password':
118 case 'search':
119 case 'tel':
120 case 'text':
121 case 'url':
122 if (value != null && value != '')
123 node.setAttribute('placeholder', value);
124 else
125 node.removeAttribute('placeholder');
126 }
127 }
128 },
129
130 /**
131 * Check whether the input value was altered by the user.
132 *
133 * @instance
134 * @memberof LuCI.ui.AbstractElement
135 * @returns {boolean}
136 * Returns `true` if the input value has been altered by the user or
137 * `false` if it is unchanged. Note that if the user modifies the initial
138 * value and changes it back to the original state, it is still reported
139 * as changed.
140 */
141 isChanged: function() {
142 return (this.node ? this.node.getAttribute('data-changed') : null) == 'true';
143 },
144
145 /**
146 * Check whether the current input value is valid.
147 *
148 * @instance
149 * @memberof LuCI.ui.AbstractElement
150 * @returns {boolean}
151 * Returns `true` if the current input value is valid or `false` if it does
152 * not meet the validation constraints.
153 */
154 isValid: function() {
155 return (this.validState !== false);
156 },
157
158 /**
159 * Returns the current validation error
160 *
161 * @instance
162 * @memberof LuCI.ui.AbstractElement
163 * @returns {string}
164 * The validation error at this time
165 */
166 getValidationError: function() {
167 return this.validationError || '';
168 },
169
170 /**
171 * Force validation of the current input value.
172 *
173 * Usually input validation is automatically triggered by various DOM events
174 * bound to the input widget. In some cases it is required though to manually
175 * trigger validation runs, e.g. when programmatically altering values.
176 *
177 * @instance
178 * @memberof LuCI.ui.AbstractElement
179 */
180 triggerValidation: function() {
181 if (typeof(this.vfunc) != 'function')
182 return false;
183
184 var wasValid = this.isValid();
185
186 this.vfunc();
187
188 return (wasValid != this.isValid());
189 },
190
191 /**
192 * Dispatch a custom (synthetic) event in response to received events.
193 *
194 * Sets up event handlers on the given target DOM node for the given event
195 * names that dispatch a custom event of the given type to the widget root
196 * DOM node.
197 *
198 * The primary purpose of this function is to set up a series of custom
199 * uniform standard events such as `widget-update`, `validation-success`,
200 * `validation-failure` etc. which are triggered by various different
201 * widget specific native DOM events.
202 *
203 * @instance
204 * @memberof LuCI.ui.AbstractElement
205 * @param {Node} targetNode
206 * Specifies the DOM node on which the native event listeners should be
207 * registered.
208 *
209 * @param {string} synevent
210 * The name of the custom event to dispatch to the widget root DOM node.
211 *
212 * @param {string[]} events
213 * The native DOM events for which event handlers should be registered.
214 */
215 registerEvents: function(targetNode, synevent, events) {
216 var dispatchFn = L.bind(function(ev) {
217 this.node.dispatchEvent(new CustomEvent(synevent, { bubbles: true }));
218 }, this);
219
220 for (var i = 0; i < events.length; i++)
221 targetNode.addEventListener(events[i], dispatchFn);
222 },
223
224 /**
225 * Set up listeners for native DOM events that may update the widget value.
226 *
227 * Sets up event handlers on the given target DOM node for the given event
228 * names which may cause the input value to update, such as `keyup` or
229 * `onclick` events. In contrast to change events, such update events will
230 * trigger input value validation.
231 *
232 * @instance
233 * @memberof LuCI.ui.AbstractElement
234 * @param {Node} targetNode
235 * Specifies the DOM node on which the event listeners should be registered.
236 *
237 * @param {...string} events
238 * The DOM events for which event handlers should be registered.
239 */
240 setUpdateEvents: function(targetNode /*, ... */) {
241 var datatype = this.options.datatype,
242 optional = this.options.hasOwnProperty('optional') ? this.options.optional : true,
243 validate = this.options.validate,
244 events = this.varargs(arguments, 1);
245
246 this.registerEvents(targetNode, 'widget-update', events);
247
248 if (!datatype && !validate)
249 return;
250
251 this.vfunc = UI.prototype.addValidator.apply(UI.prototype, [
252 targetNode, datatype || 'string',
253 optional, validate
254 ].concat(events));
255
256 this.node.addEventListener('validation-success', L.bind(function(ev) {
257 this.validState = true;
258 this.validationError = '';
259 }, this));
260
261 this.node.addEventListener('validation-failure', L.bind(function(ev) {
262 this.validState = false;
263 this.validationError = ev.detail.message;
264 }, this));
265 },
266
267 /**
268 * Set up listeners for native DOM events that may change the widget value.
269 *
270 * Sets up event handlers on the given target DOM node for the given event
271 * names which may cause the input value to change completely, such as
272 * `change` events in a select menu. In contrast to update events, such
273 * change events will not trigger input value validation but they may cause
274 * field dependencies to get re-evaluated and will mark the input widget
275 * as dirty.
276 *
277 * @instance
278 * @memberof LuCI.ui.AbstractElement
279 * @param {Node} targetNode
280 * Specifies the DOM node on which the event listeners should be registered.
281 *
282 * @param {...string} events
283 * The DOM events for which event handlers should be registered.
284 */
285 setChangeEvents: function(targetNode /*, ... */) {
286 var tag_changed = L.bind(function(ev) { this.setAttribute('data-changed', true) }, this.node);
287
288 for (var i = 1; i < arguments.length; i++)
289 targetNode.addEventListener(arguments[i], tag_changed);
290
291 this.registerEvents(targetNode, 'widget-change', this.varargs(arguments, 1));
292 },
293
294 /**
295 * Render the widget, set up event listeners and return resulting markup.
296 *
297 * @instance
298 * @memberof LuCI.ui.AbstractElement
299 *
300 * @returns {Node}
301 * Returns a DOM Node or DocumentFragment containing the rendered
302 * widget markup.
303 */
304 render: function() {}
305 });
306
307 /**
308 * Instantiate a text input widget.
309 *
310 * @constructor Textfield
311 * @memberof LuCI.ui
312 * @augments LuCI.ui.AbstractElement
313 *
314 * @classdesc
315 *
316 * The `Textfield` class implements a standard single line text input field.
317 *
318 * UI widget instances are usually not supposed to be created by view code
319 * directly, instead they're implicitly created by `LuCI.form` when
320 * instantiating CBI forms.
321 *
322 * This class is automatically instantiated as part of `LuCI.ui`. To use it
323 * in views, use `'require ui'` and refer to `ui.Textfield`. To import it in
324 * external JavaScript, use `L.require("ui").then(...)` and access the
325 * `Textfield` property of the class instance value.
326 *
327 * @param {string} [value=null]
328 * The initial input value.
329 *
330 * @param {LuCI.ui.Textfield.InitOptions} [options]
331 * Object describing the widget specific options to initialize the input.
332 */
333 var UITextfield = UIElement.extend(/** @lends LuCI.ui.Textfield.prototype */ {
334 /**
335 * In addition to the [AbstractElement.InitOptions]{@link LuCI.ui.AbstractElement.InitOptions}
336 * the following properties are recognized:
337 *
338 * @typedef {LuCI.ui.AbstractElement.InitOptions} InitOptions
339 * @memberof LuCI.ui.Textfield
340 *
341 * @property {boolean} [password=false]
342 * Specifies whether the input should be rendered as concealed password field.
343 *
344 * @property {boolean} [readonly=false]
345 * Specifies whether the input widget should be rendered readonly.
346 *
347 * @property {number} [maxlength]
348 * Specifies the HTML `maxlength` attribute to set on the corresponding
349 * `<input>` element. Note that this a legacy property that exists for
350 * compatibility reasons. It is usually better to `maxlength(N)` validation
351 * expression.
352 *
353 * @property {string} [placeholder]
354 * Specifies the HTML `placeholder` attribute which is displayed when the
355 * corresponding `<input>` element is empty.
356 */
357 __init__: function(value, options) {
358 this.value = value;
359 this.options = Object.assign({
360 optional: true,
361 password: false
362 }, options);
363 },
364
365 /** @override */
366 render: function() {
367 var frameEl = E('div', { 'id': this.options.id });
368 var inputEl = E('input', {
369 'id': this.options.id ? 'widget.' + this.options.id : null,
370 'name': this.options.name,
371 'type': 'text',
372 'class': this.options.password ? 'cbi-input-password' : 'cbi-input-text',
373 'readonly': this.options.readonly ? '' : null,
374 'disabled': this.options.disabled ? '' : null,
375 'maxlength': this.options.maxlength,
376 'placeholder': this.options.placeholder,
377 'autocomplete': this.options.password ? 'new-password' : null,
378 'value': this.value,
379 });
380
381 if (this.options.password) {
382 frameEl.appendChild(E('div', { 'class': 'control-group' }, [
383 inputEl,
384 E('button', {
385 'class': 'cbi-button cbi-button-neutral',
386 'title': _('Reveal/hide password'),
387 'aria-label': _('Reveal/hide password'),
388 'click': function(ev) {
389 var e = this.previousElementSibling;
390 e.type = (e.type === 'password') ? 'text' : 'password';
391 ev.preventDefault();
392 }
393 }, '∗')
394 ]));
395
396 window.requestAnimationFrame(function() { inputEl.type = 'password' });
397 }
398 else {
399 frameEl.appendChild(inputEl);
400 }
401
402 return this.bind(frameEl);
403 },
404
405 /** @private */
406 bind: function(frameEl) {
407 var inputEl = frameEl.querySelector('input');
408
409 this.node = frameEl;
410
411 this.setUpdateEvents(inputEl, 'keyup', 'blur');
412 this.setChangeEvents(inputEl, 'change');
413
414 dom.bindClassInstance(frameEl, this);
415
416 return frameEl;
417 },
418
419 /** @override */
420 getValue: function() {
421 var inputEl = this.node.querySelector('input');
422 return inputEl.value;
423 },
424
425 /** @override */
426 setValue: function(value) {
427 var inputEl = this.node.querySelector('input');
428 inputEl.value = value;
429 }
430 });
431
432 /**
433 * Instantiate a textarea widget.
434 *
435 * @constructor Textarea
436 * @memberof LuCI.ui
437 * @augments LuCI.ui.AbstractElement
438 *
439 * @classdesc
440 *
441 * The `Textarea` class implements a multiline text area input field.
442 *
443 * UI widget instances are usually not supposed to be created by view code
444 * directly, instead they're implicitly created by `LuCI.form` when
445 * instantiating CBI forms.
446 *
447 * This class is automatically instantiated as part of `LuCI.ui`. To use it
448 * in views, use `'require ui'` and refer to `ui.Textarea`. To import it in
449 * external JavaScript, use `L.require("ui").then(...)` and access the
450 * `Textarea` property of the class instance value.
451 *
452 * @param {string} [value=null]
453 * The initial input value.
454 *
455 * @param {LuCI.ui.Textarea.InitOptions} [options]
456 * Object describing the widget specific options to initialize the input.
457 */
458 var UITextarea = UIElement.extend(/** @lends LuCI.ui.Textarea.prototype */ {
459 /**
460 * In addition to the [AbstractElement.InitOptions]{@link LuCI.ui.AbstractElement.InitOptions}
461 * the following properties are recognized:
462 *
463 * @typedef {LuCI.ui.AbstractElement.InitOptions} InitOptions
464 * @memberof LuCI.ui.Textarea
465 *
466 * @property {boolean} [readonly=false]
467 * Specifies whether the input widget should be rendered readonly.
468 *
469 * @property {string} [placeholder]
470 * Specifies the HTML `placeholder` attribute which is displayed when the
471 * corresponding `<textarea>` element is empty.
472 *
473 * @property {boolean} [monospace=false]
474 * Specifies whether a monospace font should be forced for the textarea
475 * contents.
476 *
477 * @property {number} [cols]
478 * Specifies the HTML `cols` attribute to set on the corresponding
479 * `<textarea>` element.
480 *
481 * @property {number} [rows]
482 * Specifies the HTML `rows` attribute to set on the corresponding
483 * `<textarea>` element.
484 *
485 * @property {boolean} [wrap=false]
486 * Specifies whether the HTML `wrap` attribute should be set.
487 */
488 __init__: function(value, options) {
489 this.value = value;
490 this.options = Object.assign({
491 optional: true,
492 wrap: false,
493 cols: null,
494 rows: null
495 }, options);
496 },
497
498 /** @override */
499 render: function() {
500 var style = !this.options.cols ? 'width:100%' : null,
501 frameEl = E('div', { 'id': this.options.id, 'style': style }),
502 value = (this.value != null) ? String(this.value) : '';
503
504 frameEl.appendChild(E('textarea', {
505 'id': this.options.id ? 'widget.' + this.options.id : null,
506 'name': this.options.name,
507 'class': 'cbi-input-textarea',
508 'readonly': this.options.readonly ? '' : null,
509 'disabled': this.options.disabled ? '' : null,
510 'placeholder': this.options.placeholder,
511 'style': style,
512 'cols': this.options.cols,
513 'rows': this.options.rows,
514 'wrap': this.options.wrap ? '' : null
515 }, [ value ]));
516
517 if (this.options.monospace)
518 frameEl.firstElementChild.style.fontFamily = 'monospace';
519
520 return this.bind(frameEl);
521 },
522
523 /** @private */
524 bind: function(frameEl) {
525 var inputEl = frameEl.firstElementChild;
526
527 this.node = frameEl;
528
529 this.setUpdateEvents(inputEl, 'keyup', 'blur');
530 this.setChangeEvents(inputEl, 'change');
531
532 dom.bindClassInstance(frameEl, this);
533
534 return frameEl;
535 },
536
537 /** @override */
538 getValue: function() {
539 return this.node.firstElementChild.value;
540 },
541
542 /** @override */
543 setValue: function(value) {
544 this.node.firstElementChild.value = value;
545 }
546 });
547
548 /**
549 * Instantiate a checkbox widget.
550 *
551 * @constructor Checkbox
552 * @memberof LuCI.ui
553 * @augments LuCI.ui.AbstractElement
554 *
555 * @classdesc
556 *
557 * The `Checkbox` class implements a simple checkbox input field.
558 *
559 * UI widget instances are usually not supposed to be created by view code
560 * directly, instead they're implicitly created by `LuCI.form` when
561 * instantiating CBI forms.
562 *
563 * This class is automatically instantiated as part of `LuCI.ui`. To use it
564 * in views, use `'require ui'` and refer to `ui.Checkbox`. To import it in
565 * external JavaScript, use `L.require("ui").then(...)` and access the
566 * `Checkbox` property of the class instance value.
567 *
568 * @param {string} [value=null]
569 * The initial input value.
570 *
571 * @param {LuCI.ui.Checkbox.InitOptions} [options]
572 * Object describing the widget specific options to initialize the input.
573 */
574 var UICheckbox = UIElement.extend(/** @lends LuCI.ui.Checkbox.prototype */ {
575 /**
576 * In addition to the [AbstractElement.InitOptions]{@link LuCI.ui.AbstractElement.InitOptions}
577 * the following properties are recognized:
578 *
579 * @typedef {LuCI.ui.AbstractElement.InitOptions} InitOptions
580 * @memberof LuCI.ui.Checkbox
581 *
582 * @property {string} [value_enabled=1]
583 * Specifies the value corresponding to a checked checkbox.
584 *
585 * @property {string} [value_disabled=0]
586 * Specifies the value corresponding to an unchecked checkbox.
587 *
588 * @property {string} [hiddenname]
589 * Specifies the HTML `name` attribute of the hidden input backing the
590 * checkbox. This is a legacy property existing for compatibility reasons,
591 * it is required for HTML based form submissions.
592 */
593 __init__: function(value, options) {
594 this.value = value;
595 this.options = Object.assign({
596 value_enabled: '1',
597 value_disabled: '0'
598 }, options);
599 },
600
601 /** @override */
602 render: function() {
603 var id = 'cb%08x'.format(Math.random() * 0xffffffff);
604 var frameEl = E('div', {
605 'id': this.options.id,
606 'class': 'cbi-checkbox'
607 });
608
609 if (this.options.hiddenname)
610 frameEl.appendChild(E('input', {
611 'type': 'hidden',
612 'name': this.options.hiddenname,
613 'value': 1
614 }));
615
616 frameEl.appendChild(E('input', {
617 'id': id,
618 'name': this.options.name,
619 'type': 'checkbox',
620 'value': this.options.value_enabled,
621 'checked': (this.value == this.options.value_enabled) ? '' : null,
622 'disabled': this.options.disabled ? '' : null,
623 'data-widget-id': this.options.id ? 'widget.' + this.options.id : null
624 }));
625
626 frameEl.appendChild(E('label', { 'for': id }));
627
628 if (this.options.tooltip != null) {
629 var icon = "⚠️";
630
631 if (this.options.tooltipicon != null)
632 icon = this.options.tooltipicon;
633
634 frameEl.appendChild(
635 E('label', { 'class': 'cbi-tooltip-container' },[
636 icon,
637 E('div', { 'class': 'cbi-tooltip' },
638 this.options.tooltip
639 )
640 ])
641 );
642 }
643
644 return this.bind(frameEl);
645 },
646
647 /** @private */
648 bind: function(frameEl) {
649 this.node = frameEl;
650
651 var input = frameEl.querySelector('input[type="checkbox"]');
652 this.setUpdateEvents(input, 'click', 'blur');
653 this.setChangeEvents(input, 'change');
654
655 dom.bindClassInstance(frameEl, this);
656
657 return frameEl;
658 },
659
660 /**
661 * Test whether the checkbox is currently checked.
662 *
663 * @instance
664 * @memberof LuCI.ui.Checkbox
665 * @returns {boolean}
666 * Returns `true` when the checkbox is currently checked, otherwise `false`.
667 */
668 isChecked: function() {
669 return this.node.querySelector('input[type="checkbox"]').checked;
670 },
671
672 /** @override */
673 getValue: function() {
674 return this.isChecked()
675 ? this.options.value_enabled
676 : this.options.value_disabled;
677 },
678
679 /** @override */
680 setValue: function(value) {
681 this.node.querySelector('input[type="checkbox"]').checked = (value == this.options.value_enabled);
682 }
683 });
684
685 /**
686 * Instantiate a select dropdown or checkbox/radiobutton group.
687 *
688 * @constructor Select
689 * @memberof LuCI.ui
690 * @augments LuCI.ui.AbstractElement
691 *
692 * @classdesc
693 *
694 * The `Select` class implements either a traditional HTML `<select>` element
695 * or a group of checkboxes or radio buttons, depending on whether multiple
696 * values are enabled or not.
697 *
698 * UI widget instances are usually not supposed to be created by view code
699 * directly, instead they're implicitly created by `LuCI.form` when
700 * instantiating CBI forms.
701 *
702 * This class is automatically instantiated as part of `LuCI.ui`. To use it
703 * in views, use `'require ui'` and refer to `ui.Select`. To import it in
704 * external JavaScript, use `L.require("ui").then(...)` and access the
705 * `Select` property of the class instance value.
706 *
707 * @param {string|string[]} [value=null]
708 * The initial input value(s).
709 *
710 * @param {Object<string, string>} choices
711 * Object containing the selectable choices of the widget. The object keys
712 * serve as values for the different choices while the values are used as
713 * choice labels.
714 *
715 * @param {LuCI.ui.Select.InitOptions} [options]
716 * Object describing the widget specific options to initialize the inputs.
717 */
718 var UISelect = UIElement.extend(/** @lends LuCI.ui.Select.prototype */ {
719 /**
720 * In addition to the [AbstractElement.InitOptions]{@link LuCI.ui.AbstractElement.InitOptions}
721 * the following properties are recognized:
722 *
723 * @typedef {LuCI.ui.AbstractElement.InitOptions} InitOptions
724 * @memberof LuCI.ui.Select
725 *
726 * @property {boolean} [multiple=false]
727 * Specifies whether multiple choice values may be selected.
728 *
729 * @property {"select"|"individual"} [widget=select]
730 * Specifies the kind of widget to render. May be either `select` or
731 * `individual`. When set to `select` an HTML `<select>` element will be
732 * used, otherwise a group of checkbox or radio button elements is created,
733 * depending on the value of the `multiple` option.
734 *
735 * @property {string} [orientation=horizontal]
736 * Specifies whether checkbox / radio button groups should be rendered
737 * in a `horizontal` or `vertical` manner. Does not apply to the `select`
738 * widget type.
739 *
740 * @property {boolean|string[]} [sort=false]
741 * Specifies if and how to sort choice values. If set to `true`, the choice
742 * values will be sorted alphabetically. If set to an array of strings, the
743 * choice sort order is derived from the array.
744 *
745 * @property {number} [size]
746 * Specifies the HTML `size` attribute to set on the `<select>` element.
747 * Only applicable to the `select` widget type.
748 *
749 * @property {string} [placeholder=-- Please choose --]
750 * Specifies a placeholder text which is displayed when no choice is
751 * selected yet. Only applicable to the `select` widget type.
752 */
753 __init__: function(value, choices, options) {
754 if (!L.isObject(choices))
755 choices = {};
756
757 if (!Array.isArray(value))
758 value = (value != null && value != '') ? [ value ] : [];
759
760 if (!options.multiple && value.length > 1)
761 value.length = 1;
762
763 this.values = value;
764 this.choices = choices;
765 this.options = Object.assign({
766 multiple: false,
767 widget: 'select',
768 orientation: 'horizontal'
769 }, options);
770
771 if (this.choices.hasOwnProperty(''))
772 this.options.optional = true;
773 },
774
775 /** @override */
776 render: function() {
777 var frameEl = E('div', { 'id': this.options.id }),
778 keys = Object.keys(this.choices);
779
780 if (this.options.sort === true)
781 keys.sort(L.naturalCompare);
782 else if (Array.isArray(this.options.sort))
783 keys = this.options.sort;
784
785 if (this.options.widget != 'radio' && this.options.widget != 'checkbox') {
786 frameEl.appendChild(E('select', {
787 'id': this.options.id ? 'widget.' + this.options.id : null,
788 'name': this.options.name,
789 'size': this.options.size,
790 'class': 'cbi-input-select',
791 'multiple': this.options.multiple ? '' : null,
792 'disabled': this.options.disabled ? '' : null
793 }));
794
795 if (this.options.optional)
796 frameEl.lastChild.appendChild(E('option', {
797 'value': '',
798 'selected': (this.values.length == 0 || this.values[0] == '') ? '' : null
799 }, [ this.choices[''] || this.options.placeholder || _('-- Please choose --') ]));
800
801 for (var i = 0; i < keys.length; i++) {
802 if (keys[i] == null || keys[i] == '')
803 continue;
804
805 frameEl.lastChild.appendChild(E('option', {
806 'value': keys[i],
807 'selected': (this.values.indexOf(keys[i]) > -1) ? '' : null
808 }, [ this.choices[keys[i]] || keys[i] ]));
809 }
810 }
811 else {
812 var brEl = (this.options.orientation === 'horizontal') ? document.createTextNode(' \xa0 ') : E('br');
813
814 for (var i = 0; i < keys.length; i++) {
815 frameEl.appendChild(E('span', {
816 'class': 'cbi-%s'.format(this.options.multiple ? 'checkbox' : 'radio')
817 }, [
818 E('input', {
819 'id': this.options.id ? 'widget.%s.%d'.format(this.options.id, i) : null,
820 'name': this.options.id || this.options.name,
821 'type': this.options.multiple ? 'checkbox' : 'radio',
822 'class': this.options.multiple ? 'cbi-input-checkbox' : 'cbi-input-radio',
823 'value': keys[i],
824 'checked': (this.values.indexOf(keys[i]) > -1) ? '' : null,
825 'disabled': this.options.disabled ? '' : null
826 }),
827 E('label', { 'for': this.options.id ? 'widget.%s.%d'.format(this.options.id, i) : null }),
828 E('span', {
829 'click': function(ev) {
830 ev.currentTarget.previousElementSibling.previousElementSibling.click();
831 }
832 }, [ this.choices[keys[i]] || keys[i] ])
833 ]));
834
835 frameEl.appendChild(brEl.cloneNode());
836 }
837 }
838
839 return this.bind(frameEl);
840 },
841
842 /** @private */
843 bind: function(frameEl) {
844 this.node = frameEl;
845
846 if (this.options.widget != 'radio' && this.options.widget != 'checkbox') {
847 this.setUpdateEvents(frameEl.firstChild, 'change', 'click', 'blur');
848 this.setChangeEvents(frameEl.firstChild, 'change');
849 }
850 else {
851 var radioEls = frameEl.querySelectorAll('input[type="radio"]');
852 for (var i = 0; i < radioEls.length; i++) {
853 this.setUpdateEvents(radioEls[i], 'change', 'click', 'blur');
854 this.setChangeEvents(radioEls[i], 'change', 'click', 'blur');
855 }
856 }
857
858 dom.bindClassInstance(frameEl, this);
859
860 return frameEl;
861 },
862
863 /** @override */
864 getValue: function() {
865 if (this.options.widget != 'radio' && this.options.widget != 'checkbox')
866 return this.node.firstChild.value;
867
868 var radioEls = this.node.querySelectorAll('input[type="radio"]');
869 for (var i = 0; i < radioEls.length; i++)
870 if (radioEls[i].checked)
871 return radioEls[i].value;
872
873 return null;
874 },
875
876 /** @override */
877 setValue: function(value) {
878 if (this.options.widget != 'radio' && this.options.widget != 'checkbox') {
879 if (value == null)
880 value = '';
881
882 for (var i = 0; i < this.node.firstChild.options.length; i++)
883 this.node.firstChild.options[i].selected = (this.node.firstChild.options[i].value == value);
884
885 return;
886 }
887
888 var radioEls = frameEl.querySelectorAll('input[type="radio"]');
889 for (var i = 0; i < radioEls.length; i++)
890 radioEls[i].checked = (radioEls[i].value == value);
891 }
892 });
893
894 /**
895 * Instantiate a rich dropdown choice widget.
896 *
897 * @constructor Dropdown
898 * @memberof LuCI.ui
899 * @augments LuCI.ui.AbstractElement
900 *
901 * @classdesc
902 *
903 * The `Dropdown` class implements a rich, stylable dropdown menu which
904 * supports non-text choice labels.
905 *
906 * UI widget instances are usually not supposed to be created by view code
907 * directly, instead they're implicitly created by `LuCI.form` when
908 * instantiating CBI forms.
909 *
910 * This class is automatically instantiated as part of `LuCI.ui`. To use it
911 * in views, use `'require ui'` and refer to `ui.Dropdown`. To import it in
912 * external JavaScript, use `L.require("ui").then(...)` and access the
913 * `Dropdown` property of the class instance value.
914 *
915 * @param {string|string[]} [value=null]
916 * The initial input value(s).
917 *
918 * @param {Object<string, *>} choices
919 * Object containing the selectable choices of the widget. The object keys
920 * serve as values for the different choices while the values are used as
921 * choice labels.
922 *
923 * @param {LuCI.ui.Dropdown.InitOptions} [options]
924 * Object describing the widget specific options to initialize the dropdown.
925 */
926 var UIDropdown = UIElement.extend(/** @lends LuCI.ui.Dropdown.prototype */ {
927 /**
928 * In addition to the [AbstractElement.InitOptions]{@link LuCI.ui.AbstractElement.InitOptions}
929 * the following properties are recognized:
930 *
931 * @typedef {LuCI.ui.AbstractElement.InitOptions} InitOptions
932 * @memberof LuCI.ui.Dropdown
933 *
934 * @property {boolean} [optional=true]
935 * Specifies whether the dropdown selection is optional. In contrast to
936 * other widgets, the `optional` constraint of dropdowns works differently;
937 * instead of marking the widget invalid on empty values when set to `false`,
938 * the user is not allowed to deselect all choices.
939 *
940 * For single value dropdowns that means that no empty "please select"
941 * choice is offered and for multi value dropdowns, the last selected choice
942 * may not be deselected without selecting another choice first.
943 *
944 * @property {boolean} [multiple]
945 * Specifies whether multiple choice values may be selected. It defaults
946 * to `true` when an array is passed as input value to the constructor.
947 *
948 * @property {boolean|string[]} [sort=false]
949 * Specifies if and how to sort choice values. If set to `true`, the choice
950 * values will be sorted alphabetically. If set to an array of strings, the
951 * choice sort order is derived from the array.
952 *
953 * @property {string} [select_placeholder=-- Please choose --]
954 * Specifies a placeholder text which is displayed when no choice is
955 * selected yet.
956 *
957 * @property {string} [custom_placeholder=-- custom --]
958 * Specifies a placeholder text which is displayed in the text input
959 * field allowing to enter custom choice values. Only applicable if the
960 * `create` option is set to `true`.
961 *
962 * @property {boolean} [create=false]
963 * Specifies whether custom choices may be entered into the dropdown
964 * widget.
965 *
966 * @property {string} [create_query=.create-item-input]
967 * Specifies a CSS selector expression used to find the input element
968 * which is used to enter custom choice values. This should not normally
969 * be used except by widgets derived from the Dropdown class.
970 *
971 * @property {string} [create_template=script[type="item-template"]]
972 * Specifies a CSS selector expression used to find an HTML element
973 * serving as template for newly added custom choice values.
974 *
975 * Any `{{value}}` placeholder string within the template elements text
976 * content will be replaced by the user supplied choice value, the
977 * resulting string is parsed as HTML and appended to the end of the
978 * choice list. The template markup may specify one HTML element with a
979 * `data-label-placeholder` attribute which is replaced by a matching
980 * label value from the `choices` object or with the user supplied value
981 * itself in case `choices` contains no matching choice label.
982 *
983 * If the template element is not found or if no `create_template` selector
984 * expression is specified, the default markup for newly created elements is
985 * `<li data-value="{{value}}"><span data-label-placeholder="true" /></li>`.
986 *
987 * @property {string} [create_markup]
988 * This property allows specifying the markup for custom choices directly
989 * instead of referring to a template element through CSS selectors.
990 *
991 * Apart from that it works exactly like `create_template`.
992 *
993 * @property {number} [display_items=3]
994 * Specifies the maximum amount of choice labels that should be shown in
995 * collapsed dropdown state before further selected choices are cut off.
996 *
997 * Only applicable when `multiple` is `true`.
998 *
999 * @property {number} [dropdown_items=-1]
1000 * Specifies the maximum amount of choices that should be shown when the
1001 * dropdown is open. If the amount of available choices exceeds this number,
1002 * the dropdown area must be scrolled to reach further items.
1003 *
1004 * If set to `-1`, the dropdown menu will attempt to show all choice values
1005 * and only resort to scrolling if the amount of choices exceeds the available
1006 * screen space above and below the dropdown widget.
1007 *
1008 * @property {string} [placeholder]
1009 * This property serves as a shortcut to set both `select_placeholder` and
1010 * `custom_placeholder`. Either of these properties will fallback to
1011 * `placeholder` if not specified.
1012 *
1013 * @property {boolean} [readonly=false]
1014 * Specifies whether the custom choice input field should be rendered
1015 * readonly. Only applicable when `create` is `true`.
1016 *
1017 * @property {number} [maxlength]
1018 * Specifies the HTML `maxlength` attribute to set on the custom choice
1019 * `<input>` element. Note that this a legacy property that exists for
1020 * compatibility reasons. It is usually better to `maxlength(N)` validation
1021 * expression. Only applicable when `create` is `true`.
1022 */
1023 __init__: function(value, choices, options) {
1024 if (typeof(choices) != 'object')
1025 choices = {};
1026
1027 if (!Array.isArray(value))
1028 this.values = (value != null && value != '') ? [ value ] : [];
1029 else
1030 this.values = value;
1031
1032 this.choices = choices;
1033 this.options = Object.assign({
1034 sort: true,
1035 multiple: Array.isArray(value),
1036 optional: true,
1037 select_placeholder: _('-- Please choose --'),
1038 custom_placeholder: _('-- custom --'),
1039 display_items: 3,
1040 dropdown_items: -1,
1041 create: false,
1042 create_query: '.create-item-input',
1043 create_template: 'script[type="item-template"]'
1044 }, options);
1045 },
1046
1047 /** @override */
1048 render: function() {
1049 var sb = E('div', {
1050 'id': this.options.id,
1051 'class': 'cbi-dropdown',
1052 'multiple': this.options.multiple ? '' : null,
1053 'optional': this.options.optional ? '' : null,
1054 'disabled': this.options.disabled ? '' : null,
1055 'tabindex': -1
1056 }, E('ul'));
1057
1058 var keys = Object.keys(this.choices);
1059
1060 if (this.options.sort === true)
1061 keys.sort(L.naturalCompare);
1062 else if (Array.isArray(this.options.sort))
1063 keys = this.options.sort;
1064
1065 if (this.options.create)
1066 for (var i = 0; i < this.values.length; i++)
1067 if (!this.choices.hasOwnProperty(this.values[i]))
1068 keys.push(this.values[i]);
1069
1070 for (var i = 0; i < keys.length; i++) {
1071 var label = this.choices[keys[i]];
1072
1073 if (dom.elem(label))
1074 label = label.cloneNode(true);
1075
1076 sb.lastElementChild.appendChild(E('li', {
1077 'data-value': keys[i],
1078 'selected': (this.values.indexOf(keys[i]) > -1) ? '' : null
1079 }, [ label || keys[i] ]));
1080 }
1081
1082 if (this.options.create) {
1083 var createEl = E('input', {
1084 'type': 'text',
1085 'class': 'create-item-input',
1086 'readonly': this.options.readonly ? '' : null,
1087 'maxlength': this.options.maxlength,
1088 'placeholder': this.options.custom_placeholder || this.options.placeholder
1089 });
1090
1091 if (this.options.datatype || this.options.validate)
1092 UI.prototype.addValidator(createEl, this.options.datatype || 'string',
1093 true, this.options.validate, 'blur', 'keyup');
1094
1095 sb.lastElementChild.appendChild(E('li', { 'data-value': '-' }, createEl));
1096 }
1097
1098 if (this.options.create_markup)
1099 sb.appendChild(E('script', { type: 'item-template' },
1100 this.options.create_markup));
1101
1102 return this.bind(sb);
1103 },
1104
1105 /** @private */
1106 bind: function(sb) {
1107 var o = this.options;
1108
1109 o.multiple = sb.hasAttribute('multiple');
1110 o.optional = sb.hasAttribute('optional');
1111 o.placeholder = sb.getAttribute('placeholder') || o.placeholder;
1112 o.display_items = parseInt(sb.getAttribute('display-items') || o.display_items);
1113 o.dropdown_items = parseInt(sb.getAttribute('dropdown-items') || o.dropdown_items);
1114 o.create_query = sb.getAttribute('item-create') || o.create_query;
1115 o.create_template = sb.getAttribute('item-template') || o.create_template;
1116
1117 var ul = sb.querySelector('ul'),
1118 more = sb.appendChild(E('span', { class: 'more', tabindex: -1 }, '···')),
1119 open = sb.appendChild(E('span', { class: 'open', tabindex: -1 }, '▾')),
1120 canary = sb.appendChild(E('div')),
1121 create = sb.querySelector(this.options.create_query),
1122 ndisplay = this.options.display_items,
1123 n = 0;
1124
1125 if (this.options.multiple) {
1126 var items = ul.querySelectorAll('li');
1127
1128 for (var i = 0; i < items.length; i++) {
1129 this.transformItem(sb, items[i]);
1130
1131 if (items[i].hasAttribute('selected') && ndisplay-- > 0)
1132 items[i].setAttribute('display', n++);
1133 }
1134 }
1135 else {
1136 if (this.options.optional && !ul.querySelector('li[data-value=""]')) {
1137 var placeholder = E('li', { placeholder: '' },
1138 this.options.select_placeholder || this.options.placeholder);
1139
1140 ul.firstChild
1141 ? ul.insertBefore(placeholder, ul.firstChild)
1142 : ul.appendChild(placeholder);
1143 }
1144
1145 var items = ul.querySelectorAll('li'),
1146 sel = sb.querySelectorAll('[selected]');
1147
1148 sel.forEach(function(s) {
1149 s.removeAttribute('selected');
1150 });
1151
1152 var s = sel[0] || items[0];
1153 if (s) {
1154 s.setAttribute('selected', '');
1155 s.setAttribute('display', n++);
1156 }
1157
1158 ndisplay--;
1159 }
1160
1161 this.saveValues(sb, ul);
1162
1163 ul.setAttribute('tabindex', -1);
1164 sb.setAttribute('tabindex', 0);
1165
1166 if (ndisplay < 0)
1167 sb.setAttribute('more', '')
1168 else
1169 sb.removeAttribute('more');
1170
1171 if (ndisplay == this.options.display_items)
1172 sb.setAttribute('empty', '')
1173 else
1174 sb.removeAttribute('empty');
1175
1176 dom.content(more, (ndisplay == this.options.display_items)
1177 ? (this.options.select_placeholder || this.options.placeholder) : '···');
1178
1179
1180 sb.addEventListener('click', this.handleClick.bind(this));
1181 sb.addEventListener('keydown', this.handleKeydown.bind(this));
1182 sb.addEventListener('cbi-dropdown-close', this.handleDropdownClose.bind(this));
1183 sb.addEventListener('cbi-dropdown-select', this.handleDropdownSelect.bind(this));
1184
1185 if ('ontouchstart' in window) {
1186 sb.addEventListener('touchstart', function(ev) { ev.stopPropagation(); });
1187 window.addEventListener('touchstart', this.closeAllDropdowns);
1188 }
1189 else {
1190 sb.addEventListener('focus', this.handleFocus.bind(this));
1191
1192 canary.addEventListener('focus', this.handleCanaryFocus.bind(this));
1193
1194 window.addEventListener('click', this.closeAllDropdowns);
1195 }
1196
1197 if (create) {
1198 create.addEventListener('keydown', this.handleCreateKeydown.bind(this));
1199 create.addEventListener('focus', this.handleCreateFocus.bind(this));
1200 create.addEventListener('blur', this.handleCreateBlur.bind(this));
1201
1202 var li = findParent(create, 'li');
1203
1204 li.setAttribute('unselectable', '');
1205 li.addEventListener('click', this.handleCreateClick.bind(this));
1206 }
1207
1208 this.node = sb;
1209
1210 this.setUpdateEvents(sb, 'cbi-dropdown-open', 'cbi-dropdown-close');
1211 this.setChangeEvents(sb, 'cbi-dropdown-change', 'cbi-dropdown-close');
1212
1213 dom.bindClassInstance(sb, this);
1214
1215 return sb;
1216 },
1217
1218 /** @private */
1219 getScrollParent: function(element) {
1220 var parent = element,
1221 style = getComputedStyle(element),
1222 excludeStaticParent = (style.position === 'absolute');
1223
1224 if (style.position === 'fixed')
1225 return document.body;
1226
1227 while ((parent = parent.parentElement) != null) {
1228 style = getComputedStyle(parent);
1229
1230 if (excludeStaticParent && style.position === 'static')
1231 continue;
1232
1233 if (/(auto|scroll)/.test(style.overflow + style.overflowY + style.overflowX))
1234 return parent;
1235 }
1236
1237 return document.body;
1238 },
1239
1240 /** @private */
1241 openDropdown: function(sb) {
1242 var st = window.getComputedStyle(sb, null),
1243 ul = sb.querySelector('ul'),
1244 li = ul.querySelectorAll('li'),
1245 fl = findParent(sb, '.cbi-value-field'),
1246 sel = ul.querySelector('[selected]'),
1247 rect = sb.getBoundingClientRect(),
1248 items = Math.min(this.options.dropdown_items, li.length),
1249 scrollParent = this.getScrollParent(sb);
1250
1251 document.querySelectorAll('.cbi-dropdown[open]').forEach(function(s) {
1252 s.dispatchEvent(new CustomEvent('cbi-dropdown-close', {}));
1253 });
1254
1255 sb.setAttribute('open', '');
1256
1257 var pv = ul.cloneNode(true);
1258 pv.classList.add('preview');
1259
1260 if (fl)
1261 fl.classList.add('cbi-dropdown-open');
1262
1263 if ('ontouchstart' in window) {
1264 var vpWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0),
1265 vpHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0),
1266 start = null;
1267
1268 ul.style.top = sb.offsetHeight + 'px';
1269 ul.style.left = -rect.left + 'px';
1270 ul.style.right = (rect.right - vpWidth) + 'px';
1271 ul.style.maxHeight = (vpHeight * 0.5) + 'px';
1272 ul.style.WebkitOverflowScrolling = 'touch';
1273
1274 var scrollFrom = scrollParent.scrollTop,
1275 scrollTo = scrollFrom + rect.top - vpHeight * 0.5;
1276
1277 var scrollStep = function(timestamp) {
1278 if (!start) {
1279 start = timestamp;
1280 ul.scrollTop = sel ? Math.max(sel.offsetTop - sel.offsetHeight, 0) : 0;
1281 }
1282
1283 var duration = Math.max(timestamp - start, 1);
1284 if (duration < 100) {
1285 scrollParent.scrollTop = scrollFrom + (scrollTo - scrollFrom) * (duration / 100);
1286 window.requestAnimationFrame(scrollStep);
1287 }
1288 else {
1289 scrollParent.scrollTop = scrollTo;
1290 }
1291 };
1292
1293 window.requestAnimationFrame(scrollStep);
1294 }
1295 else {
1296 ul.style.maxHeight = '1px';
1297 ul.style.top = ul.style.bottom = '';
1298
1299 window.requestAnimationFrame(function() {
1300 var containerRect = scrollParent.getBoundingClientRect(),
1301 itemHeight = li[Math.max(0, li.length - 2)].getBoundingClientRect().height,
1302 fullHeight = 0,
1303 spaceAbove = rect.top - containerRect.top,
1304 spaceBelow = containerRect.bottom - rect.bottom;
1305
1306 for (var i = 0; i < (items == -1 ? li.length : items); i++)
1307 fullHeight += li[i].getBoundingClientRect().height;
1308
1309 if (fullHeight <= spaceBelow) {
1310 ul.style.top = rect.height + 'px';
1311 ul.style.maxHeight = spaceBelow + 'px';
1312 }
1313 else if (fullHeight <= spaceAbove) {
1314 ul.style.bottom = rect.height + 'px';
1315 ul.style.maxHeight = spaceAbove + 'px';
1316 }
1317 else if (spaceBelow >= spaceAbove) {
1318 ul.style.top = rect.height + 'px';
1319 ul.style.maxHeight = (spaceBelow - (spaceBelow % itemHeight)) + 'px';
1320 }
1321 else {
1322 ul.style.bottom = rect.height + 'px';
1323 ul.style.maxHeight = (spaceAbove - (spaceAbove % itemHeight)) + 'px';
1324 }
1325
1326 ul.scrollTop = sel ? Math.max(sel.offsetTop - sel.offsetHeight, 0) : 0;
1327 });
1328 }
1329
1330 var cboxes = ul.querySelectorAll('[selected] input[type="checkbox"]');
1331 for (var i = 0; i < cboxes.length; i++) {
1332 cboxes[i].checked = true;
1333 cboxes[i].disabled = (cboxes.length == 1 && !this.options.optional);
1334 };
1335
1336 ul.classList.add('dropdown');
1337
1338 sb.insertBefore(pv, ul.nextElementSibling);
1339
1340 li.forEach(function(l) {
1341 if (!l.hasAttribute('unselectable'))
1342 l.setAttribute('tabindex', 0);
1343 });
1344
1345 sb.lastElementChild.setAttribute('tabindex', 0);
1346
1347 var focusFn = L.bind(function(el) {
1348 this.setFocus(sb, el, true);
1349 ul.removeEventListener('transitionend', focusFn);
1350 }, this, sel || li[0]);
1351
1352 ul.addEventListener('transitionend', focusFn);
1353 },
1354
1355 /** @private */
1356 closeDropdown: function(sb, no_focus) {
1357 if (!sb.hasAttribute('open'))
1358 return;
1359
1360 var pv = sb.querySelector('ul.preview'),
1361 ul = sb.querySelector('ul.dropdown'),
1362 li = ul.querySelectorAll('li'),
1363 fl = findParent(sb, '.cbi-value-field');
1364
1365 li.forEach(function(l) { l.removeAttribute('tabindex'); });
1366 sb.lastElementChild.removeAttribute('tabindex');
1367
1368 sb.removeChild(pv);
1369 sb.removeAttribute('open');
1370 sb.style.width = sb.style.height = '';
1371
1372 ul.classList.remove('dropdown');
1373 ul.style.top = ul.style.bottom = ul.style.maxHeight = '';
1374
1375 if (fl)
1376 fl.classList.remove('cbi-dropdown-open');
1377
1378 if (!no_focus)
1379 this.setFocus(sb, sb);
1380
1381 this.saveValues(sb, ul);
1382 },
1383
1384 /** @private */
1385 toggleItem: function(sb, li, force_state) {
1386 var ul = li.parentNode;
1387
1388 if (li.hasAttribute('unselectable'))
1389 return;
1390
1391 if (this.options.multiple) {
1392 var cbox = li.querySelector('input[type="checkbox"]'),
1393 items = li.parentNode.querySelectorAll('li'),
1394 label = sb.querySelector('ul.preview'),
1395 sel = li.parentNode.querySelectorAll('[selected]').length,
1396 more = sb.querySelector('.more'),
1397 ndisplay = this.options.display_items,
1398 n = 0;
1399
1400 if (li.hasAttribute('selected')) {
1401 if (force_state !== true) {
1402 if (sel > 1 || this.options.optional) {
1403 li.removeAttribute('selected');
1404 cbox.checked = cbox.disabled = false;
1405 sel--;
1406 }
1407 else {
1408 cbox.disabled = true;
1409 }
1410 }
1411 }
1412 else {
1413 if (force_state !== false) {
1414 li.setAttribute('selected', '');
1415 cbox.checked = true;
1416 cbox.disabled = false;
1417 sel++;
1418 }
1419 }
1420
1421 while (label && label.firstElementChild)
1422 label.removeChild(label.firstElementChild);
1423
1424 for (var i = 0; i < items.length; i++) {
1425 items[i].removeAttribute('display');
1426 if (items[i].hasAttribute('selected')) {
1427 if (ndisplay-- > 0) {
1428 items[i].setAttribute('display', n++);
1429 if (label)
1430 label.appendChild(items[i].cloneNode(true));
1431 }
1432 var c = items[i].querySelector('input[type="checkbox"]');
1433 if (c)
1434 c.disabled = (sel == 1 && !this.options.optional);
1435 }
1436 }
1437
1438 if (ndisplay < 0)
1439 sb.setAttribute('more', '');
1440 else
1441 sb.removeAttribute('more');
1442
1443 if (ndisplay === this.options.display_items)
1444 sb.setAttribute('empty', '');
1445 else
1446 sb.removeAttribute('empty');
1447
1448 dom.content(more, (ndisplay === this.options.display_items)
1449 ? (this.options.select_placeholder || this.options.placeholder) : '···');
1450 }
1451 else {
1452 var sel = li.parentNode.querySelector('[selected]');
1453 if (sel) {
1454 sel.removeAttribute('display');
1455 sel.removeAttribute('selected');
1456 }
1457
1458 li.setAttribute('display', 0);
1459 li.setAttribute('selected', '');
1460
1461 this.closeDropdown(sb);
1462 }
1463
1464 this.saveValues(sb, ul);
1465 },
1466
1467 /** @private */
1468 transformItem: function(sb, li) {
1469 var cbox = E('form', {}, E('input', { type: 'checkbox', tabindex: -1, onclick: 'event.preventDefault()' })),
1470 label = E('label');
1471
1472 while (li.firstChild)
1473 label.appendChild(li.firstChild);
1474
1475 li.appendChild(cbox);
1476 li.appendChild(label);
1477 },
1478
1479 /** @private */
1480 saveValues: function(sb, ul) {
1481 var sel = ul.querySelectorAll('li[selected]'),
1482 div = sb.lastElementChild,
1483 name = this.options.name,
1484 strval = '',
1485 values = [];
1486
1487 while (div.lastElementChild)
1488 div.removeChild(div.lastElementChild);
1489
1490 sel.forEach(function (s) {
1491 if (s.hasAttribute('placeholder'))
1492 return;
1493
1494 var v = {
1495 text: s.innerText,
1496 value: s.hasAttribute('data-value') ? s.getAttribute('data-value') : s.innerText,
1497 element: s
1498 };
1499
1500 div.appendChild(E('input', {
1501 type: 'hidden',
1502 name: name,
1503 value: v.value
1504 }));
1505
1506 values.push(v);
1507
1508 strval += strval.length ? ' ' + v.value : v.value;
1509 });
1510
1511 var detail = {
1512 instance: this,
1513 element: sb
1514 };
1515
1516 if (this.options.multiple)
1517 detail.values = values;
1518 else
1519 detail.value = values.length ? values[0] : null;
1520
1521 sb.value = strval;
1522
1523 sb.dispatchEvent(new CustomEvent('cbi-dropdown-change', {
1524 bubbles: true,
1525 detail: detail
1526 }));
1527 },
1528
1529 /** @private */
1530 setValues: function(sb, values) {
1531 var ul = sb.querySelector('ul');
1532
1533 if (this.options.create) {
1534 for (var value in values) {
1535 this.createItems(sb, value);
1536
1537 if (!this.options.multiple)
1538 break;
1539 }
1540 }
1541
1542 if (this.options.multiple) {
1543 var lis = ul.querySelectorAll('li[data-value]');
1544 for (var i = 0; i < lis.length; i++) {
1545 var value = lis[i].getAttribute('data-value');
1546 if (values === null || !(value in values))
1547 this.toggleItem(sb, lis[i], false);
1548 else
1549 this.toggleItem(sb, lis[i], true);
1550 }
1551 }
1552 else {
1553 var ph = ul.querySelector('li[placeholder]');
1554 if (ph)
1555 this.toggleItem(sb, ph);
1556
1557 var lis = ul.querySelectorAll('li[data-value]');
1558 for (var i = 0; i < lis.length; i++) {
1559 var value = lis[i].getAttribute('data-value');
1560 if (values !== null && (value in values))
1561 this.toggleItem(sb, lis[i]);
1562 }
1563 }
1564 },
1565
1566 /** @private */
1567 setFocus: function(sb, elem, scroll) {
1568 if (sb.hasAttribute('locked-in'))
1569 return;
1570
1571 sb.querySelectorAll('.focus').forEach(function(e) {
1572 e.classList.remove('focus');
1573 });
1574
1575 elem.classList.add('focus');
1576
1577 if (scroll)
1578 elem.parentNode.scrollTop = elem.offsetTop - elem.parentNode.offsetTop;
1579
1580 elem.focus();
1581 },
1582
1583 /** @private */
1584 createChoiceElement: function(sb, value, label) {
1585 var tpl = sb.querySelector(this.options.create_template),
1586 markup = null;
1587
1588 if (tpl)
1589 markup = (tpl.textContent || tpl.innerHTML || tpl.firstChild.data).replace(/^<!--|--!?>$/, '').trim();
1590 else
1591 markup = '<li data-value="{{value}}"><span data-label-placeholder="true" /></li>';
1592
1593 var new_item = E(markup.replace(/{{value}}/g, '%h'.format(value))),
1594 placeholder = new_item.querySelector('[data-label-placeholder]');
1595
1596 if (placeholder) {
1597 var content = E('span', {}, label || this.choices[value] || [ value ]);
1598
1599 while (content.firstChild)
1600 placeholder.parentNode.insertBefore(content.firstChild, placeholder);
1601
1602 placeholder.parentNode.removeChild(placeholder);
1603 }
1604
1605 if (this.options.multiple)
1606 this.transformItem(sb, new_item);
1607
1608 if (!new_item.hasAttribute('unselectable'))
1609 new_item.setAttribute('tabindex', 0);
1610
1611 return new_item;
1612 },
1613
1614 /** @private */
1615 createItems: function(sb, value) {
1616 var sbox = this,
1617 val = (value || '').trim(),
1618 ul = sb.querySelector('ul');
1619
1620 if (!sbox.options.multiple)
1621 val = val.length ? [ val ] : [];
1622 else
1623 val = val.length ? val.split(/\s+/) : [];
1624
1625 val.forEach(function(item) {
1626 var new_item = null;
1627
1628 ul.childNodes.forEach(function(li) {
1629 if (li.getAttribute && li.getAttribute('data-value') === item)
1630 new_item = li;
1631 });
1632
1633 if (!new_item) {
1634 new_item = sbox.createChoiceElement(sb, item);
1635
1636 if (!sbox.options.multiple) {
1637 var old = ul.querySelector('li[created]');
1638 if (old)
1639 ul.removeChild(old);
1640
1641 new_item.setAttribute('created', '');
1642 }
1643
1644 new_item = ul.insertBefore(new_item, ul.lastElementChild);
1645 }
1646
1647 sbox.toggleItem(sb, new_item, true);
1648 sbox.setFocus(sb, new_item, true);
1649 });
1650 },
1651
1652 /**
1653 * Remove all existing choices from the dropdown menu.
1654 *
1655 * This function removes all preexisting dropdown choices from the widget,
1656 * keeping only choices currently being selected unless `reset_values` is
1657 * given, in which case all choices and deselected and removed.
1658 *
1659 * @instance
1660 * @memberof LuCI.ui.Dropdown
1661 * @param {boolean} [reset_value=false]
1662 * If set to `true`, deselect and remove selected choices as well instead
1663 * of keeping them.
1664 */
1665 clearChoices: function(reset_value) {
1666 var ul = this.node.querySelector('ul'),
1667 lis = ul ? ul.querySelectorAll('li[data-value]') : [],
1668 len = lis.length - (this.options.create ? 1 : 0),
1669 val = reset_value ? null : this.getValue();
1670
1671 for (var i = 0; i < len; i++) {
1672 var lival = lis[i].getAttribute('data-value');
1673 if (val == null ||
1674 (!this.options.multiple && val != lival) ||
1675 (this.options.multiple && val.indexOf(lival) == -1))
1676 ul.removeChild(lis[i]);
1677 }
1678
1679 if (reset_value)
1680 this.setValues(this.node, {});
1681 },
1682
1683 /**
1684 * Add new choices to the dropdown menu.
1685 *
1686 * This function adds further choices to an existing dropdown menu,
1687 * ignoring choice values which are already present.
1688 *
1689 * @instance
1690 * @memberof LuCI.ui.Dropdown
1691 * @param {string[]} values
1692 * The choice values to add to the dropdown widget.
1693 *
1694 * @param {Object<string, *>} labels
1695 * The choice label values to use when adding dropdown choices. If no
1696 * label is found for a particular choice value, the value itself is used
1697 * as label text. Choice labels may be any valid value accepted by
1698 * {@link LuCI.dom#content}.
1699 */
1700 addChoices: function(values, labels) {
1701 var sb = this.node,
1702 ul = sb.querySelector('ul'),
1703 lis = ul ? ul.querySelectorAll('li[data-value]') : [];
1704
1705 if (!Array.isArray(values))
1706 values = L.toArray(values);
1707
1708 if (!L.isObject(labels))
1709 labels = {};
1710
1711 for (var i = 0; i < values.length; i++) {
1712 var found = false;
1713
1714 for (var j = 0; j < lis.length; j++) {
1715 if (lis[j].getAttribute('data-value') === values[i]) {
1716 found = true;
1717 break;
1718 }
1719 }
1720
1721 if (found)
1722 continue;
1723
1724 ul.insertBefore(
1725 this.createChoiceElement(sb, values[i], labels[values[i]]),
1726 ul.lastElementChild);
1727 }
1728 },
1729
1730 /**
1731 * Close all open dropdown widgets in the current document.
1732 */
1733 closeAllDropdowns: function() {
1734 document.querySelectorAll('.cbi-dropdown[open]').forEach(function(s) {
1735 s.dispatchEvent(new CustomEvent('cbi-dropdown-close', {}));
1736 });
1737 },
1738
1739 /** @private */
1740 handleClick: function(ev) {
1741 var sb = ev.currentTarget;
1742
1743 if (!sb.hasAttribute('open')) {
1744 if (!matchesElem(ev.target, 'input'))
1745 this.openDropdown(sb);
1746 }
1747 else {
1748 var li = findParent(ev.target, 'li');
1749 if (li && li.parentNode.classList.contains('dropdown'))
1750 this.toggleItem(sb, li);
1751 else if (li && li.parentNode.classList.contains('preview'))
1752 this.closeDropdown(sb);
1753 else if (matchesElem(ev.target, 'span.open, span.more'))
1754 this.closeDropdown(sb);
1755 }
1756
1757 ev.preventDefault();
1758 ev.stopPropagation();
1759 },
1760
1761 /** @private */
1762 handleKeydown: function(ev) {
1763 var sb = ev.currentTarget,
1764 ul = sb.querySelector('ul.dropdown');
1765
1766 if (matchesElem(ev.target, 'input'))
1767 return;
1768
1769 if (!sb.hasAttribute('open')) {
1770 switch (ev.keyCode) {
1771 case 37:
1772 case 38:
1773 case 39:
1774 case 40:
1775 this.openDropdown(sb);
1776 ev.preventDefault();
1777 }
1778 }
1779 else {
1780 var active = findParent(document.activeElement, 'li');
1781
1782 switch (ev.keyCode) {
1783 case 27:
1784 this.closeDropdown(sb);
1785 ev.stopPropagation();
1786 break;
1787
1788 case 13:
1789 if (active) {
1790 if (!active.hasAttribute('selected'))
1791 this.toggleItem(sb, active);
1792 this.closeDropdown(sb);
1793 ev.preventDefault();
1794 }
1795 break;
1796
1797 case 32:
1798 if (active) {
1799 this.toggleItem(sb, active);
1800 ev.preventDefault();
1801 }
1802 break;
1803
1804 case 38:
1805 if (active && active.previousElementSibling) {
1806 this.setFocus(sb, active.previousElementSibling);
1807 ev.preventDefault();
1808 }
1809 else if (document.activeElement === ul) {
1810 this.setFocus(sb, ul.lastElementChild);
1811 ev.preventDefault();
1812 }
1813 break;
1814
1815 case 40:
1816 if (active && active.nextElementSibling) {
1817 var li = active.nextElementSibling;
1818 this.setFocus(sb, li);
1819 if (this.options.create && li == li.parentNode.lastElementChild) {
1820 var input = li.querySelector('input:not([type="hidden"]):not([type="checkbox"]');
1821 if (input) input.focus();
1822 }
1823 ev.preventDefault();
1824 }
1825 else if (document.activeElement === ul) {
1826 this.setFocus(sb, ul.firstElementChild);
1827 ev.preventDefault();
1828 }
1829 break;
1830 }
1831 }
1832 },
1833
1834 /** @private */
1835 handleDropdownClose: function(ev) {
1836 var sb = ev.currentTarget;
1837
1838 this.closeDropdown(sb, true);
1839 },
1840
1841 /** @private */
1842 handleDropdownSelect: function(ev) {
1843 var sb = ev.currentTarget,
1844 li = findParent(ev.target, 'li');
1845
1846 if (!li)
1847 return;
1848
1849 this.toggleItem(sb, li);
1850 this.closeDropdown(sb, true);
1851 },
1852
1853 /** @private */
1854 handleFocus: function(ev) {
1855 var sb = ev.currentTarget;
1856
1857 document.querySelectorAll('.cbi-dropdown[open]').forEach(function(s) {
1858 if (s !== sb || sb.hasAttribute('open'))
1859 s.dispatchEvent(new CustomEvent('cbi-dropdown-close', {}));
1860 });
1861 },
1862
1863 /** @private */
1864 handleCanaryFocus: function(ev) {
1865 this.closeDropdown(ev.currentTarget.parentNode);
1866 },
1867
1868 /** @private */
1869 handleCreateKeydown: function(ev) {
1870 var input = ev.currentTarget,
1871 li = findParent(input, 'li'),
1872 sb = findParent(li, '.cbi-dropdown');
1873
1874 switch (ev.keyCode) {
1875 case 13:
1876 ev.preventDefault();
1877
1878 if (input.classList.contains('cbi-input-invalid'))
1879 return;
1880
1881 this.handleCreateBlur(ev);
1882 this.createItems(sb, input.value);
1883 input.value = '';
1884 break;
1885
1886 case 27:
1887 this.handleCreateBlur(ev);
1888 this.closeDropdown(sb);
1889 ev.stopPropagation();
1890 input.value = '';
1891 break;
1892
1893 case 38:
1894 if (li.previousElementSibling) {
1895 this.handleCreateBlur(ev);
1896 this.setFocus(sb, li.previousElementSibling, true);
1897 }
1898 break;
1899 }
1900 },
1901
1902 /** @private */
1903 handleCreateFocus: function(ev) {
1904 var input = ev.currentTarget,
1905 li = findParent(input, 'li'),
1906 cbox = li.querySelector('input[type="checkbox"]'),
1907 sb = findParent(input, '.cbi-dropdown');
1908
1909 if (cbox)
1910 cbox.checked = true;
1911
1912 sb.setAttribute('locked-in', '');
1913 this.setFocus(sb, li, true);
1914 },
1915
1916 /** @private */
1917 handleCreateBlur: function(ev) {
1918 var input = ev.currentTarget,
1919 cbox = findParent(input, 'li').querySelector('input[type="checkbox"]'),
1920 sb = findParent(input, '.cbi-dropdown');
1921
1922 if (cbox)
1923 cbox.checked = false;
1924
1925 sb.removeAttribute('locked-in');
1926 },
1927
1928 /** @private */
1929 handleCreateClick: function(ev) {
1930 ev.currentTarget.querySelector(this.options.create_query).focus();
1931 },
1932
1933 /** @override */
1934 setValue: function(values) {
1935 if (this.options.multiple) {
1936 if (!Array.isArray(values))
1937 values = (values != null && values != '') ? [ values ] : [];
1938
1939 var v = {};
1940
1941 for (var i = 0; i < values.length; i++)
1942 v[values[i]] = true;
1943
1944 this.setValues(this.node, v);
1945 }
1946 else {
1947 var v = {};
1948
1949 if (values != null) {
1950 if (Array.isArray(values))
1951 v[values[0]] = true;
1952 else
1953 v[values] = true;
1954 }
1955
1956 this.setValues(this.node, v);
1957 }
1958 },
1959
1960 /** @override */
1961 getValue: function() {
1962 var div = this.node.lastElementChild,
1963 h = div.querySelectorAll('input[type="hidden"]'),
1964 v = [];
1965
1966 for (var i = 0; i < h.length; i++)
1967 v.push(h[i].value);
1968
1969 return this.options.multiple ? v : v[0];
1970 }
1971 });
1972
1973 /**
1974 * Instantiate a rich dropdown choice widget allowing custom values.
1975 *
1976 * @constructor Combobox
1977 * @memberof LuCI.ui
1978 * @augments LuCI.ui.Dropdown
1979 *
1980 * @classdesc
1981 *
1982 * The `Combobox` class implements a rich, stylable dropdown menu which allows
1983 * to enter custom values. Historically, comboboxes used to be a dedicated
1984 * widget type in LuCI but nowadays they are direct aliases of dropdown widgets
1985 * with a set of enforced default properties for easier instantiation.
1986 *
1987 * UI widget instances are usually not supposed to be created by view code
1988 * directly, instead they're implicitly created by `LuCI.form` when
1989 * instantiating CBI forms.
1990 *
1991 * This class is automatically instantiated as part of `LuCI.ui`. To use it
1992 * in views, use `'require ui'` and refer to `ui.Combobox`. To import it in
1993 * external JavaScript, use `L.require("ui").then(...)` and access the
1994 * `Combobox` property of the class instance value.
1995 *
1996 * @param {string|string[]} [value=null]
1997 * The initial input value(s).
1998 *
1999 * @param {Object<string, *>} choices
2000 * Object containing the selectable choices of the widget. The object keys
2001 * serve as values for the different choices while the values are used as
2002 * choice labels.
2003 *
2004 * @param {LuCI.ui.Combobox.InitOptions} [options]
2005 * Object describing the widget specific options to initialize the dropdown.
2006 */
2007 var UICombobox = UIDropdown.extend(/** @lends LuCI.ui.Combobox.prototype */ {
2008 /**
2009 * Comboboxes support the same properties as
2010 * [Dropdown.InitOptions]{@link LuCI.ui.Dropdown.InitOptions} but enforce
2011 * specific values for the following properties:
2012 *
2013 * @typedef {LuCI.ui.Dropdown.InitOptions} InitOptions
2014 * @memberof LuCI.ui.Combobox
2015 *
2016 * @property {boolean} multiple=false
2017 * Since Comboboxes never allow selecting multiple values, this property
2018 * is forcibly set to `false`.
2019 *
2020 * @property {boolean} create=true
2021 * Since Comboboxes always allow custom choice values, this property is
2022 * forcibly set to `true`.
2023 *
2024 * @property {boolean} optional=true
2025 * Since Comboboxes are always optional, this property is forcibly set to
2026 * `true`.
2027 */
2028 __init__: function(value, choices, options) {
2029 this.super('__init__', [ value, choices, Object.assign({
2030 select_placeholder: _('-- Please choose --'),
2031 custom_placeholder: _('-- custom --'),
2032 dropdown_items: -1,
2033 sort: true
2034 }, options, {
2035 multiple: false,
2036 create: true,
2037 optional: true
2038 }) ]);
2039 }
2040 });
2041
2042 /**
2043 * Instantiate a combo button widget offering multiple action choices.
2044 *
2045 * @constructor ComboButton
2046 * @memberof LuCI.ui
2047 * @augments LuCI.ui.Dropdown
2048 *
2049 * @classdesc
2050 *
2051 * The `ComboButton` class implements a button element which can be expanded
2052 * into a dropdown to chose from a set of different action choices.
2053 *
2054 * UI widget instances are usually not supposed to be created by view code
2055 * directly, instead they're implicitly created by `LuCI.form` when
2056 * instantiating CBI forms.
2057 *
2058 * This class is automatically instantiated as part of `LuCI.ui`. To use it
2059 * in views, use `'require ui'` and refer to `ui.ComboButton`. To import it in
2060 * external JavaScript, use `L.require("ui").then(...)` and access the
2061 * `ComboButton` property of the class instance value.
2062 *
2063 * @param {string|string[]} [value=null]
2064 * The initial input value(s).
2065 *
2066 * @param {Object<string, *>} choices
2067 * Object containing the selectable choices of the widget. The object keys
2068 * serve as values for the different choices while the values are used as
2069 * choice labels.
2070 *
2071 * @param {LuCI.ui.ComboButton.InitOptions} [options]
2072 * Object describing the widget specific options to initialize the button.
2073 */
2074 var UIComboButton = UIDropdown.extend(/** @lends LuCI.ui.ComboButton.prototype */ {
2075 /**
2076 * ComboButtons support the same properties as
2077 * [Dropdown.InitOptions]{@link LuCI.ui.Dropdown.InitOptions} but enforce
2078 * specific values for some properties and add additional button specific
2079 * properties.
2080 *
2081 * @typedef {LuCI.ui.Dropdown.InitOptions} InitOptions
2082 * @memberof LuCI.ui.ComboButton
2083 *
2084 * @property {boolean} multiple=false
2085 * Since ComboButtons never allow selecting multiple actions, this property
2086 * is forcibly set to `false`.
2087 *
2088 * @property {boolean} create=false
2089 * Since ComboButtons never allow creating custom choices, this property
2090 * is forcibly set to `false`.
2091 *
2092 * @property {boolean} optional=false
2093 * Since ComboButtons must always select one action, this property is
2094 * forcibly set to `false`.
2095 *
2096 * @property {Object<string, string>} [classes]
2097 * Specifies a mapping of choice values to CSS class names. If an action
2098 * choice is selected by the user and if a corresponding entry exists in
2099 * the `classes` object, the class names corresponding to the selected
2100 * value are set on the button element.
2101 *
2102 * This is useful to apply different button styles, such as colors, to the
2103 * combined button depending on the selected action.
2104 *
2105 * @property {function} [click]
2106 * Specifies a handler function to invoke when the user clicks the button.
2107 * This function will be called with the button DOM node as `this` context
2108 * and receive the DOM click event as first as well as the selected action
2109 * choice value as second argument.
2110 */
2111 __init__: function(value, choices, options) {
2112 this.super('__init__', [ value, choices, Object.assign({
2113 sort: true
2114 }, options, {
2115 multiple: false,
2116 create: false,
2117 optional: false
2118 }) ]);
2119 },
2120
2121 /** @override */
2122 render: function(/* ... */) {
2123 var node = UIDropdown.prototype.render.apply(this, arguments),
2124 val = this.getValue();
2125
2126 if (L.isObject(this.options.classes) && this.options.classes.hasOwnProperty(val))
2127 node.setAttribute('class', 'cbi-dropdown ' + this.options.classes[val]);
2128
2129 return node;
2130 },
2131
2132 /** @private */
2133 handleClick: function(ev) {
2134 var sb = ev.currentTarget,
2135 t = ev.target;
2136
2137 if (sb.hasAttribute('open') || dom.matches(t, '.cbi-dropdown > span.open'))
2138 return UIDropdown.prototype.handleClick.apply(this, arguments);
2139
2140 if (this.options.click)
2141 return this.options.click.call(sb, ev, this.getValue());
2142 },
2143
2144 /** @private */
2145 toggleItem: function(sb /*, ... */) {
2146 var rv = UIDropdown.prototype.toggleItem.apply(this, arguments),
2147 val = this.getValue();
2148
2149 if (L.isObject(this.options.classes) && this.options.classes.hasOwnProperty(val))
2150 sb.setAttribute('class', 'cbi-dropdown ' + this.options.classes[val]);
2151 else
2152 sb.setAttribute('class', 'cbi-dropdown');
2153
2154 return rv;
2155 }
2156 });
2157
2158 /**
2159 * Instantiate a dynamic list widget.
2160 *
2161 * @constructor DynamicList
2162 * @memberof LuCI.ui
2163 * @augments LuCI.ui.AbstractElement
2164 *
2165 * @classdesc
2166 *
2167 * The `DynamicList` class implements a widget which allows the user to specify
2168 * an arbitrary amount of input values, either from free formed text input or
2169 * from a set of predefined choices.
2170 *
2171 * UI widget instances are usually not supposed to be created by view code
2172 * directly, instead they're implicitly created by `LuCI.form` when
2173 * instantiating CBI forms.
2174 *
2175 * This class is automatically instantiated as part of `LuCI.ui`. To use it
2176 * in views, use `'require ui'` and refer to `ui.DynamicList`. To import it in
2177 * external JavaScript, use `L.require("ui").then(...)` and access the
2178 * `DynamicList` property of the class instance value.
2179 *
2180 * @param {string|string[]} [value=null]
2181 * The initial input value(s).
2182 *
2183 * @param {Object<string, *>} [choices]
2184 * Object containing the selectable choices of the widget. The object keys
2185 * serve as values for the different choices while the values are used as
2186 * choice labels. If omitted, no default choices are presented to the user,
2187 * instead a plain text input field is rendered allowing the user to add
2188 * arbitrary values to the dynamic list.
2189 *
2190 * @param {LuCI.ui.DynamicList.InitOptions} [options]
2191 * Object describing the widget specific options to initialize the dynamic list.
2192 */
2193 var UIDynamicList = UIElement.extend(/** @lends LuCI.ui.DynamicList.prototype */ {
2194 /**
2195 * In case choices are passed to the dynamic list constructor, the widget
2196 * supports the same properties as [Dropdown.InitOptions]{@link LuCI.ui.Dropdown.InitOptions}
2197 * but enforces specific values for some dropdown properties.
2198 *
2199 * @typedef {LuCI.ui.Dropdown.InitOptions} InitOptions
2200 * @memberof LuCI.ui.DynamicList
2201 *
2202 * @property {boolean} multiple=false
2203 * Since dynamic lists never allow selecting multiple choices when adding
2204 * another list item, this property is forcibly set to `false`.
2205 *
2206 * @property {boolean} optional=true
2207 * Since dynamic lists use an embedded dropdown to present a list of
2208 * predefined choice values, the dropdown must be made optional to allow
2209 * it to remain unselected.
2210 */
2211 __init__: function(values, choices, options) {
2212 if (!Array.isArray(values))
2213 values = (values != null && values != '') ? [ values ] : [];
2214
2215 if (typeof(choices) != 'object')
2216 choices = null;
2217
2218 this.values = values;
2219 this.choices = choices;
2220 this.options = Object.assign({}, options, {
2221 multiple: false,
2222 optional: true
2223 });
2224 },
2225
2226 /** @override */
2227 render: function() {
2228 var dl = E('div', {
2229 'id': this.options.id,
2230 'class': 'cbi-dynlist',
2231 'disabled': this.options.disabled ? '' : null
2232 }, E('div', { 'class': 'add-item control-group' }));
2233
2234 if (this.choices) {
2235 if (this.options.placeholder != null)
2236 this.options.select_placeholder = this.options.placeholder;
2237
2238 var cbox = new UICombobox(null, this.choices, this.options);
2239
2240 dl.lastElementChild.appendChild(cbox.render());
2241 }
2242 else {
2243 var inputEl = E('input', {
2244 'id': this.options.id ? 'widget.' + this.options.id : null,
2245 'type': 'text',
2246 'class': 'cbi-input-text',
2247 'placeholder': this.options.placeholder,
2248 'disabled': this.options.disabled ? '' : null
2249 });
2250
2251 dl.lastElementChild.appendChild(inputEl);
2252 dl.lastElementChild.appendChild(E('div', { 'class': 'btn cbi-button cbi-button-add' }, '+'));
2253
2254 if (this.options.datatype || this.options.validate)
2255 UI.prototype.addValidator(inputEl, this.options.datatype || 'string',
2256 true, this.options.validate, 'blur', 'keyup');
2257 }
2258
2259 for (var i = 0; i < this.values.length; i++) {
2260 var label = this.choices ? this.choices[this.values[i]] : null;
2261
2262 if (dom.elem(label))
2263 label = label.cloneNode(true);
2264
2265 this.addItem(dl, this.values[i], label);
2266 }
2267
2268 return this.bind(dl);
2269 },
2270
2271 /** @private */
2272 bind: function(dl) {
2273 dl.addEventListener('click', L.bind(this.handleClick, this));
2274 dl.addEventListener('keydown', L.bind(this.handleKeydown, this));
2275 dl.addEventListener('cbi-dropdown-change', L.bind(this.handleDropdownChange, this));
2276
2277 this.node = dl;
2278
2279 this.setUpdateEvents(dl, 'cbi-dynlist-change');
2280 this.setChangeEvents(dl, 'cbi-dynlist-change');
2281
2282 dom.bindClassInstance(dl, this);
2283
2284 return dl;
2285 },
2286
2287 /** @private */
2288 addItem: function(dl, value, text, flash) {
2289 var exists = false,
2290 new_item = E('div', { 'class': flash ? 'item flash' : 'item', 'tabindex': 0 }, [
2291 E('span', {}, [ text || value ]),
2292 E('input', {
2293 'type': 'hidden',
2294 'name': this.options.name,
2295 'value': value })]);
2296
2297 dl.querySelectorAll('.item').forEach(function(item) {
2298 if (exists)
2299 return;
2300
2301 var hidden = item.querySelector('input[type="hidden"]');
2302
2303 if (hidden && hidden.parentNode !== item)
2304 hidden = null;
2305
2306 if (hidden && hidden.value === value)
2307 exists = true;
2308 });
2309
2310 if (!exists) {
2311 var ai = dl.querySelector('.add-item');
2312 ai.parentNode.insertBefore(new_item, ai);
2313 }
2314
2315 dl.dispatchEvent(new CustomEvent('cbi-dynlist-change', {
2316 bubbles: true,
2317 detail: {
2318 instance: this,
2319 element: dl,
2320 value: value,
2321 add: true
2322 }
2323 }));
2324 },
2325
2326 /** @private */
2327 removeItem: function(dl, item) {
2328 var value = item.querySelector('input[type="hidden"]').value;
2329 var sb = dl.querySelector('.cbi-dropdown');
2330 if (sb)
2331 sb.querySelectorAll('ul > li').forEach(function(li) {
2332 if (li.getAttribute('data-value') === value) {
2333 if (li.hasAttribute('dynlistcustom'))
2334 li.parentNode.removeChild(li);
2335 else
2336 li.removeAttribute('unselectable');
2337 }
2338 });
2339
2340 item.parentNode.removeChild(item);
2341
2342 dl.dispatchEvent(new CustomEvent('cbi-dynlist-change', {
2343 bubbles: true,
2344 detail: {
2345 instance: this,
2346 element: dl,
2347 value: value,
2348 remove: true
2349 }
2350 }));
2351 },
2352
2353 /** @private */
2354 handleClick: function(ev) {
2355 var dl = ev.currentTarget,
2356 item = findParent(ev.target, '.item');
2357
2358 if (this.options.disabled)
2359 return;
2360
2361 if (item) {
2362 this.removeItem(dl, item);
2363 }
2364 else if (matchesElem(ev.target, '.cbi-button-add')) {
2365 var input = ev.target.previousElementSibling;
2366 if (input.value.length && !input.classList.contains('cbi-input-invalid')) {
2367 this.addItem(dl, input.value, null, true);
2368 input.value = '';
2369 }
2370 }
2371 },
2372
2373 /** @private */
2374 handleDropdownChange: function(ev) {
2375 var dl = ev.currentTarget,
2376 sbIn = ev.detail.instance,
2377 sbEl = ev.detail.element,
2378 sbVal = ev.detail.value;
2379
2380 if (sbVal === null)
2381 return;
2382
2383 sbIn.setValues(sbEl, null);
2384 sbVal.element.setAttribute('unselectable', '');
2385
2386 if (sbVal.element.hasAttribute('created')) {
2387 sbVal.element.removeAttribute('created');
2388 sbVal.element.setAttribute('dynlistcustom', '');
2389 }
2390
2391 var label = sbVal.text;
2392
2393 if (sbVal.element) {
2394 label = E([]);
2395
2396 for (var i = 0; i < sbVal.element.childNodes.length; i++)
2397 label.appendChild(sbVal.element.childNodes[i].cloneNode(true));
2398 }
2399
2400 this.addItem(dl, sbVal.value, label, true);
2401 },
2402
2403 /** @private */
2404 handleKeydown: function(ev) {
2405 var dl = ev.currentTarget,
2406 item = findParent(ev.target, '.item');
2407
2408 if (item) {
2409 switch (ev.keyCode) {
2410 case 8: /* backspace */
2411 if (item.previousElementSibling)
2412 item.previousElementSibling.focus();
2413
2414 this.removeItem(dl, item);
2415 break;
2416
2417 case 46: /* delete */
2418 if (item.nextElementSibling) {
2419 if (item.nextElementSibling.classList.contains('item'))
2420 item.nextElementSibling.focus();
2421 else
2422 item.nextElementSibling.firstElementChild.focus();
2423 }
2424
2425 this.removeItem(dl, item);
2426 break;
2427 }
2428 }
2429 else if (matchesElem(ev.target, '.cbi-input-text')) {
2430 switch (ev.keyCode) {
2431 case 13: /* enter */
2432 if (ev.target.value.length && !ev.target.classList.contains('cbi-input-invalid')) {
2433 this.addItem(dl, ev.target.value, null, true);
2434 ev.target.value = '';
2435 ev.target.blur();
2436 ev.target.focus();
2437 }
2438
2439 ev.preventDefault();
2440 break;
2441 }
2442 }
2443 },
2444
2445 /** @override */
2446 getValue: function() {
2447 var items = this.node.querySelectorAll('.item > input[type="hidden"]'),
2448 input = this.node.querySelector('.add-item > input[type="text"]'),
2449 v = [];
2450
2451 for (var i = 0; i < items.length; i++)
2452 v.push(items[i].value);
2453
2454 if (input && input.value != null && input.value.match(/\S/) &&
2455 input.classList.contains('cbi-input-invalid') == false &&
2456 v.filter(function(s) { return s == input.value }).length == 0)
2457 v.push(input.value);
2458
2459 return v;
2460 },
2461
2462 /** @override */
2463 setValue: function(values) {
2464 if (!Array.isArray(values))
2465 values = (values != null && values != '') ? [ values ] : [];
2466
2467 var items = this.node.querySelectorAll('.item');
2468
2469 for (var i = 0; i < items.length; i++)
2470 if (items[i].parentNode === this.node)
2471 this.removeItem(this.node, items[i]);
2472
2473 for (var i = 0; i < values.length; i++)
2474 this.addItem(this.node, values[i],
2475 this.choices ? this.choices[values[i]] : null);
2476 },
2477
2478 /**
2479 * Add new suggested choices to the dynamic list.
2480 *
2481 * This function adds further choices to an existing dynamic list,
2482 * ignoring choice values which are already present.
2483 *
2484 * @instance
2485 * @memberof LuCI.ui.DynamicList
2486 * @param {string[]} values
2487 * The choice values to add to the dynamic lists suggestion dropdown.
2488 *
2489 * @param {Object<string, *>} labels
2490 * The choice label values to use when adding suggested choices. If no
2491 * label is found for a particular choice value, the value itself is used
2492 * as label text. Choice labels may be any valid value accepted by
2493 * {@link LuCI.dom#content}.
2494 */
2495 addChoices: function(values, labels) {
2496 var dl = this.node.lastElementChild.firstElementChild;
2497 dom.callClassMethod(dl, 'addChoices', values, labels);
2498 },
2499
2500 /**
2501 * Remove all existing choices from the dynamic list.
2502 *
2503 * This function removes all preexisting suggested choices from the widget.
2504 *
2505 * @instance
2506 * @memberof LuCI.ui.DynamicList
2507 */
2508 clearChoices: function() {
2509 var dl = this.node.lastElementChild.firstElementChild;
2510 dom.callClassMethod(dl, 'clearChoices');
2511 }
2512 });
2513
2514 /**
2515 * Instantiate a hidden input field widget.
2516 *
2517 * @constructor Hiddenfield
2518 * @memberof LuCI.ui
2519 * @augments LuCI.ui.AbstractElement
2520 *
2521 * @classdesc
2522 *
2523 * The `Hiddenfield` class implements an HTML `<input type="hidden">` field
2524 * which allows to store form data without exposing it to the user.
2525 *
2526 * UI widget instances are usually not supposed to be created by view code
2527 * directly, instead they're implicitly created by `LuCI.form` when
2528 * instantiating CBI forms.
2529 *
2530 * This class is automatically instantiated as part of `LuCI.ui`. To use it
2531 * in views, use `'require ui'` and refer to `ui.Hiddenfield`. To import it in
2532 * external JavaScript, use `L.require("ui").then(...)` and access the
2533 * `Hiddenfield` property of the class instance value.
2534 *
2535 * @param {string|string[]} [value=null]
2536 * The initial input value.
2537 *
2538 * @param {LuCI.ui.AbstractElement.InitOptions} [options]
2539 * Object describing the widget specific options to initialize the hidden input.
2540 */
2541 var UIHiddenfield = UIElement.extend(/** @lends LuCI.ui.Hiddenfield.prototype */ {
2542 __init__: function(value, options) {
2543 this.value = value;
2544 this.options = Object.assign({
2545
2546 }, options);
2547 },
2548
2549 /** @override */
2550 render: function() {
2551 var hiddenEl = E('input', {
2552 'id': this.options.id,
2553 'type': 'hidden',
2554 'value': this.value
2555 });
2556
2557 return this.bind(hiddenEl);
2558 },
2559
2560 /** @private */
2561 bind: function(hiddenEl) {
2562 this.node = hiddenEl;
2563
2564 dom.bindClassInstance(hiddenEl, this);
2565
2566 return hiddenEl;
2567 },
2568
2569 /** @override */
2570 getValue: function() {
2571 return this.node.value;
2572 },
2573
2574 /** @override */
2575 setValue: function(value) {
2576 this.node.value = value;
2577 }
2578 });
2579
2580 /**
2581 * Instantiate a file upload widget.
2582 *
2583 * @constructor FileUpload
2584 * @memberof LuCI.ui
2585 * @augments LuCI.ui.AbstractElement
2586 *
2587 * @classdesc
2588 *
2589 * The `FileUpload` class implements a widget which allows the user to upload,
2590 * browse, select and delete files beneath a predefined remote directory.
2591 *
2592 * UI widget instances are usually not supposed to be created by view code
2593 * directly, instead they're implicitly created by `LuCI.form` when
2594 * instantiating CBI forms.
2595 *
2596 * This class is automatically instantiated as part of `LuCI.ui`. To use it
2597 * in views, use `'require ui'` and refer to `ui.FileUpload`. To import it in
2598 * external JavaScript, use `L.require("ui").then(...)` and access the
2599 * `FileUpload` property of the class instance value.
2600 *
2601 * @param {string|string[]} [value=null]
2602 * The initial input value.
2603 *
2604 * @param {LuCI.ui.DynamicList.InitOptions} [options]
2605 * Object describing the widget specific options to initialize the file
2606 * upload control.
2607 */
2608 var UIFileUpload = UIElement.extend(/** @lends LuCI.ui.FileUpload.prototype */ {
2609 /**
2610 * In addition to the [AbstractElement.InitOptions]{@link LuCI.ui.AbstractElement.InitOptions}
2611 * the following properties are recognized:
2612 *
2613 * @typedef {LuCI.ui.AbstractElement.InitOptions} InitOptions
2614 * @memberof LuCI.ui.FileUpload
2615 *
2616 * @property {boolean} [browser=false]
2617 * Use a file browser mode.
2618 *
2619 * @property {boolean} [show_hidden=false]
2620 * Specifies whether hidden files should be displayed when browsing remote
2621 * files. Note that this is not a security feature, hidden files are always
2622 * present in the remote file listings received, this option merely controls
2623 * whether they're displayed or not.
2624 *
2625 * @property {boolean} [enable_upload=true]
2626 * Specifies whether the widget allows the user to upload files. If set to
2627 * `false`, only existing files may be selected. Note that this is not a
2628 * security feature. Whether file upload requests are accepted remotely
2629 * depends on the ACL setup for the current session. This option merely
2630 * controls whether the upload controls are rendered or not.
2631 *
2632 * @property {boolean} [enable_remove=true]
2633 * Specifies whether the widget allows the user to delete remove files.
2634 * If set to `false`, existing files may not be removed. Note that this is
2635 * not a security feature. Whether file delete requests are accepted
2636 * remotely depends on the ACL setup for the current session. This option
2637 * merely controls whether the file remove controls are rendered or not.
2638 *
2639 * @property {string} [root_directory=/etc/luci-uploads]
2640 * Specifies the remote directory the upload and file browsing actions take
2641 * place in. Browsing to directories outside the root directory is
2642 * prevented by the widget. Note that this is not a security feature.
2643 * Whether remote directories are browsable or not solely depends on the
2644 * ACL setup for the current session.
2645 */
2646 __init__: function(value, options) {
2647 this.value = value;
2648 this.options = Object.assign({
2649 browser: false,
2650 show_hidden: false,
2651 enable_upload: true,
2652 enable_remove: true,
2653 root_directory: '/etc/luci-uploads'
2654 }, options);
2655 },
2656
2657 /** @private */
2658 bind: function(browserEl) {
2659 this.node = browserEl;
2660
2661 this.setUpdateEvents(browserEl, 'cbi-fileupload-select', 'cbi-fileupload-cancel');
2662 this.setChangeEvents(browserEl, 'cbi-fileupload-select', 'cbi-fileupload-cancel');
2663
2664 dom.bindClassInstance(browserEl, this);
2665
2666 return browserEl;
2667 },
2668
2669 /** @override */
2670 render: function() {
2671 var renderFileBrowser = L.resolveDefault(this.value != null ? fs.stat(this.value) : null).then(L.bind(function(stat) {
2672 var label;
2673
2674 if (L.isObject(stat) && stat.type != 'directory')
2675 this.stat = stat;
2676
2677 if (this.stat != null)
2678 label = [ this.iconForType(this.stat.type), ' %s (%1000mB)'.format(this.truncatePath(this.stat.path), this.stat.size) ];
2679 else if (this.value != null)
2680 label = [ this.iconForType('file'), ' %s (%s)'.format(this.truncatePath(this.value), _('File not accessible')) ];
2681 else
2682 label = [ _('Select file…') ];
2683 let btnOpenFileBrowser = E('button', {
2684 'class': 'btn open-file-browser',
2685 'click': UI.prototype.createHandlerFn(this, 'handleFileBrowser'),
2686 'disabled': this.options.disabled ? '' : null
2687 }, label);
2688 var fileBrowserEl = E('div', { 'id': this.options.id }, [
2689 btnOpenFileBrowser,
2690 E('div', {
2691 'class': 'cbi-filebrowser'
2692 }),
2693 E('input', {
2694 'type': 'hidden',
2695 'name': this.options.name,
2696 'value': this.value
2697 })
2698 ]);
2699 return this.bind(fileBrowserEl);
2700 }, this));
2701 // in a browser mode open dir listing after render by clicking on a Select button
2702 if (this.options.browser) {
2703 return renderFileBrowser.then(function (fileBrowserEl) {
2704 var btnOpenFileBrowser = fileBrowserEl.getElementsByClassName('open-file-browser').item(0);
2705 btnOpenFileBrowser.click();
2706 return fileBrowserEl;
2707 });
2708 }
2709 return renderFileBrowser
2710 },
2711
2712 /** @private */
2713 truncatePath: function(path) {
2714 if (path.length > 50)
2715 path = path.substring(0, 25) + '…' + path.substring(path.length - 25);
2716
2717 return path;
2718 },
2719
2720 /** @private */
2721 iconForType: function(type) {
2722 switch (type) {
2723 case 'symlink':
2724 return E('img', {
2725 'src': L.resource('cbi/link.svg'),
2726 'width': 16,
2727 'title': _('Symbolic link'),
2728 'class': 'middle'
2729 });
2730
2731 case 'directory':
2732 return E('img', {
2733 'src': L.resource('cbi/folder.svg'),
2734 'width': 16,
2735 'title': _('Directory'),
2736 'class': 'middle'
2737 });
2738
2739 default:
2740 return E('img', {
2741 'src': L.resource('cbi/file.svg'),
2742 'width': 16,
2743 'title': _('File'),
2744 'class': 'middle'
2745 });
2746 }
2747 },
2748
2749 /** @private */
2750 canonicalizePath: function(path) {
2751 return path.replace(/\/{2,}/, '/')
2752 .replace(/\/\.(\/|$)/g, '/')
2753 .replace(/[^\/]+\/\.\.(\/|$)/g, '/')
2754 .replace(/\/$/, '');
2755 },
2756
2757 /** @private */
2758 splitPath: function(path) {
2759 var croot = this.canonicalizePath(this.options.root_directory || '/'),
2760 cpath = this.canonicalizePath(path || '/');
2761
2762 if (cpath.length <= croot.length)
2763 return [ croot ];
2764
2765 if (cpath.charAt(croot.length) != '/')
2766 return [ croot ];
2767
2768 var parts = cpath.substring(croot.length + 1).split(/\//);
2769
2770 parts.unshift(croot);
2771
2772 return parts;
2773 },
2774
2775 /** @private */
2776 handleUpload: function(path, list, ev) {
2777 var form = ev.target.parentNode,
2778 fileinput = form.querySelector('input[type="file"]'),
2779 nameinput = form.querySelector('input[type="text"]'),
2780 filename = (nameinput.value != null ? nameinput.value : '').trim();
2781
2782 ev.preventDefault();
2783
2784 if (filename == '' || filename.match(/\//) || fileinput.files[0] == null)
2785 return;
2786
2787 var existing = list.filter(function(e) { return e.name == filename })[0];
2788
2789 if (existing != null && existing.type == 'directory')
2790 return alert(_('A directory with the same name already exists.'));
2791 else if (existing != null && !confirm(_('Overwrite existing file "%s" ?').format(filename)))
2792 return;
2793
2794 var data = new FormData();
2795
2796 data.append('sessionid', L.env.sessionid);
2797 data.append('filename', path + '/' + filename);
2798 data.append('filedata', fileinput.files[0]);
2799
2800 return request.post(L.env.cgi_base + '/cgi-upload', data, {
2801 progress: L.bind(function(btn, ev) {
2802 btn.firstChild.data = '%.2f%%'.format((ev.loaded / ev.total) * 100);
2803 }, this, ev.target)
2804 }).then(L.bind(function(path, ev, res) {
2805 var reply = res.json();
2806
2807 if (L.isObject(reply) && reply.failure)
2808 alert(_('Upload request failed: %s').format(reply.message));
2809
2810 return this.handleSelect(path, null, ev);
2811 }, this, path, ev));
2812 },
2813
2814 /** @private */
2815 handleDelete: function(path, fileStat, ev) {
2816 var parent = path.replace(/\/[^\/]+$/, '') || '/',
2817 name = path.replace(/^.+\//, ''),
2818 msg;
2819
2820 ev.preventDefault();
2821
2822 if (fileStat.type == 'directory')
2823 msg = _('Do you really want to recursively delete the directory "%s" ?').format(name);
2824 else
2825 msg = _('Do you really want to delete "%s" ?').format(name);
2826
2827 if (confirm(msg)) {
2828 var button = this.node.firstElementChild,
2829 hidden = this.node.lastElementChild;
2830
2831 if (path == hidden.value) {
2832 dom.content(button, _('Select file…'));
2833 hidden.value = '';
2834 }
2835
2836 return fs.remove(path).then(L.bind(function(parent, ev) {
2837 return this.handleSelect(parent, null, ev);
2838 }, this, parent, ev)).catch(function(err) {
2839 alert(_('Delete request failed: %s').format(err.message));
2840 });
2841 }
2842 },
2843
2844 /** @private */
2845 renderUpload: function(path, list) {
2846 if (!this.options.enable_upload)
2847 return E([]);
2848
2849 return E([
2850 E('a', {
2851 'href': '#',
2852 'class': 'btn cbi-button-positive',
2853 'click': function(ev) {
2854 var uploadForm = ev.target.nextElementSibling,
2855 fileInput = uploadForm.querySelector('input[type="file"]');
2856
2857 ev.target.style.display = 'none';
2858 uploadForm.style.display = '';
2859 fileInput.click();
2860 }
2861 }, _('Upload file…')),
2862 E('div', { 'class': 'upload', 'style': 'display:none' }, [
2863 E('input', {
2864 'type': 'file',
2865 'style': 'display:none',
2866 'change': function(ev) {
2867 var nameinput = ev.target.parentNode.querySelector('input[type="text"]'),
2868 uploadbtn = ev.target.parentNode.querySelector('button.cbi-button-save');
2869
2870 nameinput.value = ev.target.value.replace(/^.+[\/\\]/, '');
2871 uploadbtn.disabled = false;
2872 }
2873 }),
2874 E('button', {
2875 'class': 'btn',
2876 'click': function(ev) {
2877 ev.preventDefault();
2878 ev.target.previousElementSibling.click();
2879 }
2880 }, [ _('Browse…') ]),
2881 E('div', {}, E('input', { 'type': 'text', 'placeholder': _('Filename') })),
2882 E('button', {
2883 'class': 'btn cbi-button-save',
2884 'click': UI.prototype.createHandlerFn(this, 'handleUpload', path, list),
2885 'disabled': true
2886 }, [ _('Upload file') ])
2887 ])
2888 ]);
2889 },
2890
2891 /** @private */
2892 renderListing: function(container, path, list) {
2893 var breadcrumb = E('p'),
2894 rows = E('ul');
2895
2896 list.sort(function(a, b) {
2897 return L.naturalCompare(a.type == 'directory', b.type == 'directory') ||
2898 L.naturalCompare(a.name, b.name);
2899 });
2900
2901 for (var i = 0; i < list.length; i++) {
2902 if (!this.options.show_hidden && list[i].name.charAt(0) == '.')
2903 continue;
2904
2905 var entrypath = this.canonicalizePath(path + '/' + list[i].name),
2906 selected = (entrypath == this.node.lastElementChild.value),
2907 mtime = new Date(list[i].mtime * 1000);
2908
2909 rows.appendChild(E('li', [
2910 E('div', { 'class': 'name' }, [
2911 this.iconForType(list[i].type),
2912 ' ',
2913 E('a', {
2914 'href': '#',
2915 'style': selected ? 'font-weight:bold' : null,
2916 'click': UI.prototype.createHandlerFn(this, 'handleSelect',
2917 entrypath, list[i].type != 'directory' ? list[i] : null)
2918 }, '%h'.format(list[i].name))
2919 ]),
2920 E('div', { 'class': 'mtime hide-xs' }, [
2921 ' %04d-%02d-%02d %02d:%02d:%02d '.format(
2922 mtime.getFullYear(),
2923 mtime.getMonth() + 1,
2924 mtime.getDate(),
2925 mtime.getHours(),
2926 mtime.getMinutes(),
2927 mtime.getSeconds())
2928 ]),
2929 E('div', [
2930 selected ? E('button', {
2931 'class': 'btn',
2932 'click': UI.prototype.createHandlerFn(this, 'handleReset')
2933 }, [ _('Deselect') ]) : '',
2934 this.options.enable_remove ? E('button', {
2935 'class': 'btn cbi-button-negative',
2936 'click': UI.prototype.createHandlerFn(this, 'handleDelete', entrypath, list[i])
2937 }, [ _('Delete') ]) : ''
2938 ])
2939 ]));
2940 }
2941
2942 if (!rows.firstElementChild)
2943 rows.appendChild(E('em', _('No entries in this directory')));
2944
2945 var dirs = this.splitPath(path),
2946 cur = '';
2947
2948 for (var i = 0; i < dirs.length; i++) {
2949 cur = cur ? cur + '/' + dirs[i] : dirs[i];
2950 dom.append(breadcrumb, [
2951 i ? ' » ' : '',
2952 E('a', {
2953 'href': '#',
2954 'click': UI.prototype.createHandlerFn(this, 'handleSelect', cur || '/', null)
2955 }, dirs[i] != '' ? '%h'.format(dirs[i]) : E('em', '(root)')),
2956 ]);
2957 }
2958
2959 dom.content(container, [
2960 breadcrumb,
2961 rows,
2962 E('div', { 'class': 'right' }, [
2963 this.renderUpload(path, list),
2964 !this.options.browser ? E('a', {
2965 'href': '#',
2966 'class': 'btn',
2967 'click': UI.prototype.createHandlerFn(this, 'handleCancel')
2968 }, _('Cancel')) : ''
2969 ]),
2970 ]);
2971 },
2972
2973 /** @private */
2974 handleCancel: function(ev) {
2975 var button = this.node.firstElementChild,
2976 browser = button.nextElementSibling;
2977
2978 browser.classList.remove('open');
2979 button.style.display = '';
2980
2981 this.node.dispatchEvent(new CustomEvent('cbi-fileupload-cancel', {}));
2982
2983 ev.preventDefault();
2984 },
2985
2986 /** @private */
2987 handleReset: function(ev) {
2988 var button = this.node.firstElementChild,
2989 hidden = this.node.lastElementChild;
2990
2991 hidden.value = '';
2992 dom.content(button, _('Select file…'));
2993
2994 this.handleCancel(ev);
2995 },
2996
2997 /** @private */
2998 handleSelect: function(path, fileStat, ev) {
2999 var browser = dom.parent(ev.target, '.cbi-filebrowser'),
3000 ul = browser.querySelector('ul');
3001
3002 if (fileStat == null) {
3003 dom.content(ul, E('em', { 'class': 'spinning' }, _('Loading directory contents…')));
3004 L.resolveDefault(fs.list(path), []).then(L.bind(this.renderListing, this, browser, path));
3005 }
3006 else if (!this.options.browser) {
3007 var button = this.node.firstElementChild,
3008 hidden = this.node.lastElementChild;
3009
3010 path = this.canonicalizePath(path);
3011
3012 dom.content(button, [
3013 this.iconForType(fileStat.type),
3014 ' %s (%1000mB)'.format(this.truncatePath(path), fileStat.size)
3015 ]);
3016
3017 browser.classList.remove('open');
3018 button.style.display = '';
3019 hidden.value = path;
3020
3021 this.stat = Object.assign({ path: path }, fileStat);
3022 this.node.dispatchEvent(new CustomEvent('cbi-fileupload-select', { detail: this.stat }));
3023 }
3024 },
3025
3026 /** @private */
3027 handleFileBrowser: function(ev) {
3028 var button = ev.target,
3029 browser = button.nextElementSibling,
3030 path = this.stat ? this.stat.path.replace(/\/[^\/]+$/, '') : (this.options.initial_directory || this.options.root_directory);
3031
3032 if (path.indexOf(this.options.root_directory) != 0)
3033 path = this.options.root_directory;
3034
3035 ev.preventDefault();
3036
3037 return L.resolveDefault(fs.list(path), []).then(L.bind(function(button, browser, path, list) {
3038 document.querySelectorAll('.cbi-filebrowser.open').forEach(function(browserEl) {
3039 dom.findClassInstance(browserEl).handleCancel(ev);
3040 });
3041
3042 button.style.display = 'none';
3043 browser.classList.add('open');
3044
3045 return this.renderListing(browser, path, list);
3046 }, this, button, browser, path));
3047 },
3048
3049 /** @override */
3050 getValue: function() {
3051 return this.node.lastElementChild.value;
3052 },
3053
3054 /** @override */
3055 setValue: function(value) {
3056 this.node.lastElementChild.value = value;
3057 }
3058 });
3059
3060
3061 function scrubMenu(node) {
3062 var hasSatisfiedChild = false;
3063
3064 if (L.isObject(node.children)) {
3065 for (var k in node.children) {
3066 var child = scrubMenu(node.children[k]);
3067
3068 if (child.title && !child.firstchild_ineligible)
3069 hasSatisfiedChild = hasSatisfiedChild || child.satisfied;
3070 }
3071 }
3072
3073 if (L.isObject(node.action) &&
3074 node.action.type == 'firstchild' &&
3075 hasSatisfiedChild == false)
3076 node.satisfied = false;
3077
3078 return node;
3079 };
3080
3081 /**
3082 * Handle menu.
3083 *
3084 * @constructor menu
3085 * @memberof LuCI.ui
3086 *
3087 * @classdesc
3088 *
3089 * Handles menus.
3090 */
3091 var UIMenu = baseclass.singleton(/** @lends LuCI.ui.menu.prototype */ {
3092 /**
3093 * @typedef {Object} MenuNode
3094 * @memberof LuCI.ui.menu
3095
3096 * @property {string} name - The internal name of the node, as used in the URL
3097 * @property {number} order - The sort index of the menu node
3098 * @property {string} [title] - The title of the menu node, `null` if the node should be hidden
3099 * @property {satisfied} boolean - Boolean indicating whether the menu entries dependencies are satisfied
3100 * @property {readonly} [boolean] - Boolean indicating whether the menu entries underlying ACLs are readonly
3101 * @property {LuCI.ui.menu.MenuNode[]} [children] - Array of child menu nodes.
3102 */
3103
3104 /**
3105 * Load and cache current menu tree.
3106 *
3107 * @returns {Promise<LuCI.ui.menu.MenuNode>}
3108 * Returns a promise resolving to the root element of the menu tree.
3109 */
3110 load: function() {
3111 if (this.menu == null)
3112 this.menu = session.getLocalData('menu');
3113
3114 if (!L.isObject(this.menu)) {
3115 this.menu = request.get(L.url('admin/menu')).then(L.bind(function(menu) {
3116 this.menu = scrubMenu(menu.json());
3117 session.setLocalData('menu', this.menu);
3118
3119 return this.menu;
3120 }, this));
3121 }
3122
3123 return Promise.resolve(this.menu);
3124 },
3125
3126 /**
3127 * Flush the internal menu cache to force loading a new structure on the
3128 * next page load.
3129 */
3130 flushCache: function() {
3131 session.setLocalData('menu', null);
3132 },
3133
3134 /**
3135 * @param {LuCI.ui.menu.MenuNode} [node]
3136 * The menu node to retrieve the children for. Defaults to the menu's
3137 * internal root node if omitted.
3138 *
3139 * @returns {LuCI.ui.menu.MenuNode[]}
3140 * Returns an array of child menu nodes.
3141 */
3142 getChildren: function(node) {
3143 var children = [];
3144
3145 if (node == null)
3146 node = this.menu;
3147
3148 for (var k in node.children) {
3149 if (!node.children.hasOwnProperty(k))
3150 continue;
3151
3152 if (!node.children[k].satisfied)
3153 continue;
3154
3155 if (!node.children[k].hasOwnProperty('title'))
3156 continue;
3157
3158 var subnode = Object.assign(node.children[k], { name: k });
3159
3160 if (L.isObject(subnode.action) && subnode.action.path != null &&
3161 (subnode.action.type == 'alias' || subnode.action.type == 'rewrite')) {
3162 var root = this.menu,
3163 path = subnode.action.path.split('/');
3164
3165 for (var i = 0; root != null && i < path.length; i++)
3166 root = L.isObject(root.children) ? root.children[path[i]] : null;
3167
3168 if (root)
3169 subnode = Object.assign({}, subnode, {
3170 children: root.children,
3171 action: root.action
3172 });
3173 }
3174
3175 children.push(subnode);
3176 }
3177
3178 return children.sort(function(a, b) {
3179 var wA = a.order || 1000,
3180 wB = b.order || 1000;
3181
3182 if (wA != wB)
3183 return wA - wB;
3184
3185 return L.naturalCompare(a.name, b.name);
3186 });
3187 }
3188 });
3189
3190 var UITable = baseclass.extend(/** @lends LuCI.ui.table.prototype */ {
3191 __init__: function(captions, options, placeholder) {
3192 if (!Array.isArray(captions)) {
3193 this.initFromMarkup(captions);
3194
3195 return;
3196 }
3197
3198 var id = options.id || 'table%08x'.format(Math.random() * 0xffffffff);
3199
3200 var table = E('table', { 'id': id, 'class': 'table' }, [
3201 E('tr', { 'class': 'tr table-titles', 'click': UI.prototype.createHandlerFn(this, 'handleSort') })
3202 ]);
3203
3204 this.id = id;
3205 this.node = table
3206 this.options = options;
3207
3208 var sorting = this.getActiveSortState();
3209
3210 for (var i = 0; i < captions.length; i++) {
3211 if (captions[i] == null)
3212 continue;
3213
3214 var th = E('th', { 'class': 'th' }, [ captions[i] ]);
3215
3216 if (typeof(options.captionClasses) == 'object')
3217 DOMTokenList.prototype.add.apply(th.classList, L.toArray(options.captionClasses[i]));
3218
3219 if (options.sortable !== false && (typeof(options.sortable) != 'object' || options.sortable[i] !== false)) {
3220 th.setAttribute('data-sortable-row', true);
3221
3222 if (sorting && sorting[0] == i)
3223 th.setAttribute('data-sort-direction', sorting[1] ? 'desc' : 'asc');
3224 }
3225
3226 table.firstElementChild.appendChild(th);
3227 }
3228
3229 if (placeholder) {
3230 var trow = table.appendChild(E('tr', { 'class': 'tr placeholder' })),
3231 td = trow.appendChild(E('td', { 'class': 'td' }, placeholder));
3232
3233 if (typeof(captionClasses) == 'object')
3234 DOMTokenList.prototype.add.apply(td.classList, L.toArray(captionClasses[0]));
3235 }
3236
3237 DOMTokenList.prototype.add.apply(table.classList, L.toArray(options.classes));
3238 },
3239
3240 update: function(data, placeholder) {
3241 var placeholder = placeholder || this.options.placeholder || _('No data', 'empty table placeholder'),
3242 sorting = this.getActiveSortState();
3243
3244 if (!Array.isArray(data))
3245 return;
3246
3247 this.data = data;
3248 this.placeholder = placeholder;
3249
3250 var n = 0,
3251 rows = this.node.querySelectorAll('tr, .tr'),
3252 trows = [],
3253 headings = [].slice.call(this.node.firstElementChild.querySelectorAll('th, .th')),
3254 captionClasses = this.options.captionClasses,
3255 trTag = (rows[0] && rows[0].nodeName == 'DIV') ? 'div' : 'tr',
3256 tdTag = (headings[0] && headings[0].nodeName == 'DIV') ? 'div' : 'td';
3257
3258 if (sorting) {
3259 var list = data.map(L.bind(function(row) {
3260 return [ this.deriveSortKey(row[sorting[0]], sorting[0]), row ];
3261 }, this));
3262
3263 list.sort(function(a, b) {
3264 return sorting[1]
3265 ? -L.naturalCompare(a[0], b[0])
3266 : L.naturalCompare(a[0], b[0]);
3267 });
3268
3269 data.length = 0;
3270
3271 list.forEach(function(item) {
3272 data.push(item[1]);
3273 });
3274
3275 headings.forEach(function(th, i) {
3276 if (i == sorting[0])
3277 th.setAttribute('data-sort-direction', sorting[1] ? 'desc' : 'asc');
3278 else
3279 th.removeAttribute('data-sort-direction');
3280 });
3281 }
3282
3283 data.forEach(function(row) {
3284 trows[n] = E(trTag, { 'class': 'tr' });
3285
3286 for (var i = 0; i < headings.length; i++) {
3287 var text = (headings[i].innerText || '').trim();
3288 var raw_val = Array.isArray(row[i]) ? row[i][0] : null;
3289 var disp_val = Array.isArray(row[i]) ? row[i][1] : row[i];
3290 var td = trows[n].appendChild(E(tdTag, {
3291 'class': 'td',
3292 'data-title': (text !== '') ? text : null,
3293 'data-value': raw_val
3294 }, (disp_val != null) ? ((disp_val instanceof DocumentFragment) ? disp_val.cloneNode(true) : disp_val) : ''));
3295
3296 if (typeof(captionClasses) == 'object')
3297 DOMTokenList.prototype.add.apply(td.classList, L.toArray(captionClasses[i]));
3298
3299 if (!td.classList.contains('cbi-section-actions'))
3300 headings[i].setAttribute('data-sortable-row', true);
3301 }
3302
3303 trows[n].classList.add('cbi-rowstyle-%d'.format((n++ % 2) ? 2 : 1));
3304 });
3305
3306 for (var i = 0; i < n; i++) {
3307 if (rows[i+1])
3308 this.node.replaceChild(trows[i], rows[i+1]);
3309 else
3310 this.node.appendChild(trows[i]);
3311 }
3312
3313 while (rows[++n])
3314 this.node.removeChild(rows[n]);
3315
3316 if (placeholder && this.node.firstElementChild === this.node.lastElementChild) {
3317 var trow = this.node.appendChild(E(trTag, { 'class': 'tr placeholder' })),
3318 td = trow.appendChild(E(tdTag, { 'class': 'td' }, placeholder));
3319
3320 if (typeof(captionClasses) == 'object')
3321 DOMTokenList.prototype.add.apply(td.classList, L.toArray(captionClasses[0]));
3322 }
3323
3324 return this.node;
3325 },
3326
3327 render: function() {
3328 return this.node;
3329 },
3330
3331 /** @private */
3332 initFromMarkup: function(node) {
3333 if (!dom.elem(node))
3334 node = document.querySelector(node);
3335
3336 if (!node)
3337 throw 'Invalid table selector';
3338
3339 var options = {},
3340 headrow = node.querySelector('tr, .tr');
3341
3342 if (!headrow)
3343 return;
3344
3345 options.id = node.id;
3346 options.classes = [].slice.call(node.classList).filter(function(c) { return c != 'table' });
3347 options.sortable = [];
3348 options.captionClasses = [];
3349
3350 headrow.querySelectorAll('th, .th').forEach(function(th, i) {
3351 options.sortable[i] = !th.classList.contains('cbi-section-actions');
3352 options.captionClasses[i] = [].slice.call(th.classList).filter(function(c) { return c != 'th' });
3353 });
3354
3355 headrow.addEventListener('click', UI.prototype.createHandlerFn(this, 'handleSort'));
3356
3357 this.id = node.id;
3358 this.node = node;
3359 this.options = options;
3360 },
3361
3362 /** @private */
3363 deriveSortKey: function(value, index) {
3364 var opts = this.options || {},
3365 hint, m;
3366
3367 if (opts.sortable == true || opts.sortable == null)
3368 hint = 'auto';
3369 else if (typeof( opts.sortable) == 'object')
3370 hint = opts.sortable[index];
3371
3372 if (dom.elem(value)) {
3373 if (value.hasAttribute('data-value'))
3374 value = value.getAttribute('data-value');
3375 else
3376 value = (value.innerText || '').trim();
3377 }
3378
3379 switch (hint || 'auto') {
3380 case true:
3381 case 'auto':
3382 m = /^([0-9a-fA-F:.]+)(?:\/([0-9a-fA-F:.]+))?$/.exec(value);
3383
3384 if (m) {
3385 var addr, mask;
3386
3387 addr = validation.parseIPv6(m[1]);
3388 mask = m[2] ? validation.parseIPv6(m[2]) : null;
3389
3390 if (addr && mask != null)
3391 return '%04x%04x%04x%04x%04x%04x%04x%04x%04x%04x%04x%04x%04x%04x%04x%04x'.format(
3392 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7],
3393 mask[0], mask[1], mask[2], mask[3], mask[4], mask[5], mask[6], mask[7]
3394 );
3395 else if (addr)
3396 return '%04x%04x%04x%04x%04x%04x%04x%04x%02x'.format(
3397 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7],
3398 m[2] ? +m[2] : 128
3399 );
3400
3401 addr = validation.parseIPv4(m[1]);
3402 mask = m[2] ? validation.parseIPv4(m[2]) : null;
3403
3404 if (addr && mask != null)
3405 return '%03d%03d%03d%03d%03d%03d%03d%03d'.format(
3406 addr[0], addr[1], addr[2], addr[3],
3407 mask[0], mask[1], mask[2], mask[3]
3408 );
3409 else if (addr)
3410 return '%03d%03d%03d%03d%02d'.format(
3411 addr[0], addr[1], addr[2], addr[3],
3412 m[2] ? +m[2] : 32
3413 );
3414 }
3415
3416 m = /^(?:(\d+)d )?(\d+)h (\d+)m (\d+)s$/.exec(value);
3417
3418 if (m)
3419 return '%05d%02d%02d%02d'.format(+m[1], +m[2], +m[3], +m[4]);
3420
3421 m = /^(\d+)\b(\D*)$/.exec(value);
3422
3423 if (m)
3424 return '%010d%s'.format(+m[1], m[2]);
3425
3426 return String(value);
3427
3428 case 'ignorecase':
3429 return String(value).toLowerCase();
3430
3431 case 'numeric':
3432 return +value;
3433
3434 default:
3435 return String(value);
3436 }
3437 },
3438
3439 /** @private */
3440 getActiveSortState: function() {
3441 if (this.sortState)
3442 return this.sortState;
3443
3444 if (!this.options.id)
3445 return null;
3446
3447 var page = document.body.getAttribute('data-page'),
3448 key = page + '.' + this.options.id,
3449 state = session.getLocalData('tablesort');
3450
3451 if (L.isObject(state) && Array.isArray(state[key]))
3452 return state[key];
3453
3454 return null;
3455 },
3456
3457 /** @private */
3458 setActiveSortState: function(index, descending) {
3459 this.sortState = [ index, descending ];
3460
3461 if (!this.options.id)
3462 return;
3463
3464 var page = document.body.getAttribute('data-page'),
3465 key = page + '.' + this.options.id,
3466 state = session.getLocalData('tablesort');
3467
3468 if (!L.isObject(state))
3469 state = {};
3470
3471 state[key] = this.sortState;
3472
3473 session.setLocalData('tablesort', state);
3474 },
3475
3476 /** @private */
3477 handleSort: function(ev) {
3478 if (!ev.target.matches('th[data-sortable-row]'))
3479 return;
3480
3481 var index, direction;
3482
3483 this.node.firstElementChild.querySelectorAll('th, .th').forEach(function(th, i) {
3484 if (th === ev.target) {
3485 index = i;
3486 direction = th.getAttribute('data-sort-direction') == 'asc';
3487 }
3488 });
3489
3490 this.setActiveSortState(index, direction);
3491 this.update(this.data, this.placeholder);
3492 }
3493 });
3494
3495 /**
3496 * @class ui
3497 * @memberof LuCI
3498 * @hideconstructor
3499 * @classdesc
3500 *
3501 * Provides high level UI helper functionality.
3502 * To import the class in views, use `'require ui'`, to import it in
3503 * external JavaScript, use `L.require("ui").then(...)`.
3504 */
3505 var UI = baseclass.extend(/** @lends LuCI.ui.prototype */ {
3506 __init__: function() {
3507 modalDiv = document.body.appendChild(
3508 dom.create('div', {
3509 id: 'modal_overlay',
3510 tabindex: -1,
3511 keydown: this.cancelModal
3512 }, [
3513 dom.create('div', {
3514 class: 'modal',
3515 role: 'dialog',
3516 'aria-modal': true
3517 })
3518 ]));
3519
3520 tooltipDiv = document.body.appendChild(
3521 dom.create('div', { class: 'cbi-tooltip' }));
3522
3523 /* set up old aliases */
3524 L.showModal = this.showModal;
3525 L.hideModal = this.hideModal;
3526 L.showTooltip = this.showTooltip;
3527 L.hideTooltip = this.hideTooltip;
3528 L.itemlist = this.itemlist;
3529
3530 document.addEventListener('mouseover', this.showTooltip.bind(this), true);
3531 document.addEventListener('mouseout', this.hideTooltip.bind(this), true);
3532 document.addEventListener('focus', this.showTooltip.bind(this), true);
3533 document.addEventListener('blur', this.hideTooltip.bind(this), true);
3534
3535 document.addEventListener('luci-loaded', this.tabs.init.bind(this.tabs));
3536 document.addEventListener('luci-loaded', this.changes.init.bind(this.changes));
3537 document.addEventListener('uci-loaded', this.changes.init.bind(this.changes));
3538 },
3539
3540 /**
3541 * Display a modal overlay dialog with the specified contents.
3542 *
3543 * The modal overlay dialog covers the current view preventing interaction
3544 * with the underlying view contents. Only one modal dialog instance can
3545 * be opened. Invoking showModal() while a modal dialog is already open will
3546 * replace the open dialog with a new one having the specified contents.
3547 *
3548 * Additional CSS class names may be passed to influence the appearance of
3549 * the dialog. Valid values for the classes depend on the underlying theme.
3550 *
3551 * @see LuCI.dom.content
3552 *
3553 * @param {string} [title]
3554 * The title of the dialog. If `null`, no title element will be rendered.
3555 *
3556 * @param {*} children
3557 * The contents to add to the modal dialog. This should be a DOM node or
3558 * a document fragment in most cases. The value is passed as-is to the
3559 * `dom.content()` function - refer to its documentation for applicable
3560 * values.
3561 *
3562 * @param {...string} [classes]
3563 * A number of extra CSS class names which are set on the modal dialog
3564 * element.
3565 *
3566 * @returns {Node}
3567 * Returns a DOM Node representing the modal dialog element.
3568 */
3569 showModal: function(title, children /* , ... */) {
3570 var dlg = modalDiv.firstElementChild;
3571
3572 dlg.setAttribute('class', 'modal');
3573
3574 for (var i = 2; i < arguments.length; i++)
3575 dlg.classList.add(arguments[i]);
3576
3577 dom.content(dlg, dom.create('h4', {}, title));
3578 dom.append(dlg, children);
3579
3580 document.body.classList.add('modal-overlay-active');
3581 modalDiv.scrollTop = 0;
3582 modalDiv.focus();
3583
3584 return dlg;
3585 },
3586
3587 /**
3588 * Close the open modal overlay dialog.
3589 *
3590 * This function will close an open modal dialog and restore the normal view
3591 * behaviour. It has no effect if no modal dialog is currently open.
3592 *
3593 * Note that this function is stand-alone, it does not rely on `this` and
3594 * will not invoke other class functions so it is suitable to be used as event
3595 * handler as-is without the need to bind it first.
3596 */
3597 hideModal: function() {
3598 document.body.classList.remove('modal-overlay-active');
3599 modalDiv.blur();
3600 },
3601
3602 /** @private */
3603 cancelModal: function(ev) {
3604 if (ev.key == 'Escape') {
3605 var btn = modalDiv.querySelector('.right > button, .right > .btn');
3606
3607 if (btn)
3608 btn.click();
3609 }
3610 },
3611
3612 /** @private */
3613 showTooltip: function(ev) {
3614 var target = findParent(ev.target, '[data-tooltip]');
3615
3616 if (!target)
3617 return;
3618
3619 if (tooltipTimeout !== null) {
3620 window.clearTimeout(tooltipTimeout);
3621 tooltipTimeout = null;
3622 }
3623
3624 var rect = target.getBoundingClientRect(),
3625 x = rect.left + window.pageXOffset,
3626 y = rect.top + rect.height + window.pageYOffset,
3627 above = false;
3628
3629 tooltipDiv.className = 'cbi-tooltip';
3630 tooltipDiv.innerHTML = '▲ ';
3631 tooltipDiv.firstChild.data += target.getAttribute('data-tooltip');
3632
3633 if (target.hasAttribute('data-tooltip-style'))
3634 tooltipDiv.classList.add(target.getAttribute('data-tooltip-style'));
3635
3636 if ((y + tooltipDiv.offsetHeight) > (window.innerHeight + window.pageYOffset))
3637 above = true;
3638
3639 var dropdown = target.querySelector('ul.dropdown[style]:first-child');
3640
3641 if (dropdown && dropdown.style.top)
3642 above = true;
3643
3644 if (above) {
3645 y -= (tooltipDiv.offsetHeight + target.offsetHeight);
3646 tooltipDiv.firstChild.data = '▼ ' + tooltipDiv.firstChild.data.substr(2);
3647 }
3648
3649 tooltipDiv.style.top = y + 'px';
3650 tooltipDiv.style.left = x + 'px';
3651 tooltipDiv.style.opacity = 1;
3652
3653 tooltipDiv.dispatchEvent(new CustomEvent('tooltip-open', {
3654 bubbles: true,
3655 detail: { target: target }
3656 }));
3657 },
3658
3659 /** @private */
3660 hideTooltip: function(ev) {
3661 if (ev.target === tooltipDiv || ev.relatedTarget === tooltipDiv ||
3662 tooltipDiv.contains(ev.target) || tooltipDiv.contains(ev.relatedTarget))
3663 return;
3664
3665 if (tooltipTimeout !== null) {
3666 window.clearTimeout(tooltipTimeout);
3667 tooltipTimeout = null;
3668 }
3669
3670 tooltipDiv.style.opacity = 0;
3671 tooltipTimeout = window.setTimeout(function() { tooltipDiv.removeAttribute('style'); }, 250);
3672
3673 tooltipDiv.dispatchEvent(new CustomEvent('tooltip-close', { bubbles: true }));
3674 },
3675
3676 /**
3677 * Add a notification banner at the top of the current view.
3678 *
3679 * A notification banner is an alert message usually displayed at the
3680 * top of the current view, spanning the entire available width.
3681 * Notification banners will stay in place until dismissed by the user.
3682 * Multiple banners may be shown at the same time.
3683 *
3684 * Additional CSS class names may be passed to influence the appearance of
3685 * the banner. Valid values for the classes depend on the underlying theme.
3686 *
3687 * @see LuCI.dom.content
3688 *
3689 * @param {string} [title]
3690 * The title of the notification banner. If `null`, no title element
3691 * will be rendered.
3692 *
3693 * @param {*} children
3694 * The contents to add to the notification banner. This should be a DOM
3695 * node or a document fragment in most cases. The value is passed as-is
3696 * to the `dom.content()` function - refer to its documentation for
3697 * applicable values.
3698 *
3699 * @param {...string} [classes]
3700 * A number of extra CSS class names which are set on the notification
3701 * banner element.
3702 *
3703 * @returns {Node}
3704 * Returns a DOM Node representing the notification banner element.
3705 */
3706 addNotification: function(title, children /*, ... */) {
3707 var mc = document.querySelector('#maincontent') || document.body;
3708 var msg = E('div', {
3709 'class': 'alert-message fade-in',
3710 'style': 'display:flex',
3711 'transitionend': function(ev) {
3712 var node = ev.currentTarget;
3713 if (node.parentNode && node.classList.contains('fade-out'))
3714 node.parentNode.removeChild(node);
3715 }
3716 }, [
3717 E('div', { 'style': 'flex:10' }),
3718 E('div', { 'style': 'flex:1 1 auto; display:flex' }, [
3719 E('button', {
3720 'class': 'btn',
3721 'style': 'margin-left:auto; margin-top:auto',
3722 'click': function(ev) {
3723 dom.parent(ev.target, '.alert-message').classList.add('fade-out');
3724 },
3725
3726 }, [ _('Dismiss') ])
3727 ])
3728 ]);
3729
3730 if (title != null)
3731 dom.append(msg.firstElementChild, E('h4', {}, title));
3732
3733 dom.append(msg.firstElementChild, children);
3734
3735 for (var i = 2; i < arguments.length; i++)
3736 msg.classList.add(arguments[i]);
3737
3738 mc.insertBefore(msg, mc.firstElementChild);
3739
3740 return msg;
3741 },
3742
3743 /**
3744 * Display or update a header area indicator.
3745 *
3746 * An indicator is a small label displayed in the header area of the screen
3747 * providing few amounts of status information such as item counts or state
3748 * toggle indicators.
3749 *
3750 * Multiple indicators may be shown at the same time and indicator labels
3751 * may be made clickable to display extended information or to initiate
3752 * further actions.
3753 *
3754 * Indicators can either use a default `active` or a less accented `inactive`
3755 * style which is useful for indicators representing state toggles.
3756 *
3757 * @param {string} id
3758 * The ID of the indicator. If an indicator with the given ID already exists,
3759 * it is updated with the given label and style.
3760 *
3761 * @param {string} label
3762 * The text to display in the indicator label.
3763 *
3764 * @param {function} [handler]
3765 * A handler function to invoke when the indicator label is clicked/touched
3766 * by the user. If omitted, the indicator is not clickable/touchable.
3767 *
3768 * Note that this parameter only applies to new indicators, when updating
3769 * existing labels it is ignored.
3770 *
3771 * @param {"active"|"inactive"} [style=active]
3772 * The indicator style to use. May be either `active` or `inactive`.
3773 *
3774 * @returns {boolean}
3775 * Returns `true` when the indicator has been updated or `false` when no
3776 * changes were made.
3777 */
3778 showIndicator: function(id, label, handler, style) {
3779 if (indicatorDiv == null) {
3780 indicatorDiv = document.body.querySelector('#indicators');
3781
3782 if (indicatorDiv == null)
3783 return false;
3784 }
3785
3786 var handlerFn = (typeof(handler) == 'function') ? handler : null,
3787 indicatorElem = indicatorDiv.querySelector('span[data-indicator="%s"]'.format(id));
3788
3789 if (indicatorElem == null) {
3790 var beforeElem = null;
3791
3792 for (beforeElem = indicatorDiv.firstElementChild;
3793 beforeElem != null;
3794 beforeElem = beforeElem.nextElementSibling)
3795 if (beforeElem.getAttribute('data-indicator') > id)
3796 break;
3797
3798 indicatorElem = indicatorDiv.insertBefore(E('span', {
3799 'data-indicator': id,
3800 'data-clickable': handlerFn ? true : null,
3801 'click': handlerFn
3802 }, ['']), beforeElem);
3803 }
3804
3805 if (label == indicatorElem.firstChild.data && style == indicatorElem.getAttribute('data-style'))
3806 return false;
3807
3808 indicatorElem.firstChild.data = label;
3809 indicatorElem.setAttribute('data-style', (style == 'inactive') ? 'inactive' : 'active');
3810 return true;
3811 },
3812
3813 /**
3814 * Remove a header area indicator.
3815 *
3816 * This function removes the given indicator label from the header indicator
3817 * area. When the given indicator is not found, this function does nothing.
3818 *
3819 * @param {string} id
3820 * The ID of the indicator to remove.
3821 *
3822 * @returns {boolean}
3823 * Returns `true` when the indicator has been removed or `false` when the
3824 * requested indicator was not found.
3825 */
3826 hideIndicator: function(id) {
3827 var indicatorElem = indicatorDiv ? indicatorDiv.querySelector('span[data-indicator="%s"]'.format(id)) : null;
3828
3829 if (indicatorElem == null)
3830 return false;
3831
3832 indicatorDiv.removeChild(indicatorElem);
3833 return true;
3834 },
3835
3836 /**
3837 * Formats a series of label/value pairs into list-like markup.
3838 *
3839 * This function transforms a flat array of alternating label and value
3840 * elements into a list-like markup, using the values in `separators` as
3841 * separators and appends the resulting nodes to the given parent DOM node.
3842 *
3843 * Each label is suffixed with `: ` and wrapped into a `<strong>` tag, the
3844 * `<strong>` element and the value corresponding to the label are
3845 * subsequently wrapped into a `<span class="nowrap">` element.
3846 *
3847 * The resulting `<span>` element tuples are joined by the given separators
3848 * to form the final markup which is appended to the given parent DOM node.
3849 *
3850 * @param {Node} node
3851 * The parent DOM node to append the markup to. Any previous child elements
3852 * will be removed.
3853 *
3854 * @param {Array<*>} items
3855 * An alternating array of labels and values. The label values will be
3856 * converted to plain strings, the values are used as-is and may be of
3857 * any type accepted by `LuCI.dom.content()`.
3858 *
3859 * @param {*|Array<*>} [separators=[E('br')]]
3860 * A single value or an array of separator values to separate each
3861 * label/value pair with. The function will cycle through the separators
3862 * when joining the pairs. If omitted, the default separator is a sole HTML
3863 * `<br>` element. Separator values are used as-is and may be of any type
3864 * accepted by `LuCI.dom.content()`.
3865 *
3866 * @returns {Node}
3867 * Returns the parent DOM node the formatted markup has been added to.
3868 */
3869 itemlist: function(node, items, separators) {
3870 var children = [];
3871
3872 if (!Array.isArray(separators))
3873 separators = [ separators || E('br') ];
3874
3875 for (var i = 0; i < items.length; i += 2) {
3876 if (items[i+1] !== null && items[i+1] !== undefined) {
3877 var sep = separators[(i/2) % separators.length],
3878 cld = [];
3879
3880 children.push(E('span', { class: 'nowrap' }, [
3881 items[i] ? E('strong', items[i] + ': ') : '',
3882 items[i+1]
3883 ]));
3884
3885 if ((i+2) < items.length)
3886 children.push(dom.elem(sep) ? sep.cloneNode(true) : sep);
3887 }
3888 }
3889
3890 dom.content(node, children);
3891
3892 return node;
3893 },
3894
3895 /**
3896 * @class
3897 * @memberof LuCI.ui
3898 * @hideconstructor
3899 * @classdesc
3900 *
3901 * The `tabs` class handles tab menu groups used throughout the view area.
3902 * It takes care of setting up tab groups, tracking their state and handling
3903 * related events.
3904 *
3905 * This class is automatically instantiated as part of `LuCI.ui`. To use it
3906 * in views, use `'require ui'` and refer to `ui.tabs`. To import it in
3907 * external JavaScript, use `L.require("ui").then(...)` and access the
3908 * `tabs` property of the class instance value.
3909 */
3910 tabs: baseclass.singleton(/* @lends LuCI.ui.tabs.prototype */ {
3911 /** @private */
3912 init: function() {
3913 var groups = [], prevGroup = null, currGroup = null;
3914
3915 document.querySelectorAll('[data-tab]').forEach(function(tab) {
3916 var parent = tab.parentNode;
3917
3918 if (dom.matches(tab, 'li') && dom.matches(parent, 'ul.cbi-tabmenu'))
3919 return;
3920
3921 if (!parent.hasAttribute('data-tab-group'))
3922 parent.setAttribute('data-tab-group', groups.length);
3923
3924 currGroup = +parent.getAttribute('data-tab-group');
3925
3926 if (currGroup !== prevGroup) {
3927 prevGroup = currGroup;
3928
3929 if (!groups[currGroup])
3930 groups[currGroup] = [];
3931 }
3932
3933 groups[currGroup].push(tab);
3934 });
3935
3936 for (var i = 0; i < groups.length; i++)
3937 this.initTabGroup(groups[i]);
3938
3939 document.addEventListener('dependency-update', this.updateTabs.bind(this));
3940
3941 this.updateTabs();
3942 },
3943
3944 /**
3945 * Initializes a new tab group from the given tab pane collection.
3946 *
3947 * This function cycles through the given tab pane DOM nodes, extracts
3948 * their tab IDs, titles and active states, renders a corresponding
3949 * tab menu and prepends it to the tab panes common parent DOM node.
3950 *
3951 * The tab menu labels will be set to the value of the `data-tab-title`
3952 * attribute of each corresponding pane. The last pane with the
3953 * `data-tab-active` attribute set to `true` will be selected by default.
3954 *
3955 * If no pane is marked as active, the first one will be preselected.
3956 *
3957 * @instance
3958 * @memberof LuCI.ui.tabs
3959 * @param {Array<Node>|NodeList} panes
3960 * A collection of tab panes to build a tab group menu for. May be a
3961 * plain array of DOM nodes or a NodeList collection, such as the result
3962 * of a `querySelectorAll()` call or the `.childNodes` property of a
3963 * DOM node.
3964 */
3965 initTabGroup: function(panes) {
3966 if (typeof(panes) != 'object' || !('length' in panes) || panes.length === 0)
3967 return;
3968
3969 var menu = E('ul', { 'class': 'cbi-tabmenu' }),
3970 group = panes[0].parentNode,
3971 groupId = +group.getAttribute('data-tab-group'),
3972 selected = null;
3973
3974 if (group.getAttribute('data-initialized') === 'true')
3975 return;
3976
3977 for (var i = 0, pane; pane = panes[i]; i++) {
3978 var name = pane.getAttribute('data-tab'),
3979 title = pane.getAttribute('data-tab-title'),
3980 active = pane.getAttribute('data-tab-active') === 'true';
3981
3982 menu.appendChild(E('li', {
3983 'style': this.isEmptyPane(pane) ? 'display:none' : null,
3984 'class': active ? 'cbi-tab' : 'cbi-tab-disabled',
3985 'data-tab': name
3986 }, E('a', {
3987 'href': '#',
3988 'click': this.switchTab.bind(this)
3989 }, title)));
3990
3991 if (active)
3992 selected = i;
3993 }
3994
3995 group.parentNode.insertBefore(menu, group);
3996 group.setAttribute('data-initialized', true);
3997
3998 if (selected === null) {
3999 selected = this.getActiveTabId(panes[0]);
4000
4001 if (selected < 0 || selected >= panes.length || this.isEmptyPane(panes[selected])) {
4002 for (var i = 0; i < panes.length; i++) {
4003 if (!this.isEmptyPane(panes[i])) {
4004 selected = i;
4005 break;
4006 }
4007 }
4008 }
4009
4010 menu.childNodes[selected].classList.add('cbi-tab');
4011 menu.childNodes[selected].classList.remove('cbi-tab-disabled');
4012 panes[selected].setAttribute('data-tab-active', 'true');
4013
4014 this.setActiveTabId(panes[selected], selected);
4015 }
4016
4017 requestAnimationFrame(L.bind(function(pane) {
4018 pane.dispatchEvent(new CustomEvent('cbi-tab-active', {
4019 detail: { tab: pane.getAttribute('data-tab') }
4020 }));
4021 }, this, panes[selected]));
4022
4023 this.updateTabs(group);
4024 },
4025
4026 /**
4027 * Checks whether the given tab pane node is empty.
4028 *
4029 * @instance
4030 * @memberof LuCI.ui.tabs
4031 * @param {Node} pane
4032 * The tab pane to check.
4033 *
4034 * @returns {boolean}
4035 * Returns `true` if the pane is empty, else `false`.
4036 */
4037 isEmptyPane: function(pane) {
4038 return dom.isEmpty(pane, function(n) { return n.classList.contains('cbi-tab-descr') });
4039 },
4040
4041 /** @private */
4042 getPathForPane: function(pane) {
4043 var path = [], node = null;
4044
4045 for (node = pane ? pane.parentNode : null;
4046 node != null && node.hasAttribute != null;
4047 node = node.parentNode)
4048 {
4049 if (node.hasAttribute('data-tab'))
4050 path.unshift(node.getAttribute('data-tab'));
4051 else if (node.hasAttribute('data-section-id'))
4052 path.unshift(node.getAttribute('data-section-id'));
4053 }
4054
4055 return path.join('/');
4056 },
4057
4058 /** @private */
4059 getActiveTabState: function() {
4060 var page = document.body.getAttribute('data-page'),
4061 state = session.getLocalData('tab');
4062
4063 if (L.isObject(state) && state.page === page && L.isObject(state.paths))
4064 return state;
4065
4066 session.setLocalData('tab', null);
4067
4068 return { page: page, paths: {} };
4069 },
4070
4071 /** @private */
4072 getActiveTabId: function(pane) {
4073 var path = this.getPathForPane(pane);
4074 return +this.getActiveTabState().paths[path] || 0;
4075 },
4076
4077 /** @private */
4078 setActiveTabId: function(pane, tabIndex) {
4079 var path = this.getPathForPane(pane),
4080 state = this.getActiveTabState();
4081
4082 state.paths[path] = tabIndex;
4083
4084 return session.setLocalData('tab', state);
4085 },
4086
4087 /** @private */
4088 updateTabs: function(ev, root) {
4089 (root || document).querySelectorAll('[data-tab-title]').forEach(L.bind(function(pane) {
4090 var menu = pane.parentNode.previousElementSibling,
4091 tab = menu ? menu.querySelector('[data-tab="%s"]'.format(pane.getAttribute('data-tab'))) : null,
4092 n_errors = pane.querySelectorAll('.cbi-input-invalid').length;
4093
4094 if (!menu || !tab)
4095 return;
4096
4097 if (this.isEmptyPane(pane)) {
4098 tab.style.display = 'none';
4099 tab.classList.remove('flash');
4100 }
4101 else if (tab.style.display === 'none') {
4102 tab.style.display = '';
4103 requestAnimationFrame(function() { tab.classList.add('flash') });
4104 }
4105
4106 if (n_errors) {
4107 tab.setAttribute('data-errors', n_errors);
4108 tab.setAttribute('data-tooltip', _('%d invalid field(s)').format(n_errors));
4109 tab.setAttribute('data-tooltip-style', 'error');
4110 }
4111 else {
4112 tab.removeAttribute('data-errors');
4113 tab.removeAttribute('data-tooltip');
4114 }
4115 }, this));
4116 },
4117
4118 /** @private */
4119 switchTab: function(ev) {
4120 var tab = ev.target.parentNode,
4121 name = tab.getAttribute('data-tab'),
4122 menu = tab.parentNode,
4123 group = menu.nextElementSibling,
4124 groupId = +group.getAttribute('data-tab-group'),
4125 index = 0;
4126
4127 ev.preventDefault();
4128
4129 if (!tab.classList.contains('cbi-tab-disabled'))
4130 return;
4131
4132 menu.querySelectorAll('[data-tab]').forEach(function(tab) {
4133 tab.classList.remove('cbi-tab');
4134 tab.classList.remove('cbi-tab-disabled');
4135 tab.classList.add(
4136 tab.getAttribute('data-tab') === name ? 'cbi-tab' : 'cbi-tab-disabled');
4137 });
4138
4139 group.childNodes.forEach(function(pane) {
4140 if (dom.matches(pane, '[data-tab]')) {
4141 if (pane.getAttribute('data-tab') === name) {
4142 pane.setAttribute('data-tab-active', 'true');
4143 pane.dispatchEvent(new CustomEvent('cbi-tab-active', { detail: { tab: name } }));
4144 UI.prototype.tabs.setActiveTabId(pane, index);
4145 }
4146 else {
4147 pane.setAttribute('data-tab-active', 'false');
4148 }
4149
4150 index++;
4151 }
4152 });
4153 }
4154 }),
4155
4156 /**
4157 * @typedef {Object} FileUploadReply
4158 * @memberof LuCI.ui
4159
4160 * @property {string} name - Name of the uploaded file without directory components
4161 * @property {number} size - Size of the uploaded file in bytes
4162 * @property {string} checksum - The MD5 checksum of the received file data
4163 * @property {string} sha256sum - The SHA256 checksum of the received file data
4164 */
4165
4166 /**
4167 * Display a modal file upload prompt.
4168 *
4169 * This function opens a modal dialog prompting the user to select and
4170 * upload a file to a predefined remote destination path.
4171 *
4172 * @param {string} path
4173 * The remote file path to upload the local file to.
4174 *
4175 * @param {Node} [progressStatusNode]
4176 * An optional DOM text node whose content text is set to the progress
4177 * percentage value during file upload.
4178 *
4179 * @returns {Promise<LuCI.ui.FileUploadReply>}
4180 * Returns a promise resolving to a file upload status object on success
4181 * or rejecting with an error in case the upload failed or has been
4182 * cancelled by the user.
4183 */
4184 uploadFile: function(path, progressStatusNode) {
4185 return new Promise(function(resolveFn, rejectFn) {
4186 UI.prototype.showModal(_('Uploading file…'), [
4187 E('p', _('Please select the file to upload.')),
4188 E('div', { 'style': 'display:flex' }, [
4189 E('div', { 'class': 'left', 'style': 'flex:1' }, [
4190 E('input', {
4191 type: 'file',
4192 style: 'display:none',
4193 change: function(ev) {
4194 var modal = dom.parent(ev.target, '.modal'),
4195 body = modal.querySelector('p'),
4196 upload = modal.querySelector('.cbi-button-action.important'),
4197 file = ev.currentTarget.files[0];
4198
4199 if (file == null)
4200 return;
4201
4202 dom.content(body, [
4203 E('ul', {}, [
4204 E('li', {}, [ '%s: %s'.format(_('Name'), file.name.replace(/^.*[\\\/]/, '')) ]),
4205 E('li', {}, [ '%s: %1024mB'.format(_('Size'), file.size) ])
4206 ])
4207 ]);
4208
4209 upload.disabled = false;
4210 upload.focus();
4211 }
4212 }),
4213 E('button', {
4214 'class': 'btn',
4215 'click': function(ev) {
4216 ev.target.previousElementSibling.click();
4217 }
4218 }, [ _('Browse…') ])
4219 ]),
4220 E('div', { 'class': 'right', 'style': 'flex:1' }, [
4221 E('button', {
4222 'class': 'btn',
4223 'click': function() {
4224 UI.prototype.hideModal();
4225 rejectFn(new Error(_('Upload has been cancelled')));
4226 }
4227 }, [ _('Cancel') ]),
4228 ' ',
4229 E('button', {
4230 'class': 'btn cbi-button-action important',
4231 'disabled': true,
4232 'click': function(ev) {
4233 var input = dom.parent(ev.target, '.modal').querySelector('input[type="file"]');
4234
4235 if (!input.files[0])
4236 return;
4237
4238 var progress = E('div', { 'class': 'cbi-progressbar', 'title': '0%' }, E('div', { 'style': 'width:0' }));
4239
4240 UI.prototype.showModal(_('Uploading file…'), [ progress ]);
4241
4242 var data = new FormData();
4243
4244 data.append('sessionid', rpc.getSessionID());
4245 data.append('filename', path);
4246 data.append('filedata', input.files[0]);
4247
4248 var filename = input.files[0].name;
4249
4250 request.post(L.env.cgi_base + '/cgi-upload', data, {
4251 timeout: 0,
4252 progress: function(pev) {
4253 var percent = (pev.loaded / pev.total) * 100;
4254
4255 if (progressStatusNode)
4256 progressStatusNode.data = '%.2f%%'.format(percent);
4257
4258 progress.setAttribute('title', '%.2f%%'.format(percent));
4259 progress.firstElementChild.style.width = '%.2f%%'.format(percent);
4260 }
4261 }).then(function(res) {
4262 var reply = res.json();
4263
4264 UI.prototype.hideModal();
4265
4266 if (L.isObject(reply) && reply.failure) {
4267 UI.prototype.addNotification(null, E('p', _('Upload request failed: %s').format(reply.message)));
4268 rejectFn(new Error(reply.failure));
4269 }
4270 else {
4271 reply.name = filename;
4272 resolveFn(reply);
4273 }
4274 }, function(err) {
4275 UI.prototype.hideModal();
4276 rejectFn(err);
4277 });
4278 }
4279 }, [ _('Upload') ])
4280 ])
4281 ])
4282 ]);
4283 });
4284 },
4285
4286 /**
4287 * Perform a device connectivity test.
4288 *
4289 * Attempt to fetch a well known resource from the remote device via HTTP
4290 * in order to test connectivity. This function is mainly useful to wait
4291 * for the router to come back online after a reboot or reconfiguration.
4292 *
4293 * @param {string} [proto=http]
4294 * The protocol to use for fetching the resource. May be either `http`
4295 * (the default) or `https`.
4296 *
4297 * @param {string} [ipaddr=window.location.host]
4298 * Override the host address to probe. By default the current host as seen
4299 * in the address bar is probed.
4300 *
4301 * @returns {Promise<Event>}
4302 * Returns a promise resolving to a `load` event in case the device is
4303 * reachable or rejecting with an `error` event in case it is not reachable
4304 * or rejecting with `null` when the connectivity check timed out.
4305 */
4306 pingDevice: function(proto, ipaddr) {
4307 var target = '%s://%s%s?%s'.format(proto || 'http', ipaddr || window.location.host, L.resource('icons/loading.gif'), Math.random());
4308
4309 return new Promise(function(resolveFn, rejectFn) {
4310 var img = new Image();
4311
4312 img.onload = resolveFn;
4313 img.onerror = rejectFn;
4314
4315 window.setTimeout(rejectFn, 1000);
4316
4317 img.src = target;
4318 });
4319 },
4320
4321 /**
4322 * Wait for device to come back online and reconnect to it.
4323 *
4324 * Poll each given hostname or IP address and navigate to it as soon as
4325 * one of the addresses becomes reachable.
4326 *
4327 * @param {...string} [hosts=[window.location.host]]
4328 * The list of IP addresses and host names to check for reachability.
4329 * If omitted, the current value of `window.location.host` is used by
4330 * default.
4331 */
4332 awaitReconnect: function(/* ... */) {
4333 var ipaddrs = arguments.length ? arguments : [ window.location.host ];
4334
4335 window.setTimeout(L.bind(function() {
4336 poll.add(L.bind(function() {
4337 var tasks = [], reachable = false;
4338
4339 for (var i = 0; i < 2; i++)
4340 for (var j = 0; j < ipaddrs.length; j++)
4341 tasks.push(this.pingDevice(i ? 'https' : 'http', ipaddrs[j])
4342 .then(function(ev) { reachable = ev.target.src.replace(/^(https?:\/\/[^\/]+).*$/, '$1/') }, function() {}));
4343
4344 return Promise.all(tasks).then(function() {
4345 if (reachable) {
4346 poll.stop();
4347 window.location = reachable;
4348 }
4349 });
4350 }, this));
4351 }, this), 5000);
4352 },
4353
4354 /**
4355 * @class
4356 * @memberof LuCI.ui
4357 * @hideconstructor
4358 * @classdesc
4359 *
4360 * The `changes` class encapsulates logic for visualizing, applying,
4361 * confirming and reverting staged UCI changesets.
4362 *
4363 * This class is automatically instantiated as part of `LuCI.ui`. To use it
4364 * in views, use `'require ui'` and refer to `ui.changes`. To import it in
4365 * external JavaScript, use `L.require("ui").then(...)` and access the
4366 * `changes` property of the class instance value.
4367 */
4368 changes: baseclass.singleton(/* @lends LuCI.ui.changes.prototype */ {
4369 init: function() {
4370 if (!L.env.sessionid)
4371 return;
4372
4373 return uci.changes().then(L.bind(this.renderChangeIndicator, this));
4374 },
4375
4376 /**
4377 * Set the change count indicator.
4378 *
4379 * This function updates or hides the UCI change count indicator,
4380 * depending on the passed change count. When the count is greater
4381 * than 0, the change indicator is displayed or updated, otherwise it
4382 * is removed.
4383 *
4384 * @instance
4385 * @memberof LuCI.ui.changes
4386 * @param {number} n
4387 * The number of changes to indicate.
4388 */
4389 setIndicator: function(n) {
4390 if (n > 0) {
4391 UI.prototype.showIndicator('uci-changes',
4392 '%s: %d'.format(_('Unsaved Changes'), n),
4393 L.bind(this.displayChanges, this));
4394 }
4395 else {
4396 UI.prototype.hideIndicator('uci-changes');
4397 }
4398 },
4399
4400 /**
4401 * Update the change count indicator.
4402 *
4403 * This function updates the UCI change count indicator from the given
4404 * UCI changeset structure.
4405 *
4406 * @instance
4407 * @memberof LuCI.ui.changes
4408 * @param {Object<string, Array<LuCI.uci.ChangeRecord>>} changes
4409 * The UCI changeset to count.
4410 */
4411 renderChangeIndicator: function(changes) {
4412 var n_changes = 0;
4413
4414 for (var config in changes)
4415 if (changes.hasOwnProperty(config))
4416 n_changes += changes[config].length;
4417
4418 this.changes = changes;
4419 this.setIndicator(n_changes);
4420 },
4421
4422 /** @private */
4423 changeTemplates: {
4424 'add-3': '<ins>uci add %0 <strong>%3</strong> # =%2</ins>',
4425 'set-3': '<ins>uci set %0.<strong>%2</strong>=%3</ins>',
4426 'set-4': '<var><ins>uci set %0.%2.%3=<strong>%4</strong></ins></var>',
4427 'remove-2': '<del>uci del %0.<strong>%2</strong></del>',
4428 'remove-3': '<var><del>uci del %0.%2.<strong>%3</strong></del></var>',
4429 'order-3': '<var>uci reorder %0.%2=<strong>%3</strong></var>',
4430 'list-add-4': '<var><ins>uci add_list %0.%2.%3=<strong>%4</strong></ins></var>',
4431 'list-del-4': '<var><del>uci del_list %0.%2.%3=<strong>%4</strong></del></var>',
4432 'rename-3': '<var>uci rename %0.%2=<strong>%3</strong></var>',
4433 'rename-4': '<var>uci rename %0.%2.%3=<strong>%4</strong></var>'
4434 },
4435
4436 /**
4437 * Display the current changelog.
4438 *
4439 * Open a modal dialog visualizing the currently staged UCI changes
4440 * and offer options to revert or apply the shown changes.
4441 *
4442 * @instance
4443 * @memberof LuCI.ui.changes
4444 */
4445 displayChanges: function() {
4446 var list = E('div', { 'class': 'uci-change-list' }),
4447 dlg = UI.prototype.showModal(_('Configuration') + ' / ' + _('Changes'), [
4448 E('div', { 'class': 'cbi-section' }, [
4449 E('strong', _('Legend:')),
4450 E('div', { 'class': 'uci-change-legend' }, [
4451 E('div', { 'class': 'uci-change-legend-label' }, [
4452 E('ins', '&#160;'), ' ', _('Section added') ]),
4453 E('div', { 'class': 'uci-change-legend-label' }, [
4454 E('del', '&#160;'), ' ', _('Section removed') ]),
4455 E('div', { 'class': 'uci-change-legend-label' }, [
4456 E('var', {}, E('ins', '&#160;')), ' ', _('Option changed') ]),
4457 E('div', { 'class': 'uci-change-legend-label' }, [
4458 E('var', {}, E('del', '&#160;')), ' ', _('Option removed') ])]),
4459 E('br'), list,
4460 E('div', { 'class': 'right' }, [
4461 E('button', {
4462 'class': 'btn',
4463 'click': UI.prototype.hideModal
4464 }, [ _('Close') ]), ' ',
4465 new UIComboButton('0', {
4466 0: [ _('Save & Apply') ],
4467 1: [ _('Apply unchecked') ]
4468 }, {
4469 classes: {
4470 0: 'btn cbi-button cbi-button-positive important',
4471 1: 'btn cbi-button cbi-button-negative important'
4472 },
4473 click: L.bind(function(ev, mode) { this.apply(mode == '0') }, this)
4474 }).render(), ' ',
4475 E('button', {
4476 'class': 'cbi-button cbi-button-reset',
4477 'click': L.bind(this.revert, this)
4478 }, [ _('Revert') ])])])
4479 ]);
4480
4481 for (var config in this.changes) {
4482 if (!this.changes.hasOwnProperty(config))
4483 continue;
4484
4485 list.appendChild(E('h5', '# /etc/config/%s'.format(config)));
4486
4487 for (var i = 0, added = null; i < this.changes[config].length; i++) {
4488 var chg = this.changes[config][i],
4489 tpl = this.changeTemplates['%s-%d'.format(chg[0], chg.length)];
4490
4491 list.appendChild(E(tpl.replace(/%([01234])/g, function(m0, m1) {
4492 switch (+m1) {
4493 case 0:
4494 return config;
4495
4496 case 2:
4497 if (added != null && chg[1] == added[0])
4498 return '@' + added[1] + '[-1]';
4499 else
4500 return chg[1];
4501
4502 case 4:
4503 return "'%h'".format(chg[3].replace(/'/g, "'\"'\"'"));
4504
4505 default:
4506 return chg[m1-1];
4507 }
4508 })));
4509
4510 if (chg[0] == 'add')
4511 added = [ chg[1], chg[2] ];
4512 }
4513 }
4514
4515 list.appendChild(E('br'));
4516 dlg.classList.add('uci-dialog');
4517 },
4518
4519 /** @private */
4520 displayStatus: function(type, content) {
4521 if (type) {
4522 var message = UI.prototype.showModal('', '');
4523
4524 message.classList.add('alert-message');
4525 DOMTokenList.prototype.add.apply(message.classList, type.split(/\s+/));
4526
4527 if (content)
4528 dom.content(message, content);
4529
4530 if (!this.was_polling) {
4531 this.was_polling = request.poll.active();
4532 request.poll.stop();
4533 }
4534 }
4535 else {
4536 UI.prototype.hideModal();
4537
4538 if (this.was_polling)
4539 request.poll.start();
4540 }
4541 },
4542
4543 /** @private */
4544 checkConnectivityAffected: function() {
4545 return L.resolveDefault(fs.exec_direct('/usr/libexec/luci-peeraddr', null, 'json')).then(L.bind(function(info) {
4546 if (L.isObject(info) && Array.isArray(info.inbound_interfaces)) {
4547 for (var i = 0; i < info.inbound_interfaces.length; i++) {
4548 var iif = info.inbound_interfaces[i];
4549
4550 for (var j = 0; this.changes && this.changes.network && j < this.changes.network.length; j++) {
4551 var chg = this.changes.network[j];
4552
4553 if (chg[0] == 'set' && chg[1] == iif &&
4554 ((chg[2] == 'disabled' && chg[3] == '1') || chg[2] == 'proto' || chg[2] == 'ipaddr' || chg[2] == 'netmask'))
4555 return iif;
4556 }
4557 }
4558 }
4559
4560 return null;
4561 }, this));
4562 },
4563
4564 /** @private */
4565 rollback: function(checked) {
4566 if (checked) {
4567 this.displayStatus('warning spinning',
4568 E('p', _('Failed to confirm apply within %ds, waiting for rollback…')
4569 .format(L.env.apply_rollback)));
4570
4571 var call = function(r, data, duration) {
4572 if (r.status === 204) {
4573 UI.prototype.changes.displayStatus('warning', [
4574 E('h4', _('Configuration changes have been rolled back!')),
4575 E('p', _('The device could not be reached within %d seconds after applying the pending changes, which caused the configuration to be rolled back for safety reasons. If you believe that the configuration changes are correct nonetheless, perform an unchecked configuration apply. Alternatively, you can dismiss this warning and edit changes before attempting to apply again, or revert all pending changes to keep the currently working configuration state.').format(L.env.apply_rollback)),
4576 E('div', { 'class': 'right' }, [
4577 E('button', {
4578 'class': 'btn',
4579 'click': L.bind(UI.prototype.changes.displayStatus, UI.prototype.changes, false)
4580 }, [ _('Dismiss') ]), ' ',
4581 E('button', {
4582 'class': 'btn cbi-button-action important',
4583 'click': L.bind(UI.prototype.changes.revert, UI.prototype.changes)
4584 }, [ _('Revert changes') ]), ' ',
4585 E('button', {
4586 'class': 'btn cbi-button-negative important',
4587 'click': L.bind(UI.prototype.changes.apply, UI.prototype.changes, false)
4588 }, [ _('Apply unchecked') ])
4589 ])
4590 ]);
4591
4592 return;
4593 }
4594
4595 var delay = isNaN(duration) ? 0 : Math.max(1000 - duration, 0);
4596 window.setTimeout(function() {
4597 request.request(L.url('admin/uci/confirm'), {
4598 method: 'post',
4599 timeout: L.env.apply_timeout * 1000,
4600 query: { sid: L.env.sessionid, token: L.env.token }
4601 }).then(call, call.bind(null, { status: 0 }, null, 0));
4602 }, delay);
4603 };
4604
4605 call({ status: 0 });
4606 }
4607 else {
4608 this.displayStatus('warning', [
4609 E('h4', _('Device unreachable!')),
4610 E('p', _('Could not regain access to the device after applying the configuration changes. You might need to reconnect if you modified network related settings such as the IP address or wireless security credentials.'))
4611 ]);
4612 }
4613 },
4614
4615 /** @private */
4616 confirm: function(checked, deadline, override_token) {
4617 var tt;
4618 var ts = Date.now();
4619
4620 this.displayStatus('notice');
4621
4622 if (override_token)
4623 this.confirm_auth = { token: override_token };
4624
4625 var call = function(r, data, duration) {
4626 if (Date.now() >= deadline) {
4627 window.clearTimeout(tt);
4628 UI.prototype.changes.rollback(checked);
4629 return;
4630 }
4631 else if (r && (r.status === 200 || r.status === 204)) {
4632 document.dispatchEvent(new CustomEvent('uci-applied'));
4633
4634 UI.prototype.changes.setIndicator(0);
4635 UI.prototype.changes.displayStatus('notice',
4636 E('p', _('Configuration changes applied.')));
4637
4638 window.clearTimeout(tt);
4639 window.setTimeout(function() {
4640 //UI.prototype.changes.displayStatus(false);
4641 window.location = window.location.href.split('#')[0];
4642 }, L.env.apply_display * 1000);
4643
4644 return;
4645 }
4646
4647 var delay = isNaN(duration) ? 0 : Math.max(1000 - duration, 0);
4648 window.setTimeout(function() {
4649 request.request(L.url('admin/uci/confirm'), {
4650 method: 'post',
4651 timeout: L.env.apply_timeout * 1000,
4652 query: UI.prototype.changes.confirm_auth
4653 }).then(call, call);
4654 }, delay);
4655 };
4656
4657 var tick = function() {
4658 var now = Date.now();
4659
4660 UI.prototype.changes.displayStatus('notice spinning',
4661 E('p', _('Applying configuration changes… %ds')
4662 .format(Math.max(Math.floor((deadline - Date.now()) / 1000), 0))));
4663
4664 if (now >= deadline)
4665 return;
4666
4667 tt = window.setTimeout(tick, 1000 - (now - ts));
4668 ts = now;
4669 };
4670
4671 tick();
4672
4673 /* wait a few seconds for the settings to become effective */
4674 window.setTimeout(call, Math.max(L.env.apply_holdoff * 1000 - ((ts + L.env.apply_rollback * 1000) - deadline), 1));
4675 },
4676
4677 /**
4678 * Apply the staged configuration changes.
4679 *
4680 * Start applying staged configuration changes and open a modal dialog
4681 * with a progress indication to prevent interaction with the view
4682 * during the apply process. The modal dialog will be automatically
4683 * closed and the current view reloaded once the apply process is
4684 * complete.
4685 *
4686 * @instance
4687 * @memberof LuCI.ui.changes
4688 * @param {boolean} [checked=false]
4689 * Whether to perform a checked (`true`) configuration apply or an
4690 * unchecked (`false`) one.
4691
4692 * In case of a checked apply, the configuration changes must be
4693 * confirmed within a specific time interval, otherwise the device
4694 * will begin to roll back the changes in order to restore the previous
4695 * settings.
4696 */
4697 apply: function(checked) {
4698 this.displayStatus('notice spinning',
4699 E('p', _('Starting configuration apply…')));
4700
4701 (new Promise(function(resolveFn, rejectFn) {
4702 if (!checked)
4703 return resolveFn(false);
4704
4705 UI.prototype.changes.checkConnectivityAffected().then(function(affected) {
4706 if (!affected)
4707 return resolveFn(true);
4708
4709 UI.prototype.changes.displayStatus('warning', [
4710 E('h4', _('Connectivity change')),
4711 E('p', _('The network access to this device could be interrupted by changing settings of the "%h" interface.').format(affected)),
4712 E('p', _('If the IP address used to access LuCI changes, a <strong>manual reconnect to the new IP</strong> is required within %d seconds to confirm the settings, otherwise modifications will be reverted.').format(L.env.apply_rollback)),
4713 E('div', { 'class': 'right' }, [
4714 E('button', {
4715 'class': 'btn',
4716 'click': rejectFn,
4717 }, [ _('Cancel') ]), ' ',
4718 E('button', {
4719 'class': 'btn cbi-button-action important',
4720 'click': resolveFn.bind(null, true)
4721 }, [ _('Apply with revert after connectivity loss') ]), ' ',
4722 E('button', {
4723 'class': 'btn cbi-button-negative important',
4724 'click': resolveFn.bind(null, false)
4725 }, [ _('Apply and keep settings') ])
4726 ])
4727 ]);
4728 });
4729 })).then(function(checked) {
4730 request.request(L.url('admin/uci', checked ? 'apply_rollback' : 'apply_unchecked'), {
4731 method: 'post',
4732 query: { sid: L.env.sessionid, token: L.env.token }
4733 }).then(function(r) {
4734 if (r.status === (checked ? 200 : 204)) {
4735 var tok = null; try { tok = r.json(); } catch(e) {}
4736 if (checked && tok !== null && typeof(tok) === 'object' && typeof(tok.token) === 'string')
4737 UI.prototype.changes.confirm_auth = tok;
4738
4739 UI.prototype.changes.confirm(checked, Date.now() + L.env.apply_rollback * 1000);
4740 }
4741 else if (checked && r.status === 204) {
4742 UI.prototype.changes.displayStatus('notice',
4743 E('p', _('There are no changes to apply')));
4744
4745 window.setTimeout(function() {
4746 UI.prototype.changes.displayStatus(false);
4747 }, L.env.apply_display * 1000);
4748 }
4749 else {
4750 UI.prototype.changes.displayStatus('warning',
4751 E('p', _('Apply request failed with status <code>%h</code>')
4752 .format(r.responseText || r.statusText || r.status)));
4753
4754 window.setTimeout(function() {
4755 UI.prototype.changes.displayStatus(false);
4756 }, L.env.apply_display * 1000);
4757 }
4758 });
4759 }, this.displayStatus.bind(this, false));
4760 },
4761
4762 /**
4763 * Revert the staged configuration changes.
4764 *
4765 * Start reverting staged configuration changes and open a modal dialog
4766 * with a progress indication to prevent interaction with the view
4767 * during the revert process. The modal dialog will be automatically
4768 * closed and the current view reloaded once the revert process is
4769 * complete.
4770 *
4771 * @instance
4772 * @memberof LuCI.ui.changes
4773 */
4774 revert: function() {
4775 this.displayStatus('notice spinning',
4776 E('p', _('Reverting configuration…')));
4777
4778 request.request(L.url('admin/uci/revert'), {
4779 method: 'post',
4780 query: { sid: L.env.sessionid, token: L.env.token }
4781 }).then(function(r) {
4782 if (r.status === 200) {
4783 document.dispatchEvent(new CustomEvent('uci-reverted'));
4784
4785 UI.prototype.changes.setIndicator(0);
4786 UI.prototype.changes.displayStatus('notice',
4787 E('p', _('Changes have been reverted.')));
4788
4789 window.setTimeout(function() {
4790 //UI.prototype.changes.displayStatus(false);
4791 window.location = window.location.href.split('#')[0];
4792 }, L.env.apply_display * 1000);
4793 }
4794 else {
4795 UI.prototype.changes.displayStatus('warning',
4796 E('p', _('Revert request failed with status <code>%h</code>')
4797 .format(r.statusText || r.status)));
4798
4799 window.setTimeout(function() {
4800 UI.prototype.changes.displayStatus(false);
4801 }, L.env.apply_display * 1000);
4802 }
4803 });
4804 }
4805 }),
4806
4807 /**
4808 * Add validation constraints to an input element.
4809 *
4810 * Compile the given type expression and optional validator function into
4811 * a validation function and bind it to the specified input element events.
4812 *
4813 * @param {Node} field
4814 * The DOM input element node to bind the validation constraints to.
4815 *
4816 * @param {string} type
4817 * The datatype specification to describe validation constraints.
4818 * Refer to the `LuCI.validation` class documentation for details.
4819 *
4820 * @param {boolean} [optional=false]
4821 * Specifies whether empty values are allowed (`true`) or not (`false`).
4822 * If an input element is not marked optional it must not be empty,
4823 * otherwise it will be marked as invalid.
4824 *
4825 * @param {function} [vfunc]
4826 * Specifies a custom validation function which is invoked after the
4827 * other validation constraints are applied. The validation must return
4828 * `true` to accept the passed value. Any other return type is converted
4829 * to a string and treated as validation error message.
4830 *
4831 * @param {...string} [events=blur, keyup]
4832 * The list of events to bind. Each received event will trigger a field
4833 * validation. If omitted, the `keyup` and `blur` events are bound by
4834 * default.
4835 *
4836 * @returns {function}
4837 * Returns the compiled validator function which can be used to manually
4838 * trigger field validation or to bind it to further events.
4839 *
4840 * @see LuCI.validation
4841 */
4842 addValidator: function(field, type, optional, vfunc /*, ... */) {
4843 if (type == null)
4844 return;
4845
4846 var events = this.varargs(arguments, 3);
4847 if (events.length == 0)
4848 events.push('blur', 'keyup');
4849
4850 try {
4851 var cbiValidator = validation.create(field, type, optional, vfunc),
4852 validatorFn = cbiValidator.validate.bind(cbiValidator);
4853
4854 for (var i = 0; i < events.length; i++)
4855 field.addEventListener(events[i], validatorFn);
4856
4857 validatorFn();
4858
4859 return validatorFn;
4860 }
4861 catch (e) { }
4862 },
4863
4864 /**
4865 * Create a pre-bound event handler function.
4866 *
4867 * Generate and bind a function suitable for use in event handlers. The
4868 * generated function automatically disables the event source element
4869 * and adds an active indication to it by adding appropriate CSS classes.
4870 *
4871 * It will also await any promises returned by the wrapped function and
4872 * re-enable the source element after the promises ran to completion.
4873 *
4874 * @param {*} ctx
4875 * The `this` context to use for the wrapped function.
4876 *
4877 * @param {function|string} fn
4878 * Specifies the function to wrap. In case of a function value, the
4879 * function is used as-is. If a string is specified instead, it is looked
4880 * up in `ctx` to obtain the function to wrap. In both cases the bound
4881 * function will be invoked with `ctx` as `this` context
4882 *
4883 * @param {...*} extra_args
4884 * Any further parameter as passed as-is to the bound event handler
4885 * function in the same order as passed to `createHandlerFn()`.
4886 *
4887 * @returns {function|null}
4888 * Returns the pre-bound handler function which is suitable to be passed
4889 * to `addEventListener()`. Returns `null` if the given `fn` argument is
4890 * a string which could not be found in `ctx` or if `ctx[fn]` is not a
4891 * valid function value.
4892 */
4893 createHandlerFn: function(ctx, fn /*, ... */) {
4894 if (typeof(fn) == 'string')
4895 fn = ctx[fn];
4896
4897 if (typeof(fn) != 'function')
4898 return null;
4899
4900 var arg_offset = arguments.length - 2;
4901
4902 return Function.prototype.bind.apply(function() {
4903 var t = arguments[arg_offset].currentTarget;
4904
4905 t.classList.add('spinning');
4906 t.disabled = true;
4907
4908 if (t.blur)
4909 t.blur();
4910
4911 Promise.resolve(fn.apply(ctx, arguments)).finally(function() {
4912 t.classList.remove('spinning');
4913 t.disabled = false;
4914 });
4915 }, this.varargs(arguments, 2, ctx));
4916 },
4917
4918 /**
4919 * Load specified view class path and set it up.
4920 *
4921 * Transforms the given view path into a class name, requires it
4922 * using [LuCI.require()]{@link LuCI#require} and asserts that the
4923 * resulting class instance is a descendant of
4924 * [LuCI.view]{@link LuCI.view}.
4925 *
4926 * By instantiating the view class, its corresponding contents are
4927 * rendered and included into the view area. Any runtime errors are
4928 * caught and rendered using [LuCI.error()]{@link LuCI#error}.
4929 *
4930 * @param {string} path
4931 * The view path to render.
4932 *
4933 * @returns {Promise<LuCI.view>}
4934 * Returns a promise resolving to the loaded view instance.
4935 */
4936 instantiateView: function(path) {
4937 var className = 'view.%s'.format(path.replace(/\//g, '.'));
4938
4939 return L.require(className).then(function(view) {
4940 if (!(view instanceof View))
4941 throw new TypeError('Loaded class %s is not a descendant of View'.format(className));
4942
4943 return view;
4944 }).catch(function(err) {
4945 dom.content(document.querySelector('#view'), null);
4946 L.error(err);
4947 });
4948 },
4949
4950 menu: UIMenu,
4951
4952 Table: UITable,
4953
4954 AbstractElement: UIElement,
4955
4956 /* Widgets */
4957 Textfield: UITextfield,
4958 Textarea: UITextarea,
4959 Checkbox: UICheckbox,
4960 Select: UISelect,
4961 Dropdown: UIDropdown,
4962 DynamicList: UIDynamicList,
4963 Combobox: UICombobox,
4964 ComboButton: UIComboButton,
4965 Hiddenfield: UIHiddenfield,
4966 FileUpload: UIFileUpload
4967 });
4968
4969 return UI;