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',
update: function(event, ui) {
var position = null;
// if($(ui.item).closest('ol').hasClass('item-groups')) {
// position = $(ui.item).index() - 1;
// } else {
// position = $(ui.item).index();
// };
position = $(ui.item).index();
$.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 } );
if($(ui.item).closest('ol').hasClass('item-groups')) {
position = $(ui.item).index() - 1;
} else {
position = $(ui.item).index();
};
var next_sibling = $(ui.item).next(),
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){

View File

@ -24,7 +24,7 @@ class Admin::ItemsController < OrbitBackendController
def update_position
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
end

View File

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

View File

@ -1,6 +1,7 @@
defaults: &defaults
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'
ruling_digital: 'RulingDigital'