diff --git a/app/assets/javascripts/app/controllers/ticket_link_add.coffee b/app/assets/javascripts/app/controllers/ticket_link_add.coffee new file mode 100644 index 000000000..ac9db0c51 --- /dev/null +++ b/app/assets/javascripts/app/controllers/ticket_link_add.coffee @@ -0,0 +1,107 @@ +class App.TicketLinkAdd extends App.ControllerModal + buttonClose: true + buttonCancel: true + buttonSubmit: true + head: 'Link' + shown: false + + constructor: -> + super + @fetch() + + url: -> + if @object instanceof App.Ticket + "#{@apiPath}/ticket_related/#{@object.id}" + else + @apiPath + '/ticket_recent' + + fetch: => + @ajax( + id: 'ticket_related' + type: 'GET' + url: @url() + processData: true + success: (data, status, xhr) => + App.Collection.loadAssets(data.assets) + + @ticketIdsByCustomer = data.ticket_ids_by_customer + @ticketIdsRecentViewed = data.ticket_ids_recent_viewed + @render() + ) + + content: => + content = $( App.view('link/ticket/add')( + link_object: @link_object + link_object_id: @link_object_id + link_types: @link_types + object: @object + hasByCustomer: (@ticketIdsByCustomer isnt undefined) + hasRecentViewed: (@ticketIdsRecentViewed isnt undefined) + )) + + if @ticketIdsByCustomer + @buildContentTable(content, @ticketIdsByCustomer, 'ticket-merge-customer-tickets') + + if @ticketIdsRecentViewed + @buildContentTable(content, @ticketIdsRecentViewed, 'ticket-merge-recent-tickets') + + content.delegate('[name="ticket_number"]', 'focus', (e) -> + $(e.target).parents().find('[name="radio"]').prop('checked', false) + ) + + content.delegate('[name="radio"]', 'click', (e) -> + if $(e.target).prop('checked') + ticket_id = $(e.target).val() + ticket = App.Ticket.fullLocal( ticket_id ) + $(e.target).parents().find('[name="ticket_number"]').val(ticket.number) + ) + content + + buildContentTable: (container, ticket_ids, tableId) -> + if @object instanceof App.Ticket + ticket_ids = ticket_ids.filter (elem) => elem != @object.id + + new App.TicketList( + tableId: tableId + el: container.find("##{tableId}") + ticket_ids: ticket_ids + radio: true + ) + + + onSubmit: (e) => + params = @formParam(e.target) + + if !params['ticket_number'] + alert('Ticket# is needed!') + return + if !params['link_type'] + alert('Link type is needed!') + return + + # get data + @ajax( + id: "links_add_#{@object.id}_#{@object_type}" + type: 'GET' + url: "#{@apiPath}/links/add" + data: + link_type: params['link_type'] + link_object_target: @link_object + link_object_target_value: @object.id + link_object_source: 'Ticket' + link_object_source_number: params['ticket_number'] + processData: true + success: (data, status, xhr) => + @close() + @parent.fetch() + error: (xhr, statusText, error) => + detailsRaw = xhr.responseText + details = {} + if !_.isEmpty(detailsRaw) + details = JSON.parse(detailsRaw) + @notify( + type: 'error' + msg: App.i18n.translateContent(details.error) + removeAll: true + ) + ) diff --git a/app/assets/javascripts/app/controllers/ticket_zoom/sidebar_ticket.coffee b/app/assets/javascripts/app/controllers/ticket_zoom/sidebar_ticket.coffee index e25c1e833..097ef8e3f 100644 --- a/app/assets/javascripts/app/controllers/ticket_zoom/sidebar_ticket.coffee +++ b/app/assets/javascripts/app/controllers/ticket_zoom/sidebar_ticket.coffee @@ -115,7 +115,7 @@ class SidebarTicket extends App.Controller object: @ticket tags: @tags ) - @linkWidget = new App.WidgetLink( + @linkWidget = new App.WidgetLink.Ticket( el: localEl.filter('.links') object_type: 'Ticket' object: @ticket diff --git a/app/assets/javascripts/app/controllers/widget/link.coffee b/app/assets/javascripts/app/controllers/widget/link.coffee index 043ee6b4d..9b6ce92c9 100644 --- a/app/assets/javascripts/app/controllers/widget/link.coffee +++ b/app/assets/javascripts/app/controllers/widget/link.coffee @@ -1,6 +1,6 @@ +# Abstract base class for Link controllers class App.WidgetLink extends App.Controller @extend App.PopoverProvidable - @registerPopovers 'Ticket' @popoversDefaults: position: 'left' @@ -8,7 +8,6 @@ class App.WidgetLink extends App.Controller events: 'click .js-add': 'add' 'click .js-delete': 'delete' - 'click .js-delete .icon': 'delete' constructor: -> super @@ -42,37 +41,6 @@ class App.WidgetLink extends App.Controller @localLinks = _.clone(links) @render() - render: => - return if @lastLocalLinks && _.isEqual(@lastLocalLinks, @localLinks) - @lastLocalLinks = _.clone(@localLinks) - list = {} - for item in @localLinks - if !list[ item['link_type'] ] - list[ item['link_type'] ] = { - tickets: [] - } - - if item['link_object'] is 'Ticket' - ticket = App.Ticket.fullLocal( item['link_object_value'] ) - if ticket.state.name is 'merged' - ticket.css = 'merged' - list[ item['link_type'] ].tickets.push ticket - - # create ticket lists - for type of list - list[type].ticketList = App.view('generic/ticket_list')( - tickets: list[type].tickets - object: 'Ticket' - linkType: type - ) unless list[type].tickets.length == 0 - - # insert data - @html App.view('link/info')( - links: list - ) - - @renderPopovers() - delete: (e) => e.preventDefault() link_type = $(e.currentTarget).data('link-type') @@ -95,117 +63,10 @@ class App.WidgetLink extends App.Controller processData: true success: (data, status, xhr) => @fetch() - ) - - add: (e) => - e.preventDefault() - new App.LinkAdd( - link_object: @object_type - link_object_id: @object.id - object: @object - parent: @ - container: @el.closest('.content') - ) - -class App.LinkAdd extends App.ControllerModal - buttonClose: true - buttonCancel: true - buttonSubmit: true - head: 'Link' - shown: false - - constructor: -> - super - @ticket = @object - @fetch() - - fetch: => - @ajax( - id: 'ticket_related' - type: 'GET' - url: "#{@apiPath}/ticket_related/#{@ticket.id}" - processData: true - success: (data, status, xhr) => - App.Collection.loadAssets(data.assets) - @ticketIdsByCustomer = data.ticket_ids_by_customer - @ticketIdsRecentViewed = data.ticket_ids_recent_viewed - @render() - ) - - content: => - content = $( App.view('link/add')( - link_object: @link_object - link_object_id: @link_object_id - object: @object - )) - - ticketIdsByCustomer = [] - for ticket_id in @ticketIdsByCustomer - if ticket_id isnt @ticket.id - ticketIdsByCustomer.push ticket_id - new App.TicketList( - tableId: 'ticket-merge-customer-tickets' - el: content.find('#ticket-merge-customer-tickets') - ticket_ids: ticketIdsByCustomer - radio: true - ) - - ticketIdsByRecentView = [] - for ticket_id in @ticketIdsRecentViewed - if ticket_id isnt @ticket.id - ticketIdsByRecentView.push ticket_id - new App.TicketList( - tableId: 'ticket-merge-recent-tickets' - el: content.find('#ticket-merge-recent-tickets') - ticket_ids: ticketIdsByRecentView - radio: true - ) - - content.delegate('[name="ticket_number"]', 'focus', (e) -> - $(e.target).parents().find('[name="radio"]').prop('checked', false) - ) - - content.delegate('[name="radio"]', 'click', (e) -> - if $(e.target).prop('checked') - ticket_id = $(e.target).val() - ticket = App.Ticket.fullLocal( ticket_id ) - $(e.target).parents().find('[name="ticket_number"]').val(ticket.number) - ) - content - - onSubmit: (e) => - params = @formParam(e.target) - - if !params['ticket_number'] - alert('Ticket# is needed!') - return - if !params['link_type'] - alert('Link type is needed!') - return - - # get data - @ajax( - id: "links_add_#{@object.id}_#{@object_type}" - type: 'GET' - url: "#{@apiPath}/links/add" - data: - link_type: params['link_type'] - link_object_target: 'Ticket' - link_object_target_value: @object.id - link_object_source: 'Ticket' - link_object_source_number: params['ticket_number'] - processData: true - success: (data, status, xhr) => - @close() - @parent.fetch() error: (xhr, statusText, error) => - detailsRaw = xhr.responseText - details = {} - if !_.isEmpty(detailsRaw) - details = JSON.parse(detailsRaw) @notify( type: 'error' - msg: App.i18n.translateContent(details.error) + msg: App.i18n.translateContent(xhr.responseJSON?.error || "Couldn't save changes") removeAll: true ) ) diff --git a/app/assets/javascripts/app/controllers/widget/link/ticket.coffee b/app/assets/javascripts/app/controllers/widget/link/ticket.coffee new file mode 100644 index 000000000..2ec145265 --- /dev/null +++ b/app/assets/javascripts/app/controllers/widget/link/ticket.coffee @@ -0,0 +1,46 @@ +class App.WidgetLink.Ticket extends App.WidgetLink + @registerPopovers 'Ticket' + + render: => + return if @lastLocalLinks && _.isEqual(@lastLocalLinks, @localLinks) + @lastLocalLinks = _.clone(@localLinks) + + list = {} + + for item in @localLinks + if !list[ item['link_type'] ] + list[ item['link_type'] ] = { + tickets: [] + } + + if item['link_object'] is 'Ticket' + ticket = App.Ticket.fullLocal( item['link_object_value'] ) + if ticket.state.name is 'merged' + ticket.css = 'merged' + list[ item['link_type'] ].tickets.push ticket + + # create ticket lists + for type of list + list[type].ticketList = App.view('generic/ticket_list')( + tickets: list[type].tickets + object: 'Ticket' + linkType: type + ) unless list[type].tickets.length == 0 + + # insert data + @html App.view('link/ticket/list')( + links: list + ) + + @renderPopovers() + + add: (e) => + e.preventDefault() + new App.TicketLinkAdd( + link_object: @object_type + link_object_id: @object.id + link_types: [['normal', 'Normal'], ['child', 'Child'], ['parent', 'Parent']] + object: @object + parent: @ + container: @el.closest('.content') + ) diff --git a/app/assets/javascripts/app/views/link/add.jst.eco b/app/assets/javascripts/app/views/link/ticket/add.jst.eco similarity index 100% rename from app/assets/javascripts/app/views/link/add.jst.eco rename to app/assets/javascripts/app/views/link/ticket/add.jst.eco diff --git a/app/assets/javascripts/app/views/link/info.jst.eco b/app/assets/javascripts/app/views/link/ticket/list.jst.eco similarity index 96% rename from app/assets/javascripts/app/views/link/info.jst.eco rename to app/assets/javascripts/app/views/link/ticket/list.jst.eco index 3b22687f4..f58cc7549 100644 --- a/app/assets/javascripts/app/views/link/info.jst.eco +++ b/app/assets/javascripts/app/views/link/ticket/list.jst.eco @@ -5,4 +5,4 @@ <%- @links[type].ticketList %> <% end %> -