-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschemas.js
47 lines (38 loc) · 910 Bytes
/
schemas.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
class Schemas {
constructor(model) {
if (!(model instanceof Array)) throw new Error('Bad model');
this.id = model[0];
this.schemas = model[1].map((s) => new Schema(s));
// Enable events
Event.enable(this);
}
add(schema) {
this.schemas.push(schema);
this.dispatch('add', schema);
}
remove(schema) {
var idx = this.schemas.findIndex((s) => s.id === schema.id);
if (idx === -1)
throw new Error(`No such schema: ${schema}`);
this.schemas.splice(idx, 1);
this.dispatch('remove', schema);
}
create() {
this.add(Schema.create());
}
get(id) {
var schema = this.schemas.find(s => s.id === id);
if (!schema)
throw new Error(`No such id: ${id}`);
return schema;
}
map(callback, thisArg) {
return this.schemas.map(callback, thisArg);
}
toJSON() {
var json = new Array();
json.push(this.id);
json.push(this.schemas.map((s) => s.toJSON()));
return json;
}
}