/
Editor.js
352 lines (332 loc) · 9.86 KB
/
Editor.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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
define(["dojo/_base/array", "dojo/aspect", "dojo/_base/lang", "dojo/_base/declare",
"dojox/mvc/_Container", "dojox/mvc/at",
"dojo/dom-construct", "./schema/labelHelper", "dojo/query", "dijit/registry"],
function (array, aspect, lang, declare, Container, at, domConstruct, labelHelper, query, registry) {
// module:
// gform/Editor
// at needs to be available globally.
window.at = at;
return declare("gform.Editor", [Container], {
// summary:
// this widget generates a form based on a schema.
// description:
// Editor needs a schema and a gform/EditorFactory that helps creating the widget tree appropriate for the schema.
// | require(["gform/createStandardEditorFactory"],
// | function(createStandardEditorFactory){
// | var myGformSchema=
// | {attributes:[
// | {code: "name", type: "string"}
// | ]}
// | var editorFactory= createStandardEditorFactory();
// | var editor= new Editor(
// | {meta: myGformSchema,
// | editorFactory: editorFactory()
// | });
// | editor.placeAt("myContainer");
// | }
//
// editorFactory:
// the editorFactory is responsible for translating the schema into a widget tree.
editorFactory: null,
doLayout: true,
// widget:
// this is the single child group widget.
widget: null,
// modelHandle:
// the data that is bound to the form.
modelHandle: null,
// context:
// the context provides extra features for this editor instance
context: null,
shown: true,
// meta:
// the schema describing the form.
meta: null,
bufferOnChange: false,
// isLayoutContainer:
// Editor supports layouts by propagating the resizing to its child.
isLayoutContainer: true,
// remember all field paths where an explicit error message was added via addError()
_explicitErrorPaths: [],
setMetaAndPlainValue: function (meta, value, setOldValue) {
// summary:
// Change the schema and the value simultaneously.
delete this.modelHandle;
this.meta = meta;
this._init(value, setOldValue);
this._buildContained();
},
setMetaAndDefault: function (meta) {
// summary:
// Change the schema and the value simultaneously.
delete this.modelHandle;
this.meta = meta;
this.initDefault(false);
this._buildContained();
},
setModel: function (model) {
// summary:
// Change the schema and the value simultaneously.
this.modelHandle = model;
this.meta = model.schema;
this._buildContained();
},
setPlainValue: function (/*Object*/ plainValue) {
// summary:
// update the modelHandle bound to the editor
// plainValue:
// the plainValue.
this.set("plainValue", plainValue);
},
getPlainValue: function () {
// summary:
// gets the plainValue from the current modelHandle.
// returns: Object
// the plainValue.
return this.get("plainValue");
},
createBadge: function () {
return this.editorFactory.createBadge(this.modelHandle);
},
_setPlainValueAttr: function (value) {
this._init(value, true);
},
_init: function (value, setOldValue) {
if (this.modelHandle) {
this.modelHandle.resetMetaRecursively();
}
if (value === null) {
value = {};
}
if (!this.modelHandle) {
this.modelHandle = this.editorFactory.createGroupModel(this.meta);
this.modelHandle.update(value, setOldValue);
this.modelHandle.init();
var me = this;
aspect.after(this.modelHandle, "onChange", lang.hitch(this, "onChangeInternally"));
} else {
this.modelHandle.update(value, setOldValue);
}
if (!this.meta) {
throw new Error("cannot set plainValue before setting meta");
}
this._explicitErrorPaths = [];
// send change event because oldValue changed and hasChanged will return something new
this.emit("value-changed");
},
// summary:
// if an input in the form still has focus then its current value may be different from the widget's value. We force a synchronization (and validation) here.
syncPendingChanges: function () {
var focused = query("input:focus", this.domNode);
if (focused && focused.length === 1) {
var dijit = registry.getEnclosingWidget(focused[0]);
dijit._onBlur();
}
},
getOldValue: function () {
return this.modelHandle.get("oldValue");
},
hasChanged: function () {
// summary:
// returns true if the data was changed.
// returns: Boolean
return this.modelHandle.hasChanged();
},
addError: function (path, message) {
// summary:
// add an error message to an attribute defined by the path.
// path: String
// path like "addresses.0.name"
// message: String
// the error message
var model = this.modelHandle.getModelByPath(path);
model.set("state", "Error");
model.set("message", message);
this._explicitErrorPaths.push(path);
},
resetErrors: function () {
// summary:
// reset all field errors that were added via addError().
array.forEach(this._explicitErrorPaths, function (path) {
var model = this.modelHandle.getModelByPath(path);
model.resetErrors();
}, this);
this._explicitErrorPaths = [];
},
updateValue: function (path, value) {
// summary:
// update an attribute of the data.
// path: String
// path like "addresses.0.name"
// value: String
// the new value
this.modelHandle.getModelByPath(path).set("value", value);
},
visit: function (path, cb) {
// summary:
// visit the data attribute defined by the path. The path elements are separated by dots -even the indices (e.g.: "person.friends.1.name").
// path: String
// path like "addresses.0.name"
// cb: function
// callback.
var model = this.modelHandle.getPath(path);
cb(model);
},
reset: function () {
// summary:
// reset the data to its original value.
this.bufferChange(function () {
// TODO bubble prevention does not work properly.
this.modelHandle.reset();
this.onChange();
}.bind(this));
},
_setDoLayoutAttr: function (doLayout) {
this._set("doLayout", doLayout);
if (this.widget) {
this.widget.set("doLayout", doLayout);
}
},
resize: function (dim) {
this.dim = dim;
if (this.widget && this.widget.resize) {
if (dim) {
this.widget.resize({t: 0, l: 0, w: dim.w, h: dim.h});
} else {
this.widget.resize();
}
}
},
removeChangeIndicators: function () {
this.modelHandle.removeIndicators();
},
postCreate: function () {
this.inherited(arguments);
this.containerNode = this.domNode;
this.watch("meta", lang.hitch(this, "_buildContained"));
if (this.meta && this.shown) {
this._buildContained();
}
},
getLabel: function () {
if (this.meta) {
return labelHelper.getTypeLabel(this.meta, this.modelHandle);
} else {
return "";
}
},
show: function () {
if (!this.shown) {
this.shown = true;
this._buildContained();
}
},
startup: function () {
this.inherited(arguments);
this._started = true;
if (this.widget) {
this.widget.startup();
}
this.resize();
},
_getPlainValueAttr: function () {
if (this.modelHandle === null) {
return null;
} else {
return this.modelHandle.getPlainValue();
}
},
_setMetaUrlAttr: function (url) {
var me = this;
require(["dojo/text!" + url], function (metaJson) {
me.set("meta", dojo.fromJson(metaJson));
});
},
_buildContained: function () {
if (!this.modelHandle) {
this.set("plainValue", {});
}
try {
if (this.widget) {
this.widget.destroyRecursive();
}
if (this.get("meta") && this.editorFactory) {
this.widget = this.editorFactory.create(this.get("meta"),
this.modelHandle, this.ctx);
this.widget.set("doLayout", this.doLayout);
if (typeof this.get("doLayout") != "undefined") {
this.widget.set("doLayout", this.get("doLayout"));
var widget = this.widget;
if (widget.resize) {
aspect.after(this.widget, "startup", function () {
// widget.resize();
});
}
}
if (this.widget && this.domNode) {
domConstruct.place(this.widget.domNode, this.domNode);
if (this._started) {
this.widget.startup();
}
}
if (this._started && this.dim) {
this.resize(this.dim);
} else if (this._started) {
this.resize();
}
}
} catch (e) {
console.log("cannot create editor. " + e.message + " " + e.stack);
throw e;
}
},
_destroyBody: function () {
if (this.widget != null) {
this.widget.destroyRecursive();
this.widget = null;
}
},
_getErrorCountAttr: function () {
return this.modelHandle.errorCount;
},
_getChangedCountAttr: function () {
return this.modelHandle.changedCount;
},
_getIncompleteCountAttr: function () {
return this.modelHandle.incompleteCount;
},
validate: function (force) {
this.modelHandle.validateRecursively(force);
return this.modelHandle.get("errorCount");
},
getErrorCount: function () {
return this.get("errorCount");
},
onChange: function () {
},
bufferChange: function (cb) {
var me = this;
var oldBufferOnChange = !!this.bufferOnChange;
this.bufferOnChange = true;
try {
cb();
me.onChange();
} finally {
this.bufferOnChange = oldBufferOnChange;
}
},
onChangeInternally: function () {
if (!this.bufferOnChange) {
this.onChange();
}
},
initDefault: function (setOldValue) {
if (!this.modelHandle) {
this.modelHandle = this.editorFactory.createGroupModel(this.meta, {});
aspect.after(this.modelHandle, "onChange", lang.hitch(this, "onChangeInternally"));
}
this.modelHandle.init();
this.modelHandle.initDefault(setOldValue);
}
});
});