-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathforms.js
394 lines (384 loc) · 13.7 KB
/
forms.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/** @jsx React.DOM */
//form.js
var CreateFieldsMixin = {
generateComponent: function(property,name,updateValues,id,childId,child,bulletId,bulletGroup){
var fieldType = "text";
var value = property.value;
var bullets = null;
if(this.state.values) bullets = this.state.values.bullets;
if (property.type == "number" || property.type == "integer") fieldType = "number";
else if (property.type == "boolean"){
fieldType = "checkbox";
if(!value) value = false;
}
if (property['ux-widget']) fieldType = property['ux-widget'];
if (property.format){
fieldType = property.format;
if(property.format == "uri") fieldType = "file";
}
if(property.enum){
if(property.enum.length == 2) fieldType = "radio";
else fieldType = "select";
}
if (!property.title) property.title = name;
if(bulletId >= 0) value = "";
return <generateField
type = {fieldType}
items = {property.enum}
values = {bullets}
label = {property.title}
name = {name}
placeholder = {property['ux-placeholder']}
value = {value}
required = {property.required}
description = {property.description}
updateValues = {updateValues}
child = {child}
id = {id}
groupId = {property.groupId}
childId = {childId}
bulletId = {bulletId}
bulletGroup = {bulletGroup}
minimum = {property.minimum}
maximum = {property.maximum}
step = {property.step}
fileUpload = {this.fileUpload} />;
},
fileUpload: function(files){
if(this.props.filesOnSelect) this.props.filesOnSelect(files);
this.state.files.push(files);
}
};
var formjs = React.createClass({
mixins: [CreateFieldsMixin],
updateValues: function(element){
var currentValues = {};
var parentValues = this.state.parentValues;
var childValues = this.state.childValues;
if(!element.value) element.value = '';
if(element.bulletGroup){
if(!childValues[element.bulletGroup]) childValues[element.bulletGroup] = {};
childValues[element.bulletGroup][element.name] = element.value;
}
else if(element.child){
if(!childValues[element.groupId]) childValues[element.groupId] = {};
childValues[element.groupId][element.name] = element.value;
}
else{
parentValues[element.name] = element.value;
}
this.setState({parentValues: parentValues});
this.setState({childValues: childValues});
parentValues['bullets'] = childValues;
if(element.initial === true) return false;
if(this.props.currentState) this.props.currentState(JSON.stringify(parentValues));
return false;
},
getValues: function() {
var parentValues = this.state.parentValues;
parentValues['bullets'] = this.state.childValues;
return JSON.stringify(parentValues);
},
getFiles: function() {
return JSON.stringify(this.state.files);
},
handleSubmit: function() {
var currentValues = this.state.parentValues;
if(this.props.submitState) this.props.submitState(JSON.stringify(currentValues));
if(this.state.files != '' && this.props.filesOnSubmit) this.props.filesOnSubmit(this.state.files);
return false;
},
getInitialState: function(){
return{ data: this.props.data, values: this.props.values, properties: this.props.data.schema.properties, iteration: this.props.iteration, parentValues: {}, childValues: {}, files: []};
},
generateChildComponent: function(fixedProps,iteration,count,id,updateValues) {
var stop, check, valueKey, childId;
stop = check = valueKey = childId = 0;
var amount = count;
var generateComponent = this.generateComponent;
var values = this.state.values;
var childElements = _.map(fixedProps, function (property,name) {
property.value = null;
if(check == count){
if(childId != 3) amount += count;
stop = amount + count;
check = 0;
valueKey++;
}
if(values){
if(values['bullets'][valueKey]){
if(childId < amount) property.value = values['bullets'][valueKey][property.title];
if(childId >= amount && childId < stop) property.value = values['bullets'][valueKey][property.title];
}
}
property.groupId = valueKey;
childId++;
check++;
return generateComponent(property,name,updateValues,id,childId,true);
});
return childElements;
},
render: function() {
var id = 0;
var updateValues = this.updateValues;
var iteration = this.state.iteration;
var generateChildComponent = this.generateChildComponent;
var generateComponent = this.generateComponent
var values = this.state.values;
var elements = _.map(this.state.properties, function (property,name) {
id++;
if(property.type == "array"){
if(property.items.properties){
var fixedProps = {};
var i, count, bulletCount;
i = count = bulletCount = 0;
for(var prop in property.items.properties) {
if(property.items.properties.hasOwnProperty(prop))
++count;
}
if(values){
for(var prop in values.bullets) {
if(values.bullets.hasOwnProperty(prop))
++bulletCount;
}
}
if(count >= 2){
arrayNum = 0;
for (var key in property.items.properties) {
if(i == count) break;
fixedProps[key] = property.items.properties[key];
fixedProps[i] = property.items.properties[key];
if(count > 2){
var amount = count - 2;
_(amount).times(function (n){
var num = n + 1;
fixedProps[i+50*num] = property.items.properties[key];
});
}
i++;
}
arrayNum++;
}
var childElements = generateChildComponent(fixedProps,iteration,count,id,updateValues);
return <div className="child">
<p>{property.title}</p>
{childElements}
<addField fields={property.items.properties} updateValues={updateValues} fieldCount={count} />
</div>;
}
}
else{
property.value = null;
if(values) property.value = values[name];
return generateComponent(property,name,updateValues,id);
}
});
var formDescription;
if(this.state.data.description){
formDescription = <p dangerouslySetInnerHTML={{__html: this.state.data.description}} />;
}
return(
<div>
{formDescription}
<form encType="multipart/form-data" onSubmit={this.handleSubmit}>
{elements}
<input type="submit" value={this.state.data['ux-submit-text']} />
</form>
</div>
);
}
});
var addField = React.createClass({
mixins: [CreateFieldsMixin],
getInitialState: function() {
return {fields: this.props.fields, generatedFields: [], bulletGroup: this.props.fieldCount};
},
generateFields: function(fields,bulletGroup){
var bulletId = this.state.generatedFields.length * this.props.fieldCount;
var updateValues = this.props.updateValues;
var id = this.props.id;
var generateComponent = this.generateComponent;
var generatedFields = _.map(fields, function (property,name) {
return generateComponent(property,name,updateValues,id,null,null,bulletId,bulletGroup);
});
return generatedFields;
},
handleClick: function(event) {
this.state.generatedFields.push(this.generateFields(this.state.fields,this.state.bulletGroup));
this.forceUpdate();
this.setState({bulletGroup: this.state.bulletGroup + 1});
},
render: function() {
return (
<div className="extraBullets">
{this.state.generatedFields}
<div className="button" onClick={this.handleClick}>Add</div>
</div>
);
}
});
var generateField = React.createClass({
getInitialState: function() {
var name = this.props.name;
if(this.props.label) name = this.props.label;
return {value: this.props.value, name: name};
},
componentDidMount: function(){
this.props.updateValues({initial: true, id: this.props.id, groupId: this.props.groupId, bulletGroup: this.props.bulletGroup, name: this.state.name, value: this.props.value,child: this.props.child,childId: this.props.childId, bulletId: this.props.bulletId});
},
handleChange: function(e) {
var value = e.target.value;
if(e.target.files) this.props.fileUpload(e.target.files);
else{
if(this.state.value === true) value = false;
if(this.state.value === false) value = true;
}
this.props.updateValues({id: this.props.id, groupId: this.props.groupId, bulletGroup: this.props.bulletGroup, name: this.state.name, value: value,child: this.props.child, childId: this.props.childId, bulletId: this.props.bulletId});
this.setState({value: value});
},
render: function(){
if(this.props.type == "select"){
return(
<selectField label = {this.props.label}
description = {this.props.description}
name = {this.props.name}
items = {this.props.items}
value = {this.props.value}
change = {this.handleChange} />
);
}
else if(this.props.type == "radio"){
return(
<radioField type = {this.props.type}
label = {this.props.label}
description = {this.props.description}
value = {this.state.value}
required = {this.props.required}
name = {this.props.name}
change = {this.handleChange}
items = {this.props.items} />
);
}
else if(this.props.type == "textarea"){
return(
<textareaField
label = {this.props.label}
required = {this.props.required}
change = {this.handleChange}
value = {this.state.value}
description = {this.props.description} />
);
}
else if (this.props.type == "checkbox"){
return(
<checkboxField description = {this.props.description}
label = {this.props.label}
value = {this.state.value}
required = {this.props.required}
change = {this.handleChange} />
);
}
else{
return(
<genericField type = {this.props.type}
description = {this.props.description}
label = {this.props.label}
placeholder = {this.props.placeholder}
value = {this.state.value}
required = {this.props.required}
min = {this.props.minimum}
max = {this.props.maximum}
step = {this.props.step}
change = {this.handleChange}/>
);
}
}
});
var genericField = React.createClass({
render: function(){
return(
<div className="element inputfield">
<p dangerouslySetInnerHTML={{__html: this.props.description}} />
<label>{this.props.label}</label>
<input type = {this.props.type}
placeholder = {this.props.placeholder}
value = {this.props.value}
required = {this.props.required}
min = {this.props.min}
max = {this.props.max}
step = {this.props.step}
onChange = {this.props.change}/>
</div>
);
}
});
var checkboxField = React.createClass({
render: function(){
var checked = false;
if(this.props.value === true) checked = 'checked';
return(
<div className="element checkbox">
<p dangerouslySetInnerHTML={{__html: this.props.description}} />
<label>{this.props.label}</label>
<input type = 'checkbox'
required = {this.props.required}
value = {this.props.value}
checked = {checked}
onChange = {this.props.change}/>
</div>
);
}
});
var radioField = React.createClass({
render: function(){
if(this.props.value == this.props.items[0]) var selectedFirst = "checked";
else var selectedSecond = "checked";
return(
<div className="element radio">
<p dangerouslySetInnerHTML={{__html: this.props.description}} />
<label>{this.props.label}</label><br />
<label>{this.props.items[0]}</label>
<input type = 'radio'
value = {this.props.items[0]}
required = {this.props.required}
name = {this.props.name}
checked = {selectedFirst}
onChange = {this.props.change}/>
<label>{this.props.items[1]}</label>
<input type = 'radio'
value = {this.props.items[1]}
required = {this.props.required}
name = {this.props.name}
checked = {selectedSecond}
onChange = {this.props.change}/>
</div>
);
}
});
var textareaField = React.createClass({
render: function(){
return(
<div className="element textarea">
<p dangerouslySetInnerHTML={{__html: this.props.description}} />
<label>{this.props.label}</label>
<textarea required={this.props.required} onChange={this.props.change}>{this.props.value}</textarea>
</div>
);
}
});
var selectField = React.createClass({
render: function(){
var options = this.props.items.map(function (option) {
return <option value={option}>{option}</option>
});
return(
<div className="element select">
<p dangerouslySetInnerHTML={{__html: this.props.description}} />
<label>{this.props.label}</label>
<select value={this.props.value} onChange={this.props.change}>
{options}
</select>
</div>
);
}
});