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 fa52b4122..ee55b5836 100644 --- a/app/assets/javascripts/discourse/controllers/full-page-search.js.es6 +++ b/app/assets/javascripts/discourse/controllers/full-page-search.js.es6 @@ -3,6 +3,13 @@ import showModal from 'discourse/lib/show-modal'; import { default as computed, observes } from 'ember-addons/ember-computed-decorators'; import Category from 'discourse/models/category'; +const SortOrders = [ + {name: I18n.t('search.relevance'), id: 0}, + {name: I18n.t('search.latest_post'), id: 1, term: 'order:latest'}, + {name: I18n.t('search.most_liked'), id: 2, term: 'order:likes'}, + {name: I18n.t('search.most_viewed'), id: 3, term: 'order:views'}, +]; + export default Ember.Controller.extend({ needs: ["application"], @@ -13,6 +20,13 @@ export default Ember.Controller.extend({ context_id: null, context: null, searching: false, + sortOrder: 0, + sortOrders: SortOrders, + + @computed('model.posts') + resultCount(posts){ + return posts && posts.length; + }, @computed('q') hasAutofocus(q) { @@ -51,18 +65,61 @@ export default Ember.Controller.extend({ return !!(searching || !isValidSearchTerm(searchTerm)); }, + @computed('q') + noSortQ(q) { + if (q) { + SortOrders.forEach((order) => { + if (q.indexOf(order.term) > -1){ + q = q.replace(order.term, ""); + q = q.trim(); + } + }); + } + return q; + }, + + _searchOnSortChange: true, + + setSearchTerm(term) { + this._searchOnSortChange = false; + if (term) { + SortOrders.forEach((order) => { + if (term.indexOf(order.term) > -1){ + this.set('sortOrder', order.id); + term = term.replace(order.term, ""); + term = term.trim(); + } + }); + } + this._searchOnSortChange = true; + this.set('searchTerm', term); + }, + + @observes('sortOrder') + triggerSearch(){ + if (this._searchOnSortChange) { + this.search(); + } + }, + @observes('model') modelChanged() { if (this.get("searchTerm") !== this.get("q")) { - this.set("searchTerm", this.get("q")); + this.setSearchTerm(this.get("q")); } }, + @computed('q') + showLikeCount(q) { + console.log(q); + return q && q.indexOf("order:likes") > -1; + }, + @observes('q') qChanged() { const model = this.get("model"); if (model && this.get("model.q") !== this.get("q")) { - this.set("searchTerm", this.get("q")); + this.setSearchTerm(this.get("q")); this.send("search"); } }, @@ -80,11 +137,16 @@ export default Ember.Controller.extend({ const router = Discourse.__container__.lookup('router:main'); - this.set("q", this.get("searchTerm")); - this.set("model", null); - var args = { q: this.get("searchTerm") }; + const sortOrder = this.get("sortOrder"); + if (sortOrder && SortOrders[sortOrder].term) { + args.q += " " + SortOrders[sortOrder].term; + } + + this.set("q", args.q); + this.set("model", null); + const skip = this.get("skip_context"); if ((!skip && this.get('context')) || skip==="false"){ args.search_context = { diff --git a/app/assets/javascripts/discourse/templates/full-page-search.hbs b/app/assets/javascripts/discourse/templates/full-page-search.hbs index 13a16e145..c85fae3f7 100644 --- a/app/assets/javascripts/discourse/templates/full-page-search.hbs +++ b/app/assets/javascripts/discourse/templates/full-page-search.hbs @@ -37,6 +37,22 @@ {{/unless}} + {{#if model.posts}} +