From ed08545d1fd6dd24e8727e76547dfa1293ff36bb Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 24 May 2018 12:34:23 +1000 Subject: [PATCH] FEATURE: allow searching in title in advanced search Add UI for matching in title only in advanced search options --- .../components/search-advanced-options.js.es6 | 48 +++++++------------ .../components/search-advanced-options.hbs | 1 + config/locales/client.en.yml | 1 + 3 files changed, 20 insertions(+), 30 deletions(-) diff --git a/app/assets/javascripts/discourse/components/search-advanced-options.js.es6 b/app/assets/javascripts/discourse/components/search-advanced-options.js.es6 index a171083523d..22976d76d34 100644 --- a/app/assets/javascripts/discourse/components/search-advanced-options.js.es6 +++ b/app/assets/javascripts/discourse/components/search-advanced-options.js.es6 @@ -18,6 +18,7 @@ const REGEXP_TAGS_REPLACE = /(^(tags?:|#(?=[a-z0-9\-]+::tag))|::tag\s?$ const REGEXP_IN_MATCH = /^(in|with):(posted|watching|tracking|bookmarks|first|pinned|unpinned|wiki|unseen|image)/ig; const REGEXP_SPECIAL_IN_LIKES_MATCH = /^in:likes/ig; +const REGEXP_SPECIAL_IN_TITLE_MATCH = /^in:title/ig; const REGEXP_SPECIAL_IN_PRIVATE_MATCH = /^in:private/ig; const REGEXP_SPECIAL_IN_SEEN_MATCH = /^in:seen/ig; @@ -81,6 +82,7 @@ export default Em.Component.extend({ in: '', special: { in: { + title: false, likes: false, private: false, seen: false @@ -111,6 +113,7 @@ export default Em.Component.extend({ this.setSearchedTermValueForTags(); 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.private', REGEXP_SPECIAL_IN_PRIVATE_MATCH); this.setSearchedTermSpecialInValue('searchedTerms.special.in.seen', REGEXP_SPECIAL_IN_SEEN_MATCH); this.setSearchedTermValue('searchedTerms.status', REGEXP_STATUS_PREFIX); @@ -424,15 +427,14 @@ export default Em.Component.extend({ } }, - @observes('searchedTerms.special.in.likes') - updateSearchTermForSpecialInLikes() { - const match = this.filterBlocks(REGEXP_SPECIAL_IN_LIKES_MATCH); - const inFilter = this.get('searchedTerms.special.in.likes'); + updateInRegex(regex, filter) { + const match = this.filterBlocks(regex); + const inFilter = this.get('searchedTerms.special.in.' + filter); let searchTerm = this.get('searchTerm') || ''; if (inFilter) { if (match.length === 0) { - searchTerm += ` in:likes`; + searchTerm += ` in:${filter}`; this.set('searchTerm', searchTerm.trim()); } } else if (match.length !== 0) { @@ -441,38 +443,24 @@ export default Em.Component.extend({ } }, + @observes('searchedTerms.special.in.likes') + updateSearchTermForSpecialInLikes() { + this.updateInRegex(REGEXP_SPECIAL_IN_LIKES_MATCH, 'likes'); + }, + @observes('searchedTerms.special.in.private') updateSearchTermForSpecialInPrivate() { - const match = this.filterBlocks(REGEXP_SPECIAL_IN_PRIVATE_MATCH); - const inFilter = this.get('searchedTerms.special.in.private'); - let searchTerm = this.get('searchTerm') || ''; - - if (inFilter) { - if (match.length === 0) { - searchTerm += ` in:private`; - this.set('searchTerm', searchTerm.trim()); - } - } else if (match.length !== 0) { - searchTerm = searchTerm.replace(match, ''); - this.set('searchTerm', searchTerm.trim()); - } + this.updateInRegex(REGEXP_SPECIAL_IN_PRIVATE_MATCH, 'private'); }, @observes('searchedTerms.special.in.seen') updateSearchTermForSpecialInSeen() { - const match = this.filterBlocks(REGEXP_SPECIAL_IN_SEEN_MATCH); - const inFilter = this.get('searchedTerms.special.in.seen'); - let searchTerm = this.get('searchTerm') || ''; + this.updateInRegex(REGEXP_SPECIAL_IN_SEEN_MATCH, 'seen'); + }, - if (inFilter) { - if (match.length === 0) { - searchTerm += ` in:seen`; - this.set('searchTerm', searchTerm.trim()); - } - } else if (match.length !== 0) { - searchTerm = searchTerm.replace(match, ''); - this.set('searchTerm', searchTerm.trim()); - } + @observes('searchedTerms.special.in.title') + updateSearchTermForSpecialInTitle() { + this.updateInRegex(REGEXP_SPECIAL_IN_TITLE_MATCH, 'title'); }, @observes('searchedTerms.status') diff --git a/app/assets/javascripts/discourse/templates/components/search-advanced-options.hbs b/app/assets/javascripts/discourse/templates/components/search-advanced-options.hbs index 3ce9b3d84f9..fd330828a99 100644 --- a/app/assets/javascripts/discourse/templates/components/search-advanced-options.hbs +++ b/app/assets/javascripts/discourse/templates/components/search-advanced-options.hbs @@ -60,6 +60,7 @@
{{#if currentUser}}
+ diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 15c0898f5d5..e936696bf8d 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -1508,6 +1508,7 @@ en: label: Tagged filters: label: Only return topics/posts... + title: Matching in title only likes: I liked posted: I posted in watching: I'm watching