diff --git a/app/assets/javascripts/app/controllers/_application_controller_form.coffee b/app/assets/javascripts/app/controllers/_application_controller_form.coffee index 9afbad5a1..1a8841d32 100644 --- a/app/assets/javascripts/app/controllers/_application_controller_form.coffee +++ b/app/assets/javascripts/app/controllers/_application_controller_form.coffee @@ -443,6 +443,8 @@ class App.ControllerForm extends App.Controller if item.type is 'boolean' if value is '' value = undefined + else if value is undefined + value = false else if value is 'true' value = true else if value is 'false' @@ -463,21 +465,31 @@ class App.ControllerForm extends App.Controller # verify if we have not checked checkboxes uncheckParam = {} lookupForm.find('input[type=checkbox]').each( (index) -> + type = $(@).data('field-type') checked = $(@).attr('checked') name = $(@).attr('name') if name && !checked && (!(name of param) || param[name] is '') if !(name of uncheckParam) - uncheckParam[name] = undefined + if type is 'boolean' + uncheckParam[name] = false + else + uncheckParam[name] = undefined else uncheckParam[name] = [] + true ) # verify if we have not checked radios lookupForm.find('input[type=radio]').each( (index) -> + type = $(@).data('field-type') checked = $(@).attr('checked') name = $(@).attr('name') if name && !checked && !(name of param) - uncheckParam[name] = undefined + if type is 'boolean' + uncheckParam[name] = false + else + uncheckParam[name] = undefined + true ) # apply empty checkboxes & radio values to params diff --git a/app/assets/javascripts/app/controllers/_ui_element/object_manager_attribute.coffee b/app/assets/javascripts/app/controllers/_ui_element/object_manager_attribute.coffee index 80d61840b..271ad8cd7 100644 --- a/app/assets/javascripts/app/controllers/_ui_element/object_manager_attribute.coffee +++ b/app/assets/javascripts/app/controllers/_ui_element/object_manager_attribute.coffee @@ -134,6 +134,17 @@ class App.UiElement.object_manager_attribute extends App.UiElement.ApplicationUi init = false if params && !params.id init = true + + data = objects[object] + if init + for role, screenOptions of data + for screen, options of screenOptions + for key, defaultValue of options + params.screens ||= {} + params.screens[screen] ||= {} + params.screens[screen][role] ||= {} + params.screens[screen][role][key] = defaultValue + item = $(App.view('object_manager/screens')( attribute: attribute data: objects[object] diff --git a/app/assets/javascripts/app/models/_application_model.coffee b/app/assets/javascripts/app/models/_application_model.coffee index b0f1315db..141cb5ae1 100644 --- a/app/assets/javascripts/app/models/_application_model.coffee +++ b/app/assets/javascripts/app/models/_application_model.coffee @@ -237,8 +237,8 @@ set new attributes of model (remove already available attributes) attributesNew = {} if screen for attribute in attributes - if attribute && attribute.screen && attribute.screen[ screen ] && !_.isEmpty(attribute.screen[ screen ]) - for item, value of attribute.screen[ screen ] + if attribute && attribute.screen && attribute.screen[screen] && (!_.isEmpty(attribute.screen[screen]) && (attribute.screen[screen].shown is true || attribute.screen[screen].shown is undefined)) + for item, value of attribute.screen[screen] attribute[item] = value attributesNew[ attribute.name ] = attribute diff --git a/app/assets/javascripts/app/views/object_manager/screens.jst.eco b/app/assets/javascripts/app/views/object_manager/screens.jst.eco index 1c9337972..ebf8f3dbd 100644 --- a/app/assets/javascripts/app/views/object_manager/screens.jst.eco +++ b/app/assets/javascripts/app/views/object_manager/screens.jst.eco @@ -20,7 +20,7 @@ <%= screen %> <% for key, defaultValue of options: %> - <%- @T(key) %>: checked<% end %> value="true"> + <%- @T(key) %>: checked<% end %> value="true"> <% end %> <% end %> <% end %> diff --git a/public/assets/tests/form.js b/public/assets/tests/form.js index 895c433b2..c49ff44f3 100644 --- a/public/assets/tests/form.js +++ b/public/assets/tests/form.js @@ -26,6 +26,8 @@ test("form elements check", function() { datetime1: Date.parse('2015-01-11T12:40:00Z'), checkbox1: [], checkbox2: '1', + boolean1: true, + boolean2: false, } new App.ControllerForm({ el: el, @@ -49,6 +51,9 @@ test("form elements check", function() { { name: 'datetime2', display: 'Datetime2', tag: 'datetime', null: false, default: defaults['datetime2'] }, { name: 'checkbox1', display: 'Checkbox1', tag: 'checkbox', null: false, default: defaults['checkbox1'], options: { a: 'AA', b: 'BB' } }, { name: 'checkbox2', display: 'Checkbox2', tag: 'checkbox', null: false, default: defaults['checkbox2'], options: { 1: '11' } }, + { name: 'boolean1', display: 'Boolean1', tag: 'boolean', null: false, default: defaults['boolean1'] }, + { name: 'boolean2', display: 'Boolean2', tag: 'boolean', null: false, default: defaults['boolean2'] }, + { name: 'boolean3', display: 'Boolean3', tag: 'boolean', null: false, default: defaults['boolean3'] }, ] }, autofocus: true @@ -108,6 +113,9 @@ test("form elements check", function() { equal(el.find('[name="checkbox1"]').last().is(":checked"), false) equal(el.find('[name="checkbox2"]').is(":checked"), true) + equal(el.find('[name="boolean1"]').val(), 'true') + equal(el.find('[name="boolean1"]').val(), 'true') + equal(el.find('[name="boolean2"]').val(), 'false') }); test("form params check", function() { @@ -147,6 +155,8 @@ test("form params check", function() { checkbox3: 'd', radiobox1: undefined, radiobox2: 'a', + boolean1: true, + boolean2: false, } new App.ControllerForm({ el: el, @@ -192,6 +202,9 @@ test("form params check", function() { { name: 'checkbox4', display: 'Checkbox4', tag: 'checkbox', null: false, default: defaults['checkbox4'], options: { aa: 'AA', bb: 'BB' } }, { name: 'radiobox1', display: 'Radiobox1', tag: 'radio', null: false, default: defaults['radiobox1'], options: { a: 'AA', b: 'BB' } }, { name: 'radiobox2', display: 'Radiobox2', tag: 'radio', null: false, default: defaults['radiobox2'], options: { a: '11' } }, + { name: 'boolean1', display: 'Boolean1', tag: 'boolean', null: false, default: defaults['boolean1'] }, + { name: 'boolean2', display: 'Boolean2', tag: 'boolean', null: false, default: defaults['boolean2'] }, + { name: 'boolean3', display: 'Boolean3', tag: 'boolean', null: false, default: defaults['boolean3'] }, ], }, params: defaults, @@ -296,6 +309,9 @@ test("form params check", function() { checkbox4: [], radiobox1: undefined, radiobox2: 'a', + boolean1: true, + boolean2: false, + boolean3: true, } deepEqual(params, test_params, 'form param check') @@ -995,5 +1011,168 @@ test("form params check direct", function() { //console.log('params', params) //console.log('test_params', test_params) deepEqual(params, test_params, 'form param check') +}); + +test("object manager form 1", function() { + + $('#forms').append('

