|
| 1 | +import {ReactiveController, ReactiveControllerHost} from 'lit'; |
| 2 | +import {FilterMethod, InternalOption, Option} from './types'; |
| 3 | +import { |
| 4 | + containsSearch, |
| 5 | + fuzzySearch, |
| 6 | + SearchResult, |
| 7 | + startsWithPerTermSearch, |
| 8 | + startsWithSearch, |
| 9 | +} from './helpers'; |
| 10 | + |
| 11 | +export class OptionListController implements ReactiveController { |
| 12 | + private _host: ReactiveControllerHost; |
| 13 | + private _options: InternalOption[] = []; |
| 14 | + private _filterPattern = ''; |
| 15 | + private _filterMethod: FilterMethod = 'fuzzy'; |
| 16 | + private _combobox = false; |
| 17 | + private _indexByValue: Map<string, number> = new Map(); |
| 18 | + private _selectedIndex = -1; |
| 19 | + private _selectedIndexes: number[] = []; |
| 20 | + private _multiSelect = false; |
| 21 | + |
| 22 | + constructor(host: ReactiveControllerHost) { |
| 23 | + (this._host = host).addController(this); |
| 24 | + } |
| 25 | + |
| 26 | + hostConnected(): void {} |
| 27 | + |
| 28 | + set multiSelect(multiSelect: boolean) { |
| 29 | + this._multiSelect = multiSelect; |
| 30 | + } |
| 31 | + |
| 32 | + get multiSelect() { |
| 33 | + return this._multiSelect; |
| 34 | + } |
| 35 | + |
| 36 | + set selectedIndex(value: number) { |
| 37 | + this._selectedIndex = value; |
| 38 | + } |
| 39 | + |
| 40 | + get selectedIndex() { |
| 41 | + return this._selectedIndex; |
| 42 | + } |
| 43 | + |
| 44 | + get value(): string | string[] { |
| 45 | + if (this._multiSelect) { |
| 46 | + return this._selectedIndexes.map((v) => this._options[v].value); |
| 47 | + } else { |
| 48 | + return this._options[this._selectedIndex].value; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + set value(newValue: string | string[]) { |
| 53 | + if (this._multiSelect) { |
| 54 | + this._selectedIndexes = (newValue as string[]) |
| 55 | + .map((v) => this._indexByValue.get(v)) |
| 56 | + .filter((v) => v !== undefined); |
| 57 | + } else { |
| 58 | + this._selectedIndex = this._indexByValue.get(newValue as string) ?? -1; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + set filterPattern(pattern: string) { |
| 63 | + this._filterPattern = pattern; |
| 64 | + this._updateState(); |
| 65 | + } |
| 66 | + |
| 67 | + get filterPattern() { |
| 68 | + return this._filterPattern; |
| 69 | + } |
| 70 | + |
| 71 | + populate(options: Option[]) { |
| 72 | + this._indexByValue.clear(); |
| 73 | + |
| 74 | + this._options = options.map((op, index) => { |
| 75 | + this._indexByValue.set(op.value, index); |
| 76 | + |
| 77 | + return { |
| 78 | + description: op.description ?? '', |
| 79 | + disabled: op.disabled ?? false, |
| 80 | + label: op.label ?? '', |
| 81 | + selected: op.selected ?? false, |
| 82 | + value: op.value ?? '', |
| 83 | + index, |
| 84 | + absoluteIndex: index, |
| 85 | + ranges: [], |
| 86 | + visible: true, |
| 87 | + }; |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + toggleComboboxMode(enabled: boolean) { |
| 92 | + this._combobox = enabled; |
| 93 | + } |
| 94 | + |
| 95 | + get options(): InternalOption[] { |
| 96 | + return this._options; |
| 97 | + } |
| 98 | + |
| 99 | + getOptionByValue(value: string): InternalOption | null { |
| 100 | + const index = this._indexByValue.get(value) ?? -1; |
| 101 | + |
| 102 | + if (index === -1) { |
| 103 | + return null; |
| 104 | + } |
| 105 | + |
| 106 | + return this._options[index]; |
| 107 | + } |
| 108 | + |
| 109 | + getNextSelectableOption(fromIndex = 0): InternalOption | null { |
| 110 | + if (this._options.length === 0) { |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + if (this._options.length === 1) { |
| 115 | + return this._options[0]; |
| 116 | + } |
| 117 | + |
| 118 | + if (!this._options[fromIndex] || !this._options[fromIndex + 1]) { |
| 119 | + return this._options[this._options.length - 1]; |
| 120 | + } |
| 121 | + |
| 122 | + let nextIndex = -1; |
| 123 | + |
| 124 | + for (let i = fromIndex + 1; i < this._options.length; i++) { |
| 125 | + if ( |
| 126 | + this._options[i] && |
| 127 | + !this._options[i].disabled && |
| 128 | + this._options[i].visible |
| 129 | + ) { |
| 130 | + nextIndex = i; |
| 131 | + break; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return nextIndex > -1 ? this._options[nextIndex] : null; |
| 136 | + } |
| 137 | + |
| 138 | + _updateState() { |
| 139 | + if (!this._combobox || this._filterPattern === '') { |
| 140 | + this._options.forEach((_, i) => { |
| 141 | + this._options[i].visible = true; |
| 142 | + }); |
| 143 | + |
| 144 | + this._host.requestUpdate(); |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + this._options.forEach((op, i) => { |
| 149 | + let result: SearchResult; |
| 150 | + |
| 151 | + switch (this._filterMethod) { |
| 152 | + case 'startsWithPerTerm': |
| 153 | + result = startsWithPerTermSearch(op.label, this._filterPattern); |
| 154 | + break; |
| 155 | + case 'startsWith': |
| 156 | + result = startsWithSearch(op.label, this._filterPattern); |
| 157 | + break; |
| 158 | + case 'contains': |
| 159 | + result = containsSearch(op.label, this._filterPattern); |
| 160 | + break; |
| 161 | + default: |
| 162 | + result = fuzzySearch(op.label, this._filterPattern); |
| 163 | + } |
| 164 | + |
| 165 | + this._options[i].visible = result.match; |
| 166 | + this._options[i].ranges = result.ranges; |
| 167 | + }); |
| 168 | + |
| 169 | + this._host.requestUpdate(); |
| 170 | + } |
| 171 | +} |
0 commit comments