Browse Source

Improved App.ControllerForm.params(), store field types in .data() and not longer in name of field. Also use values of disabled fields.

Martin Edenhofer 8 years ago
parent
commit
da754d5d25

+ 23 - 19
app/assets/javascripts/app/controllers/_application_controller_form.coffee

@@ -406,39 +406,43 @@ class App.ControllerForm extends App.Controller
         param[name] = $(element).ceg()
 
     # get form elements
-    array = lookupForm.serializeArray()
+    array = lookupForm.serializeArrayWithType()
 
     # array to names
-    for key in array
+    for item in array
 
       # check if item is-hidden and should not be used
-      if lookupForm.find('[name="' + key.name + '"]').hasClass('is-hidden') || lookupForm.find('div[data-name="' + key.name + '"]').hasClass('is-hidden')
-        delete param[key.name]
+      if lookupForm.find('[name="' + item.name + '"]').hasClass('is-hidden') || lookupForm.find('div[data-name="' + item.name + '"]').hasClass('is-hidden')
+        delete param[item.name]
         continue
 
       # collect all params, push it to an array if already exists
-      if param[key.name] isnt undefined
-        if typeof param[key.name] is 'string'
-          param[key.name] = [param[key.name], key.value.trim()]
+      value = item.value.trim()
+      if item.type is 'boolean'
+        if value is ''
+          value = undefined
+        else if value is 'true'
+          value = true
+        else if value is 'false'
+          value = false
+      if item.type is 'integer'
+        if value is ''
+          value = undefined
         else
-          param[key.name].push key.value.trim()
+          value = parseInt(value)
+      if param[item.name] isnt undefined
+        if typeof param[item.name] is 'string'
+          param[item.name] = [param[item.name], value]
+        else
+          param[item.name].push value
       else
-        param[key.name] = key.value.trim()
+        param[item.name] = value
 
     # data type conversion
     for key of param
 
-      # get boolean
-      if key.substr(0,9) is '{boolean}'
-        newKey = key.substr(9, key.length)
-        if param[key] && param[key].toString() is 'true'
-          param[newKey] = true
-        else
-          param[newKey] = false
-        delete param[key]
-
       # get {date}
-      else if key.substr(0,6) is '{date}'
+      if key.substr(0,6) is '{date}'
         newKey = key.substr(6, key.length)
         if lookupForm.find("[data-name=\"#{newKey}\"]").hasClass('is-hidden')
           param[newKey] = null

+ 3 - 5
app/assets/javascripts/app/controllers/_ui_element/active.coffee

@@ -12,10 +12,6 @@ class App.UiElement.active extends App.UiElement.ApplicationUiElement
       { name: 'inactive', value: false }
     ]
 
-    # set data type
-    if attribute.name
-      attribute.name = '{boolean}' + attribute.name
-
     # build options list based on config
     @getConfigOptionList(attribute, params)
 
@@ -26,4 +22,6 @@ class App.UiElement.active extends App.UiElement.ApplicationUiElement
     @selectedOptions(attribute, params)
 
     # return item
-    $( App.view('generic/select')( attribute: attribute ) )
+    item = $( App.view('generic/select')(attribute: attribute) )
+    item.find('select').data('field-type', 'boolean')
+    item

+ 3 - 6
app/assets/javascripts/app/controllers/_ui_element/boolean.coffee

@@ -10,10 +10,6 @@ class App.UiElement.boolean extends App.UiElement.ApplicationUiElement
       ]
       attribute.translate = true
 
-    # set data type
-    if attribute.name
-      attribute.name = '{boolean}' + attribute.name
-
     # build options list based on config
     @getConfigOptionList(attribute, params)
 
@@ -23,5 +19,6 @@ class App.UiElement.boolean extends App.UiElement.ApplicationUiElement
     # finde selected/checked item of list
     @selectedOptions(attribute, params)
 
-    # return item
-    $(App.view('generic/select')(attribute: attribute))
+    item = $(App.view('generic/select')(attribute: attribute))
+    item.find('select').data('field-type', 'boolean')
+    item

