@ckeditor/ckeditor5-utils 34.1.0 → 35.0.1

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.
Files changed (61) hide show
  1. package/CHANGELOG.md +324 -0
  2. package/LICENSE.md +1 -1
  3. package/package.json +19 -8
  4. package/src/areconnectedthroughproperties.js +54 -71
  5. package/src/ckeditorerror.js +92 -114
  6. package/src/collection.js +594 -760
  7. package/src/comparearrays.js +22 -28
  8. package/src/config.js +193 -223
  9. package/src/count.js +8 -12
  10. package/src/diff.js +85 -110
  11. package/src/difftochanges.js +47 -57
  12. package/src/dom/createelement.js +17 -25
  13. package/src/dom/emittermixin.js +217 -257
  14. package/src/dom/getancestors.js +9 -13
  15. package/src/dom/getborderwidths.js +10 -13
  16. package/src/dom/getcommonancestor.js +9 -15
  17. package/src/dom/getdatafromelement.js +5 -9
  18. package/src/dom/getpositionedancestor.js +9 -14
  19. package/src/dom/global.js +0 -3
  20. package/src/dom/indexof.js +7 -11
  21. package/src/dom/insertat.js +2 -4
  22. package/src/dom/iscomment.js +2 -5
  23. package/src/dom/isnode.js +10 -12
  24. package/src/dom/isrange.js +2 -4
  25. package/src/dom/istext.js +2 -4
  26. package/src/dom/isvisible.js +2 -4
  27. package/src/dom/iswindow.js +11 -16
  28. package/src/dom/position.js +220 -410
  29. package/src/dom/rect.js +335 -414
  30. package/src/dom/remove.js +5 -8
  31. package/src/dom/resizeobserver.js +109 -342
  32. package/src/dom/scroll.js +151 -183
  33. package/src/dom/setdatainelement.js +5 -9
  34. package/src/dom/tounit.js +10 -12
  35. package/src/elementreplacer.js +30 -44
  36. package/src/emittermixin.js +383 -631
  37. package/src/env.js +93 -115
  38. package/src/eventinfo.js +12 -65
  39. package/src/fastdiff.js +96 -128
  40. package/src/first.js +8 -12
  41. package/src/focustracker.js +77 -131
  42. package/src/index.js +0 -9
  43. package/src/inserttopriorityarray.js +9 -30
  44. package/src/isiterable.js +2 -4
  45. package/src/keyboard.js +117 -196
  46. package/src/keystrokehandler.js +72 -88
  47. package/src/language.js +9 -15
  48. package/src/locale.js +61 -158
  49. package/src/mapsequal.js +12 -17
  50. package/src/mix.js +13 -16
  51. package/src/nth.js +8 -11
  52. package/src/objecttomap.js +7 -11
  53. package/src/observablemixin.js +465 -768
  54. package/src/priorities.js +20 -32
  55. package/src/spy.js +3 -6
  56. package/src/toarray.js +2 -13
  57. package/src/tomap.js +8 -10
  58. package/src/translation-service.js +51 -87
  59. package/src/uid.js +34 -38
  60. package/src/unicode.js +28 -43
  61. package/src/version.js +134 -143
@@ -2,23 +2,19 @@
2
2
  * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module utils/emittermixin
8
7
  */
9
-
10
8
  import EventInfo from './eventinfo';
11
9
  import uid from './uid';
12
10
  import priorities from './priorities';
13
11
  import insertToPriorityArray from './inserttopriorityarray';
14
-
15
12
  // To check if component is loaded more than once.
16
13
  import './version';
17
14
  import CKEditorError from './ckeditorerror';
18
-
19
- const _listeningTo = Symbol( 'listeningTo' );
20
- const _emitterId = Symbol( 'emitterId' );
21
-
15
+ const _listeningTo = Symbol('listeningTo');
16
+ const _emitterId = Symbol('emitterId');
17
+ const _delegations = Symbol('delegations');
22
18
  /**
23
19
  * Mixin that injects the {@link ~Emitter events API} into its host.
24
20
  *
@@ -31,634 +27,406 @@ const _emitterId = Symbol( 'emitterId' );
31
27
  * @implements module:utils/emittermixin~Emitter
32
28
  */
