Created
September 26, 2016 07:26
-
-
Save stevenquiroa/7bc784bf3253f2accf7cf8138eff7d55 to your computer and use it in GitHub Desktop.
Form Format, pequeña libreria para formatear forms.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use-strict"; | |
var FormVM = function ( json_fields, callback ){ | |
this.fields = {}; | |
this.json_fields = json_fields; | |
this.response = {}; | |
this.form = null; | |
this.rules = { | |
"required" : function( field_id, field ) { | |
return field.value.trim() !== ""; | |
} | |
}; | |
this.format = { | |
"text" : function ( field_id, field ) { | |
this.response[field_id] = field.value.trim(); | |
}, | |
"url" : function ( field_id, field ) { | |
this.response[fibmeld_id] = field.value.trim(); | |
}, | |
"integer" : function ( field_id, field) { | |
this.response[field_id] = ( field.value ) ? parseInt(field.value) : 0; | |
}, | |
"numeric" : function ( field_id, field) { | |
this.response[field_id] = ( field.value ) ? parseFloat(field.value) : 0.0 ; | |
} | |
}; | |
if ( !( this instanceof FormVM ) ) { | |
throw new Exception("Te falto el new"); | |
} | |
//si existe json_fields lo agrega a this.fields | |
// console.log(json_fields); | |
if ( ! json_fields ) { | |
throw new Exception("fields: no definido"); | |
} | |
if ( typeof callback === "function" ) { | |
this.sync_init( this.go.call( this, callback ) ); | |
} else if(typeof json_fields.nodeName === "string") { | |
this.async_form_init( ); | |
this.prepare( ); | |
} else { | |
this.async_init( ); | |
} | |
return this; | |
}; | |
FormVM.prototype.prepare = function( ) { | |
var value_get = function () { | |
if ( this.type === "radio" ) { | |
var radios = document.querySelectorAll("form#"+this.container+" [name="+this.input+"]" ); | |
if ( radios ) { | |
for (var i in radios) { | |
if (radios[i].checked) { | |
return radios[i].value; | |
} | |
} | |
} | |
return ""; | |
} | |
return document.querySelector("form#"+this.container+" #"+ this.input ).value; | |
}; | |
for (var i in this.json_fields ){ | |
Object.defineProperty(this.json_fields[i], "value", { | |
get : value_get | |
}); | |
} | |
}; | |
FormVM.prototype.async_form_init = function( ) { | |
this.form = this.json_fields; | |
var fields = this.form.querySelectorAll("input:not([type=submit]), select, textarea"); | |
var counter = 1; | |
var total = Object.keys(fields).length; | |
this.json_fields = {}; | |
console.log(fields); | |
for (var i in fields){ | |
this.json_fields[fields[i].name] = { | |
"input" : fields[i].name, | |
"format" : fields[i].getAttribute("data-format"), | |
"type" : fields[i].type, | |
"container" : this.form.id | |
}; | |
console.log(counter, total); | |
if (counter === total /* && typeof callback === "function" */ ) { | |
return true; | |
} | |
counter++; | |
} | |
}; | |
FormVM.prototype.async_init = function() { | |
for (var i in this.json_fields){ | |
this.add(i, this.json_fields[i].input, this.json_fields[i].search_by); | |
} | |
}; | |
FormVM.prototype.sync_init = function( callback ) { | |
var counter = 1; | |
var total = Object.keys(json_fields).length; | |
for (var i in this.json_fields){ | |
this.add(i, this.json_fields[i].input); | |
if (counter === total /* && typeof callback === "function" */) { | |
callback(); | |
} | |
counter++; | |
} | |
}; | |
//Adiere un nuevo field al objeto fields | |
FormVM.prototype.add = function( field_id, input_id, by ){ | |
var field; | |
if ( by == "name" ) { | |
field = document.querySelector("input[name=" + input_id + "]"); | |
}else{ | |
field = document.getElementById( input_id ); | |
} | |
this.fields[field_id] = field; | |
}; | |
//Adiere un nuevo field al objeto fields | |
FormVM.prototype.addDOMField = function( input_id ){ | |
var field = document.getElementById( input_id ); | |
this.fields[field_id] = field; | |
}; | |
// FormVM.prototype.validate = function(callback){ | |
// var errors = []; | |
// var fields_to_insert = {}; | |
// var fields_to_error = {}; | |
// for ( k in this.domFields ){ | |
// console.log("value", k, this.domFields[k] ); | |
// var r = this.evaluate( k, this.domFields[k] ); | |
// if ( r ) { | |
// fields_to_insert[r.id] = r.value; | |
// }else{ | |
// errors.push("Error en el campo: " + this.domFields[k].id); | |
// fields_to_error[r.id] = this.domFields[k]; | |
// }; | |
// } | |
// if (errors.length > 0) { | |
// callback(errors, fields_to_error); | |
// } else { | |
// callback(null, fields_to_insert); | |
// } | |
// }; | |
FormVM.prototype.go = function ( callback ) { | |
this.response = {}; | |
this.errors = null; | |
//Format | |
for ( var i in this.json_fields ) { | |
console.log(i, this.json_fields[i].format); | |
this.format[this.json_fields[i].format].call(this, i, this.json_fields[i]); | |
} | |
callback(this.errors, this.response); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment