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
@overview_id = params.overview_id
else
@overview_id = false
@overview_id = undefined
@key = "ticket::#{@ticket_id}"
cache = App.SessionStorage.get(@key)

View file

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

View file

@ -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
@openTicket(id, url)

View file

@ -2,6 +2,7 @@
#= require_self
#= require_tree ./lib/app_init
#= require_tree ./lib/mixins
#= require ./config.coffee
#= require_tree ./models
#= 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: ->
"#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 = {}

View file

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