33
29
  const EmitterMixin = {
34
- /**
35
- * @inheritDoc
36
- */
37
- on( event, callback, options = {} ) {
38
- this.listenTo( this, event, callback, options );
39
- },
40
-
41
- /**
42
- * @inheritDoc
43
- */
44
- once( event, callback, options ) {
45
- let wasFired = false;
46
-
47
- const onceCallback = function( event, ...args ) {
48
- // Ensure the callback is called only once even if the callback itself leads to re-firing the event
49
- // (which would call the callback again).
50
- if ( !wasFired ) {
51
- wasFired = true;
52
-
53
- // Go off() at the first call.
54
- event.off();
55
-
56
- // Go with the original callback.
57
- callback.call( this, event, ...args );
58
- }
59
- };
60
-
61
- // Make a similar on() call, simply replacing the callback.
62
- this.listenTo( this, event, onceCallback, options );
63
- },
64
-
65
- /**
66
- * @inheritDoc
67
- */
68
- off( event, callback ) {
69
- this.stopListening( this, event, callback );
70
- },
71
-
72
- /**
73
- * @inheritDoc
74
- */
75
- listenTo( emitter, event, callback, options = {} ) {
76
- let emitterInfo, eventCallbacks;
77
-
78
- // _listeningTo contains a list of emitters that this object is listening to.
79
- // This list has the following format:
80
- //
81
- // _listeningTo: {
82
- // emitterId: {
83
- // emitter: emitter,
84
- // callbacks: {
85
- // event1: [ callback1, callback2, ... ]
86
- // ....
87
- // }
88
- // },
89
- // ...
90
- // }
91
-
92
- if ( !this[ _listeningTo ] ) {
93
- this[ _listeningTo ] = {};
94
- }
95
-
96
- const emitters = this[ _listeningTo ];
97
-
98
- if ( !_getEmitterId( emitter ) ) {
99
- _setEmitterId( emitter );
100
- }
101
-
102
- const emitterId = _getEmitterId( emitter );
103
-
104
- if ( !( emitterInfo = emitters[ emitterId ] ) ) {
105
- emitterInfo = emitters[ emitterId ] = {
106
- emitter,
107
- callbacks: {}
108
- };
109
- }
110
-
111
- if ( !( eventCallbacks = emitterInfo.callbacks[ event ] ) ) {
112
- eventCallbacks = emitterInfo.callbacks[ event ] = [];
113
- }
114
-
115
- eventCallbacks.push( callback );
116
-
117
- // Finally register the callback to the event.
118
- addEventListener( this, emitter, event, callback, options );
119
- },
120
-
121
- /**
122
- * @inheritDoc
123
- */
124
- stopListening( emitter, event, callback ) {
125
- const emitters = this[ _listeningTo ];
126
- let emitterId = emitter && _getEmitterId( emitter );
127
- const emitterInfo = emitters && emitterId && emitters[ emitterId ];
128
- const eventCallbacks = emitterInfo && event && emitterInfo.callbacks[ event ];
129
-
130
- // Stop if nothing has been listened.
131
- if ( !emitters || ( emitter && !emitterInfo ) || ( event && !eventCallbacks ) ) {
132
- return;
133
- }
134
-
135
- // All params provided. off() that single callback.
136
- if ( callback ) {
137
- removeEventListener( this, emitter, event, callback );
138
-
139
- // We must remove callbacks as well in order to prevent memory leaks.
140
- // See https://github.com/ckeditor/ckeditor5/pull/8480
141
- const index = eventCallbacks.indexOf( callback );
142
-
143
- if ( index !== -1 ) {
144
- if ( eventCallbacks.length === 1 ) {
145
- delete emitterInfo.callbacks[ event ];
146
- } else {
147
- removeEventListener( this, emitter, event, callback );
148
- }
149
- }
150
- }
151
- // Only `emitter` and `event` provided. off() all callbacks for that event.
152
- else if ( eventCallbacks ) {
153
- while ( ( callback = eventCallbacks.pop() ) ) {
154
- removeEventListener( this, emitter, event, callback );
155
- }
156
-
157
- delete emitterInfo.callbacks[ event ];
158
- }
159
- // Only `emitter` provided. off() all events for that emitter.
160
- else if ( emitterInfo ) {
161
- for ( event in emitterInfo.callbacks ) {
162
- this.stopListening( emitter, event );
163
- }
164
- delete emitters[ emitterId ];
165
- }
166
- // No params provided. off() all emitters.
167
- else {
168
- for ( emitterId in emitters ) {
169
- this.stopListening( emitters[ emitterId ].emitter );
170
- }
171
- delete this[ _listeningTo ];
172
- }
173
- },
174
-
175
- /**
176
- * @inheritDoc
177
- */
178
- fire( eventOrInfo, ...args ) {
179
- try {
180
- const eventInfo = eventOrInfo instanceof EventInfo ? eventOrInfo : new EventInfo( this, eventOrInfo );
181
- const event = eventInfo.name;
182
- let callbacks = getCallbacksForEvent( this, event );
183
-
184
- // Record that the event passed this emitter on its path.
185
- eventInfo.path.push( this );
186
-
187
- // Handle event listener callbacks first.
188
- if ( callbacks ) {
189
- // Arguments passed to each callback.
190
- const callbackArgs = [ eventInfo, ...args ];
191
-
192
- // Copying callbacks array is the easiest and most secure way of preventing infinite loops, when event callbacks
193
- // are added while processing other callbacks. Previous solution involved adding counters (unique ids) but
194
- // failed if callbacks were added to the queue before currently processed callback.
195
- // If this proves to be too inefficient, another method is to change `.on()` so callbacks are stored if same
196
- // event is currently processed. Then, `.fire()` at the end, would have to add all stored events.
197
- callbacks = Array.from( callbacks );
198
-
199
- for ( let i = 0; i < callbacks.length; i++ ) {
200
- callbacks[ i ].callback.apply( this, callbackArgs );
201
-
202
- // Remove the callback from future requests if off() has been called.
203
- if ( eventInfo.off.called ) {
204
- // Remove the called mark for the next calls.
205
- delete eventInfo.off.called;
206
-
207
- this._removeEventListener( event, callbacks[ i ].callback );
208
- }
209
-
210
- // Do not execute next callbacks if stop() was called.
211
- if ( eventInfo.stop.called ) {
212
- break;
213
- }
214
- }
215
- }
216
-
217
- // Delegate event to other emitters if needed.
218
- if ( this._delegations ) {
219
- const destinations = this._delegations.get( event );
220
- const passAllDestinations = this._delegations.get( '*' );
221
-
222
- if ( destinations ) {
223
- fireDelegatedEvents( destinations, eventInfo, args );
224
- }
225
-
226
- if ( passAllDestinations ) {
227
- fireDelegatedEvents( passAllDestinations, eventInfo, args );
228
- }
229
- }
230
-
231
- return eventInfo.return;
232
- } catch ( err ) {
233
- // @if CK_DEBUG // throw err;
234
- /* istanbul ignore next */
235
- CKEditorError.rethrowUnexpectedError( err, this );
236
- }
237
- },
238
-
239
- /**
240
- * @inheritDoc
241
- */
242
- delegate( ...events ) {
243
- return {
244
- to: ( emitter, nameOrFunction ) => {
245
- if ( !this._delegations ) {
246
- this._delegations = new Map();
247
- }
248
-
249
- // Originally there was a for..of loop which unfortunately caused an error in Babel that didn't allow
250
- // build an application. See: https://github.com/ckeditor/ckeditor5-react/issues/40.
251
- events.forEach( eventName => {
252
- const destinations = this._delegations.get( eventName );
253
-
254
- if ( !destinations ) {
255
- this._delegations.set( eventName, new Map( [ [ emitter, nameOrFunction ] ] ) );
256
- } else {
257
- destinations.set( emitter, nameOrFunction );
258
- }
259
- } );
260
- }
261
- };
262
- },
263
-
264
- /**
265
- * @inheritDoc
266
- */
267
- stopDelegating( event, emitter ) {
268
- if ( !this._delegations ) {
269
- return;
270
- }
271
-
272
- if ( !event ) {
273
- this._delegations.clear();
274
- } else if ( !emitter ) {
275
- this._delegations.delete( event );
276
- } else {
277
- const destinations = this._delegations.get( event );
278
-
279
- if ( destinations ) {
280
- destinations.delete( emitter );
281
- }
282
- }
283
- },
284
-
285
- /**
286
- * @inheritDoc
287
- */
288
- _addEventListener( event, callback, options ) {
289
- createEventNamespace( this, event );
290
-
291
- const lists = getCallbacksListsForNamespace( this, event );
292
- const priority = priorities.get( options.priority );
293
-
294
- const callbackDefinition = {
295
- callback,
296
- priority
297
- };
298
-
299
- // Add the callback to all callbacks list.
300
- for ( const callbacks of lists ) {
301
- // Add the callback to the list in the right priority position.
302
- insertToPriorityArray( callbacks, callbackDefinition );
303
- }
304
- },
305
-
306
- /**
307
- * @inheritDoc
308
- */
309
- _removeEventListener( event, callback ) {
310
- const lists = getCallbacksListsForNamespace( this, event );
311
-
312
- for ( const callbacks of lists ) {
313
- for ( let i = 0; i < callbacks.length; i++ ) {
314
- if ( callbacks[ i ].callback == callback ) {
315
- // Remove the callback from the list (fixing the next index).
316
- callbacks.splice( i, 1 );
317
- i--;
318
- }
319
- }
320
- }
321
- }
30
+ /**
31
+ * @inheritDoc
32
+ */
33
+ on(event, callback, options = {}) {
34
+ this.listenTo(this, event, callback, options);
35
+ },
36
+ /**
37
+ * @inheritDoc
38
+ */
39
+ once(event, callback, options) {
40
+ let wasFired = false;
41
+ const onceCallback = (event, ...args) => {
42
+ // Ensure the callback is called only once even if the callback itself leads to re-firing the event
43
+ // (which would call the callback again).
44
+ if (!wasFired) {
45
+ wasFired = true;
46
+ // Go off() at the first call.
47
+ event.off();
48
+ // Go with the original callback.
49
+ callback.call(this, event, ...args);
50
+ }
51
+ };
52
+ // Make a similar on() call, simply replacing the callback.
53
+ this.listenTo(this, event, onceCallback, options);
54
+ },
55
+ /**
56
+ * @inheritDoc
57
+ */
58
+ off(event, callback) {
59
+ this.stopListening(this, event, callback);
60
+ },
61
+ /**
62
+ * @inheritDoc
63
+ */
64
+ listenTo(emitter, event, callback, options = {}) {
65
+ let emitterInfo, eventCallbacks;
66
+ // _listeningTo contains a list of emitters that this object is listening to.
67
+ // This list has the following format:
68
+ //
69
+ // _listeningTo: {
70
+ // emitterId: {
71
+ // emitter: emitter,
72
+ // callbacks: {
73
+ // event1: [ callback1, callback2, ... ]
74
+ // ....
75
+ // }
76
+ // },
77
+ // ...
78
+ // }
79
+ if (!this[_listeningTo]) {
80
+ this[_listeningTo] = {};
81
+ }
82
+ const emitters = this[_listeningTo];
83
+ if (!_getEmitterId(emitter)) {
84
+ _setEmitterId(emitter);
85
+ }
86
+ const emitterId = _getEmitterId(emitter);
87
+ if (!(emitterInfo = emitters[emitterId])) {
88
+ emitterInfo = emitters[emitterId] = {
89
+ emitter,
90
+ callbacks: {}
91
+ };
92
+ }
93
+ if (!(eventCallbacks = emitterInfo.callbacks[event])) {
94
+ eventCallbacks = emitterInfo.callbacks[event] = [];
95
+ }
96
+ eventCallbacks.push(callback);
97
+ // Finally register the callback to the event.
98
+ addEventListener(this, emitter, event, callback, options);
99
+ },
100
+ /**
101
+ * @inheritDoc
102
+ */
103
+ stopListening(emitter, event, callback) {
104
+ const emitters = this[_listeningTo];
105
+ let emitterId = emitter && _getEmitterId(emitter);
106
+ const emitterInfo = (emitters && emitterId) ? emitters[emitterId] : undefined;
107
+ const eventCallbacks = (emitterInfo && event) ? emitterInfo.callbacks[event] : undefined;
108
+ // Stop if nothing has been listened.
109
+ if (!emitters || (emitter && !emitterInfo) || (event && !eventCallbacks)) {
110
+ return;
111
+ }
112
+ // All params provided. off() that single callback.
113
+ if (callback) {
114
+ removeEventListener(this, emitter, event, callback);
115
+ // We must remove callbacks as well in order to prevent memory leaks.
116
+ // See https://github.com/ckeditor/ckeditor5/pull/8480
117
+ const index = eventCallbacks.indexOf(callback);
118
+ if (index !== -1) {
119
+ if (eventCallbacks.length === 1) {
120
+ delete emitterInfo.callbacks[event];
121
+ }
122
+ else {
123
+ removeEventListener(this, emitter, event, callback);
124
+ }
125
+ }
126
+ }
127
+ // Only `emitter` and `event` provided. off() all callbacks for that event.
128
+ else if (eventCallbacks) {
129
+ while ((callback = eventCallbacks.pop())) {
130
+ removeEventListener(this, emitter, event, callback);
131
+ }
132
+ delete emitterInfo.callbacks[event];
133
+ }
134
+ // Only `emitter` provided. off() all events for that emitter.
135
+ else if (emitterInfo) {
136
+ for (event in emitterInfo.callbacks) {
137
+ this.stopListening(emitter, event);
138
+ }
139
+ delete emitters[emitterId];
140
+ }
141
+ // No params provided. off() all emitters.
142
+ else {
143
+ for (emitterId in emitters) {
144
+ this.stopListening(emitters[emitterId].emitter);
145
+ }
146
+ delete this[_listeningTo];
147
+ }
148
+ },
149
+ /**
150
+ * @inheritDoc
151
+ */
152
+ fire(eventOrInfo, ...args) {
153
+ try {
154
+ const eventInfo = eventOrInfo instanceof EventInfo ? eventOrInfo : new EventInfo(this, eventOrInfo);
155
+ const event = eventInfo.name;
156
+ let callbacks = getCallbacksForEvent(this, event);
157
+ // Record that the event passed this emitter on its path.
158
+ eventInfo.path.push(this);
159
+ // Handle event listener callbacks first.
160
+ if (callbacks) {
161
+ // Arguments passed to each callback.
162
+ const callbackArgs = [eventInfo, ...args];
163
+ // Copying callbacks array is the easiest and most secure way of preventing infinite loops, when event callbacks
164
+ // are added while processing other callbacks. Previous solution involved adding counters (unique ids) but
165
+ // failed if callbacks were added to the queue before currently processed callback.
166
+ // If this proves to be too inefficient, another method is to change `.on()` so callbacks are stored if same
167
+ // event is currently processed. Then, `.fire()` at the end, would have to add all stored events.
168
+ callbacks = Array.from(callbacks);
169
+ for (let i = 0; i < callbacks.length; i++) {
170
+ callbacks[i].callback.apply(this, callbackArgs);
171
+ // Remove the callback from future requests if off() has been called.
172
+ if (eventInfo.off.called) {
173
+ // Remove the called mark for the next calls.
174
+ delete eventInfo.off.called;
175
+ this._removeEventListener(event, callbacks[i].callback);
176
+ }
177
+ // Do not execute next callbacks if stop() was called.
178
+ if (eventInfo.stop.called) {
179
+ break;
180
+ }
181
+ }
182
+ }
183
+ // Delegate event to other emitters if needed.
184
+ const delegations = this[_delegations];
185
+ if (delegations) {
186
+ const destinations = delegations.get(event);
187
+ const passAllDestinations = delegations.get('*');
188
+ if (destinations) {
189
+ fireDelegatedEvents(destinations, eventInfo, args);
190
+ }
191
+ if (passAllDestinations) {
192
+ fireDelegatedEvents(passAllDestinations, eventInfo, args);
193
+ }
194
+ }
195
+ return eventInfo.return;
196
+ }
197
+ catch (err) {
198
+ // @if CK_DEBUG // throw err;
199
+ /* istanbul ignore next */
200
+ CKEditorError.rethrowUnexpectedError(err, this);
201
+ }
202
+ },
203
+ /**
204
+ * @inheritDoc
205
+ */
206
+ delegate(...events) {
207
+ return {
208
+ to: (emitter, nameOrFunction) => {
209
+ if (!this[_delegations]) {
210
+ this[_delegations] = new Map();
211
+ }
212
+ // Originally there was a for..of loop which unfortunately caused an error in Babel that didn't allow
213
+ // build an application. See: https://github.com/ckeditor/ckeditor5-react/issues/40.
214
+ events.forEach(eventName => {
215
+ const destinations = this[_delegations].get(eventName);
216
+ if (!destinations) {
217
+ this[_delegations].set(eventName, new Map([[emitter, nameOrFunction]]));
218
+ }
219
+ else {
220
+ destinations.set(emitter, nameOrFunction);
221
+ }
222
+ });
223
+ }
224
+ };
225
+ },
226
+ /**
227
+ * @inheritDoc
228
+ */
229
+ stopDelegating(event, emitter) {
230
+ if (!this[_delegations]) {
231
+ return;
232
+ }
233
+ if (!event) {
234
+ this[_delegations].clear();
235
+ }
236
+ else if (!emitter) {
237
+ this[_delegations].delete(event);
238
+ }
239
+ else {
240
+ const destinations = this[_delegations].get(event);
241
+ if (destinations) {
242
+ destinations.delete(emitter);
243
+ }
244
+ }
245
+ },
246
+ /**
247
+ * @inheritDoc
248
+ */
249
+ _addEventListener(event, callback, options) {
250
+ createEventNamespace(this, event);
251
+ const lists = getCallbacksListsForNamespace(this, event);
252
+ const priority = priorities.get(options.priority);
253
+ const callbackDefinition = {
254
+ callback,
255
+ priority
256
+ };
257
+ // Add the callback to all callbacks list.
258
+ for (const callbacks of lists) {
259
+ // Add the callback to the list in the right priority position.
260
+ insertToPriorityArray(callbacks, callbackDefinition);
261
+ }
262
+ },
263
+ /**
264
+ * @inheritDoc
265
+ */
266
+ _removeEventListener(event, callback) {
267
+ const lists = getCallbacksListsForNamespace(this, event);
268
+ for (const callbacks of lists) {
269
+ for (let i = 0; i < callbacks.length; i++) {
270
+ if (callbacks[i].callback == callback) {
271
+ // Remove the callback from the list (fixing the next index).
272
+ callbacks.splice(i, 1);
273
+ i--;
274
+ }
275
+ }
276
+ }
277
+ }
322
278
  };
