class TableView extends View { /** * The constructor accepts a DOM element as a parameter. If it is omitted, a new element is created using the constructor name in kebab case. */ constructor(element = null) { super(element); this.cells = [ ]; this.selectedCells = [ ]; this.selectedCell = null; this.sections = [ ]; //Gather the table view cell instances from the markup. var cellElements = this.element.querySelectorAll("table-view-cell"); for (var i = 0; i < cellElements.length; i++) { var cellInstance = UIKit.getViewInstanceForElement(cellElements[i]); if (!cellInstance) { cellInstance = new TableViewCell(cellElements[i]); } this.cells.push(cellInstance); } //Gather the section instances from the markup. var sectionElements = this.element.querySelectorAll("table-view-section"); for (var i = 0; i < sectionElements.length; i++) { var sectionInstance = UIKit.getViewInstanceForElement(sectionElements[i]); if (!sectionInstance) { sectionInstance = new TableViewSection(sectionElements[i]); } this.sections.push(sectionInstance); } } /** * The build function is called in the constructor when no element was passed as the base. * Override it to create custom subviews or manipulate the root element. * Don't forget to return the element in the end! */ build(element) { return element; } /** * Called once the view has been processed by UIKit during the view controller initialization. */ viewDidLoad() { } /** * Called everytime before the view is shown. * The layout is already done at this step, so it's safe to use scrollWidth and scrollHeight and what not. */ viewWillAppear() { } /** * Called everytime after the view is shown. */ viewDidAppear() { } /** * Called everytime before the view is hidden. */ viewWillDisappear() { } /** * Called everytime after the view is hidden. */ viewDidDisappear() { } /** * Adds the given cell to the table view. * You can optionally provide a section index to which the cell should be added. */ addCell(cell, section = 0) { //Add sections until there are enough to satisfy the required section index. while (this.sections.length <= section) { this.addSection(); } this.sections[section].addCell(cell); if (this.cells.indexOf(cell) == -1) { this.cells.push(cell); } cell.tableView = this; } /** * Removes the specified cell from the table view. */ removeCell(cell) { var index = this.cells.indexOf(cell); if (index == -1) { console.error("Trying to remove a cell from a table view, but the cell is not actually in there."); return; } //Deselect the cell using the multiselect deselection feature if it is selected. if (cell.selected) { this.onCellSelected(cell, true); } cell.section.removeCell(cell); this.cells.splice(index, 1); cell.tableView = null; } /** * Adds a new section to the table view. * Speficy a title to designate the section. */ addSection(title = "") { var section = new TableViewSection(); section.setTitle(title); this.sections.push(section); this.element.appendChild(section.element); } /** * Called by the cells when they get selected. */ onCellSelected(cell, multiselectKeyDown) { var alreadySelected = cell.selected; //First, deselect all cells if the multiselect key is not held down. if (!multiselectKeyDown || (multiselectKeyDown && !this.multiselect)) { this.deselectAll(false); } //Only deselect the cell if it is selected and the multiselect key is held down. if (alreadySelected && multiselectKeyDown) { cell.deselect(); var index = this.selectedCells.indexOf(cell); if (index != -1) { this.selectedCells.splice(index); } this.selectedCell = null; } else { //Otherwise just select the cell, but only if it is the only selected cell or multiselect is enabled. cell.select(); this.selectedCells.push(cell); this.selectedCell = cell; } var cellSelectedEvent = new Event("cellselected"); cellSelectedEvent.cell = cell.selected ? cell : null; this.dispatchEvent(cellSelectedEvent); } /** * Deselects all cells in the table view. */ deselectAll(callListeners = true) { for (var i = 0; i < this.cells.length; i++) { if (this.cells[i].selected) { this.cells[i].deselect(); } } this.selectedCells = [ ]; this.selectedCell = null; if (callListeners) { var cellSelectedEvent = new Event("cellselected"); cellSelectedEvent.cell = null; this.dispatchEvent(cellSelectedEvent); } } /** * Selects the given cell inside this table view. */ selectCell(cell) { this.onCellSelected(cell, false); } } UIKit.registerViewType(TableView, "table-view");