summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/namespace_select.js
blob: b98e6121967a7336281f840ca7cf7b4f6d846882 (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
/* eslint-disable func-names, space-before-function-paren, no-var, prefer-rest-params, wrap-iife, one-var, vars-on-top, one-var-declaration-per-line, comma-dangle, object-shorthand, no-else-return, prefer-template, quotes, prefer-arrow-callback, no-param-reassign, no-cond-assign, max-len */
/* global Api */

(function() {
  var bind = function(fn, me) { return function() { return fn.apply(me, arguments); }; };

  window.NamespaceSelect = (function() {
    function NamespaceSelect(opts) {
      this.onSelectItem = bind(this.onSelectItem, this);
      var fieldName, showAny;
      this.dropdown = opts.dropdown;
      showAny = true;
      fieldName = 'namespace_id';
      if (this.dropdown.attr('data-field-name')) {
        fieldName = this.dropdown.data('fieldName');
      }
      if (this.dropdown.attr('data-show-any')) {
        showAny = this.dropdown.data('showAny');
      }
      this.dropdown.glDropdown({
        filterable: true,
        selectable: true,
        filterRemote: true,
        search: {
          fields: ['path']
        },
        fieldName: fieldName,
        toggleLabel: function(selected) {
          if (selected.id == null) {
            return selected.text;
          } else {
            return selected.kind + ": " + selected.full_path;
          }
        },
        data: function(term, dataCallback) {
          return Api.namespaces(term, function(namespaces) {
            var anyNamespace;
            if (showAny) {
              anyNamespace = {
                text: 'Any namespace',
                id: null
              };
              namespaces.unshift(anyNamespace);
              namespaces.splice(1, 0, 'divider');
            }
            return dataCallback(namespaces);
          });
        },
        text: function(namespace) {
          if (namespace.id == null) {
            return namespace.text;
          } else {
            return namespace.kind + ": " + namespace.full_path;
          }
        },
        renderRow: this.renderRow,
        clicked: this.onSelectItem
      });
    }

    NamespaceSelect.prototype.onSelectItem = function(item, el, e) {
      return e.preventDefault();
    };

    return NamespaceSelect;
  })();

  window.NamespaceSelects = (function() {
    function NamespaceSelects(opts) {
      var ref;
      if (opts == null) {
        opts = {};
      }
      this.$dropdowns = (ref = opts.$dropdowns) != null ? ref : $('.js-namespace-select');
      this.$dropdowns.each(function(i, dropdown) {
        var $dropdown;
        $dropdown = $(dropdown);
        return new window.NamespaceSelect({
          dropdown: $dropdown
        });
      });
    }

    return NamespaceSelects;
  })();
}).call(window);