From b1867dd47a9edd3416f43934ba8a05fdfc275908 Mon Sep 17 00:00:00 2001 From: rulingcom Date: Mon, 4 Nov 2013 22:20:50 +0800 Subject: [PATCH] new code logic for saving tree.. should work fine even if the node positions fail --- app/assets/javascripts/lib/items/items.js.erb | 17 ++++++----- app/controllers/admin/items_controller.rb | 2 +- app/models/item.rb | 28 ++++++++++--------- config/config.yml | 3 +- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/app/assets/javascripts/lib/items/items.js.erb b/app/assets/javascripts/lib/items/items.js.erb index 304d21b3..0900fab0 100644 --- a/app/assets/javascripts/lib/items/items.js.erb +++ b/app/assets/javascripts/lib/items/items.js.erb @@ -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){ diff --git a/app/controllers/admin/items_controller.rb b/app/controllers/admin/items_controller.rb index 4aa7ae17..f9a7c971 100644 --- a/app/controllers/admin/items_controller.rb +++ b/app/controllers/admin/items_controller.rb @@ -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 diff --git a/app/models/item.rb b/app/models/item.rb index b9ee2da7..6854c5eb 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -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 diff --git a/config/config.yml b/config/config.yml index 439386fb..228255c7 100644 --- a/config/config.yml +++ b/config/config.yml @@ -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'