A platform for community discussion. Free, open, simple.
UX: Replace advanced search filters with multi-select
zogstrip633f31e89984
diff --git a/config/locales/client.en.yml b/config/locales/client.en.ymlindex 91faa9772a7..3fbc57da20e 100644--- a/config/locales/client.en.yml+++ b/config/locales/client.en.yml@@ -3413,6 +3413,8 @@ en:aria_label: Filter using tagsfilters:label: Only return topics/posts…+ none: Select filters…+ all: Include messagestitle: Matching in title onlylikes: I likedposted: I posted indiff --git a/frontend/discourse/app/components/search-advanced-options.gjs b/frontend/discourse/app/components/search-advanced-options.gjsindex 404709af893..420be69e0ee 100644--- a/frontend/discourse/app/components/search-advanced-options.gjs+++ b/frontend/discourse/app/components/search-advanced-options.gjs@@ -17,6 +17,7 @@ import withEventValue from "discourse/helpers/with-event-value";import { escapeExpression } from "discourse/lib/utilities";import Category from "discourse/models/category";import ComboBox from "discourse/select-kit/components/combo-box";+import MultiSelect from "discourse/select-kit/components/multi-select";import SearchAdvancedCategoryChooser from "discourse/select-kit/components/search-advanced-category-chooser";import TagChooser from "discourse/select-kit/components/tag-chooser";import UserChooser from "discourse/select-kit/components/user-chooser";@@ -36,11 +37,6 @@ const REGEXP_MAX_VIEWS_PREFIX = /^max_views:/gi;const REGEXP_POST_TIME_PREFIX = /^(before|after):/gi;const REGEXP_TAGS_REPLACE = /(^(tags?:|#(?=[a-z0-9\-]+::tag))|::tag\s?$)/gi;-const REGEXP_SPECIAL_IN_LIKES_MATCH = /^in:likes$/gi;-const REGEXP_SPECIAL_IN_TITLE_MATCH = /^in:title$/gi;-const REGEXP_SPECIAL_IN_MESSAGES_MATCH = /^in:(personal|messages)$/gi;-const REGEXP_SPECIAL_IN_SEEN_MATCH = /^in:seen$/gi;-const REGEXP_CATEGORY_SLUG = /^(\#[a-zA-Z0-9\-:]+)/gi;const REGEXP_CATEGORY_ID = /^(category:[0-9]+)/gi;const REGEXP_POST_TIME_WHEN = /^(before|after)/gi;@@ -49,24 +45,50 @@ const IN_OPTIONS_MAPPING = { images: "with" };let _extraOptions = [];+function buildFilterOptions(keys, extraOptionsKey) {+ return keys+ .map((key) => ({+ name: i18n(`search.advanced.filters.${key}`),+ value: key === "private" ? "messages" : key,+ }))+ .concat(..._extraOptions.map((eo) => eo[extraOptionsKey]).filter(Boolean));+}+function inOptionsForUsers() {- return [- { name: i18n("search.advanced.filters.unseen"), value: "unseen" },- { name: i18n("search.advanced.filters.posted"), value: "posted" },- { name: i18n("search.advanced.filters.created"), value: "created" },- { name: i18n("search.advanced.filters.watching"), value: "watching" },- { name: i18n("search.advanced.filters.tracking"), value: "tracking" },- { name: i18n("search.advanced.filters.bookmarks"), value: "bookmarks" },- ].concat(..._extraOptions.map((eo) => eo.inOptionsForUsers).filter(Boolean));+ return buildFilterOptions(+ [+ // User actions+ "created",+ "posted",+ "likes",+ "bookmarks",+ // Read state+ "seen",+ "unseen",+ // Subscriptions+ "watching",+ "tracking",+ // Messages+ "all",+ "private",+ // Search scope+ "title",+ ],+ "inOptionsForUsers"+ );}function inOptionsForAll() {- return [- { name: i18n("search.advanced.filters.first"), value: "first" },- { name: i18n("search.advanced.filters.pinned"), value: "pinned" },- { name: i18n("search.advanced.filters.wiki"), value: "wiki" },- { name: i18n("search.advanced.filters.images"), value: "images" },- ].concat(..._extraOptions.map((eo) => eo.inOptionsForAll).filter(Boolean));+ return buildFilterOptions(+ [+ // Post properties+ "first",+ "pinned",+ "wiki",+ "images",+ ],+ "inOptionsForAll"+ );}function statusOptions() {@@ -111,14 +133,8 @@ export default class SearchAdvancedOptions extends Component {username: null,category: null,tags: null,- in: null,+ in: [],special: {- in: {- title: false,- likes: false,- messages: false,- seen: false,- },all_tags: false,},status: null,@@ -146,35 +162,7 @@ export default class SearchAdvancedOptions extends Component {this.setSearchedTermValue("searchedTerms.username", REGEXP_USERNAME_PREFIX);this.setSearchedTermValueForCategory();this.setSearchedTermValueForTags();-- let regExpInMatch = this.inOptions.map((option) => option.value).join("|");- const REGEXP_IN_MATCH = new RegExp(`(in|with):(${regExpInMatch})`, "i");-- this.setSearchedTermValue(- "searchedTerms.in",- REGEXP_IN_PREFIX,- REGEXP_IN_MATCH- );-- this.setSearchedTermSpecialInValue(- "searchedTerms.special.in.likes",- REGEXP_SPECIAL_IN_LIKES_MATCH- );-- this.setSearchedTermSpecialInValue(- "searchedTerms.special.in.title",- REGEXP_SPECIAL_IN_TITLE_MATCH- );-- this.setSearchedTermSpecialInValue(- "searchedTerms.special.in.messages",- REGEXP_SPECIAL_IN_MESSAGES_MATCH- );-- this.setSearchedTermSpecialInValue(- "searchedTerms.special.in.seen",- REGEXP_SPECIAL_IN_SEEN_MATCH- );+ this.setSearchedTermValueForIn();let regExpStatusMatch = this.statusOptions.map((status) => status.value)@@ -212,6 +200,32 @@ export default class SearchAdvancedOptions extends Component {);}+ setSearchedTermValueForIn() {+ const validValues = this.inOptions.map((option) => option.value);+ const blocks = this.filterBlocks(REGEXP_IN_PREFIX);+ const selectedFilters = [];++ for (const block of blocks) {+ let value = block.replace(REGEXP_IN_PREFIX, "").toLowerCase();+ // Handle "with:images" -> "images" and "in:personal" -> "messages"+ if (value === "personal") {+ value = "messages";+ }+ if (validValues.includes(value)) {+ selectedFilters.push(value);+ }+ }++ const currentIn = this.get("searchedTerms.in") || [];+ const hasChanged =+ selectedFilters.length !== currentIn.length ||+ !selectedFilters.every((v) => currentIn.includes(v));++ if (hasChanged) {+ this.set("searchedTerms.in", selectedFilters);+ }+ }+findSearchTerms() {const searchTerm = escapeExpression(this.searchTerm);if (!searchTerm) {@@ -265,18 +279,6 @@ export default class SearchAdvancedOptions extends Component {}}- setSearchedTermSpecialInValue(key, replaceRegEx) {- const match = this.filterBlocks(replaceRegEx);-- if (match.length !== 0) {- if (this.get(key) !== true) {- this.set(key, true);- }- } else if (this.get(key) !== false) {- this.set(key, false);- }- }-setSearchedTermValueForCategory() {const match = this.filterBlocks(REGEXP_CATEGORY_PREFIX);if (match.length !== 0) {@@ -371,22 +373,6 @@ export default class SearchAdvancedOptions extends Component {}}- updateInRegex(regex, filter) {- const match = this.filterBlocks(regex);- const inFilter = this.get("searchedTerms.special.in." + filter);- let searchTerm = this.searchTerm || "";-- if (inFilter) {- if (match.length === 0) {- searchTerm += ` in:${filter}`;- this._updateSearchTerm(searchTerm);- }- } else if (match.length !== 0) {- searchTerm = searchTerm.replace(match, "");- this._updateSearchTerm(searchTerm);- }- }-@actiononChangeSearchTermMinPostCount(value) {this.set("searchedTerms.min_posts", value.length ? value : null);@@ -412,8 +398,8 @@ export default class SearchAdvancedOptions extends Component {}@action- onChangeSearchTermForIn(value) {- this.set("searchedTerms.in", value);+ onChangeSearchTermForIn(selectedValues) {+ this.set("searchedTerms.in", selectedValues || []);this._updateSearchTermForIn();}@@ -471,30 +457,6 @@ export default class SearchAdvancedOptions extends Component {this._updateSearchTermForTags();}- @action- onChangeSearchTermForSpecialInLikes(event) {- this.set("searchedTerms.special.in.likes", event.target.checked);- this.updateInRegex(REGEXP_SPECIAL_IN_LIKES_MATCH, "likes");- }-- @action- onChangeSearchTermForSpecialInMessages(event) {- this.set("searchedTerms.special.in.messages", event.target.checked);- this.updateInRegex(REGEXP_SPECIAL_IN_MESSAGES_MATCH, "messages");- }-- @action- onChangeSearchTermForSpecialInSeen(event) {- this.set("searchedTerms.special.in.seen", event.target.checked);- this.updateInRegex(REGEXP_SPECIAL_IN_SEEN_MATCH, "seen");- }-- @action- onChangeSearchTermForSpecialInTitle(event) {- this.set("searchedTerms.special.in.title", event.target.checked);- this.updateInRegex(REGEXP_SPECIAL_IN_TITLE_MATCH, "title");- }-@actiononChangeSearchedTermField(path, updateFnName, value) {this.set(`searchedTerms.${path}`, value);@@ -626,29 +588,30 @@ export default class SearchAdvancedOptions extends Component {}_updateSearchTermForIn() {- let regExpInMatch = this.inOptions.map((option) => option.value).join("|");- const REGEXP_IN_MATCH = new RegExp(`(in|with):(${regExpInMatch})`, "i");-- const match = this.filterBlocks(REGEXP_IN_MATCH);- const inFilter = this.get("searchedTerms.in");- let keyword = "in";- if (inFilter in IN_OPTIONS_MAPPING) {- keyword = IN_OPTIONS_MAPPING[inFilter];- }let searchTerm = this.searchTerm || "";+ const selectedFilters = this.get("searchedTerms.in") || [];- if (inFilter) {- if (match.length !== 0) {- searchTerm = searchTerm.replace(match[0], `${keyword}:${inFilter}`);- } else {- searchTerm += ` ${keyword}:${inFilter}`;+ // Remove all existing in:/with: filters that match our options+ const allFilterValues = this.inOptions.map((option) => option.value);+ // Also include "personal" as an alias for "messages"+ allFilterValues.push("personal");++ const regExpAllIn = new RegExp(+ `\\s*(in|with):(${allFilterValues.join("|")})`,+ "gi"+ );+ searchTerm = searchTerm.replace(regExpAllIn, "");++ // Add the selected filters+ selectedFilters.forEach((filter) => {+ let keyword = "in";+ if (filter in IN_OPTIONS_MAPPING) {+ keyword = IN_OPTIONS_MAPPING[filter];}+ searchTerm += ` ${keyword}:${filter}`;+ });- this._updateSearchTerm(searchTerm);- } else if (match.length !== 0) {- searchTerm = searchTerm.replace(match, "");- this._updateSearchTerm(searchTerm);- }+ this._updateSearchTerm(searchTerm);}_updateSearchTermForStatus() {@@ -834,76 +797,21 @@ export default class SearchAdvancedOptions extends Component {{{/if}}<div class="control-group advanced-search-topics-posts">+ <label class="control-label">{{i18n+ "search.advanced.filters.label"+ }}</label><div class="controls">- <fieldset class="grouped-control">- <legend class="grouped-control-label">{{i18n- "search.advanced.filters.label"- }}</legend>-- {{#if this.currentUser}}- <div class="grouped-control-field">- <Input- id="matching-title-only"- @type="checkbox"- class="in-title"- @checked={{this.searchedTerms.special.in.title}}- {{on "click" this.onChangeSearchTermForSpecialInTitle}}- />- <label class="checkbox-label" for="matching-title-only">- {{i18n "search.advanced.filters.title"}}- </label>- </div>-- <div class="grouped-control-field">- <Input- id="matching-liked"- @type="checkbox"- class="in-likes"- @checked={{this.searchedTerms.special.in.likes}}- {{on "click" this.onChangeSearchTermForSpecialInLikes}}- />- <label class="checkbox-label" for="matching-liked">{{i18n- "search.advanced.filters.likes"- }}</label>- </div>-- <div class="grouped-control-field">- <Input- id="matching-in-messages"- @type="checkbox"- class="in-private"- @checked={{this.searchedTerms.special.in.messages}}- {{on "click" this.onChangeSearchTermForSpecialInMessages}}- />- <label- class="checkbox-label"- for="matching-in-messages"- >{{i18n "search.advanced.filters.private"}}</label>- </div>-- <div class="grouped-control-field">- <Input- id="matching-seen"- @type="checkbox"- class="in-seen"- @checked={{this.searchedTerms.special.in.seen}}- {{on "click" this.onChangeSearchTermForSpecialInSeen}}- />- <label class="checkbox-label" for="matching-seen">{{i18n- "search.advanced.filters.seen"- }}</label>- </div>- {{/if}}-- <ComboBox- @id="in"- @valueProperty="value"- @content={{this.inOptions}}- @value={{this.searchedTerms.in}}- @onChange={{this.onChangeSearchTermForIn}}- @options={{hash none="user.locale.any" clearable=true}}- />- </fieldset>+ <MultiSelect+ @id="search-in-options"+ @valueProperty="value"+ @content={{this.inOptions}}+ @value={{this.searchedTerms.in}}+ @onChange={{this.onChangeSearchTermForIn}}+ @options={{hash+ none="search.advanced.filters.none"+ headerAriaLabel=(i18n "search.advanced.filters.label")+ }}+ /></div></div>diff --git a/frontend/discourse/tests/acceptance/search-full-test.js b/frontend/discourse/tests/acceptance/search-full-test.jsindex 92eee2add2e..f13f0284f12 100644--- a/frontend/discourse/tests/acceptance/search-full-test.js+++ b/frontend/discourse/tests/acceptance/search-full-test.js@@ -8,6 +8,9 @@ import {import { acceptance, selectDate } from "discourse/tests/helpers/qunit-helpers";import selectKit from "discourse/tests/helpers/select-kit-helper";+const IN_OPTIONS_SELECTOR =+ ".search-advanced-options .select-kit#search-in-options";+let searchResultClickTracked = false;acceptance("Search - Full Page", function (needs) {@@ -198,45 +201,65 @@ acceptance("Search - Full Page", function (needs) {});test("update in:title filter through advanced search UI", async function (assert) {+ const inSelector = selectKit(IN_OPTIONS_SELECTOR);+await visit("/search");+ await click(".advanced-filters > summary");await fillIn(".search-query", "none");- await click(".search-advanced-options .in-title");+ await inSelector.expand();+ await inSelector.selectRowByValue("title");- assert- .dom(".search-advanced-options .in-title")- .isChecked('has "in title" populated');+ assert.strictEqual(+ inSelector.header().value(),+ "title",+ 'has "in title" selected'+ );assert.dom(".search-query").hasValue("none in:title", 'has updated search term to "none in:title"');await fillIn(".search-query", "none in:titleasd");- assert- .dom(".search-advanced-options .in-title")- .isNotChecked("does not populate title only checkbox");+ assert.strictEqual(+ inSelector.header().value(),+ null,+ "does not have title selected for invalid filter"+ );});test("update in:likes filter through advanced search UI", async function (assert) {+ const inSelector = selectKit(IN_OPTIONS_SELECTOR);+await visit("/search");+ await click(".advanced-filters > summary");await fillIn(".search-query", "none");- await click(".search-advanced-options .in-likes");+ await inSelector.expand();+ await inSelector.selectRowByValue("likes");- assert- .dom(".search-advanced-options .in-likes")- .isChecked('has "I liked" populated');+ assert.strictEqual(+ inSelector.header().value(),+ "likes",+ 'has "I liked" selected'+ );assert.dom(".search-query").hasValue("none in:likes", 'has updated search term to "none in:likes"');});test("update in:messages filter through advanced search UI", async function (assert) {+ const inSelector = selectKit(IN_OPTIONS_SELECTOR);+await visit("/search");+ await click(".advanced-filters > summary");await fillIn(".search-query", "none");- await click(".search-advanced-options .in-private");+ await inSelector.expand();+ await inSelector.selectRowByValue("messages");- assert- .dom(".search-advanced-options .in-private")- .isChecked('has "are in my messages" populated');+ assert.strictEqual(+ inSelector.header().value(),+ "messages",+ 'has "In my messages" selected'+ );assert.dom(".search-query")@@ -247,19 +270,27 @@ acceptance("Search - Full Page", function (needs) {await fillIn(".search-query", "none in:personal-direct");- assert- .dom(".search-advanced-options .in-private")- .isNotChecked("does not populate messages checkbox");+ assert.strictEqual(+ inSelector.header().value(),+ null,+ "does not have messages selected for personal-direct filter"+ );});test("update in:seen filter through advanced search UI", async function (assert) {+ const inSelector = selectKit(IN_OPTIONS_SELECTOR);+await visit("/search");+ await click(".advanced-filters > summary");await fillIn(".search-query", "none");- await click(".search-advanced-options .in-seen");+ await inSelector.expand();+ await inSelector.selectRowByValue("seen");- assert- .dom(".search-advanced-options .in-seen")- .isChecked("it should check the right checkbox");+ assert.strictEqual(+ inSelector.header().value(),+ "seen",+ "has seen selected"+ );assert.dom(".search-query")@@ -267,13 +298,15 @@ acceptance("Search - Full Page", function (needs) {await fillIn(".search-query", "none in:seenasdan");- assert- .dom(".search-advanced-options .in-seen")- .isNotChecked("does not populate seen checkbox");+ assert.strictEqual(+ inSelector.header().value(),+ null,+ "does not have seen selected for invalid filter"+ );});test("update in filter through advanced search UI", async function (assert) {- const inSelector = selectKit(".search-advanced-options .select-kit#in");+ const inSelector = selectKit(IN_OPTIONS_SELECTOR);await visit("/search");@@ -283,9 +316,9 @@ acceptance("Search - Full Page", function (needs) {await inSelector.selectRowByValue("bookmarks");assert.strictEqual(- inSelector.header().label(),- "I bookmarked",- 'has "I bookmarked" populated'+ inSelector.header().value(),+ "bookmarks",+ 'has "I bookmarked" selected');assert.dom(".search-query")@@ -295,6 +328,34 @@ acceptance("Search - Full Page", function (needs) {);});+ test("selecting multiple in: filters through advanced search UI", async function (assert) {+ const inSelector = selectKit(IN_OPTIONS_SELECTOR);++ await visit("/search?expanded=true");+ await fillIn(".search-query", "none");+ await inSelector.expand();+ await inSelector.selectRowByValue("title");+ await inSelector.selectRowByValue("likes");++ assert.true(+ inSelector.header().value().includes("title"),+ "has title selected"+ );+ assert.true(+ inSelector.header().value().includes("likes"),+ "has likes selected"+ );+ assert+ .dom(".search-query")+ .hasValue("none in:title in:likes", "has both filters in search term");++ await inSelector.deselectItemByValue("title");++ assert+ .dom(".search-query")+ .hasValue("none in:likes", "removes deselected filter from search term");+ });+test("update status through advanced search UI", async function (assert) {const statusSelector = selectKit(".search-advanced-options .select-kit#search-status-options"@@ -338,7 +399,7 @@ acceptance("Search - Full Page", function (needs) {});test("doesn't update in filter header if wrong value entered through searchbox", async function (assert) {- const inSelector = selectKit(".search-advanced-options .select-kit#in");+ const inSelector = selectKit(IN_OPTIONS_SELECTOR);await visit("/search");@@ -346,9 +407,9 @@ acceptance("Search - Full Page", function (needs) {await fillIn(".search-query", "in:none");assert.strictEqual(- inSelector.header().label(),- "any",- 'has "any" populated'+ inSelector.header().value(),+ null,+ "has no filters selected");});@@ -423,12 +484,17 @@ acceptance("Search - Full Page", function (needs) {});test("validate advanced search when initially empty", async function (assert) {+ const inSelector = selectKit(IN_OPTIONS_SELECTOR);+await visit("/search?expanded=true");- await click(".search-advanced-options .in-likes");+ await inSelector.expand();+ await inSelector.selectRowByValue("likes");- assert- .dom(".search-advanced-options .in-likes")- .isChecked('has "I liked" populated');+ assert.strictEqual(+ inSelector.header().value(),+ "likes",+ 'has "I liked" selected'+ );assert.dom(".search-query")@@ -436,9 +502,11 @@ acceptance("Search - Full Page", function (needs) {await fillIn(".search-query", "in:likesasdas");- assert- .dom(".search-advanced-options .in-likes")- .isNotChecked("does not populate the likes checkbox");+ assert.strictEqual(+ inSelector.header().value(),+ null,+ "does not have likes selected for invalid filter"+ );});test("all tags checkbox only visible for two or more tags", async function (assert) {diff --git a/frontend/discourse/tests/acceptance/search-test.js b/frontend/discourse/tests/acceptance/search-test.jsindex 7a3941c6a38..c84cbe7d274 100644--- a/frontend/discourse/tests/acceptance/search-test.js+++ b/frontend/discourse/tests/acceptance/search-test.js@@ -470,7 +470,7 @@ acceptance("Search - Authenticated", function (needs) {});test("full page search - the right filters are shown", async function (assert) {- const inSelector = selectKit(".select-kit#in");+ const inSelector = selectKit("#search-in-options");await visit("/search?expanded=true");await inSelector.expand();@@ -485,9 +485,12 @@ acceptance("Search - Authenticated", function (needs) {assert.true(inSelector.rowByValue("tracking").exists());assert.true(inSelector.rowByValue("bookmarks").exists());- assert.dom(".search-advanced-options .in-likes").exists();- assert.dom(".search-advanced-options .in-private").exists();- assert.dom(".search-advanced-options .in-seen").exists();+ assert.true(inSelector.rowByValue("likes").exists());+ assert.true(inSelector.rowByValue("messages").exists());+ assert.true(inSelector.rowByValue("seen").exists());+ assert.true(inSelector.rowByValue("title").exists());+ assert.true(inSelector.rowByValue("created").exists());+ assert.true(inSelector.rowByValue("all").exists());});test("topic results - topic search scope - works with empty result sets", async function (assert) {