-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormGenerator.js
628 lines (559 loc) · 18.3 KB
/
FormGenerator.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
dojo.provide('unc.FormGenerator');
dojo.require('dijit._Templated');
dojo.require('dijit._Widget');
dojo.require('dijit._Container');
dojo.require('dijit.layout._LayoutWidget');
dojo.require('unc.SayOrPlaySelector');
dojo.require('unc.AudioSelector');
dojo.require('unc.ImageSelector');
dojo.require('dijit.form.Form');
dojo.require('dijit.form.Textarea');
dojo.require('dijit.form.Button');
dojo.require('dijit.form.NumberSpinner');
dojo.require('dijit.form.NumberTextBox');
dojo.require('dijit.form.CheckBox');
dojo.require('dijit.form.Select');
dojo.require('dijit.layout.BorderContainer');
dojo.require('dijit.layout.ContentPane');
dojo.require('dijit.Editor');
dojo.require('dijit.Tooltip');
dojo.require('dojox.grid.DataGrid');
dojo.require('dojo.data.ItemFileWriteStore');
function EditRemove(text, start, stop) {
while (true) {
var i1 = text.indexOf(start);
if (i1 < 0) {
break;
}
var i2 = text.indexOf(stop, i1);
if (i2 > i1) {
text = text.slice(0, i1) + text.slice(i2+stop.length);
}
}
return text;
}
/* pasting from MS Word produces HORRIBLY huge html. This filter tries to fix that. */
function EditorFilter(text) {
// get rid of comment
text = EditRemove(text, '<!--', '-->');
// get rid of meta content
text = EditRemove(text, '<meta content', '/>');
// get rid of style
text = EditRemove(text, '<style>', '</style>');
// get rid of link
text = EditRemove(text, '<link', '</link>');
// bash class tags
var pat = new RegExp(' class="[^"]*"', 'g');
text = text.replace(pat, '');
// bash the o:p tags
text = text.replace(/<o:p>/g, '').replace(/<\/o:p>/g, '');
return text;
}
dojo.declare('unc.FormGenerator', [ dijit.form.Form ], {
schema: {}, // object describing the JSON schema for the object this form is to edit,
initValue: {}, // object giving the initial value for the object
/**
* Called to initialize the FormGenerator. The content is generated and linked into the DOM.
*/
postCreate: function() {
this.inherited(arguments);
var w = this.generate('formContent', this.schema, this.initValue);
dojo.addClass(this.containerNode, 'uncFormGenerator');
dojo.place(w.domNode, this.containerNode);
w.startup();
},
/**
* Sorts an flat object based on a "position" attribute
*/
sort: function(obj){
var tmp = [], x;
for( x in obj.properties)
tmp.push([x,obj.properties[x].position]);
tmp.sort(function(a,b) {return a[1]-b[1];});
var neworder = {}, l = tmp.length, i;
for( i=0; i<l; i++) neworder[tmp[i][0]] = obj.properties[tmp[i][0]];
obj.properties = neworder;
},
/**
* Generate controls for the given schema initialized with value, called recursively
*/
generate: function(name, schema, value) {
//console.log('generate', name, schema, value);
// Sort pased on 'position' attr
if(schema.type == 'object')
this.sort(schema);
// determine the method to call by introspection
var type = schema.type;
var method = 'generate_' + type;
if (method in this) {
var r = this[method](name, schema, value);
return r;
} else {
console.log('FormGenerator: no method', method);
}
},
/**
* Generate fieldset for an object schema
*/
generate_object: function(name, schema, value) {
var title = schema.title || name;
var manager = new unc.ObjectManager({
name: name,
theTitle: title,
description: schema.description || ''
});
for(var propertyName in schema.properties) {
var propertySchema = schema.properties[propertyName];
var propertyValue = null;
if (value && value[propertyName] !== undefined) {
propertyValue = value[propertyName];
}
manager.addChild(this.generate(propertyName, propertySchema, propertyValue));
}
return manager;
},
/**
* Generate a string input control
*/
generate_string: function(name, schema, value) {
//console.log('generate_string', name, schema, value);
var title = schema.title || name;
var format = schema.format || 'text';
var init = value || schema['default'] || '';
var description = schema.description || '';
var control;
var filter = null;
if (format == 'html') {
control = new dijit.Editor({
name: name,
value: init
});
filter = EditorFilter;
} else if(format == 'audioMedia') {
control = new unc.AudioSelector({
name: name,
value: init
});
} else if(format == 'audio') {
control = new unc.SayOrPlaySelector({
name: name,
value: init
});
} else if(format == 'imageMedia') {
control = new unc.ImageSelector({
name: name,
value: init
});
} else {
if ('enum' in schema) {
var options = dojo.map(schema['enum'], function(e, i) {
return {
value: e,
label: e,
selected: e == init,
disabled: false
};
});
control = new dijit.form.Select({
name: name,
options: options
});
} else {
//console.log(1, name, init);
control = new dijit.form.Textarea({
name: name,
value: init,
readOnly: 'readonly' in schema && schema['readonly'],
baseClass: 'dijitTextBox dijitTextArea' // why do I need this?
});
//console.log(2);
}
}
//console.log('creating FieldManager');
var manager = new unc.FieldManager({
theTitle: title,
control: control,
description: description,
filter: filter
});
//console.log('generate_string returns', manager);
return manager;
},
/**
* Generate an integer input control
*/
generate_integer: function(name, schema, value) {
var title = schema.title || name;
var format = schema.format || 'text';
var init = value || schema['default'] || 0;
var constraints = {places: 0};
var description = schema.description || '';
if ("minimum" in schema) {
constraints.min = schema.minimum;
}
if ("maximum" in schema) {
constraints.max = schema.maximum;
}
var control = new dijit.form.NumberSpinner({
name: name,
value: init,
constraints: constraints
});
manager = new unc.FieldManager({
theTitle: title,
control: control,
description: description
});
return manager;
},
/**
* Generate a number input control
*/
generate_number: function(name, schema, value) {
var title = schema.title || name;
var format = schema.format || 'text';
var init = value || schema['default'] || 0;
var constraints = { };
var description = schema.description || '';
if ("minimum" in schema) {
constraints.min = schema.minimum;
}
if ("maximum" in schema) {
constraints.max = schema.maximum;
}
var control = new dijit.form.NumberSpinner({
name: name,
value: init,
constraints: constraints
});
manager = new unc.FieldManager({
theTitle: title,
control: control,
description: description
});
return manager;
},
/**
* Generate a boolean input control
*/
generate_boolean: function(name, schema, value) {
var title = schema.title || name;
var init = value || schema['default'] || 0;
var description = schema.description || '';
var control = new dijit.form.CheckBox({
name: name,
checked: init
});
manager = new unc.FieldManager({
theTitle: title,
control: control,
inline:true,
description: description
});
return manager;
},
/**
* Generate an array manager
*/
generate_array: function(name, schema, value) {
var title = schema.title || name;
var default_value = schema['default'] || [];
var init = value || default_value;
var description = schema.description || "";
var format = schema.format || 'list';
var minItems = schema.minItems || 0;
var managerClass = format == 'grid' ? unc.ArrayGridManager : unc.ArrayManager;
//console.log('generate_array', name, schema, default_value, init);
var manager = new managerClass({
name: name,
theTitle: title,
description: description,
generator: dojo.hitch(this, function(init) {
return this.generate(title, schema.items, init);
}),
init: init,
minItems: minItems
});
return manager;
},
/**
* Return the value of the object represented by the form
*/
_getValueAttr: function() {
var children = this.getChildren();
var result = dojo.map(children, function(child) { return child.attr('value'); });
// I expect only one child here at the top level
if (result.length == 1) {
result = result[0];
}
return result;
},
/**
* Signal any of the contained controls changing value
*/
onChange: function() {
//console.log('onChange called');
}
});
dojo.declare('unc.ArrayGridManager', [ dijit._Widget , dijit._Templated ], {
templatePath: dojo.moduleUrl("unc", "ArrayGridManager.html"),
widgetsInTemplate: true,
name: "",
theTitle: "",
description: "",
generator: null,
minItems: 0,
init: [],
postCreate: function() {
this.inherited(arguments);
this.store = new dojo.data.ItemFileWriteStore({
identifier: 'id',
data: {
items: []
}
});
this.grid = new dojox.grid.DataGrid({
store: this.store,
structure: [ { name: 'Name', field: 'Name', width: '100%' } ],
sortInfo: 1
}, this.gridNode);
// insert the initial content
for (var i=0; i < this.init.length; i++) {
var it = this.init[i];
var n = this.store.newItem({ Name: it.Name, Value: it });
}
// connect up the buttons
this.connect(this.addButton, 'onClick', 'addItem');
this.connect(this.deleteButton, 'onClick', 'deleteItem');
this.connect(this.grid, 'onRowClick', dojo.hitch(this, function(e) {
var selected = this.grid.selection.getSelected()[0];
this.showEditor(selected);
}));
if (this.init.length > 0) {
var first = this.grid.getItem(0);
this.showEditor(first);
this.showItem();
} else {
this.addItem();
}
},
addItem: function() {
//console.log('addItem');
var item = this.store.newItem({
Name: '...',
Value: {}
});
//console.log('addItem', item);
this.showEditor(item);
this.showItem();
},
deleteItem: function(){
this.store.deleteItem(this.toEdit);
var first = this.grid.getItem(0);
if(first){
this.showEditor(first);
this.showItem();
}else{
this.addItem();
}
},
showItem: function() {
var index = this.grid.getItemIndex(this.toEdit);
//console.log('showEditor', index);
this.grid.selection.select(index);
setTimeout(dojo.hitch(this, function() {
this.grid.scrollToRow(index);
}, 100));
},
showEditor: function(toEdit) {
if (this.toEdit === toEdit) {
return;
}
this.toEdit = toEdit;
myGrid = this.grid;
var value = this.store.getValue(toEdit, 'Value');
this.itemEditor = this.generator(value);
dojo.place(this.itemEditor.domNode, this.editorNode, 'only');
this.connect(this.itemEditor, 'onChange', 'onChange');
},
_getValueAttr: function() {
//console.log('getValue', this);
if (!this.hasOwnProperty('store')) return [];
var res = [];
this.store.fetch({
onItem: dojo.hitch(this, function(item) {
res.push(this.store.getValue(item, 'Value'))
})
});
return res;
},
startup: function() {
this.grid.startup();
},
onChange: function() {
//console.log('array g change', this.name);
var newValue = this.itemEditor.get('value');
//console.log('newValue', newValue);
//console.log('toEdit', this.toEdit);
var oldName = this.toEdit.Name;
this.store.setValue(this.toEdit, 'Value', newValue);
this.store.setValue(this.toEdit, 'Name', newValue.Name);
if (oldName != newValue.Name) {
// resort
this.grid.setSortInfo(1);
this.showItem();
}
}
});
dojo.declare('unc.ArrayManager', [ dijit._Widget, dijit._Templated, dijit._Container ], {
templatePath: dojo.moduleUrl("unc", "ArrayManager.html"),
widgetsInTemplate: true,
name: "",
theTitle: "",
description: "",
generator: null,
minItems: 0,
init: [],
postCreate: function(args) {
this.inherited(arguments);
this.addButton.set('label', 'Add ' + this.theTitle);
this.connect(this.addButton, 'onClick', function() {
var item = this.generator(null);
this.addItem(item);
});
// now generate the initial nodes
for (var i=0; i < Math.max(this.minItems, this.init.length); i++) {
item = this.generator(this.init[i]);
this.addItem(item);
}
},
/**
* Add an item to the array and decorate it with the array controls
*/
addItem: function(item) {
var children = this.getChildren();
var N = children.length;
var button = dojo.create('img', {src: "images/cross.png", width: "16", height: "16",
title: "Click to delete this item." }, item.arrayControl);
this.connect(button, 'onclick', function() {
item.destroyRecursive();
this.renumber();
this.onChange();
});
dojo.place(item.domNode, this.containerNode, 'last');
item.arrayIndex.innerHTML = N;
this.connect(item, 'onChange', 'onChange');
},
/**
* Renumber the array items after a delete or other reorganization
*/
renumber: function(item) {
dojo.forEach(this.getChildren(), function(item, index) {
item.arrayIndex.innerHTML = index;
});
},
/**
* Return the value of the array
*/
_getValueAttr: function() {
var children = this.getChildren();
var result = dojo.map(children, function(child) { return child.attr('value'); }, this);
return result;
},
startup: function() {
dojo.forEach(this.getChildren(), function(child) {
child.startup(); });
},
onChange: function() {
//console.log('array change', this.name);
}
});
dojo.declare('unc.ObjectManager', [ dijit._Widget, dijit._Templated, dijit._Container ], {
templatePath: dojo.moduleUrl("unc", "ObjectManager.html"),
widgetsInTemplate: true,
name: "",
theTitle: "",
description: "",
postCreate: function() {
this.inherited(arguments);
},
addChild: function(child) {
this.inherited(arguments);
this.connect(child, 'onChange', 'onChange');
},
/**
* Return the object's value
*/
_getValueAttr: function() {
var children = this.getChildren();
var result = {};
dojo.forEach(children, function(child) {
result[child.name] = child.attr('value');
}, this);
return result;
},
/**
* Set the disabled attribute for all the children
*/
_setDisabledAttr: function(value) {
dojo.forEach(this.getChildren(), function(child) {
child.attr('disabled', value);
});
},
startup: function() {
dojo.forEach(this.getChildren(), function(child) {
child.startup();
});
},
onChange: function() {
//console.log('object Change', this.name);
}
});
dojo.declare('unc.FieldManager', [ dijit._Widget, dijit._Templated, dijit._Container ], {
templatePath: dojo.moduleUrl("unc", "FieldManager.html"),
widgetsInTemplate: true,
control: null,
inline: false,
name: "",
theTitle: "",
description: "",
filter: null, // you can supply a function to filter the value
/**
* Initialize the widget and connect up the tooltip
*/
postCreate: function() {
this.inherited(arguments);
dojo.place(this.control.domNode, this.containerNode);
this.name = this.control.name;
if (this.description) {
var tt = new dijit.Tooltip({
connectId: [ this.control.domNode ],
label: this.description});
}
this.connect(this.control, 'onChange', 'onChange');
if(!this.inline)
dojo.create("br",{},this.label,"after");
},
/**
* Return the value of the enclosed control
*/
_getValueAttr: function() {
var value = this.control.attr('value');
if (this.filter) {
value = this.filter(value);
}
return value;
},
/**
* Set the disabled attribute of the enclosed control
*/
_setDisabledAttr: function(value) {
this.control.attr('disabled', value);
},
startup: function() {
this.control.startup();
},
onChange: function() {
//console.log('Field change', this.name);
}
});