class InvalidCSSIDSelector extends Error {
constructor(message) {
super(message);
this.name = "InvalidCSSIDSelector";
}
}
class InvalidDataAttributeName extends Error {
constructor(message) {
super(message);
this.name = "InvalidDataAttributeName";
}
}
/**
* CSSIDSelector: A CSS ID Selector
* @param {String} A CSS selector of the form '#...'
*/
class CSSIDSelector {
constructor(selector) {
if(!selector.startsWith("#")) {
throw new InvalidCSSIDSelector(
"Expected the CSS selector to begin with a `#` character.");
}
let id_str = selector.slice(1, selector.length);
if(document.getElementById(id_str) == null) {
throw new InvalidCSSIDSelector(
"Element with ID '" + id_str + "' does not exist.");
}
this.selector = selector;
}
}
/**
* TableDataSource: A type to represent a table's data source
* @param {String} A CSS selector for an ID
* @param {String} A `data-*` attribute name
*/
class TableDataSource {
constructor(table_id, data_attribute_name, checkbox_creation_function) {
this.table_id = new CSSIDSelector(table_id);
let data = document.querySelector(
table_id).getAttribute(data_attribute_name);
if(data == null) {
throw new InvalidDataAttributeName(
"data-* attribute '" + data_attribute_name + "' does not exist " +
"for table with ID '" + table_id.slice(1, table_id.length) +
"'.");
} else {
this.data_attribute_name = data_attribute_name;
}
this.checkbox_creation_function = checkbox_creation_function;
}
}
/**
* Render the table
* @param {String} The selector for the table's ID
* @param {String} The name of the data-* attribute holding the table's data
* @param {Function} The function to call to generate the appropriate checkbox
*/
function render_table(table_data_source) {
table_id = table_data_source.table_id.selector;
data_attr_name = table_data_source.data_attribute_name;
$(table_id + " tbody tr").remove();
table_data = JSON.parse($(table_id).attr(data_attr_name)).sort((d1, d2) => {
return (d1.dataset_name > d2.dataset_name ? 1 : (
d1.dataset_name < d2.dataset_name ? -1 : 0))
});
if(table_data.length < 1) {
row = $("