web resource order added

This commit is contained in:
Harry Bomrah 2015-05-05 16:59:29 +08:00
parent 8cef8fc133
commit 5711618d63
8 changed files with 164 additions and 6 deletions

View File

@ -7,8 +7,12 @@ class Admin::WebResourcesController < OrbitAdminController
@categories = @module_app.categories.enabled @categories = @module_app.categories.enabled
@filter_fields = filter_fields(@categories, @tags) @filter_fields = filter_fields(@categories, @tags)
@table_fields = [:status, :category, :title] @table_fields = [:status, :category, :title]
if params[:sort].blank?
@links = WebLink.order_by(sort) s = {:order_position => "asc"}
else
s = sort
end
@links = WebLink.order_by(s)
.with_categories(filters("category")) .with_categories(filters("category"))
.with_tags(filters("tag")) .with_tags(filters("tag"))
.with_status(filters("status")) .with_status(filters("status"))
@ -18,6 +22,31 @@ class Admin::WebResourcesController < OrbitAdminController
render :partial => "index" if request.xhr? render :partial => "index" if request.xhr?
end end
def order
empty_position_links = WebLink.where(:order_position => nil)
if empty_position_links.count > 0
max_position = WebLink.max(:order_position)
max_position = 0 if max_position.nil?
empty_position_links.each_with_index do |epl,i|
epl.order_position = i + max_position
epl.save
end
end
@links = WebLink.all.asc(:order_position)
end
def updateorder
ids_with_order = params[:order]
ids_with_order.each_with_index do |id,index|
link = WebLink.find(id) rescue nil
if !link.nil?
link.order_position = index
link.save
end
end
render :json => {"success" => true}.to_json
end
def new def new
@tags =@module_app.tags @tags =@module_app.tags
@categories = @module_app.categories.enabled @categories = @module_app.categories.enabled
@ -29,6 +58,9 @@ class Admin::WebResourcesController < OrbitAdminController
link = WebLink.new(link_params) link = WebLink.new(link_params)
link.create_user_id = current_user.id link.create_user_id = current_user.id
link.update_user_id = current_user.id link.update_user_id = current_user.id
max_position = WebLink.max(:order_position)
max_position = -1 if max_position.nil?
link.order_position = max_position + 1
link.save link.save
redirect_to admin_web_resources_path redirect_to admin_web_resources_path
end end

View File

