import TableSortDirection from "html/Table/SortDirection.js";
import Table from "html/Table/Table.js";
export default class Column {
constructor(identifier, title) {
console.assert(identifier, identifier);
this._identifier = identifier;
this._sorted = false;
this._sortDirection = TableSortDirection.Ascending;
this._table = null;
this._element = document.createElement("span");
this._element.classList.add("cell", identifier);
this._element.addEventListener("click", this._handleClick.bind(this));
let cellDataElement = this._element.appendChild(document.createElement("span"));
cellDataElement.className = "data";
cellDataElement.textContent = title || "";
}
// Public
get identifier() { return this._identifier; }
get sorted() { return this._sorted; }
set sorted(sorted) {
console.assert(this._table instanceof Table && typeof this._table.columnSortedChanged === "function", this);
if (this._sorted === sorted)
return;
this._sorted = !!sorted;
this._element.classList.toggle(this._sortDirection.description, this._sorted);
this._table.columnSortedChanged(this);
}
get sortDirection() { return this._sortDirection; }
set sortDirection(sortDirection) {
console.assert(this._table instanceof Table && typeof this._table.columnSortDirectionChanged === "function", this);
console.assert(Object.values(TableSortDirection).includes(sortDirection), sortDirection, this);
if (this._sortDirection === sortDirection)
return;
this._element.classList.remove(this._sortDirection.description);
this._sortDirection = sortDirection;
this._element.classList.add(this._sortDirection.description);
this._table.columnSortDirectionChanged(this);
}
// Protected
attach(table) {
console.assert(!this._table, this);
console.assert(table instanceof Table, table, this);
this._table = table;
console.assert(typeof this._table.columnSortedChanged === "function", this);
console.assert(typeof this._table.columnSortDirectionChanged === "function", this);
return this._element;
}
// Private
_handleClick(event) {
console.assert(this._table, event, this);
if (!this.sorted) {
this.sorted = true;
return;
}
switch (this.sortDirection) {
case TableSortDirection.Ascending:
this.sortDirection = TableSortDirection.Descending;
break;
case TableSortDirection.Descending:
this.sortDirection = TableSortDirection.Ascending;
break;
}
}
};