/
Form.js
608 lines (537 loc) · 17.5 KB
/
Form.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
/* ************************************************************************
qcl - the qooxdoo component library
http://qooxdoo.org/contrib/project/qcl/
Copyright:
2007-2009 Christian Boulanger
License:
LGPL: http://www.gnu.org/licenses/lgpl.html
EPL: http://www.eclipse.org/org/documents/epl-v10.php
See the LICENSE file in the project's top-level directory for details.
Authors:
* Christian Boulanger (cboulanger)
************************************************************************ */
/* ************************************************************************
#require(dialog.FormRenderer)
#require(qx.util.Serializer)
#require(qx.util.Validate)
************************************************************************ */
/**
* A dialog with a form that is constructed on-the-fly
*/
qx.Class.define("dialog.Form",
{
extend : dialog.Dialog,
/*
*****************************************************************************
PROPERTIES
*****************************************************************************
*/
properties :
{
/**
* Data to create a form with multiple fields.
* So far implemented:
* TextField / TextArea
* ComboBox
* SelectBox
* RadioGroup
*
* <pre>
* {
* "username" : {
* 'type' : "TextField",
* 'label' : "User Name",
* 'value' : ""
* },
* "address" : {
* 'type' : "TextArea",
* 'label' : "Address",
* 'lines' : 3
* },
* "domain" : {
* 'type' : "SelectBox",
* 'label' : "Domain",
* 'value' : 1,
* 'options' : [
* { 'label' : "Company", 'value' : 0 },
* { 'label' : "Home", 'value' : 1 }
* ]
* },
* "commands" : {
* 'type' : "ComboBox",
* 'label' : "Shell command to execute",
* 'options' : [
* { 'label' : "ln -s *" },
* { 'label' : "rm -Rf /" }
* ]
* }
* }
* </pre>
*
*
*
*/
formData :
{
check : "Map",
nullable : true,
apply : "_applyFormData"
},
/**
* The model of the result data
*/
model :
{
check : "qx.core.Object",
nullable : true
}
},
/*
*****************************************************************************
EVENTS
*****************************************************************************
*/
events :
{
},
/*
*****************************************************************************
MEMBERS
*****************************************************************************
*/
members :
{
/*
---------------------------------------------------------------------------
PRIVATE MEMBERS
---------------------------------------------------------------------------
*/
_formContainer : null,
_form : null,
_formValidator : null,
_formController : null,
/*
---------------------------------------------------------------------------
WIDGET LAYOUT
---------------------------------------------------------------------------
*/
/**
* Create the main content of the widget
*/
_createWidgetContent : function()
{
/*
* groupbox
*/
var groupboxContainer = new qx.ui.groupbox.GroupBox().set({
contentPadding: [16, 16, 16, 16]
});
groupboxContainer.setLayout( new qx.ui.layout.VBox(10) );
this.add( groupboxContainer );
var hbox = new qx.ui.container.Composite;
hbox.setLayout( new qx.ui.layout.HBox(10) );
groupboxContainer.add( hbox );
/*
* Add message label
*/
this._message = new qx.ui.basic.Label();
this._message.setRich(true);
this._message.setMinWidth(200);
this._message.setAllowStretchX(true);
hbox.add( this._message, {flex:1} );
/*
* Form container
*/
this._formContainer = new qx.ui.container.Composite;
this._formContainer.setLayout( new qx.ui.layout.Grow() );
groupboxContainer.add( this._formContainer, {flex: 1} );
/*
* buttons pane
*/
var buttonPane = new qx.ui.container.Composite;
var bpLayout = new qx.ui.layout.HBox(5)
bpLayout.setAlignX("center");
buttonPane.setLayout( bpLayout );
groupboxContainer.add(buttonPane);
/*
* Ok Button
*/
var okButton = this._createOkButton();
buttonPane.add( okButton );
/*
* Cancel Button
*/
var cancelButton = this._createCancelButton();
buttonPane.add( cancelButton );
},
/*
---------------------------------------------------------------------------
APPLY METHODS
---------------------------------------------------------------------------
*/
/**
* Constructs the form on-the-fly
* @param formData
* @param old
* @return
*/
_applyFormData : function ( formData, old )
{
/*
* remove container content, form, controller
*/
if ( this._formController )
{
this.getModel().removeAllBindings();
this._formController.dispose();
}
if ( this._form )
{
this._form.getValidationManager().removeAllBindings();
this._form.dispose();
}
this._formContainer.removeAll();
/*
* if form is to be deleted
*/
if ( ! formData )
{
return;
}
/*
* if a model doesn't exist, create it from the
* form data
*/
if ( ! this.getModel() )
{
var modelData = {};
for ( var key in formData )
{
modelData[key] = formData[key].value !== undefined
? formData[key].value
: null;
}
var model = qx.data.marshal.Json.createModel( modelData );
this.setModel( model );
}
/*
* create new form and form controller
*/
this._form = new qx.ui.form.Form();
this._formController = new qx.data.controller.Object( this.getModel() );
/*
* hook for subclasses to do something with the new form
*/
this._onFormReady( this._form );
/*
* loop through form data array
*/
for ( var key in formData )
{
var fieldData = formData[key];
/*
* Form element
*/
var formElement = null;
switch ( fieldData.type.toLowerCase() )
{
case "groupheader" :
this._form.addGroupHeader( fieldData.value );
break;
case "textarea":
formElement = new qx.ui.form.TextArea();
formElement.setHeight(fieldData.lines * 16);
formElement.setLiveUpdate(true);
break;
case "textfield":
formElement = new qx.ui.form.TextField();
formElement.setLiveUpdate(true);
break;
case "passwordfield":
formElement = new qx.ui.form.PasswordField();
break;
case "combobox":
//@todo use data model for list
formElement = new qx.ui.form.ComboBox();
fieldData.options.forEach(function( item ){
var listItem = new qx.ui.form.ListItem( item.label, item.icon );
formElement.add( listItem );
});
break;
case "selectbox":
formElement = new qx.ui.form.SelectBox();
var model = qx.data.marshal.Json.createModel( fieldData.options );
new qx.data.controller.List( model, formElement, "label");
break;
case "radiogroup":
formElement = new qx.ui.form.RadioGroup();
if ( fieldData.orientation )
{
formElement.setUserData("orientation", fieldData.orientation );
}
var selected = null;
fieldData.options.forEach( function( item )
{
var radioButton = new qx.ui.form.RadioButton( item.label );
radioButton.setUserData( "value",
item.value !== undefined ? item.value : item.label
);
formElement.add( radioButton );
},this);
break;
case "label":
formElement = new qx.ui.form.TextField(); // dummy
formElement.setUserData("excluded",true);
break;
default:
this.error("Invalid form field type:" + fieldData.type);
}
/*
* Add form element to controller so that result data
* model is updated when form element value changes
*/
formElement.setUserData("key",key);
var _this = this;
switch ( fieldData.type.toLowerCase() )
{
/*
* simple form elements
*/
case "textarea":
case "textfield":
case "passwordfield":
case "combobox":
this._formController.addTarget(
formElement, "value", key, true,
null,
{
"converter" : function( value )
{
_this._form.getValidationManager().validate();
return value;
}
}
);
break;
/*
* single selection form elements
*/
case "selectbox":
this._formController.addTarget(
formElement, "selection", key, true, {
"converter" : qx.lang.Function.bind( function( value )
{
var selected=null;
var selectables = this.getSelectables();
selectables.forEach( function( selectable )
{
var key = this.getUserData("key");
//console.warn( key +": '" + value + "' looking at '" + selectable.getLabel() + "' => " + selectable.getModel().getValue() );
if ( selectable.getModel().getValue() === value )
{
//console.warn("Getting value for '" + key +"': " + value + " -> Setting selection to '" + selectable.getLabel() + "'..");
selected=selectable;
}
}, this );
if( ! selected )
{
//console.warn("Getting value for " + key +": " + value + " -> No selection found" );
return [selectables[0]];
}
return [selected];
}, formElement)
},{
"converter" : qx.lang.Function.bind( function( selection )
{
var value = selection[0].getModel().getValue();
var key = this.getUserData("key");
//console.warn("Selection is " + ( selection.length ? selection[0].getLabel() : "none" ) + " -> Setting value for " + key +": " + value );
return value;
}, formElement)
}
);
break;
case "radiogroup":
this._formController.addTarget(
formElement, "selection", key, true, {
"converter" : qx.lang.Function.bind( function( value )
{
var selectables = this.getSelectables();
var selection = [];
if ( value )
{
selectables.forEach( function( selectable )
{
var sValue = selectable.getUserData("value");
if ( sValue === value )
{
selection = [selectable];
}
}, this );
}
//console.warn("Getting value for " + key +": " + value + " -> Setting selection to " + ( selection.length ? selection[0].getLabel() : "none" ) );
return selection;
},formElement)
},
{
"converter" : function( selection )
{
var value = selection[0].getUserData("value");
//console.warn("Selection is " + ( selection.length ? selection[0].getLabel() : "none" ) + " -> Setting value for " + key +": " + value );
return value;
}
}
);
break;
}
/*
* form element validation
*/
var validator = null;
if ( formElement && fieldData.validation )
{
/*
* is field required?
*/
if ( fieldData.validation.required )
{
formElement.setRequired(true);
}
/*
* is there a validator?
*/
if ( fieldData.validation.validator )
{
var validator = fieldData.validation.validator;
/*
* if validator is a string ...
*/
if ( typeof validator == "string" )
{
/*
* if a validation factory exists, use this
*/
if ( qx.util.Validate[validator] )
{
validator = qx.util.Validate[validator]();
}
/*
* else, is it a regular expression?
*/
else if ( validator.charAt(0) == "/" )
{
validator = qx.util.Validate.regExp( new RegExp( validator.substr( 1, validator.length-2 ) ) );
}
/*
* error
*/
else
{
this.error("Invalid string validator.");
}
}
/*
* in all other cases, it must be a funciton or an async validation
* object
*/
else if ( ! ( validator instanceof qx.ui.form.validation.AsyncValidator )
&& typeof validator != "function" )
{
this.error("Invalid validator.");
}
}
/*
* Server validation?
*/
if ( fieldData.validation.service )
{
var service = fieldData.validation.service;
var _this = this;
validator = new qx.ui.form.validation.AsyncValidator(
function( validatorObj, value)
{
if ( ! validatorObj.__asyncInProgress )
{
validatorObj.__asyncInProgress = true;
qx.core.Init.getApplication().getRpcManager().execute(
service.name, service.method, [value], function(response)
{
try {
var valid = ( response && typeof response == "object" && response.data ) ? response.data : response;
validatorObj.setValid( valid );
validatorObj.__asyncInProgress = false;
} catch(e ) { alert(e) };
}
);
}
}
);
}
}
/*
* if field width is specified
*/
if ( fieldData.width !== undefined )
{
formElement.setWidth( fieldData.width );
}
/*
* placeholder
*/
if ( fieldData.placeholder !== undefined )
{
formElement.setPlaceholder( fieldData.placeholder );
}
/*
* add label and form element to form
*/
var label = fieldData.label;
this._form.add( formElement, label, validator );
}
/*
* render the form
*/
var view = new dialog.FormRenderer( this._form );
view.getLayout().setColumnFlex(0, 0);
view.getLayout().setColumnMaxWidth(0, 100); // FIXME Hardcoded is BAD!
view.getLayout().setColumnFlex(1, 1);
view.setAllowGrowX(true);
this._formContainer.add( view );
/*
* validate the form
*/
this._form.getValidationManager().validate();
},
/**
* Hook for subclasses to do something with the form, for example
* in order to attach bindings to the validation manager.
* Default behavior: bind the enabled state of the "OK" button to the
* validity of the current form.
*
* @param form {qx.ui.form.Form}
* @return {void}
*/
_onFormReady : function( form )
{
form.getValidationManager().bind( "valid", this._okButton, "enabled" );
},
/*
---------------------------------------------------------------------------
EVENT HANDLERS
---------------------------------------------------------------------------
*/
/**
* Handle click on ok button. Calls callback with the result map
* @override
*/
_handleOk : function()
{
this.hide();
if( this.getCallback() )
{
this.getCallback()( qx.util.Serializer.toNativeObject( this.getModel() ) );
}
this.resetCallback();
}
}
});