forked from stemey/dojo-generate-form
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEditor.js
174 lines (165 loc) · 4.79 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
define([ "dojo/_base/array", "dojo/_base/lang", "dojo/_base/declare",
"dojox/mvc/_Container", "dijit/layout/_LayoutWidget","dojox/mvc/at",
"dojo/dom-construct", "dojo/Stateful", "./getPlainValue","./updateModelHandle","./hasChanged","./group/_GroupMixin" ], function(array, lang,
declare, Container, _LayoutWidget,at, domConstruct,
Stateful,getPlainValue,updateModelHandle,hasChanged,_GroupMixin) {
// module:
// gform/Editor
// at needs to be globally defined.
window.at = at;
return declare("gform.Editor", [ Container,_GroupMixin ], {
// summary:
// this widget generates a form based on a schema.
editorFactory : null,
// summary:
// the editorFactory is responsible for translating the schema into a widget tree.
widget : null,
children : null,
modelHandle : null,
// summary:
// the data that is bound to the form.
meta : null,
// summary:
// the schema describing the form.
// _relTargetProp: String
// The name of the property that is used by child
// widgets for relative
// data binding.
_relTargetProp : "children",
isLayoutContainer:true,
// //////////////////// PRIVATE METHODS
// ////////////////////////
setMetaAndPlainValue: function(meta,value) {
this.meta=meta;
this.modelHandle=updateModelHandle.createMeta();
updateModelHandle.update(this.meta,value,this.modelHandle,this.editorFactory);
this.modelHandle.oldValue=getPlainValue(this.modelHandle);
this._buildContained();
},
_setPlainValueAttr: function(value) {
if (value==null) {
value={};
}
if (!this.modelHandle) {
this.modelHandle=updateModelHandle.createMeta();
}
if (!this.meta) {
throw new Error("cannot set plainValue before setting meta");
}
updateModelHandle.update(this.meta,value,this.modelHandle,this.editorFactory);
this.modelHandle.oldValue=getPlainValue(this.modelHandle);
},
_getPlainValueAttr: function() {
return getPlainValue(this.modelHandle);
},
_setMetaUrlAttr: function(url) {
var me = this;
require(["dojo/text!"+url],function(metaJson) {
me.set("meta",dojo.fromJson(metaJson));
});
},
hasChanged: function() {
// summary:
// returns true if the data was changed.
return hasChanged(this.modelHandle);
},
resize: function(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(null);
}
}
},
postCreate : function() {
this.inherited(arguments);
this.containerNode=this.domNode;
//this.domNode.style.height="100%";
//this.domNode.style.width="100%";
this.watch("meta", lang.hitch(this, "_buildContained"));
if (this.meta) {
this._buildContained();
}
},
startup : function() {
this.inherited(arguments);
},
_buildContained: function() {
if (this.modelHandle == null) {
this.set("plainValue",{});
}
try {
if (this.widget) {
this.widget.destroy();
}
if (this.get("meta") && this.editorFactory) {
this.widget = this.editorFactory.create(this.get("meta"),
this.modelHandle);
if (this.widget && this.domNode) {
domConstruct.place(this.widget.domNode, this.domNode);
this.widget.startup();
}
}
} catch (e) {
console.log("cannot create editor. " + e.message+" "+ e.stack);
throw e;
}
},
addError: function(path,message) {
// summary:
// add an error message to an attribute defined by the path.
this.visit(path,function(model) {
model.set("valid",false);
model.set("message",message);
});
},
updateValue: function(path,value) {
// summary:
// update an attribute of the data.
this.visit(path,function(model) {
model.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").
var pathElements=path.split(".");
var model=this.get("modelHandle");
array.forEach(pathElements,function(pathElement){
if (!model) {
throw new Error("cannot resolve path "+path);
}
model=model.value;
if (!model) {
throw new Error("cannot resolve path "+path);
}
model=model[pathElement];
},this);
if (!model) {
throw new Error("cannot resolve path "+path);
}
cb(model);
},
reset: function() {
// summary:
// reset the data to its original value.
var oldValue=this.modelHandle.oldValue;
this.set("plainValue",oldValue);
},
setMetaAndValue: function(meta,plainValue) {
// summary:
// change the form and its data.
this.meta=meta;
this.modelHandle=null;
this.set("plainValue",plainValue);
this._buildContained();
},
_destroyBody : function() {
if (this.widget != null) {
this.widget.destroy();
this.widget = null;
}
}
});
});