From b4974f5876d53969e7b523ac4592cf4fe58fb34c Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Sat, 12 Sep 2015 13:38:20 +0800 Subject: [PATCH] UX: Don't allow search if searchTerm is not valid. --- .../discourse/components/search-menu.js.es6 | 9 ++++----- .../discourse/controllers/full-page-search.js.es6 | 10 ++++++++-- app/assets/javascripts/discourse/lib/search.js.es6 | 10 +++++++++- .../discourse/routes/full-page-search.js.es6 | 4 ++-- .../discourse/templates/full-page-search.hbs | 2 +- 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/app/assets/javascripts/discourse/components/search-menu.js.es6 b/app/assets/javascripts/discourse/components/search-menu.js.es6 index 8a4f2ddecef..153929ecd6b 100644 --- a/app/assets/javascripts/discourse/components/search-menu.js.es6 +++ b/app/assets/javascripts/discourse/components/search-menu.js.es6 @@ -1,4 +1,4 @@ -import {searchForTerm, searchContextDescription} from 'discourse/lib/search'; +import {searchForTerm, searchContextDescription, isValidSearchTerm } from 'discourse/lib/search'; import DiscourseURL from 'discourse/lib/url'; import { default as computed, observes } from 'ember-addons/ember-computed-decorators'; import showModal from 'discourse/lib/show-modal'; @@ -61,8 +61,8 @@ export default Ember.Component.extend({ @observes('searchService.term', 'typeFilter') newSearchNeeded() { this.set('noResults', false); - const term = (this.get('searchService.term') || '').trim(); - if (term.length >= Discourse.SiteSettings.min_search_term_length) { + const term = this.get('searchService.term') + if (isValidSearchTerm(term)) { this.set('loading', true); Ember.run.debounce(this, 'searchTerm', term, this.get('typeFilter'), 400); } else { @@ -154,8 +154,7 @@ export default Ember.Component.extend({ }, keyDown(e) { - const term = this.get('searchService.term'); - if (e.which === 13 && term && term.length >= this.siteSettings.min_search_term_length) { + if (e.which === 13 && isValidSearchTerm(this.get('searchService.term'))) { this.set('visible', false); this.send('fullSearch'); } diff --git a/app/assets/javascripts/discourse/controllers/full-page-search.js.es6 b/app/assets/javascripts/discourse/controllers/full-page-search.js.es6 index fce0fecd836..2e909601fb6 100644 --- a/app/assets/javascripts/discourse/controllers/full-page-search.js.es6 +++ b/app/assets/javascripts/discourse/controllers/full-page-search.js.es6 @@ -1,4 +1,4 @@ -import { translateResults, searchContextDescription, getSearchKey } from "discourse/lib/search"; +import { translateResults, searchContextDescription, getSearchKey, isValidSearchTerm } from "discourse/lib/search"; import showModal from 'discourse/lib/show-modal'; import { default as computed, observes } from 'ember-addons/ember-computed-decorators'; import Category from 'discourse/models/category'; @@ -37,7 +37,12 @@ export default Ember.Controller.extend({ @computed('q') searchActive(q){ - return q && q.length > 0; + return isValidSearchTerm(q); + }, + + @computed('searchTerm') + isNotValidSearchTerm(searchTerm) { + return !isValidSearchTerm(searchTerm); }, @observes('model') @@ -129,6 +134,7 @@ export default Ember.Controller.extend({ }, search() { + if (this.get("isNotValidSearchTerm")) return; this.search(); } } diff --git a/app/assets/javascripts/discourse/lib/search.js.es6 b/app/assets/javascripts/discourse/lib/search.js.es6 index 21fd8afeb65..9d7bec2d67c 100644 --- a/app/assets/javascripts/discourse/lib/search.js.es6 +++ b/app/assets/javascripts/discourse/lib/search.js.es6 @@ -106,4 +106,12 @@ const getSearchKey = function(args){ ((args.searchContext && args.searchContext.id) || "") }; -export { searchForTerm, searchContextDescription, getSearchKey }; +const isValidSearchTerm = function(searchTerm) { + if (searchTerm) { + return searchTerm.trim().length >= Discourse.SiteSettings.min_search_term_length; + } else { + return false; + } +}; + +export { searchForTerm, searchContextDescription, getSearchKey, isValidSearchTerm }; diff --git a/app/assets/javascripts/discourse/routes/full-page-search.js.es6 b/app/assets/javascripts/discourse/routes/full-page-search.js.es6 index 10072d99f55..3464250a74f 100644 --- a/app/assets/javascripts/discourse/routes/full-page-search.js.es6 +++ b/app/assets/javascripts/discourse/routes/full-page-search.js.es6 @@ -1,4 +1,4 @@ -import { translateResults, getSearchKey } from "discourse/lib/search"; +import { translateResults, getSearchKey, isValidSearchTerm } from "discourse/lib/search"; export default Discourse.Route.extend({ queryParams: { q: {}, context_id: {}, context: {} }, @@ -23,7 +23,7 @@ export default Discourse.Route.extend({ } return PreloadStore.getAndRemove("search", function() { - if (params.q && params.q.length > 2) { + if (isValidSearchTerm(params.q)) { return Discourse.ajax("/search", { data: args }); } else { return null; diff --git a/app/assets/javascripts/discourse/templates/full-page-search.hbs b/app/assets/javascripts/discourse/templates/full-page-search.hbs index db60849ff9a..43e3e835ad5 100644 --- a/app/assets/javascripts/discourse/templates/full-page-search.hbs +++ b/app/assets/javascripts/discourse/templates/full-page-search.hbs @@ -1,6 +1,6 @@