<commit_before>class App.WidgetLink extends App.Controller
  events:
    'click .js-add': 'add'
    'click .js-delete': 'delete'
    'click .js-delete .icon': 'delete'

  constructor: ->
    super

    # if links are given, do not init fetch
    if @links
      @localLinks = _.clone(@links)
      @render()
      return

    @fetch()

  fetch: =>
    # fetch item on demand
    # get data
    @ajax(
      id:   "links_#{@object.id}_#{@object_type}"
      type: 'GET'
      url:  "#{@apiPath}/links"
      data:
        link_object:       @object_type
        link_object_value: @object.id
      processData: true
      success: (data, status, xhr) =>
        @localLinks = data.links
        App.Collection.loadAssets(data.assets)
        @render()
    )

  reload: (links) =>
    @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'] ] = []

      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'] ].push ticket

    # insert data
    @html App.view('link/info')(
      links: list
    )
    @ticketPopups('left')

  delete: (e) =>
    e.preventDefault()
    link_type   = $(e.currentTarget).data('link-type')
    link_object_source = $(e.currentTarget).data('object')
    link_object_source_value = $(e.currentTarget).data('object-id')
    link_object_target = @object_type
    link_object_target_value = @object.id

    # get data
    @ajax(
      id:   "links_remove_#{@object.id}_#{@object_type}"
      type: 'GET'
      url:  "#{@apiPath}/links/remove"
      data:
        link_type:                link_type
        link_object_source:       link_object_source
        link_object_source_value: link_object_source_value
        link_object_target:       link_object_target
        link_object_target_value: link_object_target_value
      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)
        @ticket_ids_by_customer    = data.ticket_ids_by_customer
        @ticket_ids_recent_viewed  = data.ticket_ids_recent_viewed
        @render()
    )

  content: =>
    content = $( App.view('link/add')(
      link_object:    @link_object
      link_object_id: @link_object_id
      object:         @object
    ))

    list = []
    for ticket_id in @ticket_ids_by_customer
      if ticket_id isnt @ticket.id
        ticketItem = App.Ticket.fullLocal(ticket_id)
        list.push ticketItem
    new App.ControllerTable(
      el:       content.find('#ticket-merge-customer-tickets')
      overview: ['number', 'title', 'state', 'group', 'created_at']
      model:    App.Ticket
      objects:  list
      radio:    true
    )

    list = []
    for ticket_id in @ticket_ids_recent_viewed
      if ticket_id isnt @ticket.id
        ticketItem = App.Ticket.fullLocal( ticket_id )
        list.push ticketItem
    new App.ControllerTable(
      el:       content.find('#ticket-merge-recent-tickets')
      overview: ['number', 'title', 'state', 'group', 'created_at']
      model:    App.Ticket
      objects:  list
      radio:    true
    )

    content.delegate('[name="ticket_number"]', 'focus', (e) ->
      $(e.target).parents().find('[name="radio"]').prop('checked', false)
    )<commit_msg>Fixed issue #907 - “Add link”-screen is not really useful.<commit_after>class App.WidgetLink extends App.Controller
  events:
    'click .js-add': 'add'
    'click .js-delete': 'delete'
    'click .js-delete .icon': 'delete'

  constructor: ->
    super

    # if links are given, do not init fetch
    if @links
      @localLinks = _.clone(@links)
      @render()
      return

    @fetch()

  fetch: =>
    # fetch item on demand
    # get data
    @ajax(
      id:   "links_#{@object.id}_#{@object_type}"
      type: 'GET'
      url:  "#{@apiPath}/links"
      data:
        link_object:       @object_type
        link_object_value: @object.id
      processData: true
      success: (data, status, xhr) =>
        @localLinks = data.links
        App.Collection.loadAssets(data.assets)
        @render()
    )

  reload: (links) =>
    @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'] ] = []

      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'] ].push ticket

    # insert data
    @html App.view('link/info')(
      links: list
    )
    @ticketPopups('left')

  delete: (e) =>
    e.preventDefault()
    link_type   = $(e.currentTarget).data('link-type')
    link_object_source = $(e.currentTarget).data('object')
    link_object_source_value = $(e.currentTarget).data('object-id')
    link_object_target = @object_type
    link_object_target_value = @object.id

    # get data
    @ajax(
      id:   "links_remove_#{@object.id}_#{@object_type}"
      type: 'GET'
      url:  "#{@apiPath}/links/remove"
      data:
        link_type:                link_type
        link_object_source:       link_object_source
        link_object_source_value: link_object_source_value
        link_object_target:       link_object_target
        link_object_target_value: link_object_target_value
      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)
    )
{'fraction_non_alphanumeric': 0.12573768517403347, 'fraction_numerical': 0.0003613151872817054, 'mean_word_length': 2.399099467867376, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '253495', 'n_tokens_mistral': 2731, 'n_tokens_neox': 2656, 'n_words': 539}