@ -2,7 +2,6 @@ class WebResourcesController < ApplicationController
def index def index
links = WebLink.where(:title.ne => "").can_display.filter_by_categories links = WebLink.where(:title.ne => "").can_display.filter_by_categories
web_link = links.collect do |link| web_link = links.collect do |link|
statuses = link.statuses_with_classname.collect do |status| statuses = link.statuses_with_classname.collect do |status|
{ {

View File

@ -10,7 +10,7 @@ class WebLink
field :title, localize: true field :title, localize: true
field :context, localize: true field :context, localize: true
field :order_position, type: Integer, default: -1
field :url, localize: true field :url, localize: true
field :create_user_id field :create_user_id
field :update_user_id field :update_user_id
@ -19,7 +19,7 @@ class WebLink
field :link_open, type: String field :link_open, type: String
LINK_OPEN_TYPES = ["local", "new_window"] LINK_OPEN_TYPES = ["local", "new_window"]
scope :can_display, ->{where(is_hidden: false).order_by([:is_top,:desc],[:created_at,:desc])} scope :can_display, ->{where(is_hidden: false).order_by([:is_top,:desc],[:order_position,:asc])}
before_save :add_http before_save :add_http

View File

@ -0,0 +1,117 @@
<% content_for :page_specific_css do %>
<%= stylesheet_link_tag "admin/web_links" %>
<% end %>
<table width="100%" id="web_resource_order_table">
<thead>
<th>Position</th>
<th>Link</th>
</thead>
<tbody>
<% @links.each_with_index do |link,i| %>
<tr>
<td class="position-text">
<div class="position-text-div" data-value="<%= (i + 1).to_s %>"><%= (i + 1).to_s %></div>
</td>
<td>
<div class="link-text-id" data-link-id="<%= link.id.to_s %>"><%= link.title %></div>
</td>
</tr>
<% end %>
</tbody>
</table>
<div class="bottomnav clearfix" style="left: 81px;">
<div class="action pull-right">
<a class="btn btn-info disabled" id="save-order-button" href="#">Save Order</a>
</div>
<div class="pagination pagination-centered"></div>
</div>
<script type="text/javascript">
var makeEditable = function(){
var input_box = $("<input type='text'/>"),
el = $(this);
input_box.val(el.data("value"));
input_box.attr("data-old-id",el.data("value"));
input_box.on("blur",function(){
putBackdiv($(this));
});
input_box.on("keypress",function(e){
if(e.keyCode == 13 || e.keyCode == 27){
putBackdiv($(this),e.keyCode);
}
})
el.parent().html(input_box);
input_box.focus();
}
var putBackdiv = function(el,keyCode){
current_value = parseInt((el.val() == "" ? el.data("old-id") : el.val())),
old_value = parseInt(el.data("old-id"));
if(isNaN(current_value) || keyCode == 27){
current_value = old_value;
}
if(old_value != current_value){
var new_index_value = (current_value > old_value ? current_value + 1 : current_value - 1),
div = $("<div class='position-text-div' data-value='" + current_value + "'>" + new_index_value + "</div>");
div.on("click",makeEditable);
el.parent().html(div);
$("#save-order-button").removeClass("disabled");
sortTable(el.data("old-id"),current_value);
}else{
var div = $("<div class='position-text-div' data-value='" + current_value + "'>" + current_value + "</div>");
div.on("click",makeEditable);
el.parent().html(div);
}
}
var sortTable = function(changed_index,changed_to){
var table_element = document.getElementById("web_resource_order_table"),
data = [],
allRows = table_element.rows;
for(i = 1; i < allRows.length; i++){
var thisRow = allRows[i],
text = thisRow.cells[0].textContent.trim(),
hash = {};
hash.index = parseInt(text);
text = thisRow.cells[1].innerHTML.trim();
if(text != "&nbsp;"){
hash.link = text;
}
data.push(hash);
}
data = data.sort(function(a,b){return a.index > b.index});
renderSortedTable(data,table_element);
}
var renderSortedTable = function(data,table_element){
var allRows = table_element.rows;
for(i = 0;i < data.length; i++){
var thisRow = allRows[i + 1],
current_value = i + 1;
thisRow.cells[0].innerHTML = "<div class='position-text-div' data-value='" + current_value + "'>" + current_value + "</div>";
thisRow.cells[1].innerHTML = data[i].link;
}
$("#web_resource_order_table div.position-text-div").on("click",makeEditable);
}
$("#save-order-button").on("click",function(){
var el = $(this);
if(!el.hasClass("disabled")){
var data = [];
$("#web_resource_order_table .link-text-id").each(function(){
data.push($(this).data("link-id"));
})
$.ajax({
url : "/admin/web_resources/updateorder",
data : {"order" : data},
dataType : "json",
type : "post"
}).done(function(){
el.addClass("disabled");
})
}
})
$("#web_resource_order_table div.position-text-div").on("click",makeEditable);
</script>

View File

@ -5,4 +5,5 @@ en:
web_link: web_link:
local: Open In Current page local: Open In Current page
new_window: Open In New window new_window: Open In New window
type: Link Open Type type: Link Open Type
order: Order

View File

@ -6,3 +6,4 @@ zh_tw:
local: 當前視窗開啟 local: 當前視窗開啟
new_window: 新視窗開啟 new_window: 新視窗開啟
type: 開啟方式 type: 開啟方式
order: Order

View File

@ -4,6 +4,8 @@ locales = Site.find_by(site_active: true).in_use_locales rescue I18n.available_l
scope "(:locale)", locale: Regexp.new(locales.join("|")) do scope "(:locale)", locale: Regexp.new(locales.join("|")) do
namespace :admin do namespace :admin do
get "/web_resources/order" => "web_resources#order"
post "/web_resources/updateorder" => "web_resources#updateorder"
resources :web_resources resources :web_resources
end end
end end

View File

@ -30,6 +30,12 @@ module WebResource
:active_for_action=>{'admin/web_resources'=>'new'}, :active_for_action=>{'admin/web_resources'=>'new'},
:available_for => 'sub_managers' :available_for => 'sub_managers'
context_link 'web_link.order',
:link_path=>"admin_web_resources_order_path" ,
:priority=>2,
:active_for_action=>{'admin/web_resources'=>'order'},
:available_for => 'managers'
context_link 'categories', context_link 'categories',
:link_path=>"admin_module_app_categories_path" , :link_path=>"admin_module_app_categories_path" ,
:link_arg=>"{:module_app_id=>ModuleApp.find_by(:key=>'web_resource').id}", :link_arg=>"{:module_app_id=>ModuleApp.find_by(:key=>'web_resource').id}",