BigW Consortium Gitlab

groups_select.js 4.17 KB
Newer Older
1 2 3 4 5
/* eslint-disable func-names, space-before-function-paren, no-var, wrap-iife, one-var,
                  camelcase, one-var-declaration-per-line, quotes, object-shorthand,
                  prefer-arrow-callback, comma-dangle, consistent-return, yoda,
                  prefer-rest-params, prefer-spread, no-unused-vars, prefer-template,
                  promise/catch-or-return */
6
import Api from './api';
7

8
var slice = [].slice;
Fatih Acet committed
9

10 11 12
window.GroupsSelect = (function() {
  function GroupsSelect() {
    $('.ajax-groups-select').each((function(_this) {
13 14
      const self = _this;

15 16
      return function(i, select) {
        var all_available, skip_groups;
17 18 19 20 21
        const $select = $(select);
        all_available = $select.data('all-available');
        skip_groups = $select.data('skip-groups') || [];

        $select.select2({
22
          placeholder: "Search for a group",
23
          multiple: $select.hasClass('multiselect'),
24
          minimumInputLength: 0,
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
          ajax: {
            url: Api.buildUrl(Api.groupsPath),
            dataType: 'json',
            quietMillis: 250,
            transport: function (params) {
              $.ajax(params).then((data, status, xhr) => {
                const results = data || [];

                const headers = gl.utils.normalizeCRLFHeaders(xhr.getAllResponseHeaders());
                const currentPage = parseInt(headers['X-PAGE'], 10) || 0;
                const totalPages = parseInt(headers['X-TOTAL-PAGES'], 10) || 0;
                const more = currentPage < totalPages;

                return {
                  results,
                  pagination: {
                    more,
                  },
                };
              }).then(params.success).fail(params.error);
            },
            data: function (search, page) {
              return {
                search,
                page,
                per_page: GroupsSelect.PER_PAGE,
                all_available,
              };
            },
            results: function (data, page) {
              if (data.length) return { results: [] };

57
              const groups = data.length ? data : data.results || [];
58
              const more = data.pagination ? data.pagination.more : false;
59
              const results = groups.filter(group => skip_groups.indexOf(group.id) === -1);
60 61 62 63 64

              return {
                results,
                page,
                more,
65
              };
66
            },
67 68 69 70 71 72
          },
          initSelection: function(element, callback) {
            var id;
            id = $(element).val();
            if (id !== "") {
              return Api.group(id, callback);
Fatih Acet committed
73
            }
74 75 76 77
          },
          formatResult: function() {
            var args;
            args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
78
            return self.formatResult.apply(self, args);
79 80 81 82
          },
          formatSelection: function() {
            var args;
            args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
83
            return self.formatSelection.apply(self, args);
84
          },
85
          dropdownCssClass: "ajax-groups-dropdown select2-infinite",
86 87 88 89 90
          // we do not want to escape markup since we are displaying html in results
          escapeMarkup: function(m) {
            return m;
          }
        });
91 92 93 94

        self.dropdown = document.querySelector('.select2-infinite .select2-results');

        $select.on('select2-loaded', self.forceOverflow.bind(self));
95 96 97
      };
    })(this));
  }
Fatih Acet committed
98

99 100 101 102 103 104 105 106 107
  GroupsSelect.prototype.formatResult = function(group) {
    var avatar;
    if (group.avatar_url) {
      avatar = group.avatar_url;
    } else {
      avatar = gon.default_avatar_url;
    }
    return "<div class='group-result'> <div class='group-name'>" + group.full_name + "</div> <div class='group-path'>" + group.full_path + "</div> </div>";
  };
Fatih Acet committed
108

109 110 111
  GroupsSelect.prototype.formatSelection = function(group) {
    return group.full_name;
  };
Fatih Acet committed
112

113 114 115 116 117 118 119
  GroupsSelect.prototype.forceOverflow = function (e) {
    const itemHeight = this.dropdown.querySelector('.select2-result:first-child').clientHeight;
    this.dropdown.style.height = `${Math.floor(this.dropdown.scrollHeight - (itemHeight * 0.9))}px`;
  };

  GroupsSelect.PER_PAGE = 20;

120 121
  return GroupsSelect;
})();