class TableViewCell 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.selected = false; this.element.addEventListener("click", this.onCellPressed.bind(this)); } /** * 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) { element.tabIndex = 0; this.titleLabel = document.createElement("SPAN"); this.titleLabel.className = "title"; element.appendChild(this.titleLabel); this.subtitleLabel = document.createElement("SPAN"); this.subtitleLabel.className = "subtitle"; element.appendChild(this.subtitleLabel); return element; } getTagName() { return "table-view-cell"; } /** * Sets the given data for this cell. * Subclasses may override this and implement their own behaviour. * Calling the super function is optional. */ setData(data) { if ("title" in data) { this.titleLabel.innerText = data.title; } if ("subtitle" in data) { this.subtitleLabel.innerText = data.subtitle; } } /** * 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() { } onCellPressed(event) { if (this.tableView) { this.tableView.onCellSelected(this, event.metaKey || event.ctrlKey); } } select() { this.setSelected(true); } deselect() { this.setSelected(false); } setSelected(selected) { if (this.selected && !selected) { this.element.classList.remove("selected"); } else if (!this.selected && selected) { this.element.classList.add("selected"); } this.selected = selected; } } UIKit.registerViewType(TableViewCell, "table-view-cell");