summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/filtered_search/dropdown_non_user.js
blob: f20193eecba1b33e34fcaa0163259356324d3bc3 (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
/* global Flash */

import Ajax from '~/droplab/plugins/ajax';
import Filter from '~/droplab/plugins/filter';
import './filtered_search_dropdown';

class DropdownNonUser extends gl.FilteredSearchDropdown {
  constructor(droplab, dropdown, input, filter, endpoint, symbol) {
    super(droplab, dropdown, input, filter);
    this.symbol = symbol;
    this.config = {
      Ajax: {
        endpoint,
        method: 'setData',
        loadingTemplate: this.loadingTemplate,
        onError() {
          /* eslint-disable no-new */
          new Flash('An error occured fetching the dropdown data.');
          /* eslint-enable no-new */
        },
      },
      Filter: {
        filterFunction: gl.DropdownUtils.filterWithSymbol.bind(null, this.symbol, input),
        template: 'title',
      },
    };
  }

  itemClicked(e) {
    super.itemClicked(e, (selected) => {
      const title = selected.querySelector('.js-data-value').innerText.trim();
      return `${this.symbol}${gl.DropdownUtils.getEscapedText(title)}`;
    });
  }

  renderContent(forceShowList = false) {
    this.droplab
      .changeHookList(this.hookId, this.dropdown, [Ajax, Filter], this.config);
    super.renderContent(forceShowList);
  }

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

window.gl = window.gl || {};
gl.DropdownNonUser = DropdownNonUser;