|
| 1 | +import type { dh } from '@deephaven/jsapi-types'; |
| 2 | +import { |
| 3 | + DATABAR_MIN_SUFFIX, |
| 4 | + DATABAR_MAX_SUFFIX, |
| 5 | + HEATMAP_MIN_SUFFIX, |
| 6 | + HEATMAP_MAX_SUFFIX, |
| 7 | +} from './UITableUtils'; |
| 8 | + |
| 9 | +export interface UITableLayoutHints { |
| 10 | + frontColumns?: string[]; |
| 11 | + frozenColumns?: string[]; |
| 12 | + backColumns?: string[]; |
| 13 | + hiddenColumns?: string[]; |
| 14 | + columnGroups?: dh.ColumnGroup[]; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Subset of the dh.Table / dh.TreeTable surface area used by the proxy base |
| 19 | + * class. Both Table and TreeTable expose these members, so the base proxy can |
| 20 | + * operate on either. |
| 21 | + */ |
| 22 | +export interface ProxyableTable { |
| 23 | + readonly columns: dh.Column[]; |
| 24 | + readonly customColumns: dh.CustomColumn[]; |
| 25 | + readonly isClosed: boolean; |
| 26 | + applyCustomColumns: ( |
| 27 | + customColumns: Array<string | dh.CustomColumn> |
| 28 | + ) => Array<dh.CustomColumn>; |
| 29 | + close: () => void; |
| 30 | + setViewport: ( |
| 31 | + firstRow: number, |
| 32 | + lastRow: number, |
| 33 | + columns?: Array<dh.Column> | undefined | null, |
| 34 | + updateIntervalMs?: number | undefined | null |
| 35 | + ) => dh.TableViewportSubscription | void; |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Base class for proxying a JS API Table or TreeTable. |
| 40 | + * |
| 41 | + * The underlying table passed to the constructor may be modified, so callers |
| 42 | + * should pass a copy. Methods implemented on this class (or a subclass) take |
| 43 | + * precedence over the underlying table's methods; everything else is proxied |
| 44 | + * through to the table. |
| 45 | + */ |
| 46 | +// eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 47 | +// @ts-ignore |
| 48 | +class JsBaseTableProxy<T extends ProxyableTable, R> { |
| 49 | + static HIDDEN_COLUMN_SUFFIXES = [ |
| 50 | + DATABAR_MIN_SUFFIX, |
| 51 | + DATABAR_MAX_SUFFIX, |
| 52 | + HEATMAP_MIN_SUFFIX, |
| 53 | + HEATMAP_MAX_SUFFIX, |
| 54 | + '__FORMAT', |
| 55 | + ]; |
| 56 | + |
| 57 | + protected table: T; |
| 58 | + |
| 59 | + /** |
| 60 | + * Keep a stable reference to all, visible, and hidden columns. |
| 61 | + * Only update when needed. |
| 62 | + */ |
| 63 | + private stableColumns: { |
| 64 | + allColumns: dh.Column[]; |
| 65 | + visibleColumns: dh.Column[]; |
| 66 | + hiddenColumns: dh.Column[]; |
| 67 | + }; |
| 68 | + |
| 69 | + protected originalCustomColumns: dh.CustomColumn[]; |
| 70 | + |
| 71 | + private onClose: () => void; |
| 72 | + |
| 73 | + layoutHints: dh.LayoutHints | null = null; |
| 74 | + |
| 75 | + constructor({ |
| 76 | + table, |
| 77 | + layoutHints, |
| 78 | + onClose, |
| 79 | + }: { |
| 80 | + table: T; |
| 81 | + layoutHints: UITableLayoutHints; |
| 82 | + onClose: () => void; |
| 83 | + }) { |
| 84 | + this.table = table; |
| 85 | + this.originalCustomColumns = table.customColumns; |
| 86 | + this.onClose = onClose; |
| 87 | + |
| 88 | + this.stableColumns = { |
| 89 | + allColumns: [], |
| 90 | + visibleColumns: [], |
| 91 | + hiddenColumns: [], |
| 92 | + }; |
| 93 | + |
| 94 | + this.layoutHints = { |
| 95 | + frontColumns: null, |
| 96 | + frozenColumns: null, |
| 97 | + backColumns: null, |
| 98 | + hiddenColumns: null, |
| 99 | + columnGroups: null, |
| 100 | + ...layoutHints, |
| 101 | + areSavedLayoutsAllowed: false, |
| 102 | + }; |
| 103 | + |
| 104 | + // eslint-disable-next-line no-constructor-return |
| 105 | + return new Proxy(this, { |
| 106 | + // We want to use any properties on the proxy model if defined |
| 107 | + // If not, then proxy to the underlying model |
| 108 | + get(target, prop, receiver) { |
| 109 | + // Walk the prototype chain so getters defined on subclasses or this |
| 110 | + // base class are found. |
| 111 | + let proto = Object.getPrototypeOf(target); |
| 112 | + while (proto != null && proto !== Object.prototype) { |
| 113 | + const descriptor = Object.getOwnPropertyDescriptor(proto, prop); |
| 114 | + if (descriptor?.get != null) { |
| 115 | + return Reflect.get(target, prop, receiver); |
| 116 | + } |
| 117 | + if (descriptor != null && typeof descriptor.value === 'function') { |
| 118 | + return descriptor.value.bind(target); |
| 119 | + } |
| 120 | + proto = Object.getPrototypeOf(proto); |
| 121 | + } |
| 122 | + |
| 123 | + // Does this instance implement the property |
| 124 | + if (Object.prototype.hasOwnProperty.call(target, prop)) { |
| 125 | + return Reflect.get(target, prop, receiver); |
| 126 | + } |
| 127 | + |
| 128 | + const value = Reflect.get(target.table, prop, receiver); |
| 129 | + if (typeof value === 'function') { |
| 130 | + return value.bind(target.table); |
| 131 | + } |
| 132 | + return value; |
| 133 | + }, |
| 134 | + set(target, prop, value) { |
| 135 | + // Walk the prototype chain looking for a setter. |
| 136 | + let proto = Object.getPrototypeOf(target); |
| 137 | + while (proto != null && proto !== Object.prototype) { |
| 138 | + const descriptor = Object.getOwnPropertyDescriptor(proto, prop); |
| 139 | + if (descriptor?.set != null) { |
| 140 | + return Reflect.set(target, prop, value); |
| 141 | + } |
| 142 | + proto = Object.getPrototypeOf(proto); |
| 143 | + } |
| 144 | + |
| 145 | + if (Object.prototype.hasOwnProperty.call(target, prop)) { |
| 146 | + return Reflect.set(target, prop, value); |
| 147 | + } |
| 148 | + |
| 149 | + return Reflect.set(target.table, prop, value, target.table); |
| 150 | + }, |
| 151 | + }); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Update the stable columns object if needed. |
| 156 | + * This lets us keep a stable array for columns unless the underlying table changes. |
| 157 | + */ |
| 158 | + private updateDisplayedColumns(): void { |
| 159 | + if (this.stableColumns.allColumns !== this.table.columns) { |
| 160 | + this.stableColumns.allColumns = this.table.columns; |
| 161 | + |
| 162 | + this.stableColumns.visibleColumns = this.table.columns.filter( |
| 163 | + column => |
| 164 | + !JsBaseTableProxy.HIDDEN_COLUMN_SUFFIXES.some(suffix => |
| 165 | + column.name.endsWith(suffix) |
| 166 | + ) |
| 167 | + ); |
| 168 | + |
| 169 | + this.stableColumns.hiddenColumns = this.table.columns.filter(column => |
| 170 | + JsBaseTableProxy.HIDDEN_COLUMN_SUFFIXES.some(suffix => |
| 171 | + column.name.endsWith(suffix) |
| 172 | + ) |
| 173 | + ); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + close(): void { |
| 178 | + // Something causes close to get called twice which will throw some log spam if we try to close the table again |
| 179 | + if (!this.table.isClosed) { |
| 180 | + this.onClose(); |
| 181 | + this.table.close(); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + applyCustomColumns( |
| 186 | + customColumns: Array<string | dh.CustomColumn> |
| 187 | + ): Array<dh.CustomColumn> { |
| 188 | + return this.table.applyCustomColumns([ |
| 189 | + ...this.originalCustomColumns, |
| 190 | + ...customColumns, |
| 191 | + ]); |
| 192 | + } |
| 193 | + |
| 194 | + get columns(): dh.Column[] { |
| 195 | + this.updateDisplayedColumns(); |
| 196 | + return this.stableColumns.visibleColumns; |
| 197 | + } |
| 198 | + |
| 199 | + get hiddenColumns(): dh.Column[] { |
| 200 | + this.updateDisplayedColumns(); |
| 201 | + return this.stableColumns.hiddenColumns; |
| 202 | + } |
| 203 | + |
| 204 | + setViewport( |
| 205 | + firstRow: number, |
| 206 | + lastRow: number, |
| 207 | + columns?: Array<dh.Column> | undefined | null, |
| 208 | + updateIntervalMs?: number | undefined | null |
| 209 | + ): R { |
| 210 | + if (columns == null) { |
| 211 | + return this.table.setViewport( |
| 212 | + firstRow, |
| 213 | + lastRow, |
| 214 | + columns, |
| 215 | + updateIntervalMs |
| 216 | + ) as unknown as R; |
| 217 | + } |
| 218 | + |
| 219 | + const allColumns = columns.concat(this.hiddenColumns); |
| 220 | + const viewportSubscription = this.table.setViewport( |
| 221 | + firstRow, |
| 222 | + lastRow, |
| 223 | + allColumns, |
| 224 | + updateIntervalMs |
| 225 | + ); |
| 226 | + // TreeTable.setViewport returns void; avoid wrapping undefined in a Proxy |
| 227 | + if (viewportSubscription == null) { |
| 228 | + return viewportSubscription as unknown as R; |
| 229 | + } |
| 230 | + return new Proxy(viewportSubscription, { |
| 231 | + get: (target, prop, receiver) => { |
| 232 | + // Need to proxy setViewport on the subscription in case it is changed |
| 233 | + // without creating an entirely new subscription |
| 234 | + if (prop === 'setViewport') { |
| 235 | + return ( |
| 236 | + first: number, |
| 237 | + last: number, |
| 238 | + cols?: dh.Column[] | null, |
| 239 | + interval?: number | null |
| 240 | + ) => { |
| 241 | + if (cols == null) { |
| 242 | + return target.setViewport(first, last, cols, interval); |
| 243 | + } |
| 244 | + |
| 245 | + const proxyAllColumns = cols.concat(this.hiddenColumns); |
| 246 | + |
| 247 | + return target.setViewport(first, last, proxyAllColumns, interval); |
| 248 | + }; |
| 249 | + } |
| 250 | + return Reflect.get(target, prop, receiver); |
| 251 | + }, |
| 252 | + }) as unknown as R; |
| 253 | + } |
| 254 | +} |
| 255 | + |
| 256 | +export default JsBaseTableProxy; |
0 commit comments