UX: Don't allow search if searchTerm is not valid.

This commit is contained in:
Guo Xiang Tan 2015-09-12 13:38:20 +08:00
parent 4252a2ee1e
commit b4974f5876
5 changed files with 24 additions and 11 deletions

View File

@ -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');
}

View File

@ -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();
}
}

View File

@ -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 };

View File

@ -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;

View File

@ -1,6 +1,6 @@
<div class="search row clearfix">
{{input type="text" value=searchTerm class="input-xxlarge search no-blur" action="search"}}
{{d-button action="search" icon="search" class="btn-primary"}}
{{d-button action="search" icon="search" class="btn-primary" disabled=isNotValidSearchTerm}}
{{#if canBulkSelect}}
{{#if model.posts}}
{{d-button icon="list" class="bulk-select" title="topics.bulk.toggle" action="toggleBulkSelect"}}