object manager 1

') + var el = $('#form11') + + var defaults = {} + new App.ControllerForm({ + el: el, + model: { + configure_attributes: [ + { name: 'data_type', display: 'Format', tag: 'object_manager_attribute', null: false }, + ], + }, + params: $.extend(defaults, { object: 'Ticket' }), + autofocus: true + }); + + var params = App.ControllerForm.params(el) + var test_params = { + data_option: { + default: "", + maxlength: 120, + type: "text" + }, + data_type: "input", + screens: { + create_middle: { + "ticket.agent": { + shown: true, + required: false, + }, + "ticket.customer": { + shown: true, + required: false, + } + }, + edit: { + "ticket.agent": { + shown: true, + required: false, + }, + "ticket.customer": { + shown: true, + required: false, + } + } + } + } + + deepEqual(params, test_params, 'form param check') + + el.find('[name=data_type]').val('datetime').trigger('change') + + params = App.ControllerForm.params(el) + var test_params = { + data_option: { + diff: 24, + future: true, + past: true + }, + data_type: "datetime", + screens: { + create_middle: { + "ticket.agent": { + shown: true, + required: false, + }, + "ticket.customer": { + shown: true, + required: false, + } + }, + edit: { + "ticket.agent": { + shown: true, + required: false, + }, + "ticket.customer": { + shown: true, + required: false, + } + } + } + } + deepEqual(params, test_params, 'form param check') + +}); + +test("object manager form 2", function() { + + $('#forms').append('

object manager 2

') + var el = $('#form12') + + var defaults = { + id: 123, + data_option: { + default: "", + maxlength: 120, + type: "text" + }, + data_type: "input", + screens: { + create_middle: { + "ticket.agent": { + shown: true, + required: false, + }, + }, + edit: { + "ticket.agent": { + shown: true, + required: false, + }, + } + } + } + + new App.ControllerForm({ + el: el, + model: { + configure_attributes: [ + { name: 'data_type', display: 'Format', tag: 'object_manager_attribute', null: false }, + ], + }, + params: $.extend(defaults, { object: 'Ticket' }), + autofocus: true + }); + + var params = App.ControllerForm.params(el) + var test_params = { + data_option: { + default: "", + maxlength: 120, + type: "text" + }, + data_type: "input", + screens: { + create_middle: { + "ticket.agent": { + shown: true, + required: false, + }, + "ticket.customer": { + shown: false, + required: false, + } + }, + edit: { + "ticket.agent": { + shown: true, + required: false, + }, + "ticket.customer": { + shown: false, + required: false, + } + } + } + } + + deepEqual(params, test_params, 'form param check') });