+ 4 - 1
app/assets/javascripts/app/controllers/_ui_element/holiday_selector.coffee

@@ -12,6 +12,8 @@ class App.UiElement.holiday_selector
 
     item = $( App.view('calendar/holiday_selector')( attribute: attribute, days: days_new ) )
 
+    item.find('.js-boolean').data('field-type', 'boolean')
+
     # add date picker
     attributeDatepicket =
       name: "#{attribute.name}_date"
@@ -68,9 +70,10 @@ class App.UiElement.holiday_selector
         placeholderDate: date
         placeholderSummary: summary
         nameSummary: "public_holidays::#{date}::summary"
-        nameActive: "{boolean}public_holidays::#{date}::active"
+        nameActive: "public_holidays::#{date}::active"
       )
       item.find('.settings-list-controlRow').before(template)
+      item.find('.js-boolean').data('field-type', 'boolean')
     )
 
     item

+ 3 - 1
app/assets/javascripts/app/controllers/_ui_element/integer.coffee

@@ -3,4 +3,6 @@ class App.UiElement.integer
   @render: (attribute) ->
     attribute.type = 'number'
     attribute.step = '1'
-    $( App.view('generic/input')( attribute: attribute ) )
+    item = $( App.view('generic/input')(attribute: attribute) )
+    item.find('select').data('field-type', 'integer')
+    item

+ 29 - 3
app/assets/javascripts/app/controllers/_ui_element/object_manager_attribute.coffee

@@ -22,7 +22,6 @@ class App.UiElement.object_manager_attribute extends App.UiElement.ApplicationUi
       select: 'Select'
       boolean: 'Boolean'
       integer: 'Integer'
-      autocompletion: 'Autocompletion (AJAX remote URL)'
 
     configureAttributes = [
       { name: attribute.name, display: '', tag: 'select', null: false, options: options, translate: true, default: 'input', disabled: attribute.disabled },
@@ -37,7 +36,7 @@ class App.UiElement.object_manager_attribute extends App.UiElement.ApplicationUi
       params: params
     )
     item.find('.js-dataType').html(dataType.form)
-
+    item.find('.js-boolean').data('field-type', 'boolean')
     item
 
   @dataScreens: (attribute, localParams, params) ->
@@ -130,12 +129,14 @@ class App.UiElement.object_manager_attribute extends App.UiElement.ApplicationUi
     init = false
     if params && !params.id
       init = true
