forked from cbergmiller/bootstrap-formform
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbootstrap-formform.js
More file actions
executable file
·708 lines (545 loc) · 19 KB
/
Copy pathbootstrap-formform.js
File metadata and controls
executable file
·708 lines (545 loc) · 19 KB
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/* zmip Bootstrap formform v0.1
A JSON to Bootstrap 3 form generator based on the work of https://github.com/cbergmiller
https://github.com/zmip/bootstrap-formform
*/
var FormForm = (function () {
function FormForm( parentForm, formData )
{
this.parentForm = parentForm;
this.formData = formData;
this.col1 = 4;
this.col2 = 8;
this.isHorizontal = parentForm.hasClass( 'form-horizontal' );
}
FormForm.prototype.unique_id = 1;
// -----------------------------------------------------------------------------
FormForm.prototype.createItem = function( itemData )
{
var type = itemData.type;
if ( type == 'select' ) return new ElementSelect( itemData );
else if ( type == 'textarea' ) return new ElementTextarea( itemData );
else if ( type in { 'button':0, 'submit':0, 'reset':0 } ) return new ElementButton( itemData );
else if ( type == 'static' ) return new ElementStatic( itemData );
else if ( type == 'radiogroup' ) return new ElementRadiogroup( itemData );
else if ( type == 'file' ) return new ElementFile( itemData );
else if ( type == 'checkboxgroup' ) return new ElementCheckboxgroup( itemData );
else if ( type == 'hidden' ) return new ElementHidden( itemData );
else if ( type in {
text:0,
password:0,
datetime:0,
'datetime-local':0,
date:0,
month:0,
time:0,
week:0,
number:0,
email:0,
url:0,
search:0,
tel:0,
color:0,
checkbox:0,
radio:0
} ) return new Element( itemData );
if ( ! type ) return new Element( itemData );
else throw new Error( 'unknown type: ' + itemData.type );
}
// -----------------------------------------------------------------------------
FormForm.prototype.render = function( formValues )
{
var i = 1000;
var self = this; // will ECMA 6 finally fix this crap?
var buttons = [];
var btnsAlignRight = '';
Element.prototype.isHorizontal = this.isHorizontal ? [this.col1, this.col2] : null;
$.each( this.formData, function( index, itemData )
{
if ( formValues && itemData.name in formValues ) itemData.value = formValues[ itemData.name ];
// create and render
var item = self.createItem( itemData ).render();
// filter out any buttons on the form root (with added whitespace for proper spacing)
if ( itemData.type == 'submit' || itemData.type == 'button' )
{
if ( itemData.position == 'right' ) btnsAlignRight = ' text-right';
buttons.push( item, "\n" );
}
else self.parentForm.append( item );
} );
// append buttons
if ( buttons.length )
{
if ( this.isHorizontal )
{
// wrap buttons in 'col-sm-offset' div
buttons = $('<div>').append( buttons );
buttons.addClass( 'col-sm-offset-'
+ Element.prototype.isHorizontal[0]
+ ' col-sm-' + Element.prototype.isHorizontal[1]
+ btnsAlignRight );
buttons = $('<div>').addClass('form-group').append( buttons );
}
// enclose in 'form-group' and add to form
self.parentForm.append( $('<div>').addClass('form-group'+btnsAlignRight).append( buttons ) );
}
// set up 'additions'
this.setupAdditions();
};
// -----------------------------------------------------------------------------
FormForm.prototype.setupAdditions = function ()
{
var additions = $('.addition', this.parentForm );
if ( additions.length )
{
var focus = false;
additions.each( function( index ) {
var addition = $(this);
var for_id = addition.data( 'for' );
var for_elem = $('#'+for_id);
if ( for_elem.length )
{
var name = for_elem.attr( 'name' );
// get all other options with this name
var options = $('[name='+name+']')
if ( options.length )
{
var additionGroup = $(this).closest('div.form-group');
options.each( function( index2 ) {
if ( $(this).attr( 'id' ) == for_id && ! $(this).is( ':checked' ) )
{
focus = false;
additionGroup.hide();
}
$(this).click( function( evt ) {
if ( $(this).attr( 'id' ) == for_id ) additionGroup.slideDown( 'fast', function() {
if ( ! focus && ! addition.val().length )
{
addition.focus();
focus = true;
}
} );
else
{
focus = false;
additionGroup.slideUp( 'fast', function () {
addition.val('');
} );
}
} );
} );
}
}
} );
}
}
// -----------------------------------------------------------------------------
// FormForm Elements
// -----------------------------------------------------------------------------
// default <input> constructor, functions as base class
function Element( itemData )
{
// defaults
this.classes = ['form-control'];
// determine ID
this.id = itemData.id ? itemData.id : ( itemData.name ? itemData.name : ( 'id_'+FormForm.prototype.unique_id++ ) );
this.name = itemData.name ? itemData.name : this.id;
// create default attributes
this.attributes = { id: this.id, name: this.name };
// placeholder attribute
if ( itemData.placeholder ) this.attributes.placeholder = itemData.placeholder;
// data attribute(s)
if ( itemData.data )
{
var self = this; // crap!
$.each( itemData.data, function( key, val ) {
self.attributes[ 'data-' + key ] = val;
} );
}
// classes
if ( itemData.classes ) this.classes.push( itemData.classes );
// hang on to itemData
this.itemData = itemData;
}
// ---------------------------------
Element.prototype.createGroup = function( skip_attributes )
{
var group_elements = [];
// add label (only on radiogroup and checkboxgroup a label is optional)
var label = null;
if ( this.itemData.label || this.itemData.type != 'radiogroup' || this.itemData.type != 'checkboxgroup' )
{
label = $('<label>')
.text( this.itemData.label ? this.itemData.label : 'always add a label' )
.addClass( 'control-label' + ( this.isHorizontal ? ' col-sm-'+this.isHorizontal[0] : '' ) );
}
if ( label )
{
if ( ['checkboxgroup', 'radiogroup', 'file'].indexOf( this.itemData.type ) === -1 ) label.attr( 'for', this.attributes.id );
group_elements.push( label );
}
// apply attributes *and* classes
if ( ! skip_attributes ) this.applyAttributes();
// create help-block
var help_block = $('<span>' )
.html( this.itemData.error ? this.itemData.error : this.itemData[ 'help-block' ] ? this.itemData[ 'help-block' ] : '' )
.addClass( 'help-block' );
// add element to group
if ( this.isHorizontal )
{
var group2 = $('<div>').addClass( 'col-sm-'+this.isHorizontal[1] );
group2.append( [ this.elem, help_block ] );
group_elements.push( group2 );
}
else
{
group_elements.push( this.elem );
group_elements.push( help_block );
}
// enclose in form-group
var grp = $('<div>' )
.append( group_elements )
.addClass( 'form-group' );
if ( this.itemData.error ) grp.addClass( 'has-error' );
return grp;
}
// ---------------------------------
Element.prototype.applyAttributes = function()
{
// add attributes
this.elem.attr( this.attributes );
// add classes
if ( this.classes.length ) this.elem.addClass( this.classes.join( ' ' ) );
}
// ---------------------------------
Element.prototype.createAddons = function()
{
var addonsLeft = [];
var addonsRight = [];
var self = this; //sigh....
// create addons
$.each( this.itemData.addons, function( index, addonData ) {
var addon = null;
var dropdown = null;
// button addon
if ( addonData.type == 'button' )
{
addon = new ElementButton( addonData ).render( true );
if ( addonData.dropdown ) dropdown = self.createDropdown( addonData.dropdown, addonData.position == 'right' );
}
// sort left/right
if ( addon && addonData.position == 'right' )
{
addonsRight.push( addon );
if ( dropdown ) addonsRight.push( dropdown );
}
else
{
addonsLeft.push( addon );
if ( dropdown ) addonsLeft.push( dropdown );
}
} );
// did addons render successfully?
if ( addonsLeft.length || addonsRight.length )
{
// apply attributes and classes to 'this.elem'
this.applyAttributes();
// create enclosing 'input-group'
var grp = $('<div>').addClass( 'input-group' );
// enclose lefthand addons in 'input-group-btn' div and append
if ( addonsLeft.length ) grp.append( $('<div>').addClass( 'input-group-btn' ).append( addonsLeft ) );
// append this.elem
grp.append( this.elem );
// enclose righthand addons in 'input-group-btn' div and append
if ( addonsRight.length ) grp.append( $('<div>').addClass( 'input-group-btn' ).append( addonsRight ) );
// change this.elem to be the enclosing 'input-group'
this.elem = grp;
return this.createGroup( true );
}
// addons failed, return this.elem in enclosed form-group
return this.createGroup();
}
// ---------------------------------
Element.prototype.render = function()
{
if ( ! this.itemData.type ) this.elem = $('<input type="text">');
else this.elem = $('<input type="'+this.itemData.type+'">');
// set value (use 'undefined' check here to allow for falsy values, like 0)
if ( typeof this.itemData.value != 'undefined' ) this.elem.val( this.itemData.value );
// create (optional) addons and enclosing group
if ( this.itemData.addons && this.elem.attr( 'type' ) in {
text:0,
password:0,
datetime:0,
'datetime-local':0,
date:0,
month:0,
time:0,
week:0,
number:0,
email:0,
url:0,
search:0,
tel:0,
color:0
} ) return this.createAddons();
else return this.createGroup();
}
// ---------------------------------
Element.prototype.createDropdown = function( options, right )
{
// create <ul>
var dropdown = $('<ul>').addClass( 'dropdown-menu' + ( right ? ' dropdown-menu-right' : '' ) );
// add <li> options
$.each( options, function( id, label )
{
var a = $( '<a>' ).attr( { 'href':'#', 'id':id } ).text( label );
dropdown.append( $('<li>').append( a ) );
} );
return dropdown;
}
// -----------------------------------------------------------------------------
// Using a dummy to hold the prototype for descendants of Element.
// It is to avoid calling the Element constructor twice.
// The constructor is *not* restored, there is no need for that here.
// In ES5+ we could use Object.create() to more elegantly solve this
var Tmp = function(){}; //
Tmp.prototype = Element.prototype;
// ----------------------------------------------------------------------------- select
function ElementSelect( itemData )
{
Element.call( this, itemData ); // call parent constructor
// overrides
this.elem = $('<select>');
}
ElementSelect.prototype = new Tmp();
// ---------------------------------
ElementSelect.prototype.render = function()
{
var options = [];
$.each( this.itemData.options, function( val, label ) {
options.push( $('<option>').val( val ).text( label ) );
} );
this.elem.append( options );
// set value (use 'undefined' check here to allow for falsy values, like 0)
if ( typeof this.itemData.value != 'undefined' ) this.elem.val( this.itemData.value );
return this.createGroup();
}
// ----------------------------------------------------------------------------- button
function ElementButton( itemData )
{
Element.call( this, itemData ); // call parent constructor
// overrides
this.elem = $('<button>').attr('type', itemData.type);
this.nohelp = true;
this.classes = ['btn'];
}
ElementButton.prototype = new Tmp();
// ---------------------------------
ElementButton.prototype.render = function( asAddon )
{
// add icon
if ( this.itemData.icon ) this.elem.append( $('<span>')
.addClass( 'glyphicon glyphicon-'+this.itemData.icon ) );
// set label
this.elem.append( $('<span>').text(
( this.itemData.icon ? ' ' : '' )
+ this.itemData.label
+ ( this.itemData.dropdown ? ' ' : '' ) ) );
// add dropdown adornments/attributes
if ( this.itemData.dropdown )
{
this.elem.append( $('<span>').addClass( 'caret' ) );
this.classes.push( 'dropdown-toggle' );
this.attributes[ 'data-toggle' ] = 'dropdown';
}
// add custom or default class
if ( this.itemData.classes ) this.classes.push( this.itemData.classes );
else this.classes.push( 'btn-default' );
// apply attributes and classes
this.applyAttributes();
// create dropdown
if ( ( ! asAddon ) && this.itemData.dropdown )
{
// create dropdown options
var dropdown = this.createDropdown( this.itemData.dropdown, this.itemData.position == 'right' );
// create enclosing btn-group and add btn and dropdown
return $('<div>').addClass( 'btn-group' ).append( [ this.elem, dropdown ] );
}
return this.elem;
}
// ----------------------------------------------------------------------------- textarea
function ElementTextarea( itemData )
{
Element.call( this, itemData ); // call parent constructor
// overrides
this.elem = $('<textarea>');
}
ElementTextarea.prototype = new Tmp();
// ---------------------------------
ElementTextarea.prototype.render = function()
{
// set value (use 'undefined' check here to allow for falsy values, like 0)
if ( typeof this.itemData.value != 'undefined' ) this.elem.text( this.itemData.value );
return this.createGroup();
}
// ----------------------------------------------------------------------------- static
function ElementStatic( itemData )
{
Element.call( this, itemData ); // call parent constructor
// overrides
this.elem = $('<p>');
this.classes = ['form-control-static'];
}
ElementStatic.prototype = new Tmp();
// ---------------------------------
ElementStatic.prototype.render = function()
{
// set value (use 'undefined' check here to allow for falsy values, like 0)
if ( typeof this.itemData.value != 'undefined' ) this.elem.text( this.itemData.value );
var grp = this.createGroup();
// remove some attributes
this.elem.removeAttr('name');
$('label', grp ).removeAttr( 'for' );
return grp;
}
// ----------------------------------------------------------------------------- file
function ElementFile( itemData )
{
Element.call( this, itemData ); // call parent constructor
// overrides
this.classes = [ 'btn', 'btn-file' ];
}
ElementFile.prototype = new Tmp();
// ---------------------------------
ElementFile.prototype.render = function()
{
//TODO: improve this element:
// - it's not working in IE8
// - needs additional JS to show the file name
// create the button from ElementButton with added 'btn-file' class
var tmp_data = { 'label': ( this.itemData.btnlabel ? this.itemData.btnlabel : 'set "btnlabel" prop' ) };
// if ( ! this.itemData.classes ) tmp_data.classes = 'btn-default';
// else tmp_data.classes = this.itemData.classes;
// tmp_data.classes += ' btn-file';
// tmp_data.icon = this.itemData.icon;
// var btn = new ElementButton( tmp_data ).render();
//
// // apply some inline styles to hide the original file input
// // not very elegant, but removes the requirement of additonal CSS
// btn.css( {
// position: 'relative',
// overflow: 'hidden'
// } );
//
// // append the file input
// btn.append( $('<input type="file">').css( {
// position: 'absolute',
// top: 0,
// right: 0,
// 'min-width': '100%',
// 'min-height': '100%',
// 'font-size': '100px',
// 'text-align': 'right',
// filter: 'alpha(opacity=0)',
// opacity: 0,
// outline: 'none',
// background: 'none',
// cursor: 'inherit',
// display: 'block'
// } ) );
//
// //<span class="glyphicon glyphicon-remove form-control-feedback" aria-hidden="true"></span>
//
// this.elem = $('<div>')
// .addClass( 'input-group' )
// .append( [ $('<label>').addClass( 'input-group-btn' ).append( btn ),
// $('<input type="text">').addClass( 'form-control' ).attr( 'readonly', '' ) ] );
var btn = new ElementButton( tmp_data ).render();
this.elem = $('<div>')
.addClass( 'input-group' )
.append( [ $('<label>').addClass( 'input-group-btn' ).append( btn ),
$('<input type="text">').addClass( 'form-control' ).attr( 'readonly', '' ) ] );
return this.createGroup( true );
}
// ----------------------------------------------------------------------------- checkbox
function ElementCheckboxgroup( itemData )
{
Element.call( this, itemData ); // call parent constructor
// overrides
this.elem = $('<div>');
this.classes = ['checkbox-group']; // this is not a Bootstrap class
}
ElementCheckboxgroup.prototype = new Tmp();
// ---------------------------------
ElementCheckboxgroup.prototype.render = function()
{
var options = [];
var itemData = this.itemData;
$.each( itemData.options, function( ign, checkboxData ) {
var input_elem = $( '<input type="checkbox">' )
.attr( 'name', checkboxData.name )
.attr( 'id', checkboxData.name )
.val( checkboxData.value );
if ( checkboxData.checked ) input_elem.prop( 'checked', true );
options.push( $('<div>')
.addClass( 'checkbox' )
.append( $('<label>')
.text( checkboxData.label )
.prepend( input_elem ) ) );
} );
// add classes
this.elem.addClass( this.classes.join( ' ' ) );
return this.createGroup( this.elem.append( options ), true );
}
// ----------------------------------------------------------------------------- hidden
function ElementHidden( itemData )
{
Element.call( this, itemData ); // call parent constructor
// overrides
this.elem = $('<input type="hidden">');
}
ElementHidden.prototype = new Tmp();
// ---------------------------------
ElementHidden.prototype.render = function()
{
// add attributes
this.applyAttributes();
// set value (use 'undefined' check here to allow for falsy values, like 0)
if ( typeof this.itemData.value != 'undefined' ) this.elem.val( this.itemData.value );
return this.elem;
}
// ----------------------------------------------------------------------------- radiogroup
function ElementRadiogroup( itemData )
{
Element.call( this, itemData ); // call parent constructor
// overrides
this.elem = $('<div>');
this.classes = ['radio-group']; // this is not a Bootstrap class
}
ElementRadiogroup.prototype = new Tmp();
// ---------------------------------
ElementRadiogroup.prototype.render = function()
{
var options = [];
var itemData = this.itemData;
// add group label (optional)
//if ( itemData.label ) this.elem.append( $('<label>').text( itemData.label ) );
$.each( itemData.options, function( val, label ) {
var input_elem = $( '<input type="radio">' )
.attr( 'name', itemData.name )
.attr( 'id', itemData.name + '_' + val )
.val( val );
if ( itemData.value == val ) input_elem.prop( 'checked', true );
options.push( $('<div>')
.addClass( 'radio' )
.append( $('<label>')
.text( label )
.prepend( input_elem ) ) );
} );
// add classes
this.elem.addClass( this.classes.join( ' ' ) );
return this.createGroup( this.elem.append( options ), true );
}
// -----------------------------------------------------------------------------
return FormForm;
})();