issuable.js.coffee 4.72 KB
Newer Older
1
issuable_created = false
2 3
@Issuable =
  init: ->
4
    unless issuable_created
5 6 7
      issuable_created = true
      Issuable.initTemplates()
      Issuable.initSearch()
Phil Hughes's avatar
Phil Hughes committed
8
      Issuable.initChecks()
9
      Issuable.initLabelFilterRemove()
10 11 12 13

  initTemplates: ->
    Issuable.labelRow = _.template(
      '<% _.each(labels, function(label){ %>
Phil Hughes's avatar
Phil Hughes committed
14 15 16
        <span class="label-row btn-group" role="group" aria-label="<%= _.escape(label.title) %>" style="color: <%= label.text_color %>;">
          <a href="#" class="btn btn-transparent has-tooltip" style="background-color: <%= label.color %>;" title="<%= _.escape(label.description) %>" data-container="body">
            <%= _.escape(label.title) %>
17
          </a>
Phil Hughes's avatar
Phil Hughes committed
18
          <button type="button" class="btn btn-transparent label-remove js-label-filter-remove" style="background-color: <%= label.color %>;" data-label="<%= _.escape(label.title) %>">
19 20
            <i class="fa fa-times"></i>
          </button>
21 22 23 24 25 26 27 28 29 30 31
        </span>
      <% }); %>'
    )

  initSearch: ->
    @timer = null
    $('#issue_search')
      .off 'keyup'
      .on 'keyup', ->
        clearTimeout(@timer)
        @timer = setTimeout( ->
32 33 34 35 36
          $search = $('#issue_search')
          $form = $('.js-filter-form')
          $input = $("input[name='#{$search.attr('name')}']", $form)

          if $input.length is 0
37
            $form.append "<input type='hidden' name='#{$search.attr('name')}' value='#{_.escape($search.val())}'/>"
38 39 40 41
          else
            $input.val $search.val()

          Issuable.filterResults $form
42 43
        , 500)

44 45 46 47 48 49 50 51 52 53 54 55 56
  initLabelFilterRemove: ->
    $(document)
      .off 'click', '.js-label-filter-remove'
      .on 'click', '.js-label-filter-remove', (e) ->
        $button = $(@)

        # Remove the label input box
        $('input[name="label_name[]"]')
          .filter -> @value is $button.data('label')
          .remove()

        # Submit the form to get new data
        Issuable.filterResults $('.filter-form')
57
        $('.js-label-select').trigger('update.label')
58

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  toggleLabelFilters: ->
    $filteredLabels = $('.filtered-labels')
    if $filteredLabels.find('.label-row').length > 0
      $filteredLabels.removeClass('hidden')
    else
      $filteredLabels.addClass('hidden')

  filterResults: (form) =>
    formData = form.serialize()

    $('.issues-holder, .merge-requests-holder').css('opacity', '0.5')
    formAction = form.attr('action')
    issuesUrl = formAction
    issuesUrl += ("#{if formAction.indexOf('?') < 0 then '?' else '&'}")
    issuesUrl += formData
    $.ajax
      type: 'GET'
      url: formAction
      data: formData
      complete: ->
        $('.issues-holder, .merge-requests-holder').css('opacity', '1.0')
      success: (data) ->
        $('.issues-holder, .merge-requests-holder').html(data.html)
        # Change url so if user reload a page - search results are saved
        history.replaceState {page: issuesUrl}, document.title, issuesUrl
        Issuable.reload()
        Issuable.updateStateFilters()
        $filteredLabels = $('.filtered-labels')

        if typeof Issuable.labelRow is 'function'
          $filteredLabels.html(Issuable.labelRow(data))

        Issuable.toggleLabelFilters()

      dataType: "json"

  reload: ->
Phil Hughes's avatar
Phil Hughes committed
96 97
    if Issuable.created
      Issuable.initChecks()
98 99 100

    $('#filter_issue_search').val($('#issue_search').val())

Phil Hughes's avatar
Phil Hughes committed
101 102 103 104 105 106
  initChecks: ->
    $('.check_all_issues').on 'click', ->
      $('.selected_issue').prop('checked', @checked)
      Issuable.checkChanged()

    $('.selected_issue').on 'change', Issuable.checkChanged
107

108
  updateStateFilters: ->
109
    stateFilters =  $('.issues-state-filters, .dropdown-menu-sort')
110
    newParams = {}
Phil Hughes's avatar
Phil Hughes committed
111
    paramKeys = ['author_id', 'milestone_title', 'assignee_id', 'issue_search', 'issue_search']
112 113 114 115 116 117 118 119 120 121 122 123 124 125

    for paramKey in paramKeys
      newParams[paramKey] = gl.utils.getParameterValues(paramKey)[0] or ''

    if stateFilters.length
      stateFilters.find('a').each ->
        initialUrl = gl.utils.removeParamQueryString($(this).attr('href'), 'label_name[]')
        labelNameValues = gl.utils.getParameterValues('label_name[]')
        if labelNameValues
          labelNameQueryString = ("label_name[]=#{value}" for value in labelNameValues).join('&')
          newUrl = "#{gl.utils.mergeUrlParams(newParams, initialUrl)}&#{labelNameQueryString}"
        else
          newUrl = gl.utils.mergeUrlParams(newParams, initialUrl)
        $(this).attr 'href', newUrl
126 127 128 129

  checkChanged: ->
    checked_issues = $('.selected_issue:checked')
    if checked_issues.length > 0
Phil Hughes's avatar
Phil Hughes committed
130 131
      ids = $.map checked_issues, (value) ->
        $(value).data('id')
132 133 134 135 136 137 138 139

      $('#update_issues_ids').val ids
      $('.issues-other-filters').hide()
      $('.issues_bulk_update').show()
    else
      $('#update_issues_ids').val []
      $('.issues_bulk_update').hide()
      $('.issues-other-filters').show()