summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/namespace_select.js.coffee
blob: 3b419dff105f23fc794910e2511f0e659ee6f2d2 (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
class @NamespaceSelect
  constructor: (opts) ->
    {
      @dropdown
    } = opts

    showAny = true
    fieldName = 'namespace_id'

    if @dropdown.attr 'data-field-name'
     fieldName = @dropdown.data 'fieldName'

    if @dropdown.attr 'data-show-any'
      showAny = @dropdown.data 'showAny'

    @dropdown.glDropdown(
      filterable: true
      selectable: true
      filterRemote: true
      search:
        fields: ['path']
      fieldName: fieldName
      toggleLabel: (selected) ->
        return if not selected.id? then selected.text else "#{selected.kind}: #{selected.path}"
      data: (term, dataCallback) ->
        Api.namespaces term, (namespaces) ->
          if showAny
            anyNamespace =
              text: 'Any namespace'
              id: null

            namespaces.unshift(anyNamespace)
            namespaces.splice 1, 0, 'divider'

          dataCallback(namespaces)
      text: (namespace) ->
        return if not namespace.id? then namespace.text else "#{namespace.kind}: #{namespace.path}"
      renderRow: @renderRow
      clicked: @onSelectItem
    )

  onSelectItem: (item, el, e) =>
    e.preventDefault()

class @NamespaceSelects
  constructor: (opts = {}) ->
    {
      @$dropdowns = $('.js-namespace-select')
    } = opts

    @$dropdowns.each (i, dropdown) ->
      $dropdown = $(dropdown)

      new NamespaceSelect(
        dropdown: $dropdown
      )