323
-
324
279
  export default EmitterMixin;
325
-
326
- /**
327
- * Emitter/listener interface.
328
- *
329
- * Can be easily implemented by a class by mixing the {@link module:utils/emittermixin~EmitterMixin} mixin.
330
- *
331
- * Read more about the usage of this interface in the:
332
- * * {@glink framework/guides/architecture/core-editor-architecture#event-system-and-observables Event system and observables}
333
- * section of the {@glink framework/guides/architecture/core-editor-architecture Core editor architecture} guide.
334
- * * {@glink framework/guides/deep-dive/event-system Event system} deep dive guide.
335
- *
336
- * @interface Emitter
337
- */
338
-
339
- /**
340
- * Registers a callback function to be executed when an event is fired.
341
- *
342
- * Shorthand for {@link #listenTo `this.listenTo( this, event, callback, options )`} (it makes the emitter
343
- * listen on itself).
344
- *
345
- * @method #on
346
- * @param {String} event The name of the event.
347
- * @param {Function} callback The function to be called on event.
348
- * @param {Object} [options={}] Additional options.
349
- * @param {module:utils/priorities~PriorityString|Number} [options.priority='normal'] The priority of this event callback. The higher
350
- * the priority value the sooner the callback will be fired. Events having the same priority are called in the
351
- * order they were added.
352
- */
353
-
354
- /**
355
- * Registers a callback function to be executed on the next time the event is fired only. This is similar to
356
- * calling {@link #on} followed by {@link #off} in the callback.
357
- *
358
- * @method #once
359
- * @param {String} event The name of the event.
360
- * @param {Function} callback The function to be called on event.
361
- * @param {Object} [options={}] Additional options.
362
- * @param {module:utils/priorities~PriorityString|Number} [options.priority='normal'] The priority of this event callback. The higher
363
- * the priority value the sooner the callback will be fired. Events having the same priority are called in the
364
- * order they were added.
365
- */
366
-
367
- /**
368
- * Stops executing the callback on the given event.
369
- * Shorthand for {@link #stopListening `this.stopListening( this, event, callback )`}.
370
- *
371
- * @method #off
372
- * @param {String} event The name of the event.
373
- * @param {Function} callback The function to stop being called.
374
- */
375
-
376
- /**
377
- * Registers a callback function to be executed when an event is fired in a specific (emitter) object.
378
- *
379
- * Events can be grouped in namespaces using `:`.
380
- * When namespaced event is fired, it additionally fires all callbacks for that namespace.
381
- *
382
- * // myEmitter.on( ... ) is a shorthand for myEmitter.listenTo( myEmitter, ... ).
383
- * myEmitter.on( 'myGroup', genericCallback );
384
- * myEmitter.on( 'myGroup:myEvent', specificCallback );
385
- *
386
- * // genericCallback is fired.
387
- * myEmitter.fire( 'myGroup' );
388
- * // both genericCallback and specificCallback are fired.
389
- * myEmitter.fire( 'myGroup:myEvent' );
390
- * // genericCallback is fired even though there are no callbacks for "foo".
391
- * myEmitter.fire( 'myGroup:foo' );
392
- *
393
- * An event callback can {@link module:utils/eventinfo~EventInfo#stop stop the event} and
394
- * set the {@link module:utils/eventinfo~EventInfo#return return value} of the {@link #fire} method.
395
- *
396
- * @method #listenTo
397
- * @param {module:utils/emittermixin~Emitter} emitter The object that fires the event.
398
- * @param {String} event The name of the event.
399
- * @param {Function} callback The function to be called on event.
400
- * @param {Object} [options={}] Additional options.
401
- * @param {module:utils/priorities~PriorityString|Number} [options.priority='normal'] The priority of this event callback. The higher
402
- * the priority value the sooner the callback will be fired. Events having the same priority are called in the
403
- * order they were added.
404
- */
405
-
406
- /**
407
- * Stops listening for events. It can be used at different levels:
408
- *
409
- * * To stop listening to a specific callback.
410
- * * To stop listening to a specific event.
411
- * * To stop listening to all events fired by a specific object.
412
- * * To stop listening to all events fired by all objects.
413
- *
414
- * @method #stopListening
415
- * @param {module:utils/emittermixin~Emitter} [emitter] The object to stop listening to. If omitted, stops it for all objects.
416
- * @param {String} [event] (Requires the `emitter`) The name of the event to stop listening to. If omitted, stops it
417
- * for all events from `emitter`.
418
- * @param {Function} [callback] (Requires the `event`) The function to be removed from the call list for the given
419
- * `event`.
420
- */
421
-
422
- /**
423
- * Fires an event, executing all callbacks registered for it.
424
- *
425
- * The first parameter passed to callbacks is an {@link module:utils/eventinfo~EventInfo} object,
426
- * followed by the optional `args` provided in the `fire()` method call.
427
- *
428
- * @method #fire
429
- * @param {String|module:utils/eventinfo~EventInfo} eventOrInfo The name of the event or `EventInfo` object if event is delegated.
430
- * @param {...*} [args] Additional arguments to be passed to the callbacks.
431
- * @returns {*} By default the method returns `undefined`. However, the return value can be changed by listeners
432
- * through modification of the {@link module:utils/eventinfo~EventInfo#return `evt.return`}'s property (the event info
433
- * is the first param of every callback).
434
- */
435
-
436
- /**
437
- * Delegates selected events to another {@link module:utils/emittermixin~Emitter}. For instance:
438
- *
439
- * emitterA.delegate( 'eventX' ).to( emitterB );
440
- * emitterA.delegate( 'eventX', 'eventY' ).to( emitterC );
441
- *
442
- * then `eventX` is delegated (fired by) `emitterB` and `emitterC` along with `data`:
443
- *
444
- * emitterA.fire( 'eventX', data );
445
- *
446
- * and `eventY` is delegated (fired by) `emitterC` along with `data`:
447
- *
448
- * emitterA.fire( 'eventY', data );
449
- *
450
- * @method #delegate
451
- * @param {...String} events Event names that will be delegated to another emitter.
452
- * @returns {module:utils/emittermixin~EmitterMixinDelegateChain}
453
- */
454
-
455
- /**
456
- * Stops delegating events. It can be used at different levels:
457
- *
458
- * * To stop delegating all events.
459
- * * To stop delegating a specific event to all emitters.
460
- * * To stop delegating a specific event to a specific emitter.
461
- *
462
- * @method #stopDelegating
463
- * @param {String} [event] The name of the event to stop delegating. If omitted, stops it all delegations.
464
- * @param {module:utils/emittermixin~Emitter} [emitter] (requires `event`) The object to stop delegating a particular event to.
465
- * If omitted, stops delegation of `event` to all emitters.
466
- */
467
-
468
- /**
469
- * Adds callback to emitter for given event.
470
- *
471
- * @protected
472
- * @method #_addEventListener
473
- * @param {String} event The name of the event.
474
- * @param {Function} callback The function to be called on event.
475
- * @param {Object} [options={}] Additional options.
476
- * @param {module:utils/priorities~PriorityString|Number} [options.priority='normal'] The priority of this event callback. The higher
477
- * the priority value the sooner the callback will be fired. Events having the same priority are called in the
478
- * order they were added.
479
- */
480
-
481
- /**
482
- * Removes callback from emitter for given event.
483
- *
484
- * @protected
485
- * @method #_removeEventListener
486
- * @param {String} event The name of the event.
487
- * @param {Function} callback The function to stop being called.
488
- */
489
-
490
280
  /**
491
281
  * Checks if `listeningEmitter` listens to an emitter with given `listenedToEmitterId` and if so, returns that emitter.
492
282
  * If not, returns `null`.
493
283
  *
284
+ * @internal
494
285
  * @protected
495
286
  * @param {module:utils/emittermixin~Emitter} listeningEmitter An emitter that listens.
496
287
  * @param {String} listenedToEmitterId Unique emitter id of emitter listened to.
497
288
  * @returns {module:utils/emittermixin~Emitter|null}
498
289
  */
