Make the tag autocomplete on the getting started page less broken

This commit is contained in:
Raphael Sofaer 2011-08-09 18:55:23 -07:00
parent 6406c59bc7
commit 902edb6745
4 changed files with 41 additions and 31 deletions

View file

@ -13,22 +13,27 @@ class TagsController < ApplicationController
respond_to :json, :only => [:index] respond_to :json, :only => [:index]
def index def index
if params[:q] && params[:q].length > 1 if params[:q] && params[:q].length > 1 && request.format.json?
params[:q].gsub!("#", "") params[:q].gsub!("#", "")
params[:limit] = !params[:limit].blank? ? params[:limit].to_i : 10 params[:limit] = !params[:limit].blank? ? params[:limit].to_i : 10
@tags = ActsAsTaggableOn::Tag.named_like(params[:q]).limit(params[:limit] - 1) @tags = ActsAsTaggableOn::Tag.named_like(params[:q]).limit(params[:limit] - 1)
@array = [] @tags.map! do |obj|
@tags.each do |obj| { :name => ("#"+obj.name),
@array << { :name => ("#"+obj.name), :value => ("#"+obj.name),
:value => ("#"+obj.name)} :url => tag_path(obj.name)
}
end end
@array << { :name => ('#' + params[:q]), :value => ("#" + params[:q])} @tags << {
@array.uniq! :name => ('#' + params[:q]),
:value => ("#" + params[:q]),
:url => tag_path(params[:q].downcase)
}
@tags.uniq!
respond_to do |format| respond_to do |format|
format.json{ format.json{
render(:json => @array.to_json, :status => 200) render(:json => @tags.to_json, :status => 200)
} }
end end
else else

View file

@ -18,7 +18,7 @@
- elsif (current_user || !@landing_page) - elsif (current_user || !@landing_page)
#global_search #global_search
= form_tag(people_path, :method => 'get', :id => "global_search_form") do = form_tag(people_path, :method => 'get', :class => "search_form") do
= text_field_tag 'q', nil, :placeholder => t('find_people'), :type => 'search', :results => 5 = text_field_tag 'q', nil, :placeholder => t('find_people'), :type => 'search', :results => 5
- if @notification_count - if @notification_count

View file

@ -105,8 +105,7 @@
%h4 %h4
= t('.find_friends') = t('.find_friends')
.span-5.append-1 .span-5.append-1
#global_search = form_tag(people_path, :method => 'get', :class => "search_form") do
= form_tag(people_path, :method => 'get', :id => "global_search_form") do
= text_field_tag 'q', nil, :placeholder => "Search for people", :type => 'search', :results => 5 = text_field_tag 'q', nil, :placeholder => "Search for people", :type => 'search', :results => 5
.span-5.last{:style => "height:30px;"} .span-5.last{:style => "height:30px;"}
%h4{:style => "margin-top:7px;"} %h4{:style => "margin-top:7px;"}
@ -127,8 +126,7 @@
= t('.hashtag_explanation') = t('.hashtag_explanation')
.span-5.append-1 .span-5.append-1
#global_search = form_tag(tags_path, :method => 'get', :class => "search_form") do
= form_tag(people_path, :method => 'get', :id => "global_search_form") do
= text_field_tag 'q', nil, :placeholder => "Search for #hashtags", :type => 'search', :results => 5 = text_field_tag 'q', nil, :placeholder => "Search for #hashtags", :type => 'search', :results => 5
.span-5.last .span-5.last
%h4{:style => "margin-top:7px;"} %h4{:style => "margin-top:7px;"}

View file

@ -1,10 +1,9 @@
var Search = { var Search = {
source : '/people.json', selector : '.search_form input[type="search"]',
selector : '#global_search input#q',
formatItem: function(row){ formatItem: function(row){
if(row['search']) { if(row['search']) {
return $.mustache(Diaspora.widgets.i18n.t('search_for'), { name: row['name'] }); var data = { name: this.element.val() }
return Diaspora.widgets.i18n.t('search_for', data);
} else { } else {
return "<img src='"+ row['avatar'] +"' class='avatar'/>" + row['name']; return "<img src='"+ row['avatar'] +"' class='avatar'/>" + row['name'];
} }
@ -16,18 +15,19 @@ var Search = {
results = data.map(function(person){ results = data.map(function(person){
return {data : person, value : person['name']} return {data : person, value : person['name']}
}); });
results.push(Search.searchLinkli()); results.push(Search.searchLinkli.apply(this));
return results; return results;
}, },
selectItemCallback : function(event, data, formatted) { selectItemCallback : function(element, data, formatted) {
if (data['id'] !== undefined) { // actual result if (data['search'] === true) { // The placeholder "search for" result
$(Search.selector).val(formatted); window.location = this.element.parents("form").attr("action") + '?' + this.element.attr("name") +'=' + data['name'];
} else { // The actual result
element.val(formatted);
window.location = data['url']; window.location = data['url'];
} else { //use form val to eliminate timing issue
window.location = '/people?q='+$(Search.selector).val();
} }
}, },
options : function(){return { options : function(element){return {
element: element,
minChars : 2, minChars : 2,
onSelect: Search.selectItemCallback, onSelect: Search.selectItemCallback,
max : 5, max : 5,
@ -41,11 +41,14 @@ var Search = {
};}, };},
searchLinkli : function() { searchLinkli : function() {
var searchTerm = $(Search.selector).val(); var searchTerm = this.element.val();
if(searchTerm[0] === "#"){
searchTerm = searchTerm.slice(1);
}
return { return {
data : { data : {
'search' : true, 'search' : true,
'url' : '/people?q=' + searchTerm, 'url' : this.element.parents("form").attr("action") + '?' + this.element.attr("name") +'=' + searchTerm,
'name' : searchTerm 'name' : searchTerm
}, },
value : searchTerm value : searchTerm
@ -53,7 +56,11 @@ var Search = {
}, },
initialize : function() { initialize : function() {
$(Search.selector).autocomplete(Search.source, Search.options()); $(Search.selector).each(function(index, element){
var $element = $(element);
var action = $element.parents("form").attr("action") + '.json';
$element.autocomplete(action, Search.options($element));
});
} }
} }