filtered_search_manager.js.es6 7.3 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.showOnClick = this.showOnClick.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
      this.filteredSearchInput.addEventListener('click', this.showOnClick);
40 41 42 43 44
      this.clearSearchButton.addEventListener('click', this.clearSearchWrapper);
    }

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

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

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

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

C
Clement Ho 已提交
68 69 70 71
        this.search();
      }
    }

C
Clement Ho 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    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 已提交
90
      const params = gl.utils.getUrlParamsArray();
91
      const usernameParams = this.getUsernameParams();
C
Clement Ho 已提交
92
      const inputValues = [];
C
Clement Ho 已提交
93 94 95

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

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

102 103
        if (condition) {
          inputValues.push(`${condition.tokenKey}:${condition.value}`);
C
Clement Ho 已提交
104 105 106 107
        } 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;
108
          const match = gl.FilteredSearchTokenKeys.searchByKeyParam(keyParam);
C
Clement Ho 已提交
109 110

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

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

121
            inputValues.push(`${sanitizedKey}:${symbol}${quotationsToUse}${sanitizedValue}${quotationsToUse}`);
122 123 124 125 126 127 128 129 130 131
          } 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]}`);
            }
132
          } else if (!match && keyParam === 'search') {
133
            inputValues.push(sanitizedValue);
C
Clement Ho 已提交
134 135 136 137 138
          }
        }
      });

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

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

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

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

159
        if (condition) {
160
          tokenPath = condition.url;
161
        } else {
C
Clement Ho 已提交
162 163 164 165 166 167 168 169
          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 已提交
170 171
        }

172
        paths.push(tokenPath);
C
Clement Ho 已提交
173 174
      });

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

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

    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 已提交
194 195

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

      this.setDropdownWrapper();
      currentDropdownRef.dispatchInputEvent();
    }
C
Clement Ho 已提交
202 203
  }

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