Ajax support / Auto-save support for tabledrag.js
| Project: | Drupal |
| Version: | 7.x-dev |
| Component: | javascript |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | postponed |
Jump to:
Just like it says, I know that the tabledrag creator at Drupalcon Boston said that ajax saving of the reweighed / restructured values created via tabledrag.js would be cool functionality to add so I'm proposing it be rolled into 7. It's a very simple process and I've actually achieved it in D6 via the outline designer module (see demo for example of drag/drop ajax saving -- https://elearning.psu.edu/demos/outline_designer/
This is the code that I've overloaded in order to accomplish this atm --
/**
* Overload tabledrag onDrop event so that it ajax saves the new parent for the node
*/
Drupal.tableDrag.prototype.onDrop = function() {
//row object so we don't have to call it all the time
var row_obj = this.rowObject.element;
//get the id of what was dragged
var drag_nid = $(row_obj).find('img').attr('id').replace('node-','').replace('-icon','');
//get the parent id based on the indentations, this equation is a bit evil...
var p_nid;
var weight = $("#edit-table-book-admin-"+ drag_nid +"-weight").val();
var active_indent = Math.max($('.indentation', row_obj).size());
//if we're at level 0 then the node is at the book root
if(active_indent != 0){
var tmp_indent = -1;
var tmp_obj = row_obj;
//keep walking backwards until we find the node we need
while (tmp_indent != (active_indent-1) ) {
tmp_obj = $(tmp_obj).prev();
tmp_indent = Math.max($('.indentation', tmp_obj).size());
}
p_nid = $(tmp_obj).find('img').attr('id').replace('node-','').replace('-icon','');
} else {
p_nid = ROOT_NID;
}
$.ajax({
type: "POST",
url: AJAX_PATH + "drag_drop/" + drag_nid + "/" + p_nid + "/" + weight,
success: function(msg){
//could implement some kind of history / undo list here if we want to
od_response_msg(msg);
}
});
return null;
};
The drag drop function on the other side just changes the parent / weight for the drag_nid that it's been given. jQuery ajax is how I handle it though I'm sure if we work through it we can come up with the appropriate way to make this work with anything (right now this code is just for book based transactions).
Function on the other side backend does this for the drag_drop case
case 'drag_drop':
$nid = $var1;
$parent_nid = $var2;
$weight = $var3;
//load the parent / active node
$node = node_load($nid);
$parent = node_load($parent_nid);
//set parent
$node->book['plid'] = $parent->book['mlid'];
$node->book['weight'] = $weight;
$node->revision = 1;
$node->log = "Outline Designer -- nid:$nid parent nid changed to $parent_nid";
node_save($node);
print "Node nid:$nid parent nid changed to $parent_nid";
break;
This should be easy enough to convert into allowing for block tabledrags or anything else to come across and be set correctly, even if it involes sending weight=, bid=, pid=, region= type of statements.
http://drupal.org/project/outline_designer/ for the actual module that uses this stuff
Thoughts?

#1
can you roll this into a patch?
#2
Should be able to, just need to wait till I get home (currently on the road). Wanted to at least get this out in the open and see if others thought it was worth investigating / patching in if possible. Only thing I'll need to think more about is how to make this work with any implementation of table-drag instead of just books (as it is now). Is there anything that uses tabledrag other then books / menus / blocks in core/core-optional? If not then this could be applied only to the books / menu / blocks cases and hopefully someone could look into making it extensible past that should others want to implement it.
#3
This seems almost like it needs it's own ajax API for committing drupal operations on the fly (or at least seems like it would benefit from it significantly). I tap into something like /function/node/data in terms of my formatting but it seems like it would be best served by something listening backend similar to the hook_ system but launching the hook system for system updates.
Are there any plans for a ajax api or anything like that?
#4
Proper status.