UNPKG

13.7 kBTypeScriptView Raw
1import { DataInterface, DataInterfaceForEachOptions, DataInterfaceGetIdsOptions, DataInterfaceGetOptions, DataInterfaceGetOptionsArray, DataInterfaceGetOptionsObject, DataInterfaceMapOptions, DeepPartial, FullItem, Id, PartItem, UpdateItem } from "./data-interface";
2import { Queue, QueueOptions } from "./queue";
3import { DataSetPart } from "./data-set-part";
4import { DataStream } from "./data-stream";
5/**
6 * Initial DataSet configuration object.
7 *
8 * @typeParam IdProp - Name of the property that contains the id.
9 */
10export interface DataSetInitialOptions<IdProp extends string> {
11 /**
12 * The name of the field containing the id of the items. When data is fetched from a server which uses some specific field to identify items, this field name can be specified in the DataSet using the option `fieldId`. For example [CouchDB](http://couchdb.apache.org/) uses the field `'_id'` to identify documents.
13 */
14 fieldId?: IdProp;
15 /**
16 * Queue data changes ('add', 'update', 'remove') and flush them at once. The queue can be flushed manually by calling `DataSet.flush()`, or can be flushed after a configured delay or maximum number of entries.
17 *
18 * When queue is true, a queue is created with default options. Options can be specified by providing an object.
19 */
20 queue?: QueueOptions | false;
21}
22/** DataSet configuration object. */
23export interface DataSetOptions {
24 /**
25 * Queue configuration object or false if no queue should be used.
26 *
27 * - If false and there was a queue before it will be flushed and then removed.
28 * - If {@link QueueOptions} the existing queue will be reconfigured or a new queue will be created.
29 */
30 queue?: Queue | QueueOptions | false;
31}
32/**
33 * # DataSet
34 *
35 * Vis.js comes with a flexible DataSet, which can be used to hold and
36 * manipulate unstructured data and listen for changes in the data. The DataSet
37 * is key/value based. Data items can be added, updated and removed from the
38 * DataSet, and one can subscribe to changes in the DataSet. The data in the
39 * DataSet can be filtered and ordered. Data can be normalized when appending it
40 * to the DataSet as well.
41 *
42 * ## Example
43 *
44 * The following example shows how to use a DataSet.
45 *
46 * ```javascript
47 * // create a DataSet
48 * var options = {};
49 * var data = new vis.DataSet(options);
50 *
51 * // add items
52 * // note that the data items can contain different properties and data formats
53 * data.add([
54 * {id: 1, text: 'item 1', date: new Date(2013, 6, 20), group: 1, first: true},
55 * {id: 2, text: 'item 2', date: '2013-06-23', group: 2},
56 * {id: 3, text: 'item 3', date: '2013-06-25', group: 2},
57 * {id: 4, text: 'item 4'}
58 * ]);
59 *
60 * // subscribe to any change in the DataSet
61 * data.on('*', function (event, properties, senderId) {
62 * console.log('event', event, properties);
63 * });
64 *
65 * // update an existing item
66 * data.update({id: 2, group: 1});
67 *
68 * // remove an item
69 * data.remove(4);
70 *
71 * // get all ids
72 * var ids = data.getIds();
73 * console.log('ids', ids);
74 *
75 * // get a specific item
76 * var item1 = data.get(1);
77 * console.log('item1', item1);
78 *
79 * // retrieve a filtered subset of the data
80 * var items = data.get({
81 * filter: function (item) {
82 * return item.group == 1;
83 * }
84 * });
85 * console.log('filtered items', items);
86 * ```
87 *
88 * @typeParam Item - Item type that may or may not have an id.
89 * @typeParam IdProp - Name of the property that contains the id.
90 */
91export declare class DataSet<Item extends PartItem<IdProp>, IdProp extends string = "id"> extends DataSetPart<Item, IdProp> implements DataInterface<Item, IdProp> {
92 /** Flush all queued calls. */
93 flush?: () => void;
94 /** @inheritDoc */
95 length: number;
96 /** @inheritDoc */
97 get idProp(): IdProp;
98 private readonly _options;
99 private readonly _data;
100 private readonly _idProp;
101 private _queue;
102 /**
103 * @param options - DataSet configuration.
104 */
105 constructor(options?: DataSetInitialOptions<IdProp>);
106 /**
107 * @param data - An initial set of items for the new instance.
108 * @param options - DataSet configuration.
109 */
110 constructor(data: Item[], options?: DataSetInitialOptions<IdProp>);
111 /**
112 * Set new options.
113 *
114 * @param options - The new options.
115 */
116 setOptions(options?: DataSetOptions): void;
117 /**
118 * Add a data item or an array with items.
119 *
120 * After the items are added to the DataSet, the DataSet will trigger an event `add`. When a `senderId` is provided, this id will be passed with the triggered event to all subscribers.
121 *
122 * ## Example
123 *
124 * ```javascript
125 * // create a DataSet
126 * const data = new vis.DataSet()
127 *
128 * // add items
129 * const ids = data.add([
130 * { id: 1, text: 'item 1' },
131 * { id: 2, text: 'item 2' },
132 * { text: 'item without an id' }
133 * ])
134 *
135 * console.log(ids) // [1, 2, '<UUIDv4>']
136 * ```
137 *
138 * @param data - Items to be added (ids will be generated if missing).
139 * @param senderId - Sender id.
140 * @returns addedIds - Array with the ids (generated if not present) of the added items.
141 * @throws When an item with the same id as any of the added items already exists.
142 */
143 add(data: Item | Item[], senderId?: Id | null): (string | number)[];
144 /**
145 * Update existing items. When an item does not exist, it will be created.
146 *
147 * @remarks
148 * The provided properties will be merged in the existing item. When an item does not exist, it will be created.
149 *
150 * After the items are updated, the DataSet will trigger an event `add` for the added items, and an event `update`. When a `senderId` is provided, this id will be passed with the triggered event to all subscribers.
151 *
152 * ## Example
153 *
154 * ```javascript
155 * // create a DataSet
156 * const data = new vis.DataSet([
157 * { id: 1, text: 'item 1' },
158 * { id: 2, text: 'item 2' },
159 * { id: 3, text: 'item 3' }
160 * ])
161 *
162 * // update items
163 * const ids = data.update([
164 * { id: 2, text: 'item 2 (updated)' },
165 * { id: 4, text: 'item 4 (new)' }
166 * ])
167 *
168 * console.log(ids) // [2, 4]
169 * ```
170 *
171 * ## Warning for TypeScript users
172 * This method may introduce partial items into the data set. Use add or updateOnly instead for better type safety.
173 * @param data - Items to be updated (if the id is already present) or added (if the id is missing).
174 * @param senderId - Sender id.
175 * @returns updatedIds - The ids of the added (these may be newly generated if there was no id in the item from the data) or updated items.
176 * @throws When the supplied data is neither an item nor an array of items.
177 */
178 update(data: DeepPartial<Item> | DeepPartial<Item>[], senderId?: Id | null): Id[];
179 /**
180 * Update existing items. When an item does not exist, an error will be thrown.
181 *
182 * @remarks
183 * The provided properties will be deeply merged into the existing item.
184 * When an item does not exist (id not present in the data set or absent), an error will be thrown and nothing will be changed.
185 *
186 * After the items are updated, the DataSet will trigger an event `update`.
187 * When a `senderId` is provided, this id will be passed with the triggered event to all subscribers.
188 *
189 * ## Example
190 *
191 * ```javascript
192 * // create a DataSet
193 * const data = new vis.DataSet([
194 * { id: 1, text: 'item 1' },
195 * { id: 2, text: 'item 2' },
196 * { id: 3, text: 'item 3' },
197 * ])
198 *
199 * // update items
200 * const ids = data.update([
201 * { id: 2, text: 'item 2 (updated)' }, // works
202 * // { id: 4, text: 'item 4 (new)' }, // would throw
203 * // { text: 'item 4 (new)' }, // would also throw
204 * ])
205 *
206 * console.log(ids) // [2]
207 * ```
208 * @param data - Updates (the id and optionally other props) to the items in this data set.
209 * @param senderId - Sender id.
210 * @returns updatedIds - The ids of the updated items.
211 * @throws When the supplied data is neither an item nor an array of items, when the ids are missing.
212 */
213 updateOnly(data: UpdateItem<Item, IdProp> | UpdateItem<Item, IdProp>[], senderId?: Id | null): Id[];
214 /** @inheritDoc */
215 get(): FullItem<Item, IdProp>[];
216 /** @inheritDoc */
217 get(options: DataInterfaceGetOptionsArray<Item>): FullItem<Item, IdProp>[];
218 /** @inheritDoc */
219 get(options: DataInterfaceGetOptionsObject<Item>): Record<Id, FullItem<Item, IdProp>>;
220 /** @inheritDoc */
221 get(options: DataInterfaceGetOptions<Item>): FullItem<Item, IdProp>[] | Record<Id, FullItem<Item, IdProp>>;
222 /** @inheritDoc */
223 get(id: Id): null | FullItem<Item, IdProp>;
224 /** @inheritDoc */
225 get(id: Id, options: DataInterfaceGetOptionsArray<Item>): null | FullItem<Item, IdProp>;
226 /** @inheritDoc */
227 get(id: Id, options: DataInterfaceGetOptionsObject<Item>): Record<Id, FullItem<Item, IdProp>>;
228 /** @inheritDoc */
229 get(id: Id, options: DataInterfaceGetOptions<Item>): null | FullItem<Item, IdProp> | Record<Id, FullItem<Item, IdProp>>;
230 /** @inheritDoc */
231 get(ids: Id[]): FullItem<Item, IdProp>[];
232 /** @inheritDoc */
233 get(ids: Id[], options: DataInterfaceGetOptionsArray<Item>): FullItem<Item, IdProp>[];
234 /** @inheritDoc */
235 get(ids: Id[], options: DataInterfaceGetOptionsObject<Item>): Record<Id, FullItem<Item, IdProp>>;
236 /** @inheritDoc */
237 get(ids: Id[], options: DataInterfaceGetOptions<Item>): FullItem<Item, IdProp>[] | Record<Id, FullItem<Item, IdProp>>;
238 /** @inheritDoc */
239 get(ids: Id | Id[], options?: DataInterfaceGetOptions<Item>): null | FullItem<Item, IdProp> | FullItem<Item, IdProp>[] | Record<Id, FullItem<Item, IdProp>>;
240 /** @inheritDoc */
241 getIds(options?: DataInterfaceGetIdsOptions<Item>): Id[];
242 /** @inheritDoc */
243 getDataSet(): DataSet<Item, IdProp>;
244 /** @inheritDoc */
245 forEach(callback: (item: Item, id: Id) => void, options?: DataInterfaceForEachOptions<Item>): void;
246 /** @inheritDoc */
247 map<T>(callback: (item: Item, id: Id) => T, options?: DataInterfaceMapOptions<Item, T>): T[];
248 private _filterFields;
249 /**
250 * Sort the provided array with items.
251 *
252 * @param items - Items to be sorted in place.
253 * @param order - A field name or custom sort function.
254 * @typeParam T - The type of the items in the items array.
255 */
256 private _sort;
257 /**
258 * Remove an item or multiple items by “reference” (only the id is used) or by id.
259 *
260 * The method ignores removal of non-existing items, and returns an array containing the ids of the items which are actually removed from the DataSet.
261 *
262 * After the items are removed, the DataSet will trigger an event `remove` for the removed items. When a `senderId` is provided, this id will be passed with the triggered event to all subscribers.
263 *
264 * ## Example
265 * ```javascript
266 * // create a DataSet
267 * const data = new vis.DataSet([
268 * { id: 1, text: 'item 1' },
269 * { id: 2, text: 'item 2' },
270 * { id: 3, text: 'item 3' }
271 * ])
272 *
273 * // remove items
274 * const ids = data.remove([2, { id: 3 }, 4])
275 *
276 * console.log(ids) // [2, 3]
277 * ```
278 *
279 * @param id - One or more items or ids of items to be removed.
280 * @param senderId - Sender id.
281 * @returns The ids of the removed items.
282 */
283 remove(id: Id | Item | (Id | Item)[], senderId?: Id | null): Id[];
284 /**
285 * Remove an item by its id or reference.
286 *
287 * @param id - Id of an item or the item itself.
288 * @returns The removed item if removed, null otherwise.
289 */
290 private _remove;
291 /**
292 * Clear the entire data set.
293 *
294 * After the items are removed, the {@link DataSet} will trigger an event `remove` for all removed items. When a `senderId` is provided, this id will be passed with the triggered event to all subscribers.
295 *
296 * @param senderId - Sender id.
297 * @returns removedIds - The ids of all removed items.
298 */
299 clear(senderId?: Id | null): Id[];
300 /**
301 * Find the item with maximum value of a specified field.
302 *
303 * @param field - Name of the property that should be searched for max value.
304 * @returns Item containing max value, or null if no items.
305 */
306 max(field: keyof Item): Item | null;
307 /**
308 * Find the item with minimum value of a specified field.
309 *
310 * @param field - Name of the property that should be searched for min value.
311 * @returns Item containing min value, or null if no items.
312 */
313 min(field: keyof Item): Item | null;
314 distinct<T extends keyof Item>(prop: T): Item[T][];
315 distinct(prop: string): unknown[];
316 /**
317 * Add a single item. Will fail when an item with the same id already exists.
318 *
319 * @param item - A new item to be added.
320 * @returns Added item's id. An id is generated when it is not present in the item.
321 */
322 private _addItem;
323 /**
324 * Update a single item: merge with existing item.
325 * Will fail when the item has no id, or when there does not exist an item with the same id.
326 *
327 * @param update - The new item
328 * @returns The id of the updated item.
329 */
330 private _updateItem;
331 /** @inheritDoc */
332 stream(ids?: Iterable<Id>): DataStream<Item>;
333 get testLeakData(): Map<Id, FullItem<Item, IdProp>>;
334 get testLeakIdProp(): IdProp;
335 get testLeakOptions(): DataSetInitialOptions<IdProp>;
336 get testLeakQueue(): Queue<this> | null;
337 set testLeakQueue(v: Queue<this> | null);
338}
339//# sourceMappingURL=data-set.d.ts.map
\No newline at end of file