search()

in app/assets/javascripts/filtered_search/filtered_search_manager.js [746:822]


  search(state = null) {
    const paths = [];
    const { tokens, searchToken } = this.getSearchTokens();
    let currentState = state || getParameterByName('state');
    if (!currentState && this.useDefaultState) {
      currentState = STATUS_OPEN;
    }
    if (this.states.includes(currentState)) {
      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.replacementUrl || 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}`);
    }

    let parameterizedUrl = `?scope=all&${paths.join('&')}`;

    if (this.anchor) {
      parameterizedUrl += `#${this.anchor}`;
    }

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