diff --git a/app/assets/javascripts/app/controllers/_application_controller_form.js.coffee b/app/assets/javascripts/app/controllers/_application_controller_form.js.coffee index cc8ddc75c..3c3e6a39b 100644 --- a/app/assets/javascripts/app/controllers/_application_controller_form.js.coffee +++ b/app/assets/javascripts/app/controllers/_application_controller_form.js.coffee @@ -255,6 +255,214 @@ class App.ControllerForm extends App.Controller item = $( App.view('generic/select')( attribute: attribute ) ) + # postmaster_match + else if attribute.tag is 'postmaster_match' + addItem = (key, displayName, el, defaultValue = '') => + itemInput = $("
") + + # remove on click + itemInput.find('.remove').bind('click', (e) -> + e.preventDefault() + key = $(e.target).prev().attr('name') + return if !key + $(e.target).parent().parent().parent().find('.addSelection select option[value="' + key + '"]').show() + $(e.target).parent().parent().parent().find('.list [name="' + key + '"]').parent().remove() + ) + + # add new item + el.parent().parent().parent().find('.list').append(itemInput) + el.parent().parent().parent().find('.addSelection select').val('') + el.parent().parent().parent().find('.addSelection select option[value="' + key + '"]').hide() + + # scaffold of match elements + item = $(' + ') + + # select shown attributes + loopData = [ + { + value: 'from' + name: 'From' + }, + { + value: 'to' + name: 'To' + }, + { + value: 'cc' + name: 'Cc' + }, + { + value: 'subject' + name: 'Subject' + }, + { + value: 'body' + name: 'Body' + }, + #{ + # value: '-' + # name: '' + # disable: true + #}, + ] + for listItem in loopData + listItem.value = "#{ attribute.name }::#{listItem.value}" + add = { name: '', display: '', tag: 'select', multiple: false, null: false, nulloption: true, options: loopData, translate: true } + item.find('.addSelection').append( @formGenItem( add ) ) + + # bind add click + item.find('.add').bind('click', (e) -> + e.preventDefault() + name = $(@).parent().parent().find('.addSelection').find('select').val() + displayName = $(@).parent().parent().find('.addSelection').find('select option:selected').html() + return if !name + addItem( name, displayName, $(@) ) + ) + + # show default values + loopDataValue = {} + if attribute.value + for key, value of attribute.value + displayName = key + for listItem in loopData + if listItem.value is "#{ attribute.name }::#{key}" + addItem( "#{ attribute.name }::#{key}", listItem.name, item.find('.add a'), value ) + + # postmaster_set + else if attribute.tag is 'postmaster_set' + addItem = (key, displayName, el, defaultValue = '') => + itemInput = $("") + + # remove on click + itemInput.find('.remove').bind('click', (e) -> + e.preventDefault() + key = $(e.target).prev().attr('name') + return if !key + $(e.target).parent().parent().parent().find('.addSelection select option[value="' + key + '"]').show() + $(e.target).parent().parent().parent().find('.list [name="' + key + '"]').parent().remove() + ) + + # add new item + el.parent().parent().parent().find('.list').append(itemInput) + el.parent().parent().parent().find('.addSelection select').val('') + el.parent().parent().parent().find('.addSelection select option[value="' + key + '"]').hide() + + # scaffold of perform elements + item = $(' + ') + + + # select shown attributes + loopData = [ + { + value: 'from' + name: 'From' + }, + { + value: 'to' + name: 'To' + }, + { + value: 'cc' + name: 'Cc' + }, + { + value: 'subject' + name: 'Subject' + }, + { + value: 'body' + name: 'Body' + }, + { + value: '' + name: '-' + disable: true + }, + { + value: 'x-zammad-ticket-priority' + name: 'Ticket Priority' + }, + { + value: 'x-zammad-ticket-state' + name: 'Ticket State' + }, + { + value: 'x-zammad-ticket-customer' + name: 'Ticket Customer' + }, + { + value: 'x-zammad-ticket-group' + name: 'Ticket Group' + }, + { + value: 'x-zammad-ticket-owner' + name: 'Ticket Owner' + }, + { + value: '' + name: '-' + disable: true + }, + { + value: 'x-zammad-article-visibility' + name: 'Article Visibility' + }, + { + value: 'x-zammad-article-type' + name: 'Article Type' + }, + { + value: 'x-zammad-article-sender' + name: 'Article Sender' + }, + { + value: '' + name: '-' + disable: true + }, + { + value: 'x-zammad-ignore' + name: 'Ignore Message' + }, + ] + for listItem in loopData + listItem.value = "#{ attribute.name }::#{listItem.value}" + add = { name: '', display: '', tag: 'select', multiple: false, null: false, nulloption: true, options: loopData, translate: true } + item.find('.addSelection').append( @formGenItem( add ) ) + + item.find('.add').bind('click', (e) -> + e.preventDefault() + name = $(@).parent().parent().find('.addSelection').find('select').val() + displayName = $(@).parent().parent().find('.addSelection').find('select option:selected').html() + return if !name + addItem( name, displayName, $(@) ) + ) + + # show default values + loopDataValue = {} + if attribute.value + for key, value of attribute.value + displayName = key + for listItem in loopData + if listItem.value is "#{ attribute.name }::#{key}" + addItem( "#{ attribute.name }::#{key}", listItem.name, item.find('.add a'), value ) + # select else if attribute.tag is 'input_select' item = $('') @@ -1231,27 +1439,27 @@ class App.ControllerForm extends App.Controller param = {} # create jquery object if not already exists - if typeof form isnt 'function' + if form instanceof jQuery + # do nothing + else form = $(form) - # find form based on sub elements - if form.children()[0] - form = form.children().parents('form') - - # find form based on parents next ') + var el = $('#form5') + var defaults = { + input2: 'some name', + match: { + from: 'some@address', + subject: 'some subject', + }, + set: { + to: 'some@address', + 'x-zammad-ticket-group': 'some group', + }, + } + new App.ControllerForm({ + el: el, + model: { + configure_attributes: [ + { name: 'input1', display: 'Input1', tag: 'input', type: 'text', limit: 100, null: true, default: 'some not used default' }, + { name: 'input2', display: 'Input2', tag: 'input', type: 'text', limit: 100, null: true, default: 'some used default' }, + { name: 'match', display: 'Match', tag: 'postmaster_match', null: false, default: false}, + { name: 'set', display: 'Set', tag: 'postmaster_set', null: false, default: false}, + ], + }, + params: defaults, + }); + params = App.ControllerForm.params( el ) + test_params = { + input1: "some not used default", + input2: "some name", + match: { + from: 'some@address', + subject: 'some subject', + }, + set: { + to: 'some@address', + 'x-zammad-ticket-group': 'some group', + }, + }; + deepEqual( params, test_params, 'form param check' ); + el.find('[name="set::to"]').next().click() + App.Delay.set( function() { + test( "form param check after remove click", function() { + params = App.ControllerForm.params( el ) + test_params = { + input1: "some not used default", + input2: "some name", + match: { + from: 'some@address', + subject: 'some subject', + }, + set: { + 'x-zammad-ticket-group': 'some group', + }, + }; + deepEqual( params, test_params, 'form param check' ); + }); + }, + 1000 + ); + +}); + +test( "form selector", function() { + $('#forms').append('