summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/filtered_search/dropdown_assignee.js.es6
blob: b2b03b637e7d39cce65cf049a0c15f31f5c09359 (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
/* eslint-disable no-param-reassign */
/*= require filtered_search/filtered_search_dropdown */

((global) => {
  class DropdownAssignee extends gl.FilteredSearchDropdown {
    constructor(droplab, dropdown, input) {
      super(droplab, dropdown, input);
      this.listId = 'js-dropdown-assignee';
      this.config = {
        droplabAjaxFilter: {
          endpoint: '/autocomplete/users.json',
          searchKey: 'search',
          params: {
            per_page: 20,
            active: true,
            project_id: 2,
            current_user: true,
          },
          searchValueFunction: this.getSearchInput,
        }
      };
    }

    itemClicked(e) {
      const dataValueSet = this.setDataValueIfSelected(e.detail.selected);

      if (!dataValueSet) {
        const username = e.detail.selected.querySelector('.dropdown-light-content').innerText.trim();
        gl.FilteredSearchManager.addWordToInput(this.getSelectedText(username));
      }

      this.dismissDropdown();
    }

    renderContent() {
      // TODO: Pass elements instead of querySelectors
      this.droplab.changeHookList(this.hookId, this.dropdown, [droplabAjaxFilter], this.config);
    }

    getSearchInput() {
      const query = document.querySelector('.filtered-search').value;
      const { value } = gl.FilteredSearchTokenizer.getLastTokenObject(query);
      const valueWithoutColon = value.slice(1);
      const hasPrefix = valueWithoutColon[0] === '@';
      const valueWithoutPrefix = valueWithoutColon.slice(1);

      if (hasPrefix) {
        return valueWithoutPrefix;
      } else {
        return valueWithoutColon;
      }
    }

    configure() {
      this.droplab.addHook(this.input, this.dropdown, [droplabAjaxFilter], this.config).init();
    }
  }

  global.DropdownAssignee = DropdownAssignee;
})(window.gl || (window.gl = {}));