<commit_before>issuable_created = false
@Issuable =
  init: ->
    unless issuable_created
      issuable_created = true
      Issuable.initTemplates()
      Issuable.initSearch()
      Issuable.initChecks()
      Issuable.initLabelFilterRemove()

  initTemplates: ->
    Issuable.labelRow = _.template(
      '<% _.each(labels, function(label){ %>
        <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) %>
          </a>
          <button type="button" class="btn btn-transparent label-remove js-label-filter-remove" style="background-color: <%= label.color %>;" data-label="<%= _.escape(label.title) %>">
            <i class="fa fa-times"></i>
          </button>
        </span>
      <% }); %>'
    )

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

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

          Issuable.filterResults $form
        , 500)

  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')
        $('.js-label-select').trigger('update.label')

  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: ->
    if Issuable.created
      Issuable.initChecks()

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

  initChecks: ->
    $('.check_all_issues').off('click').on('click', ->
      $('.selected_issue').prop('checked', @checked)
      Issuable.checkChanged()
    )

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

  updateStateFilters: ->
    stateFilters =  $('.issues-state-filters, .dropdown-menu-sort')
    newParams = {}
    paramKeys = ['author_id', 'milestone_title', 'assignee_id', 'issue_search', 'issue_search']

    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

  checkChanged: ->
    checked_issues = $('.selected_issue:checked')<commit_msg>Use Turbolink instead of ajax<commit_after>issuable_created = false
@Issuable =
  init: ->
    unless issuable_created
      issuable_created = true
      Issuable.initTemplates()
      Issuable.initSearch()
      Issuable.initChecks()
      Issuable.initLabelFilterRemove()

  initTemplates: ->
    Issuable.labelRow = _.template(
      '<% _.each(labels, function(label){ %>
        <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) %>
          </a>
          <button type="button" class="btn btn-transparent label-remove js-label-filter-remove" style="background-color: <%= label.color %>;" data-label="<%= _.escape(label.title) %>">
            <i class="fa fa-times"></i>
          </button>
        </span>
      <% }); %>'
    )

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

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

          Issuable.filterResults $form
        , 500)

  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')
        $('.js-label-select').trigger('update.label')

  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

    Turbolinks.visit(issuesUrl);

  initChecks: ->
    $('.check_all_issues').off('click').on('click', ->
      $('.selected_issue').prop('checked', @checked)
      Issuable.checkChanged()
    )

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

  checkChanged: ->
    checked_issues = $('.selected_issue:checked')
{'fraction_non_alphanumeric': 0.1813285457809695, 'fraction_numerical': 0.0024858444966164893, 'mean_word_length': 2.7465080186239006, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 70, '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': '4999292', 'n_tokens_mistral': 2249, 'n_tokens_neox': 2168, 'n_words': 397}