BigW Consortium Gitlab

project_select.js.coffee 2.12 KB
Newer Older
1 2
class @ProjectSelect
  constructor: ->
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
    $('.js-projects-dropdown-toggle').each (i, dropdown) ->
      $dropdown = $(dropdown)

      $dropdown.glDropdown(
        filterable: true
        filterRemote: true
        search:
          fields: ['name_with_namespace']
        data: (term, callback) ->
          finalCallback = (projects) ->
            callback projects

          if @includeGroups
            projectsCallback = (projects) ->
              groupsCallback = (groups) ->
                data = groups.concat(projects)
                finalCallback(data)

              Api.groups term, false, groupsCallback
          else
            projectsCallback = finalCallback

          if @groupId
            Api.groupProjects @groupId, term, projectsCallback
          else
            Api.projects term, @orderBy, projectsCallback
        url: (project) ->
          project.web_url
        text: (project) ->
          project.name_with_namespace
      )

35 36
    $('.ajax-project-select').each (i, select) ->
      @groupId = $(select).data('group-id')
37
      @includeGroups = $(select).data('include-groups')
38
      @orderBy = $(select).data('order-by') || 'id'
39 40 41

      placeholder = "Search for project"
      placeholder += " or group" if @includeGroups
42 43

      $(select).select2
44
        placeholder: placeholder
45 46
        minimumInputLength: 0
        query: (query) =>
47
          finalCallback = (projects) ->
48 49 50
            data = { results: projects }
            query.callback(data)

51 52 53 54 55 56 57 58 59 60
          if @includeGroups
            projectsCallback = (projects) ->
              groupsCallback = (groups) ->
                data = groups.concat(projects)
                finalCallback(data)

              Api.groups query.term, false, groupsCallback
          else
            projectsCallback = finalCallback

61
          if @groupId
62
            Api.groupProjects @groupId, query.term, projectsCallback
63
          else
64
            Api.projects query.term, @orderBy, projectsCallback
65 66 67 68 69

        id: (project) ->
          project.web_url

        text: (project) ->
70
          project.name_with_namespace || project.name
71 72

        dropdownCssClass: "ajax-project-dropdown"