order interface added to adbanner

This commit is contained in:
Harry Bomrah 2016-02-16 20:21:14 +08:00
parent 6c019a088b
commit 1db7df8f62
5 changed files with 117 additions and 14 deletions

View File

@ -1,7 +1,7 @@
class AdBannersController < ApplicationController
def widget
adbanner = Banner.find(OrbitHelper.widget_custom_value)
images = adbanner.ad_images.can_display.desc(:created_at).collect.with_index do |b,i|
images = adbanner.ad_images.can_display.asc(:sort_number,:created_at).collect.with_index do |b,i|
image_link = OrbitHelper.is_mobile_view ? b.file.mobile.url : b.file.url
klass = i == 0 ? "active" : ""
caption = i == 0 ? '<div class="cycle-overlay"></div><div class="cycle-pager"></div>' : ""

View File

@ -12,14 +12,18 @@ class Admin::AdBannersController < OrbitAdminController
.with_categories(filters("category"))
@banners = search_data(@banners,[:title]).page(params[:page]).per(6)
if request.xhr?
if request.xhr?
render :partial => "index_table"
end
end
def show
@banner = Banner.find(params[:id])
@images = @banner.ad_images.desc(:created_at).page(params[:page]).per(10)
if params[:show] == "expired"
@images = @banner.ad_images.is_expired.asc(:sort_number).page(params[:page]).per(10)
else
@images = @banner.ad_images.not_expired.asc(:sort_number).page(params[:page]).per(10)
end
@table_fields = [:banner, :title, "ad_banner.duration", :link]
end
@ -52,6 +56,18 @@ class Admin::AdBannersController < OrbitAdminController
ad_banner.destroy
redirect_to admin_ad_banners_path(:page => params[:page])
end
def save_image_order
ids = params[:ids]
ids.each_with_index do |id,index|
image = AdImage.find(id) rescue nil
if !image.nil?
image.sort_number = index
image.save
end
end
render :json => {"success" => true}.to_json
end
private

View File

@ -13,6 +13,8 @@ class AdImage
field :link_open, type: String
field :postdate , :type => DateTime, :default => Time.now
field :deadline , :type => DateTime
field :sort_number, :type => Integer
LINK_OPEN_TYPES = ["local", "new_window"]
belongs_to :banner
@ -23,6 +25,8 @@ class AdImage
# validates :title, presence: true
scope :can_display, ->{self.and(AdImage.or({:postdate.lte=>Time.now},{:postdate=>nil}).selector,AdImage.or({:deadline.gte=>Time.now},{:deadline=>nil}).selector)}
scope :is_expired, ->{self.and(AdImage.or({:deadline.lte=>Time.now}).selector)}
scope :not_expired, ->{self.and(AdImage.or({:deadline.gte=>Time.now},{:deadline=>nil}).selector)}
def expired?
self.deadline<Time.now rescue false

View File

@ -1,12 +1,39 @@
<% content_for :page_specific_javascript do %>
<%= javascript_include_tag "lib/jquery-ui-sortable.min" %>
<% end %>
<% content_for :page_specific_css do %>
<style type="text/css">
.banner-image{
height: 140px;
background-repeat: no-repeat;
background-repeat: no-repeat;
}
.order-list{
list-style: none;
}
.order-list-image i{
font-size: 20px;
cursor: move;
}
.order-list-image img{
margin-left: 10px;
width: 75px;
margin-bottom: 5px;
}
.order-list-image h4{
display: inline-block;
margin-left: 30px;
}
</style>
<% end %>
<h3><%= @banner.title %></h3>
<h3>
<%= @banner.title %>
<div class="pull-right btn-group">
<a class="btn btn-small <%= params[:show] == "expired" ? "" : "active" %>" href="<%= admin_ad_banner_path(@banner.id) %>">Not Expired</a>
<a class="btn btn-small <%= params[:show] == "expired" ? "active" : "" %>" href="<%= admin_ad_banner_path(@banner.id, :show => "expired") %>">Expired</a>
</div>
</h3>
<table class="table main-list">
<thead>
<tr class="sort-header">
@ -50,14 +77,68 @@
<% end %>
</tbody>
</table>
<% if can_edit_or_delete?(@banner) %>
<div class="bottomnav clearfix" style="left: 81px;">
<div class="action pull-right">
<a class="btn btn-primary" href="<%= new_admin_ad_image_path(:page => params[:page], :banner_id => @banner.id) %>">
<%= t("ad_banner.add") %>
</a>
<div class="bottomnav clearfix" style="left: 81px;">
<div class="action pull-right">
<a class="btn btn-primary" href="<%= new_admin_ad_image_path(:page => params[:page], :banner_id => @banner.id) %>">
<%= t("ad_banner.add") %>
</a>
<a class="btn btn-info" href="#sort-images-modal" data-toggle="modal">Order</a>
</div>
<%= content_tag :div, paginate(@images), class: "pagination pagination-centered" %>
</div>
<%= content_tag :div, paginate(@images), class: "pagination pagination-centered" %>
</div>
<% end %>
<!-- image order modal -->
<div id="sort-images-modal" class="modal hide fade" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3>Order Images</h3>
</div>
<div class="modal-body">
<ul class="order-list">
<% @banner.ad_images.not_expired.asc(:sort_number).each do |image| %>
<li class="order-list-image" data-image-id="<%= image.id.to_s %>">
<i class="icons-list-2"></i>
<img src="<%= image.file.thumb.url %>"/>
<h4><%= image.title %>
</li>
<% end %>
</ul>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button class="btn btn-primary" id="save-image-order-btn">Save changes</button>
</div>
</div>
<% end %>
<script type="text/javascript">
var sortUpdated = false;
$("#sort-images-modal").on("shown",function(){
$(".order-list").sortable({
update : function(){
sortUpdated = true;
}
});
})
$("#save-image-order-btn").on("click",function(){
if(sortUpdated){
var ids = [];
$(".order-list-image").each(function(i,image){
ids.push($(image).data("image-id"));
})
$.ajax({
url : "/admin/ad_banner/save_image_order",
data : {"ids" : ids, "banner_id" : "<%= @banner.id.to_s %>"},
dataType : "json",
type : "post"
}).done(function(){
alert("Order saved. Please refresh the page to see the changes.");
$("#sort-images-modal").modal("hide");
})
}
})
</script>

View File

@ -6,6 +6,8 @@ locales = Site.find_by(site_active: true).in_use_locales rescue I18n.available_l
scope "(:locale)", locale: Regexp.new(locales.join("|")) do
namespace :admin do
post "/ad_banner/save_image_order", to: "ad_banners#save_image_order"
resources :ad_banners
resources :ad_images
end