KB Prep: Backport changes that generalize implementation of Links in FE
This commit is contained in:
parent
b96fb53a71
commit
093d8e4722
6 changed files with 157 additions and 143 deletions
107
app/assets/javascripts/app/controllers/ticket_link_add.coffee
Normal file
107
app/assets/javascripts/app/controllers/ticket_link_add.coffee
Normal file
|
@ -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
|
||||||
|
)
|
||||||
|
)
|
|
@ -115,7 +115,7 @@ class SidebarTicket extends App.Controller
|
||||||
object: @ticket
|
object: @ticket
|
||||||
tags: @tags
|
tags: @tags
|
||||||
)
|
)
|
||||||
@linkWidget = new App.WidgetLink(
|
@linkWidget = new App.WidgetLink.Ticket(
|
||||||
el: localEl.filter('.links')
|
el: localEl.filter('.links')
|
||||||
object_type: 'Ticket'
|
object_type: 'Ticket'
|
||||||
object: @ticket
|
object: @ticket
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
|
# Abstract base class for Link controllers
|
||||||
class App.WidgetLink extends App.Controller
|
class App.WidgetLink extends App.Controller
|
||||||
@extend App.PopoverProvidable
|
@extend App.PopoverProvidable
|
||||||
@registerPopovers 'Ticket'
|
|
||||||
|
|
||||||
@popoversDefaults:
|
@popoversDefaults:
|
||||||
position: 'left'
|
position: 'left'
|
||||||
|
@ -8,7 +8,6 @@ class App.WidgetLink extends App.Controller
|
||||||
events:
|
events:
|
||||||
'click .js-add': 'add'
|
'click .js-add': 'add'
|
||||||
'click .js-delete': 'delete'
|
'click .js-delete': 'delete'
|
||||||
'click .js-delete .icon': 'delete'
|
|
||||||
|
|
||||||
constructor: ->
|
constructor: ->
|
||||||
super
|
super
|
||||||
|
@ -42,37 +41,6 @@ class App.WidgetLink extends App.Controller
|
||||||
@localLinks = _.clone(links)
|
@localLinks = _.clone(links)
|
||||||
@render()
|
@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) =>
|
delete: (e) =>
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
link_type = $(e.currentTarget).data('link-type')
|
link_type = $(e.currentTarget).data('link-type')
|
||||||
|
@ -95,117 +63,10 @@ class App.WidgetLink extends App.Controller
|
||||||
processData: true
|
processData: true
|
||||||
success: (data, status, xhr) =>
|
success: (data, status, xhr) =>
|
||||||
@fetch()
|
@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) =>
|
error: (xhr, statusText, error) =>
|
||||||
detailsRaw = xhr.responseText
|
|
||||||
details = {}
|
|
||||||
if !_.isEmpty(detailsRaw)
|
|
||||||
details = JSON.parse(detailsRaw)
|
|
||||||
@notify(
|
@notify(
|
||||||
type: 'error'
|
type: 'error'
|
||||||
msg: App.i18n.translateContent(details.error)
|
msg: App.i18n.translateContent(xhr.responseJSON?.error || "Couldn't save changes")
|
||||||
removeAll: true
|
removeAll: true
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
|
@ -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')
|
||||||
|
)
|
|
@ -5,4 +5,4 @@
|
||||||
<%- @links[type].ticketList %>
|
<%- @links[type].ticketList %>
|
||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
<div class="text-muted u-clickable js-add">+ <%- @T('Add Link') %></div>
|
<div class="text-muted u-clickable js-add">+ <%- @T('Add Link') %></div>
|
Loading…
Reference in a new issue