Add category filter to order page, fix order position logic, redirect back after create/update
This commit is contained in:
parent
681bbbec87
commit
76d317d9bb
|
|
@ -32,15 +32,22 @@ class Admin::WebResourcesController < OrbitAdminController
|
|||
epl.save
|
||||
end
|
||||
end
|
||||
@links = WebLink.all.asc(:order_position)
|
||||
@categories = @module_app.categories.enabled
|
||||
module_app = @categories[0].module_app rescue nil
|
||||
@filter_fields = {
|
||||
:category => @categories.sort_by{|c| (((module_app.asc rescue true) ? c.sort_number : -c.sort_number) rescue c.id)}.map{|c| {:title=>(c.title.blank? ? " " : c.title), :id=>c.id}}
|
||||
}
|
||||
@links = WebLink.all.with_categories(filters("category")).asc(:order_position)
|
||||
render :partial => "order" if request.xhr?
|
||||
end
|
||||
|
||||
|
||||
def updateorder
|
||||
ids_with_order = params[:order]
|
||||
ids_with_order.each_with_index do |id,index|
|
||||
positions = WebLink.in(id: ids_with_order).asc(:order_position).map(&:order_position)
|
||||
ids_with_order.each_with_index do |id, index|
|
||||
link = WebLink.find(id) rescue nil
|
||||
if !link.nil?
|
||||
link.order_position = index
|
||||
link.order_position = positions[index]
|
||||
link.save
|
||||
end
|
||||
end
|
||||
|
|
@ -48,6 +55,7 @@ class Admin::WebResourcesController < OrbitAdminController
|
|||
end
|
||||
|
||||
def new
|
||||
session[:return_to] = request.referer
|
||||
@tags =@module_app.tags
|
||||
@categories = @module_app.categories.enabled
|
||||
@statuses = []
|
||||
|
|
@ -62,10 +70,11 @@ class Admin::WebResourcesController < OrbitAdminController
|
|||
max_position = -1 if max_position.nil?
|
||||
link.order_position = max_position + 1
|
||||
link.save
|
||||
redirect_to admin_web_resources_path
|
||||
redirect_to session.delete(:return_to) || admin_web_resources_path
|
||||
end
|
||||
|
||||
def edit
|
||||
session[:return_to] = request.referer
|
||||
if can_edit_or_delete?(@link)
|
||||
@tags =@module_app.tags
|
||||
@categories = @module_app.categories.enabled
|
||||
|
|
@ -78,7 +87,7 @@ class Admin::WebResourcesController < OrbitAdminController
|
|||
def update
|
||||
@link.update_attributes(link_params)
|
||||
@link.save
|
||||
redirect_to admin_web_resources_path
|
||||
redirect_to session.delete(:return_to) || admin_web_resources_path
|
||||
end
|
||||
|
||||
def destroy
|
||||
|
|
|
|||
|
|
@ -0,0 +1,203 @@
|
|||
<style>
|
||||
.icons-list-2 {
|
||||
cursor: all-scroll;
|
||||
}
|
||||
.position-text-div {
|
||||
cursor: text;
|
||||
margin: -8px;
|
||||
padding: 8px;
|
||||
}
|
||||
.position-text-div:hover {
|
||||
background: aqua;
|
||||
}
|
||||
</style>
|
||||
<div class="order-edit-notification"><%= t("web_link.please_save") %></div>
|
||||
<table width="100%" id="web_resource_order_table" class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th></th>
|
||||
<th><%= t("web_link.position") %></th>
|
||||
<th><%= t("web_link.link") %></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<% @links.each_with_index do |link,i| %>
|
||||
<tr data-index="<%=i%>">
|
||||
<td><span class="brand"><i class="icons-list-2"></i></span></td>
|
||||
<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.html_safe %></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="#"><%= t("web_link.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.addClass("editable-input");
|
||||
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");
|
||||
$(".order-edit-notification").slideDown();
|
||||
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[1].textContent.trim(),
|
||||
hash = {};
|
||||
hash.index = parseInt(text);
|
||||
text = thisRow.cells[2].innerHTML.trim();
|
||||
if(text != " "){
|
||||
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[1].innerHTML = "<div class='position-text-div' data-value='" + current_value + "'>" + current_value + "</div>";
|
||||
thisRow.cells[2].innerHTML = data[i].link;
|
||||
}
|
||||
$("#web_resource_order_table div.position-text-div").on("click",makeEditable);
|
||||
}
|
||||
|
||||
$(document).on("click","#save-order-button",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");
|
||||
$(".order-edit-notification").slideUp();
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
$(document).on("click","#web_resource_order_table div.position-text-div",makeEditable);
|
||||
|
||||
</script>
|
||||
<script>
|
||||
var th_width = {};
|
||||
function initSortable(){
|
||||
th_width = {};
|
||||
$( ".table tbody" ).each(function(i,tbody){
|
||||
var table = $(tbody).parents("table").eq(0);
|
||||
table.data("index",i);
|
||||
th_width[i] = [];
|
||||
table.find("thead th").each(function(j,th){
|
||||
th_width[i].push($(th).outerWidth(true));
|
||||
})
|
||||
})
|
||||
$( ".table tbody" ).sortable({
|
||||
revert: true,
|
||||
axis: "y",
|
||||
handle: ".brand",
|
||||
start: function(event, ui){
|
||||
var item = ui.item;
|
||||
var target = $(event.target);
|
||||
var index = target.parents(".table").eq(0).data("index");
|
||||
item.css("width",target.width());
|
||||
item.find("td").each(function(i,td){
|
||||
$(td).width(th_width[index][i]);
|
||||
})
|
||||
},
|
||||
stop: function(event, ui) {
|
||||
var item = ui.item;
|
||||
item.css("width","");
|
||||
item.find("td").css("width","");
|
||||
},
|
||||
update: function(event, ui) {
|
||||
var item = ui.item;
|
||||
var org_index = item.data("index");
|
||||
var new_index = item.index();
|
||||
var indices = [org_index,new_index].sort();
|
||||
var table = item.parents(".table").eq(0);
|
||||
table.find("tbody tr").each(function(i,tr){
|
||||
if(i >= indices[0] && i <= indices[1]){
|
||||
var position_text_div = $(tr).find(".position-text-div");
|
||||
position_text_div.text(i+1).data("value",i+1);
|
||||
$(tr).data("index",i);
|
||||
}
|
||||
if(i > indices[1]){
|
||||
return;
|
||||
}
|
||||
})
|
||||
$("#save-order-button").removeClass("disabled");
|
||||
$(".order-edit-notification").slideDown();
|
||||
}
|
||||
});
|
||||
}
|
||||
$(document).ready(function(){
|
||||
initSortable();
|
||||
});
|
||||
$(document).on("ajaxComplete", function(){
|
||||
initSortable();
|
||||
});
|
||||
$(window).resize(function(){
|
||||
th_width = {};
|
||||
$( ".table tbody" ).each(function(i,tbody){
|
||||
var table = $(tbody).parents("table").eq(0);
|
||||
th_width[i] = [];
|
||||
table.find("thead th").each(function(j,th){
|
||||
th_width[i].push($(th).outerWidth(true));
|
||||
})
|
||||
})
|
||||
})
|
||||
</script>
|
||||
|
|
@ -1,199 +1,7 @@
|
|||
<% content_for :page_specific_css do %>
|
||||
<%= stylesheet_link_tag "admin/web_links" %>
|
||||
<% end %>
|
||||
<style>
|
||||
.icons-list-2 {
|
||||
cursor: all-scroll;
|
||||
}
|
||||
.position-text-div {
|
||||
cursor: text;
|
||||
margin: -8px;
|
||||
padding: 8px;
|
||||
}
|
||||
.position-text-div:hover {
|
||||
background: aqua;
|
||||
}
|
||||
</style>
|
||||
<div class="order-edit-notification"><%= t("web_link.please_save") %></div>
|
||||
<table width="100%" id="web_resource_order_table" class="table table-striped" class="web_soursce_table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th></th>
|
||||
<th><%= t("web_link.position") %></th>
|
||||
<th><%= t("web_link.link") %></th>
|
||||
</thead>
|
||||
<tbody>
|
||||
<% @links.each_with_index do |link,i| %>
|
||||
<tr data-index="<%=i%>">
|
||||
<td><span class="brand"><i class="icons-list-2"></i></span></td>
|
||||
<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.html_safe %></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="#"><%= t("web_link.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.addClass("editable-input");
|
||||
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");
|
||||
$(".order-edit-notification").slideDown();
|
||||
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[1].textContent.trim(),
|
||||
hash = {};
|
||||
hash.index = parseInt(text);
|
||||
text = thisRow.cells[2].innerHTML.trim();
|
||||
if(text != " "){
|
||||
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[1].innerHTML = "<div class='position-text-div' data-value='" + current_value + "'>" + current_value + "</div>";
|
||||
thisRow.cells[2].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");
|
||||
$(".order-edit-notification").slideUp();
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
$("#web_resource_order_table div.position-text-div").on("click",makeEditable);
|
||||
|
||||
</script>
|
||||
<script>
|
||||
var th_width = {};
|
||||
$(document).ready(function(){
|
||||
$( ".table tbody" ).each(function(i,tbody){
|
||||
var table = $(tbody).parents("table").eq(0);
|
||||
table.data("index",i);
|
||||
th_width[i] = [];
|
||||
table.find("thead th").each(function(j,th){
|
||||
th_width[i].push($(th).outerWidth(true));
|
||||
})
|
||||
})
|
||||
$( ".table tbody" ).sortable({
|
||||
revert: true,
|
||||
axis: "y",
|
||||
handle: ".brand",
|
||||
start: function(event, ui){
|
||||
var item = ui.item;
|
||||
var target = $(event.target);
|
||||
var index = target.parents(".table").eq(0).data("index");
|
||||
item.css("width",target.width());
|
||||
item.find("td").each(function(i,td){
|
||||
$(td).width(th_width[index][i]);
|
||||
})
|
||||
},
|
||||
stop: function(event, ui) {
|
||||
var item = ui.item;
|
||||
item.css("width","");
|
||||
item.find("td").css("width","");
|
||||
},
|
||||
update: function(event, ui) {
|
||||
var item = ui.item;
|
||||
var org_index = item.data("index");
|
||||
console.log(org_index);
|
||||
var new_index = item.index();
|
||||
var indices = [org_index,new_index].sort();
|
||||
var table = item.parents(".table").eq(0);
|
||||
table.find("tbody tr").each(function(i,tr){
|
||||
if(i >= indices[0] && i <= indices[1]){
|
||||
var position_text_div = $(tr).find(".position-text-div");
|
||||
position_text_div.text(i+1).data("value",i+1);
|
||||
$(tr).data("index",i);
|
||||
}
|
||||
if(i > indices[1]){
|
||||
return;
|
||||
}
|
||||
})
|
||||
$("#save-order-button").removeClass("disabled");
|
||||
$(".order-edit-notification").slideDown();
|
||||
}
|
||||
});
|
||||
})
|
||||
$(window).resize(function(){
|
||||
th_width = {};
|
||||
$( ".table tbody" ).each(function(i,tbody){
|
||||
var table = $(tbody).parents("table").eq(0);
|
||||
th_width[i] = [];
|
||||
table.find("thead th").each(function(j,th){
|
||||
th_width[i].push($(th).outerWidth(true));
|
||||
})
|
||||
})
|
||||
})
|
||||
</script>
|
||||
<%= render_filter @filter_fields, "order_table" %>
|
||||
<div id="order_table">
|
||||
<%= render 'order' %>
|
||||
</div>
|
||||
|
|
|
|||
Loading…
Reference in New Issue