filtered_search_manager.js.es6 7.4 KB
Newer Older
C
Clement Ho 已提交
1 2
/* global Turbolinks */

C
Clement Ho 已提交
3
(() => {
C
Clement Ho 已提交
4 5
  class FilteredSearchManager {
    constructor() {
C
Clement Ho 已提交
6 7 8
      this.filteredSearchInput = document.querySelector('.filtered-search');
      this.clearSearchButton = document.querySelector('.clear-search');

9 10 11
      if (this.filteredSearchInput) {
        this.tokenizer = gl.FilteredSearchTokenizer;
        this.dropdownManager = new gl.FilteredSearchDropdownManager();
12

13 14 15 16 17 18 19
        this.bindEvents();
        this.loadSearchParamsFromURL();
        this.dropdownManager.setDropdown();

        this.cleanupWrapper = this.cleanup.bind(this);
        document.addEventListener('page:fetch', this.cleanupWrapper);
      }
20 21 22
    }

    cleanup() {
23
      this.unbindEvents();
C
Clement Ho 已提交
24 25
      document.removeEventListener('page:fetch', this.cleanupWrapper);
    }
26

C
Clement Ho 已提交
27
    bindEvents() {
C
Clement Ho 已提交
28
      this.setDropdownWrapper = this.dropdownManager.setDropdown.bind(this.dropdownManager);
C
Clement Ho 已提交
29
      this.toggleClearSearchButtonWrapper = this.toggleClearSearchButton.bind(this);
30 31 32
      this.checkForEnterWrapper = this.checkForEnter.bind(this);
      this.clearSearchWrapper = this.clearSearch.bind(this);
      this.checkForBackspaceWrapper = this.checkForBackspace.bind(this);
P
Phil Hughes 已提交
33
      this.tokenChange = this.tokenChange.bind(this);
34 35

      this.filteredSearchInput.addEventListener('input', this.setDropdownWrapper);
C
Clement Ho 已提交
36
      this.filteredSearchInput.addEventListener('input', this.toggleClearSearchButtonWrapper);
37 38
      this.filteredSearchInput.addEventListener('keydown', this.checkForEnterWrapper);
      this.filteredSearchInput.addEventListener('keyup', this.checkForBackspaceWrapper);
P
Phil Hughes 已提交
39 40
      this.filteredSearchInput.addEventListener('click', this.tokenChange);
      this.filteredSearchInput.addEventListener('keyup', this.tokenChange);
41 42 43 44 45
      this.clearSearchButton.addEventListener('click', this.clearSearchWrapper);
    }

    unbindEvents() {
      this.filteredSearchInput.removeEventListener('input', this.setDropdownWrapper);
C
Clement Ho 已提交
46
      this.filteredSearchInput.removeEventListener('input', this.toggleClearSearchButtonWrapper);
47 48
      this.filteredSearchInput.removeEventListener('keydown', this.checkForEnterWrapper);
      this.filteredSearchInput.removeEventListener('keyup', this.checkForBackspaceWrapper);
P
Phil Hughes 已提交
49 50
      this.filteredSearchInput.removeEventListener('click', this.tokenChange);
      this.filteredSearchInput.removeEventListener('keyup', this.tokenChange);
51
      this.clearSearchButton.removeEventListener('click', this.clearSearchWrapper);
C
Clement Ho 已提交
52 53
    }

54
    checkForBackspace(e) {
C
Clement Ho 已提交
55 56 57
      // 8 = Backspace Key
      // 46 = Delete Key
      if (e.keyCode === 8 || e.keyCode === 46) {
58
        // Reposition dropdown so that it is aligned with cursor
C
Clement Ho 已提交
59
        this.dropdownManager.updateCurrentDropdownOffset();
60 61 62
      }
    }

C
Clement Ho 已提交
63 64 65
    checkForEnter(e) {
      if (e.keyCode === 13) {
        e.preventDefault();
66 67

        // Prevent droplab from opening dropdown
C
Clement Ho 已提交
68
        this.dropdownManager.destroyDroplab();
69

C
Clement Ho 已提交
70 71 72 73
        this.search();
      }
    }

C
Clement Ho 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    toggleClearSearchButton(e) {
      if (e.target.value) {
        this.clearSearchButton.classList.remove('hidden');
      } else {
        this.clearSearchButton.classList.add('hidden');
      }
    }

    clearSearch(e) {
      e.preventDefault();

      this.filteredSearchInput.value = '';
      this.clearSearchButton.classList.add('hidden');

      this.dropdownManager.resetDropdowns();
    }

    loadSearchParamsFromURL() {
C
Clement Ho 已提交
92
      const params = gl.utils.getUrlParamsArray();
93
      const usernameParams = this.getUsernameParams();
C
Clement Ho 已提交
94
      const inputValues = [];
C
Clement Ho 已提交
95 96 97

      params.forEach((p) => {
        const split = p.split('=');
98
        const keyParam = decodeURIComponent(split[0]);
C
Clement Ho 已提交
99 100
        const value = split[1];

101 102
        // Check if it matches edge conditions listed in gl.FilteredSearchTokenKeys
        const condition = gl.FilteredSearchTokenKeys.searchByConditionUrl(p);
C
Clement Ho 已提交
103

104 105
        if (condition) {
          inputValues.push(`${condition.tokenKey}:${condition.value}`);
C
Clement Ho 已提交
106 107 108 109
        } else {
          // Sanitize value since URL converts spaces into +
          // Replace before decode so that we know what was originally + versus the encoded +
          const sanitizedValue = value ? decodeURIComponent(value.replace(/\+/g, ' ')) : value;
110
          const match = gl.FilteredSearchTokenKeys.searchByKeyParam(keyParam);
C
Clement Ho 已提交
111 112

          if (match) {
113 114
            const indexOf = keyParam.indexOf('_');
            const sanitizedKey = indexOf !== -1 ? keyParam.slice(0, keyParam.indexOf('_')) : keyParam;
C
Clement Ho 已提交
115
            const symbol = match.symbol;
116
            let quotationsToUse = '';
C
Clement Ho 已提交
117

118
            if (sanitizedValue.indexOf(' ') !== -1) {
C
Clement Ho 已提交
119 120 121 122
              // Prefer ", but use ' if required
              quotationsToUse = sanitizedValue.indexOf('"') === -1 ? '"' : '\'';
            }

123
            inputValues.push(`${sanitizedKey}:${symbol}${quotationsToUse}${sanitizedValue}${quotationsToUse}`);
124 125 126 127 128 129 130 131 132 133
          } else if (!match && keyParam === 'assignee_id') {
            const id = parseInt(value, 10);
            if (usernameParams[id]) {
              inputValues.push(`assignee:@${usernameParams[id]}`);
            }
          } else if (!match && keyParam === 'author_id') {
            const id = parseInt(value, 10);
            if (usernameParams[id]) {
              inputValues.push(`author:@${usernameParams[id]}`);
            }
134
          } else if (!match && keyParam === 'search') {
135
            inputValues.push(sanitizedValue);
C
Clement Ho 已提交
136 137 138 139 140
          }
        }
      });

      // Trim the last space value
141
      this.filteredSearchInput.value = inputValues.join(' ');
C
Clement Ho 已提交
142

143
      if (inputValues.length > 0) {
C
Clement Ho 已提交
144 145 146 147
        this.clearSearchButton.classList.remove('hidden');
      }
    }

C
Clement Ho 已提交
148
    search() {
C
Clement Ho 已提交
149
      const paths = [];
C
Clement Ho 已提交
150
      const { tokens, searchToken } = this.tokenizer.processTokens(this.filteredSearchInput.value);
C
Clement Ho 已提交
151
      const currentState = gl.utils.getParameterByName('state') || 'opened';
152
      paths.push(`state=${currentState}`);
C
Clement Ho 已提交
153

C
Clement Ho 已提交
154
      tokens.forEach((token) => {
C
Clement Ho 已提交
155 156
        const condition = gl.FilteredSearchTokenKeys
          .searchByConditionKeyValue(token.key, token.value.toLowerCase());
157
        const { param } = gl.FilteredSearchTokenKeys.searchByKey(token.key);
158
        const keyParam = param ? `${token.key}_${param}` : token.key;
C
Clement Ho 已提交
159 160
        let tokenPath = '';

161
        if (condition) {
162
          tokenPath = condition.url;
163
        } else {
C
Clement Ho 已提交
164 165 166 167 168 169 170 171
          let tokenValue = token.value;

          if ((tokenValue[0] === '\'' && tokenValue[tokenValue.length - 1] === '\'') ||
            (tokenValue[0] === '"' && tokenValue[tokenValue.length - 1] === '"')) {
            tokenValue = tokenValue.slice(1, tokenValue.length - 1);
          }

          tokenPath = `${keyParam}=${encodeURIComponent(tokenValue)}`;
C
Clement Ho 已提交
172 173
        }

174
        paths.push(tokenPath);
C
Clement Ho 已提交
175 176
      });

C
Clement Ho 已提交
177
      if (searchToken) {
178
        paths.push(`search=${encodeURIComponent(searchToken)}`);
C
Clement Ho 已提交
179 180
      }

181
      Turbolinks.visit(`?scope=all&utf8=✓&${paths.join('&')}`);
C
Clement Ho 已提交
182
    }
183 184 185 186 187 188 189 190 191 192 193 194 195

    getUsernameParams() {
      const usernamesById = {};
      try {
        const attribute = this.filteredSearchInput.getAttribute('data-username-params');
        JSON.parse(attribute).forEach((user) => {
          usernamesById[user.id] = user.username;
        });
      } catch (e) {
        // do nothing
      }
      return usernamesById;
    }
P
Phil Hughes 已提交
196

P
Phil Hughes 已提交
197
    tokenChange() {
198 199
      const dropdown = this.dropdownManager.mapping[this.dropdownManager.currentDropdown];
      const currentDropdownRef = dropdown.reference;
P
Phil Hughes 已提交
200 201 202 203

      this.setDropdownWrapper();
      currentDropdownRef.dispatchInputEvent();
    }
C
Clement Ho 已提交
204 205
  }

C
Clement Ho 已提交
206 207 208
  window.gl = window.gl || {};
  gl.FilteredSearchManager = FilteredSearchManager;
})();