FIX: broken debounce in search terms.

This commit is contained in:
Robin Ward 2015-08-10 15:31:10 -04:00
parent b02d624503
commit d37accb5bc
2 changed files with 23 additions and 25 deletions
app/assets/javascripts/discourse/controllers
test/javascripts/acceptance

View file

@ -1,14 +1,15 @@
import Presence from 'discourse/mixins/presence'; import Presence from 'discourse/mixins/presence';
import searchForTerm from 'discourse/lib/search-for-term'; import searchForTerm from 'discourse/lib/search-for-term';
var _dontSearch = false; let _dontSearch = false;
export default Em.Controller.extend(Presence, { export default Em.Controller.extend(Presence, {
typeFilter: null,
contextType: function(key, value){ contextType: function(key, value){
if(arguments.length > 1) { if(arguments.length > 1) {
// a bit hacky, consider cleaning this up, need to work through all observers though // a bit hacky, consider cleaning this up, need to work through all observers though
var context = $.extend({}, this.get('searchContext')); const context = $.extend({}, this.get('searchContext'));
context.type = value; context.type = value;
this.set('searchContext', context); this.set('searchContext', context);
} }
@ -29,8 +30,8 @@ export default Em.Controller.extend(Presence, {
return null; return null;
} }
var url = '/search?q=' + encodeURIComponent(this.get('term')); let url = '/search?q=' + encodeURIComponent(this.get('term'));
var searchContext = this.get('searchContext'); const searchContext = this.get('searchContext');
if (this.get('searchContextEnabled') && searchContext) { if (this.get('searchContextEnabled') && searchContext) {
url += encodeURIComponent(" " + searchContext.type + ":" + searchContext.id); url += encodeURIComponent(" " + searchContext.type + ":" + searchContext.id);
@ -41,14 +42,14 @@ export default Em.Controller.extend(Presence, {
}.property('searchContext','term','searchContextEnabled'), }.property('searchContext','term','searchContextEnabled'),
fullSearchUrl: function(){ fullSearchUrl: function(){
var url = this.get('fullSearchUrlRelative'); const url = this.get('fullSearchUrlRelative');
if (url) { if (url) {
return Discourse.getURL(url); return Discourse.getURL(url);
} }
}.property('fullSearchUrlRelative'), }.property('fullSearchUrlRelative'),
searchContextDescription: function(){ searchContextDescription: function(){
var ctx = this.get('searchContext'); const ctx = this.get('searchContext');
if (ctx) { if (ctx) {
switch(Em.get(ctx, 'type')) { switch(Em.get(ctx, 'type')) {
case 'topic': case 'topic':
@ -71,7 +72,7 @@ export default Em.Controller.extend(Presence, {
// If we need to perform another search // If we need to perform another search
newSearchNeeded: function() { newSearchNeeded: function() {
this.set('noResults', false); this.set('noResults', false);
var term = (this.get('term') || '').trim(); const term = (this.get('term') || '').trim();
if (term.length >= Discourse.SiteSettings.min_search_term_length) { if (term.length >= Discourse.SiteSettings.min_search_term_length) {
this.set('loading', true); this.set('loading', true);
@ -82,8 +83,8 @@ export default Em.Controller.extend(Presence, {
this.set('selectedIndex', 0); this.set('selectedIndex', 0);
}.observes('term', 'typeFilter'), }.observes('term', 'typeFilter'),
searchTerm: function(term, typeFilter) { searchTerm(term, typeFilter) {
var self = this; const self = this;
// for cancelling debounced search // for cancelling debounced search
if (this._cancelSearch){ if (this._cancelSearch){
@ -95,14 +96,11 @@ export default Em.Controller.extend(Presence, {
this._search.abort(); this._search.abort();
} }
var context; const searchContext = this.get('searchContextEnabled') ? this.get('searchContext') : null;
if(this.get('searchContextEnabled')){
context = this.get('searchContext');
}
this._search = searchForTerm(term, { this._search = searchForTerm(term, {
typeFilter: typeFilter, typeFilter,
searchContext: context, searchContext,
fullSearchUrl: this.get('fullSearchUrl') fullSearchUrl: this.get('fullSearchUrl')
}); });
@ -124,7 +122,7 @@ export default Em.Controller.extend(Presence, {
}.observes('term'), }.observes('term'),
actions: { actions: {
fullSearch: function() { fullSearch() {
const self = this; const self = this;
if (this._search) { if (this._search) {
@ -138,21 +136,22 @@ export default Em.Controller.extend(Presence, {
self._cancelSearch = false; self._cancelSearch = false;
}, 400); }, 400);
var url = this.get('fullSearchUrlRelative'); const url = this.get('fullSearchUrlRelative');
if (url) { if (url) {
Discourse.URL.routeTo(url); Discourse.URL.routeTo(url);
} }
}, },
moreOfType: function(type) {
moreOfType(type) {
this.set('typeFilter', type); this.set('typeFilter', type);
}, },
cancelType: function() { cancelType() {
this.cancelTypeFilter(); this.cancelTypeFilter();
} }
}, },
cancelTypeFilter: function() { cancelTypeFilter() {
this.set('typeFilter', null); this.set('typeFilter', null);
} }
}); });

View file

@ -11,9 +11,8 @@ test("search", (assert) => {
assert.ok(!exists('#search-dropdown .results ul li'), 'no results by default'); assert.ok(!exists('#search-dropdown .results ul li'), 'no results by default');
}); });
// TODO need to change the way Discourse.ajax is stubbed so it has the .abort method fillIn('#search-term', 'dev');
// fillIn('#search-term', 'dev'); andThen(() => {
// andThen(() => { assert.ok(exists('#search-dropdown .results ul li'), 'it shows results');
// assert.ok(exists('#search-dropdown .results ul li'), 'it shows results'); });
// });
}); });