| 1 |
/* ************************************************************************ */ |
| 2 |
/* */ |
| 3 |
/* This file is part of Frama-C. */ |
| 4 |
/* */ |
| 5 |
/* Copyright (C) 2007-2022 */ |
| 6 |
/* CEA (Commissariat à l'énergie atomique et aux énergies */ |
| 7 |
/* alternatives) */ |
| 8 |
/* */ |
| 9 |
/* you can redistribute it and/or modify it under the terms of the GNU */ |
| 10 |
/* Lesser General Public License as published by the Free Software */ |
| 11 |
/* Foundation, version 2.1. */ |
| 12 |
/* */ |
| 13 |
/* It is distributed in the hope that it will be useful, */ |
| 14 |
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 15 |
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ |
| 16 |
/* GNU Lesser General Public License for more details. */ |
| 17 |
/* */ |
| 18 |
/* See the GNU Lesser General Public License version 2.1 */ |
| 19 |
/* for more details (enclosed in the file licenses/LGPLv2.1). */ |
| 20 |
/* */ |
| 21 |
/* ************************************************************************ */ |
| 22 |
|
| 23 |
// -------------------------------------------------------------------------- |
| 24 |
// --- Models |
| 25 |
// -------------------------------------------------------------------------- |
| 26 |
|
| 27 |
/** |
| 28 |
@packageDocumentation |
| 29 |
@module dome/table/models |
| 30 |
*/ |
| 31 |
|
| 32 |
import React from 'react'; |
| 33 |
import { SortDirectionType } from 'react-virtualized'; |
| 34 |
|
| 35 |
// -------------------------------------------------------------------------- |
| 36 |
// --- Listeners |
| 37 |
// -------------------------------------------------------------------------- |
| 38 |
|
| 39 |
/** Update callback. */ |
| 40 |
export type Trigger = () => void; |
| 41 |
|
| 42 |
/** Client Views. */ |
| 43 |
export interface Client { |
| 44 |
/** Set the update callback of the client. */ |
| 45 |
onUpdate(trigger?: Trigger): void; |
| 46 |
/** Set the reload callback of the client. */ |
| 47 |
onReload(trigger?: Trigger): void; |
| 48 |
/** Set the watching range of the client. */ |
| 49 |
watch(lower: number, upper: number): void; |
| 50 |
/** Unlink the client. */ |
| 51 |
unlink(): void; |
| 52 |
} |
| 53 |
|
| 54 |
/** @internal */ |
| 55 |
interface Watcher { |
| 56 |
lower: number; |
| 57 |
upper: number; |
| 58 |
update: undefined | Trigger; |
| 59 |
reload: undefined | Trigger; |
| 60 |
} |
| 61 |
|
| 62 |
// -------------------------------------------------------------------------- |
| 63 |
// --- Sorting |
| 64 |
// -------------------------------------------------------------------------- |
| 65 |
|
| 66 |
/** Sorting Info. */ |
| 67 |
export interface SortingInfo { |
| 68 |
/** The column identifier that triggers some sorting. */ |
| 69 |
sortBy: string; |
| 70 |
/** The requested sorting direction (`'ASC'` or `'DESC'`). */ |
| 71 |
sortDirection: SortDirectionType; |
| 72 |
} |
| 73 |
|
| 74 |
/** Sorting proxy. |
| 75 |
Can be provided along with Models or in a separate class or object. */ |
| 76 |
export interface Sorting { |
| 77 |
/** Whether the model can be sorted from the `dataKey` column identifier. */ |
| 78 |
canSortBy(dataKey: string): boolean; |
| 79 |
/** Callback to respond to sorting requests from columns. */ |
| 80 |
setSorting(order?: SortingInfo): void; |
| 81 |
/** Current sorting information. */ |
| 82 |
getSorting(): SortingInfo | undefined; |
| 83 |
} |
| 84 |
|
| 85 |
// -------------------------------------------------------------------------- |
| 86 |
// --- Filtering |
| 87 |
// -------------------------------------------------------------------------- |
| 88 |
|
| 89 |
export interface Filter<Key, Row> { |
| 90 |
(row: Row, key: Key): boolean; |
| 91 |
} |
| 92 |
|
| 93 |
export interface Filtering<Key, Row> { |
| 94 |
setFilter(fn?: Filter<Key, Row>): void; |
| 95 |
} |
| 96 |
|
| 97 |
// -------------------------------------------------------------------------- |
| 98 |
// --- Collection |
| 99 |
// -------------------------------------------------------------------------- |
| 100 |
|
| 101 |
/** Convenient type for a collection of items. */ |
| 102 |
export type Collection<A> = undefined | null | A | Collection<A>[]; |
| 103 |
|
| 104 |
/** Iterator over collection. */ |
| 105 |
export function forEach<A>(data: Collection<A>, fn: (elt: A) => void): void { |
| 106 |
if (Array.isArray(data)) data.forEach((e) => forEach(e, fn)); |
| 107 |
else if (data !== undefined && data !== null) fn(data); |
| 108 |
} |
| 109 |
|
| 110 |
// -------------------------------------------------------------------------- |
| 111 |
// --- Abstract Model |
| 112 |
// -------------------------------------------------------------------------- |
| 113 |
|
| 114 |
/** |
| 115 |
A Model is responsible for keeping the tables and lists views in sync |
| 116 |
with their associated data sets. The model listens for updates, retrieves |
| 117 |
items from their index, and re-render the views when necessary. |
| 118 |
|
| 119 |
Several tables may connect to the same table model, but they will share the |
| 120 |
same number and ordering of items. However, each connected table will only |
| 121 |
render its own range of items and will re-render only when impacted by |
| 122 |
individual updates. |
| 123 |
|
| 124 |
The model might not hold the entire collection of data at the same time, but |
| 125 |
serves as a proxy for fetching data on demand. A model makes a distinction |
| 126 |
between: |
| 127 |
- `Key`: a key identifies a given entry in the table at any time; |
| 128 |
- `Row`: the row data associated to some `Key` at a given time; |
| 129 |
|
| 130 |
When row data change over time, the table views associated to the model |
| 131 |
use `Key` information to keep scrolling and selection states in sync. |
| 132 |
|
| 133 |
The model is responsible for: |
| 134 |
- providing row data to the views; |
| 135 |
- informing views of data updates; |
| 136 |
- compute row ordering with respect to ordering and/or filtering; |
| 137 |
- lookup key index with respect to ordering and/or filtering; |
| 138 |
|
| 139 |
When your data change over time, you shall invoke the following methods |
| 140 |
of the model to keep views in sync: |
| 141 |
- [[update]] or [[updateIndex]] when single or contiguous row data |
| 142 |
changes over time; |
| 143 |
- [[reload]] when the number of rows, their ordering, or (many) row data |
| 144 |
has been changed. |
| 145 |
|
| 146 |
It is always safe to use `reload` instead of `update` although it might be |
| 147 |
less performant. |
| 148 |
|
| 149 |
@template Key - identification of some entry |
| 150 |
@template Row - dynamic row data associated to some key |
| 151 |
*/ |
| 152 |
export abstract class Model<Key, Row> { |
| 153 |
|
| 154 |
private clients = new Map<number, Watcher>(); |
| 155 |
private clientsId = 0; |
| 156 |
|
| 157 |
constructor() { |
| 158 |
this.reload = this.reload.bind(this); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
Shall return the number of rows to be currently displayed in the table. |
| 163 |
Negative values are considered as zero. |
| 164 |
*/ |
| 165 |
abstract getRowCount(): number; |
| 166 |
|
| 167 |
/** |
| 168 |
Shall return the current row data at a given index in the table, with |
| 169 |
respect to current filtering and ordering (if any). Might return |
| 170 |
`undefined` if the index is invalid or not (yet) available. |
| 171 |
*/ |
| 172 |
abstract getRowAt(index: number): undefined | Row; |
| 173 |
|
| 174 |
/** |
| 175 |
Shall return the key at the given index. The specified index and data are |
| 176 |
those of the last corresponding call to [[getRowAt]]. Might return |
| 177 |
`undefined` if the index is invalid. |
| 178 |
*/ |
| 179 |
abstract getKeyAt(index: number): undefined | Key; |
| 180 |
|
| 181 |
/** |
| 182 |
Shall return the key of the given entry. The specified index and data |
| 183 |
are those of the last corresponding call to [[getRowAt]]. |
| 184 |
Might return `undefined` if the index is invalid. |
| 185 |
*/ |
| 186 |
abstract getKeyFor(index: number, data: Row): undefined | Key; |
| 187 |
|
| 188 |
/** |
| 189 |
Shall return the index of a given entry in the table, identified by its |
| 190 |
key, with respect to current filtering and ordering (if any). |
| 191 |
Shall return |
| 192 |
`undefined` if the specified key no longer belong to the table or when it |
| 193 |
is currently filtered out. Out-of-range indices would be treated as |
| 194 |
`undefined`. |
| 195 |
*/ |
| 196 |
abstract getIndexOf(key: Key): undefined | number; |
| 197 |
|
| 198 |
/** |
| 199 |
Signal an item update. |
| 200 |
Default implementation uses [[getIndexOf]] to retrieve the index and then |
| 201 |
delegates to [[updateIndex]]. |
| 202 |
All views that might be rendering the specified item will be updated. |
| 203 |
*/ |
| 204 |
update(key: Key): void { |
| 205 |
const k = this.getIndexOf(key); |
| 206 |
if (k !== undefined && 0 <= k) this.updateIndex(k); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
Signal a range of updates. |
| 211 |
@param first - the first updated item index |
| 212 |
@param last - the last updated item index (defaults to `first`) |
| 213 |
*/ |
| 214 |
updateIndex(first: number, last = first): void { |
| 215 |
if (first <= last) { |
| 216 |
this.clients.forEach(({ lower, upper, update }) => { |
| 217 |
if (update && first <= upper && lower <= last) update(); |
| 218 |
}); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
Re-render all views. |
| 224 |
Bound to this. |
| 225 |
*/ |
| 226 |
reload(): void { |
| 227 |
this.clients.forEach(({ reload }) => reload && reload()); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
Connect a client view to the model. |
| 232 |
The initial watching range is empty with no trigger. |
| 233 |
You normally never call this method directly. |
| 234 |
It is automatically called by table views. |
| 235 |
@param onReload - optional callback for reloads |
| 236 |
(and updates, unless specified) |
| 237 |
@param onUpdate - optional callback for updates |
| 238 |
(when different from reloads) |
| 239 |
*/ |
| 240 |
link(onReload?: Trigger, onUpdate?: Trigger): Client { |
| 241 |
const id = this.clientsId++; |
| 242 |
const m = this.clients; |
| 243 |
const w: Watcher & Client = { |
| 244 |
lower: 0, |
| 245 |
upper: 0, |
| 246 |
reload: onReload, |
| 247 |
update: onUpdate ?? onReload, |
| 248 |
onUpdate(s?: Trigger) { w.update = s; }, |
| 249 |
onReload(s?: Trigger) { w.reload = s; }, |
| 250 |
unlink() { m.delete(id); }, |
| 251 |
watch(lower: number, upper: number) { |
| 252 |
w.lower = lower; |
| 253 |
w.upper = upper; |
| 254 |
}, |
| 255 |
}; |
| 256 |
m.set(id, w); |
| 257 |
return w; |
| 258 |
} |
| 259 |
|
| 260 |
} |
| 261 |
|
| 262 |
// -------------------------------------------------------------------------- |
| 263 |
// --- Model Hooks |
| 264 |
// -------------------------------------------------------------------------- |
| 265 |
|
| 266 |
/** |
| 267 |
Make the component to synchronize with the model and re-render on |
| 268 |
every updates. |
| 269 |
@param sync - whether to synchronize on model updates or not, `true` |
| 270 |
by default. |
| 271 |
@return a number that can be used to memoize other effects |
| 272 |
*/ |
| 273 |
|
| 274 |
export function useModel<K, R>(model: Model<K, R>, sync = true): number { |
| 275 |
const [age, setAge] = React.useState(0); |
| 276 |
React.useEffect(() => { |
| 277 |
if (sync) { |
| 278 |
const w = model.link(() => setImmediate(() => setAge(age + 1))); |
| 279 |
return w.unlink; |
| 280 |
} |
| 281 |
return undefined; |
| 282 |
}); |
| 283 |
return age; |
| 284 |
} |
| 285 |
|
| 286 |
// -------------------------------------------------------------------------- |