loadSearchParamsFromURL()

in app/assets/javascripts/filtered_search/filtered_search_manager.js [485:565]


  loadSearchParamsFromURL() {
    const urlParams = getUrlParamsArray();
    const params = this.getAllParams(urlParams);
    const usernameParams = this.getUsernameParams();
    let hasFilteredSearch = false;

    params.forEach(p => {
      const split = p.split('=');
      const keyParam = decodeURIComponent(split[0]);
      const value = split[1];

      // Check if it matches edge conditions listed in this.filteredSearchTokenKeys
      const condition = this.filteredSearchTokenKeys.searchByConditionUrl(p);

      if (condition) {
        hasFilteredSearch = true;
        const canEdit = this.canEdit && this.canEdit(condition.tokenKey);
        FilteredSearchVisualTokens.addFilterVisualToken(condition.tokenKey, condition.value, {
          canEdit,
        });
      } 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;
        const match = this.filteredSearchTokenKeys.searchByKeyParam(keyParam);

        if (match) {
          const { key, symbol } = match;
          let quotationsToUse = '';

          if (sanitizedValue.indexOf(' ') !== -1) {
            // Prefer ", but use ' if required
            quotationsToUse = sanitizedValue.indexOf('"') === -1 ? '"' : "'";
          }

          hasFilteredSearch = true;
          const canEdit = this.canEdit && this.canEdit(key, sanitizedValue);
          const { uppercaseTokenName, capitalizeTokenValue } = match;
          FilteredSearchVisualTokens.addFilterVisualToken(
            key,
            `${symbol}${quotationsToUse}${sanitizedValue}${quotationsToUse}`,
            {
              canEdit,
              uppercaseTokenName,
              capitalizeTokenValue,
            },
          );
        } else if (!match && keyParam === 'assignee_id') {
          const id = parseInt(value, 10);
          if (usernameParams[id]) {
            hasFilteredSearch = true;
            const tokenName = 'assignee';
            const canEdit = this.canEdit && this.canEdit(tokenName);
            FilteredSearchVisualTokens.addFilterVisualToken(tokenName, `@${usernameParams[id]}`, {
              canEdit,
            });
          }
        } else if (!match && keyParam === 'author_id') {
          const id = parseInt(value, 10);
          if (usernameParams[id]) {
            hasFilteredSearch = true;
            const tokenName = 'author';
            const canEdit = this.canEdit && this.canEdit(tokenName);
            FilteredSearchVisualTokens.addFilterVisualToken(tokenName, `@${usernameParams[id]}`, {
              canEdit,
            });
          }
        } else if (!match && keyParam === 'search') {
          hasFilteredSearch = true;
          this.filteredSearchInput.value = sanitizedValue;
        }
      }
    });

    this.saveCurrentSearchQuery();

    if (hasFilteredSearch) {
      this.clearSearchButton.classList.remove('hidden');
      this.handleInputPlaceholder();
    }
  }