add tabindex script - jscancer - Javascript crap (relatively small)
 (HTM) git clone git://git.codemadness.org/jscancer
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 908a85083071d1f8b4559e48f276b1a837f13e81
 (DIR) parent ce985f99424e909b959b7cf2736312074e00d4cf
 (HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
       Date:   Fri, 30 Aug 2024 11:11:54 +0200
       
       add tabindex script
       
       Diffstat:
         A tabindex/README                     |       9 +++++++++
         A tabindex/tabindex.js                |      43 ++++++++++++++++++++++++++++++
       
       2 files changed, 52 insertions(+), 0 deletions(-)
       ---
 (DIR) diff --git a/tabindex/README b/tabindex/README
       @@ -0,0 +1,9 @@
       +This script sets the tabindex for a collection of elements.
       +
       +It can be useful if there are a lot of elements which are generated in some way
       +and setting the tabindex attribute where is a chore.
       +
       +For tables it can be useful to set the tabindex based on the cells -> rows from
       +top-left vertically.  Normally this would be horizontally (left to right).
       +
       +Note that there are also bugs and inconsistencies in how Firefox handles tabindex.
 (DIR) diff --git a/tabindex/tabindex.js b/tabindex/tabindex.js
       @@ -0,0 +1,42 @@
       +// automatically increment and set tabindex on enabled/writable inputs.
       +var tabindexroots = document.querySelectorAll(".tabindex-auto");
       +if (tabindexroots && tabindexroots.length) {
       +        var tabels = []; // all input elements to potentially apply tabindex too.
       +        var selector = "input, select, textarea" // "input, select, textarea, a";
       +        for (var i = 0; i < tabindexroots.length; i++) {
       +                var tabindexroot = tabindexroots[i];
       +                if (tabindexroot.tagName === "TABLE") {
       +                        // determine max column.
       +                        var max = -1;
       +                        for (var row of tabindexroot.rows) {
       +                                if (max === -1 || row.cells.length > max)
       +                                        max = row.cells.length;
       +                        }
       +                        // for tables option to increase tabindex vertically per row per cell.
       +                        for (var j = 0; j < max; j++) {
       +                                for (var row of tabindexroot.rows) {
       +                                        if (j >= row.cells.length)
       +                                                continue;
       +                                        var inputs = row.cells[j].querySelectorAll(selector);
       +                                        if (inputs && inputs.length)
       +                                                tabels = tabels.concat(Array.from(inputs));
       +                                }
       +                        }
       +                } else {
       +                        var els = tabindexroot.querySelectorAll(selector);
       +                        if (els && els.length)
       +                                tabels = tabels.concat(Array.from(els));
       +                }
       +        }
       +
       +        // apply tabindex.
       +        var tabindexcount = 1;
       +        for (var i = 0; i < tabels.length; i++) {
       +                // filter elements
       +                if (tabels[i].tagName == "A" ||
       +                    tabels[i].readOnly || tabels[i].disabled || tabels[i].hidden)
       +                        continue;
       +                tabels[i].tabIndex = tabindexcount;
       +                tabindexcount++;
       +        }
       +}
       +\ No newline at end of file