From 77ae51c91433496859166a512c3e6fc941669473 Mon Sep 17 00:00:00 2001 From: Ryan Lue Date: Wed, 20 Jun 2018 17:39:33 +0800 Subject: [PATCH] Refactor frontend ticket navigation with TicketNavigable mixin --- .../app/controllers/ticket_zoom.coffee | 2 +- .../ticket_zoom/attribute_bar.coffee | 22 +------- .../ticket_zoom/overview_navigator.coffee | 29 ++++------- app/assets/javascripts/app/index.coffee | 1 + .../app/lib/mixins/ticket_navigable.coffee | 27 ++++++++++ .../javascripts/app/models/overview.coffee | 19 ++++--- test/browser/agent_ticket_macro_test.rb | 52 +++++++++---------- 7 files changed, 80 insertions(+), 72 deletions(-) create mode 100644 app/assets/javascripts/app/lib/mixins/ticket_navigable.coffee diff --git a/app/assets/javascripts/app/controllers/ticket_zoom.coffee b/app/assets/javascripts/app/controllers/ticket_zoom.coffee index 6cf19802e..ee59b997a 100644 --- a/app/assets/javascripts/app/controllers/ticket_zoom.coffee +++ b/app/assets/javascripts/app/controllers/ticket_zoom.coffee @@ -25,7 +25,7 @@ class App.TicketZoom extends App.Controller if !params.init @overview_id = params.overview_id else - @overview_id = false + @overview_id = undefined @key = "ticket::#{@ticket_id}" cache = App.SessionStorage.get(@key) diff --git a/app/assets/javascripts/app/controllers/ticket_zoom/attribute_bar.coffee b/app/assets/javascripts/app/controllers/ticket_zoom/attribute_bar.coffee index 8f960bcd5..c147c47bf 100644 --- a/app/assets/javascripts/app/controllers/ticket_zoom/attribute_bar.coffee +++ b/app/assets/javascripts/app/controllers/ticket_zoom/attribute_bar.coffee @@ -1,4 +1,6 @@ class App.TicketZoomAttributeBar extends App.Controller + @include App.TicketNavigable + elements: '.js-submitDropdown': 'buttonDropdown' '.js-reset': 'resetButton' @@ -98,26 +100,6 @@ class App.TicketZoomAttributeBar extends App.Controller @closeTab() @openNextTicketInOverview() - openNextTicketInOverview: (overview = @overview_id, ticket = @ticket.id) => - # coerce ids to objects - overview = App.Overview.find(overview) if !(overview instanceof App.Overview) - ticket = App.Ticket.find(ticket) if !(ticket instanceof App.Ticket) - return if !overview? || !ticket? - - nextTicket = overview.nextTicket(ticket.id) - return if !nextTicket? - - # open task via task manager to preserve overview information - App.TaskManager.execute( - key: "Ticket-#{nextTicket.id}" - controller: 'TicketZoom' - params: - ticket_id: nextTicket.id - overview_id: overview.id - ) - - @navigate "ticket/zoom/#{nextTicket.id}" - onActionMacroMouseEnter: (e) => @$(e.currentTarget).addClass('is-active') diff --git a/app/assets/javascripts/app/controllers/ticket_zoom/overview_navigator.coffee b/app/assets/javascripts/app/controllers/ticket_zoom/overview_navigator.coffee index e14a04ce3..094bb62b3 100644 --- a/app/assets/javascripts/app/controllers/ticket_zoom/overview_navigator.coffee +++ b/app/assets/javascripts/app/controllers/ticket_zoom/overview_navigator.coffee @@ -1,4 +1,6 @@ class App.TicketZoomOverviewNavigator extends App.Controller + @include App.TicketNavigable + events: 'click a': 'open' @@ -59,24 +61,13 @@ class App.TicketZoomOverviewNavigator extends App.Controller open: (e) => e.preventDefault() + ticketLink = $(e.target) - # get requested object and location - id = $(e.target).data('id') - url = $(e.target).attr('href') - if !id - id = $(e.target).closest('a').data('id') - url = $(e.target).closest('a').attr('href') + if (id = ticketLink.data('id'))? + url = ticketLink.attr('href') + else if (id = ticketLink.closest('a').data('id'))? + url = ticketLink.closest('a').attr('href') + else + return - # return if we are unable to get id - return if !id - - # open task via task manager to get overview information - App.TaskManager.execute( - key: 'Ticket-' + id - controller: 'TicketZoom' - params: - ticket_id: id - overview_id: @overview_id - show: true - ) - @navigate url \ No newline at end of file + @openTicket(id, url) diff --git a/app/assets/javascripts/app/index.coffee b/app/assets/javascripts/app/index.coffee index 67af78fe7..4c2852680 100644 --- a/app/assets/javascripts/app/index.coffee +++ b/app/assets/javascripts/app/index.coffee @@ -2,6 +2,7 @@ #= require_self #= require_tree ./lib/app_init +#= require_tree ./lib/mixins #= require ./config.coffee #= require_tree ./models #= require_tree ./controllers diff --git a/app/assets/javascripts/app/lib/mixins/ticket_navigable.coffee b/app/assets/javascripts/app/lib/mixins/ticket_navigable.coffee new file mode 100644 index 000000000..b44492345 --- /dev/null +++ b/app/assets/javascripts/app/lib/mixins/ticket_navigable.coffee @@ -0,0 +1,27 @@ +# Defines common controller behavior for: +# +# * individual ticket pages ('ticket_zoom') +# * ticket listings ('overviews') +# +# Relies on @overview_id and @ticket_id instance variables +App.TicketNavigable = + openTicket: (ticket_id, url) -> + # coerce Ticket objects to id + ticket_id = ticket_id.id if (ticket_id instanceof App.Ticket) + + @loadTicketTask(ticket_id) + @navigate url ? "ticket/zoom/#{ticket_id}" + + # preserves overview information + loadTicketTask: (ticket_id) -> + App.TaskManager.execute( + key: "Ticket-#{ticket_id}" + controller: 'TicketZoom' + params: { ticket_id: ticket_id, overview_id: @overview_id } + show: true + ) + + openNextTicketInOverview: -> + return if !(@overview_id? && @ticket?) + next_ticket = App.Overview.find(@overview_id).nextTicket(@ticket) + @openTicket(next_ticket.id) diff --git a/app/assets/javascripts/app/models/overview.coffee b/app/assets/javascripts/app/models/overview.coffee index 5ee336061..f5126170a 100644 --- a/app/assets/javascripts/app/models/overview.coffee +++ b/app/assets/javascripts/app/models/overview.coffee @@ -76,13 +76,20 @@ You can also create overvies and limit them to specific agents or to groups of a uiUrl: -> "#ticket/view/#{@link}" - nextTicket: (startTicket) => - # coerce id to Ticket object - startTicket = App.Ticket.find(startTicket) if !(startTicket instanceof App.Ticket) + tickets: => + App.OverviewListCollection.get(@link).tickets - tickets = App.OverviewListCollection.get(@link).tickets - currentIndex = _.findIndex(tickets, (t) -> t.id == startTicket.id) - tickets[currentIndex + 1] + indexOf: (ticket) => + # coerce id to Ticket object + ticket = App.Ticket.find(ticket) if !(isNaN ticket) + _.findIndex(@tickets(), (t) -> t.id == ticket.id) + + nextTicket: (thisTicket) => + thisIndex = @indexOf(thisTicket) + if thisIndex >= 0 then @tickets()[thisIndex + 1] else undefined + + prevTicket: (thisTicket) => + @tickets()[@indexOf(thisTicket) - 1] @groupByAttributes: -> groupByAttributes = {} diff --git a/test/browser/agent_ticket_macro_test.rb b/test/browser/agent_ticket_macro_test.rb index 4b854c068..bc5be96da 100644 --- a/test/browser/agent_ticket_macro_test.rb +++ b/test/browser/agent_ticket_macro_test.rb @@ -2,35 +2,35 @@ require 'browser_test_helper' class AgentTicketMacroTest < TestCase - # def test_macro - # @browser = browser_instance - # login( - # username: 'agent1@example.com', - # password: 'test', - # url: browser_url, - # ) - # tasks_close_all() + def test_macro + @browser = browser_instance + login( + username: 'agent1@example.com', + password: 'test', + url: browser_url, + ) + tasks_close_all() - # ticket1 = ticket_create( - # data: { - # customer: 'nico', - # group: 'Users', - # title: 'some subject - macro#1', - # body: 'some body - macro#1', - # }, - # ) + ticket1 = ticket_create( + data: { + customer: 'nico', + group: 'Users', + title: 'some subject - macro#1', + body: 'some body - macro#1', + }, + ) - # click(css: '.active.content .js-submitDropdown .js-openDropdownMacro') - # click(css: '.active.content .js-submitDropdown .js-dropdownActionMacro') + click(css: '.active.content .js-submitDropdown .js-openDropdownMacro') + click(css: '.active.content .js-submitDropdown .js-dropdownActionMacro') - # # verify tags - # tags_verify( - # tags: { - # 'spam' => true, - # 'tag1' => false, - # } - # ) - # end + # verify tags + tags_verify( + tags: { + 'spam' => true, + 'tag1' => false, + } + ) + end def test_macro_ux_flow_next_up @browser = browser_instance