diff --git a/app/assets/javascripts/discourse/controllers/full-page-search.js.es6 b/app/assets/javascripts/discourse/controllers/full-page-search.js.es6 new file mode 100644 index 000000000..7deae7bd2 --- /dev/null +++ b/app/assets/javascripts/discourse/controllers/full-page-search.js.es6 @@ -0,0 +1,33 @@ +import DiscourseController from 'discourse/controllers/controller'; +import { translateResults } from 'discourse/lib/search-for-term'; + +export default DiscourseController.extend({ + loading: Em.computed.not('model'), + queryParams: ['q'], + q: null, + modelChanged: function(){ + if (this.get('searchTerm') !== this.get('q')) { + this.set('searchTerm', this.get('q')); + } + }.observes('model'), + + qChanged: function(){ + var model = this.get('model'); + if (model && this.get('model.q') !== this.get('q')){ + this.set('searchTerm', this.get('q')); + this.send('search'); + } + }.observes('q'), + actions: { + search: function(){ + var self = this; + this.set('q', this.get('searchTerm')); + this.set('model', null); + + Discourse.ajax('/search2', {data: {q: this.get('searchTerm')}}).then(function(results) { + self.set('model', translateResults(results) || {}); + self.set('model.q', self.get('q')); + }); + } + } +}); diff --git a/app/assets/javascripts/discourse/lib/search-for-term.js.es6 b/app/assets/javascripts/discourse/lib/search-for-term.js.es6 index a63eacad7..e5348a6a1 100644 --- a/app/assets/javascripts/discourse/lib/search-for-term.js.es6 +++ b/app/assets/javascripts/discourse/lib/search-for-term.js.es6 @@ -1,5 +1,67 @@ import Topic from 'discourse/models/topic'; +export function translateResults(results, opts) { + if (!opts) opts = {}; + + // Topics might not be included + if (!results.topics) { results.topics = []; } + if (!results.users) { results.users = []; } + if (!results.posts) { results.posts = []; } + if (!results.categories) { results.categories = []; } + + const topicMap = {}; + results.topics = results.topics.map(function(topic){ + topic = Topic.create(topic); + topicMap[topic.id] = topic; + return topic; + }); + + results.posts = results.posts.map(function(post){ + post = Discourse.Post.create(post); + post.set('topic', topicMap[post.topic_id]); + return post; + }); + + results.users = results.users.map(function(user){ + user = Discourse.User.create(user); + return user; + }); + + results.categories = results.categories.map(function(category){ + return Discourse.Category.list().findProperty('id', category.id); + }).compact(); + + const r = results.grouped_search_result; + results.resultTypes = []; + + // TODO: consider refactoring front end to take a better structure + [['topic','posts'],['user','users'],['category','categories']].forEach(function(pair){ + const type = pair[0], name = pair[1]; + if (results[name].length > 0) { + var result = { + results: results[name], + componentName: "search-result-" + ((opts.searchContext && opts.searchContext.type === 'topic' && type === 'topic') ? 'post' : type), + type, + more: r['more_' + name] + }; + + if (result.more && name === "posts" && opts.fullSearchUrl) { + result.more = false; + result.moreUrl = opts.fullSearchUrl; + } + + results.resultTypes.push(result); + } + }); + + const noResults = !!(results.topics.length === 0 && + results.posts.length === 0 && + results.users.length === 0 && + results.categories.length === 0); + + return noResults ? null : Em.Object.create(results); +} + function searchForTerm(term, opts) { if (!opts) opts = {}; @@ -16,63 +78,7 @@ function searchForTerm(term, opts) { } return Discourse.ajax('/search/query', { data: data }).then(function(results){ - // Topics might not be included - if (!results.topics) { results.topics = []; } - if (!results.users) { results.users = []; } - if (!results.posts) { results.posts = []; } - if (!results.categories) { results.categories = []; } - - const topicMap = {}; - results.topics = results.topics.map(function(topic){ - topic = Topic.create(topic); - topicMap[topic.id] = topic; - return topic; - }); - - results.posts = results.posts.map(function(post){ - post = Discourse.Post.create(post); - post.set('topic', topicMap[post.topic_id]); - return post; - }); - - results.users = results.users.map(function(user){ - user = Discourse.User.create(user); - return user; - }); - - results.categories = results.categories.map(function(category){ - return Discourse.Category.list().findProperty('id', category.id); - }).compact(); - - const r = results.grouped_search_result; - results.resultTypes = []; - - // TODO: consider refactoring front end to take a better structure - [['topic','posts'],['user','users'],['category','categories']].forEach(function(pair){ - const type = pair[0], name = pair[1]; - if (results[name].length > 0) { - var result = { - results: results[name], - componentName: "search-result-" + ((opts.searchContext && opts.searchContext.type === 'topic' && type === 'topic') ? 'post' : type), - type, - more: r['more_' + name] - }; - - if (result.more && name === "posts" && opts.fullSearchUrl) { - result.more = false; - result.moreUrl = opts.fullSearchUrl; - } - - results.resultTypes.push(result); - } - }); - - const noResults = !!(results.topics.length === 0 && - results.posts.length === 0 && - results.users.length === 0 && - results.categories.length === 0); - - return noResults ? null : Em.Object.create(results); + return translateResults(results, opts); }); } diff --git a/app/assets/javascripts/discourse/routes/app-route-map.js.es6 b/app/assets/javascripts/discourse/routes/app-route-map.js.es6 index 98e1194df..530f4845e 100644 --- a/app/assets/javascripts/discourse/routes/app-route-map.js.es6 +++ b/app/assets/javascripts/discourse/routes/app-route-map.js.es6 @@ -99,4 +99,6 @@ export default function() { }); this.resource('queued-posts', { path: '/queued-posts' }); + + this.route('full-page-search', {path: '/search2'}); } diff --git a/app/assets/javascripts/discourse/routes/full-page-search.js.es6 b/app/assets/javascripts/discourse/routes/full-page-search.js.es6 new file mode 100644 index 000000000..935437404 --- /dev/null +++ b/app/assets/javascripts/discourse/routes/full-page-search.js.es6 @@ -0,0 +1,18 @@ +import { translateResults } from 'discourse/lib/search-for-term'; + +export default Discourse.Route.extend({ + queryParams: { + q: { + } + }, + model: function(params) { + return PreloadStore.getAndRemove("search", function() { + return Discourse.ajax('/search2', {data: {q: params.q}}); + }).then(function(results){ + var model = translateResults(results) || {}; + model.q = params.q; + return model; + }); + } + +}); diff --git a/app/assets/javascripts/discourse/templates/full-page-search.hbs b/app/assets/javascripts/discourse/templates/full-page-search.hbs new file mode 100644 index 000000000..b12f43734 --- /dev/null +++ b/app/assets/javascripts/discourse/templates/full-page-search.hbs @@ -0,0 +1,38 @@ +