Refactor frontend ticket navigation with TicketNavigable mixin

This commit is contained in:
Ryan Lue 2018-06-20 17:39:33 +08:00
parent 14fb64ba0a
commit 77ae51c914
7 changed files with 80 additions and 72 deletions

View file

@ -25,7 +25,7 @@ class App.TicketZoom extends App.Controller
if !params.init if !params.init
@overview_id = params.overview_id @overview_id = params.overview_id
else else
@overview_id = false @overview_id = undefined
@key = "ticket::#{@ticket_id}" @key = "ticket::#{@ticket_id}"
cache = App.SessionStorage.get(@key) cache = App.SessionStorage.get(@key)

View file

@ -1,4 +1,6 @@
class App.TicketZoomAttributeBar extends App.Controller class App.TicketZoomAttributeBar extends App.Controller
@include App.TicketNavigable
elements: elements:
'.js-submitDropdown': 'buttonDropdown' '.js-submitDropdown': 'buttonDropdown'
'.js-reset': 'resetButton' '.js-reset': 'resetButton'
@ -98,26 +100,6 @@ class App.TicketZoomAttributeBar extends App.Controller
@closeTab() @closeTab()
@openNextTicketInOverview() @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) => onActionMacroMouseEnter: (e) =>
@$(e.currentTarget).addClass('is-active') @$(e.currentTarget).addClass('is-active')

View file

@ -1,4 +1,6 @@
class App.TicketZoomOverviewNavigator extends App.Controller class App.TicketZoomOverviewNavigator extends App.Controller
@include App.TicketNavigable
events: events:
'click a': 'open' 'click a': 'open'
@ -59,24 +61,13 @@ class App.TicketZoomOverviewNavigator extends App.Controller
open: (e) => open: (e) =>
e.preventDefault() e.preventDefault()
ticketLink = $(e.target)
# get requested object and location if (id = ticketLink.data('id'))?
id = $(e.target).data('id') url = ticketLink.attr('href')
url = $(e.target).attr('href') else if (id = ticketLink.closest('a').data('id'))?
if !id url = ticketLink.closest('a').attr('href')
id = $(e.target).closest('a').data('id') else
url = $(e.target).closest('a').attr('href') return
# return if we are unable to get id @openTicket(id, url)
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

View file

@ -2,6 +2,7 @@
#= require_self #= require_self
#= require_tree ./lib/app_init #= require_tree ./lib/app_init
#= require_tree ./lib/mixins
#= require ./config.coffee #= require ./config.coffee
#= require_tree ./models #= require_tree ./models
#= require_tree ./controllers #= require_tree ./controllers

View file

@ -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)

View file

@ -76,13 +76,20 @@ You can also create overvies and limit them to specific agents or to groups of a
uiUrl: -> uiUrl: ->
"#ticket/view/#{@link}" "#ticket/view/#{@link}"
nextTicket: (startTicket) => tickets: =>
# coerce id to Ticket object App.OverviewListCollection.get(@link).tickets
startTicket = App.Ticket.find(startTicket) if !(startTicket instanceof App.Ticket)
tickets = App.OverviewListCollection.get(@link).tickets indexOf: (ticket) =>
currentIndex = _.findIndex(tickets, (t) -> t.id == startTicket.id) # coerce id to Ticket object
tickets[currentIndex + 1] 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: ->
groupByAttributes = {} groupByAttributes = {}

View file

@ -2,35 +2,35 @@
require 'browser_test_helper' require 'browser_test_helper'
class AgentTicketMacroTest < TestCase class AgentTicketMacroTest < TestCase
# def test_macro def test_macro
# @browser = browser_instance @browser = browser_instance
# login( login(
# username: 'agent1@example.com', username: 'agent1@example.com',
# password: 'test', password: 'test',
# url: browser_url, url: browser_url,
# ) )
# tasks_close_all() tasks_close_all()
# ticket1 = ticket_create( ticket1 = ticket_create(
# data: { data: {
# customer: 'nico', customer: 'nico',
# group: 'Users', group: 'Users',
# title: 'some subject - macro#1', title: 'some subject - macro#1',
# body: 'some body - macro#1', body: 'some body - macro#1',
# }, },
# ) )
# click(css: '.active.content .js-submitDropdown .js-openDropdownMacro') click(css: '.active.content .js-submitDropdown .js-openDropdownMacro')
# click(css: '.active.content .js-submitDropdown .js-dropdownActionMacro') click(css: '.active.content .js-submitDropdown .js-dropdownActionMacro')
# # verify tags # verify tags
# tags_verify( tags_verify(
# tags: { tags: {
# 'spam' => true, 'spam' => true,
# 'tag1' => false, 'tag1' => false,
# } }
# ) )
# end end
def test_macro_ux_flow_next_up def test_macro_ux_flow_next_up
@browser = browser_instance @browser = browser_instance