search autocomplete
This commit is contained in:
parent
47ee517b27
commit
424906baff
|
@ -9,5 +9,20 @@ class ArtistController < ApplicationController
|
||||||
end
|
end
|
||||||
@artist = Artist.getByName(name)
|
@artist = Artist.getByName(name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def autocomplete
|
||||||
|
autocomplete = Artist.getLastFmAutocomplete(params[:query])
|
||||||
|
return render :nothing => true if autocomplete.nil?
|
||||||
|
suggestions = []
|
||||||
|
pp autocomplete["response"]
|
||||||
|
autocomplete["response"]["docs"].each do |doc|
|
||||||
|
suggestions << doc["artist"] unless suggestions.include?(doc["artist"])
|
||||||
|
end
|
||||||
|
|
||||||
|
render :json => {
|
||||||
|
:query => params[:query],
|
||||||
|
:suggestions => suggestions
|
||||||
|
}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,19 @@
|
||||||
class Artist < ActiveRecord::Base
|
class Artist < ActiveRecord::Base
|
||||||
set_table_name 'musicbrainz.bh_artist'
|
set_table_name 'musicbrainz.bh_artist'
|
||||||
has_many :albums, :conditions => "release_type = 1", :order => 'year ASC, id ASC'
|
has_many :albums, :conditions => "release_type = 1", :order => 'year ASC, id ASC'
|
||||||
|
|
||||||
def self.getByName(name)
|
def self.getByName(name)
|
||||||
Artist.first(:conditions => ['name = ? AND id=credit_id', name], :order => 'rating DESC')
|
Artist.first(:conditions => ['name = ? AND id=credit_id', name], :order => 'rating DESC')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.getLastFmAutocomplete(query)
|
||||||
|
return nil if query.nil? or query.strip.empty?
|
||||||
|
|
||||||
|
json = ActiveSupport::JSON.decode(open(
|
||||||
|
'http://www.last.fm/search/autocomplete' <<
|
||||||
|
'?q=' << URI.escape(query)
|
||||||
|
).read)
|
||||||
|
return nil if json.empty? else return json
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,17 @@
|
||||||
|
<script type="text/javascript">
|
||||||
|
jQuery(function(){
|
||||||
|
var ac = $('#name').autocomplete({
|
||||||
|
serviceUrl: 'search/autocomplete', // Страница для обработки запросов автозаполнения
|
||||||
|
minChars: 2, // Минимальная длина запроса для срабатывания автозаполнения
|
||||||
|
delimiter: /(,|;)\s*/, // Разделитель для нескольких запросов, символ или регулярное выражение
|
||||||
|
maxHeight: 400, // Максимальная высота списка подсказок, в пикселях
|
||||||
|
width: 300, // Ширина списка
|
||||||
|
zIndex: 9999, // z-index списка
|
||||||
|
deferRequestBy: 150, // Задержка запроса (мсек)
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
<div class="search">
|
<div class="search">
|
||||||
<h1>SEARCH</h1>
|
<h1>SEARCH</h1>
|
||||||
<%= form_tag('/artist', :method => 'post') %>
|
<%= form_tag('/artist', :method => 'post') %>
|
||||||
|
|
|
@ -59,6 +59,7 @@ Beathaven::Application.routes.draw do
|
||||||
# Note: This route will make all actions in every controller accessible via GET requests.
|
# Note: This route will make all actions in every controller accessible via GET requests.
|
||||||
# match ':controller(/:action(/:id(.:format)))'
|
# match ':controller(/:action(/:id(.:format)))'
|
||||||
match 'artist(/:name)' => 'artist#view'
|
match 'artist(/:name)' => 'artist#view'
|
||||||
match 'listen/:id' => 'track#listen'
|
match 'listen/:id' => 'track#listen'
|
||||||
|
match 'search/autocomplete' => 'artist#autocomplete'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -110,6 +110,7 @@ body {
|
||||||
}
|
}
|
||||||
|
|
||||||
.search #name {
|
.search #name {
|
||||||
|
width: 300px;
|
||||||
border: 1px solid ##bdbdbd;
|
border: 1px solid ##bdbdbd;
|
||||||
background: #555555;
|
background: #555555;
|
||||||
color: #ffffff;
|
color: #ffffff;
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
|
|
||||||
.autocomplete-w1 { background:url(img/shadow.png) no-repeat bottom right; position:absolute; top:0px; left:0px; margin:8px 0 0 6px; /* IE6 fix: */ _background:none; _margin:0; }
|
.autocomplete-w1 { background:url(../images/shadow.png) no-repeat bottom right; position:absolute; top:0px; left:0px; margin:8px 0 0 6px; /* IE6 fix: */ _background:none; _margin:0; }
|
||||||
.autocomplete { border:1px solid #999; background:#FFF; cursor:default; text-align:left; max-height:350px; overflow:auto; margin:-6px 6px 6px -6px; /* IE6 specific: */ _height:350px; _margin:0; _overflow-x:hidden; }
|
.autocomplete { border:1px solid #999; background:#FFF; cursor:default; text-align:left; max-height:350px; overflow:auto; margin:-6px 6px 6px -6px; /* IE6 specific: */ _height:350px; _margin:0; _overflow-x:hidden; }
|
||||||
.autocomplete .selected { background:#F0F0F0; }
|
.autocomplete .selected { background:#F0F0F0; }
|
||||||
.autocomplete div { padding:2px 5px; white-space:nowrap; }
|
.autocomplete div { padding:2px 5px; white-space:nowrap; }
|
||||||
.autocomplete strong { font-weight:normal; color:#3399FF; }
|
.autocomplete strong { font-weight:normal; color:#3399FF; }
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue