App-Netdisco
view release on metacpan or search on metacpan
share/public/javascripts/netdisco-tables.js view on Meta::CPAN
* @param {string | object} spec the column's or callback's data-nd-table entry: a bare name, or an object with "name" plus its args
* @param {{[key: string]: string}} urls the URL prefixes from data-nd-urls, passed through to the resolved factory
* @param {HTMLTableElement} table the table element, passed through to the resolved factory
* @returns {Function} the function DataTables calls for this column or callback
*/
function resolveFrom(map, kind, otherMap, otherKind, spec, urls, table) {
const name = typeof spec === 'string' ? spec : spec.name;
const factory = map[name];
if (!factory) {
if (otherMap[name]) throw new Error('netdisco-tables: "' + name + '" is a ' + otherKind + ', not a ' + kind);
throw new Error('netdisco-tables: no ' + kind + ' named "' + name + '"');
}
// spec doubles as the args object, so its own dispatch key would
// otherwise shadow a renderer argument also called "name" (deviceLabel's
// dns/name/ip row-key convention).
let args = spec;
if (typeof spec !== 'string' && 'name' in spec) {
args = {};
Object.keys(spec).forEach(function (k) {
if (k !== 'name') args[k] = spec[k];
});
}
return factory(args, urls, table);
}
/**
* Resolves a column's render spec against RENDERERS.
* @param {string | object} spec the column's data-nd-table render entry: a bare name, or an object with "name" plus its args
* @param {{[key: string]: string}} urls the URL prefixes from data-nd-urls
* @param {HTMLTableElement} table the table element
* @returns {Function} the render function DataTables calls for this column
*/
function resolveRenderer(spec, urls, table) {
return resolveFrom(RENDERERS, 'renderer', CALLBACKS, 'callback', spec, urls, table);
}
/**
* Resolves a table's drawCallback or initComplete spec against CALLBACKS.
* @param {string | object} spec the table's data-nd-table callback entry: a bare name, or an object with "name" plus its args
* @param {{[key: string]: string}} urls the URL prefixes from data-nd-urls
* @param {HTMLTableElement} table the table element
* @returns {Function} the callback function DataTables calls
*/
function resolveCallback(spec, urls, table) {
return resolveFrom(CALLBACKS, 'callback', RENDERERS, 'renderer', spec, urls, table);
}
/**
* Builds the DataTables options shared by every table, reading the page's own
* data-nd-page-length and data-nd-length-menu body attributes for the paging defaults.
* @param {HTMLTableElement} table the table element being built
* @param {object} spec the table's parsed data-nd-table spec, consulted for customReport
* @returns {object} the shared DataTables configuration object, before the table's own spec is applied over it
*/
function defaults(table, spec) {
const body = document.body.dataset;
return {
processing: true,
stateSave: true,
pageLength: Number(body.ndPageLength || 10),
lengthMenu: JSON.parse(body.ndLengthMenu || '[10, 25, 50, 100]'),
dom: '<"top"l<"nd_datatables-pager"p>f>rit<"bottom"><"clear">',
// stated rather than left to the library, whose default changed to
// full_numbers, adding first and last buttons netdisco never asked for
pagingType: 'simple_numbers',
language: {
processing: 'Processing...',
search: '_INPUT_',
searchPlaceholder: 'Filter records...',
lengthMenu: 'Show _MENU_ records.',
info: ' Showing _START_ to _END_ of _TOTAL_',
infoFiltered: '(filtered from _MAX_ total)',
infoEmpty: ' No matching entries',
paginate: { previous: '← Previous', next: 'Next →' }
},
stateSaveParams: function (settings, data) {
data.search.search = '';
data.start = 0;
if (spec.customReport) data.order = '';
}
};
}
/**
* Builds one DataTable from a table element's data-nd-table and data-nd-urls
* attributes, resolving each column's render spec and the table's drawCallback and
* initComplete against the renderer and callback registries.
* @param {HTMLTableElement} table the <table> element carrying data-nd-table (and optionally data-nd-urls, data-nd-data)
* @param {Element} [root] the just-swapped htmx fragment root, if any, searched first
* for a data-nd-data JSON block before falling back to the whole document
* @returns {DataTable} the constructed DataTables instance
*/
function build(table, root) {
const spec = JSON.parse(table.dataset.ndTable || '{}');
const urls = JSON.parse(table.dataset.ndUrls || '{}');
// "defaults":false opts a table out of every shared option, rather
// than merging its own spec over them.
const config = spec.defaults === false ? {} : defaults(table, spec);
// Each spec key replaces its default wholesale, not merged one level
// deep, so a fragment setting "language" drops every default string, not
// just the ones it names. "customReport" and "defaults" are consumed
// here rather than passed through: DataTables never sees either.
Object.keys(spec).forEach(function (k) {
if (k !== 'customReport' && k !== 'defaults') config[k] = spec[k];
});
(config.columns || []).forEach(function (col) {
if (col.render) col.render = resolveRenderer(col.render, urls, table);
});
if (config.drawCallback) config.drawCallback = resolveCallback(config.drawCallback, urls, table);
if (config.initComplete) config.initComplete = resolveCallback(config.initComplete, urls, table);
if (table.dataset.ndData) {
const id = table.dataset.ndData.replace(/^#/, '');
// htmx swaps one tab pane at a time and never empties the pane it
// leaves, so a stale sibling pane's own JSON block is still in the
// document when this one is built. Look inside the just-swapped root
// first; only a table built with no known root (a full page load)
// reaches all the way out to the document.
const block = (root && root.querySelector('#' + id)) || table.ownerDocument.getElementById(id);
if (block) config.data = JSON.parse(block.textContent);
}
return new DataTable(table, config);
}
/**
* Builds every not-yet-built table[data-nd-table] under root (or the whole document),
* logging and skipping any table whose spec fails to build instead of letting one bad
* table stop the rest.
* @param {Element} [root] the just-swapped htmx fragment root, or the whole document when omitted
* @returns {void}
*/
( run in 0.701 second using v1.01-cache-2.11-cpan-60884a8df76 )