@ckeditor/ckeditor5-utils 34.2.0 → 35.1.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 +324 -0
- package/LICENSE.md +1 -1
- package/package.json +19 -8
- package/src/areconnectedthroughproperties.js +54 -71
- package/src/ckeditorerror.js +92 -114
- package/src/collection.js +594 -762
- package/src/comparearrays.js +22 -28
- package/src/config.js +193 -223
- package/src/count.js +8 -12
- package/src/diff.js +85 -110
- package/src/difftochanges.js +47 -57
- package/src/dom/createelement.js +17 -25
- package/src/dom/emittermixin.js +202 -263
- package/src/dom/getancestors.js +9 -13
- package/src/dom/getborderwidths.js +10 -13
- package/src/dom/getcommonancestor.js +9 -15
- package/src/dom/getdatafromelement.js +5 -9
- package/src/dom/getpositionedancestor.js +9 -14
- package/src/dom/global.js +15 -4
- package/src/dom/indexof.js +7 -11
- package/src/dom/insertat.js +2 -4
- package/src/dom/iscomment.js +2 -5
- package/src/dom/isnode.js +10 -12
- package/src/dom/isrange.js +2 -4
- package/src/dom/istext.js +2 -4
- package/src/dom/isvisible.js +2 -4
- package/src/dom/iswindow.js +11 -16
- package/src/dom/position.js +220 -410
- package/src/dom/rect.js +335 -414
- package/src/dom/remove.js +5 -8
- package/src/dom/resizeobserver.js +109 -342
- package/src/dom/scroll.js +151 -183
- package/src/dom/setdatainelement.js +5 -9
- package/src/dom/tounit.js +10 -12
- package/src/elementreplacer.js +30 -44
- package/src/emittermixin.js +368 -634
- package/src/env.js +109 -116
- package/src/eventinfo.js +12 -65
- package/src/fastdiff.js +96 -128
- package/src/first.js +8 -12
- package/src/focustracker.js +77 -133
- package/src/index.js +0 -9
- package/src/inserttopriorityarray.js +9 -30
- package/src/isiterable.js +2 -4
- package/src/keyboard.js +117 -196
- package/src/keystrokehandler.js +72 -88
- package/src/language.js +9 -15
- package/src/locale.js +61 -158
- package/src/mapsequal.js +12 -17
- package/src/mix.js +17 -16
- package/src/nth.js +8 -11
- package/src/objecttomap.js +7 -11
- package/src/observablemixin.js +474 -778
- package/src/priorities.js +20 -32
- package/src/spy.js +3 -6
- package/src/toarray.js +2 -13
- package/src/tomap.js +8 -10
- package/src/translation-service.js +57 -93
- package/src/uid.js +34 -38
- package/src/unicode.js +28 -43
- package/src/version.js +134 -143
package/src/observablemixin.js
CHANGED
|
@@ -2,22 +2,18 @@
|
|
|
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
|
-
|
|
5
|
+
/* eslint-disable @typescript-eslint/unified-signatures, new-cap */
|
|
6
6
|
/**
|
|
7
7
|
* @module utils/observablemixin
|
|
8
8
|
*/
|
|
9
|
-
|
|
10
|
-
import EmitterMixin from './emittermixin';
|
|
9
|
+
import { Emitter } from './emittermixin';
|
|
11
10
|
import CKEditorError from './ckeditorerror';
|
|
12
|
-
import {
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
const _decoratedMethods = Symbol( 'decoratedMethods' );
|
|
19
|
-
const _decoratedOriginal = Symbol( 'decoratedOriginal' );
|
|
20
|
-
|
|
11
|
+
import { isObject } from 'lodash-es';
|
|
12
|
+
const observablePropertiesSymbol = Symbol('observableProperties');
|
|
13
|
+
const boundObservablesSymbol = Symbol('boundObservables');
|
|
14
|
+
const boundPropertiesSymbol = Symbol('boundProperties');
|
|
15
|
+
const decoratedMethods = Symbol('decoratedMethods');
|
|
16
|
+
const decoratedOriginal = Symbol('decoratedOriginal');
|
|
21
17
|
/**
|
|
22
18
|
* A mixin that injects the "observable properties" and data binding functionality described in the
|
|
23
19
|
* {@link ~Observable} interface.
|
|
@@ -31,484 +27,408 @@ const _decoratedOriginal = Symbol( 'decoratedOriginal' );
|
|
|
31
27
|
* @mixes module:utils/emittermixin~EmitterMixin
|
|
32
28
|
* @implements module:utils/observablemixin~Observable
|
|
33
29
|
*/
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
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
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
* Cannot decorate an undefined method.
|
|
245
|
-
*
|
|
246
|
-
* @error observablemixin-cannot-decorate-undefined
|
|
247
|
-
* @param {Object} object The object which method should be decorated.
|
|
248
|
-
* @param {String} methodName Name of the method which does not exist.
|
|
249
|
-
*/
|
|
250
|
-
throw new CKEditorError(
|
|
251
|
-
'observablemixin-cannot-decorate-undefined',
|
|
252
|
-
this,
|
|
253
|
-
{ object: this, methodName }
|
|
254
|
-
);
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
this.on( methodName, ( evt, args ) => {
|
|
258
|
-
evt.return = originalMethod.apply( this, args );
|
|
259
|
-
} );
|
|
260
|
-
|
|
261
|
-
this[ methodName ] = function( ...args ) {
|
|
262
|
-
return this.fire( methodName, args );
|
|
263
|
-
};
|
|
264
|
-
|
|
265
|
-
this[ methodName ][ _decoratedOriginal ] = originalMethod;
|
|
266
|
-
|
|
267
|
-
if ( !this[ _decoratedMethods ] ) {
|
|
268
|
-
this[ _decoratedMethods ] = [];
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
this[ _decoratedMethods ].push( methodName );
|
|
272
|
-
}
|
|
273
|
-
};
|
|
274
|
-
|
|
275
|
-
extend( ObservableMixin, EmitterMixin );
|
|
276
|
-
|
|
277
|
-
// Override the EmitterMixin stopListening method to be able to clean (and restore) decorated methods.
|
|
278
|
-
// This is needed in case of:
|
|
279
|
-
// 1. Have x.foo() decorated.
|
|
280
|
-
// 2. Call x.stopListening()
|
|
281
|
-
// 3. Call x.foo(). Problem: nothing happens (the original foo() method is not executed)
|
|
282
|
-
ObservableMixin.stopListening = function( emitter, event, callback ) {
|
|
283
|
-
// Removing all listeners so let's clean the decorated methods to the original state.
|
|
284
|
-
if ( !emitter && this[ _decoratedMethods ] ) {
|
|
285
|
-
for ( const methodName of this[ _decoratedMethods ] ) {
|
|
286
|
-
this[ methodName ] = this[ methodName ][ _decoratedOriginal ];
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
delete this[ _decoratedMethods ];
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
EmitterMixin.stopListening.call( this, emitter, event, callback );
|
|
293
|
-
};
|
|
294
|
-
|
|
295
|
-
export default ObservableMixin;
|
|
296
|
-
|
|
30
|
+
export default function ObservableMixin(base) {
|
|
31
|
+
class Mixin extends base {
|
|
32
|
+
set(name, value) {
|
|
33
|
+
// If the first parameter is an Object, iterate over its properties.
|
|
34
|
+
if (isObject(name)) {
|
|
35
|
+
Object.keys(name).forEach(property => {
|
|
36
|
+
this.set(property, name[property]);
|
|
37
|
+
}, this);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
initObservable(this);
|
|
41
|
+
const properties = this[observablePropertiesSymbol];
|
|
42
|
+
if ((name in this) && !properties.has(name)) {
|
|
43
|
+
/**
|
|
44
|
+
* Cannot override an existing property.
|
|
45
|
+
*
|
|
46
|
+
* This error is thrown when trying to {@link ~Observable#set set} a property with
|
|
47
|
+
* a name of an already existing property. For example:
|
|
48
|
+
*
|
|
49
|
+
* let observable = new Model();
|
|
50
|
+
* observable.property = 1;
|
|
51
|
+
* observable.set( 'property', 2 ); // throws
|
|
52
|
+
*
|
|
53
|
+
* observable.set( 'property', 1 );
|
|
54
|
+
* observable.set( 'property', 2 ); // ok, because this is an existing property.
|
|
55
|
+
*
|
|
56
|
+
* @error observable-set-cannot-override
|
|
57
|
+
*/
|
|
58
|
+
throw new CKEditorError('observable-set-cannot-override', this);
|
|
59
|
+
}
|
|
60
|
+
Object.defineProperty(this, name, {
|
|
61
|
+
enumerable: true,
|
|
62
|
+
configurable: true,
|
|
63
|
+
get() {
|
|
64
|
+
return properties.get(name);
|
|
65
|
+
},
|
|
66
|
+
set(value) {
|
|
67
|
+
const oldValue = properties.get(name);
|
|
68
|
+
// Fire `set` event before the new value will be set to make it possible
|
|
69
|
+
// to override observable property without affecting `change` event.
|
|
70
|
+
// See https://github.com/ckeditor/ckeditor5-utils/issues/171.
|
|
71
|
+
let newValue = this.fire(`set:${name}`, name, value, oldValue);
|
|
72
|
+
if (newValue === undefined) {
|
|
73
|
+
newValue = value;
|
|
74
|
+
}
|
|
75
|
+
// Allow undefined as an initial value like A.define( 'x', undefined ) (#132).
|
|
76
|
+
// Note: When properties map has no such own property, then its value is undefined.
|
|
77
|
+
if (oldValue !== newValue || !properties.has(name)) {
|
|
78
|
+
properties.set(name, newValue);
|
|
79
|
+
this.fire(`change:${name}`, name, newValue, oldValue);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
this[name] = value;
|
|
84
|
+
}
|
|
85
|
+
bind(...bindProperties) {
|
|
86
|
+
if (!bindProperties.length || !isStringArray(bindProperties)) {
|
|
87
|
+
/**
|
|
88
|
+
* All properties must be strings.
|
|
89
|
+
*
|
|
90
|
+
* @error observable-bind-wrong-properties
|
|
91
|
+
*/
|
|
92
|
+
throw new CKEditorError('observable-bind-wrong-properties', this);
|
|
93
|
+
}
|
|
94
|
+
if ((new Set(bindProperties)).size !== bindProperties.length) {
|
|
95
|
+
/**
|
|
96
|
+
* Properties must be unique.
|
|
97
|
+
*
|
|
98
|
+
* @error observable-bind-duplicate-properties
|
|
99
|
+
*/
|
|
100
|
+
throw new CKEditorError('observable-bind-duplicate-properties', this);
|
|
101
|
+
}
|
|
102
|
+
initObservable(this);
|
|
103
|
+
const boundProperties = this[boundPropertiesSymbol];
|
|
104
|
+
bindProperties.forEach(propertyName => {
|
|
105
|
+
if (boundProperties.has(propertyName)) {
|
|
106
|
+
/**
|
|
107
|
+
* Cannot bind the same property more than once.
|
|
108
|
+
*
|
|
109
|
+
* @error observable-bind-rebind
|
|
110
|
+
*/
|
|
111
|
+
throw new CKEditorError('observable-bind-rebind', this);
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
const bindings = new Map();
|
|
115
|
+
// @typedef {Object} Binding
|
|
116
|
+
// @property {Array} property Property which is bound.
|
|
117
|
+
// @property {Array} to Array of observable–property components of the binding (`{ observable: ..., property: .. }`).
|
|
118
|
+
// @property {Array} callback A function which processes `to` components.
|
|
119
|
+
bindProperties.forEach(a => {
|
|
120
|
+
const binding = { property: a, to: [] };
|
|
121
|
+
boundProperties.set(a, binding);
|
|
122
|
+
bindings.set(a, binding);
|
|
123
|
+
});
|
|
124
|
+
// @typedef {Object} BindChain
|
|
125
|
+
// @property {Function} to See {@link ~ObservableMixin#_bindTo}.
|
|
126
|
+
// @property {Function} toMany See {@link ~ObservableMixin#_bindToMany}.
|
|
127
|
+
// @property {module:utils/observablemixin~Observable} _observable The observable which initializes the binding.
|
|
128
|
+
// @property {Array} _bindProperties Array of `_observable` properties to be bound.
|
|
129
|
+
// @property {Array} _to Array of `to()` observable–properties (`{ observable: toObservable, properties: ...toProperties }`).
|
|
130
|
+
// @property {Map} _bindings Stores bindings to be kept in
|
|
131
|
+
// {@link ~ObservableMixin#_boundProperties}/{@link ~ObservableMixin#_boundObservables}
|
|
132
|
+
// initiated in this binding chain.
|
|
133
|
+
return {
|
|
134
|
+
to: bindTo,
|
|
135
|
+
toMany: bindToMany,
|
|
136
|
+
_observable: this,
|
|
137
|
+
_bindProperties: bindProperties,
|
|
138
|
+
_to: [],
|
|
139
|
+
_bindings: bindings
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
unbind(...unbindProperties) {
|
|
143
|
+
// Nothing to do here if not inited yet.
|
|
144
|
+
if (!(this[observablePropertiesSymbol])) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const boundProperties = this[boundPropertiesSymbol];
|
|
148
|
+
const boundObservables = this[boundObservablesSymbol];
|
|
149
|
+
if (unbindProperties.length) {
|
|
150
|
+
if (!isStringArray(unbindProperties)) {
|
|
151
|
+
/**
|
|
152
|
+
* Properties must be strings.
|
|
153
|
+
*
|
|
154
|
+
* @error observable-unbind-wrong-properties
|
|
155
|
+
*/
|
|
156
|
+
throw new CKEditorError('observable-unbind-wrong-properties', this);
|
|
157
|
+
}
|
|
158
|
+
unbindProperties.forEach(propertyName => {
|
|
159
|
+
const binding = boundProperties.get(propertyName);
|
|
160
|
+
// Nothing to do if the binding is not defined
|
|
161
|
+
if (!binding) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
binding.to.forEach(([toObservable, toProperty]) => {
|
|
165
|
+
const toProperties = boundObservables.get(toObservable);
|
|
166
|
+
const toPropertyBindings = toProperties[toProperty];
|
|
167
|
+
toPropertyBindings.delete(binding);
|
|
168
|
+
if (!toPropertyBindings.size) {
|
|
169
|
+
delete toProperties[toProperty];
|
|
170
|
+
}
|
|
171
|
+
if (!Object.keys(toProperties).length) {
|
|
172
|
+
boundObservables.delete(toObservable);
|
|
173
|
+
this.stopListening(toObservable, 'change');
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
boundProperties.delete(propertyName);
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
boundObservables.forEach((bindings, boundObservable) => {
|
|
181
|
+
this.stopListening(boundObservable, 'change');
|
|
182
|
+
});
|
|
183
|
+
boundObservables.clear();
|
|
184
|
+
boundProperties.clear();
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
decorate(methodName) {
|
|
188
|
+
initObservable(this);
|
|
189
|
+
const originalMethod = this[methodName];
|
|
190
|
+
if (!originalMethod) {
|
|
191
|
+
/**
|
|
192
|
+
* Cannot decorate an undefined method.
|
|
193
|
+
*
|
|
194
|
+
* @error observablemixin-cannot-decorate-undefined
|
|
195
|
+
* @param {Object} object The object which method should be decorated.
|
|
196
|
+
* @param {String} methodName Name of the method which does not exist.
|
|
197
|
+
*/
|
|
198
|
+
throw new CKEditorError('observablemixin-cannot-decorate-undefined', this, { object: this, methodName });
|
|
199
|
+
}
|
|
200
|
+
this.on(methodName, (evt, args) => {
|
|
201
|
+
evt.return = originalMethod.apply(this, args);
|
|
202
|
+
});
|
|
203
|
+
this[methodName] = function (...args) {
|
|
204
|
+
return this.fire(methodName, args);
|
|
205
|
+
};
|
|
206
|
+
this[methodName][decoratedOriginal] = originalMethod;
|
|
207
|
+
if (!this[decoratedMethods]) {
|
|
208
|
+
this[decoratedMethods] = [];
|
|
209
|
+
}
|
|
210
|
+
this[decoratedMethods].push(methodName);
|
|
211
|
+
}
|
|
212
|
+
// Override the EmitterMixin stopListening method to be able to clean (and restore) decorated methods.
|
|
213
|
+
// This is needed in case of:
|
|
214
|
+
// 1. Have x.foo() decorated.
|
|
215
|
+
// 2. Call x.stopListening()
|
|
216
|
+
// 3. Call x.foo(). Problem: nothing happens (the original foo() method is not executed)
|
|
217
|
+
stopListening(emitter, event, callback) {
|
|
218
|
+
// Removing all listeners so let's clean the decorated methods to the original state.
|
|
219
|
+
if (!emitter && this[decoratedMethods]) {
|
|
220
|
+
for (const methodName of this[decoratedMethods]) {
|
|
221
|
+
this[methodName] = this[methodName][decoratedOriginal];
|
|
222
|
+
}
|
|
223
|
+
delete this[decoratedMethods];
|
|
224
|
+
}
|
|
225
|
+
Emitter.prototype.stopListening.call(this, emitter, event, callback);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return Mixin;
|
|
229
|
+
}
|
|
230
|
+
export const Observable = ObservableMixin(Emitter);
|
|
231
|
+
// Backward compatibility with `mix`
|
|
232
|
+
([
|
|
233
|
+
'set', 'bind', 'unbind', 'decorate',
|
|
234
|
+
'on', 'once', 'off', 'listenTo',
|
|
235
|
+
'stopListening', 'fire', 'delegate', 'stopDelegating',
|
|
236
|
+
'_addEventListener', '_removeEventListener'
|
|
237
|
+
]).forEach(key => {
|
|
238
|
+
ObservableMixin[key] = Observable.prototype[key];
|
|
239
|
+
});
|
|
297
240
|
// Init symbol properties needed for the observable mechanism to work.
|
|
298
241
|
//
|
|
299
242
|
// @private
|
|
300
243
|
// @param {module:utils/observablemixin~ObservableMixin} observable
|
|
301
|
-
function initObservable(
|
|
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
|
-
Object.defineProperty( observable, boundPropertiesSymbol, {
|
|
394
|
-
value: new Map()
|
|
395
|
-
} );
|
|
244
|
+
function initObservable(observable) {
|
|
245
|
+
// Do nothing if already inited.
|
|
246
|
+
if (observable[observablePropertiesSymbol]) {
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
// The internal hash containing the observable's state.
|
|
250
|
+
//
|
|
251
|
+
// @private
|
|
252
|
+
// @type {Map}
|
|
253
|
+
Object.defineProperty(observable, observablePropertiesSymbol, {
|
|
254
|
+
value: new Map()
|
|
255
|
+
});
|
|
256
|
+
// Map containing bindings to external observables. It shares the binding objects
|
|
257
|
+
// (`{ observable: A, property: 'a', to: ... }`) with {@link module:utils/observablemixin~ObservableMixin#_boundProperties} and
|
|
258
|
+
// it is used to observe external observables to update own properties accordingly.
|
|
259
|
+
// See {@link module:utils/observablemixin~ObservableMixin#bind}.
|
|
260
|
+
//
|
|
261
|
+
// A.bind( 'a', 'b', 'c' ).to( B, 'x', 'y', 'x' );
|
|
262
|
+
// console.log( A._boundObservables );
|
|
263
|
+
//
|
|
264
|
+
// Map( {
|
|
265
|
+
// B: {
|
|
266
|
+
// x: Set( [
|
|
267
|
+
// { observable: A, property: 'a', to: [ [ B, 'x' ] ] },
|
|
268
|
+
// { observable: A, property: 'c', to: [ [ B, 'x' ] ] }
|
|
269
|
+
// ] ),
|
|
270
|
+
// y: Set( [
|
|
271
|
+
// { observable: A, property: 'b', to: [ [ B, 'y' ] ] },
|
|
272
|
+
// ] )
|
|
273
|
+
// }
|
|
274
|
+
// } )
|
|
275
|
+
//
|
|
276
|
+
// A.bind( 'd' ).to( B, 'z' ).to( C, 'w' ).as( callback );
|
|
277
|
+
// console.log( A._boundObservables );
|
|
278
|
+
//
|
|
279
|
+
// Map( {
|
|
280
|
+
// B: {
|
|
281
|
+
// x: Set( [
|
|
282
|
+
// { observable: A, property: 'a', to: [ [ B, 'x' ] ] },
|
|
283
|
+
// { observable: A, property: 'c', to: [ [ B, 'x' ] ] }
|
|
284
|
+
// ] ),
|
|
285
|
+
// y: Set( [
|
|
286
|
+
// { observable: A, property: 'b', to: [ [ B, 'y' ] ] },
|
|
287
|
+
// ] ),
|
|
288
|
+
// z: Set( [
|
|
289
|
+
// { observable: A, property: 'd', to: [ [ B, 'z' ], [ C, 'w' ] ], callback: callback }
|
|
290
|
+
// ] )
|
|
291
|
+
// },
|
|
292
|
+
// C: {
|
|
293
|
+
// w: Set( [
|
|
294
|
+
// { observable: A, property: 'd', to: [ [ B, 'z' ], [ C, 'w' ] ], callback: callback }
|
|
295
|
+
// ] )
|
|
296
|
+
// }
|
|
297
|
+
// } )
|
|
298
|
+
//
|
|
299
|
+
// @private
|
|
300
|
+
// @type {Map}
|
|
301
|
+
Object.defineProperty(observable, boundObservablesSymbol, {
|
|
302
|
+
value: new Map()
|
|
303
|
+
});
|
|
304
|
+
// Object that stores which properties of this observable are bound and how. It shares
|
|
305
|
+
// the binding objects (`{ observable: A, property: 'a', to: ... }`) with
|
|
306
|
+
// {@link module:utils/observablemixin~ObservableMixin#_boundObservables}. This data structure is
|
|
307
|
+
// a reverse of {@link module:utils/observablemixin~ObservableMixin#_boundObservables} and it is helpful for
|
|
308
|
+
// {@link module:utils/observablemixin~ObservableMixin#unbind}.
|
|
309
|
+
//
|
|
310
|
+
// See {@link module:utils/observablemixin~ObservableMixin#bind}.
|
|
311
|
+
//
|
|
312
|
+
// A.bind( 'a', 'b', 'c' ).to( B, 'x', 'y', 'x' );
|
|
313
|
+
// console.log( A._boundProperties );
|
|
314
|
+
//
|
|
315
|
+
// Map( {
|
|
316
|
+
// a: { observable: A, property: 'a', to: [ [ B, 'x' ] ] },
|
|
317
|
+
// b: { observable: A, property: 'b', to: [ [ B, 'y' ] ] },
|
|
318
|
+
// c: { observable: A, property: 'c', to: [ [ B, 'x' ] ] }
|
|
319
|
+
// } )
|
|
320
|
+
//
|
|
321
|
+
// A.bind( 'd' ).to( B, 'z' ).to( C, 'w' ).as( callback );
|
|
322
|
+
// console.log( A._boundProperties );
|
|
323
|
+
//
|
|
324
|
+
// Map( {
|
|
325
|
+
// a: { observable: A, property: 'a', to: [ [ B, 'x' ] ] },
|
|
326
|
+
// b: { observable: A, property: 'b', to: [ [ B, 'y' ] ] },
|
|
327
|
+
// c: { observable: A, property: 'c', to: [ [ B, 'x' ] ] },
|
|
328
|
+
// d: { observable: A, property: 'd', to: [ [ B, 'z' ], [ C, 'w' ] ], callback: callback }
|
|
329
|
+
// } )
|
|
330
|
+
//
|
|
331
|
+
// @private
|
|
332
|
+
// @type {Map}
|
|
333
|
+
Object.defineProperty(observable, boundPropertiesSymbol, {
|
|
334
|
+
value: new Map()
|
|
335
|
+
});
|
|
396
336
|
}
|
|
397
|
-
|
|
398
337
|
// A chaining for {@link module:utils/observablemixin~ObservableMixin#bind} providing `.to()` interface.
|
|
399
338
|
//
|
|
400
339
|
// @private
|
|
401
340
|
// @param {...[Observable|String|Function]} args Arguments of the `.to( args )` binding.
|
|
402
|
-
function bindTo(
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
this._bindings.get( bindingsKeys[ 0 ] ).callback = parsedArgs.callback;
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
attachBindToListeners( this._observable, this._to );
|
|
456
|
-
|
|
457
|
-
// Update observable._boundProperties and observable._boundObservables.
|
|
458
|
-
updateBindToBound( this );
|
|
459
|
-
|
|
460
|
-
// Set initial values of bound properties.
|
|
461
|
-
this._bindProperties.forEach( propertyName => {
|
|
462
|
-
updateBoundObservableProperty( this._observable, propertyName );
|
|
463
|
-
} );
|
|
341
|
+
function bindTo(...args) {
|
|
342
|
+
const parsedArgs = parseBindToArgs(...args);
|
|
343
|
+
const bindingsKeys = Array.from(this._bindings.keys());
|
|
344
|
+
const numberOfBindings = bindingsKeys.length;
|
|
345
|
+
// Eliminate A.bind( 'x' ).to( B, C )
|
|
346
|
+
if (!parsedArgs.callback && parsedArgs.to.length > 1) {
|
|
347
|
+
/**
|
|
348
|
+
* Binding multiple observables only possible with callback.
|
|
349
|
+
*
|
|
350
|
+
* @error observable-bind-to-no-callback
|
|
351
|
+
*/
|
|
352
|
+
throw new CKEditorError('observable-bind-to-no-callback', this);
|
|
353
|
+
}
|
|
354
|
+
// Eliminate A.bind( 'x', 'y' ).to( B, callback )
|
|
355
|
+
if (numberOfBindings > 1 && parsedArgs.callback) {
|
|
356
|
+
/**
|
|
357
|
+
* Cannot bind multiple properties and use a callback in one binding.
|
|
358
|
+
*
|
|
359
|
+
* @error observable-bind-to-extra-callback
|
|
360
|
+
*/
|
|
361
|
+
throw new CKEditorError('observable-bind-to-extra-callback', this);
|
|
362
|
+
}
|
|
363
|
+
parsedArgs.to.forEach(to => {
|
|
364
|
+
// Eliminate A.bind( 'x', 'y' ).to( B, 'a' )
|
|
365
|
+
if (to.properties.length && to.properties.length !== numberOfBindings) {
|
|
366
|
+
/**
|
|
367
|
+
* The number of properties must match.
|
|
368
|
+
*
|
|
369
|
+
* @error observable-bind-to-properties-length
|
|
370
|
+
*/
|
|
371
|
+
throw new CKEditorError('observable-bind-to-properties-length', this);
|
|
372
|
+
}
|
|
373
|
+
// When no to.properties specified, observing source properties instead i.e.
|
|
374
|
+
// A.bind( 'x', 'y' ).to( B ) -> Observe B.x and B.y
|
|
375
|
+
if (!to.properties.length) {
|
|
376
|
+
to.properties = this._bindProperties;
|
|
377
|
+
}
|
|
378
|
+
});
|
|
379
|
+
this._to = parsedArgs.to;
|
|
380
|
+
// Fill {@link BindChain#_bindings} with callback. When the callback is set there's only one binding.
|
|
381
|
+
if (parsedArgs.callback) {
|
|
382
|
+
this._bindings.get(bindingsKeys[0]).callback = parsedArgs.callback;
|
|
383
|
+
}
|
|
384
|
+
attachBindToListeners(this._observable, this._to);
|
|
385
|
+
// Update observable._boundProperties and observable._boundObservables.
|
|
386
|
+
updateBindToBound(this);
|
|
387
|
+
// Set initial values of bound properties.
|
|
388
|
+
this._bindProperties.forEach(propertyName => {
|
|
389
|
+
updateBoundObservableProperty(this._observable, propertyName);
|
|
390
|
+
});
|
|
464
391
|
}
|
|
465
|
-
|
|
466
392
|
// Binds to an attribute in a set of iterable observables.
|
|
467
393
|
//
|
|
468
394
|
// @private
|
|
469
395
|
// @param {Array.<Observable>} observables
|
|
470
396
|
// @param {String} attribute
|
|
471
397
|
// @param {Function} callback
|
|
472
|
-
function bindToMany(
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
callback
|
|
487
|
-
);
|
|
398
|
+
function bindToMany(observables, attribute, callback) {
|
|
399
|
+
if (this._bindings.size > 1) {
|
|
400
|
+
/**
|
|
401
|
+
* Binding one attribute to many observables only possible with one attribute.
|
|
402
|
+
*
|
|
403
|
+
* @error observable-bind-to-many-not-one-binding
|
|
404
|
+
*/
|
|
405
|
+
throw new CKEditorError('observable-bind-to-many-not-one-binding', this);
|
|
406
|
+
}
|
|
407
|
+
this.to(
|
|
408
|
+
// Bind to #attribute of each observable...
|
|
409
|
+
...getBindingTargets(observables, attribute),
|
|
410
|
+
// ...using given callback to parse attribute values.
|
|
411
|
+
callback);
|
|
488
412
|
}
|
|
489
|
-
|
|
490
413
|
// Returns an array of binding components for
|
|
491
414
|
// {@link Observable#bind} from a set of iterable observables.
|
|
492
415
|
//
|
|
493
416
|
// @param {Array.<Observable>} observables
|
|
494
417
|
// @param {String} attribute
|
|
495
418
|
// @returns {Array.<String|Observable>}
|
|
496
|
-
function getBindingTargets(
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
return Array.prototype.concat.apply( [], observableAndAttributePairs );
|
|
419
|
+
function getBindingTargets(observables, attribute) {
|
|
420
|
+
const observableAndAttributePairs = observables.map(observable => [observable, attribute]);
|
|
421
|
+
// Merge pairs to one-dimension array of observables and attributes.
|
|
422
|
+
return Array.prototype.concat.apply([], observableAndAttributePairs);
|
|
501
423
|
}
|
|
502
|
-
|
|
503
424
|
// Check if all entries of the array are of `String` type.
|
|
504
425
|
//
|
|
505
426
|
// @private
|
|
506
427
|
// @param {Array} arr An array to be checked.
|
|
507
428
|
// @returns {Boolean}
|
|
508
|
-
function isStringArray(
|
|
509
|
-
|
|
429
|
+
function isStringArray(arr) {
|
|
430
|
+
return arr.every(a => typeof a == 'string');
|
|
510
431
|
}
|
|
511
|
-
|
|
512
432
|
// Parses and validates {@link Observable#bind}`.to( args )` arguments and returns
|
|
513
433
|
// an object with a parsed structure. For example
|
|
514
434
|
//
|
|
@@ -527,61 +447,54 @@ function isStringArray( arr ) {
|
|
|
527
447
|
// @private
|
|
528
448
|
// @param {...*} args Arguments of {@link Observable#bind}`.to( args )`.
|
|
529
449
|
// @returns {Object}
|
|
530
|
-
function parseBindToArgs(
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
return parsed;
|
|
450
|
+
function parseBindToArgs(...args) {
|
|
451
|
+
// Eliminate A.bind( 'x' ).to()
|
|
452
|
+
if (!args.length) {
|
|
453
|
+
/**
|
|
454
|
+
* Invalid argument syntax in `to()`.
|
|
455
|
+
*
|
|
456
|
+
* @error observable-bind-to-parse-error
|
|
457
|
+
*/
|
|
458
|
+
throw new CKEditorError('observable-bind-to-parse-error', null);
|
|
459
|
+
}
|
|
460
|
+
const parsed = { to: [] };
|
|
461
|
+
let lastObservable;
|
|
462
|
+
if (typeof args[args.length - 1] == 'function') {
|
|
463
|
+
parsed.callback = args.pop();
|
|
464
|
+
}
|
|
465
|
+
args.forEach(a => {
|
|
466
|
+
if (typeof a == 'string') {
|
|
467
|
+
lastObservable.properties.push(a);
|
|
468
|
+
}
|
|
469
|
+
else if (typeof a == 'object') {
|
|
470
|
+
lastObservable = { observable: a, properties: [] };
|
|
471
|
+
parsed.to.push(lastObservable);
|
|
472
|
+
}
|
|
473
|
+
else {
|
|
474
|
+
throw new CKEditorError('observable-bind-to-parse-error', null);
|
|
475
|
+
}
|
|
476
|
+
});
|
|
477
|
+
return parsed;
|
|
560
478
|
}
|
|
561
|
-
|
|
562
479
|
// Synchronizes {@link module:utils/observablemixin#_boundObservables} with {@link Binding}.
|
|
563
480
|
//
|
|
564
481
|
// @private
|
|
565
482
|
// @param {Binding} binding A binding to store in {@link Observable#_boundObservables}.
|
|
566
483
|
// @param {Observable} toObservable A observable, which is a new component of `binding`.
|
|
567
484
|
// @param {String} toPropertyName A name of `toObservable`'s property, a new component of the `binding`.
|
|
568
|
-
function updateBoundObservables(
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
if ( !bindingsToObservable ) {
|
|
581
|
-
boundObservables.set( toObservable, bindings );
|
|
582
|
-
}
|
|
485
|
+
function updateBoundObservables(observable, binding, toObservable, toPropertyName) {
|
|
486
|
+
const boundObservables = observable[boundObservablesSymbol];
|
|
487
|
+
const bindingsToObservable = boundObservables.get(toObservable);
|
|
488
|
+
const bindings = bindingsToObservable || {};
|
|
489
|
+
if (!bindings[toPropertyName]) {
|
|
490
|
+
bindings[toPropertyName] = new Set();
|
|
491
|
+
}
|
|
492
|
+
// Pass the binding to a corresponding Set in `observable._boundObservables`.
|
|
493
|
+
bindings[toPropertyName].add(binding);
|
|
494
|
+
if (!bindingsToObservable) {
|
|
495
|
+
boundObservables.set(toObservable, bindings);
|
|
496
|
+
}
|
|
583
497
|
}
|
|
584
|
-
|
|
585
498
|
// Synchronizes {@link Observable#_boundProperties} and {@link Observable#_boundObservables}
|
|
586
499
|
// with {@link BindChain}.
|
|
587
500
|
//
|
|
@@ -620,289 +533,72 @@ function updateBoundObservables( observable, binding, toObservable, toPropertyNa
|
|
|
620
533
|
//
|
|
621
534
|
// @private
|
|
622
535
|
// @param {BindChain} chain The binding initialized by {@link Observable#bind}.
|
|
623
|
-
function updateBindToBound(
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
} );
|
|
636
|
-
} );
|
|
536
|
+
function updateBindToBound(chain) {
|
|
537
|
+
let toProperty;
|
|
538
|
+
chain._bindings.forEach((binding, propertyName) => {
|
|
539
|
+
// Note: For a binding without a callback, this will run only once
|
|
540
|
+
// like in A.bind( 'x', 'y' ).to( B, 'a', 'b' )
|
|
541
|
+
// TODO: ES6 destructuring.
|
|
542
|
+
chain._to.forEach(to => {
|
|
543
|
+
toProperty = to.properties[binding.callback ? 0 : chain._bindProperties.indexOf(propertyName)];
|
|
544
|
+
binding.to.push([to.observable, toProperty]);
|
|
545
|
+
updateBoundObservables(chain._observable, binding, to.observable, toProperty);
|
|
546
|
+
});
|
|
547
|
+
});
|
|
637
548
|
}
|
|
638
|
-
|
|
639
549
|
// Updates an property of a {@link Observable} with a value
|
|
640
550
|
// determined by an entry in {@link Observable#_boundProperties}.
|
|
641
551
|
//
|
|
642
552
|
// @private
|
|
643
553
|
// @param {Observable} observable A observable which property is to be updated.
|
|
644
554
|
// @param {String} propertyName An property to be updated.
|
|
645
|
-
function updateBoundObservableProperty(
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
555
|
+
function updateBoundObservableProperty(observable, propertyName) {
|
|
556
|
+
const boundProperties = observable[boundPropertiesSymbol];
|
|
557
|
+
const binding = boundProperties.get(propertyName);
|
|
558
|
+
let propertyValue;
|
|
559
|
+
// When a binding with callback is created like
|
|
560
|
+
//
|
|
561
|
+
// A.bind( 'a' ).to( B, 'b', C, 'c', callback );
|
|
562
|
+
//
|
|
563
|
+
// collect B.b and C.c, then pass them to callback to set A.a.
|
|
564
|
+
if (binding.callback) {
|
|
565
|
+
propertyValue = binding.callback.apply(observable, binding.to.map(to => to[0][to[1]]));
|
|
566
|
+
}
|
|
567
|
+
else {
|
|
568
|
+
propertyValue = binding.to[0];
|
|
569
|
+
propertyValue = propertyValue[0][propertyValue[1]];
|
|
570
|
+
}
|
|
571
|
+
if (Object.prototype.hasOwnProperty.call(observable, propertyName)) {
|
|
572
|
+
observable[propertyName] = propertyValue;
|
|
573
|
+
}
|
|
574
|
+
else {
|
|
575
|
+
observable.set(propertyName, propertyValue);
|
|
576
|
+
}
|
|
667
577
|
}
|
|
668
|
-
|
|
669
578
|
// Starts listening to changes in {@link BindChain._to} observables to update
|
|
670
579
|
// {@link BindChain._observable} {@link BindChain._bindProperties}. Also sets the
|
|
671
580
|
// initial state of {@link BindChain._observable}.
|
|
672
581
|
//
|
|
673
582
|
// @private
|
|
583
|
+
// @param {Observable} observable
|
|
674
584
|
// @param {BindChain} chain The chain initialized by {@link Observable#bind}.
|
|
675
|
-
function attachBindToListeners(
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
}
|
|
695
|
-
} );
|
|
585
|
+
function attachBindToListeners(observable, toBindings) {
|
|
586
|
+
toBindings.forEach(to => {
|
|
587
|
+
const boundObservables = observable[boundObservablesSymbol];
|
|
588
|
+
let bindings;
|
|
589
|
+
// If there's already a chain between the observables (`observable` listens to
|
|
590
|
+
// `to.observable`), there's no need to create another `change` event listener.
|
|
591
|
+
if (!boundObservables.get(to.observable)) {
|
|
592
|
+
observable.listenTo(to.observable, 'change', (evt, propertyName) => {
|
|
593
|
+
bindings = boundObservables.get(to.observable)[propertyName];
|
|
594
|
+
// Note: to.observable will fire for any property change, react
|
|
595
|
+
// to changes of properties which are bound only.
|
|
596
|
+
if (bindings) {
|
|
597
|
+
bindings.forEach(binding => {
|
|
598
|
+
updateBoundObservableProperty(observable, binding.property);
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
});
|
|
602
|
+
}
|
|
603
|
+
});
|
|
696
604
|
}
|
|
697
|
-
|
|
698
|
-
/**
|
|
699
|
-
* An interface which adds "observable properties" and data binding functionality.
|
|
700
|
-
*
|
|
701
|
-
* Can be easily implemented by a class by mixing the {@link module:utils/observablemixin~ObservableMixin} mixin.
|
|
702
|
-
*
|
|
703
|
-
* Read more about the usage of this interface in the:
|
|
704
|
-
* * {@glink framework/guides/architecture/core-editor-architecture#event-system-and-observables Event system and observables}
|
|
705
|
-
* section of the {@glink framework/guides/architecture/core-editor-architecture Core editor architecture} guide,
|
|
706
|
-
* * {@glink framework/guides/deep-dive/observables Observables deep dive} guide.
|
|
707
|
-
*
|
|
708
|
-
* @interface Observable
|
|
709
|
-
* @extends module:utils/emittermixin~Emitter
|
|
710
|
-
*/
|
|
711
|
-
|
|
712
|
-
/**
|
|
713
|
-
* Fired when a property changed value.
|
|
714
|
-
*
|
|
715
|
-
* observable.set( 'prop', 1 );
|
|
716
|
-
*
|
|
717
|
-
* observable.on( 'change:prop', ( evt, propertyName, newValue, oldValue ) => {
|
|
718
|
-
* console.log( `${ propertyName } has changed from ${ oldValue } to ${ newValue }` );
|
|
719
|
-
* } );
|
|
720
|
-
*
|
|
721
|
-
* observable.prop = 2; // -> 'prop has changed from 1 to 2'
|
|
722
|
-
*
|
|
723
|
-
* @event change:{property}
|
|
724
|
-
* @param {String} name The property name.
|
|
725
|
-
* @param {*} value The new property value.
|
|
726
|
-
* @param {*} oldValue The previous property value.
|
|
727
|
-
*/
|
|
728
|
-
|
|
729
|
-
/**
|
|
730
|
-
* Fired when a property value is going to be set but is not set yet (before the `change` event is fired).
|
|
731
|
-
*
|
|
732
|
-
* You can control the final value of the property by using
|
|
733
|
-
* the {@link module:utils/eventinfo~EventInfo#return event's `return` property}.
|
|
734
|
-
*
|
|
735
|
-
* observable.set( 'prop', 1 );
|
|
736
|
-
*
|
|
737
|
-
* observable.on( 'set:prop', ( evt, propertyName, newValue, oldValue ) => {
|
|
738
|
-
* console.log( `Value is going to be changed from ${ oldValue } to ${ newValue }` );
|
|
739
|
-
* console.log( `Current property value is ${ observable[ propertyName ] }` );
|
|
740
|
-
*
|
|
741
|
-
* // Let's override the value.
|
|
742
|
-
* evt.return = 3;
|
|
743
|
-
* } );
|
|
744
|
-
*
|
|
745
|
-
* observable.on( 'change:prop', ( evt, propertyName, newValue, oldValue ) => {
|
|
746
|
-
* console.log( `Value has changed from ${ oldValue } to ${ newValue }` );
|
|
747
|
-
* } );
|
|
748
|
-
*
|
|
749
|
-
* observable.prop = 2; // -> 'Value is going to be changed from 1 to 2'
|
|
750
|
-
* // -> 'Current property value is 1'
|
|
751
|
-
* // -> 'Value has changed from 1 to 3'
|
|
752
|
-
*
|
|
753
|
-
* **Note:** The event is fired even when the new value is the same as the old value.
|
|
754
|
-
*
|
|
755
|
-
* @event set:{property}
|
|
756
|
-
* @param {String} name The property name.
|
|
757
|
-
* @param {*} value The new property value.
|
|
758
|
-
* @param {*} oldValue The previous property value.
|
|
759
|
-
*/
|
|
760
|
-
|
|
761
|
-
/**
|
|
762
|
-
* Creates and sets the value of an observable property of this object. Such a property becomes a part
|
|
763
|
-
* of the state and is observable.
|
|
764
|
-
*
|
|
765
|
-
* It accepts also a single object literal containing key/value pairs with properties to be set.
|
|
766
|
-
*
|
|
767
|
-
* This method throws the `observable-set-cannot-override` error if the observable instance already
|
|
768
|
-
* has a property with the given property name. This prevents from mistakenly overriding existing
|
|
769
|
-
* properties and methods, but means that `foo.set( 'bar', 1 )` may be slightly slower than `foo.bar = 1`.
|
|
770
|
-
*
|
|
771
|
-
* @method #set
|
|
772
|
-
* @param {String|Object} name The property's name or object with `name=>value` pairs.
|
|
773
|
-
* @param {*} [value] The property's value (if `name` was passed in the first parameter).
|
|
774
|
-
*/
|
|
775
|
-
|
|
776
|
-
/**
|
|
777
|
-
* Binds {@link #set observable properties} to other objects implementing the
|
|
778
|
-
* {@link module:utils/observablemixin~Observable} interface.
|
|
779
|
-
*
|
|
780
|
-
* Read more in the {@glink framework/guides/deep-dive/observables#property-bindings dedicated guide}
|
|
781
|
-
* covering the topic of property bindings with some additional examples.
|
|
782
|
-
*
|
|
783
|
-
* Consider two objects: a `button` and an associated `command` (both `Observable`).
|
|
784
|
-
*
|
|
785
|
-
* A simple property binding could be as follows:
|
|
786
|
-
*
|
|
787
|
-
* button.bind( 'isEnabled' ).to( command, 'isEnabled' );
|
|
788
|
-
*
|
|
789
|
-
* or even shorter:
|
|
790
|
-
*
|
|
791
|
-
* button.bind( 'isEnabled' ).to( command );
|
|
792
|
-
*
|
|
793
|
-
* which works in the following way:
|
|
794
|
-
*
|
|
795
|
-
* * `button.isEnabled` **instantly equals** `command.isEnabled`,
|
|
796
|
-
* * whenever `command.isEnabled` changes, `button.isEnabled` will immediately reflect its value.
|
|
797
|
-
*
|
|
798
|
-
* **Note**: To release the binding, use {@link module:utils/observablemixin~Observable#unbind}.
|
|
799
|
-
*
|
|
800
|
-
* You can also "rename" the property in the binding by specifying the new name in the `to()` chain:
|
|
801
|
-
*
|
|
802
|
-
* button.bind( 'isEnabled' ).to( command, 'isWorking' );
|
|
803
|
-
*
|
|
804
|
-
* It is possible to bind more than one property at a time to shorten the code:
|
|
805
|
-
*
|
|
806
|
-
* button.bind( 'isEnabled', 'value' ).to( command );
|
|
807
|
-
*
|
|
808
|
-
* which corresponds to:
|
|
809
|
-
*
|
|
810
|
-
* button.bind( 'isEnabled' ).to( command );
|
|
811
|
-
* button.bind( 'value' ).to( command );
|
|
812
|
-
*
|
|
813
|
-
* The binding can include more than one observable, combining multiple data sources in a custom callback:
|
|
814
|
-
*
|
|
815
|
-
* button.bind( 'isEnabled' ).to( command, 'isEnabled', ui, 'isVisible',
|
|
816
|
-
* ( isCommandEnabled, isUIVisible ) => isCommandEnabled && isUIVisible );
|
|
817
|
-
*
|
|
818
|
-
* Using a custom callback allows processing the value before passing it to the target property:
|
|
819
|
-
*
|
|
820
|
-
* button.bind( 'isEnabled' ).to( command, 'value', value => value === 'heading1' );
|
|
821
|
-
*
|
|
822
|
-
* It is also possible to bind to the same property in an array of observables.
|
|
823
|
-
* To bind a `button` to multiple commands (also `Observables`) so that each and every one of them
|
|
824
|
-
* must be enabled for the button to become enabled, use the following code:
|
|
825
|
-
*
|
|
826
|
-
* button.bind( 'isEnabled' ).toMany( [ commandA, commandB, commandC ], 'isEnabled',
|
|
827
|
-
* ( isAEnabled, isBEnabled, isCEnabled ) => isAEnabled && isBEnabled && isCEnabled );
|
|
828
|
-
*
|
|
829
|
-
* @method #bind
|
|
830
|
-
* @param {...String} bindProperties Observable properties that will be bound to other observable(s).
|
|
831
|
-
* @returns {Object} The bind chain with the `to()` and `toMany()` methods.
|
|
832
|
-
*/
|
|
833
|
-
|
|
834
|
-
/**
|
|
835
|
-
* Removes the binding created with {@link #bind}.
|
|
836
|
-
*
|
|
837
|
-
* // Removes the binding for the 'a' property.
|
|
838
|
-
* A.unbind( 'a' );
|
|
839
|
-
*
|
|
840
|
-
* // Removes bindings for all properties.
|
|
841
|
-
* A.unbind();
|
|
842
|
-
*
|
|
843
|
-
* @method #unbind
|
|
844
|
-
* @param {...String} [unbindProperties] Observable properties to be unbound. All the bindings will
|
|
845
|
-
* be released if no properties are provided.
|
|
846
|
-
*/
|
|
847
|
-
|
|
848
|
-
/**
|
|
849
|
-
* Turns the given methods of this object into event-based ones. This means that the new method will fire an event
|
|
850
|
-
* (named after the method) and the original action will be plugged as a listener to that event.
|
|
851
|
-
*
|
|
852
|
-
* Read more in the {@glink framework/guides/deep-dive/observables#decorating-object-methods dedicated guide}
|
|
853
|
-
* covering the topic of decorating methods with some additional examples.
|
|
854
|
-
*
|
|
855
|
-
* Decorating the method does not change its behavior (it only adds an event),
|
|
856
|
-
* but it allows to modify it later on by listening to the method's event.
|
|
857
|
-
*
|
|
858
|
-
* For example, to cancel the method execution the event can be {@link module:utils/eventinfo~EventInfo#stop stopped}:
|
|
859
|
-
*
|
|
860
|
-
* class Foo {
|
|
861
|
-
* constructor() {
|
|
862
|
-
* this.decorate( 'method' );
|
|
863
|
-
* }
|
|
864
|
-
*
|
|
865
|
-
* method() {
|
|
866
|
-
* console.log( 'called!' );
|
|
867
|
-
* }
|
|
868
|
-
* }
|
|
869
|
-
*
|
|
870
|
-
* const foo = new Foo();
|
|
871
|
-
* foo.on( 'method', ( evt ) => {
|
|
872
|
-
* evt.stop();
|
|
873
|
-
* }, { priority: 'high' } );
|
|
874
|
-
*
|
|
875
|
-
* foo.method(); // Nothing is logged.
|
|
876
|
-
*
|
|
877
|
-
*
|
|
878
|
-
* **Note**: The high {@link module:utils/priorities~PriorityString priority} listener
|
|
879
|
-
* has been used to execute this particular callback before the one which calls the original method
|
|
880
|
-
* (which uses the "normal" priority).
|
|
881
|
-
*
|
|
882
|
-
* It is also possible to change the returned value:
|
|
883
|
-
*
|
|
884
|
-
* foo.on( 'method', ( evt ) => {
|
|
885
|
-
* evt.return = 'Foo!';
|
|
886
|
-
* } );
|
|
887
|
-
*
|
|
888
|
-
* foo.method(); // -> 'Foo'
|
|
889
|
-
*
|
|
890
|
-
* Finally, it is possible to access and modify the arguments the method is called with:
|
|
891
|
-
*
|
|
892
|
-
* method( a, b ) {
|
|
893
|
-
* console.log( `${ a }, ${ b }` );
|
|
894
|
-
* }
|
|
895
|
-
*
|
|
896
|
-
* // ...
|
|
897
|
-
*
|
|
898
|
-
* foo.on( 'method', ( evt, args ) => {
|
|
899
|
-
* args[ 0 ] = 3;
|
|
900
|
-
*
|
|
901
|
-
* console.log( args[ 1 ] ); // -> 2
|
|
902
|
-
* }, { priority: 'high' } );
|
|
903
|
-
*
|
|
904
|
-
* foo.method( 1, 2 ); // -> '3, 2'
|
|
905
|
-
*
|
|
906
|
-
* @method #decorate
|
|
907
|
-
* @param {String} methodName Name of the method to decorate.
|
|
908
|
-
*/
|