This repository has been archived by the owner on Jun 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathComponent.js
94 lines (77 loc) · 2.22 KB
/
Component.js
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
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
export class Component {
constructor(props) {
if (props !== false) {
const schema = this.constructor.schema;
for (const key in schema) {
if (props && props.hasOwnProperty(key)) {
this[key] = props[key];
} else {
const schemaProp = schema[key];
if (schemaProp.hasOwnProperty("default")) {
this[key] = schemaProp.type.clone(schemaProp.default);
} else {
const type = schemaProp.type;
this[key] = type.clone(type.default);
}
}
}
if (process.env.NODE_ENV !== "production" && props !== undefined) {
this.checkUndefinedAttributes(props);
}
}
this._pool = null;
}
copy(source) {
const schema = this.constructor.schema;
for (const key in schema) {
const prop = schema[key];
if (source.hasOwnProperty(key)) {
this[key] = prop.type.copy(source[key], this[key]);
}
}
// @DEBUG
if (process.env.NODE_ENV !== "production") {
this.checkUndefinedAttributes(source);
}
return this;
}
clone() {
return new this.constructor().copy(this);
}
reset() {
const schema = this.constructor.schema;
for (const key in schema) {
const schemaProp = schema[key];
if (schemaProp.hasOwnProperty("default")) {
this[key] = schemaProp.type.copy(schemaProp.default, this[key]);
} else {
const type = schemaProp.type;
this[key] = type.copy(type.default, this[key]);
}
}
}
dispose() {
if (this._pool) {
this._pool.release(this);
}
}
getName() {
return this.constructor.getName();
}
checkUndefinedAttributes(src) {
const schema = this.constructor.schema;
// Check that the attributes defined in source are also defined in the schema
Object.keys(src).forEach((srcKey) => {
if (!schema.hasOwnProperty(srcKey)) {
console.warn(
`Trying to set attribute '${srcKey}' not defined in the '${this.constructor.name}' schema. Please fix the schema, the attribute value won't be set`
);
}
});
}
}
Component.schema = {};
Component.isComponent = true;
Component.getName = function () {
return this.displayName || this.name;
};