filtered_search_manager.js.es6 8.3 KB
Newer Older
C
Clement Ho 已提交
1
(() => {
C
Clement Ho 已提交
2 3
  class FilteredSearchManager {
    constructor() {
C
Clement Ho 已提交
4 5 6
      this.filteredSearchInput = document.querySelector('.filtered-search');
      this.clearSearchButton = document.querySelector('.clear-search');

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

11 12 13 14 15
        this.bindEvents();
        this.loadSearchParamsFromURL();
        this.dropdownManager.setDropdown();

        this.cleanupWrapper = this.cleanup.bind(this);
B
Bryce Johnson 已提交
16
        document.addEventListener('beforeunload', this.cleanupWrapper);
17
      }
18 19 20
    }

    cleanup() {
21
      this.unbindEvents();
B
Bryce Johnson 已提交
22
      document.removeEventListener('beforeunload', this.cleanupWrapper);
C
Clement Ho 已提交
23
    }
24

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

34
      this.filteredSearchInput.form.addEventListener('submit', this.handleFormSubmit);
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
      this.clearSearchButton.addEventListener('click', this.clearSearchWrapper);
    }

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

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

C
Clement Ho 已提交
64
    checkForEnter(e) {
65 66 67 68 69 70 71
      if (e.keyCode === 38 || e.keyCode === 40) {
        const selectionStart = this.filteredSearchInput.selectionStart;

        e.preventDefault();
        this.filteredSearchInput.setSelectionRange(selectionStart, selectionStart);
      }

C
Clement Ho 已提交
72
      if (e.keyCode === 13) {
73 74 75 76
        const dropdown = this.dropdownManager.mapping[this.dropdownManager.currentDropdown];
        const dropdownEl = dropdown.element;
        const activeElements = dropdownEl.querySelectorAll('.dropdown-active');

C
Clement Ho 已提交
77
        e.preventDefault();
78

79 80 81
        if (!activeElements.length) {
          // Prevent droplab from opening dropdown
          this.dropdownManager.destroyDroplab();
82

83 84
          this.search();
        }
C
Clement Ho 已提交
85 86 87
      }
    }

C
Clement Ho 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    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();
    }

105 106 107 108 109
    handleFormSubmit(e) {
      e.preventDefault();
      this.search();
    }

C
Clement Ho 已提交
110
    loadSearchParamsFromURL() {
C
Clement Ho 已提交
111
      const params = gl.utils.getUrlParamsArray();
112
      const usernameParams = this.getUsernameParams();
C
Clement Ho 已提交
113
      const inputValues = [];
C
Clement Ho 已提交
114 115 116

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

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

123 124
        if (condition) {
          inputValues.push(`${condition.tokenKey}:${condition.value}`);
C
Clement Ho 已提交
125 126 127 128
        } 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;
129
          const match = gl.FilteredSearchTokenKeys.searchByKeyParam(keyParam);
C
Clement Ho 已提交
130 131

          if (match) {
132 133
            const indexOf = keyParam.indexOf('_');
            const sanitizedKey = indexOf !== -1 ? keyParam.slice(0, keyParam.indexOf('_')) : keyParam;
C
Clement Ho 已提交
134
            const symbol = match.symbol;
135
            let quotationsToUse = '';
C
Clement Ho 已提交
136

137
            if (sanitizedValue.indexOf(' ') !== -1) {
C
Clement Ho 已提交
138 139 140 141
              // Prefer ", but use ' if required
              quotationsToUse = sanitizedValue.indexOf('"') === -1 ? '"' : '\'';
            }

142
            inputValues.push(`${sanitizedKey}:${symbol}${quotationsToUse}${sanitizedValue}${quotationsToUse}`);
143 144 145 146 147 148 149 150 151 152
          } 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]}`);
            }
153
          } else if (!match && keyParam === 'search') {
154
            inputValues.push(sanitizedValue);
C
Clement Ho 已提交
155 156 157 158 159
          }
        }
      });

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

162
      if (inputValues.length > 0) {
C
Clement Ho 已提交
163 164 165 166
        this.clearSearchButton.classList.remove('hidden');
      }
    }

C
Clement Ho 已提交
167
    search() {
C
Clement Ho 已提交
168
      const paths = [];
C
Clement Ho 已提交
169
      const { tokens, searchToken } = this.tokenizer.processTokens(this.filteredSearchInput.value);
C
Clement Ho 已提交
170
      const currentState = gl.utils.getParameterByName('state') || 'opened';
171
      paths.push(`state=${currentState}`);
C
Clement Ho 已提交
172

C
Clement Ho 已提交
173
      tokens.forEach((token) => {
C
Clement Ho 已提交
174 175
        const condition = gl.FilteredSearchTokenKeys
          .searchByConditionKeyValue(token.key, token.value.toLowerCase());
176
        const { param } = gl.FilteredSearchTokenKeys.searchByKey(token.key);
177
        const keyParam = param ? `${token.key}_${param}` : token.key;
C
Clement Ho 已提交
178 179
        let tokenPath = '';

180
        if (condition) {
181
          tokenPath = condition.url;
182
        } else {
C
Clement Ho 已提交
183 184 185 186 187 188 189 190
          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 已提交
191 192
        }

193
        paths.push(tokenPath);
C
Clement Ho 已提交
194 195
      });

C
Clement Ho 已提交
196
      if (searchToken) {
197 198
        const sanitized = searchToken.split(' ').map(t => encodeURIComponent(t)).join('+');
        paths.push(`search=${sanitized}`);
C
Clement Ho 已提交
199 200
      }

B
Bryce Johnson 已提交
201 202 203
      const parameterizedUrl = `?scope=all&utf8=✓&${paths.join('&')}`;

      gl.utils.visitUrl(parameterizedUrl);
C
Clement Ho 已提交
204
    }
205 206 207 208 209 210 211 212 213 214 215 216 217

    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 已提交
218

P
Phil Hughes 已提交
219
    tokenChange() {
220 221
      const dropdown = this.dropdownManager.mapping[this.dropdownManager.currentDropdown];
      const currentDropdownRef = dropdown.reference;
P
Phil Hughes 已提交
222 223 224 225

      this.setDropdownWrapper();
      currentDropdownRef.dispatchInputEvent();
    }
C
Clement Ho 已提交
226 227
  }

C
Clement Ho 已提交
228 229 230
  window.gl = window.gl || {};
  gl.FilteredSearchManager = FilteredSearchManager;
})();