Skip to content

Instantly share code, notes, and snippets.

@fro
Created January 26, 2012 16:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fro/1683557 to your computer and use it in GitHub Desktop.
Save fro/1683557 to your computer and use it in GitHub Desktop.
the_sortable_tree gem: erb version of partials
# _children.html.erb
<%= content_tag :ol, raw(children) %>
# _controls.html.erb
<%-
edit = link_to '', polymorphic_url(node, :action => :edit), :title => t('.edit_this'), :class => 'button edit'
if opts[:has_children]
delete = link_to('', url_for(node),
:title => t('.delete'),
:method => :delete,
:confirm => t('.delete_confirm'),
:class => 'button delete')
else
delete = link_to('', '#',
:title => t('.cant_be_deleted'),
:onclick => "alert('#{t('.delete_nested_elements')}')",
:class => 'button undeleted')
end
-%>
<%= edit + delete %>
# _js_init_sortable_tree.html.erb
<script type='text/javascript'>
//<![CDATA[
$(document).ready(function() {
$(function(){
$('ol.sortable').nestedSortable({
disableNesting: 'no-nest',
forcePlaceholderSize: true,
handle: 'i.handle',
helper: 'clone',
items: 'li',
maxLevels: <%= opts[:max_levels] %>,
opacity: .6,
placeholder: 'placeholder',
revert: 250,
tabSize: 25,
tolerance: 'pointer',
toleranceElement: '> div'
})
});
});
//]]>
</script>
# _js_on_update_tree.html
<script type='text/javascript'>
//<![CDATA[
$(document).ready(function() {
$(function(){
$('ol.sortable').sortable({
update: function(event, ui){
parent_id = ui.item.parent().parent().attr('id');
item_id = ui.item.attr('id');
prev_id = ui.item.prev().attr('id');
next_id = ui.item.next().attr('id');
sortable_tree(item_id, parent_id, prev_id, next_id);
}
});
});
});
//]]>
</script>
# _js_rebuild_ajax.html.erb
<script type='text/javascript'>
//<![CDATA[
function sortable_tree(item_id, parent_id, prev_id, next_id){
jQuery.ajax({
type: 'POST',
dataType: 'script',
url: '<%= url_for(:controller => opts[:klass].pluralize, :action => :rebuild) %>',
data: { id: item_id, parent_id: parent_id, prev_id: prev_id, next_id: next_id },
beforeSend: function(xhr){ $('.nested_set i.handle').hide() },
success: function(data, status, xhr){ $('.nested_set i.handle').show() },
error: function(xhr, status, error){ alert(error) }
});
}
//]]>
</script>
# _link.html.erb
<%-
title = node.send opts[:title]
handle = content_tag :i, '', :class => :handle
link = link_to(title, url_for(node), :title => title)
controls = render(:partial => "#{opts[:path]}/controls", :locals => { :node => node, :opts => opts })
controls = content_tag(:b, controls, :class => :controls)
element = handle + controls + link
-%>
<%= content_tag :div, element, :class => "link#{' root' if root }" %>
# _new.html.erb
<%-
image = image_tag('iconza/blue/add.png', :class => :nested_set_new_image) + ' '
link = link_to(image + t('.create'), opts[:new_url])
-%>
<%= content_tag :div, link, :class => :nested_set_new %>
# _node.html.erb
<%= content_tag :li, :id => "#{node.send(opts[:id])}_#{opts[:klass]}" do %>
<%= raw render(:partial => "#{opts[:path]}/link", :locals => {:opts => opts, :root => root, :node => node}) %>
<%= render(:partial => "#{opts[:path]}/children", :locals => {:opts => opts, :parent => node, :children => children}) unless children.blank? %>
<% end %>
# _tree.html.erb
<%= render :partial => "#{opts[:path]}/new", :locals => { :opts => opts } %>
<%- unless tree.empty? -%>
<%= render :partial => "#{opts[:path]}/js_init_sortable_tree", :locals => { :opts => opts } %>
<%= render :partial => "#{opts[:path]}/js_on_update_tree" %>
<%= render :partial => "#{opts[:path]}/js_rebuild_ajax", :locals => { :opts => opts } %>
<%= content_tag :ol, raw(tree), :class => "ui-sortable sortable nested_set", :id => "#{opts[:klass]}_nested_set" %>
<%- end -%>
@gmgp
Copy link

gmgp commented Feb 15, 2012

great work!
need change node to opts[:namespace] + [node]
to use :namespace option

@fro
Copy link
Author

fro commented Feb 16, 2012

Thx for mentioning it.

@PedramMarandi
Copy link

greate
thank you,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment