Merge pull request #3024 from TDT-AG/pr/20190829-material-logo
[oweals/luci.git] / modules / luci-base / htdocs / luci-static / resources / cbi.js
1 /*
2         LuCI - Lua Configuration Interface
3
4         Copyright 2008 Steven Barth <steven@midlink.org>
5         Copyright 2008-2018 Jo-Philipp Wich <jo@mein.io>
6
7         Licensed under the Apache License, Version 2.0 (the "License");
8         you may not use this file except in compliance with the License.
9         You may obtain a copy of the License at
10
11         http://www.apache.org/licenses/LICENSE-2.0
12 */
13
14 var cbi_d = [];
15 var cbi_strings = { path: {}, label: {} };
16
17 function s8(bytes, off) {
18         var n = bytes[off];
19         return (n > 0x7F) ? (n - 256) >>> 0 : n;
20 }
21
22 function u16(bytes, off) {
23         return ((bytes[off + 1] << 8) + bytes[off]) >>> 0;
24 }
25
26 function sfh(s) {
27         if (s === null || s.length === 0)
28                 return null;
29
30         var bytes = [];
31
32         for (var i = 0; i < s.length; i++) {
33                 var ch = s.charCodeAt(i);
34
35                 if (ch <= 0x7F)
36                         bytes.push(ch);
37                 else if (ch <= 0x7FF)
38                         bytes.push(((ch >>>  6) & 0x1F) | 0xC0,
39                                    ( ch         & 0x3F) | 0x80);
40                 else if (ch <= 0xFFFF)
41                         bytes.push(((ch >>> 12) & 0x0F) | 0xE0,
42                                    ((ch >>>  6) & 0x3F) | 0x80,
43                                    ( ch         & 0x3F) | 0x80);
44                 else if (code <= 0x10FFFF)
45                         bytes.push(((ch >>> 18) & 0x07) | 0xF0,
46                                    ((ch >>> 12) & 0x3F) | 0x80,
47                                    ((ch >>   6) & 0x3F) | 0x80,
48                                    ( ch         & 0x3F) | 0x80);
49         }
50
51         if (!bytes.length)
52                 return null;
53
54         var hash = (bytes.length >>> 0),
55             len = (bytes.length >>> 2),
56             off = 0, tmp;
57
58         while (len--) {
59                 hash += u16(bytes, off);
60                 tmp   = ((u16(bytes, off + 2) << 11) ^ hash) >>> 0;
61                 hash  = ((hash << 16) ^ tmp) >>> 0;
62                 hash += hash >>> 11;
63                 off  += 4;
64         }
65
66         switch ((bytes.length & 3) >>> 0) {
67         case 3:
68                 hash += u16(bytes, off);
69                 hash  = (hash ^ (hash << 16)) >>> 0;
70                 hash  = (hash ^ (s8(bytes, off + 2) << 18)) >>> 0;
71                 hash += hash >>> 11;
72                 break;
73
74         case 2:
75                 hash += u16(bytes, off);
76                 hash  = (hash ^ (hash << 11)) >>> 0;
77                 hash += hash >>> 17;
78                 break;
79
80         case 1:
81                 hash += s8(bytes, off);
82                 hash  = (hash ^ (hash << 10)) >>> 0;
83                 hash += hash >>> 1;
84                 break;
85         }
86
87         hash  = (hash ^ (hash << 3)) >>> 0;
88         hash += hash >>> 5;
89         hash  = (hash ^ (hash << 4)) >>> 0;
90         hash += hash >>> 17;
91         hash  = (hash ^ (hash << 25)) >>> 0;
92         hash += hash >>> 6;
93
94         return (0x100000000 + hash).toString(16).substr(1);
95 }
96
97 function _(s) {
98         return (window.TR && TR[sfh(s)]) || s;
99 }
100
101
102 function cbi_d_add(field, dep, index) {
103         var obj = (typeof(field) === 'string') ? document.getElementById(field) : field;
104         if (obj) {
105                 var entry
106                 for (var i=0; i<cbi_d.length; i++) {
107                         if (cbi_d[i].id == obj.id) {
108                                 entry = cbi_d[i];
109                                 break;
110                         }
111                 }
112                 if (!entry) {
113                         entry = {
114                                 "node": obj,
115                                 "id": obj.id,
116                                 "parent": obj.parentNode.id,
117                                 "deps": [],
118                                 "index": index
119                         };
120                         cbi_d.unshift(entry);
121                 }
122                 entry.deps.push(dep)
123         }
124 }
125
126 function cbi_d_checkvalue(target, ref) {
127         var value = null,
128             query = 'input[id="'+target+'"], input[name="'+target+'"], ' +
129                     'select[id="'+target+'"], select[name="'+target+'"]';
130
131         document.querySelectorAll(query).forEach(function(i) {
132                 if (value === null && ((i.type !== 'radio' && i.type !== 'checkbox') || i.checked === true))
133                         value = i.value;
134         });
135
136         return (((value !== null) ? value : "") == ref);
137 }
138
139 function cbi_d_check(deps) {
140         var reverse;
141         var def = false;
142         for (var i=0; i<deps.length; i++) {
143                 var istat = true;
144                 reverse = false;
145                 for (var j in deps[i]) {
146                         if (j == "!reverse") {
147                                 reverse = true;
148                         } else if (j == "!default") {
149                                 def = true;
150                                 istat = false;
151                         } else {
152                                 istat = (istat && cbi_d_checkvalue(j, deps[i][j]))
153                         }
154                 }
155
156                 if (istat ^ reverse) {
157                         return true;
158                 }
159         }
160         return def;
161 }
162
163 function cbi_d_update() {
164         var state = false;
165         for (var i=0; i<cbi_d.length; i++) {
166                 var entry = cbi_d[i];
167                 var node  = document.getElementById(entry.id);
168                 var parent = document.getElementById(entry.parent);
169
170                 if (node && node.parentNode && !cbi_d_check(entry.deps)) {
171                         node.parentNode.removeChild(node);
172                         state = true;
173                 }
174                 else if (parent && (!node || !node.parentNode) && cbi_d_check(entry.deps)) {
175                         var next = undefined;
176
177                         for (next = parent.firstChild; next; next = next.nextSibling) {
178                                 if (next.getAttribute && parseInt(next.getAttribute('data-index'), 10) > entry.index)
179                                         break;
180                         }
181
182                         if (!next)
183                                 parent.appendChild(entry.node);
184                         else
185                                 parent.insertBefore(entry.node, next);
186
187                         state = true;
188                 }
189
190                 // hide optionals widget if no choices remaining
191                 if (parent && parent.parentNode && parent.getAttribute('data-optionals'))
192                         parent.parentNode.style.display = (parent.options.length <= 1) ? 'none' : '';
193         }
194
195         if (entry && entry.parent)
196                 cbi_tag_last(parent);
197
198         if (state)
199                 cbi_d_update();
200         else if (parent)
201                 parent.dispatchEvent(new CustomEvent('dependency-update', { bubbles: true }));
202 }
203
204 function cbi_init() {
205         var nodes;
206
207         document.querySelectorAll('.cbi-dropdown').forEach(function(node) {
208                 cbi_dropdown_init(node);
209                 node.addEventListener('cbi-dropdown-change', cbi_d_update);
210         });
211
212         nodes = document.querySelectorAll('[data-strings]');
213
214         for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
215                 var str = JSON.parse(node.getAttribute('data-strings'));
216                 for (var key in str) {
217                         for (var key2 in str[key]) {
218                                 var dst = cbi_strings[key] || (cbi_strings[key] = { });
219                                     dst[key2] = str[key][key2];
220                         }
221                 }
222         }
223
224         nodes = document.querySelectorAll('[data-depends]');
225
226         for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
227                 var index = parseInt(node.getAttribute('data-index'), 10);
228                 var depends = JSON.parse(node.getAttribute('data-depends'));
229                 if (!isNaN(index) && depends.length > 0) {
230                         for (var alt = 0; alt < depends.length; alt++)
231                                 cbi_d_add(node, depends[alt], index);
232                 }
233         }
234
235         nodes = document.querySelectorAll('[data-update]');
236
237         for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
238                 var events = node.getAttribute('data-update').split(' ');
239                 for (var j = 0, event; (event = events[j]) !== undefined; j++)
240                         node.addEventListener(event, cbi_d_update);
241         }
242
243         nodes = document.querySelectorAll('[data-choices]');
244
245         for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
246                 var choices = JSON.parse(node.getAttribute('data-choices')),
247                     options = {};
248
249                 for (var j = 0; j < choices[0].length; j++)
250                         options[choices[0][j]] = choices[1][j];
251
252                 var def = (node.getAttribute('data-optional') === 'true')
253                         ? node.placeholder || '' : null;
254
255                 var cb = new L.ui.Combobox(node.value, options, {
256                         name: node.getAttribute('name'),
257                         sort: choices[0],
258                         select_placeholder: def || _('-- Please choose --'),
259                         custom_placeholder: node.getAttribute('data-manual') || _('-- custom --')
260                 });
261
262                 var n = cb.render();
263                 n.addEventListener('cbi-dropdown-change', cbi_d_update);
264                 node.parentNode.replaceChild(n, node);
265         }
266
267         nodes = document.querySelectorAll('[data-dynlist]');
268
269         for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
270                 var choices = JSON.parse(node.getAttribute('data-dynlist')),
271                     values = JSON.parse(node.getAttribute('data-values') || '[]'),
272                     options = null;
273
274                 if (choices[0] && choices[0].length) {
275                         options = {};
276
277                         for (var j = 0; j < choices[0].length; j++)
278                                 options[choices[0][j]] = choices[1][j];
279                 }
280
281                 var dl = new L.ui.DynamicList(values, options, {
282                         name: node.getAttribute('data-prefix'),
283                         sort: choices[0],
284                         datatype: choices[2],
285                         optional: choices[3],
286                         placeholder: node.getAttribute('data-placeholder')
287                 });
288
289                 var n = dl.render();
290                 n.addEventListener('cbi-dynlist-change', cbi_d_update);
291                 node.parentNode.replaceChild(n, node);
292         }
293
294         nodes = document.querySelectorAll('[data-type]');
295
296         for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
297                 cbi_validate_field(node, node.getAttribute('data-optional') === 'true',
298                                    node.getAttribute('data-type'));
299         }
300
301         document.querySelectorAll('.cbi-tooltip:not(:empty)').forEach(function(s) {
302                 s.parentNode.classList.add('cbi-tooltip-container');
303         });
304
305         document.querySelectorAll('.cbi-section-remove > input[name^="cbi.rts"]').forEach(function(i) {
306                 var handler = function(ev) {
307                         var bits = this.name.split(/\./),
308                             section = document.getElementById('cbi-' + bits[2] + '-' + bits[3]);
309
310                     section.style.opacity = (ev.type === 'mouseover') ? 0.5 : '';
311                 };
312
313                 i.addEventListener('mouseover', handler);
314                 i.addEventListener('mouseout', handler);
315         });
316
317         document.querySelectorAll('[data-ui-widget]').forEach(function(node) {
318                 var args = JSON.parse(node.getAttribute('data-ui-widget') || '[]'),
319                     widget = new (Function.prototype.bind.apply(L.ui[args[0]], args)),
320                     markup = widget.render();
321
322                 Promise.resolve(markup).then(function(markup) {
323                         markup.addEventListener('widget-change', cbi_d_update);
324                         node.parentNode.replaceChild(markup, node);
325                 });
326         });
327
328         cbi_d_update();
329 }
330
331 function cbi_validate_form(form, errmsg)
332 {
333         /* if triggered by a section removal or addition, don't validate */
334         if (form.cbi_state == 'add-section' || form.cbi_state == 'del-section')
335                 return true;
336
337         if (form.cbi_validators) {
338                 for (var i = 0; i < form.cbi_validators.length; i++) {
339                         var validator = form.cbi_validators[i];
340
341                         if (!validator() && errmsg) {
342                                 alert(errmsg);
343                                 return false;
344                         }
345                 }
346         }
347
348         return true;
349 }
350
351 function cbi_validate_reset(form)
352 {
353         window.setTimeout(
354                 function() { cbi_validate_form(form, null) }, 100
355         );
356
357         return true;
358 }
359
360 function cbi_validate_field(cbid, optional, type)
361 {
362         var field = isElem(cbid) ? cbid : document.getElementById(cbid);
363         var validatorFn;
364
365         try {
366                 var cbiValidator = L.validation.create(field, type, optional);
367                 validatorFn = cbiValidator.validate.bind(cbiValidator);
368         }
369         catch(e) {
370                 validatorFn = null;
371         };
372
373         if (validatorFn !== null) {
374                 var form = findParent(field, 'form');
375
376                 if (!form.cbi_validators)
377                         form.cbi_validators = [ ];
378
379                 form.cbi_validators.push(validatorFn);
380
381                 field.addEventListener("blur",  validatorFn);
382                 field.addEventListener("keyup", validatorFn);
383                 field.addEventListener("cbi-dropdown-change", validatorFn);
384
385                 if (matchesElem(field, 'select')) {
386                         field.addEventListener("change", validatorFn);
387                         field.addEventListener("click",  validatorFn);
388                 }
389
390                 validatorFn();
391         }
392 }
393
394 function cbi_row_swap(elem, up, store)
395 {
396         var tr = findParent(elem.parentNode, '.cbi-section-table-row');
397
398         if (!tr)
399                 return false;
400
401         tr.classList.remove('flash');
402
403         if (up) {
404                 var prev = tr.previousElementSibling;
405
406                 if (prev && prev.classList.contains('cbi-section-table-row'))
407                         tr.parentNode.insertBefore(tr, prev);
408                 else
409                         return;
410         }
411         else {
412                 var next = tr.nextElementSibling ? tr.nextElementSibling.nextElementSibling : null;
413
414                 if (next && next.classList.contains('cbi-section-table-row'))
415                         tr.parentNode.insertBefore(tr, next);
416                 else if (!next)
417                         tr.parentNode.appendChild(tr);
418                 else
419                         return;
420         }
421
422         var ids = [ ];
423
424         for (var i = 0, n = 0; i < tr.parentNode.childNodes.length; i++) {
425                 var node = tr.parentNode.childNodes[i];
426                 if (node.classList && node.classList.contains('cbi-section-table-row')) {
427                         node.classList.remove('cbi-rowstyle-1');
428                         node.classList.remove('cbi-rowstyle-2');
429                         node.classList.add((n++ % 2) ? 'cbi-rowstyle-2' : 'cbi-rowstyle-1');
430
431                         if (/-([^\-]+)$/.test(node.id))
432                                 ids.push(RegExp.$1);
433                 }
434         }
435
436         var input = document.getElementById(store);
437         if (input)
438                 input.value = ids.join(' ');
439
440         window.scrollTo(0, tr.offsetTop);
441         void tr.offsetWidth;
442         tr.classList.add('flash');
443
444         return false;
445 }
446
447 function cbi_tag_last(container)
448 {
449         var last;
450
451         for (var i = 0; i < container.childNodes.length; i++) {
452                 var c = container.childNodes[i];
453                 if (matchesElem(c, 'div')) {
454                         c.classList.remove('cbi-value-last');
455                         last = c;
456                 }
457         }
458
459         if (last)
460                 last.classList.add('cbi-value-last');
461 }
462
463 function cbi_submit(elem, name, value, action)
464 {
465         var form = elem.form || findParent(elem, 'form');
466
467         if (!form)
468                 return false;
469
470         if (action)
471                 form.action = action;
472
473         if (name) {
474                 var hidden = form.querySelector('input[type="hidden"][name="%s"]'.format(name)) ||
475                         E('input', { type: 'hidden', name: name });
476
477                 hidden.value = value || '1';
478                 form.appendChild(hidden);
479         }
480
481         form.submit();
482         return true;
483 }
484
485 String.prototype.format = function()
486 {
487         if (!RegExp)
488                 return;
489
490         var html_esc = [/&/g, '&#38;', /"/g, '&#34;', /'/g, '&#39;', /</g, '&#60;', />/g, '&#62;'];
491         var quot_esc = [/"/g, '&#34;', /'/g, '&#39;'];
492
493         function esc(s, r) {
494                 if (typeof(s) !== 'string' && !(s instanceof String))
495                         return '';
496
497                 for (var i = 0; i < r.length; i += 2)
498                         s = s.replace(r[i], r[i+1]);
499
500                 return s;
501         }
502
503         var str = this;
504         var out = '';
505         var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X|q|h|j|t|m))/;
506         var a = b = [], numSubstitutions = 0, numMatches = 0;
507
508         while (a = re.exec(str)) {
509                 var m = a[1];
510                 var leftpart = a[2], pPad = a[3], pJustify = a[4], pMinLength = a[5];
511                 var pPrecision = a[6], pType = a[7];
512
513                 numMatches++;
514
515                 if (pType == '%') {
516                         subst = '%';
517                 }
518                 else {
519                         if (numSubstitutions < arguments.length) {
520                                 var param = arguments[numSubstitutions++];
521
522                                 var pad = '';
523                                 if (pPad && pPad.substr(0,1) == "'")
524                                         pad = leftpart.substr(1,1);
525                                 else if (pPad)
526                                         pad = pPad;
527                                 else
528                                         pad = ' ';
529
530                                 var justifyRight = true;
531                                 if (pJustify && pJustify === "-")
532                                         justifyRight = false;
533
534                                 var minLength = -1;
535                                 if (pMinLength)
536                                         minLength = +pMinLength;
537
538                                 var precision = -1;
539                                 if (pPrecision && pType == 'f')
540                                         precision = +pPrecision.substring(1);
541
542                                 var subst = param;
543
544                                 switch(pType) {
545                                         case 'b':
546                                                 subst = Math.floor(+param || 0).toString(2);
547                                                 break;
548
549                                         case 'c':
550                                                 subst = String.fromCharCode(+param || 0);
551                                                 break;
552
553                                         case 'd':
554                                                 subst = Math.floor(+param || 0).toFixed(0);
555                                                 break;
556
557                                         case 'u':
558                                                 var n = +param || 0;
559                                                 subst = Math.floor((n < 0) ? 0x100000000 + n : n).toFixed(0);
560                                                 break;
561
562                                         case 'f':
563                                                 subst = (precision > -1)
564                                                         ? ((+param || 0.0)).toFixed(precision)
565                                                         : (+param || 0.0);
566                                                 break;
567
568                                         case 'o':
569                                                 subst = Math.floor(+param || 0).toString(8);
570                                                 break;
571
572                                         case 's':
573                                                 subst = param;
574                                                 break;
575
576                                         case 'x':
577                                                 subst = Math.floor(+param || 0).toString(16).toLowerCase();
578                                                 break;
579
580                                         case 'X':
581                                                 subst = Math.floor(+param || 0).toString(16).toUpperCase();
582                                                 break;
583
584                                         case 'h':
585                                                 subst = esc(param, html_esc);
586                                                 break;
587
588                                         case 'q':
589                                                 subst = esc(param, quot_esc);
590                                                 break;
591
592                                         case 't':
593                                                 var td = 0;
594                                                 var th = 0;
595                                                 var tm = 0;
596                                                 var ts = (param || 0);
597
598                                                 if (ts > 60) {
599                                                         tm = Math.floor(ts / 60);
600                                                         ts = (ts % 60);
601                                                 }
602
603                                                 if (tm > 60) {
604                                                         th = Math.floor(tm / 60);
605                                                         tm = (tm % 60);
606                                                 }
607
608                                                 if (th > 24) {
609                                                         td = Math.floor(th / 24);
610                                                         th = (th % 24);
611                                                 }
612
613                                                 subst = (td > 0)
614                                                         ? String.format('%dd %dh %dm %ds', td, th, tm, ts)
615                                                         : String.format('%dh %dm %ds', th, tm, ts);
616
617                                                 break;
618
619                                         case 'm':
620                                                 var mf = pMinLength ? +pMinLength : 1000;
621                                                 var pr = pPrecision ? ~~(10 * +('0' + pPrecision)) : 2;
622
623                                                 var i = 0;
624                                                 var val = (+param || 0);
625                                                 var units = [ ' ', ' K', ' M', ' G', ' T', ' P', ' E' ];
626
627                                                 for (i = 0; (i < units.length) && (val > mf); i++)
628                                                         val /= mf;
629
630                                                 subst = (i ? val.toFixed(pr) : val) + units[i];
631                                                 pMinLength = null;
632                                                 break;
633                                 }
634                         }
635                 }
636
637                 if (pMinLength) {
638                         subst = subst.toString();
639                         for (var i = subst.length; i < pMinLength; i++)
640                                 if (pJustify == '-')
641                                         subst = subst + ' ';
642                                 else
643                                         subst = pad + subst;
644                 }
645
646                 out += leftpart + subst;
647                 str = str.substr(m.length);
648         }
649
650         return out + str;
651 }
652
653 String.prototype.nobr = function()
654 {
655         return this.replace(/[\s\n]+/g, '&#160;');
656 }
657
658 String.format = function()
659 {
660         var a = [ ];
661
662         for (var i = 1; i < arguments.length; i++)
663                 a.push(arguments[i]);
664
665         return ''.format.apply(arguments[0], a);
666 }
667
668 String.nobr = function()
669 {
670         var a = [ ];
671
672         for (var i = 1; i < arguments.length; i++)
673                 a.push(arguments[i]);
674
675         return ''.nobr.apply(arguments[0], a);
676 }
677
678 if (window.NodeList && !NodeList.prototype.forEach) {
679         NodeList.prototype.forEach = function (callback, thisArg) {
680                 thisArg = thisArg || window;
681                 for (var i = 0; i < this.length; i++) {
682                         callback.call(thisArg, this[i], i, this);
683                 }
684         };
685 }
686
687 if (!window.requestAnimationFrame) {
688         window.requestAnimationFrame = function(f) {
689                 window.setTimeout(function() {
690                         f(new Date().getTime())
691                 }, 1000/30);
692         };
693 }
694
695
696 function isElem(e) { return L.dom.elem(e) }
697 function toElem(s) { return L.dom.parse(s) }
698 function matchesElem(node, selector) { return L.dom.matches(node, selector) }
699 function findParent(node, selector) { return L.dom.parent(node, selector) }
700 function E() { return L.dom.create.apply(L.dom, arguments) }
701
702 if (typeof(window.CustomEvent) !== 'function') {
703         function CustomEvent(event, params) {
704                 params = params || { bubbles: false, cancelable: false, detail: undefined };
705                 var evt = document.createEvent('CustomEvent');
706                     evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
707                 return evt;
708         }
709
710         CustomEvent.prototype = window.Event.prototype;
711         window.CustomEvent = CustomEvent;
712 }
713
714 function cbi_dropdown_init(sb) {
715         var dl = new L.ui.Dropdown(sb, null, { name: sb.getAttribute('name') });
716         return dl.bind(sb);
717 }
718
719 function cbi_update_table(table, data, placeholder) {
720         var target = isElem(table) ? table : document.querySelector(table);
721
722         if (!isElem(target))
723                 return;
724
725         target.querySelectorAll('.tr.table-titles, .cbi-section-table-titles').forEach(function(thead) {
726                 var titles = [];
727
728                 thead.querySelectorAll('.th').forEach(function(th) {
729                         titles.push(th);
730                 });
731
732                 if (Array.isArray(data)) {
733                         var n = 0, rows = target.querySelectorAll('.tr');
734
735                         data.forEach(function(row) {
736                                 var trow = E('div', { 'class': 'tr' });
737
738                                 for (var i = 0; i < titles.length; i++) {
739                                         var text = (titles[i].innerText || '').trim();
740                                         var td = trow.appendChild(E('div', {
741                                                 'class': titles[i].className,
742                                                 'data-title': (text !== '') ? text : null
743                                         }, row[i] || ''));
744
745                                         td.classList.remove('th');
746                                         td.classList.add('td');
747                                 }
748
749                                 trow.classList.add('cbi-rowstyle-%d'.format((n++ % 2) ? 2 : 1));
750
751                                 if (rows[n])
752                                         target.replaceChild(trow, rows[n]);
753                                 else
754                                         target.appendChild(trow);
755                         });
756
757                         while (rows[++n])
758                                 target.removeChild(rows[n]);
759
760                         if (placeholder && target.firstElementChild === target.lastElementChild) {
761                                 var trow = target.appendChild(E('div', { 'class': 'tr placeholder' }));
762                                 var td = trow.appendChild(E('div', { 'class': titles[0].className }, placeholder));
763
764                                 td.classList.remove('th');
765                                 td.classList.add('td');
766                         }
767                 }
768                 else {
769                         thead.parentNode.style.display = 'none';
770
771                         thead.parentNode.querySelectorAll('.tr, .cbi-section-table-row').forEach(function(trow) {
772                                 if (trow !== thead) {
773                                         var n = 0;
774                                         trow.querySelectorAll('.th, .td').forEach(function(td) {
775                                                 if (n < titles.length) {
776                                                         var text = (titles[n++].innerText || '').trim();
777                                                         if (text !== '')
778                                                                 td.setAttribute('data-title', text);
779                                                 }
780                                         });
781                                 }
782                         });
783
784                         thead.parentNode.style.display = '';
785                 }
786         });
787 }
788
789 function showModal(title, children)
790 {
791         return L.showModal(title, children);
792 }
793
794 function hideModal()
795 {
796         return L.hideModal();
797 }
798
799
800 document.addEventListener('DOMContentLoaded', function() {
801         document.addEventListener('validation-failure', function(ev) {
802                 if (ev.target === document.activeElement)
803                         L.showTooltip(ev);
804         });
805
806         document.addEventListener('validation-success', function(ev) {
807                 if (ev.target === document.activeElement)
808                         L.hideTooltip(ev);
809         });
810
811         document.querySelectorAll('.table').forEach(cbi_update_table);
812 });