app/assets/javascripts/formagic/form.coffee in formagic-0.3.4 vs app/assets/javascripts/formagic/form.coffee in formagic-0.3.7

- old
+ new

@@ -1,52 +1,42 @@ # ----------------------------------------------------------------------------- # Author: Alexander Kravets <alex@slatestudio.com>, # Slate Studio (http://www.slatestudio.com) -# -# Coding Guide: -# https://github.com/thoughtbot/guides/tree/master/style/coffeescript # ----------------------------------------------------------------------------- - -# ----------------------------------------------------------------------------- # FORM # ----------------------------------------------------------------------------- -# # Generates form based on provided configuration schema. If schema is not # provided generates default form based on object keys. This uses Rails # conventions for managing names for attributes, arrays, hashs and nested # objects. -# # ----------------------------------------------------------------------------- - class @Form constructor: (@object, @config) -> @groups = [] @inputs = {} - @$el = $(@config.rootEl || "<form class='form'>") + defaultEl = "<form class='form' onsubmit='return false;'>" + @$el = $(@config.rootEl || defaultEl) @schema = @_get_schema() @isRemoved = false @_build_schema(@schema, @$el) @_add_nested_form_remove_button() + # PRIVATE =================================================================== - # PRIVATE =============================================== - _get_schema: -> schema = @config.formSchema if @object schema ?= @_generate_default_schema() return schema - _generate_default_schema: -> schema = {} for key, value of @object schema[key] = @_generate_default_input_config(key, value) return schema - _generate_default_input_config: (fieldName, value) -> config = {} if fieldName[0] == '_' config.type = 'hidden' @@ -62,13 +52,12 @@ else if value.length > 60 config.type = 'text' return config + # INPUTS ==================================================================== - # INPUTS ================================================ - _build_schema: (schema, $el) -> for fieldName, config of schema config.fieldName = fieldName if config.type == 'group' @@ -77,11 +66,10 @@ else input = @_generate_input(fieldName, config) $el.append input.$el - _generate_inputs_group: (klassName, groupConfig) -> $group =$ """<div class='group #{ klassName }' />""" if groupConfig.groupClass $group.addClass groupConfig.groupClass @@ -98,11 +86,10 @@ group = { $el: $group, klassName: klassName, onInitialize: groupConfig.onInitialize } @groups.push group return group - _generate_input: (fieldName, inputConfig) -> if @object value = @object[fieldName] else @@ -114,11 +101,10 @@ input = @_render_input(inputName, inputConfig, value) @inputs[fieldName] = input return input - _render_input: (name, config, value) -> inputConfig = $.extend {}, config inputConfig.label ?= name.titleize() inputConfig.type ?= 'string' @@ -142,13 +128,12 @@ else inputConfig.namePrefix = @config.namePrefix return new inputClass(inputName, value, inputConfig, @object) + # NESTED ==================================================================== - # NESTED ================================================ - _add_nested_form_remove_button: -> if @config.removeButton # add hidden input to the form fieldName = '_destroy' @@ -169,11 +154,10 @@ input.updateValue('true') @$el.hide() @isRemoved = true @config.onRemove?(this) - _forms: -> forms = [ @ ] addNestedForms = (form) -> for name, input of form.inputs @@ -185,19 +169,17 @@ addNestedForms(form) for form in input.forms addNestedForms(@) return forms + # PUBLIC ==================================================================== - # PUBLIC ================================================ - destroy: -> group.destroy?() for group in @groups input.destroy?() for name, input of @inputs @$el.remove() - serialize: (obj={}) -> # serialize everything except file inputs obj[input.name] = input.value for input in @$el.serializeArray() for form in @_forms() @@ -214,30 +196,24 @@ # remove fields with ignoreOnSubmission for key, input of form.inputs if input.config.ignoreOnSubmission delete obj[input.name] - # for k, v of obj - # console.log k - return obj - hash: (hash={}) -> for name, input of @inputs input.hash(hash) return hash - initializePlugins: -> for group in @groups group.onInitialize?(@, group) for name, input of @inputs input.initialize() - showValidationErrors: (errors) -> @hideValidationErrors() for inputName, messages of errors input = @inputs[inputName] @@ -249,19 +225,13 @@ if input firstMessage = messages[0] input.showErrorMessage(firstMessage) - hideValidationErrors: -> for inputName, input of @inputs input.hideErrorMessage() - - updateValues: (object) -> - for name, value of object + updateValues: (@object) -> + for name, value of @object if @inputs[name] - @inputs[name].updateValue(value, object) - - - - + @inputs[name].updateValue(value, @object)