Skip to content

Commit

Permalink
Render the page tree after successful update_positions.
Browse files Browse the repository at this point in the history
Fixes #1985
  • Loading branch information
parndt committed Dec 18, 2012
1 parent 7906ba8 commit 1647bdb
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 22 deletions.
44 changes: 26 additions & 18 deletions core/app/assets/javascripts/refinery/sortable_list.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
var create_sortable_list = function(options){
ordered_list = {
initialised: false
,init: function(options) {
, init: function(options) {
if(!this.initialised){
this.update_url = options.update_url;
this.sortable_list = options.sortable_list;
this.tree = options.tree;

this.replaceContentsAfterUpdate = options.replaceContentsAfterUpdate;

//setup dom
this.div_with_list_and_actions = this.sortable_list.parents().filter(function(index) {
this.div_with_list_and_actions = this.sortable_list.parents().filter(function(index) {
return ($(this).children("#actions").length > 0);
});
this.actions = $("#actions",this.div_with_list_and_actions);
this.reorder_action = $('#reorder_action',this.actions);
this.reorder_action_done = $('#reorder_action_done',this.actions);


this.sortable_list.addClass("sortable_list");
this.reorder_action.click($.proxy(this.enable_reordering, {list: this}));
this.reorder_action_done.click($.proxy(this.disable_reordering, {list: this}));

if(this.tree == false){
this.sortable_list.find('li').addClass('no-nest');
}

this.sortable_list.nestedSortable({
listType: 'ul',
disableNesting: 'no-nest',
Expand Down Expand Up @@ -52,31 +52,34 @@ var create_sortable_list = function(options){
return this;
}
, stop: function() {
if (this.list.tree) {
this.list.reset_branch_classes(this.list.sortable_list);
this.list.reset_icon_classes(this.list.sortable_list);
} else {
this.list.reset_on_off_classes(this.list.sortable_list);
}
this.list.reset_classes();
}
, change: function() {
if (this.list.tree) {
this.list.reset_branch_classes(this.list.sortable_list)
}
}
, reset_classes: function(){
if (this.tree) {
this.reset_branch_classes(this.sortable_list);
this.reset_icon_classes(this.sortable_list);
} else {
this.reset_on_off_classes(this.sortable_list);
}
}
, reset_on_off_classes: function(ul) {
$("> li", ul).each(function(i, li) {
$(li).removeClass('on off on-hover').addClass(i % 2 === 0 ? 'on' : 'off');
});
}

, reset_branch_classes: function (ul) {
$("li.ui-sortable-helper", this).removeClass("record").removeClass("branch_start").removeClass("branch_end");
$("li", ul).removeClass("branch_start").removeClass("branch_end");

$("> li:first", ul).addClass("branch_start");
$("> li:last", ul).addClass("branch_end");

var nested_ul = $("ul", ul);
$("> li:last", nested_ul).addClass("branch_end");
}
Expand All @@ -85,7 +88,7 @@ var create_sortable_list = function(options){
$('li', ul).each(function() {
var $li = $(this);
var $icon = $li.find('.icon:first');

if ($li.find('ul li').size() > 0) {
$icon.addClass('toggle expanded');
}
Expand All @@ -94,7 +97,7 @@ var create_sortable_list = function(options){
}
});
}

, enable_reordering: function(e) {
if(e) { e.preventDefault(); }
this.list.sortable_list.addClass("reordering");
Expand All @@ -113,7 +116,7 @@ var create_sortable_list = function(options){
this.list.reorder_action_done.addClass('loading');
this.list.sortable_list.nestedSortable("disable");
this.list.sortable_list.removeClass("reordering");

if (this.list.update_url !== null) {
var serialized = this.list.sortable_list.serializelist();

Expand All @@ -130,6 +133,11 @@ var create_sortable_list = function(options){
$('*:not("#reorder_action_done, #reorder_action")',this.list.actions).not($('#reorder_action_done').parents('li, ul, div')).fadeTo(250, 1);
this.list.reorder_action_done.hide().removeClass('loading');
this.list.reorder_action.show();
if (this.list.replaceContentsAfterUpdate) {
this.list.sortable_list.children().remove();
this.list.sortable_list.append($(e).children());
this.list.reset_classes();
}
}
}
ordered_list.init(options);
Expand Down
3 changes: 2 additions & 1 deletion core/app/views/refinery/admin/_make_sortable.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
create_sortable_list({
update_url: '<%= update_url ||= refinery.url_for(:action => "update_positions", :only_path => true) %>',
sortable_list: $('#<%= list_id ||= "sortable_list" %>, .sortable_list'),
tree: <%= tree %>
tree: <%= tree %>,
replaceContentsAfterUpdate: <%= local_assigns[:replace_contents_after_update] || false %>
});
});
</script>
Expand Down
9 changes: 8 additions & 1 deletion core/lib/refinery/crud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ def update_positions
end
#{class_name}.rebuild! if #{class_name}.respond_to?(:rebuild!)
render :nothing => true
after_update_positions
end
def update_child_positions(_node, #{singular_name})
Expand All @@ -307,6 +308,12 @@ def update_child_positions(_node, #{singular_name})
end
end
end
def after_update_positions
render :nothing => true
end
protected :after_update_positions
)
end

Expand Down
5 changes: 5 additions & 0 deletions pages/app/controllers/refinery/admin/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ def update

protected

def after_update_positions
find_all_pages
render :partial => '/refinery/admin/pages/sortable_list' and return
end

def find_page
@page = Refinery::Page.find_by_path_or_id(params[:path], params[:id])
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<ul id='sortable_list'>
<%= render :partial => 'page', :collection => @pages.select{|p| p.parent_id.nil?} %>
<%= render :partial => 'page', :collection => @pages.roots %>
</ul>
<%= render '/refinery/admin/sortable_list', :continue_reordering => !!local_assigns[:continue_reordering] %>
2 changes: 1 addition & 1 deletion pages/app/views/refinery/admin/pages/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
<section id='actions'>
<%= render 'actions' %>
</section>
<%= render '/refinery/admin/make_sortable', :tree => true if @pages.many? -%>
<%= render '/refinery/admin/make_sortable', :tree => true, :replace_contents_after_update => true if @pages.many? -%>

0 comments on commit 1647bdb

Please sign in to comment.