BigW Consortium Gitlab

namespace_select.js 2.59 KB
Newer Older
1
/* 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 */
2
import Api from './api';
3

Fatih Acet committed
4
(function() {
5
  window.NamespaceSelect = (function() {
Fatih Acet committed
6
    function NamespaceSelect(opts) {
7
      this.onSelectItem = this.onSelectItem.bind(this);
Fatih Acet committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
      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 {
30
            return selected.kind + ": " + selected.full_path;
Fatih Acet committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
          }
        },
        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 {
51
            return namespace.kind + ": " + namespace.full_path;
Fatih Acet committed
52 53 54 55 56 57 58
          }
        },
        renderRow: this.renderRow,
        clicked: this.onSelectItem
      });
    }

59 60
    NamespaceSelect.prototype.onSelectItem = function(options) {
      const { e } = options;
Fatih Acet committed
61 62 63 64 65 66
      return e.preventDefault();
    };

    return NamespaceSelect;
  })();

67
  window.NamespaceSelects = (function() {
Fatih Acet committed
68 69 70 71 72 73 74 75 76
    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);
77
        return new window.NamespaceSelect({
Fatih Acet committed
78 79 80 81 82 83 84
          dropdown: $dropdown
        });
      });
    }

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