forked from stemey/mongomat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGformGenerator.js
166 lines (153 loc) · 4.46 KB
/
GformGenerator.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
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
var GformGenerator = function (config) {
this.config = config || {};
if (config && config.editorMapping) {
this.editorMapping = config.editorMapping;
} else {
this.editorMapping = {};
}
}
GformGenerator.prototype.generateMulti = function (schema) {
var groups = this.group(schema);
var schemas = {};
Object.keys(groups).forEach(function (discriminator) {
schemas[discriminator] = this.generate(groups[discriminator]);
}, this);
return schemas;
}
GformGenerator.prototype.group = function (schema) {
var groups = {};
Object.keys(schema).forEach(function (path) {
var d = path.match(/subtypes\[([^\]]+)\]/);
if (d && d.length == 2) {
var discriminator = d[1];
var subPath = path.substring(("subtypes[" + discriminator + "]").length + 1)
var samples = groups[discriminator];
if (!samples) {
samples = {};
groups[discriminator] = samples;
}
samples[subPath] = schema[path];
} else {
console.log("no discriminator found " + path);
}
}, {})
return groups;
}
GformGenerator.prototype.generate = function (schema) {
var gschema = {editor: "listpane", attributes: []};
Object.keys(schema).forEach(function (prop) {
var path = prop.split(".");
if (path.length == 1) {
var attribute = this.generateAttribute(prop, schema);
gschema.attributes.push(attribute);
}
}, this);
return gschema;
}
GformGenerator.prototype.getLeaf = function (path) {
return path.substring(path.lastIndexOf("."));
}
GformGenerator.prototype.generateAttribute = function (prop, schema) {
var propSchema = schema[prop];
var method = "generate_" + propSchema.type;
var attribute;
if (this[method]) {
attribute = this[method](prop, schema);
} else {
var attribute = {code: prop, type: propSchema.type};
if (propSchema.required) {
attribute.required = propSchema.required;
}
attribute.editor = this.getEditor(attribute);
}
if (attribute.code === this.config.typeProperty) {
attribute.visible = false;
}
return attribute;
}
GformGenerator.prototype.getEditor = function (attribute) {
var editor = this.editorMapping[attribute.type];
if (editor) {
return editor;
} else {
return attribute.type;
}
}
GformGenerator.prototype.generate_string = function (prop, mainSchema) {
var propSchema = mainSchema[prop];
var attribute = {
code: this.getLeaf(prop),
type: "string",
required: propSchema.required
}
// make sure we dont have a values array of ObjectIds
if (propSchema.values && attribute.code!=="_id") {
attribute.values = propSchema.values.map(function (value) {
return {value: value, label: value};
});
attribute.editor = "select";
} else {
attribute.editor = "string";
}
return attribute;
}
GformGenerator.prototype.generate_object = function (prop, mainSchema) {
var attribute = {
code: this.getLeaf(prop),
type: "object"
}
var schema = mainSchema[prop]
if (schema.required) {
attribute.required = schema.required;
}
var subProps = Object.keys(mainSchema).filter(function (key) {
return key.indexOf(prop) == 0;
})
var subSchema = {};
subProps.forEach(function (subProp) {
var subCode = subProp.substring(prop.length + 1);
if (subCode.length > 0 && subCode.indexOf(".") > 0) {
subSchema[subCode] = mainSchema[subProp];
}
})
// TODO lacks prop parameter to mnage deeply nested schemas
var groups = this.generateMulti(subSchema);
var keys = Object.keys(groups);
if (keys.length == 1) {
attribute.group = groups[keys[0]];
attribute.editor = "object";
} else {
attribute.groups = keys.map(function (discriminator) {
var group = groups[discriminator];
group.code = discriminator;
group.editor = "listpane";
return group;
})
attribute.typeProperty = this.config.typeProperty;
attribute.editor = "multi-object";
}
return attribute;
}
GformGenerator.prototype.generate_array = function (prop, mainSchema) {
var schema = mainSchema[prop]
var attribute = {code: this.getLeaf(prop), type: "array"}
var itemsSchema = mainSchema[prop + ".items"];
if (itemsSchema.type === "object") {
var group = this.generate_object(prop + ".items", mainSchema);
delete group.code;
if (group.group) {
attribute.group = group.group;
attribute.editor = "array";
} else {
attribute.typeProperty = this.config.typeProperty;
attribute.groups = group.groups;
attribute.editor = "multi-array";
}
} else {
attribute.element = this.generateAttribute(prop + ".items", mainSchema);
attribute.editor = "primitive-array";
delete attribute.element.code;
}
return attribute;
}
module.exports = GformGenerator;