|
| 1 | +import { cached } from '@glimmer/tracking'; |
| 2 | +import { assert } from '@ember/debug'; |
| 3 | + |
| 4 | +import { BasePlugin, meta, options } from '../-private/base'; |
| 5 | + |
| 6 | +import type { Row, Table } from '[public-types]'; |
| 7 | +import type { PluginSignature, RowApi } from '#interfaces'; |
| 8 | + |
| 9 | +export interface Signature<DataType, Key = DataType> extends PluginSignature { |
| 10 | + Meta: { |
| 11 | + Table: TableMeta; |
| 12 | + Row: RowMeta; |
| 13 | + }; |
| 14 | + Options: { |
| 15 | + Plugin: { |
| 16 | + /** |
| 17 | + * A set of selected things using the same type of Identifier |
| 18 | + * returned from `key` |
| 19 | + */ |
| 20 | + selection: Set<Key> | Array<Key>; |
| 21 | + } & ( |
| 22 | + | { |
| 23 | + /** |
| 24 | + * For a given row's data, how should the key be determined? |
| 25 | + * this could be a remote id from a database, or some other attribute |
| 26 | + * |
| 27 | + * This could be useful for indicating in UI if a particular item is selected. |
| 28 | + * |
| 29 | + * If not provided, the row's data will be used as the key |
| 30 | + */ |
| 31 | + key: (data: DataType) => Key; |
| 32 | + /** |
| 33 | + * When a row is clicked, this will be invoked, |
| 34 | + * allowing you to update your selection object |
| 35 | + */ |
| 36 | + onSelect: (key: Key, row: Row) => void; |
| 37 | + /** |
| 38 | + * When a row is clicked (and the row is selected), this will be invoked, |
| 39 | + * allowing you to update your selection object |
| 40 | + */ |
| 41 | + onDeselect: (key: DataType, row: Row) => void; |
| 42 | + } |
| 43 | + | { |
| 44 | + /** |
| 45 | + * When a row is clicked (and the row is not selected), this will be invoked, |
| 46 | + * allowing you to update your selection object |
| 47 | + */ |
| 48 | + onSelect: (key: DataType, row: Row) => void; |
| 49 | + /** |
| 50 | + * When a row is clicked (and the row is selected), this will be invoked, |
| 51 | + * allowing you to update your selection object |
| 52 | + */ |
| 53 | + onDeselect: (key: DataType, row: Row) => void; |
| 54 | + } |
| 55 | + ); |
| 56 | + }; |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * This plugin provides a means of managing selection of a single row in a table. |
| 61 | + * |
| 62 | + * The state of what is actually selected is managed by you, but this plugin |
| 63 | + * will wire up the click listeners as well as let you know which *data* is clicked. |
| 64 | + */ |
| 65 | +export class RowSelection<DataType, Key = DataType> extends BasePlugin<Signature<DataType, Key>> { |
| 66 | + name = 'row-selection'; |
| 67 | + |
| 68 | + rowModifier = (element: HTMLElement, { row }: RowApi<Table<any>>) => { |
| 69 | + let handler = (event: Event) => { |
| 70 | + this.#clickHandler(row, event); |
| 71 | + }; |
| 72 | + |
| 73 | + element.addEventListener('click', handler); |
| 74 | + |
| 75 | + return () => { |
| 76 | + element.removeEventListener('click', handler); |
| 77 | + }; |
| 78 | + }; |
| 79 | + |
| 80 | + #clickHandler = (row: Row, event: Event) => { |
| 81 | + assert( |
| 82 | + `expected event.target to be an instance of HTMLElement`, |
| 83 | + event.target instanceof HTMLElement || event.target instanceof SVGElement |
| 84 | + ); |
| 85 | + |
| 86 | + let selection = document.getSelection(); |
| 87 | + |
| 88 | + if (selection) { |
| 89 | + let { type, anchorNode } = selection; |
| 90 | + let isSelectingText = type === 'Range' && event.target?.contains(anchorNode); |
| 91 | + |
| 92 | + if (isSelectingText) { |
| 93 | + event.stopPropagation(); |
| 94 | + |
| 95 | + return; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Ignore clicks on interactive elements within the row |
| 100 | + let inputParent = event.target.closest('input, button, label, a, select'); |
| 101 | + |
| 102 | + if (inputParent) { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + let rowMeta = meta.forRow(row, RowSelection); |
| 107 | + |
| 108 | + rowMeta.toggle(); |
| 109 | + }; |
| 110 | +} |
| 111 | + |
| 112 | +class TableMeta { |
| 113 | + #table: Table; |
| 114 | + |
| 115 | + constructor(table: Table) { |
| 116 | + this.#table = table; |
| 117 | + } |
| 118 | + |
| 119 | + @cached |
| 120 | + get selection(): Set<unknown> { |
| 121 | + let passedSelection = options.forTable(this.#table, RowSelection).selection; |
| 122 | + |
| 123 | + assert(`Cannot access selection because it is undefined`, passedSelection); |
| 124 | + |
| 125 | + if (passedSelection instanceof Set) { |
| 126 | + return passedSelection; |
| 127 | + } |
| 128 | + |
| 129 | + return new Set(passedSelection); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +class RowMeta { |
| 134 | + #row: Row; |
| 135 | + |
| 136 | + constructor(row: Row) { |
| 137 | + this.#row = row; |
| 138 | + } |
| 139 | + |
| 140 | + get isSelected(): boolean { |
| 141 | + let tableMeta = meta.forTable(this.#row.table, RowSelection); |
| 142 | + let pluginOptions = options.forTable(this.#row.table, RowSelection); |
| 143 | + |
| 144 | + if ('key' in pluginOptions && pluginOptions.key) { |
| 145 | + let compareWith = pluginOptions.key(this.#row.data); |
| 146 | + |
| 147 | + return tableMeta.selection.has(compareWith); |
| 148 | + } |
| 149 | + |
| 150 | + let compareWith = this.#row.data; |
| 151 | + |
| 152 | + return tableMeta.selection.has(compareWith); |
| 153 | + } |
| 154 | + |
| 155 | + toggle = () => { |
| 156 | + if (this.isSelected) { |
| 157 | + this.deselect(); |
| 158 | + |
| 159 | + return; |
| 160 | + } |
| 161 | + |
| 162 | + this.select(); |
| 163 | + }; |
| 164 | + |
| 165 | + select = () => { |
| 166 | + let pluginOptions = options.forTable(this.#row.table, RowSelection); |
| 167 | + |
| 168 | + if ('key' in pluginOptions && pluginOptions.key) { |
| 169 | + let key = pluginOptions.key(this.#row.data); |
| 170 | + |
| 171 | + pluginOptions.onSelect?.(key, this.#row); |
| 172 | + |
| 173 | + return; |
| 174 | + } |
| 175 | + |
| 176 | + pluginOptions.onSelect?.(this.#row.data, this.#row); |
| 177 | + }; |
| 178 | + |
| 179 | + deselect = () => { |
| 180 | + let pluginOptions = options.forTable(this.#row.table, RowSelection); |
| 181 | + |
| 182 | + if ('key' in pluginOptions && pluginOptions.key) { |
| 183 | + let key = pluginOptions.key(this.#row.data); |
| 184 | + |
| 185 | + pluginOptions.onDeselect?.(key, this.#row); |
| 186 | + |
| 187 | + return; |
| 188 | + } |
| 189 | + |
| 190 | + pluginOptions.onDeselect?.(this.#row.data, this.#row); |
| 191 | + }; |
| 192 | +} |
0 commit comments