-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
CommonMethods.ts
62 lines (56 loc) · 1.73 KB
/
CommonMethods.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
import { Observable } from './Observable';
export class CommonMethods<EventSpec> extends Observable<EventSpec> {
/**
* Sets object's properties from options, for initialization only
* @protected
* @param {Object} [options] Options object
*/
protected _setOptions(options: any = {}) {
for (const prop in options) {
this.set(prop, options[prop]);
}
}
/**
* @private
*/
_setObject(obj: Record<string, any>) {
for (const prop in obj) {
this._set(prop, obj[prop]);
}
}
/**
* Sets property to a given value. When changing position/dimension -related properties (left, top, scale, angle, etc.) `set` does not update position of object's borders/controls. If you need to update those, call `setCoords()`.
* @param {String|Object} key Property name or object (if object, iterate over the object properties)
* @param {Object|Function} value Property value (if function, the value is passed into it and its return value is used as a new one)
*/
set(key: string | Record<string, any>, value?: any) {
if (typeof key === 'object') {
this._setObject(key);
} else {
this._set(key, value);
}
return this;
}
_set(key: string, value: any) {
this[key as keyof this] = value;
}
/**
* Toggles specified property from `true` to `false` or from `false` to `true`
* @param {String} property Property to toggle
*/
toggle(property: string) {
const value = this.get(property);
if (typeof value === 'boolean') {
this.set(property, !value);
}
return this;
}
/**
* Basic getter
* @param {String} property Property name
* @return {*} value of a property
*/
get(property: string): any {
return this[property as keyof this];
}
}