ENHANCEMENT Added drag'n'drop support for tree

This commit is contained in:
Ingo Schommer 2011-03-02 09:52:15 +13:00
parent 4f0c5d115a
commit 564038b463

View File

@ -4,42 +4,67 @@
(function($) { (function($) {
$(document).ready(function() { $(document).ready(function() {
$('#sitetree_ul').jstree({ var treeContainer = $('#sitetree_ul');
'core': { treeContainer
'initially_open': ['record-0'] .jstree({
}, 'core': {
'html_data': { 'initially_open': ['record-0']
// TODO Hack to avoid ajax load on init, see http://code.google.com/p/jstree/issues/detail?id=911 },
'data': $('#sitetree_ul').html(), 'html_data': {
'ajax': { // TODO Hack to avoid ajax load on init, see http://code.google.com/p/jstree/issues/detail?id=911
'url': 'admin/getsubtree', 'data': treeContainer.html(),
'data': function(node) { 'ajax': {
return { ID : $(node).data("id") ? $(node).data("id") : 0 , ajax: 1}; 'url': 'admin/getsubtree',
'data': function(node) {
return { ID : $(node).data("id") ? $(node).data("id") : 0 , ajax: 1};
}
} }
},
'ui': {
"select_limit" : 1,
'initially_select': [treeContainer.find('.current').attr('id')]
},
'plugins': ['themes', 'html_data', 'ui', 'dnd']
})
// .bind('before.jstree', function(e, data) {
// if(data.func == 'drag_start') {
// // Only allow drag'n'drop if it has been specifically enabled
// return $('input[id=sortitems]').is(':checked');
// }
// })
// TODO Move to EditForm logic
.bind('select_node.jstree', function(e, data) {
var node = data.rslt.obj, loadedNodeID = $('#Form_EditForm :input[name=ID]').val()
// Don't allow reloading of currently selected node,
// mainly to avoid doing an ajax request on initial page load
if($(node).data('id') == loadedNodeID) return;
var url = $(node).find('a:first').attr('href');
if(url && url != '#') {
var xmlhttp = $('#Form_EditForm').entwine('ss').loadForm(
url,
function(response) {}
);
} else {
$('#Form_EditForm').entwine('ss').removeForm();
} }
}, })
'ui': { .bind('move_node.jstree', function(e, data) {
"select_limit" : 1, var movedNode = data.rslt.o, newParentNode = data.rslt.np, oldParentNode = data.inst._get_parent(movedNode);
'initially_select': [$('#sitetree_ul').find('.current').attr('id')] var siblingIDs = $.map($(movedNode).siblings().andSelf(), function(el) {
}, return $(el).data('id');
'plugins': ['themes', 'html_data', 'ui'] });
});
$.ajax({
$('#sitetree_ul').bind('select_node.jstree', function(e, data) { 'url': 'admin/savetreenode',
var node = data.rslt.obj; 'data': {
var url = $(node).find('a:first').attr('href'); ID: $(movedNode).data('id'),
if(url && url != '#') { ParentID: $(newParentNode).data('id') || 0,
var xmlhttp = $('#Form_EditForm').entwine('ss').loadForm( SiblingIDs: siblingIDs
url, }
function(response) {} });
); });
// TODO Mark node as loading
// if(xmlhttp) this.addNodeClass('loading');
} else {
jQuery('#Form_EditForm').entwine('ss').removeForm();
}
});
}); });