Merge pull request #1735 from sumpfralle/olsr-jsoninfo-parser-handle-empty-result
[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         var tasks = [];
318
319         document.querySelectorAll('[data-ui-widget]').forEach(function(node) {
320                 var args = JSON.parse(node.getAttribute('data-ui-widget') || '[]'),
321                     widget = new (Function.prototype.bind.apply(L.ui[args[0]], args)),
322                     markup = widget.render();
323
324                 tasks.push(Promise.resolve(markup).then(function(markup) {
325                         markup.addEventListener('widget-change', cbi_d_update);
326                         node.parentNode.replaceChild(markup, node);
327                 }));
328         });
329
330         Promise.all(tasks).then(cbi_d_update);
331 }
332
333 function cbi_validate_form(form, errmsg)
334 {
335         /* if triggered by a section removal or addition, don't validate */
336         if (form.cbi_state == 'add-section' || form.cbi_state == 'del-section')
337                 return true;
338
339         if (form.cbi_validators) {
340                 for (var i = 0; i < form.cbi_validators.length; i++) {
341                         var validator = form.cbi_validators[i];
342
343                         if (!validator() && errmsg) {
344                                 alert(errmsg);
345                                 return false;
346                         }
347                 }
348         }
349
350         return true;
351 }
352
353 function cbi_validate_reset(form)
354 {
355         window.setTimeout(
356                 function() { cbi_validate_form(form, null) }, 100
357         );
358
359         return true;
360 }
361
362 function cbi_validate_field(cbid, optional, type)
363 {
364         var field = isElem(cbid) ? cbid : document.getElementById(cbid);
365         var validatorFn;
366
367         try {
368                 var cbiValidator = L.validation.create(field, type, optional);
369                 validatorFn = cbiValidator.validate.bind(cbiValidator);
370         }
371         catch(e) {
372                 validatorFn = null;
373         };
374
375         if (validatorFn !== null) {
376                 var form = findParent(field, 'form');
377
378                 if (!form.cbi_validators)
379                         form.cbi_validators = [ ];
380
381                 form.cbi_validators.push(validatorFn);
382
383                 field.addEventListener("blur",  validatorFn);
384                 field.addEventListener("keyup", validatorFn);
385                 field.addEventListener("cbi-dropdown-change", validatorFn);
386
387                 if (matchesElem(field, 'select')) {
388                         field.addEventListener("change", validatorFn);
389                         field.addEventListener("click",  validatorFn);
390                 }
391
392                 validatorFn();
393         }
394 }
395
396 function cbi_row_swap(elem, up, store)
397 {
398         var tr = findParent(elem.parentNode, '.cbi-section-table-row');
399
400         if (!tr)
401                 return false;
402
403         tr.classList.remove('flash');
404
405         if (up) {
406                 var prev = tr.previousElementSibling;
407
408                 if (prev && prev.classList.contains('cbi-section-table-row'))
409                         tr.parentNode.insertBefore(tr, prev);
410                 else
411                         return;
412         }
413         else {
414                 var next = tr.nextElementSibling ? tr.nextElementSibling.nextElementSibling : null;
415
416                 if (next && next.classList.contains('cbi-section-table-row'))
417                         tr.parentNode.insertBefore(tr, next);
418                 else if (!next)
419                         tr.parentNode.appendChild(tr);
420                 else
421                         return;
422         }
423
424         var ids = [ ];
425
426         for (var i = 0, n = 0; i < tr.parentNode.childNodes.length; i++) {
427                 var node = tr.parentNode.childNodes[i];
428                 if (node.classList && node.classList.contains('cbi-section-table-row')) {
429                         node.classList.remove('cbi-rowstyle-1');
430                         node.classList.remove('cbi-rowstyle-2');
431                         node.classList.add((n++ % 2) ? 'cbi-rowstyle-2' : 'cbi-rowstyle-1');
432
433                         if (/-([^\-]+)$/.test(node.id))
434                                 ids.push(RegExp.$1);
435                 }
436         }
437
438         var input = document.getElementById(store);
439         if (input)
440                 input.value = ids.join(' ');
441
442         window.scrollTo(0, tr.offsetTop);
443         void tr.offsetWidth;
444         tr.classList.add('flash');
445
446         return false;
447 }
448
449 function cbi_tag_last(container)
450 {
451         var last;
452
453         for (var i = 0; i < container.childNodes.length; i++) {
454                 var c = container.childNodes[i];
455                 if (matchesElem(c, 'div')) {
456                         c.classList.remove('cbi-value-last');
457                         last = c;
458                 }
459         }
460
461         if (last)
462                 last.classList.add('cbi-value-last');
463 }
464
465 function cbi_submit(elem, name, value, action)
466 {
467         var form = elem.form || findParent(elem, 'form');
468
469         if (!form)
470                 return false;
471
472         if (action)
473                 form.action = action;
474
475         if (name) {
476                 var hidden = form.querySelector('input[type="hidden"][name="%s"]'.format(name)) ||
477                         E('input', { type: 'hidden', name: name });
478
479                 hidden.value = value || '1';
480                 form.appendChild(hidden);
481         }
482
483         form.submit();
484         return true;
485 }
486
487 String.prototype.format = function()
488 {
489         if (!RegExp)
490                 return;
491
492         var html_esc = [/&/g, '&#38;', /"/g, '&#34;', /'/g, '&#39;', /</g, '&#60;', />/g, '&#62;'];
493         var quot_esc = [/"/g, '&#34;', /'/g, '&#39;'];
494
495         function esc(s, r) {
496                 if (typeof(s) !== 'string' && !(s instanceof String))
497                         return '';
498
499                 for (var i = 0; i < r.length; i += 2)
500                         s = s.replace(r[i], r[i+1]);
501
502                 return s;
503         }
504
505         var str = this;
506         var out = '';
507         var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X|q|h|j|t|m))/;
508         var a = b = [], numSubstitutions = 0, numMatches = 0;
509
510         while (a = re.exec(str)) {
511                 var m = a[1];
512                 var leftpart = a[2], pPad = a[3], pJustify = a[4], pMinLength = a[5];
513                 var pPrecision = a[6], pType = a[7];
514
515                 numMatches++;
516
517                 if (pType == '%') {
518                         subst = '%';
519                 }
520                 else {
521                         if (numSubstitutions < arguments.length) {
522                                 var param = arguments[numSubstitutions++];
523
524                                 var pad = '';
525                                 if (pPad && pPad.substr(0,1) == "'")
526                                         pad = leftpart.substr(1,1);
527                                 else if (pPad)
528                                         pad = pPad;
529                                 else
530                                         pad = ' ';
531
532                                 var justifyRight = true;
533                                 if (pJustify && pJustify === "-")
534                                         justifyRight = false;
535
536                                 var minLength = -1;
537                                 if (pMinLength)
538                                         minLength = +pMinLength;
539
540                                 var precision = -1;
541                                 if (pPrecision && pType == 'f')
542                                         precision = +pPrecision.substring(1);
543
544                                 var subst = param;
545
546                                 switch(pType) {
547                                         case 'b':
548                                                 subst = Math.floor(+param || 0).toString(2);
549                                                 break;
550
551                                         case 'c':
552                                                 subst = String.fromCharCode(+param || 0);
553                                                 break;
554
555                                         case 'd':
556                                                 subst = Math.floor(+param || 0).toFixed(0);
557                                                 break;
558
559                                         case 'u':
560                                                 var n = +param || 0;
561                                                 subst = Math.floor((n < 0) ? 0x100000000 + n : n).toFixed(0);
562                                                 break;
563
564                                         case 'f':
565                                                 subst = (precision > -1)
566                                                         ? ((+param || 0.0)).toFixed(precision)
567                                                         : (+param || 0.0);
568                                                 break;
569
570                                         case 'o':
571                                                 subst = Math.floor(+param || 0).toString(8);
572                                                 break;
573
574                                         case 's':
575                                                 subst = param;
576                                                 break;
577
578                                         case 'x':
579                                                 subst = Math.floor(+param || 0).toString(16).toLowerCase();
580                                                 break;
581
582                                         case 'X':
583                                                 subst = Math.floor(+param || 0).toString(16).toUpperCase();
584                                                 break;
585
586                                         case 'h':
587                                                 subst = esc(param, html_esc);
588                                                 break;
589
590                                         case 'q':
591                                                 subst = esc(param, quot_esc);
592                                                 break;
593
594                                         case 't':
595                                                 var td = 0;
596                                                 var th = 0;
597                                                 var tm = 0;
598                                                 var ts = (param || 0);
599
600                                                 if (ts > 60) {
601                                                         tm = Math.floor(ts / 60);
602                                                         ts = (ts % 60);
603                                                 }
604
605                                                 if (tm > 60) {
606                                                         th = Math.floor(tm / 60);
607                                                         tm = (tm % 60);
608                                                 }
609
610                                                 if (th > 24) {
611                                                         td = Math.floor(th / 24);
612                                                         th = (th % 24);
613                                                 }
614
615                                                 subst = (td > 0)
616                                                         ? String.format('%dd %dh %dm %ds', td, th, tm, ts)
617                                                         : String.format('%dh %dm %ds', th, tm, ts);
618
619                                                 break;
620
621                                         case 'm':
622                                                 var mf = pMinLength ? +pMinLength : 1000;
623                                                 var pr = pPrecision ? ~~(10 * +('0' + pPrecision)) : 2;
624
625                                                 var i = 0;
626                                                 var val = (+param || 0);
627                                                 var units = [ ' ', ' K', ' M', ' G', ' T', ' P', ' E' ];
628
629                                                 for (i = 0; (i < units.length) && (val > mf); i++)
630                                                         val /= mf;
631
632                                                 subst = (i ? val.toFixed(pr) : val) + units[i];
633                                                 pMinLength = null;
634                                                 break;
635                                 }
636                         }
637                 }
638
639                 if (pMinLength) {
640                         subst = subst.toString();
641                         for (var i = subst.length; i < pMinLength; i++)
642                                 if (pJustify == '-')
643                                         subst = subst + ' ';
644                                 else
645                                         subst = pad + subst;
646                 }
647
648                 out += leftpart + subst;
649                 str = str.substr(m.length);
650         }
651
652         return out + str;
653 }
654
655 String.prototype.nobr = function()
656 {
657         return this.replace(/[\s\n]+/g, '&#160;');
658 }
659
660 String.format = function()
661 {
662         var a = [ ];
663
664         for (var i = 1; i < arguments.length; i++)
665                 a.push(arguments[i]);
666
667         return ''.format.apply(arguments[0], a);
668 }
669
670 String.nobr = function()
671 {
672         var a = [ ];
673
674         for (var i = 1; i < arguments.length; i++)
675                 a.push(arguments[i]);
676
677         return ''.nobr.apply(arguments[0], a);
678 }
679
680 if (window.NodeList && !NodeList.prototype.forEach) {
681         NodeList.prototype.forEach = function (callback, thisArg) {
682                 thisArg = thisArg || window;
683                 for (var i = 0; i < this.length; i++) {
684                         callback.call(thisArg, this[i], i, this);
685                 }
686         };
687 }
688
689 if (!window.requestAnimationFrame) {
690         window.requestAnimationFrame = function(f) {
691                 window.setTimeout(function() {
692                         f(new Date().getTime())
693                 }, 1000/30);
694         };
695 }
696
697
698 function isElem(e) { return L.dom.elem(e) }
699 function toElem(s) { return L.dom.parse(s) }
700 function matchesElem(node, selector) { return L.dom.matches(node, selector) }
701 function findParent(node, selector) { return L.dom.parent(node, selector) }
702 function E() { return L.dom.create.apply(L.dom, arguments) }
703
704 if (typeof(window.CustomEvent) !== 'function') {
705         function CustomEvent(event, params) {
706                 params = params || { bubbles: false, cancelable: false, detail: undefined };
707                 var evt = document.createEvent('CustomEvent');
708                     evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
709                 return evt;
710         }
711
712         CustomEvent.prototype = window.Event.prototype;
713         window.CustomEvent = CustomEvent;
714 }
715
716 function cbi_dropdown_init(sb) {
717         var dl = new L.ui.Dropdown(sb, null, { name: sb.getAttribute('name') });
718         return dl.bind(sb);
719 }
720
721 function cbi_update_table(table, data, placeholder) {
722         var target = isElem(table) ? table : document.querySelector(table);
723
724         if (!isElem(target))
725                 return;
726
727         target.querySelectorAll('.tr.table-titles, .cbi-section-table-titles').forEach(function(thead) {
728                 var titles = [];
729
730                 thead.querySelectorAll('.th').forEach(function(th) {
731                         titles.push(th);
732                 });
733
734                 if (Array.isArray(data)) {
735                         var n = 0, rows = target.querySelectorAll('.tr');
736
737                         data.forEach(function(row) {
738                                 var trow = E('div', { 'class': 'tr' });
739
740                                 for (var i = 0; i < titles.length; i++) {
741                                         var text = (titles[i].innerText || '').trim();
742                                         var td = trow.appendChild(E('div', {
743                                                 'class': titles[i].className,
744                                                 'data-title': (text !== '') ? text : null
745                                         }, row[i] || ''));
746
747                                         td.classList.remove('th');
748                                         td.classList.add('td');
749                                 }
750
751                                 trow.classList.add('cbi-rowstyle-%d'.format((n++ % 2) ? 2 : 1));
752
753                                 if (rows[n])
754                                         target.replaceChild(trow, rows[n]);
755                                 else
756                                         target.appendChild(trow);
757                         });
758
759                         while (rows[++n])
760                                 target.removeChild(rows[n]);
761
762                         if (placeholder && target.firstElementChild === target.lastElementChild) {
763                                 var trow = target.appendChild(E('div', { 'class': 'tr placeholder' }));
764                                 var td = trow.appendChild(E('div', { 'class': titles[0].className }, placeholder));
765
766                                 td.classList.remove('th');
767                                 td.classList.add('td');
768                         }
769                 }
770                 else {
771                         thead.parentNode.style.display = 'none';
772
773                         thead.parentNode.querySelectorAll('.tr, .cbi-section-table-row').forEach(function(trow) {
774                                 if (trow !== thead) {
775                                         var n = 0;
776                                         trow.querySelectorAll('.th, .td').forEach(function(td) {
777                                                 if (n < titles.length) {
778                                                         var text = (titles[n++].innerText || '').trim();
779                                                         if (text !== '')
780                                                                 td.setAttribute('data-title', text);
781                                                 }
782                                         });
783                                 }
784                         });
785
786                         thead.parentNode.style.display = '';
787                 }
788         });
789 }
790
791 function showModal(title, children)
792 {
793         return L.showModal(title, children);
794 }
795
796 function hideModal()
797 {
798         return L.hideModal();
799 }
800
801
802 document.addEventListener('DOMContentLoaded', function() {
803         document.addEventListener('validation-failure', function(ev) {
804                 if (ev.target === document.activeElement)
805                         L.showTooltip(ev);
806         });
807
808         document.addEventListener('validation-success', function(ev) {
809                 if (ev.target === document.activeElement)
810                         L.hideTooltip(ev);
811         });
812
813         document.querySelectorAll('.table').forEach(cbi_update_table);
814 });