From 4cf1d9c2667b80e8cc18b2929b14a6b938cc16e3 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 29 May 2013 12:19:03 -0400 Subject: [PATCH] Display details about the search context in the placeholder --- .../javascripts/discourse/models/category.js | 2 +- .../javascripts/discourse/models/user.js | 2 +- .../discourse/routes/user_route.js | 6 ++-- .../discourse/templates/search.js.handlebars | 2 +- .../discourse/views/discourse_text_field.js | 4 +-- .../views/search/search_text_field.js | 33 +++++++++++++++++++ config/locales/client.en.yml | 4 +++ 7 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 app/assets/javascripts/discourse/views/search/search_text_field.js diff --git a/app/assets/javascripts/discourse/models/category.js b/app/assets/javascripts/discourse/models/category.js index af90a532fa0..abe1efeb4e3 100644 --- a/app/assets/javascripts/discourse/models/category.js +++ b/app/assets/javascripts/discourse/models/category.js @@ -15,7 +15,7 @@ Discourse.Category = Discourse.Model.extend({ }, searchContext: function() { - return ({ type: 'category', id: this.get('id') }); + return ({ type: 'category', id: this.get('id'), category: this }); }.property('id'), url: function() { diff --git a/app/assets/javascripts/discourse/models/user.js b/app/assets/javascripts/discourse/models/user.js index a4af0adc8cc..68850c9124b 100644 --- a/app/assets/javascripts/discourse/models/user.js +++ b/app/assets/javascripts/discourse/models/user.js @@ -29,7 +29,7 @@ Discourse.User = Discourse.Model.extend({ }).property('username'), searchContext: function() { - return ({ type: 'user', id: this.get('username_lower') }); + return ({ type: 'user', id: this.get('username_lower'), user: this }); }.property('username_lower'), /** diff --git a/app/assets/javascripts/discourse/routes/user_route.js b/app/assets/javascripts/discourse/routes/user_route.js index d033f265406..c4a1530af0b 100644 --- a/app/assets/javascripts/discourse/routes/user_route.js +++ b/app/assets/javascripts/discourse/routes/user_route.js @@ -18,6 +18,9 @@ Discourse.UserRoute = Discourse.Route.extend({ setupController: function(controller, user) { user.findDetails(); + + // Add a search context + this.controllerFor('search').set('searchContext', user.get('searchContext')); }, activate: function() { @@ -26,9 +29,6 @@ Discourse.UserRoute = Discourse.Route.extend({ Discourse.MessageBus.subscribe("/users/" + user.get('username_lower'), function(data) { user.loadUserAction(data); }); - - // Add a search context - this.controllerFor('search').set('searchContext', user.get('searchContext')); }, deactivate: function() { diff --git a/app/assets/javascripts/discourse/templates/search.js.handlebars b/app/assets/javascripts/discourse/templates/search.js.handlebars index 17ce55b86de..480624f965d 100644 --- a/app/assets/javascripts/discourse/templates/search.js.handlebars +++ b/app/assets/javascripts/discourse/templates/search.js.handlebars @@ -1,4 +1,4 @@ -{{textField value=term placeholderKey="search.placeholder"}} +{{view Discourse.SearchTextField valueBinding="term" searchContextBinding="searchContext"}} {{#unless loading}} {{#unless noResults}} {{#each resultType in content}} diff --git a/app/assets/javascripts/discourse/views/discourse_text_field.js b/app/assets/javascripts/discourse/views/discourse_text_field.js index 19d6953d777..5f0199faf26 100644 --- a/app/assets/javascripts/discourse/views/discourse_text_field.js +++ b/app/assets/javascripts/discourse/views/discourse_text_field.js @@ -9,13 +9,13 @@ Discourse.TextField = Ember.TextField.extend({ attributeBindings: ['autocorrect', 'autocapitalize', 'autofocus'], - placeholder: (function() { + placeholder: function() { if( this.get('placeholderKey') ) { return Em.String.i18n(this.get('placeholderKey')); } else { return ''; } - }).property('placeholderKey') + }.property('placeholderKey') }); diff --git a/app/assets/javascripts/discourse/views/search/search_text_field.js b/app/assets/javascripts/discourse/views/search/search_text_field.js new file mode 100644 index 00000000000..1dbd155d363 --- /dev/null +++ b/app/assets/javascripts/discourse/views/search/search_text_field.js @@ -0,0 +1,33 @@ +/** + This is a text field that supports a dynamic placeholder based on search context. + + @class SearchTextField + @extends Discourse.TextField + @namespace Discourse + @module Discourse +**/ +Discourse.SearchTextField = Discourse.TextField.extend({ + + /** + A dynamic placeholder for the search field based on our context + + @property placeholder + **/ + placeholder: function() { + + var ctx = this.get('searchContext'); + if (ctx) { + switch(Em.get(ctx, 'type')) { + case 'user': + return Em.String.i18n('search.prefer.user', {username: Em.get(ctx, 'user.username')}); + case 'category': + return Em.String.i18n('search.prefer.category', {category: Em.get(ctx, 'category.name')}); + } + } + + return Em.String.i18n('search.placeholder'); + }.property('searchContext') + +}); + + diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index d99beeb4e79..652add6a3ba 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -435,6 +435,10 @@ en: no_results: "No results found." searching: "Searching ..." + prefer: + user: "search will prefer results by @{{username}}" + category: "search will prefer results in {{category}}" + site_map: "go to another topic list or category" go_back: 'go back' current_user: 'go to your user page'