From 962f0dd5f9c46f625bba2cde7c15825d7bd2c0c8 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 24 May 2013 12:21:53 -0400 Subject: [PATCH] Search correctly passes context data to server --- .../javascripts/discourse/components/search.js | 17 ++++++++++++----- .../discourse/controllers/search_controller.js | 7 ++++++- .../javascripts/discourse/models/category.js | 4 ++++ .../javascripts/discourse/models/topic.js | 8 ++++++-- app/assets/javascripts/discourse/models/user.js | 4 ++++ .../discourse/routes/list_category_route.js | 2 +- .../javascripts/discourse/routes/topic_route.js | 2 +- .../javascripts/discourse/routes/user_route.js | 2 +- .../discourse/views/choose_topic_view.js | 2 +- .../discourse/views/modal/edit_category_view.js | 1 + 10 files changed, 37 insertions(+), 12 deletions(-) diff --git a/app/assets/javascripts/discourse/components/search.js b/app/assets/javascripts/discourse/components/search.js index ae733877119..a05a28e1a1e 100644 --- a/app/assets/javascripts/discourse/components/search.js +++ b/app/assets/javascripts/discourse/components/search.js @@ -12,13 +12,20 @@ Discourse.Search = { @method forTerm @param {String} term The term to search for - @param {String} typeFilter An optional filter to restrict the search by type + @param {Object} opts Options for searching + @param {String} opts.typeFilter Filter our results to one type only + @param {Ember.Object} opts.searchContext data to help searching within a context (say, a category or user) @return {Promise} a promise that resolves the search results **/ - forTerm: function(term, typeFilter) { - return Discourse.ajax('/search', { - data: { term: term, type_filter: typeFilter } - }); + forTerm: function(term, opts) { + if (!opts) opts = {}; + + // Only include the data we have + var data = { term: term } + if (opts.typeFilter) data.typeFilter = opts.typeFilter; + if (opts.searchContext) data.search_context = opts.searchContext; + + return Discourse.ajax('/search', { data: data }); } } diff --git a/app/assets/javascripts/discourse/controllers/search_controller.js b/app/assets/javascripts/discourse/controllers/search_controller.js index f01cc4d68f5..c93fbc80a41 100644 --- a/app/assets/javascripts/discourse/controllers/search_controller.js +++ b/app/assets/javascripts/discourse/controllers/search_controller.js @@ -25,7 +25,12 @@ Discourse.SearchController = Em.ArrayController.extend(Discourse.Presence, { var searchController = this; this.set('count', 0); - return Discourse.Search.forTerm(term, typeFilter).then(function(results) { + var searcher = Discourse.Search.forTerm(term, { + typeFilter: typeFilter, + searchContext: searchController.get('searchContext') + }); + + return searcher.then(function(results) { searchController.set('results', results); if (results) { searchController.set('noResults', results.length === 0); diff --git a/app/assets/javascripts/discourse/models/category.js b/app/assets/javascripts/discourse/models/category.js index 96a995c5dd9..46bc8af4925 100644 --- a/app/assets/javascripts/discourse/models/category.js +++ b/app/assets/javascripts/discourse/models/category.js @@ -20,6 +20,10 @@ Discourse.Category = Discourse.Model.extend({ this.set("groups", Em.A(this.groups)); }, + searchContext: function() { + return ({ type: 'category', id: this.get('id') }); + }.property('id'), + url: function() { return Discourse.getURL("/category/") + (this.get('slug')); }.property('name'), diff --git a/app/assets/javascripts/discourse/models/topic.js b/app/assets/javascripts/discourse/models/topic.js index 43a0ad5bf0b..b6df4e317f7 100644 --- a/app/assets/javascripts/discourse/models/topic.js +++ b/app/assets/javascripts/discourse/models/topic.js @@ -13,10 +13,10 @@ Discourse.Topic = Discourse.Model.extend({ return this.get('participants').slice(0, 3); }.property('participants'), - canConvertToRegular: (function() { + canConvertToRegular: function() { var a = this.get('archetype'); return a !== 'regular' && a !== 'private_message'; - }).property('archetype'), + }.property('archetype'), convertArchetype: function(archetype) { var a = this.get('archetype'); @@ -29,6 +29,10 @@ Discourse.Topic = Discourse.Model.extend({ } }, + searchContext: function() { + return ({ type: 'topic', id: this.get('id') }); + }.property('id'), + category: function() { return Discourse.Category.list().findProperty('name', this.get('categoryName')); }.property('categoryName'), diff --git a/app/assets/javascripts/discourse/models/user.js b/app/assets/javascripts/discourse/models/user.js index 080bb6c6379..fa1f462f890 100644 --- a/app/assets/javascripts/discourse/models/user.js +++ b/app/assets/javascripts/discourse/models/user.js @@ -28,6 +28,10 @@ Discourse.User = Discourse.Model.extend({ return Discourse.Utilities.avatarUrl(this.get('username'), 'small', this.get('avatar_template')); }).property('username'), + searchContext: function() { + return ({ type: 'user', id: this.get('username_lower') }); + }.property('username_lower'), + /** This user's website. diff --git a/app/assets/javascripts/discourse/routes/list_category_route.js b/app/assets/javascripts/discourse/routes/list_category_route.js index 7ac95308382..a3fc1f59925 100644 --- a/app/assets/javascripts/discourse/routes/list_category_route.js +++ b/app/assets/javascripts/discourse/routes/list_category_route.js @@ -47,7 +47,7 @@ Discourse.ListCategoryRoute = Discourse.FilteredListRoute.extend({ this._super(); // Add a search context - this.controllerFor('search').set('searchContext', this.modelFor('listCategory')); + this.controllerFor('search').set('searchContext', this.modelFor('listCategory').get('searchContext')); }, deactivate: function() { diff --git a/app/assets/javascripts/discourse/routes/topic_route.js b/app/assets/javascripts/discourse/routes/topic_route.js index 65a3b2eafef..34eb934158e 100644 --- a/app/assets/javascripts/discourse/routes/topic_route.js +++ b/app/assets/javascripts/discourse/routes/topic_route.js @@ -25,7 +25,7 @@ Discourse.TopicRoute = Discourse.Route.extend({ Discourse.set('transient.lastTopicIdViewed', parseInt(topic.get('id'), 10)); // Set the search context - this.controllerFor('search').set('searchContext', topic); + this.controllerFor('search').set('searchContext', topic.get('searchContext')); }, deactivate: function() { diff --git a/app/assets/javascripts/discourse/routes/user_route.js b/app/assets/javascripts/discourse/routes/user_route.js index 8cb7991877b..d033f265406 100644 --- a/app/assets/javascripts/discourse/routes/user_route.js +++ b/app/assets/javascripts/discourse/routes/user_route.js @@ -28,7 +28,7 @@ Discourse.UserRoute = Discourse.Route.extend({ }); // Add a search context - this.controllerFor('search').set('searchContext', user); + this.controllerFor('search').set('searchContext', user.get('searchContext')); }, deactivate: function() { diff --git a/app/assets/javascripts/discourse/views/choose_topic_view.js b/app/assets/javascripts/discourse/views/choose_topic_view.js index e2c75e793f7..e0c4f4ebdce 100644 --- a/app/assets/javascripts/discourse/views/choose_topic_view.js +++ b/app/assets/javascripts/discourse/views/choose_topic_view.js @@ -26,7 +26,7 @@ Discourse.ChooseTopicView = Discourse.View.extend({ search: Discourse.debounce(function(title) { var chooseTopicView = this; - Discourse.Search.forTerm(title, 'topic').then(function (facets) { + Discourse.Search.forTerm(title, {typeFilter: 'topic'}).then(function (facets) { if (facets && facets[0] && facets[0].results) { chooseTopicView.set('topics', facets[0].results); } else { diff --git a/app/assets/javascripts/discourse/views/modal/edit_category_view.js b/app/assets/javascripts/discourse/views/modal/edit_category_view.js index 233a782c237..f67eab4f799 100644 --- a/app/assets/javascripts/discourse/views/modal/edit_category_view.js +++ b/app/assets/javascripts/discourse/views/modal/edit_category_view.js @@ -55,6 +55,7 @@ Discourse.EditCategoryView = Discourse.ModalBodyView.extend({ // background colors are available as a pipe-separated string backgroundColors: function() { + var categories = Discourse.Category.list(); return Discourse.SiteSettings.category_colors.split("|").map(function(i) { return i.toUpperCase(); }).concat( categories.map(function(c) { return c.color.toUpperCase(); }) ).uniq(); }.property('Discourse.SiteSettings.category_colors'),