Add tags filter for frontend

This commit is contained in:
manson 2014-07-11 18:30:52 +08:00
parent 843dcef9f8
commit fca37fdeb9
2 changed files with 38 additions and 12 deletions

View File

@ -1,7 +1,8 @@
class AnnouncementsController < ApplicationController class AnnouncementsController < ApplicationController
def index def index
announcements = Bulletin.where(:is_preview.in=>[false,nil]).can_display.order_by(:created_at=>'desc').filter_by_categories announcements = Bulletin.where(:is_preview.in=>[false,nil]).can_display.order_by(:created_at=>'desc').filter_by_categories.filter_by_tags(OrbitHelper.params['tags'])
anns = announcements.collect do |a| anns = announcements.collect do |a|
statuses = a.statuses_with_classname.collect do |status| statuses = a.statuses_with_classname.collect do |status|
{ {
@ -16,19 +17,22 @@ class AnnouncementsController < ApplicationController
"postdate" => a.postdate.strftime("%b %d, %Y at %I:%M %p"), "postdate" => a.postdate.strftime("%b %d, %Y at %I:%M %p"),
"link_to_show" => OrbitHelper.url_to_show(a.to_param), "link_to_show" => OrbitHelper.url_to_show(a.to_param),
"img_src" => a.image.thumb.url || "http://placehold.it/100x100", "img_src" => a.image.thumb.url || "http://placehold.it/100x100",
"more" => "More" "more" => t(:more_plus)
} }
end end
{ {
"announcements" => anns, "announcements" => anns,
"extras" => {"widget-title"=>"Announcements"}, "extras" => {
"widget-title"=>t('announcement.announcement')
},
"total_pages" => announcements.total_pages "total_pages" => announcements.total_pages
} }
end end
def widget def widget
announcements = Bulletin.where(:is_preview.in=>[false,nil]).can_display.order_by(:created_at=>'desc').filter_by_widget_categories announcements = Bulletin.where(:is_preview.in=>[false,nil]).can_display.order_by(:created_at=>'desc').filter_by_widget_categories.filter_by_tags(OrbitHelper.widget_tags)
anns = announcements.collect do |a| anns = announcements.collect do |a|
statuses = a.statuses_with_classname.collect do |status| statuses = a.statuses_with_classname.collect do |status|
{ {
@ -47,7 +51,9 @@ class AnnouncementsController < ApplicationController
end end
{ {
"announcements" => anns, "announcements" => anns,
"extras" => {"widget-title"=>"Announcements","more_url"=>OrbitHelper.widget_more_url} "extras" => {
"more_url"=>OrbitHelper.widget_more_url
}
} }
end end
@ -57,7 +63,10 @@ class AnnouncementsController < ApplicationController
announcement = Bulletin.can_display.find_by(:uid=>params[:uid]) announcement = Bulletin.can_display.find_by(:uid=>params[:uid])
tags = announcement.tags.map{|tag| { "tag" => tag.name } } rescue [] tags = announcement.tags.map{|tag| {
"tag" => tag.name ,
"url" => OrbitHelper.page_for_tag(tag)
} } rescue []
files = announcement.bulletin_files.map{|file| { "file_url" => file.file.url, "file_title" => (file.title.blank? ? File.basename(file.file.path) : file.title) } } rescue [] files = announcement.bulletin_files.map{|file| { "file_url" => file.file.url, "file_title" => (file.title.blank? ? File.basename(file.file.path) : file.title) } } rescue []
links = announcement.bulletin_links.map{|link| { "link_url" => link.url, "link_title" => (link.title.blank? ? link.url : link.title) } } rescue [] links = announcement.bulletin_links.map{|link| { "link_url" => link.url, "link_title" => (link.title.blank? ? link.url : link.title) } } rescue []
update_user = announcement.update_user.member_profile.name rescue "" update_user = announcement.update_user.member_profile.name rescue ""

View File

@ -13,7 +13,7 @@
<div class="accordion-body collapse" id="collapse-<%= field %>"> <div class="accordion-body collapse" id="collapse-<%= field %>">
<div class="accordion-inner pagination-right" data-toggle="buttons-checkbox"> <div class="accordion-inner pagination-right" data-toggle="buttons-checkbox">
<% @filter_fields[field].each do |val| %> <% @filter_fields[field].each do |val| %>
<%= link_to t(val[:title]), "#", :onclick => "filter.addFilter('filters[#{field}][]=#{val[:id]}')", :class => "btn btn-small #{is_filter_active?(field, val[:id])}" %> <%= link_to t(val[:title]), "#", :onclick => "filter.addFilter('filters[#{field}][]=#{val[:id]}')", :class => "btn btn-small #{is_filter_active?(field, val[:id])}", :id => "filter_#{val[:id]}" %>
<% end %> <% end %>
</div> </div>
<div class="filter-clear"> <div class="filter-clear">
@ -31,21 +31,27 @@
<%= render 'layouts/delete_modal', delete_options: @delete_options %> <%= render 'layouts/delete_modal', delete_options: @delete_options %>
<script type="text/javascript"> <script type="text/javascript">
var update = false;
var Filter = function(dom){ var Filter = function(dom){
var makeFilters = function(){ var makeFilters = function(){
return (window.location.search ? window.location.search.replace('?','').split('&') : []); return (window.location.search ? window.location.search.replace('?','').split('&') : []);
} }
var filters = makeFilters(), var filters = makeFilters(),
dom = $(dom), dom = $(dom),
mainUrl = window.location.pathname; mainUrl = window.location.pathname;
var updateTable = function(url){ var updateTable = function(url, goback){
xurl = (url == null ? ( filters.length ? mainUrl + "?" + filters.join('&') : mainUrl ) : null); update = true;
xurl = (url == null ? ( filters.length ? mainUrl + "?" + filters.join('&') : mainUrl ) : url);
$.ajax({ $.ajax({
url : xurl, url : xurl,
type : "get", type : "get",
dataType : "html" dataType : "html"
}).done(function(data){ }).done(function(data){
history.pushState(null, null, decodeURIComponent(xurl)); if(!goback){
history.replaceState(null, null, decodeURIComponent(xurl));
}
filters = makeFilters(); filters = makeFilters();
dom.html(data); dom.html(data);
$(".pagination a").click(function(){ $(".pagination a").click(function(){
@ -76,6 +82,17 @@
updateTable(); updateTable();
return false; return false;
} }
window.onpopstate = function(event){
if(!update){
updateTable(document.location.href, true);
$(".filter-group .btn-small").removeClass('active');
$.each(document.location.search.split('&'),function(key,filter){
$('#filter_'+filter.split('=')[1]).addClass('active');
});
}
update = false;
}
} }
var filter = new Filter("#index_table"); var filter = new Filter("#index_table");
</script> </script>