filtered_search_manager.js.es6 5.8 KB
Newer Older
C
Clement Ho 已提交
1
/* eslint-disable no-param-reassign */
C
Clement Ho 已提交
2 3 4
((global) => {
  class FilteredSearchManager {
    constructor() {
C
Clement Ho 已提交
5
      this.tokenizer = gl.FilteredSearchTokenizer;
C
Clement Ho 已提交
6 7
      this.filteredSearchInput = document.querySelector('.filtered-search');
      this.clearSearchButton = document.querySelector('.clear-search');
C
Clement Ho 已提交
8
      this.dropdownManager = new gl.FilteredSearchDropdownManager();
C
Clement Ho 已提交
9

C
Clement Ho 已提交
10
      this.bindEvents();
C
Clement Ho 已提交
11
      this.loadSearchParamsFromURL();
C
Clement Ho 已提交
12
      this.dropdownManager.setDropdown();
13

C
Clement Ho 已提交
14 15
      this.cleanupWrapper = this.cleanup.bind(this);
      document.addEventListener('page:fetch', this.cleanupWrapper);
16 17 18
    }

    cleanup() {
19
      this.unbindEvents();
C
Clement Ho 已提交
20 21
      document.removeEventListener('page:fetch', this.cleanupWrapper);
    }
22

C
Clement Ho 已提交
23
    bindEvents() {
C
Clement Ho 已提交
24
      this.setDropdownWrapper = this.dropdownManager.setDropdown.bind(this.dropdownManager);
C
Clement Ho 已提交
25
      this.toggleClearSearchButtonWrapper = this.toggleClearSearchButton.bind(this);
26 27 28 29 30
      this.checkForEnterWrapper = this.checkForEnter.bind(this);
      this.clearSearchWrapper = this.clearSearch.bind(this);
      this.checkForBackspaceWrapper = this.checkForBackspace.bind(this);

      this.filteredSearchInput.addEventListener('input', this.setDropdownWrapper);
C
Clement Ho 已提交
31
      this.filteredSearchInput.addEventListener('input', this.toggleClearSearchButtonWrapper);
32 33 34 35 36 37 38
      this.filteredSearchInput.addEventListener('keydown', this.checkForEnterWrapper);
      this.filteredSearchInput.addEventListener('keyup', this.checkForBackspaceWrapper);
      this.clearSearchButton.addEventListener('click', this.clearSearchWrapper);
    }

    unbindEvents() {
      this.filteredSearchInput.removeEventListener('input', this.setDropdownWrapper);
C
Clement Ho 已提交
39
      this.filteredSearchInput.removeEventListener('input', this.toggleClearSearchButtonWrapper);
40 41 42
      this.filteredSearchInput.removeEventListener('keydown', this.checkForEnterWrapper);
      this.filteredSearchInput.removeEventListener('keyup', this.checkForBackspaceWrapper);
      this.clearSearchButton.removeEventListener('click', this.clearSearchWrapper);
C
Clement Ho 已提交
43 44
    }

45
    checkForBackspace(e) {
C
Clement Ho 已提交
46 47 48
      // 8 = Backspace Key
      // 46 = Delete Key
      if (e.keyCode === 8 || e.keyCode === 46) {
49
        // Reposition dropdown so that it is aligned with cursor
C
Clement Ho 已提交
50
        this.dropdownManager.updateCurrentDropdownOffset();
51 52 53
      }
    }

C
Clement Ho 已提交
54 55 56
    checkForEnter(e) {
      if (e.keyCode === 13) {
        e.preventDefault();
57 58

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

C
Clement Ho 已提交
61 62 63 64
        this.search();
      }
    }

C
Clement Ho 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    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 已提交
83
      const params = gl.utils.getUrlParamsArray();
84
      let inputValues = [];
C
Clement Ho 已提交
85 86 87

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

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

94 95
        if (condition) {
          inputValues.push(`${condition.tokenKey}:${condition.value}`);
C
Clement Ho 已提交
96 97 98 99
        } 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;
100
          const match = gl.FilteredSearchTokenKeys.searchByKeyParam(keyParam);
C
Clement Ho 已提交
101 102

          if (match) {
103
            const sanitizedKey = keyParam.slice(0, keyParam.indexOf('_'));
C
Clement Ho 已提交
104
            const symbol = match.symbol;
105
            let quotationsToUse = '';
C
Clement Ho 已提交
106

107
            if (sanitizedValue.indexOf(' ') !== -1) {
C
Clement Ho 已提交
108 109 110 111
              // Prefer ", but use ' if required
              quotationsToUse = sanitizedValue.indexOf('"') === -1 ? '"' : '\'';
            }

112 113
            inputValues.push(`${sanitizedKey}:${symbol}${quotationsToUse}${sanitizedValue}${quotationsToUse}`);
          } else if (!match && keyParam === 'search') {
114
            inputValues.push(sanitizedValue);
C
Clement Ho 已提交
115 116 117 118 119
          }
        }
      });

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

122
      if (inputValues.length > 0) {
C
Clement Ho 已提交
123 124 125 126
        this.clearSearchButton.classList.remove('hidden');
      }
    }

C
Clement Ho 已提交
127
    search() {
128
      let paths = [];
C
Clement Ho 已提交
129
      const { tokens, searchToken } = this.tokenizer.processTokens(this.filteredSearchInput.value);
C
Clement Ho 已提交
130
      const currentState = gl.utils.getParameterByName('state') || 'opened';
131
      paths.push(`state=${currentState}`);
C
Clement Ho 已提交
132

C
Clement Ho 已提交
133
      tokens.forEach((token) => {
134 135
        const condition = gl.FilteredSearchTokenKeys.searchByConditionKeyValue(token.key, token.value.toLowerCase());
        const { param } = gl.FilteredSearchTokenKeys.searchByKey(token.key);
C
Clement Ho 已提交
136 137
        let tokenPath = '';

138 139
        if (token.wildcard && condition) {
          tokenPath = condition.url;
140 141
        } else if (token.wildcard) {
          // wildcard means that the token does not have a symbol
142
          tokenPath = `${token.key}_${param}=${encodeURIComponent(token.value)}`;
143 144 145
        } else {
          // Remove the token symbol
          tokenPath = `${token.key}_${param}=${encodeURIComponent(token.value.slice(1))}`;
C
Clement Ho 已提交
146 147
        }

148
        paths.push(tokenPath);
C
Clement Ho 已提交
149 150
      });

C
Clement Ho 已提交
151
      if (searchToken) {
152
        paths.push(`search=${encodeURIComponent(searchToken)}`);
C
Clement Ho 已提交
153 154
      }

155
      Turbolinks.visit(`?scope=all&utf8=✓&${paths.join('&')}`);
C
Clement Ho 已提交
156 157 158 159
    }
  }

  global.FilteredSearchManager = FilteredSearchManager;
C
Clement Ho 已提交
160
})(window.gl || (window.gl = {}));