BigW Consortium Gitlab

namespace_select.js.coffee 1.38 KB
Newer Older
1
class @NamespaceSelect
2 3 4 5 6 7 8 9
  constructor: (opts) ->
    {
      @dropdown
    } = opts

    showAny = true
    fieldName = 'namespace_id'

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

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

16 17 18
    @dropdown.glDropdown(
      filterable: true
      selectable: true
19
      filterRemote: true
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
      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
      )