Skip to content

Instantly share code, notes, and snippets.

@lyralemos
Forked from anonymous/jquery.formalize.js
Last active December 17, 2015 04:39
Show Gist options
  • Save lyralemos/5551914 to your computer and use it in GitHub Desktop.
Save lyralemos/5551914 to your computer and use it in GitHub Desktop.
A jQuery plugin that tries to facilitate the creation of forms based on the twitter's bootstrap layout!
/*!
* jQuery Formularize Plugin
*
* Copyright 2013, Alexandre Marinho
* Licensed under the MIT license.
* http://www.opensource.org/licenses/mit-license.php
*/
;(function($){
function define_attributes(elm,attrs){
for (attr in attrs){
elm.attr(attr,attrs[attr])
}
}
function define_binds(elm,binds){
for(bind in binds){
elm.on(bind,binds[bind])
}
}
function newInput(name,field,initial){
var type = field.type || 'text'
var label = field.label || field
//console.log(initial)
switch(type){
case "textarea":
input = $('<textarea />')
.attr('id','id_'+name)
.attr('name',name)
.val(initial)
define_attributes(input,field.attrs)
define_binds(input,field.binds)
return input
case "select":
input = $('<select />').attr('id','id_'+name).attr('name',name)
data = field.source
for(opt in field.source){
value = field.source[opt] || opt
option = $('<option />')
.val(opt)
.html(value)
if(initial==opt){
option.attr('selected','selected')
}
option.appendTo(input)
}
define_attributes(input,field.attrs)
define_binds(input,field.binds)
return input
case "checkbox":
aux = $('<input />')
.attr('id','id_'+name)
.attr('name',name)
.attr('type',type)
if(initial){
aux.attr('checked','checked')
}
input = $('<label />')
.addClass('checkbox')
.append(aux)
.append(label)
define_attributes(input,field.attrs)
define_binds(input,field.binds)
return input
case "radio":
enclosure = $('<div />')
$.each(field.source,function(key,radio){
label_tag = $('<label class="radio" />')
input = $('<input type="radio" />')
.val(key)
.attr('id','id_'+name+'-'+radio)
.attr('name',name)
define_attributes(input,field.attrs)
define_binds(input,field.binds)
if(initial==key){
input.attr('checked','checked')
}
if(field.inline)
label_tag.addClass('inline')
label_tag.append(input)
.append(radio)
.appendTo(enclosure)
})
return enclosure
default:
input = $('<input />')
.attr('id','id_'+name)
.attr('name',name)
.attr('type',type)
.val(initial)
define_attributes(input,field.attrs)
define_binds(input,field.binds)
return input
}
}
function newField(key,field){
type = field.type || 'text'
label = field.label || key
ommit_label = field.ommit_label || false
control_group = $('<div />').addClass('control-group')
controls = $('<div />').addClass('controls')
if(ommit_label!=true){
label_tag = $('<label />')
.addClass('control-label')
.attr('for','id_'+key)
.html(label)
.appendTo(control_group)
}
if(type=='group'){
$.each(field.fields,function(chave,value){
newInput(chave,value,initial[chave]).appendTo(controls)
})
}else{
newInput(key,field,initial[key]).appendTo(controls)
}
controls.appendTo(control_group)
return control_group
}
var methods = {
init: function(options) {
form = $(this)
formularize = $('<div class="formularize" />')
return this.each(function(){
fields = options['fields']
initial = options['initial'] || {}
$.each(fields,function(key,field){
newField(key,field).appendTo(formularize)
})
formularize.prependTo(form)
})
},
append: function(options) {
formularize = $(this).find('.formularize')
return this.each(function(){
fields = options['fields']
initial = options['initial'] || {}
$.each(fields,function(key,field){
newField(key,field).appendTo(formularize)
})
})
},
prepend: function(options){
formularize = $(this).find('.formularize')
return this.each(function(){
fields = options['fields']
initial = options['initial'] || {}
$.each(fields,function(key,field){
newField(key,field).prependTo(formularize)
})
})
}
};
$.fn.formularize = function(method,options) {
var settings = $.extend({
},options)
// Method calling logic
if ( methods[method] ) {
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.formularize' );
}
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment