Refactor `setSearchTerm`, to be reused on `noSortQ`

This commit is contained in:
Yana Agun Siswanto 2017-03-30 01:49:54 +07:00
parent cd2d2f16e5
commit 47c1cc95dd
1 changed files with 11 additions and 14 deletions

View File

@ -75,14 +75,7 @@ export default Ember.Controller.extend({
@computed('q') @computed('q')
noSortQ(q) { noSortQ(q) {
if (q) { q = this.cleanTerm(q);
SortOrders.forEach((order) => {
if (q.indexOf(order.term) > -1){
q = q.replace(order.term, "");
q = q.trim();
}
});
}
return escapeExpression(q); return escapeExpression(q);
}, },
@ -90,19 +83,23 @@ export default Ember.Controller.extend({
setSearchTerm(term) { setSearchTerm(term) {
this._searchOnSortChange = false; this._searchOnSortChange = false;
term = this.cleanTerm(term);
this._searchOnSortChange = true;
this.set('searchTerm', term);
},
cleanTerm(term) {
if (term) { if (term) {
SortOrders.forEach(order => { SortOrders.forEach(order => {
let term_start = term.indexOf(order.term) let matches = term.match(new RegExp(`${order.term}\\b`));
let char_after_term = term[term_start + (order.term || '').length] if (matches) {
if (term_start > -1 && (!char_after_term || char_after_term.match(/\s/))){
this.set('sortOrder', order.id); this.set('sortOrder', order.id);
term = term.replace(order.term, ""); term = term.replace(new RegExp(`${order.term}\\b`, 'g'), "");
term = term.trim(); term = term.trim();
} }
}); });
} }
this._searchOnSortChange = true; return term;
this.set('searchTerm', term);
}, },
@observes('sortOrder') @observes('sortOrder')