-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEditorFactory.js
More file actions
303 lines (296 loc) · 9.33 KB
/
Copy pathEditorFactory.js
File metadata and controls
303 lines (296 loc) · 9.33 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
define([ //
'./primitive/binary/MimeTypeHelper',
'./validate/UniqueItem',
"dojo/_base/declare",//
"dojo/Stateful", //
"./converter/urlToIdConverter", //
"./converter/anyToTextConverter", //
"./group/DecoratorFactory", //
"./validate/UniqueProperties",//
"./validate/AdditionalProperties",//
"./validate/Min",//
"./validate/Max",//
"./validate/MinItems",//
"./validate/MaxItems",//
"./validate/MinLength",//
"./validate/MaxLength",//
"./validate/Pattern"//
], function (MimeTypeHelper, UniqueItem, declare, Stateful, urlToIdConverter, anyToTextConverter, DecoratorFactory, UniqueProperties, AdditionalProperties, 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: null,
convertersByType: null,
functions:null,
constructor: function () {
this.mimeTypeHelper=new MimeTypeHelper();
this.groupFactories = {};
this.convertersById = {};
this.convertersByType = {};
this.functions={};
this.decoratorFactory = new DecoratorFactory();
this.addConverterForType(urlToIdConverter, "ref");
this.addConverterForType(urlToIdConverter, "multi-ref");
this.addConverterForType(anyToTextConverter, "any");
this.factoryValidators = {
uniqueProperties: UniqueProperties,
pattern: Pattern,
minLength: MinLength,
maxLength: MaxLength,
min: Min,
max: Max,
minItems: MinItems,
maxItems: MaxItems,
additionalProperties: AdditionalProperties,
uniqueItem:UniqueItem
}
this.constructorValidators = {};
},
createBadge: function (model) {
return this.decoratorFactory.createBadge(model);
},
createGroupModel: function (schema, model) {
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);
}
var group = factory.createModel(schema);
return group;
},
createAttributeModel: function (attribute) {
var factory = this.getAttributeFactory(attribute);
if (factory === null) {
throw new Error("cannot find attribute factory for type " + attribute.type);
}
var model = factory.createModel(attribute);
model.model=true;
return model;
},
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, decorator) {
// 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, decorator);
},
create: function (group, modelHandle, ctx, options) {
// 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
// ctx: gform/Context
// access to store and schema
// options: Object
// hidden: don't load content of widget. Wait for show to be called on returned widget.
// 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, options);
}
else {
return this.defaultGroupFactory.create(group, modelHandle, ctx, options);
}
},
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:
return this.attributeFactoryFinder.getAttributeFactories();
},
getAttributeFactoryMap: function () {
// summary:
// get the map of editor is to AttributeFactory
// returns: Object
return this.attributeFactoryFinder.getAttributeFactoryMap();
},
addAttributeFactory: function (af) {
// summary:
// the AttributeFactory
// returns: id
return this.attributeFactoryFinder.addAttributeFactory(af);
},
getAttributeFactoryById: function (id) {
// summary:
// the AttributeFactory
// returns: id
return this.attributeFactoryFinder.getAttributeFactoryMap()[id];
},
getModelValidators: function (attribute) {
// summary:
// attach validation to modelHandle.
//
var validators = [];
for (var key in this.factoryValidators) {
if (attribute[key]) {
var validator = this.factoryValidators[key];
if (validator) {
validators.push(validator(attribute[key]));
}
}
}
for (var key in this.constructorValidators) {
if (attribute[key]) {
var validator = this.constructorValidators[key];
if (validator) {
validators.push(new validator(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;
},
factoryValidators: null,
constructorValidators: null,
// 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") {
var conv = new c(attribute, ctx);
return {
format:conv.format.bind(conv),
parse:conv.parse.bind(conv)
}
} 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;
},
addValidator: function (id, validator) {
this.factoryValidators[id] = validator;
},
addCtrValidator: function (id, validator) {
this.constructorValidators[id] = validator;
},
putFunction: function(id,fn) {
this.functions[id]=fn;
},
getFunction: function(id) {
return this.functions[id];
},
getMimeTypeHelper: function() {
return this.mimeTypeHelper;
}
});
});