search()

in app/assets/javascripts/filtered_search/filtered_search_manager.js [694:765]


  search(state = null) {
    const paths = [];
    const searchQuery = DropdownUtils.getSearchQuery();
    this.saveCurrentSearchQuery();

    const tokenKeys = this.filteredSearchTokenKeys.getKeys();
    const { tokens, searchToken } = this.tokenizer.processTokens(searchQuery, tokenKeys);
    const currentState = state || getParameterByName('state') || 'opened';
    paths.push(`state=${currentState}`);

    tokens.forEach(token => {
      const condition = this.filteredSearchTokenKeys.searchByConditionKeyValue(
        token.key,
        token.operator,
        token.value,
      );
      const tokenConfig = this.filteredSearchTokenKeys.searchByKey(token.key) || {};
      const { param } = tokenConfig;

      // Replace hyphen with underscore to use as request parameter
      // e.g. 'my-reaction' => 'my_reaction'
      const underscoredKey = token.key.replace('-', '_');
      const keyParam = param ? `${underscoredKey}_${param}` : underscoredKey;
      let tokenPath = '';

      if (condition) {
        tokenPath = condition.url;
      } else {
        let tokenValue = token.value;

        if (tokenConfig.lowercaseValueOnSubmit) {
          tokenValue = tokenValue.toLowerCase();
        }

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

        if (token.operator === '!=') {
          const isArrayParam = keyParam.endsWith('[]');

          tokenPath = `not[${isArrayParam ? keyParam.slice(0, -2) : keyParam}]${
            isArrayParam ? '[]' : ''
          }=${encodeURIComponent(tokenValue)}`;
        } else {
          // Default operator is `=`
          tokenPath = `${keyParam}=${encodeURIComponent(tokenValue)}`;
        }
      }

      paths.push(tokenPath);
    });

    if (searchToken) {
      const sanitized = searchToken
        .split(' ')
        .map(t => encodeURIComponent(t))
        .join('+');
      paths.push(`search=${sanitized}`);
    }

    const parameterizedUrl = `?scope=all&utf8=%E2%9C%93&${paths.join('&')}`;

    if (this.updateObject) {
      this.updateObject(parameterizedUrl);
    } else {
      visitUrl(parameterizedUrl);
    }
  }