app/assets/javascripts/formagic/inputs/documents.coffee in formagic-0.3.4 vs app/assets/javascripts/formagic/inputs/documents.coffee in formagic-0.3.7
- old
+ new
@@ -1,14 +1,9 @@
# -----------------------------------------------------------------------------
# Author: Alexander Kravets <alex@slatestudio.com>,
# Slate Studio (http://www.slatestudio.com)
-#
-# Coding Guide:
-# https://github.com/thoughtbot/guides/tree/master/style/coffeescript
# -----------------------------------------------------------------------------
-
-# -----------------------------------------------------------------------------
# INPUT "NESTED" FORM
# -----------------------------------------------------------------------------
# Name for this input comes from the Rails gem 'nested_forms'.
#
# Public methods:
@@ -19,13 +14,11 @@
# hideErrorMessage()
# addNewForm(object)
#
# Dependencies:
#= require ./documents_reorder
-#
# -----------------------------------------------------------------------------
-
class @InputForm
constructor: (@name, @nestedObjects, @config, @object) ->
@forms = []
@config.namePrefix ||= name
@@ -44,30 +37,31 @@
@_add_forms()
@_add_new_button()
return this
+ # PRIVATE ===================================================================
- # PRIVATE ===============================================
-
_create_el: ->
@$el =$ "<div class='form-input nested-forms input-#{ @config.klassName }'>"
-
_add_label: ->
- @$label =$ "<span class='label'>#{ @config.label }</span>"
+ @$label =$ "<span class='label'></span>"
+ @$labelTitle =$ "<span class='label-title'>#{ @config.label }</span>"
@$errorMessage =$ "<span class='error-message'></span>"
- @$label.append(@$errorMessage)
- @$el.append(@$label)
+ @$label.append @$labelTitle
+ @$label.append @$errorMessage
+ @$el.append @$label
+ if @config.label == false
+ @$labelTitle.hide()
_extend_schema_with: (name, config) ->
schemaConfig = {}
schemaConfig[name] = config
@config.formSchema = $.extend(schemaConfig, @config.formSchema)
-
_add_forms: ->
# add id to schema
# @NOTE: here we use _id, cause mongosteen returns objects _id, but we should send id for nested documents
@_extend_schema_with('_id', { type: 'hidden', name: 'id', ignoreOnSubmission: @config.ignoreOnSubmission })
@@ -86,70 +80,62 @@
namePrefix = "#{ @config.namePrefix }[#{ i }]"
@forms.push @_render_form(object, namePrefix, @config)
@_bind_forms_reorder()
-
_sort_nested_objects: ->
if @config.sortBy
if @nestedObjects
# this is not required but make things a bit easier on the backend
# as object don't have to be in a specific order.
@nestedObjects.sort (a, b) => parseFloat(a[@config.sortBy]) - parseFloat(b[@config.sortBy])
# normalizes nested objects positions
(o[@config.sortBy] = parseInt(i) + 1) for i, o of @nestedObjects
-
_render_form: (object, namePrefix, config) ->
formConfig = $.extend {}, config,
namePrefix: namePrefix
rootEl: "<li>"
form = new Form(object, formConfig)
@$forms.append form.$el
return form
-
_add_new_button: ->
if ! @config.disableNewDocuments
label = @config.newButtonLabel || "Add"
- @$newButton =$ """<a href='#' class='nested-form-new'>#{ label }</a>"""
+ @$newButton =$ """<button class='nested-form-new'>#{ label }</button>"""
@$el.append @$newButton
- @$newButton.on 'click', (e) => e.preventDefault() ; @addNewForm()
+ @$newButton.on 'click', (e) => @addNewForm()
+ # PUBLIC ====================================================================
- # PUBLIC ================================================
-
initialize: ->
@config.beforeInitialize?(this)
for nestedForm in @forms
nestedForm.initializePlugins()
@config.onInitialize?(this)
-
hash: (hash={}) ->
objects = []
objects.push(form.hash()) for form in @forms
hash[@config.fieldName] = objects
return hash
-
showErrorMessage: (message) ->
@$el.addClass 'error'
@$errorMessage.html(message)
-
hideErrorMessage: ->
@$el.removeClass 'error'
@$errorMessage.html('')
-
addNewForm: (object=null) ->
namePrefix = "#{ @config.namePrefix }[#{ Date.now() }]"
newFormConfig = $.extend({}, @config)
delete newFormConfig.formSchema._id
@@ -171,11 +157,10 @@
@config.onNew?(form)
return form
-
updateValue: (@nestedObjects, @object) ->
# New document should update id, also after uploading images form for existing
# document might change, so we reset all nested forms to reflect these updates.
# This implementation has some problems with jumping screen when using plugins
# also it requires complex implementation for data local storage caching.
@@ -185,15 +170,9 @@
# Initialize input plugins after update
for nestedForm in @forms
nestedForm.initializePlugins()
-
include(InputForm, inputFormReorder)
-
chr.formInputs['form'] = InputForm
chr.formInputs['documents'] = InputForm
-
-
-
-