luci-base: cbi.js: fix cbi_row_swap() after recent table markup changes
authorJo-Philipp Wich <jo@mein.io>
Wed, 30 May 2018 13:59:52 +0000 (15:59 +0200)
committerJo-Philipp Wich <jo@mein.io>
Wed, 30 May 2018 13:59:52 +0000 (15:59 +0200)
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
modules/luci-base/htdocs/luci-static/resources/cbi.js

index 47eead5d8aad9757eab671c3b1af9f01aba90ec3..724691ace2a752310e7228ed7cbaf683bd3dad8a 100644 (file)
@@ -1244,44 +1244,44 @@ function cbi_validate_field(cbid, optional, type)
 function cbi_row_swap(elem, up, store)
 {
        var tr = elem.parentNode;
-       while (tr && tr.nodeName.toLowerCase() != 'tr')
+
+       while (tr && !tr.classList.contains('cbi-section-table-row'))
                tr = tr.parentNode;
 
        if (!tr)
                return false;
 
-       var table = tr.parentNode;
-       while (table && table.nodeName.toLowerCase() != 'table')
-               table = table.parentNode;
-
-       if (!table)
-               return false;
-
-       var s = up ? 3 : 2;
-       var e = up ? table.rows.length : table.rows.length - 1;
+       if (up) {
+               var prev = tr.previousElementSibling;
 
-       for (var idx = s; idx < e; idx++)
-       {
-               if (table.rows[idx] == tr)
-               {
-                       if (up)
-                               tr.parentNode.insertBefore(table.rows[idx], table.rows[idx-1]);
-                       else
-                               tr.parentNode.insertBefore(table.rows[idx+1], table.rows[idx]);
+               if (prev && prev.classList.contains('cbi-section-table-row'))
+                       tr.parentNode.insertBefore(tr, prev);
+               else
+                       return;
+       }
+       else {
+               var next = tr.nextElementSibling ? tr.nextElementSibling.nextElementSibling : null;
 
-                       break;
-               }
+               if (next && next.classList.contains('cbi-section-table-row'))
+                       tr.parentNode.insertBefore(tr, next);
+               else if (!next)
+                       tr.parentNode.appendChild(tr);
+               else
+                       return;
        }
 
        var ids = [ ];
-       for (idx = 2; idx < table.rows.length; idx++)
-       {
-               table.rows[idx].className = table.rows[idx].className.replace(
-                       /cbi-rowstyle-[12]/, 'cbi-rowstyle-' + (1 + (idx % 2))
-               );
 
-               if (table.rows[idx].id && table.rows[idx].id.match(/-([^\-]+)$/) )
-                       ids.push(RegExp.$1);
+       for (var i = 0, n = 0; i < tr.parentNode.childNodes.length; i++) {
+               var node = tr.parentNode.childNodes[i];
+               if (node.classList && node.classList.contains('cbi-section-table-row')) {
+                       node.classList.remove('cbi-rowstyle-1');
+                       node.classList.remove('cbi-rowstyle-2');
+                       node.classList.add((n++ % 2) ? 'cbi-rowstyle-2' : 'cbi-rowstyle-1');
+
+                       if (/-([^\-]+)$/.test(node.id))
+                               ids.push(RegExp.$1);
+               }
        }
 
        var input = document.getElementById(store);