luci-base: cbi: fix button handling for dynamic lists
[project/luci.git] / modules / luci-base / htdocs / luci-static / resources / cbi.js
index b27c564..b285ee2 100644 (file)
@@ -454,7 +454,7 @@ function cbi_d_update() {
                if (node && node.parentNode && !cbi_d_check(entry.deps)) {
                        node.parentNode.removeChild(node);
                        state = true;
-               } else if ((!node || !node.parentNode) && cbi_d_check(entry.deps)) {
+               } else if (parent && (!node || !node.parentNode) && cbi_d_check(entry.deps)) {
                        var next = undefined;
 
                        for (next = parent.firstChild; next; next = next.nextSibling) {
@@ -473,7 +473,7 @@ function cbi_d_update() {
                }
 
                // hide optionals widget if no choices remaining
-               if (parent.parentNode && parent.getAttribute('data-optionals'))
+               if (parent && parent.parentNode && parent.getAttribute('data-optionals'))
                        parent.parentNode.style.display = (parent.options.length <= 1) ? 'none' : '';
        }
 
@@ -550,10 +550,14 @@ function cbi_init() {
 
        for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
                var choices = JSON.parse(node.getAttribute('data-dynlist'));
-               var options = {};
+               var options = null;
 
-               for (var j = 0; j < choices[0].length; j++)
-                       options[choices[0][j]] = choices[1][j];
+               if (choices[0] && choices[0].length) {
+                       options = {};
+
+                       for (var j = 0; j < choices[0].length; j++)
+                               options[choices[0][j]] = choices[1][j];
+               }
 
                cbi_dynlist_init(node, choices[2], choices[3], options);
        }
@@ -588,6 +592,7 @@ function cbi_combobox(id, values, def, man, focus) {
        var obj = document.getElementById(id)
        var sel = document.createElement("select");
                sel.id = selid;
+               sel.index = obj.index;
                sel.className = obj.className.replace(/cbi-input-text/, 'cbi-input-select');
 
        if (obj.nextSibling) {
@@ -921,14 +926,14 @@ function cbi_dynlist_init(parent, datatype, optional, choices)
                        input.value = '';
 
                        cbi_dynlist_keydown({
-                               target:  se,
+                               target:  input,
                                keyCode: 8
                        });
                }
                else
                {
                        cbi_dynlist_keydown({
-                               target:  se,
+                               target:  input,
                                keyCode: 13
                        });
                }
@@ -1305,7 +1310,7 @@ String.prototype.format = function()
        var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X|q|h|j|t|m))/;
        var a = b = [], numSubstitutions = 0, numMatches = 0;
 
-       while( a = re.exec(str) )
+       while (a = re.exec(str))
        {
                var m = a[1];
                var leftpart = a[2], pPad = a[3], pJustify = a[4], pMinLength = a[5];
@@ -1328,6 +1333,8 @@ String.prototype.format = function()
                                        pad = leftpart.substr(1,1);
                                else if (pPad)
                                        pad = pPad;
+                               else
+                                       pad = ' ';
 
                                var justifyRight = true;
                                if (pJustify && pJustify === "-")
@@ -1354,11 +1361,11 @@ String.prototype.format = function()
                                                break;
 
                                        case 'd':
-                                               subst = (+param || 0);
+                                               subst = ~~(+param || 0);
                                                break;
 
                                        case 'u':
-                                               subst = Math.abs(+param || 0);
+                                               subst = ~~Math.abs(+param || 0);
                                                break;
 
                                        case 'f':
@@ -1428,17 +1435,27 @@ String.prototype.format = function()
 
                                                var i = 0;
                                                var val = (+param || 0);
-                                               var units = [ '', 'K', 'M', 'G', 'T', 'P', 'E' ];
+                                               var units = [ ' ', ' K', ' M', ' G', ' T', ' P', ' E' ];
 
                                                for (i = 0; (i < units.length) && (val > mf); i++)
                                                        val /= mf;
 
-                                               subst = val.toFixed(pr) + ' ' + units[i];
+                                               subst = (i ? val.toFixed(pr) : val) + units[i];
+                                               pMinLength = null;
                                                break;
                                }
                        }
                }
 
+               if (pMinLength) {
+                       subst = subst.toString();
+                       for (var i = subst.length; i < pMinLength; i++)
+                               if (pJustify == '-')
+                                       subst = subst + ' ';
+                               else
+                                       subst = pad + subst;
+               }
+
                out += leftpart + subst;
                str = str.substr(m.length);
        }