/** * Runtime for the data table: column sorting, row search and the sticky * header's fit check. ES5-style, like `runtime.ts`, and emitted only on pages * that use the feature. * * The markup reads completely without it: rows keep their authored order and * the search bar stays hidden until `data-ak-table-ready` is set. Sort buttons * are created here, inside each ``, so a page without scripts never shows a * control that does nothing. * * Search hides a row with `data-ak-dt-miss`, not `hidden`, so a filter that * toggles `hidden` on the same rows composes with it instead of fighting it. */ export declare const DATA_TABLE_RUNTIME = "\nvar DT_NUMBER = /^-?\\d+(?:\\.\\d+)?(?:e[+-]?\\d+)?$/i;\nfunction dtRows(table) { return table.tBodies.length ? Array.prototype.slice.call(table.tBodies[0].rows) : []; }\nfunction dtHeads(table) { return table.tHead && table.tHead.rows.length ? Array.prototype.slice.call(table.tHead.rows[0].cells) : []; }\nfunction sortDataTable(table, index) {\n var heads = dtHeads(table);\n var head = heads[index];\n if (!head) return;\n var direction = head.getAttribute('aria-sort') === 'ascending' ? 'descending' : 'ascending';\n var rows = dtRows(table);\n var values = rows.map(function (row) {\n var cell = row.cells[index];\n return cell && cell.hasAttribute('data-sort') ? cell.getAttribute('data-sort') : null;\n });\n var numeric = values.every(function (value) { return value === null || DT_NUMBER.test(value); });\n var order = rows.map(function (row, position) { return position; });\n order.sort(function (a, b) {\n var x = values[a];\n var y = values[b];\n // Empty cells sort last in both directions; ties keep their current order.\n if (x === null || y === null) return x === y ? a - b : (x === null ? 1 : -1);\n var result = numeric ? Number(x) - Number(y) : (x < y ? -1 : (x > y ? 1 : 0));\n if (direction === 'descending') result = -result;\n return result || a - b;\n });\n var body = table.tBodies[0];\n order.forEach(function (position) { body.appendChild(rows[position]); });\n heads.forEach(function (cell) { cell.removeAttribute('aria-sort'); });\n head.setAttribute('aria-sort', direction);\n announce('Sorted by ' + (head.textContent || '').trim() + ', ' + direction);\n}\nfunction searchDataTable(block, table, query, speak) {\n var needle = String(query || '').trim().toLowerCase();\n var rows = dtRows(table);\n var shown = 0;\n rows.forEach(function (row) {\n var hit = needle === '' || (row.textContent || '').toLowerCase().indexOf(needle) !== -1;\n if (hit) row.removeAttribute('data-ak-dt-miss');\n else row.setAttribute('data-ak-dt-miss', '');\n if (hit && !row.hidden) shown += 1;\n });\n var total = rows.length;\n var noun = total === 1 ? ' row' : ' rows';\n var count = q('[data-ak-dt-count]', block);\n if (count) count.textContent = shown === total ? total + noun : shown + ' of ' + total + noun;\n var empty = q('[data-ak-dt-empty]', block);\n if (empty) empty.hidden = shown !== 0;\n if (speak) announce(shown + ' of ' + total + noun + ' shown');\n}\nfunction fitDataTables() {\n qa('[data-ak-data-table][data-ak-sticky]').forEach(function (block) {\n var wrap = q('.ak-table-wrap', block);\n if (!wrap) return;\n // Measure as a scroller; a table that fits needs no sideways scroll, so its\n // frame stops being a scroll container and the header can stick to the page.\n block.removeAttribute('data-ak-dt-fits');\n if (wrap.scrollWidth <= wrap.clientWidth + 1) block.setAttribute('data-ak-dt-fits', '');\n });\n}\nfunction wireDataTables() {\n var blocks = qa('[data-ak-data-table]');\n blocks.forEach(function (block) {\n var table = q('table', block);\n if (!table) return;\n dtHeads(table).forEach(function (head, index) {\n var button = doc.createElement('button');\n button.type = 'button';\n button.className = 'ak-dt-sort';\n while (head.firstChild) button.appendChild(head.firstChild);\n var icon = doc.createElement('span');\n icon.className = 'ak-dt-sort-icon';\n icon.setAttribute('aria-hidden', 'true');\n button.appendChild(icon);\n head.appendChild(button);\n button.addEventListener('click', function () { sortDataTable(table, index); }, false);\n });\n var input = q('[data-ak-dt-search]', block);\n if (input) {\n input.addEventListener('input', function () { searchDataTable(block, table, input.value, true); }, false);\n if (input.value) searchDataTable(block, table, input.value, false);\n }\n block.setAttribute('data-ak-table-ready', '');\n });\n if (!blocks.length) return;\n fitDataTables();\n var pending = false;\n window.addEventListener('resize', function () {\n if (pending) return;\n pending = true;\n window.requestAnimationFrame(function () { pending = false; fitDataTables(); });\n }, false);\n}"; //# sourceMappingURL=data-table-runtime.d.ts.map