@ckeditor/ckeditor5-utils 40.0.0 → 40.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -26
- package/LICENSE.md +3 -3
- package/package.json +1 -1
- package/src/abortabledebounce.d.ts +17 -0
- package/src/abortabledebounce.js +22 -0
- package/src/areconnectedthroughproperties.d.ts +11 -11
- package/src/areconnectedthroughproperties.js +73 -73
- package/src/ckeditorerror.d.ts +123 -123
- package/src/ckeditorerror.js +176 -176
- package/src/collection.d.ts +433 -427
- package/src/collection.js +583 -575
- package/src/comparearrays.d.ts +30 -30
- package/src/comparearrays.js +47 -47
- package/src/config.d.ts +163 -163
- package/src/config.js +163 -162
- package/src/count.d.ts +18 -18
- package/src/count.js +24 -24
- package/src/delay.d.ts +19 -19
- package/src/delay.js +26 -26
- package/src/diff.d.ts +31 -31
- package/src/diff.js +115 -115
- package/src/difftochanges.d.ts +59 -59
- package/src/difftochanges.js +79 -79
- package/src/dom/createelement.d.ts +57 -57
- package/src/dom/createelement.js +40 -40
- package/src/dom/emittermixin.d.ts +142 -142
- package/src/dom/emittermixin.js +239 -239
- package/src/dom/findclosestscrollableancestor.d.ts +11 -11
- package/src/dom/findclosestscrollableancestor.js +31 -31
- package/src/dom/getancestors.d.ts +17 -17
- package/src/dom/getancestors.js +27 -27
- package/src/dom/getborderwidths.d.ts +24 -24
- package/src/dom/getborderwidths.js +24 -24
- package/src/dom/getcommonancestor.d.ts +12 -12
- package/src/dom/getcommonancestor.js +25 -25
- package/src/dom/getdatafromelement.d.ts +14 -14
- package/src/dom/getdatafromelement.js +20 -20
- package/src/dom/getpositionedancestor.d.ts +10 -10
- package/src/dom/getpositionedancestor.js +22 -22
- package/src/dom/global.d.ts +32 -32
- package/src/dom/global.js +35 -35
- package/src/dom/indexof.d.ts +14 -14
- package/src/dom/indexof.js +21 -21
- package/src/dom/insertat.d.ts +15 -15
- package/src/dom/insertat.js +17 -17
- package/src/dom/iscomment.d.ts +11 -11
- package/src/dom/iscomment.js +14 -14
- package/src/dom/isnode.d.ts +11 -11
- package/src/dom/isnode.js +21 -21
- package/src/dom/isrange.d.ts +11 -11
- package/src/dom/isrange.js +13 -13
- package/src/dom/istext.d.ts +11 -11
- package/src/dom/istext.js +13 -13
- package/src/dom/isvalidattributename.d.ts +10 -10
- package/src/dom/isvalidattributename.js +22 -22
- package/src/dom/isvisible.d.ts +18 -18
- package/src/dom/isvisible.js +20 -20
- package/src/dom/iswindow.d.ts +11 -11
- package/src/dom/iswindow.js +22 -22
- package/src/dom/position.d.ts +211 -211
- package/src/dom/position.js +313 -313
- package/src/dom/rect.d.ts +195 -195
- package/src/dom/rect.js +474 -474
- package/src/dom/remove.d.ts +13 -13
- package/src/dom/remove.js +18 -18
- package/src/dom/resizeobserver.d.ts +74 -74
- package/src/dom/resizeobserver.js +126 -126
- package/src/dom/scroll.d.ts +73 -73
- package/src/dom/scroll.js +383 -383
- package/src/dom/setdatainelement.d.ts +14 -14
- package/src/dom/setdatainelement.js +20 -20
- package/src/dom/tounit.d.ts +22 -22
- package/src/dom/tounit.js +16 -16
- package/src/elementreplacer.d.ts +31 -31
- package/src/elementreplacer.js +43 -43
- package/src/emittermixin.d.ts +312 -312
- package/src/emittermixin.js +453 -453
- package/src/env.d.ts +117 -117
- package/src/env.js +122 -122
- package/src/eventinfo.d.ts +58 -58
- package/src/eventinfo.js +26 -26
- package/src/fastdiff.d.ts +112 -112
- package/src/fastdiff.js +248 -248
- package/src/first.d.ts +11 -11
- package/src/first.js +17 -17
- package/src/focustracker.d.ts +75 -75
- package/src/focustracker.js +95 -95
- package/src/index.d.ts +64 -61
- package/src/index.js +63 -60
- package/src/inserttopriorityarray.d.ts +30 -30
- package/src/inserttopriorityarray.js +21 -21
- package/src/isiterable.d.ts +14 -14
- package/src/isiterable.js +16 -16
- package/src/keyboard.d.ts +126 -126
- package/src/keyboard.js +221 -221
- package/src/keystrokehandler.d.ts +87 -87
- package/src/keystrokehandler.js +122 -122
- package/src/language.d.ts +17 -17
- package/src/language.js +19 -19
- package/src/locale.d.ts +120 -120
- package/src/locale.js +76 -76
- package/src/mapsequal.d.ts +15 -15
- package/src/mapsequal.js +27 -27
- package/src/mix.d.ts +85 -85
- package/src/mix.js +50 -50
- package/src/nth.d.ts +16 -16
- package/src/nth.js +24 -24
- package/src/objecttomap.d.ts +23 -23
- package/src/objecttomap.js +27 -27
- package/src/observablemixin.d.ts +560 -560
- package/src/observablemixin.js +580 -580
- package/src/priorities.d.ts +33 -33
- package/src/priorities.js +23 -23
- package/src/retry.d.ts +33 -0
- package/src/retry.js +47 -0
- package/src/splicearray.d.ts +26 -26
- package/src/splicearray.js +40 -40
- package/src/spy.d.ts +21 -21
- package/src/spy.js +22 -22
- package/src/toarray.d.ts +25 -25
- package/src/toarray.js +7 -7
- package/src/tomap.d.ts +19 -19
- package/src/tomap.js +29 -29
- package/src/translation-service.d.ts +168 -168
- package/src/translation-service.js +198 -198
- package/src/uid.d.ts +15 -15
- package/src/uid.js +57 -57
- package/src/unicode.d.ts +54 -54
- package/src/unicode.js +85 -85
- package/src/verifylicense.d.ts +15 -15
- package/src/verifylicense.js +87 -87
- package/src/version.d.ts +10 -10
- package/src/version.js +153 -153
- package/src/wait.d.ts +16 -0
- package/src/wait.js +29 -0
package/src/collection.d.ts
CHANGED
|
@@ -1,427 +1,433 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
-
*/
|
|
5
|
-
declare const Collection_base: {
|
|
6
|
-
new (): import("./emittermixin").Emitter;
|
|
7
|
-
prototype: import("./emittermixin").Emitter;
|
|
8
|
-
};
|
|
9
|
-
/**
|
|
10
|
-
* Collections are ordered sets of objects. Items in the collection can be retrieved by their indexes
|
|
11
|
-
* in the collection (like in an array) or by their ids.
|
|
12
|
-
*
|
|
13
|
-
* If an object without an `id` property is being added to the collection, the `id` property will be generated
|
|
14
|
-
* automatically. Note that the automatically generated id is unique only within this single collection instance.
|
|
15
|
-
*
|
|
16
|
-
* By default an item in the collection is identified by its `id` property. The name of the identifier can be
|
|
17
|
-
* configured through the constructor of the collection.
|
|
18
|
-
*
|
|
19
|
-
* @typeParam T The type of the collection element.
|
|
20
|
-
*/
|
|
21
|
-
export default class Collection<T extends Record<string, any>> extends Collection_base implements Iterable<T> {
|
|
22
|
-
/**
|
|
23
|
-
* The internal list of items in the collection.
|
|
24
|
-
*/
|
|
25
|
-
private readonly _items;
|
|
26
|
-
/**
|
|
27
|
-
* The internal map of items in the collection.
|
|
28
|
-
*/
|
|
29
|
-
private readonly _itemMap;
|
|
30
|
-
/**
|
|
31
|
-
* The name of the property which is considered to identify an item.
|
|
32
|
-
*/
|
|
33
|
-
private readonly _idProperty;
|
|
34
|
-
/**
|
|
35
|
-
* A collection instance this collection is bound to as a result
|
|
36
|
-
* of calling {@link #bindTo} method.
|
|
37
|
-
*/
|
|
38
|
-
private _bindToCollection?;
|
|
39
|
-
/**
|
|
40
|
-
* A helper mapping external items of a bound collection ({@link #bindTo})
|
|
41
|
-
* and actual items of this collection. It provides information
|
|
42
|
-
* necessary to properly remove items bound to another collection.
|
|
43
|
-
*
|
|
44
|
-
* See {@link #_bindToInternalToExternalMap}.
|
|
45
|
-
*/
|
|
46
|
-
private readonly _bindToExternalToInternalMap;
|
|
47
|
-
/**
|
|
48
|
-
* A helper mapping items of this collection to external items of a bound collection
|
|
49
|
-
* ({@link #bindTo}). It provides information necessary to manage the bindings, e.g.
|
|
50
|
-
* to avoid loops in two–way bindings.
|
|
51
|
-
*
|
|
52
|
-
* See {@link #_bindToExternalToInternalMap}.
|
|
53
|
-
*/
|
|
54
|
-
private readonly _bindToInternalToExternalMap;
|
|
55
|
-
/**
|
|
56
|
-
* Stores indexes of skipped items from bound external collection.
|
|
57
|
-
*/
|
|
58
|
-
private _skippedIndexesFromExternal;
|
|
59
|
-
/**
|
|
60
|
-
* Creates a new Collection instance.
|
|
61
|
-
*
|
|
62
|
-
* You can pass a configuration object as the argument of the constructor:
|
|
63
|
-
*
|
|
64
|
-
* ```ts
|
|
65
|
-
* const emptyCollection = new Collection<{ name: string }>( { idProperty: 'name' } );
|
|
66
|
-
* emptyCollection.add( { name: 'John' } );
|
|
67
|
-
* console.log( collection.get( 'John' ) ); // -> { name: 'John' }
|
|
68
|
-
* ```
|
|
69
|
-
*
|
|
70
|
-
* The collection is empty by default. You can add new items using the {@link #add} method:
|
|
71
|
-
*
|
|
72
|
-
* ```ts
|
|
73
|
-
* const collection = new Collection<{ id: string }>();
|
|
74
|
-
*
|
|
75
|
-
* collection.add( { id: 'John' } );
|
|
76
|
-
* console.log( collection.get( 0 ) ); // -> { id: 'John' }
|
|
77
|
-
* ```
|
|
78
|
-
*
|
|
79
|
-
* @label NO_ITEMS
|
|
80
|
-
* @param options The options object.
|
|
81
|
-
* @param options.idProperty The name of the property which is used to identify an item.
|
|
82
|
-
* Items that do not have such a property will be assigned one when added to the collection.
|
|
83
|
-
*/
|
|
84
|
-
constructor(options?: {
|
|
85
|
-
readonly idProperty?: string;
|
|
86
|
-
});
|
|
87
|
-
/**
|
|
88
|
-
* Creates a new Collection instance with specified initial items.
|
|
89
|
-
*
|
|
90
|
-
* ```ts
|
|
91
|
-
* const collection = new Collection<{ id: string }>( [ { id: 'John' }, { id: 'Mike' } ] );
|
|
92
|
-
*
|
|
93
|
-
* console.log( collection.get( 0 ) ); // -> { id: 'John' }
|
|
94
|
-
* console.log( collection.get( 1 ) ); // -> { id: 'Mike' }
|
|
95
|
-
* console.log( collection.get( 'Mike' ) ); // -> { id: 'Mike' }
|
|
96
|
-
* ```
|
|
97
|
-
*
|
|
98
|
-
* You can always pass a configuration object as the last argument of the constructor:
|
|
99
|
-
*
|
|
100
|
-
* ```ts
|
|
101
|
-
* const nonEmptyCollection = new Collection<{ name: string }>( [ { name: 'John' } ], { idProperty: 'name' } );
|
|
102
|
-
* nonEmptyCollection.add( { name: 'George' } );
|
|
103
|
-
* console.log( collection.get( 'George' ) ); // -> { name: 'George' }
|
|
104
|
-
* console.log( collection.get( 'John' ) ); // -> { name: 'John' }
|
|
105
|
-
* ```
|
|
106
|
-
*
|
|
107
|
-
* @label INITIAL_ITEMS
|
|
108
|
-
* @param initialItems The initial items of the collection.
|
|
109
|
-
* @param options The options object.
|
|
110
|
-
* @param options.idProperty The name of the property which is used to identify an item.
|
|
111
|
-
* Items that do not have such a property will be assigned one when added to the collection.
|
|
112
|
-
*/
|
|
113
|
-
constructor(initialItems: Iterable<T>, options?: {
|
|
114
|
-
readonly idProperty?: string;
|
|
115
|
-
});
|
|
116
|
-
/**
|
|
117
|
-
* The number of items available in the collection.
|
|
118
|
-
*/
|
|
119
|
-
get length(): number;
|
|
120
|
-
/**
|
|
121
|
-
* Returns the first item from the collection or null when collection is empty.
|
|
122
|
-
*/
|
|
123
|
-
get first(): T | null;
|
|
124
|
-
/**
|
|
125
|
-
* Returns the last item from the collection or null when collection is empty.
|
|
126
|
-
*/
|
|
127
|
-
get last(): T | null;
|
|
128
|
-
/**
|
|
129
|
-
* Adds an item into the collection.
|
|
130
|
-
*
|
|
131
|
-
* If the item does not have an id, then it will be automatically generated and set on the item.
|
|
132
|
-
*
|
|
133
|
-
* @param item
|
|
134
|
-
* @param index The position of the item in the collection. The item
|
|
135
|
-
* is pushed to the collection when `index` not specified.
|
|
136
|
-
* @fires add
|
|
137
|
-
* @fires change
|
|
138
|
-
*/
|
|
139
|
-
add(item: T, index?: number): this;
|
|
140
|
-
/**
|
|
141
|
-
* Adds multiple items into the collection.
|
|
142
|
-
*
|
|
143
|
-
* Any item not containing an id will get an automatically generated one.
|
|
144
|
-
*
|
|
145
|
-
* @param items
|
|
146
|
-
* @param index The position of the insertion. Items will be appended if no `index` is specified.
|
|
147
|
-
* @fires add
|
|
148
|
-
* @fires change
|
|
149
|
-
*/
|
|
150
|
-
addMany(items: Iterable<T>, index?: number): this;
|
|
151
|
-
/**
|
|
152
|
-
* Gets an item by its ID or index.
|
|
153
|
-
*
|
|
154
|
-
* @param idOrIndex The item ID or index in the collection.
|
|
155
|
-
* @returns The requested item or `null` if such item does not exist.
|
|
156
|
-
*/
|
|
157
|
-
get(idOrIndex: string | number): T | null;
|
|
158
|
-
/**
|
|
159
|
-
* Returns a Boolean indicating whether the collection contains an item.
|
|
160
|
-
*
|
|
161
|
-
* @param itemOrId The item or its ID in the collection.
|
|
162
|
-
* @returns `true` if the collection contains the item, `false` otherwise.
|
|
163
|
-
*/
|
|
164
|
-
has(itemOrId: T | string): boolean;
|
|
165
|
-
/**
|
|
166
|
-
* Gets an index of an item in the collection.
|
|
167
|
-
* When an item is not defined in the collection, the index will equal -1.
|
|
168
|
-
*
|
|
169
|
-
* @param itemOrId The item or its ID in the collection.
|
|
170
|
-
* @returns The index of a given item.
|
|
171
|
-
*/
|
|
172
|
-
getIndex(itemOrId: T | string): number;
|
|
173
|
-
/**
|
|
174
|
-
* Removes an item from the collection.
|
|
175
|
-
*
|
|
176
|
-
* @param subject The item to remove, its ID or index in the collection.
|
|
177
|
-
* @returns The removed item.
|
|
178
|
-
* @fires remove
|
|
179
|
-
* @fires change
|
|
180
|
-
*/
|
|
181
|
-
remove(subject: T | number | string): T;
|
|
182
|
-
/**
|
|
183
|
-
* Executes the callback for each item in the collection and composes an array or values returned by this callback.
|
|
184
|
-
*
|
|
185
|
-
* @typeParam U The result type of the callback.
|
|
186
|
-
* @param callback
|
|
187
|
-
* @param ctx Context in which the `callback` will be called.
|
|
188
|
-
* @returns The result of mapping.
|
|
189
|
-
*/
|
|
190
|
-
map<U>(callback: (item: T, index: number) => U, ctx?: any): Array<U>;
|
|
191
|
-
/**
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
* @param callback
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
* @
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
*
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
*
|
|
225
|
-
*
|
|
226
|
-
*
|
|
227
|
-
*
|
|
228
|
-
*
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
*
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
* source
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
*
|
|
239
|
-
*
|
|
240
|
-
* source.
|
|
241
|
-
*
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
*
|
|
247
|
-
*
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
*
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
*
|
|
256
|
-
*
|
|
257
|
-
*
|
|
258
|
-
*
|
|
259
|
-
*
|
|
260
|
-
*
|
|
261
|
-
*
|
|
262
|
-
*
|
|
263
|
-
*
|
|
264
|
-
*
|
|
265
|
-
*
|
|
266
|
-
*
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
*
|
|
270
|
-
*
|
|
271
|
-
*
|
|
272
|
-
*
|
|
273
|
-
*
|
|
274
|
-
*
|
|
275
|
-
*
|
|
276
|
-
*
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
*
|
|
282
|
-
*
|
|
283
|
-
*
|
|
284
|
-
*
|
|
285
|
-
*
|
|
286
|
-
*
|
|
287
|
-
*
|
|
288
|
-
*
|
|
289
|
-
*
|
|
290
|
-
*
|
|
291
|
-
*
|
|
292
|
-
* source
|
|
293
|
-
*
|
|
294
|
-
*
|
|
295
|
-
*
|
|
296
|
-
*
|
|
297
|
-
*
|
|
298
|
-
*
|
|
299
|
-
*
|
|
300
|
-
*
|
|
301
|
-
*
|
|
302
|
-
*
|
|
303
|
-
*
|
|
304
|
-
*
|
|
305
|
-
*
|
|
306
|
-
*
|
|
307
|
-
*
|
|
308
|
-
*
|
|
309
|
-
*
|
|
310
|
-
*
|
|
311
|
-
*
|
|
312
|
-
*
|
|
313
|
-
*
|
|
314
|
-
*
|
|
315
|
-
*
|
|
316
|
-
*
|
|
317
|
-
*
|
|
318
|
-
*
|
|
319
|
-
*
|
|
320
|
-
*
|
|
321
|
-
*
|
|
322
|
-
*
|
|
323
|
-
*
|
|
324
|
-
*
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
*
|
|
329
|
-
*
|
|
330
|
-
* @
|
|
331
|
-
*/
|
|
332
|
-
|
|
333
|
-
/**
|
|
334
|
-
*
|
|
335
|
-
*
|
|
336
|
-
*
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
*
|
|
343
|
-
*
|
|
344
|
-
*
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
* @
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
*
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
*
|
|
421
|
-
*
|
|
422
|
-
* @param
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
declare const Collection_base: {
|
|
6
|
+
new (): import("./emittermixin").Emitter;
|
|
7
|
+
prototype: import("./emittermixin").Emitter;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Collections are ordered sets of objects. Items in the collection can be retrieved by their indexes
|
|
11
|
+
* in the collection (like in an array) or by their ids.
|
|
12
|
+
*
|
|
13
|
+
* If an object without an `id` property is being added to the collection, the `id` property will be generated
|
|
14
|
+
* automatically. Note that the automatically generated id is unique only within this single collection instance.
|
|
15
|
+
*
|
|
16
|
+
* By default an item in the collection is identified by its `id` property. The name of the identifier can be
|
|
17
|
+
* configured through the constructor of the collection.
|
|
18
|
+
*
|
|
19
|
+
* @typeParam T The type of the collection element.
|
|
20
|
+
*/
|
|
21
|
+
export default class Collection<T extends Record<string, any>> extends Collection_base implements Iterable<T> {
|
|
22
|
+
/**
|
|
23
|
+
* The internal list of items in the collection.
|
|
24
|
+
*/
|
|
25
|
+
private readonly _items;
|
|
26
|
+
/**
|
|
27
|
+
* The internal map of items in the collection.
|
|
28
|
+
*/
|
|
29
|
+
private readonly _itemMap;
|
|
30
|
+
/**
|
|
31
|
+
* The name of the property which is considered to identify an item.
|
|
32
|
+
*/
|
|
33
|
+
private readonly _idProperty;
|
|
34
|
+
/**
|
|
35
|
+
* A collection instance this collection is bound to as a result
|
|
36
|
+
* of calling {@link #bindTo} method.
|
|
37
|
+
*/
|
|
38
|
+
private _bindToCollection?;
|
|
39
|
+
/**
|
|
40
|
+
* A helper mapping external items of a bound collection ({@link #bindTo})
|
|
41
|
+
* and actual items of this collection. It provides information
|
|
42
|
+
* necessary to properly remove items bound to another collection.
|
|
43
|
+
*
|
|
44
|
+
* See {@link #_bindToInternalToExternalMap}.
|
|
45
|
+
*/
|
|
46
|
+
private readonly _bindToExternalToInternalMap;
|
|
47
|
+
/**
|
|
48
|
+
* A helper mapping items of this collection to external items of a bound collection
|
|
49
|
+
* ({@link #bindTo}). It provides information necessary to manage the bindings, e.g.
|
|
50
|
+
* to avoid loops in two–way bindings.
|
|
51
|
+
*
|
|
52
|
+
* See {@link #_bindToExternalToInternalMap}.
|
|
53
|
+
*/
|
|
54
|
+
private readonly _bindToInternalToExternalMap;
|
|
55
|
+
/**
|
|
56
|
+
* Stores indexes of skipped items from bound external collection.
|
|
57
|
+
*/
|
|
58
|
+
private _skippedIndexesFromExternal;
|
|
59
|
+
/**
|
|
60
|
+
* Creates a new Collection instance.
|
|
61
|
+
*
|
|
62
|
+
* You can pass a configuration object as the argument of the constructor:
|
|
63
|
+
*
|
|
64
|
+
* ```ts
|
|
65
|
+
* const emptyCollection = new Collection<{ name: string }>( { idProperty: 'name' } );
|
|
66
|
+
* emptyCollection.add( { name: 'John' } );
|
|
67
|
+
* console.log( collection.get( 'John' ) ); // -> { name: 'John' }
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* The collection is empty by default. You can add new items using the {@link #add} method:
|
|
71
|
+
*
|
|
72
|
+
* ```ts
|
|
73
|
+
* const collection = new Collection<{ id: string }>();
|
|
74
|
+
*
|
|
75
|
+
* collection.add( { id: 'John' } );
|
|
76
|
+
* console.log( collection.get( 0 ) ); // -> { id: 'John' }
|
|
77
|
+
* ```
|
|
78
|
+
*
|
|
79
|
+
* @label NO_ITEMS
|
|
80
|
+
* @param options The options object.
|
|
81
|
+
* @param options.idProperty The name of the property which is used to identify an item.
|
|
82
|
+
* Items that do not have such a property will be assigned one when added to the collection.
|
|
83
|
+
*/
|
|
84
|
+
constructor(options?: {
|
|
85
|
+
readonly idProperty?: string;
|
|
86
|
+
});
|
|
87
|
+
/**
|
|
88
|
+
* Creates a new Collection instance with specified initial items.
|
|
89
|
+
*
|
|
90
|
+
* ```ts
|
|
91
|
+
* const collection = new Collection<{ id: string }>( [ { id: 'John' }, { id: 'Mike' } ] );
|
|
92
|
+
*
|
|
93
|
+
* console.log( collection.get( 0 ) ); // -> { id: 'John' }
|
|
94
|
+
* console.log( collection.get( 1 ) ); // -> { id: 'Mike' }
|
|
95
|
+
* console.log( collection.get( 'Mike' ) ); // -> { id: 'Mike' }
|
|
96
|
+
* ```
|
|
97
|
+
*
|
|
98
|
+
* You can always pass a configuration object as the last argument of the constructor:
|
|
99
|
+
*
|
|
100
|
+
* ```ts
|
|
101
|
+
* const nonEmptyCollection = new Collection<{ name: string }>( [ { name: 'John' } ], { idProperty: 'name' } );
|
|
102
|
+
* nonEmptyCollection.add( { name: 'George' } );
|
|
103
|
+
* console.log( collection.get( 'George' ) ); // -> { name: 'George' }
|
|
104
|
+
* console.log( collection.get( 'John' ) ); // -> { name: 'John' }
|
|
105
|
+
* ```
|
|
106
|
+
*
|
|
107
|
+
* @label INITIAL_ITEMS
|
|
108
|
+
* @param initialItems The initial items of the collection.
|
|
109
|
+
* @param options The options object.
|
|
110
|
+
* @param options.idProperty The name of the property which is used to identify an item.
|
|
111
|
+
* Items that do not have such a property will be assigned one when added to the collection.
|
|
112
|
+
*/
|
|
113
|
+
constructor(initialItems: Iterable<T>, options?: {
|
|
114
|
+
readonly idProperty?: string;
|
|
115
|
+
});
|
|
116
|
+
/**
|
|
117
|
+
* The number of items available in the collection.
|
|
118
|
+
*/
|
|
119
|
+
get length(): number;
|
|
120
|
+
/**
|
|
121
|
+
* Returns the first item from the collection or null when collection is empty.
|
|
122
|
+
*/
|
|
123
|
+
get first(): T | null;
|
|
124
|
+
/**
|
|
125
|
+
* Returns the last item from the collection or null when collection is empty.
|
|
126
|
+
*/
|
|
127
|
+
get last(): T | null;
|
|
128
|
+
/**
|
|
129
|
+
* Adds an item into the collection.
|
|
130
|
+
*
|
|
131
|
+
* If the item does not have an id, then it will be automatically generated and set on the item.
|
|
132
|
+
*
|
|
133
|
+
* @param item
|
|
134
|
+
* @param index The position of the item in the collection. The item
|
|
135
|
+
* is pushed to the collection when `index` not specified.
|
|
136
|
+
* @fires add
|
|
137
|
+
* @fires change
|
|
138
|
+
*/
|
|
139
|
+
add(item: T, index?: number): this;
|
|
140
|
+
/**
|
|
141
|
+
* Adds multiple items into the collection.
|
|
142
|
+
*
|
|
143
|
+
* Any item not containing an id will get an automatically generated one.
|
|
144
|
+
*
|
|
145
|
+
* @param items
|
|
146
|
+
* @param index The position of the insertion. Items will be appended if no `index` is specified.
|
|
147
|
+
* @fires add
|
|
148
|
+
* @fires change
|
|
149
|
+
*/
|
|
150
|
+
addMany(items: Iterable<T>, index?: number): this;
|
|
151
|
+
/**
|
|
152
|
+
* Gets an item by its ID or index.
|
|
153
|
+
*
|
|
154
|
+
* @param idOrIndex The item ID or index in the collection.
|
|
155
|
+
* @returns The requested item or `null` if such item does not exist.
|
|
156
|
+
*/
|
|
157
|
+
get(idOrIndex: string | number): T | null;
|
|
158
|
+
/**
|
|
159
|
+
* Returns a Boolean indicating whether the collection contains an item.
|
|
160
|
+
*
|
|
161
|
+
* @param itemOrId The item or its ID in the collection.
|
|
162
|
+
* @returns `true` if the collection contains the item, `false` otherwise.
|
|
163
|
+
*/
|
|
164
|
+
has(itemOrId: T | string): boolean;
|
|
165
|
+
/**
|
|
166
|
+
* Gets an index of an item in the collection.
|
|
167
|
+
* When an item is not defined in the collection, the index will equal -1.
|
|
168
|
+
*
|
|
169
|
+
* @param itemOrId The item or its ID in the collection.
|
|
170
|
+
* @returns The index of a given item.
|
|
171
|
+
*/
|
|
172
|
+
getIndex(itemOrId: T | string): number;
|
|
173
|
+
/**
|
|
174
|
+
* Removes an item from the collection.
|
|
175
|
+
*
|
|
176
|
+
* @param subject The item to remove, its ID or index in the collection.
|
|
177
|
+
* @returns The removed item.
|
|
178
|
+
* @fires remove
|
|
179
|
+
* @fires change
|
|
180
|
+
*/
|
|
181
|
+
remove(subject: T | number | string): T;
|
|
182
|
+
/**
|
|
183
|
+
* Executes the callback for each item in the collection and composes an array or values returned by this callback.
|
|
184
|
+
*
|
|
185
|
+
* @typeParam U The result type of the callback.
|
|
186
|
+
* @param callback
|
|
187
|
+
* @param ctx Context in which the `callback` will be called.
|
|
188
|
+
* @returns The result of mapping.
|
|
189
|
+
*/
|
|
190
|
+
map<U>(callback: (item: T, index: number) => U, ctx?: any): Array<U>;
|
|
191
|
+
/**
|
|
192
|
+
* Performs the specified action for each item in the collection.
|
|
193
|
+
*
|
|
194
|
+
* @param ctx Context in which the `callback` will be called.
|
|
195
|
+
*/
|
|
196
|
+
forEach(callback: (item: T, index: number) => unknown, ctx?: any): void;
|
|
197
|
+
/**
|
|
198
|
+
* Finds the first item in the collection for which the `callback` returns a true value.
|
|
199
|
+
*
|
|
200
|
+
* @param callback
|
|
201
|
+
* @param ctx Context in which the `callback` will be called.
|
|
202
|
+
* @returns The item for which `callback` returned a true value.
|
|
203
|
+
*/
|
|
204
|
+
find(callback: (item: T, index: number) => boolean, ctx?: any): T | undefined;
|
|
205
|
+
/**
|
|
206
|
+
* Returns an array with items for which the `callback` returned a true value.
|
|
207
|
+
*
|
|
208
|
+
* @param callback
|
|
209
|
+
* @param ctx Context in which the `callback` will be called.
|
|
210
|
+
* @returns The array with matching items.
|
|
211
|
+
*/
|
|
212
|
+
filter(callback: (item: T, index: number) => boolean, ctx?: any): Array<T>;
|
|
213
|
+
/**
|
|
214
|
+
* Removes all items from the collection and destroys the binding created using
|
|
215
|
+
* {@link #bindTo}.
|
|
216
|
+
*
|
|
217
|
+
* @fires remove
|
|
218
|
+
* @fires change
|
|
219
|
+
*/
|
|
220
|
+
clear(): void;
|
|
221
|
+
/**
|
|
222
|
+
* Binds and synchronizes the collection with another one.
|
|
223
|
+
*
|
|
224
|
+
* The binding can be a simple factory:
|
|
225
|
+
*
|
|
226
|
+
* ```ts
|
|
227
|
+
* class FactoryClass {
|
|
228
|
+
* public label: string;
|
|
229
|
+
*
|
|
230
|
+
* constructor( data: { label: string } ) {
|
|
231
|
+
* this.label = data.label;
|
|
232
|
+
* }
|
|
233
|
+
* }
|
|
234
|
+
*
|
|
235
|
+
* const source = new Collection<{ label: string }>( { idProperty: 'label' } );
|
|
236
|
+
* const target = new Collection<FactoryClass>();
|
|
237
|
+
*
|
|
238
|
+
* target.bindTo( source ).as( FactoryClass );
|
|
239
|
+
*
|
|
240
|
+
* source.add( { label: 'foo' } );
|
|
241
|
+
* source.add( { label: 'bar' } );
|
|
242
|
+
*
|
|
243
|
+
* console.log( target.length ); // 2
|
|
244
|
+
* console.log( target.get( 1 ).label ); // 'bar'
|
|
245
|
+
*
|
|
246
|
+
* source.remove( 0 );
|
|
247
|
+
* console.log( target.length ); // 1
|
|
248
|
+
* console.log( target.get( 0 ).label ); // 'bar'
|
|
249
|
+
* ```
|
|
250
|
+
*
|
|
251
|
+
* or the factory driven by a custom callback:
|
|
252
|
+
*
|
|
253
|
+
* ```ts
|
|
254
|
+
* class FooClass {
|
|
255
|
+
* public label: string;
|
|
256
|
+
*
|
|
257
|
+
* constructor( data: { label: string } ) {
|
|
258
|
+
* this.label = data.label;
|
|
259
|
+
* }
|
|
260
|
+
* }
|
|
261
|
+
*
|
|
262
|
+
* class BarClass {
|
|
263
|
+
* public label: string;
|
|
264
|
+
*
|
|
265
|
+
* constructor( data: { label: string } ) {
|
|
266
|
+
* this.label = data.label;
|
|
267
|
+
* }
|
|
268
|
+
* }
|
|
269
|
+
*
|
|
270
|
+
* const source = new Collection<{ label: string }>( { idProperty: 'label' } );
|
|
271
|
+
* const target = new Collection<FooClass | BarClass>();
|
|
272
|
+
*
|
|
273
|
+
* target.bindTo( source ).using( ( item ) => {
|
|
274
|
+
* if ( item.label == 'foo' ) {
|
|
275
|
+
* return new FooClass( item );
|
|
276
|
+
* } else {
|
|
277
|
+
* return new BarClass( item );
|
|
278
|
+
* }
|
|
279
|
+
* } );
|
|
280
|
+
*
|
|
281
|
+
* source.add( { label: 'foo' } );
|
|
282
|
+
* source.add( { label: 'bar' } );
|
|
283
|
+
*
|
|
284
|
+
* console.log( target.length ); // 2
|
|
285
|
+
* console.log( target.get( 0 ) instanceof FooClass ); // true
|
|
286
|
+
* console.log( target.get( 1 ) instanceof BarClass ); // true
|
|
287
|
+
* ```
|
|
288
|
+
*
|
|
289
|
+
* or the factory out of property name:
|
|
290
|
+
*
|
|
291
|
+
* ```ts
|
|
292
|
+
* const source = new Collection<{ nested: { value: string } }>();
|
|
293
|
+
* const target = new Collection<{ value: string }>();
|
|
294
|
+
*
|
|
295
|
+
* target.bindTo( source ).using( 'nested' );
|
|
296
|
+
*
|
|
297
|
+
* source.add( { nested: { value: 'foo' } } );
|
|
298
|
+
* source.add( { nested: { value: 'bar' } } );
|
|
299
|
+
*
|
|
300
|
+
* console.log( target.length ); // 2
|
|
301
|
+
* console.log( target.get( 0 ).value ); // 'foo'
|
|
302
|
+
* console.log( target.get( 1 ).value ); // 'bar'
|
|
303
|
+
* ```
|
|
304
|
+
*
|
|
305
|
+
* It's possible to skip specified items by returning null value:
|
|
306
|
+
*
|
|
307
|
+
* ```ts
|
|
308
|
+
* const source = new Collection<{ hidden: boolean }>();
|
|
309
|
+
* const target = new Collection<{ hidden: boolean }>();
|
|
310
|
+
*
|
|
311
|
+
* target.bindTo( source ).using( item => {
|
|
312
|
+
* if ( item.hidden ) {
|
|
313
|
+
* return null;
|
|
314
|
+
* }
|
|
315
|
+
*
|
|
316
|
+
* return item;
|
|
317
|
+
* } );
|
|
318
|
+
*
|
|
319
|
+
* source.add( { hidden: true } );
|
|
320
|
+
* source.add( { hidden: false } );
|
|
321
|
+
*
|
|
322
|
+
* console.log( source.length ); // 2
|
|
323
|
+
* console.log( target.length ); // 1
|
|
324
|
+
* ```
|
|
325
|
+
*
|
|
326
|
+
* **Note**: {@link #clear} can be used to break the binding.
|
|
327
|
+
*
|
|
328
|
+
* @typeParam S The type of `externalCollection` element.
|
|
329
|
+
* @param externalCollection A collection to be bound.
|
|
330
|
+
* @returns The binding chain object.
|
|
331
|
+
*/
|
|
332
|
+
bindTo<S extends Record<string, any>>(externalCollection: Collection<S>): CollectionBindToChain<S, T>;
|
|
333
|
+
/**
|
|
334
|
+
* Finalizes and activates a binding initiated by {@link #bindTo}.
|
|
335
|
+
*
|
|
336
|
+
* @param factory A function which produces collection items.
|
|
337
|
+
*/
|
|
338
|
+
private _setUpBindToBinding;
|
|
339
|
+
/**
|
|
340
|
+
* Returns an unique id property for a given `item`.
|
|
341
|
+
*
|
|
342
|
+
* The method will generate new id and assign it to the `item` if it doesn't have any.
|
|
343
|
+
*
|
|
344
|
+
* @param item Item to be added.
|
|
345
|
+
*/
|
|
346
|
+
private _getItemIdBeforeAdding;
|
|
347
|
+
/**
|
|
348
|
+
* Core {@link #remove} method implementation shared in other functions.
|
|
349
|
+
*
|
|
350
|
+
* In contrast this method **does not** fire the {@link #event:change} event.
|
|
351
|
+
*
|
|
352
|
+
* @param subject The item to remove, its id or index in the collection.
|
|
353
|
+
* @returns Returns an array with the removed item and its index.
|
|
354
|
+
* @fires remove
|
|
355
|
+
*/
|
|
356
|
+
private _remove;
|
|
357
|
+
/**
|
|
358
|
+
* Iterable interface.
|
|
359
|
+
*/
|
|
360
|
+
[Symbol.iterator](): Iterator<T>;
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Fired when an item is added to the collection.
|
|
364
|
+
*
|
|
365
|
+
* @eventName ~Collection#add
|
|
366
|
+
* @param item The added item.
|
|
367
|
+
* @param index An index where the addition occurred.
|
|
368
|
+
*/
|
|
369
|
+
export type CollectionAddEvent<T = any> = {
|
|
370
|
+
name: 'add';
|
|
371
|
+
args: [item: T, index: number];
|
|
372
|
+
};
|
|
373
|
+
/**
|
|
374
|
+
* Fired when the collection was changed due to adding or removing items.
|
|
375
|
+
*
|
|
376
|
+
* @eventName ~Collection#change
|
|
377
|
+
* @param data Changed items.
|
|
378
|
+
*/
|
|
379
|
+
export type CollectionChangeEvent<T = any> = {
|
|
380
|
+
name: 'change';
|
|
381
|
+
args: [data: CollectionChangeEventData<T>];
|
|
382
|
+
};
|
|
383
|
+
/**
|
|
384
|
+
* A structure describing the {@link ~Collection#event:change `Collection#change`} event.
|
|
385
|
+
*/
|
|
386
|
+
export type CollectionChangeEventData<T = any> = {
|
|
387
|
+
/**
|
|
388
|
+
* A list of added items.
|
|
389
|
+
*/
|
|
390
|
+
added: Iterable<T>;
|
|
391
|
+
/**
|
|
392
|
+
* A list of removed items.
|
|
393
|
+
*/
|
|
394
|
+
removed: Iterable<T>;
|
|
395
|
+
/**
|
|
396
|
+
* An index where the addition or removal occurred.
|
|
397
|
+
*/
|
|
398
|
+
index: number;
|
|
399
|
+
};
|
|
400
|
+
/**
|
|
401
|
+
* Fired when an item is removed from the collection.
|
|
402
|
+
*
|
|
403
|
+
* @eventName ~Collection#remove
|
|
404
|
+
* @param item The removed item.
|
|
405
|
+
* @param index Index from which item was removed.
|
|
406
|
+
*/
|
|
407
|
+
export type CollectionRemoveEvent<T = any> = {
|
|
408
|
+
name: 'remove';
|
|
409
|
+
args: [item: T, index: number];
|
|
410
|
+
};
|
|
411
|
+
/**
|
|
412
|
+
* An object returned by the {@link module:utils/collection~Collection#bindTo `bindTo()`} method
|
|
413
|
+
* providing functions that specify the type of the binding.
|
|
414
|
+
*
|
|
415
|
+
* See the {@link module:utils/collection~Collection#bindTo `bindTo()`} documentation for examples.
|
|
416
|
+
*/
|
|
417
|
+
export interface CollectionBindToChain<S, T> {
|
|
418
|
+
/**
|
|
419
|
+
* Creates the class factory binding in which items of the source collection are passed to
|
|
420
|
+
* the constructor of the specified class.
|
|
421
|
+
*
|
|
422
|
+
* @param Class The class constructor used to create instances in the factory.
|
|
423
|
+
*/
|
|
424
|
+
as(Class: new (item: S) => T): void;
|
|
425
|
+
/**
|
|
426
|
+
* Creates a callback or a property binding.
|
|
427
|
+
*
|
|
428
|
+
* @param callbackOrProperty When the function is passed, it should return
|
|
429
|
+
* the collection items. When the string is provided, the property value is used to create the bound collection items.
|
|
430
|
+
*/
|
|
431
|
+
using(callbackOrProperty: keyof S | ((item: S) => T | null)): void;
|
|
432
|
+
}
|
|
433
|
+
export {};
|