mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-14 23:47:48 +00:00
The cols could have been a lot cleaner with simple classnames, but I preferred to mark up in a way that reveals the purpose (otherwise they could be used for styling).
It doesn't seem to be any faster comparing querySelector('[data]') vs class, or iterating through the dom nodes.
34 lines
927 B
JavaScript
34 lines
927 B
JavaScript
import { Controller } from "stimulus";
|
|
|
|
|
|
// Manage column visibility according to checkbox selection
|
|
//
|
|
export default class ColumnPreferencesController extends Controller {
|
|
connect() {
|
|
this.checkboxes = this.element.querySelectorAll('input[type=checkbox]');
|
|
|
|
for (const element of this.checkboxes) {
|
|
// On initial load
|
|
this.#showHideColumn(element);
|
|
// On checkbox changed
|
|
element.addEventListener("change", this.#showHideColumn.bind(this));
|
|
}
|
|
}
|
|
|
|
// private
|
|
|
|
#showHideColumn(e) {
|
|
const element = e.target || e;
|
|
const name = element.dataset.columnName;
|
|
const selector = `col[data-column-preferences-name="${name}"]`;
|
|
const column = document.querySelector(selector);
|
|
|
|
if (column == null) {
|
|
console.error(`ColumnPreferencesController: could not find ${selector}`);
|
|
return;
|
|
}
|
|
|
|
column.style.visibility = (element.checked ? '' : 'collapse');
|
|
}
|
|
}
|