summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/project.js.coffee
blob: 96e10dd7e8ae34410f8d9f35949075cf9455f08a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
class @Project
  constructor: ->
    # Git protocol switcher
    $('ul.clone-options-dropdown a').click ->
      return if $(@).hasClass('active')


      # Remove the active class for all buttons (ssh, http, kerberos if shown)
      $('.active').not($(@)).removeClass('active');
      # Add the active class for the clicked button
      $(@).toggleClass('active')

      url = $("#project_clone").val()

      # Update the input field
      $('#project_clone').val(url)

      # Update the command line instructions
      $('.clone').text(url)

    # Ref switcher
    @initRefSwitcher()
    $('.project-refs-select').on 'change', ->
      $(@).parents('form').submit()

    $('.hide-no-ssh-message').on 'click', (e) ->
      path = '/'
      $.cookie('hide_no_ssh_message', 'false', { path: path })
      $(@).parents('.no-ssh-key-message').remove()
      e.preventDefault()

    $('.hide-no-password-message').on 'click', (e) ->
      path = '/'
      $.cookie('hide_no_password_message', 'false', { path: path })
      $(@).parents('.no-password-message').remove()
      e.preventDefault()

    @projectSelectDropdown()

  projectSelectDropdown: ->
    new ProjectSelect()

    $('.project-item-select').on 'click', (e) =>
      @changeProject $(e.currentTarget).val()

    $('.js-projects-dropdown-toggle').on 'click', (e) ->
      e.preventDefault()

      $('.js-projects-dropdown').select2('open')

  changeProject: (url) ->
    window.location = url

  initRefSwitcher: ->
    $('.js-project-refs-dropdown').each ->
      $dropdown = $(@)
      selected = $dropdown.data('selected')

      $dropdown.glDropdown(
        data: (term, callback) ->
          $.ajax(
            url: $dropdown.data('refs-url')
            data:
              ref: $dropdown.data('ref')
          ).done (refs) ->
            callback(refs)
        selectable: true
        filterable: true
        filterByText: true
        fieldName: 'ref'
        renderRow: (ref) ->
          if ref.header?
            "<li class='dropdown-header'>#{ref.header}</li>"
          else
            isActiveClass = if ref is selected then 'is-active' else ''

            "<li>
              <a href='#' data-ref='#{escape(ref)}' class='#{isActiveClass}'>
                #{ref}
              </a>
            </li>"
        id: (obj, $el) ->
          $el.data('ref')
        toggleLabel: (obj, $el) ->
          $el.text().trim()
        clicked: (e) ->
          $dropdown.closest('form').submit()
      )