@ckeditor/ckeditor5-utils 35.3.2 → 35.4.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/package.json +5 -5
- package/src/areconnectedthroughproperties.js +5 -7
- package/src/ckeditorerror.js +51 -70
- package/src/collection.js +106 -148
- package/src/comparearrays.js +10 -8
- package/src/config.js +29 -83
- package/src/count.js +5 -3
- package/src/diff.js +7 -5
- package/src/difftochanges.js +17 -14
- package/src/dom/createelement.js +11 -9
- package/src/dom/emittermixin.js +43 -84
- package/src/dom/getancestors.js +2 -2
- package/src/dom/getborderwidths.js +2 -2
- package/src/dom/getcommonancestor.js +3 -3
- package/src/dom/getdatafromelement.js +2 -2
- package/src/dom/getpositionedancestor.js +1 -2
- package/src/dom/global.js +8 -10
- package/src/dom/indexof.js +2 -2
- package/src/dom/insertat.js +3 -3
- package/src/dom/iscomment.js +0 -3
- package/src/dom/isnode.js +0 -3
- package/src/dom/isrange.js +0 -3
- package/src/dom/istext.js +0 -3
- package/src/dom/isvisible.js +0 -3
- package/src/dom/iswindow.js +0 -3
- package/src/dom/position.js +110 -133
- package/src/dom/rect.js +42 -52
- package/src/dom/remove.js +1 -1
- package/src/dom/resizeobserver.js +10 -35
- package/src/dom/scroll.js +85 -91
- package/src/dom/setdatainelement.js +2 -2
- package/src/dom/tounit.js +1 -10
- package/src/elementreplacer.js +2 -2
- package/src/emittermixin.js +48 -48
- package/src/env.js +14 -75
- package/src/eventinfo.js +2 -2
- package/src/fastdiff.js +115 -96
- package/src/first.js +0 -3
- package/src/focustracker.js +10 -18
- package/src/index.js +17 -0
- package/src/inserttopriorityarray.js +2 -2
- package/src/isiterable.js +2 -2
- package/src/keyboard.js +20 -21
- package/src/keystrokehandler.js +26 -24
- package/src/language.js +1 -2
- package/src/locale.js +11 -14
- package/src/mapsequal.js +3 -3
- package/src/mix.js +15 -13
- package/src/nth.js +0 -4
- package/src/objecttomap.js +6 -4
- package/src/observablemixin.js +126 -150
- package/src/priorities.js +0 -9
- package/src/splicearray.js +12 -11
- package/src/spy.js +1 -1
- package/src/tomap.js +7 -5
- package/src/translation-service.js +70 -52
- package/src/uid.js +5 -3
- package/src/unicode.js +9 -15
- package/src/version.js +32 -26
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ckeditor/ckeditor5-utils",
|
|
3
|
-
"version": "35.
|
|
3
|
+
"version": "35.4.0",
|
|
4
4
|
"description": "Miscellaneous utilities used by CKEditor 5.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ckeditor",
|
|
@@ -14,10 +14,10 @@
|
|
|
14
14
|
"lodash-es": "^4.17.15"
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
|
-
"@ckeditor/ckeditor5-build-classic": "^35.
|
|
18
|
-
"@ckeditor/ckeditor5-editor-classic": "^35.
|
|
19
|
-
"@ckeditor/ckeditor5-core": "^35.
|
|
20
|
-
"@ckeditor/ckeditor5-engine": "^35.
|
|
17
|
+
"@ckeditor/ckeditor5-build-classic": "^35.4.0",
|
|
18
|
+
"@ckeditor/ckeditor5-editor-classic": "^35.4.0",
|
|
19
|
+
"@ckeditor/ckeditor5-core": "^35.4.0",
|
|
20
|
+
"@ckeditor/ckeditor5-engine": "^35.4.0",
|
|
21
21
|
"@types/lodash-es": "^4.17.6",
|
|
22
22
|
"typescript": "^4.8.4"
|
|
23
23
|
},
|
|
@@ -8,10 +8,6 @@
|
|
|
8
8
|
/* globals EventTarget, Event */
|
|
9
9
|
/**
|
|
10
10
|
* Traverses both structures to find out whether there is a reference that is shared between both structures.
|
|
11
|
-
*
|
|
12
|
-
* @param {Object|Array} obj1
|
|
13
|
-
* @param {Object|Array} obj2
|
|
14
|
-
* @returns {Boolean}
|
|
15
11
|
*/
|
|
16
12
|
export default function areConnectedThroughProperties(obj1, obj2) {
|
|
17
13
|
if (obj1 === obj2 && isObject(obj1)) {
|
|
@@ -26,9 +22,11 @@ export default function areConnectedThroughProperties(obj1, obj2) {
|
|
|
26
22
|
}
|
|
27
23
|
return false;
|
|
28
24
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Traverses JS structure and stores all sub-nodes, including the head node.
|
|
27
|
+
* It walks into each iterable structures with the `try catch` block to omit errors that might be thrown during
|
|
28
|
+
* tree walking. All primitives, functions and built-ins are skipped.
|
|
29
|
+
*/
|
|
32
30
|
function getSubNodes(head) {
|
|
33
31
|
const nodes = [head];
|
|
34
32
|
// Nodes are stored to prevent infinite looping.
|
package/src/ckeditorerror.js
CHANGED
|
@@ -24,57 +24,43 @@ export const DOCUMENTATION_URL = 'https://ckeditor.com/docs/ckeditor5/latest/sup
|
|
|
24
24
|
* {@link module:utils/ckeditorerror~logError `logError()`}
|
|
25
25
|
* to improve developers experience and let them see the a working editor as soon as possible.
|
|
26
26
|
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
27
|
+
* ```ts
|
|
28
|
+
* /**
|
|
29
|
+
* * Error thrown when a plugin cannot be loaded due to JavaScript errors, lack of plugins with a given name, etc.
|
|
30
|
+
* *
|
|
31
|
+
* * @error plugin-load
|
|
32
|
+
* * @param pluginName The name of the plugin that could not be loaded.
|
|
33
|
+
* * @param moduleName The name of the module which tried to load this plugin.
|
|
34
|
+
* *\/
|
|
35
|
+
* throw new CKEditorError( 'plugin-load', {
|
|
36
|
+
* pluginName: 'foo',
|
|
37
|
+
* moduleName: 'bar'
|
|
38
|
+
* } );
|
|
39
|
+
* ```
|
|
40
40
|
*/
|
|
41
41
|
export default class CKEditorError extends Error {
|
|
42
42
|
/**
|
|
43
43
|
* Creates an instance of the CKEditorError class.
|
|
44
44
|
*
|
|
45
|
-
* @param
|
|
45
|
+
* @param errorName The error id in an `error-name` format. A link to this error documentation page will be added
|
|
46
46
|
* to the thrown error's `message`.
|
|
47
|
-
* @param
|
|
47
|
+
* @param context A context of the error by which the {@link module:watchdog/watchdog~Watchdog watchdog}
|
|
48
48
|
* is able to determine which editor crashed. It should be an editor instance or a property connected to it. It can be also
|
|
49
49
|
* a `null` value if the editor should not be restarted in case of the error (e.g. during the editor initialization).
|
|
50
50
|
* The error context should be checked using the `areConnectedThroughProperties( editor, context )` utility
|
|
51
51
|
* to check if the object works as the context.
|
|
52
|
-
* @param
|
|
52
|
+
* @param data Additional data describing the error. A stringified version of this object
|
|
53
53
|
* will be appended to the error message, so the data are quickly visible in the console. The original
|
|
54
54
|
* data object will also be later available under the {@link #data} property.
|
|
55
55
|
*/
|
|
56
56
|
constructor(errorName, context, data) {
|
|
57
57
|
super(getErrorMessage(errorName, data));
|
|
58
|
-
/**
|
|
59
|
-
* @type {String}
|
|
60
|
-
*/
|
|
61
58
|
this.name = 'CKEditorError';
|
|
62
|
-
/**
|
|
63
|
-
* A context of the error by which the Watchdog is able to determine which editor crashed.
|
|
64
|
-
*
|
|
65
|
-
* @type {Object|null}
|
|
66
|
-
*/
|
|
67
59
|
this.context = context;
|
|
68
|
-
/**
|
|
69
|
-
* The additional error data passed to the constructor. Undefined if none was passed.
|
|
70
|
-
*
|
|
71
|
-
* @type {Object|undefined}
|
|
72
|
-
*/
|
|
73
60
|
this.data = data;
|
|
74
61
|
}
|
|
75
62
|
/**
|
|
76
63
|
* Checks if the error is of the `CKEditorError` type.
|
|
77
|
-
* @returns {Boolean}
|
|
78
64
|
*/
|
|
79
65
|
is(type) {
|
|
80
66
|
return type === 'CKEditorError';
|
|
@@ -84,9 +70,8 @@ export default class CKEditorError extends Error {
|
|
|
84
70
|
* It is useful when combined with the {@link module:watchdog/watchdog~Watchdog} feature, which can restart the editor in case
|
|
85
71
|
* of a {@link module:utils/ckeditorerror~CKEditorError} error.
|
|
86
72
|
*
|
|
87
|
-
* @
|
|
88
|
-
* @param
|
|
89
|
-
* @param {Object} context An object connected through properties with the editor instance. This context will be used
|
|
73
|
+
* @param err The error to rethrow.
|
|
74
|
+
* @param context An object connected through properties with the editor instance. This context will be used
|
|
90
75
|
* by the watchdog to verify which editor should be restarted.
|
|
91
76
|
*/
|
|
92
77
|
static rethrowUnexpectedError(err, context) {
|
|
@@ -113,20 +98,22 @@ export default class CKEditorError extends Error {
|
|
|
113
98
|
* Logs a warning to the console with a properly formatted message and adds a link to the documentation.
|
|
114
99
|
* Use whenever you want to log a warning to the console.
|
|
115
100
|
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
101
|
+
* ```ts
|
|
102
|
+
* /**
|
|
103
|
+
* * There was a problem processing the configuration of the toolbar. The item with the given
|
|
104
|
+
* * name does not exist, so it was omitted when rendering the toolbar.
|
|
105
|
+
* *
|
|
106
|
+
* * @error toolbarview-item-unavailable
|
|
107
|
+
* * @param {String} name The name of the component.
|
|
108
|
+
* *\/
|
|
109
|
+
* logWarning( 'toolbarview-item-unavailable', { name } );
|
|
110
|
+
* ```
|
|
124
111
|
*
|
|
125
112
|
* See also {@link module:utils/ckeditorerror~CKEditorError} for an explanation when to throw an error and when to log
|
|
126
113
|
* a warning or an error to the console.
|
|
127
114
|
*
|
|
128
|
-
* @param
|
|
129
|
-
* @param
|
|
115
|
+
* @param errorName The error name to be logged.
|
|
116
|
+
* @param data Additional data to be logged.
|
|
130
117
|
*/
|
|
131
118
|
export function logWarning(errorName, data) {
|
|
132
119
|
console.warn(...formatConsoleArguments(errorName, data));
|
|
@@ -135,39 +122,36 @@ export function logWarning(errorName, data) {
|
|
|
135
122
|
* Logs an error to the console with a properly formatted message and adds a link to the documentation.
|
|
136
123
|
* Use whenever you want to log an error to the console.
|
|
137
124
|
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
*
|
|
125
|
+
* ```ts
|
|
126
|
+
* /**
|
|
127
|
+
* * There was a problem processing the configuration of the toolbar. The item with the given
|
|
128
|
+
* * name does not exist, so it was omitted when rendering the toolbar.
|
|
129
|
+
* *
|
|
130
|
+
* * @error toolbarview-item-unavailable
|
|
131
|
+
* * @param {String} name The name of the component.
|
|
132
|
+
* *\/
|
|
133
|
+
* logError( 'toolbarview-item-unavailable', { name } );
|
|
134
|
+
* ```
|
|
146
135
|
*
|
|
147
136
|
* **Note**: In most cases logging a warning using {@link module:utils/ckeditorerror~logWarning} is enough.
|
|
148
137
|
*
|
|
149
138
|
* See also {@link module:utils/ckeditorerror~CKEditorError} for an explanation when to use each method.
|
|
150
139
|
*
|
|
151
|
-
* @param
|
|
152
|
-
* @param
|
|
140
|
+
* @param errorName The error name to be logged.
|
|
141
|
+
* @param data Additional data to be logged.
|
|
153
142
|
*/
|
|
154
143
|
export function logError(errorName, data) {
|
|
155
144
|
console.error(...formatConsoleArguments(errorName, data));
|
|
156
145
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
// @param {String} errorName
|
|
161
|
-
// @returns {string}
|
|
146
|
+
/**
|
|
147
|
+
* Returns formatted link to documentation message.
|
|
148
|
+
*/
|
|
162
149
|
function getLinkToDocumentationMessage(errorName) {
|
|
163
150
|
return `\nRead more: ${DOCUMENTATION_URL}#error-${errorName}`;
|
|
164
151
|
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
// @param {String} errorName
|
|
169
|
-
// @param {Object} [data]
|
|
170
|
-
// @returns {string}
|
|
152
|
+
/**
|
|
153
|
+
* Returns formatted error message.
|
|
154
|
+
*/
|
|
171
155
|
function getErrorMessage(errorName, data) {
|
|
172
156
|
const processedObjects = new WeakSet();
|
|
173
157
|
const circularReferencesReplacer = (key, value) => {
|
|
@@ -183,12 +167,9 @@ function getErrorMessage(errorName, data) {
|
|
|
183
167
|
const documentationLink = getLinkToDocumentationMessage(errorName);
|
|
184
168
|
return errorName + stringifiedData + documentationLink;
|
|
185
169
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
// @param {String} errorName
|
|
190
|
-
// @param {Object} [data]
|
|
191
|
-
// @returns {Array}
|
|
170
|
+
/**
|
|
171
|
+
* Returns formatted console error arguments.
|
|
172
|
+
*/
|
|
192
173
|
function formatConsoleArguments(errorName, data) {
|
|
193
174
|
const documentationMessage = getLinkToDocumentationMessage(errorName);
|
|
194
175
|
return data ? [errorName, data, documentationMessage] : [errorName, documentationMessage];
|
package/src/collection.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
/**
|
|
6
6
|
* @module utils/collection
|
|
7
7
|
*/
|
|
8
|
-
import
|
|
8
|
+
import EmitterMixin from './emittermixin';
|
|
9
9
|
import CKEditorError from './ckeditorerror';
|
|
10
10
|
import uid from './uid';
|
|
11
11
|
import isIterable from './isiterable';
|
|
@@ -19,45 +19,9 @@ import isIterable from './isiterable';
|
|
|
19
19
|
* By default an item in the collection is identified by its `id` property. The name of the identifier can be
|
|
20
20
|
* configured through the constructor of the collection.
|
|
21
21
|
*
|
|
22
|
-
* @
|
|
22
|
+
* @typeParam T The type of the collection element.
|
|
23
23
|
*/
|
|
24
|
-
export default class Collection extends
|
|
25
|
-
/**
|
|
26
|
-
* Creates a new Collection instance.
|
|
27
|
-
*
|
|
28
|
-
* You can provide an iterable of initial items the collection will be created with:
|
|
29
|
-
*
|
|
30
|
-
* const collection = new Collection( [ { id: 'John' }, { id: 'Mike' } ] );
|
|
31
|
-
*
|
|
32
|
-
* console.log( collection.get( 0 ) ); // -> { id: 'John' }
|
|
33
|
-
* console.log( collection.get( 1 ) ); // -> { id: 'Mike' }
|
|
34
|
-
* console.log( collection.get( 'Mike' ) ); // -> { id: 'Mike' }
|
|
35
|
-
*
|
|
36
|
-
* Or you can first create a collection and then add new items using the {@link #add} method:
|
|
37
|
-
*
|
|
38
|
-
* const collection = new Collection();
|
|
39
|
-
*
|
|
40
|
-
* collection.add( { id: 'John' } );
|
|
41
|
-
* console.log( collection.get( 0 ) ); // -> { id: 'John' }
|
|
42
|
-
*
|
|
43
|
-
* Whatever option you choose, you can always pass a configuration object as the last argument
|
|
44
|
-
* of the constructor:
|
|
45
|
-
*
|
|
46
|
-
* const emptyCollection = new Collection( { idProperty: 'name' } );
|
|
47
|
-
* emptyCollection.add( { name: 'John' } );
|
|
48
|
-
* console.log( collection.get( 'John' ) ); // -> { name: 'John' }
|
|
49
|
-
*
|
|
50
|
-
* const nonEmptyCollection = new Collection( [ { name: 'John' } ], { idProperty: 'name' } );
|
|
51
|
-
* nonEmptyCollection.add( { name: 'George' } );
|
|
52
|
-
* console.log( collection.get( 'George' ) ); // -> { name: 'George' }
|
|
53
|
-
* console.log( collection.get( 'John' ) ); // -> { name: 'John' }
|
|
54
|
-
*
|
|
55
|
-
* @param {Iterable.<Object>|Object} [initialItemsOrOptions] The initial items of the collection or
|
|
56
|
-
* the options object.
|
|
57
|
-
* @param {Object} [options={}] The options object, when the first argument is an array of initial items.
|
|
58
|
-
* @param {String} [options.idProperty='id'] The name of the property which is used to identify an item.
|
|
59
|
-
* Items that do not have such a property will be assigned one when added to the collection.
|
|
60
|
-
*/
|
|
24
|
+
export default class Collection extends EmitterMixin() {
|
|
61
25
|
constructor(initialItemsOrOptions = {}, options = {}) {
|
|
62
26
|
super();
|
|
63
27
|
const hasInitialItems = isIterable(initialItemsOrOptions);
|
|
@@ -80,24 +44,18 @@ export default class Collection extends Emitter {
|
|
|
80
44
|
}
|
|
81
45
|
/**
|
|
82
46
|
* The number of items available in the collection.
|
|
83
|
-
*
|
|
84
|
-
* @member {Number} #length
|
|
85
47
|
*/
|
|
86
48
|
get length() {
|
|
87
49
|
return this._items.length;
|
|
88
50
|
}
|
|
89
51
|
/**
|
|
90
52
|
* Returns the first item from the collection or null when collection is empty.
|
|
91
|
-
*
|
|
92
|
-
* @returns {Object|null} The first item or `null` if collection is empty.
|
|
93
53
|
*/
|
|
94
54
|
get first() {
|
|
95
55
|
return this._items[0] || null;
|
|
96
56
|
}
|
|
97
57
|
/**
|
|
98
58
|
* Returns the last item from the collection or null when collection is empty.
|
|
99
|
-
*
|
|
100
|
-
* @returns {Object|null} The last item or `null` if collection is empty.
|
|
101
59
|
*/
|
|
102
60
|
get last() {
|
|
103
61
|
return this._items[this.length - 1] || null;
|
|
@@ -107,9 +65,8 @@ export default class Collection extends Emitter {
|
|
|
107
65
|
*
|
|
108
66
|
* If the item does not have an id, then it will be automatically generated and set on the item.
|
|
109
67
|
*
|
|
110
|
-
* @
|
|
111
|
-
* @param
|
|
112
|
-
* @param {Number} [index] The position of the item in the collection. The item
|
|
68
|
+
* @param item
|
|
69
|
+
* @param index The position of the item in the collection. The item
|
|
113
70
|
* is pushed to the collection when `index` not specified.
|
|
114
71
|
* @fires add
|
|
115
72
|
* @fires change
|
|
@@ -122,9 +79,8 @@ export default class Collection extends Emitter {
|
|
|
122
79
|
*
|
|
123
80
|
* Any item not containing an id will get an automatically generated one.
|
|
124
81
|
*
|
|
125
|
-
* @
|
|
126
|
-
* @param
|
|
127
|
-
* @param {Number} [index] The position of the insertion. Items will be appended if no `index` is specified.
|
|
82
|
+
* @param items
|
|
83
|
+
* @param index The position of the insertion. Items will be appended if no `index` is specified.
|
|
128
84
|
* @fires add
|
|
129
85
|
* @fires change
|
|
130
86
|
*/
|
|
@@ -160,8 +116,8 @@ export default class Collection extends Emitter {
|
|
|
160
116
|
/**
|
|
161
117
|
* Gets an item by its ID or index.
|
|
162
118
|
*
|
|
163
|
-
* @param
|
|
164
|
-
* @returns
|
|
119
|
+
* @param idOrIndex The item ID or index in the collection.
|
|
120
|
+
* @returns The requested item or `null` if such item does not exist.
|
|
165
121
|
*/
|
|
166
122
|
get(idOrIndex) {
|
|
167
123
|
let item;
|
|
@@ -184,8 +140,8 @@ export default class Collection extends Emitter {
|
|
|
184
140
|
/**
|
|
185
141
|
* Returns a Boolean indicating whether the collection contains an item.
|
|
186
142
|
*
|
|
187
|
-
* @param
|
|
188
|
-
* @returns
|
|
143
|
+
* @param itemOrId The item or its ID in the collection.
|
|
144
|
+
* @returns `true` if the collection contains the item, `false` otherwise.
|
|
189
145
|
*/
|
|
190
146
|
has(itemOrId) {
|
|
191
147
|
if (typeof itemOrId == 'string') {
|
|
@@ -201,8 +157,8 @@ export default class Collection extends Emitter {
|
|
|
201
157
|
* Gets an index of an item in the collection.
|
|
202
158
|
* When an item is not defined in the collection, the index will equal -1.
|
|
203
159
|
*
|
|
204
|
-
* @param
|
|
205
|
-
* @returns
|
|
160
|
+
* @param itemOrId The item or its ID in the collection.
|
|
161
|
+
* @returns The index of a given item.
|
|
206
162
|
*/
|
|
207
163
|
getIndex(itemOrId) {
|
|
208
164
|
let item;
|
|
@@ -217,8 +173,8 @@ export default class Collection extends Emitter {
|
|
|
217
173
|
/**
|
|
218
174
|
* Removes an item from the collection.
|
|
219
175
|
*
|
|
220
|
-
* @param
|
|
221
|
-
* @returns
|
|
176
|
+
* @param subject The item to remove, its ID or index in the collection.
|
|
177
|
+
* @returns The removed item.
|
|
222
178
|
* @fires remove
|
|
223
179
|
* @fires change
|
|
224
180
|
*/
|
|
@@ -234,11 +190,10 @@ export default class Collection extends Emitter {
|
|
|
234
190
|
/**
|
|
235
191
|
* Executes the callback for each item in the collection and composes an array or values returned by this callback.
|
|
236
192
|
*
|
|
237
|
-
* @
|
|
238
|
-
* @param
|
|
239
|
-
* @param
|
|
240
|
-
* @
|
|
241
|
-
* @returns {Array} The result of mapping.
|
|
193
|
+
* @typeParam U The result type of the callback.
|
|
194
|
+
* @param callback
|
|
195
|
+
* @param ctx Context in which the `callback` will be called.
|
|
196
|
+
* @returns The result of mapping.
|
|
242
197
|
*/
|
|
243
198
|
map(callback, ctx) {
|
|
244
199
|
return this._items.map(callback, ctx);
|
|
@@ -246,11 +201,9 @@ export default class Collection extends Emitter {
|
|
|
246
201
|
/**
|
|
247
202
|
* Finds the first item in the collection for which the `callback` returns a true value.
|
|
248
203
|
*
|
|
249
|
-
* @param
|
|
250
|
-
* @param
|
|
251
|
-
* @
|
|
252
|
-
* @param {Object} [ctx] Context in which the `callback` will be called.
|
|
253
|
-
* @returns {Object|undefined} The item for which `callback` returned a true value.
|
|
204
|
+
* @param callback
|
|
205
|
+
* @param ctx Context in which the `callback` will be called.
|
|
206
|
+
* @returns The item for which `callback` returned a true value.
|
|
254
207
|
*/
|
|
255
208
|
find(callback, ctx) {
|
|
256
209
|
return this._items.find(callback, ctx);
|
|
@@ -258,11 +211,9 @@ export default class Collection extends Emitter {
|
|
|
258
211
|
/**
|
|
259
212
|
* Returns an array with items for which the `callback` returned a true value.
|
|
260
213
|
*
|
|
261
|
-
* @param
|
|
262
|
-
* @param
|
|
263
|
-
* @
|
|
264
|
-
* @param {Object} [ctx] Context in which the `callback` will be called.
|
|
265
|
-
* @returns {Array} The array with matching items.
|
|
214
|
+
* @param callback
|
|
215
|
+
* @param ctx Context in which the `callback` will be called.
|
|
216
|
+
* @returns The array with matching items.
|
|
266
217
|
*/
|
|
267
218
|
filter(callback, ctx) {
|
|
268
219
|
return this._items.filter(callback, ctx);
|
|
@@ -294,96 +245,111 @@ export default class Collection extends Emitter {
|
|
|
294
245
|
*
|
|
295
246
|
* The binding can be a simple factory:
|
|
296
247
|
*
|
|
297
|
-
*
|
|
298
|
-
*
|
|
299
|
-
*
|
|
300
|
-
* }
|
|
301
|
-
* }
|
|
248
|
+
* ```ts
|
|
249
|
+
* class FactoryClass {
|
|
250
|
+
* public label: string;
|
|
302
251
|
*
|
|
303
|
-
*
|
|
304
|
-
*
|
|
252
|
+
* constructor( data: { label: string } ) {
|
|
253
|
+
* this.label = data.label;
|
|
254
|
+
* }
|
|
255
|
+
* }
|
|
305
256
|
*
|
|
306
|
-
*
|
|
257
|
+
* const source = new Collection<{ label: string }>( { idProperty: 'label' } );
|
|
258
|
+
* const target = new Collection<FactoryClass>();
|
|
307
259
|
*
|
|
308
|
-
*
|
|
309
|
-
* source.add( { label: 'bar' } );
|
|
260
|
+
* target.bindTo( source ).as( FactoryClass );
|
|
310
261
|
*
|
|
311
|
-
*
|
|
312
|
-
*
|
|
262
|
+
* source.add( { label: 'foo' } );
|
|
263
|
+
* source.add( { label: 'bar' } );
|
|
313
264
|
*
|
|
314
|
-
*
|
|
315
|
-
*
|
|
316
|
-
*
|
|
265
|
+
* console.log( target.length ); // 2
|
|
266
|
+
* console.log( target.get( 1 ).label ); // 'bar'
|
|
267
|
+
*
|
|
268
|
+
* source.remove( 0 );
|
|
269
|
+
* console.log( target.length ); // 1
|
|
270
|
+
* console.log( target.get( 0 ).label ); // 'bar'
|
|
271
|
+
* ```
|
|
317
272
|
*
|
|
318
273
|
* or the factory driven by a custom callback:
|
|
319
274
|
*
|
|
320
|
-
*
|
|
321
|
-
*
|
|
322
|
-
*
|
|
323
|
-
*
|
|
324
|
-
*
|
|
275
|
+
* ```ts
|
|
276
|
+
* class FooClass {
|
|
277
|
+
* public label: string;
|
|
278
|
+
*
|
|
279
|
+
* constructor( data: { label: string } ) {
|
|
280
|
+
* this.label = data.label;
|
|
281
|
+
* }
|
|
282
|
+
* }
|
|
325
283
|
*
|
|
326
|
-
*
|
|
327
|
-
*
|
|
328
|
-
* this.label = data.label;
|
|
329
|
-
* }
|
|
330
|
-
* }
|
|
284
|
+
* class BarClass {
|
|
285
|
+
* public label: string;
|
|
331
286
|
*
|
|
332
|
-
*
|
|
333
|
-
*
|
|
287
|
+
* constructor( data: { label: string } ) {
|
|
288
|
+
* this.label = data.label;
|
|
289
|
+
* }
|
|
290
|
+
* }
|
|
334
291
|
*
|
|
335
|
-
*
|
|
336
|
-
*
|
|
337
|
-
* return new FooClass( item );
|
|
338
|
-
* } else {
|
|
339
|
-
* return new BarClass( item );
|
|
340
|
-
* }
|
|
341
|
-
* } );
|
|
292
|
+
* const source = new Collection<{ label: string }>( { idProperty: 'label' } );
|
|
293
|
+
* const target = new Collection<FooClass | BarClass>();
|
|
342
294
|
*
|
|
343
|
-
*
|
|
344
|
-
*
|
|
295
|
+
* target.bindTo( source ).using( ( item ) => {
|
|
296
|
+
* if ( item.label == 'foo' ) {
|
|
297
|
+
* return new FooClass( item );
|
|
298
|
+
* } else {
|
|
299
|
+
* return new BarClass( item );
|
|
300
|
+
* }
|
|
301
|
+
* } );
|
|
345
302
|
*
|
|
346
|
-
*
|
|
347
|
-
*
|
|
348
|
-
*
|
|
303
|
+
* source.add( { label: 'foo' } );
|
|
304
|
+
* source.add( { label: 'bar' } );
|
|
305
|
+
*
|
|
306
|
+
* console.log( target.length ); // 2
|
|
307
|
+
* console.log( target.get( 0 ) instanceof FooClass ); // true
|
|
308
|
+
* console.log( target.get( 1 ) instanceof BarClass ); // true
|
|
309
|
+
* ```
|
|
349
310
|
*
|
|
350
311
|
* or the factory out of property name:
|
|
351
312
|
*
|
|
352
|
-
*
|
|
353
|
-
*
|
|
313
|
+
* ```ts
|
|
314
|
+
* const source = new Collection<{ nested: { value: string } }>();
|
|
315
|
+
* const target = new Collection<{ value: string }>();
|
|
354
316
|
*
|
|
355
|
-
*
|
|
317
|
+
* target.bindTo( source ).using( 'nested' );
|
|
356
318
|
*
|
|
357
|
-
*
|
|
358
|
-
*
|
|
319
|
+
* source.add( { nested: { value: 'foo' } } );
|
|
320
|
+
* source.add( { nested: { value: 'bar' } } );
|
|
359
321
|
*
|
|
360
|
-
*
|
|
361
|
-
*
|
|
362
|
-
*
|
|
322
|
+
* console.log( target.length ); // 2
|
|
323
|
+
* console.log( target.get( 0 ).value ); // 'foo'
|
|
324
|
+
* console.log( target.get( 1 ).value ); // 'bar'
|
|
325
|
+
* ```
|
|
363
326
|
*
|
|
364
|
-
* It's possible to skip specified items by returning
|
|
327
|
+
* It's possible to skip specified items by returning null value:
|
|
365
328
|
*
|
|
366
|
-
*
|
|
367
|
-
*
|
|
329
|
+
* ```ts
|
|
330
|
+
* const source = new Collection<{ hidden: boolean }>();
|
|
331
|
+
* const target = new Collection<{ hidden: boolean }>();
|
|
368
332
|
*
|
|
369
|
-
*
|
|
370
|
-
*
|
|
371
|
-
*
|
|
372
|
-
*
|
|
333
|
+
* target.bindTo( source ).using( item => {
|
|
334
|
+
* if ( item.hidden ) {
|
|
335
|
+
* return null;
|
|
336
|
+
* }
|
|
373
337
|
*
|
|
374
|
-
*
|
|
375
|
-
*
|
|
338
|
+
* return item;
|
|
339
|
+
* } );
|
|
376
340
|
*
|
|
377
|
-
*
|
|
378
|
-
*
|
|
341
|
+
* source.add( { hidden: true } );
|
|
342
|
+
* source.add( { hidden: false } );
|
|
379
343
|
*
|
|
380
|
-
*
|
|
381
|
-
*
|
|
344
|
+
* console.log( source.length ); // 2
|
|
345
|
+
* console.log( target.length ); // 1
|
|
346
|
+
* ```
|
|
382
347
|
*
|
|
383
348
|
* **Note**: {@link #clear} can be used to break the binding.
|
|
384
349
|
*
|
|
385
|
-
* @
|
|
386
|
-
* @
|
|
350
|
+
* @typeParam S The type of `externalCollection` element.
|
|
351
|
+
* @param externalCollection A collection to be bound.
|
|
352
|
+
* @returns The binding chain object.
|
|
387
353
|
*/
|
|
388
354
|
bindTo(externalCollection) {
|
|
389
355
|
if (this._bindToCollection) {
|
|
@@ -412,14 +378,11 @@ export default class Collection extends Emitter {
|
|
|
412
378
|
/**
|
|
413
379
|
* Finalizes and activates a binding initiated by {#bindTo}.
|
|
414
380
|
*
|
|
415
|
-
* @
|
|
416
|
-
* @param {Function} factory A function which produces collection items.
|
|
381
|
+
* @param factory A function which produces collection items.
|
|
417
382
|
*/
|
|
418
383
|
_setUpBindToBinding(factory) {
|
|
419
384
|
const externalCollection = this._bindToCollection;
|
|
420
385
|
// Adds the item to the collection once a change has been done to the external collection.
|
|
421
|
-
//
|
|
422
|
-
// @private
|
|
423
386
|
const addItem = (evt, externalItem, index) => {
|
|
424
387
|
const isExternalBoundToThis = externalCollection._bindToCollection == this;
|
|
425
388
|
const externalItemBound = externalCollection._bindToInternalToExternalMap.get(externalItem);
|
|
@@ -523,9 +486,7 @@ export default class Collection extends Emitter {
|
|
|
523
486
|
*
|
|
524
487
|
* The method will generate new id and assign it to the `item` if it doesn't have any.
|
|
525
488
|
*
|
|
526
|
-
* @
|
|
527
|
-
* @param {Object} item Item to be added.
|
|
528
|
-
* @returns {String}
|
|
489
|
+
* @param item Item to be added.
|
|
529
490
|
*/
|
|
530
491
|
_getItemIdBeforeAdding(item) {
|
|
531
492
|
const idProperty = this._idProperty;
|
|
@@ -559,9 +520,8 @@ export default class Collection extends Emitter {
|
|
|
559
520
|
*
|
|
560
521
|
* In contrast this method **does not** fire the {@link #event:change} event.
|
|
561
522
|
*
|
|
562
|
-
* @
|
|
563
|
-
* @
|
|
564
|
-
* @returns {Array} Returns an array with the removed item and its index.
|
|
523
|
+
* @param subject The item to remove, its id or index in the collection.
|
|
524
|
+
* @returns Returns an array with the removed item and its index.
|
|
565
525
|
* @fires remove
|
|
566
526
|
*/
|
|
567
527
|
_remove(subject) {
|
|
@@ -608,8 +568,6 @@ export default class Collection extends Emitter {
|
|
|
608
568
|
}
|
|
609
569
|
/**
|
|
610
570
|
* Iterable interface.
|
|
611
|
-
*
|
|
612
|
-
* @returns {Iterator.<*>}
|
|
613
571
|
*/
|
|
614
572
|
[Symbol.iterator]() {
|
|
615
573
|
return this._items[Symbol.iterator]();
|