forked from stemey/dojo-generate-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditorFactory.js
235 lines (231 loc) · 7.34 KB
/
EditorFactory.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
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
define([ //
"dojo/_base/declare",//
"dojo/Stateful", //
"./converter/urlToIdConverter", //
"./group/DecoratorFactory", //
"./validate/UniqueProperties",//
"./validate/Min",//
"./validate/Max",//
"./validate/MinItems",//
"./validate/MaxItems",//
"./validate/MinLength",//
"./validate/MaxLength",//
"./validate/Pattern"//
], function (declare, Stateful, urlToIdConverter, DecoratorFactory, UniqueProperties, Min, Max, MinItems, MaxItems, MinLength, MaxLength, Pattern) {
// module:
// gform/EditorFactory
return declare("gform.EditorFactory", [Stateful], {
// summary:
// EditorFactory defines the mapping of a gform schema to a widget tree.
// asyncModelValidation: boolean
// dijits validate after setting the value. Therefore model validation must happen asynchronuously
// to value change events.
convertersById: {},
convertersByType: {},
constructor: function () {
this.groupFactories = {};
this.decoratorFactory = new DecoratorFactory();
this.addConverterForType(urlToIdConverter, "ref");
},
createGroupModel: function (schema, plainValue) {
var factory;
if (schema.editor) {
factory = this.getGroupFactory(schema.editor);
} else {
factory = this.defaultGroupFactory;
}
if (factory == null) {
throw new Error("cannot find group factory for type " + schema.editor);
}
return factory.createModel(schema, plainValue);
},
createAttributeModel: function (attribute, plainValue) {
var factory = this.getAttributeFactory(attribute);
if (factory == null) {
throw new Error("cannot find attribute factory for type " + attribute.type);
}
return factory.createModel(attribute, plainValue);
},
addGroupFactory: function (id, factory) {
// summary:
// add a groupFactory by id
// id: String
// matched against the groupType from the schema
// factory: gform/api/GroupFactory
// the groupFactory
this.groupFactories[id] = factory;
},
setDefaultGroupFactory: function (id) {
// summary:
// set default groupFactory by id
// id: String
// the id of the default groupFactory
this.defaultGroupFactory = this.groupFactories[id];
},
// decoratorFactory:
// used to create decorators
decoratorFactory: null,
// defaultGroupFactory:
// used to create group if groupType property is absent from schema.
defaultGroupFactory: null,
// groupFactories:
// map of all groupFactories.
groupFactories: null,
// attributeFactoryFinder:
// manages the attributeFactories.
attributeFactoryFinder: null,
createDecorator: function (/*Object*/attribute, /*dojo.Stateful*/modelHandle) {
// summary:
// creates a Decorator Widget for the given attribute. The Decorator displays the label, description and state of the attribute. The actual widget for editing the attribute's value is added as a child.
// attribute: Object
// The attribute schema.
// modelHandle: dojo/Stateful
// The modelHandle. The Decorator watches the state and value of the attribute via its modelHandle meta data.
// returns: dijit/Widget
// The Widget ich contains all widgets for the attributes and groups contained in the given attribute schema.
return this.decoratorFactory.create(/*Object*/attribute, /*dojo/stateful*/modelHandle);
},
create: function (group, modelHandle, ctx) {
// summary:
// creates a group widget.
// group: Object
// The group schema. If no groupType property is present then the defaultGroupFactory creates the widget.
// modelHandle: dojo/Stateful
// the modelHandle
// return: dijit/Widget
// Widget
if (!group) {
return null;
}
if (group.editor) {
var groupFactory = this.find(group.editor);
if (groupFactory == null) {
throw new Error("cannot find group factory " + group.editor);
}
return groupFactory.create(group, modelHandle, ctx);
}
else {
return this.defaultGroupFactory.create(group, modelHandle, ctx);
}
},
getGroupFactory: function (group) {
// summary:
// get the groupFactory by meta data
// group: Object
// the group meta data
// returns: gform/api/GroupFactory
return this.find(group);
},
getGroupFactoryMap: function () {
// summary:
// get the map of groupType to groupFactory
// returns: Object
return this.groupFactories;
},
find: function (groupType) {
// summary:
// get groupFactory by type
// groupType: String
// the groupType as in the meta data
// returns: gform/api/GroupFactory
return this.groupFactories[groupType];
},
getAttributeFactory: function (attribute) {
// summary:
// get attributeFactory by meta data
// attribute:
// attribute meta data
// returns: gform/api/AttributeFactory
return this.attributeFactoryFinder.getFactory(attribute);
},
getAttributeFactories: function () {
// summary:
// get all attributeFactories
// returns: Array
return this.attributeFactoryFinder.getAttributeFactories();
},
getAttributeFactoryMap: function () {
// summary:
// get the map of editor is to AttributeFactory
// returns: Object
return this.attributeFactoryFinder.getAttributeFactoryMap();
},
getModelValidators: function (attribute) {
// summary:
// attach validation to modelHandle.
//
var validators = [];
for (var key in this.arrayValidators) {
if (attribute[key]) {
var validate = this.arrayValidators[key];
if (validate) {
validators.push(validate(attribute[key]));
}
}
}
return validators;
},
createValidateFunction: function (validator) {
var validateFn = null
if (this.asyncModelValidation) {
validateFn = function () {
setTimeout(function () {
validator.validate();
}, 0);
};
} else {
validateFn = function () {
validator.validate();
};
}
return validateFn;
},
arrayValidators: {
uniqueProperties: UniqueProperties,
pattern: Pattern,
minLength: MinLength,
maxLength: MaxLength,
min: Min,
max: Max,
minItems: MinItems,
maxItems: MaxItems
},
// summary:
// get a converter for the given attribute.
// attribute: object
// the attribute meta data. the type property is usually used to lookup a converter
// ctx: gform/Context
// the context provides extra information (e.g. the storeRegistry that can translate from store ids to actual urls).
getConverter: function (attribute, ctx) {
var c;
if (attribute.converter) {
c = this.convertersById[attribute.converter];
} else {
c = this.convertersByType[attribute.type];
}
if (typeof c === "function") {
return new c(attribute, ctx);
} else {
return c;
}
},
// summary:
// add a converter for a specific type
// converter:
// The converter may be an instance or a constructor. constructor will be called with the parameters attribute and ctx.
// type: string
// type (e.g. "string", "ref")
addConverterForType: function (converter, type) {
this.convertersByType[type] = converter;
},
// summary:
// add a converter for a specific id (ids an be used in the schema).
// converter:
// The converter may be an instance or a constructor. constructor will be called with the parameters attribute and ctx.
// id: string
// the id is matched against the converter property in the attribute meta data.
addConverterForid: function (converter, id) {
this.convertersById[id] = converter;
}
});
});