499
- export function _getEmitterListenedTo( listeningEmitter, listenedToEmitterId ) {
500
- if ( listeningEmitter[ _listeningTo ] && listeningEmitter[ _listeningTo ][ listenedToEmitterId ] ) {
501
- return listeningEmitter[ _listeningTo ][ listenedToEmitterId ].emitter;
502
- }
503
-
504
- return null;
290
+ export function _getEmitterListenedTo(listeningEmitter, listenedToEmitterId) {
291
+ const listeningTo = listeningEmitter[_listeningTo];
292
+ if (listeningTo && listeningTo[listenedToEmitterId]) {
293
+ return listeningTo[listenedToEmitterId].emitter;
294
+ }
295
+ return null;
505
296
  }
506
-
507
297
  /**
508
298
  * Sets emitter's unique id.
509
299
  *
510
300
  * **Note:** `_emitterId` can be set only once.
511
301
  *
302
+ * @internal
512
303
  * @protected
513
304
  * @param {module:utils/emittermixin~Emitter} emitter An emitter for which id will be set.
514
305
  * @param {String} [id] Unique id to set. If not passed, random unique id will be set.
515
306
  */
516
- export function _setEmitterId( emitter, id ) {
517
- if ( !emitter[ _emitterId ] ) {
518
- emitter[ _emitterId ] = id || uid();
519
- }
307
+ export function _setEmitterId(emitter, id) {
308
+ if (!emitter[_emitterId]) {
309
+ emitter[_emitterId] = id || uid();
310
+ }
520
311
  }
521
-
522
312
  /**
523
313
  * Returns emitter's unique id.
524
314
  *
315
+ * @internal
525
316
  * @protected
526
317
  * @param {module:utils/emittermixin~Emitter} emitter An emitter which id will be returned.
318
+ * @returns {String|undefined}
527
319
  */
528
- export function _getEmitterId( emitter ) {
529
- return emitter[ _emitterId ];
320
+ export function _getEmitterId(emitter) {
321
+ return emitter[_emitterId];
530
322
  }
531
-
532
323
  // Gets the internal `_events` property of the given object.
533
324
  // `_events` property store all lists with callbacks for registered event names.
534
325
  // If there were no events registered on the object, empty `_events` object is created.
535
- function getEvents( source ) {
536
- if ( !source._events ) {
537
- Object.defineProperty( source, '_events', {
538
- value: {}
539
- } );
540
- }
541
-
542
- return source._events;
326
+ function getEvents(source) {
327
+ if (!source._events) {
328
+ Object.defineProperty(source, '_events', {
329
+ value: {}
330
+ });
331
+ }
332
+ return source._events;
543
333
  }
544
-
545
334
  // Creates event node for generic-specific events relation architecture.
546
335
  function makeEventNode() {
547
- return {
548
- callbacks: [],
549
- childEvents: []
550
- };
336
+ return {
337
+ callbacks: [],
338
+ childEvents: []
339
+ };
551
340
  }
552
-
553
341
  // Creates an architecture for generic-specific events relation.
554
342
  // If needed, creates all events for given eventName, i.e. if the first registered event
555
343
  // is foo:bar:abc, it will create foo:bar:abc, foo:bar and foo event and tie them together.
556
344
  // It also copies callbacks from more generic events to more specific events when
557
345
  // specific events are created.
558
- function createEventNamespace( source, eventName ) {
559
- const events = getEvents( source );
560
-
561
- // First, check if the event we want to add to the structure already exists.
562
- if ( events[ eventName ] ) {
563
- // If it exists, we don't have to do anything.
564
- return;
565
- }
566
-
567
- // In other case, we have to create the structure for the event.
568
- // Note, that we might need to create intermediate events too.
569
- // I.e. if foo:bar:abc is being registered and we only have foo in the structure,
570
- // we need to also register foo:bar.
571
-
572
- // Currently processed event name.
573
- let name = eventName;
574
- // Name of the event that is a child event for currently processed event.
575
- let childEventName = null;
576
-
577
- // Array containing all newly created specific events.
578
- const newEventNodes = [];
579
-
580
- // While loop can't check for ':' index because we have to handle generic events too.
581
- // In each loop, we truncate event name, going from the most specific name to the generic one.
582
- // I.e. foo:bar:abc -> foo:bar -> foo.
583
- while ( name !== '' ) {
584
- if ( events[ name ] ) {
585
- // If the currently processed event name is already registered, we can be sure
586
- // that it already has all the structure created, so we can break the loop here
587
- // as no more events need to be registered.
588
- break;
589
- }
590
-
591
- // If this event is not yet registered, create a new object for it.
592
- events[ name ] = makeEventNode();
593
- // Add it to the array with newly created events.
594
- newEventNodes.push( events[ name ] );
595
-
596
- // Add previously processed event name as a child of this event.
597
- if ( childEventName ) {
598
- events[ name ].childEvents.push( childEventName );
599
- }
600
-
601
- childEventName = name;
602
- // If `.lastIndexOf()` returns -1, `.substr()` will return '' which will break the loop.
603
- name = name.substr( 0, name.lastIndexOf( ':' ) );
604
- }
605
-
606
- if ( name !== '' ) {
607
- // If name is not empty, we found an already registered event that was a parent of the
608
- // event we wanted to register.
609
-
610
- // Copy that event's callbacks to newly registered events.
611
- for ( const node of newEventNodes ) {
612
- node.callbacks = events[ name ].callbacks.slice();
613
- }
614
-
615
- // Add last newly created event to the already registered event.
616
- events[ name ].childEvents.push( childEventName );
617
- }
346
+ function createEventNamespace(source, eventName) {
347
+ const events = getEvents(source);
348
+ // First, check if the event we want to add to the structure already exists.
349
+ if (events[eventName]) {
350
+ // If it exists, we don't have to do anything.
351
+ return;
352
+ }
353
+ // In other case, we have to create the structure for the event.
354
+ // Note, that we might need to create intermediate events too.
355
+ // I.e. if foo:bar:abc is being registered and we only have foo in the structure,
356
+ // we need to also register foo:bar.
357
+ // Currently processed event name.
358
+ let name = eventName;
359
+ // Name of the event that is a child event for currently processed event.
360
+ let childEventName = null;
361
+ // Array containing all newly created specific events.
362
+ const newEventNodes = [];
363
+ // While loop can't check for ':' index because we have to handle generic events too.
364
+ // In each loop, we truncate event name, going from the most specific name to the generic one.
365
+ // I.e. foo:bar:abc -> foo:bar -> foo.
366
+ while (name !== '') {
367
+ if (events[name]) {
368
+ // If the currently processed event name is already registered, we can be sure
369
+ // that it already has all the structure created, so we can break the loop here
370
+ // as no more events need to be registered.
371
+ break;
372
+ }
373
+ // If this event is not yet registered, create a new object for it.
374
+ events[name] = makeEventNode();
375
+ // Add it to the array with newly created events.
376
+ newEventNodes.push(events[name]);
377
+ // Add previously processed event name as a child of this event.
378
+ if (childEventName) {
379
+ events[name].childEvents.push(childEventName);
380
+ }
381
+ childEventName = name;
382
+ // If `.lastIndexOf()` returns -1, `.substr()` will return '' which will break the loop.
383
+ name = name.substr(0, name.lastIndexOf(':'));
384
+ }
385
+ if (name !== '') {
386
+ // If name is not empty, we found an already registered event that was a parent of the
387
+ // event we wanted to register.
388
+ // Copy that event's callbacks to newly registered events.
389
+ for (const node of newEventNodes) {
390
+ node.callbacks = events[name].callbacks.slice();
391
+ }
392
+ // Add last newly created event to the already registered event.
393
+ events[name].childEvents.push(childEventName);
394
+ }
618
395
  }
619
-
620
396
  // Gets an array containing callbacks list for a given event and it's more specific events.
621
397
  // I.e. if given event is foo:bar and there is also foo:bar:abc event registered, this will
622
398
  // return callback list of foo:bar and foo:bar:abc (but not foo).
623
- function getCallbacksListsForNamespace( source, eventName ) {
624
- const eventNode = getEvents( source )[ eventName ];
625
-
626
- if ( !eventNode ) {
627
- return [];
628
- }
629
-
630
- let callbacksLists = [ eventNode.callbacks ];
631
-
632
- for ( let i = 0; i < eventNode.childEvents.length; i++ ) {
633
- const childCallbacksLists = getCallbacksListsForNamespace( source, eventNode.childEvents[ i ] );
634
-
635
- callbacksLists = callbacksLists.concat( childCallbacksLists );
636
- }
637
-
638
- return callbacksLists;
399
+ function getCallbacksListsForNamespace(source, eventName) {
400
+ const eventNode = getEvents(source)[eventName];
401
+ if (!eventNode) {
402
+ return [];
403
+ }
404
+ let callbacksLists = [eventNode.callbacks];
405
+ for (let i = 0; i < eventNode.childEvents.length; i++) {
406
+ const childCallbacksLists = getCallbacksListsForNamespace(source, eventNode.childEvents[i]);
407
+ callbacksLists = callbacksLists.concat(childCallbacksLists);
408
+ }
409
+ return callbacksLists;
639
410
  }
640
-
641
411
  // Get the list of callbacks for a given event, but only if there any callbacks have been registered.
642
412
  // If there are no callbacks registered for given event, it checks if this is a specific event and looks
643
413
  // for callbacks for it's more generic version.
644
- function getCallbacksForEvent( source, eventName ) {
645
- let event;
646
-
647
- if ( !source._events || !( event = source._events[ eventName ] ) || !event.callbacks.length ) {
648
- // There are no callbacks registered for specified eventName.
649
- // But this could be a specific-type event that is in a namespace.
650
- if ( eventName.indexOf( ':' ) > -1 ) {
651
- // If the eventName is specific, try to find callback lists for more generic event.
652
- return getCallbacksForEvent( source, eventName.substr( 0, eventName.lastIndexOf( ':' ) ) );
653
- } else {
654
- // If this is a top-level generic event, return null;
655
- return null;
656
- }
657
- }
658
-
659
- return event.callbacks;
414
+ function getCallbacksForEvent(source, eventName) {
415
+ let event;
416
+ if (!source._events || !(event = source._events[eventName]) || !event.callbacks.length) {
417
+ // There are no callbacks registered for specified eventName.
418
+ // But this could be a specific-type event that is in a namespace.
419
+ if (eventName.indexOf(':') > -1) {
420
+ // If the eventName is specific, try to find callback lists for more generic event.
421
+ return getCallbacksForEvent(source, eventName.substr(0, eventName.lastIndexOf(':')));
422
+ }
423
+ else {
424
+ // If this is a top-level generic event, return null;
425
+ return null;
426
+ }
427
+ }
428
+ return event.callbacks;
660
429
  }
661
-
662
430
  // Fires delegated events for given map of destinations.
663
431
  //
664
432
  // @private
@@ -666,54 +434,38 @@ function getCallbacksForEvent( source, eventName ) {
666
434
  // `[ {@link module:utils/emittermixin~Emitter}, "event name" ]` pair destinations.
667
435
  // * @param {utils.EventInfo} eventInfo The original event info object.
668
436
  // * @param {Array.<*>} fireArgs Arguments the original event was fired with.
669
- function fireDelegatedEvents( destinations, eventInfo, fireArgs ) {
670
- for ( let [ emitter, name ] of destinations ) {
671
- if ( !name ) {
672
- name = eventInfo.name;
673
- } else if ( typeof name == 'function' ) {
674
- name = name( eventInfo.name );
675
- }
676
-
677
- const delegatedInfo = new EventInfo( eventInfo.source, name );
678
-
679
- delegatedInfo.path = [ ...eventInfo.path ];
680
-
681
- emitter.fire( delegatedInfo, ...fireArgs );
682
- }
437
+ function fireDelegatedEvents(destinations, eventInfo, fireArgs) {
438
+ for (let [emitter, name] of destinations) {
439
+ if (!name) {
440
+ name = eventInfo.name;
441
+ }
442
+ else if (typeof name == 'function') {
443
+ name = name(eventInfo.name);
444
+ }
445
+ const delegatedInfo = new EventInfo(eventInfo.source, name);
446
+ delegatedInfo.path = [...eventInfo.path];
447
+ emitter.fire(delegatedInfo, ...fireArgs);
448
+ }
683
449
  }
684
-
685
450
  // Helper for registering event callback on the emitter.
686
- function addEventListener( listener, emitter, event, callback, options ) {
687
- if ( emitter._addEventListener ) {
688
- emitter._addEventListener( event, callback, options );
689
- } else {
690
- // Allow listening on objects that do not implement Emitter interface.
691
- // This is needed in some tests that are using mocks instead of the real objects with EmitterMixin mixed.
692
- listener._addEventListener.call( emitter, event, callback, options );
693
- }
451
+ function addEventListener(listener, emitter, event, callback, options) {
452
+ if (emitter._addEventListener) {
453
+ emitter._addEventListener(event, callback, options);
454
+ }
455
+ else {
456
+ // Allow listening on objects that do not implement Emitter interface.
457
+ // This is needed in some tests that are using mocks instead of the real objects with EmitterMixin mixed.
458
+ listener._addEventListener.call(emitter, event, callback, options);
459
+ }
694
460
  }
695
-
696
461
  // Helper for removing event callback from the emitter.
697
- function removeEventListener( listener, emitter, event, callback ) {
698
- if ( emitter._removeEventListener ) {
699
- emitter._removeEventListener( event, callback );
700
- } else {
701
- // Allow listening on objects that do not implement Emitter interface.
702
- // This is needed in some tests that are using mocks instead of the real objects with EmitterMixin mixed.
703
- listener._removeEventListener.call( emitter, event, callback );
704
- }
462
+ function removeEventListener(listener, emitter, event, callback) {
463
+ if (emitter._removeEventListener) {
464
+ emitter._removeEventListener(event, callback);
465
+ }
466
+ else {
467
+ // Allow listening on objects that do not implement Emitter interface.
468
+ // This is needed in some tests that are using mocks instead of the real objects with EmitterMixin mixed.
469
+ listener._removeEventListener.call(emitter, event, callback);
470
+ }
705
471
  }
706
-
707
- /**
708
- * The return value of {@link ~EmitterMixin#delegate}.
709
- *
710
- * @interface module:utils/emittermixin~EmitterMixinDelegateChain
711
- */
712
-
713
- /**
714
- * Selects destination for {@link module:utils/emittermixin~EmitterMixin#delegate} events.
715
- *
716
- * @method #to
717
- * @param {module:utils/emittermixin~Emitter} emitter An `EmitterMixin` instance which is the destination for delegated events.
718
- * @param {String|Function} [nameOrFunction] A custom event name or function which converts the original name string.
719
- */