-    $(App.view('object_manager/screens')(
+    item = $(App.view('object_manager/screens')(
       attribute: attribute
       data: objects[object]
       params: params
       init: init
     ))
+    item.find('.js-boolean').data('field-type', 'boolean')
+    item
 
   @input: (item, localParams, params) ->
     configureAttributes = [
@@ -284,11 +285,36 @@ class App.UiElement.object_manager_attribute extends App.UiElement.ApplicationUi
       addRow.find('.js-value').val('')
       addRow.find('.js-selected').prop('checked', false)
     )
+    item.on('change', '.js-key', (e) ->
+      key = $(e.target).val()
+      valueField = $(e.target).closest('tr').find('.js-value[name]')
+      valueField.attr('name', "data_option::options::#{key}")
+    )
     item.on('click', '.js-remove', (e) ->
       $(e.target).closest('tr').remove()
     )
+    lastSelected = undefined
+    item.on('click', '.js-selected', (e) ->
+      checked = $(e.target).prop('checked')
+      value = $(e.target).attr('value')
+      if checked && lastSelected && lastSelected is value
+        $(e.target).prop('checked', false)
+        lastSelected = false
+        return
+      lastSelected = value
+    )
 
   @boolean: (item, localParams, params) ->
+    lastSelected = undefined
+    item.on('click', '.js-selected', (e) ->
+      checked = $(e.target).prop('checked')
+      value = $(e.target).attr('value')
+      if checked && lastSelected && lastSelected is value
+        $(e.target).prop('checked', false)
+        lastSelected = false
+        return
+      lastSelected = value
+    )
 
   @autocompletion: (item, localParams, params) ->
     configureAttributes = [

+ 1 - 1
app/assets/javascripts/app/controllers/_ui_element/timer.coffee

@@ -80,7 +80,7 @@ class App.UiElement.timer
         0: true
 
     timer = $( App.view('generic/timer')( attribute: attribute, days: days, hours: hours, minutes: minutes ) )
-
+    timer.find('.js-boolean').data('field-type', 'boolean')
     timer.find('.select-value').bind('click', (e) =>
       @select(e)
     )

+ 1 - 1
app/assets/javascripts/app/views/calendar/holiday_selector.jst.eco

@@ -11,7 +11,7 @@
     <tr <% if !meta.active: %>class="is-inactive"<% end %> data-date="<%= day %>">
       <td class="u-positionOrigin">
         <label class="checkbox-replacement checkbox-replacement--fullscreen">
-          <input type="checkbox" <% if meta.active: %>checked<% end %> class="js-active" name="{boolean}public_holidays::<%= day %>::active" value="true">
+          <input type="checkbox" <% if meta.active: %>checked<% end %> class="js-active js-boolean" name="public_holidays::<%= day %>::active" value="true">
           <%- @Icon('checkbox', 'icon-unchecked') %>
           <%- @Icon('checkbox-checked', 'icon-checked') %>
         </label>

+ 1 - 1
app/assets/javascripts/app/views/calendar/holiday_selector_placeholder.jst.eco

@@ -1,7 +1,7 @@
 <tr class="" data-date="<%= @placeholderDate %>">
   <td>
     <label class="checkbox-replacement">
-      <input type="checkbox" checked class="js-active" name="<%= @nameActive %>" value="true">
+      <input type="checkbox" checked class="js-active js-boolean" name="<%= @nameActive %>" value="true">
       <%- @Icon('checkbox', 'icon-unchecked') %>
       <%- @Icon('checkbox-checked', 'icon-checked') %>
     </label>

+ 1 - 4
app/assets/javascripts/app/views/generic/input.jst.eco

@@ -1,4 +1 @@
-<input id="<%= @attribute.id %>" type="<%= @attribute.type %>" name="<%= @attribute.name %>" value="<%= @attribute.value %>" class="form-control <%= @attribute.class %>" <% if @attribute.placeholder: %>placeholder="<%- @Ti(@attribute.placeholder) %>"<% end %> <%= @attribute.required %> <%= @attribute.autofocus %> <%- @attribute.autocapitalize %> <%- @attribute.autocomplete %> <% if @attribute.min isnt undefined: %> min="<%= @attribute.min %>"<% end %><% if @attribute.max isnt undefined: %> max="<%= @attribute.max %>"<% end %><% if @attribute.step: %> step="<%= @attribute.step %>"<% end %><% if @attribute.disabled: %> disabled<% end %>/>
-<% if @attribute.disabled: %>
-  <input type="hidden" name="<%= @attribute.name %>" value="<%= @attribute.value %>">
-<% end %>
+<input id="<%= @attribute.id %>" type="<%= @attribute.type %>" name="<%= @attribute.name %>" value="<%= @attribute.value %>" class="form-control <%= @attribute.class %>" <% if @attribute.placeholder: %>placeholder="<%- @Ti(@attribute.placeholder) %>"<% end %> <%= @attribute.required %> <%= @attribute.autofocus %> <%- @attribute.autocapitalize %> <%- @attribute.autocomplete %> <% if @attribute.min isnt undefined: %> min="<%= @attribute.min %>"<% end %><% if @attribute.max isnt undefined: %> max="<%= @attribute.max %>"<% end %><% if @attribute.step: %> step="<%= @attribute.step %>"<% end %><% if @attribute.disabled: %> disabled<% end %>/>

Some files were not shown because too many files changed in this diff