new code logic for saving tree.. should work fine even if the node positions fail

This commit is contained in:
rulingcom 2013-11-04 22:20:50 +08:00
parent 3db075b9d2
commit b1867dd47a
4 changed files with 28 additions and 22 deletions

View File

@ -321,13 +321,16 @@ $(function() {
disableNesting: 'no-nest', disableNesting: 'no-nest',
update: function(event, ui) { update: function(event, ui) {
var position = null; var position = null;
// if($(ui.item).closest('ol').hasClass('item-groups')) { if($(ui.item).closest('ol').hasClass('item-groups')) {
// position = $(ui.item).index() - 1; position = $(ui.item).index() - 1;
// } else { } else {
// position = $(ui.item).index(); position = $(ui.item).index();
// }; };
position = $(ui.item).index(); var next_sibling = $(ui.item).next(),
$.post("<%= Rails.application.routes.url_helpers.update_position_admin_items_path %>", { id: ui.item.attr('id'), parent_id: (ui.item.parent().closest('li').attr('id') || ui.item.parent().closest('ol').attr('id')), position: position } ); next_sibling_id = null;
if( next_sibling )
next_sibling_id = next_sibling.attr("id");
$.post("<%= Rails.application.routes.url_helpers.update_position_admin_items_path %>", { id: ui.item.attr('id'), parent_id: (ui.item.parent().closest('li').attr('id') || ui.item.parent().closest('ol').attr('id')), position: position, "next_sibling_id" : next_sibling_id } );
} }
}); });
$(".sortable").delegate(".brand, .delete", clickEvent, function(e){ $(".sortable").delegate(".brand, .delete", clickEvent, function(e){

View File

@ -24,7 +24,7 @@ class Admin::ItemsController < OrbitBackendController
def update_position def update_position
item = Item.find(params[:id]) item = Item.find(params[:id])
item.shift_to(params[:parent_id], params[:position].to_i) item.shift_to(params[:parent_id], params[:position].to_i, params[:next_sibling_id])
render :nothing => true, status: 200 render :nothing => true, status: 200
end end

View File

@ -44,17 +44,23 @@ class Item
a a
end end
def shift_to(new_parent, position) def find_by_parent_and_position(parent, position)
parent.children.detect{|child| child.position == position}
end
def shift_to(new_parent, position, next_sibling_id)
position = position.to_i position = position.to_i
new_parent = Item.find(new_parent) new_parent = Item.find(new_parent)
current_position_sibling = find_by_parent_and_position(new_parent, position)
if current_position_sibling if next_sibling_id
if self.position > current_position_sibling.position next_sibling = Item.find(next_sibling_id)
self.move_above(current_position_sibling) self.move_above(next_sibling)
else else
self.move_below(current_position_sibling) self.move_to_bottom
end end
elsif self.parent != new_parent
if self.parent != new_parent
self.parent = new_parent self.parent = new_parent
save save
end end
@ -86,10 +92,6 @@ class Item
end end
end end
def find_by_parent_and_position(parent, position)
parent.children.detect{|child| child.position == position}
end
# Enable the validation for parent_id # Enable the validation for parent_id
def validates_presence_of_parent_id? def validates_presence_of_parent_id?
true true

View File

@ -1,6 +1,7 @@
defaults: &defaults defaults: &defaults
main_public_key: 'lib/main_public_key.pem' main_public_key: 'lib/main_public_key.pem'
store_ip: 'redmine.rulingcom.com:3001' # store_ip: 'redmine.rulingcom.com:3001'
store_ip: 'tp.rulingcom.com:33335'
orbit: 'Orbit' orbit: 'Orbit'
ruling_digital: 'RulingDigital' ruling_digital: 'RulingDigital'