summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/namespace_select.js
blob: 960c121e5fe9ae9c7f87db13533b7baeff231b03 (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
var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };

this.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.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.path;
        }
      },
      renderRow: this.renderRow,
      clicked: this.onSelectItem
    });
  }

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

  return NamespaceSelect;

})();

this.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 NamespaceSelect({
        dropdown: $dropdown
      });
    });
  }

  return NamespaceSelects;

})();