From 37f63cbac335e5730cd1a27086d5ea2e62fa1e90 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 23 Jan 2015 10:46:17 +0100 Subject: [PATCH] Centralised ticket form changes observer. --- .../_application_controller.js.coffee | 31 +++++++++++++++++-- .../controllers/agent_ticket_create.js.coffee | 29 ++--------------- .../customer_ticket_create.js.coffee | 27 ++-------------- .../app/controllers/ticket_zoom.js.coffee | 25 +-------------- app/controllers/tickets_controller.rb | 6 ++-- 5 files changed, 38 insertions(+), 80 deletions(-) diff --git a/app/assets/javascripts/app/controllers/_application_controller.js.coffee b/app/assets/javascripts/app/controllers/_application_controller.js.coffee index 078e3dd82..5db49b072 100644 --- a/app/assets/javascripts/app/controllers/_application_controller.js.coffee +++ b/app/assets/javascripts/app/controllers/_application_controller.js.coffee @@ -467,6 +467,31 @@ class App.Controller extends Spine.Controller ws_send: (data) -> App.Event.trigger( 'ws:send', JSON.stringify(data) ) + # central method, is getting called on every ticket form change + ticketFormChanges: (params, attribute, attributes, classname, form, ui) => + if @form_meta.dependencies && @form_meta.dependencies[attribute.name] + dependency = @form_meta.dependencies[attribute.name][ parseInt(params[attribute.name]) ] + if !dependency + dependency = @form_meta.dependencies[attribute.name][ params[attribute.name] ] + if dependency + for fieldNameToChange of dependency + filter = [] + if dependency[fieldNameToChange] + filter = dependency[fieldNameToChange] + + # find element to replace + for item in attributes + if item.name is fieldNameToChange + item['filter'] = {} + item['filter'][ fieldNameToChange ] = filter + item.default = params[item.name] + #if !item.default + # delete item['default'] + newElement = ui.formGenItem( item, classname, form ) + + # replace new option list + form.find('[name="' + fieldNameToChange + '"]').closest('.form-group').replaceWith( newElement ) + class App.ControllerPermanent extends App.Controller constructor: -> super @@ -485,10 +510,10 @@ class App.ControllerModal extends App.Controller '.modal-body': 'body' events: - 'submit form': 'onSubmit' - 'click .js-submit:not(.is-disabled)': 'onSubmit' + 'submit form': 'onSubmit' + 'click .js-submit: not(.is-disabled)': 'onSubmit' 'click .js-cancel': 'hide' - 'click .js-close': 'hide' + 'click .js-close': 'hide' className: 'modal fade' diff --git a/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee b/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee index aa7632475..387657240 100644 --- a/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee +++ b/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee @@ -205,29 +205,6 @@ class App.TicketCreate extends App.Controller form_id: @form_id ) - formChanges = (params, attribute, attributes, classname, form, ui) => - if @form_meta.dependencies && @form_meta.dependencies[attribute.name] - dependency = @form_meta.dependencies[attribute.name][ parseInt(params[attribute.name]) ] - if dependency - - for fieldNameToChange of dependency - filter = [] - if dependency[fieldNameToChange] - filter = dependency[fieldNameToChange] - - # find element to replace - for item in attributes - if item.name is fieldNameToChange - item['filter'] = {} - item['filter'][ fieldNameToChange ] = filter - item.default = params[item.name] - #if !item.default - # delete item['default'] - newElement = ui.formGenItem( item, classname, form ) - - # replace new option list - form.find('[name="' + fieldNameToChange + '"]').closest('.form-group').replaceWith( newElement ) - signatureChanges = (params, attribute, attributes, classname, form, ui) => if attribute && attribute.name is 'group_id' signature = undefined @@ -274,7 +251,7 @@ class App.TicketCreate extends App.Controller events: 'change [name=customer_id]': @localUserInfo handlers: [ - formChanges, + @ticketFormChanges, signatureChanges, ] filter: @form_meta.filter @@ -297,7 +274,7 @@ class App.TicketCreate extends App.Controller events: 'change [name=customer_id]': @localUserInfo handlers: [ - formChanges, + @ticketFormChanges, signatureChanges, ] filter: @form_meta.filter @@ -312,7 +289,7 @@ class App.TicketCreate extends App.Controller events: 'change [name=customer_id]': @localUserInfo handlers: [ - formChanges, + @ticketFormChanges, signatureChanges, ] filter: @form_meta.filter diff --git a/app/assets/javascripts/app/controllers/customer_ticket_create.js.coffee b/app/assets/javascripts/app/controllers/customer_ticket_create.js.coffee index 62b295826..fbcf2bd60 100644 --- a/app/assets/javascripts/app/controllers/customer_ticket_create.js.coffee +++ b/app/assets/javascripts/app/controllers/customer_ticket_create.js.coffee @@ -64,29 +64,6 @@ class Index extends App.ControllerContent groupFilter = [groupFilter] @form_meta.filter.group_id = groupFilter - formChanges = (params, attribute, attributes, classname, form, ui) => - if @form_meta.dependencies && @form_meta.dependencies[attribute.name] - dependency = @form_meta.dependencies[attribute.name][ parseInt(params[attribute.name]) ] - if dependency - - for fieldNameToChange of dependency - filter = [] - if dependency[fieldNameToChange] - filter = dependency[fieldNameToChange] - - # find element to replace - for item in attributes - if item.name is fieldNameToChange - item['filter'] = {} - item['filter'][ fieldNameToChange ] = filter - item.default = params[item.name] - #if !item.default - # delete item['default'] - newElement = ui.formGenItem( item, classname, form ) - - # replace new option list - form.find('[name="' + fieldNameToChange + '"]').closest('.form-group').replaceWith( newElement ) - @html App.view('customer_ticket_create')( head: 'New Ticket' ) new App.ControllerForm( @@ -95,7 +72,7 @@ class Index extends App.ControllerContent model: App.Ticket screen: 'create_top' handlers: [ - formChanges + @ticketFormChanges ] filter: @form_meta.filter autofocus: true @@ -115,7 +92,7 @@ class Index extends App.ControllerContent model: App.Ticket screen: 'create_middle' handlers: [ - formChanges + @ticketFormChanges ] filter: @form_meta.filter params: defaults diff --git a/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee b/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee index 501225356..0967cb093 100644 --- a/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee +++ b/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee @@ -267,29 +267,6 @@ class App.TicketZoom extends App.Controller console.log('SHOW', ticket.id) el.find('.edit').html('') - formChanges = (params, attribute, attributes, classname, form, ui) => - if @form_meta.dependencies && @form_meta.dependencies[attribute.name] - dependency = @form_meta.dependencies[attribute.name][ parseInt(params[attribute.name]) ] - if dependency - - for fieldNameToChange of dependency - filter = [] - if dependency[fieldNameToChange] - filter = dependency[fieldNameToChange] - - # find element to replace - for item in attributes - if item.name is fieldNameToChange - item['filter'] = {} - item['filter'][ fieldNameToChange ] = filter - item.default = params[item.name] - #if !item.default - # delete item['default'] - newElement = ui.formGenItem( item, classname, form ) - - # replace new option list - form.find('[name="' + fieldNameToChange + '"]').closest('.form-group').replaceWith( newElement ) - defaults = ticket.attributes() task_state = @taskGet('ticket') modelDiff = @getDiff( defaults, task_state ) @@ -305,7 +282,7 @@ class App.TicketZoom extends App.Controller screen: 'edit' params: App.Ticket.find(ticket.id) handlers: [ - formChanges + @ticketFormChanges ] filter: @form_meta.filter params: defaults diff --git a/app/controllers/tickets_controller.rb b/app/controllers/tickets_controller.rb index b8fc5b27b..10e134267 100644 --- a/app/controllers/tickets_controller.rb +++ b/app/controllers/tickets_controller.rb @@ -279,8 +279,10 @@ class TicketsController < ApplicationController :assets => assets, :links => link_list, :tags => tags, - :form_meta => attributes_to_change, - :edit_form => attributes_to_change, + :form_meta => { + :filter => attributes_to_change[:filter], + :dependencies => attributes_to_change[:dependencies], + } } end