2
* TableDnD plug-in for JQuery, allows you to drag and drop table rows
3
* You can set up various options to control how the system will work
4
* Copyright (c) Denis Howlett <denish@isocra.com>
5
* Licensed like jQuery, see http://docs.jquery.com/License.
7
* Configuration options:
10
* This is the style that is assigned to the row during drag. There are limitations to the styles that can be
11
* associated with a row (such as you can't assign a border--well you can, but it won't be
12
* displayed). (So instead consider using onDragClass.) The CSS style to apply is specified as
13
* a map (as used in the jQuery css(...) function).
15
* This is the style that is assigned to the row when it is dropped. As for onDragStyle, there are limitations
16
* to what you can do. Also this replaces the original style, so again consider using onDragClass which
17
* is simply added and then removed on drop.
19
* This class is added for the duration of the drag and then removed when the row is dropped. It is more
20
* flexible than using onDragStyle since it can be inherited by the row cells and other content. The default
21
* is class is tDnD_whileDrag. So to use the default, simply customise this CSS class in your
24
* Pass a function that will be called when the row is dropped. The function takes 2 parameters: the table
25
* and the row that was dropped. You can work out the new order of the rows by using
28
* Pass a function that will be called when the user starts dragging. The function takes 2 parameters: the
29
* table and the row which the user has started to drag.
31
* Pass a function that will be called as a row is over another row. If the function returns true, allow
32
* dropping on that row, otherwise not. The function takes 2 parameters: the dragged row and the row under
33
* the cursor. It returns a boolean: true allows the drop, false doesn't allow it.
35
* This is the number of pixels to scroll if the user moves the mouse cursor to the top or bottom of the
36
* window. The page should automatically scroll up or down as appropriate (tested in IE6, IE7, Safari, FF2,
39
* This is the name of a class that you assign to one or more cells in each row that is draggable. If you
40
* specify this class, then you are responsible for setting cursor: move in the CSS and only these cells
41
* will have the drag behaviour. If you do not specify a dragHandle, then you get the old behaviour where
42
* the whole row is draggable.
44
* Other ways to control behaviour:
46
* Add class="nodrop" to any rows for which you don't want to allow dropping, and class="nodrag" to any rows
47
* that you don't want to be draggable.
49
* Inside the onDrop method you can also call $.tableDnD.serialize() this returns a string of the form
50
* <tableID>[]=<rowID1>&<tableID>[]=<rowID2> so that you can send this back to the server. The table must have
51
* an ID as must all the rows.
55
* $("...").tableDnDUpdate()
56
* Will update all the matching tables, that is it will reapply the mousedown method to the rows (or handle cells).
57
* This is useful if you have updated the table rows using Ajax and you want to make the table draggable again.
58
* The table maintains the original configuration (so you don't have to specify it again).
60
* $("...").tableDnDSerialize()
61
* Will serialize and return the serialized string as above, but for each of the matching tables--so it can be
62
* called from anywhere and isn't dependent on the currentTable being set up correctly before calling
65
* - Auto-scoll has some problems with IE7 (it scrolls even when it shouldn't), work-around: set scrollAmount to 0
67
* Version 0.2: 2008-02-20 First public version
68
* Version 0.3: 2008-02-07 Added onDragStart option
69
* Made the scroll amount configurable (default is 5 as before)
70
* Version 0.4: 2008-03-15 Changed the noDrag/noDrop attributes to nodrag/nodrop classes
71
* Added onAllowDrop to control dropping
72
* Fixed a bug which meant that you couldn't set the scroll amount in both directions
73
* Added serialize method
74
* Version 0.5: 2008-05-16 Changed so that if you specify a dragHandle class it doesn't make the whole row
76
* Improved the serialize method to use a default (and settable) regular expression.
77
* Added tableDnDupate() and tableDnDSerialize() to be called when you are outside the table
80
/** Keep hold of the current table being dragged */
82
/** Keep hold of the current drag object if any */
84
/** The current mouse offset */
86
/** Remember the old value of Y so that we don't do too much processing */
89
/** Actually build the structure */
90
build: function(options) {
91
// Set up the defaults if any
93
this.each(function() {
94
// This is bound to each matching table, set up the defaults and override with user options
95
this.tableDnDConfig = jQuery.extend({
98
// Add in the default class for whileDragging
99
onDragClass: "tDnD_whileDrag",
103
serializeRegexp: /[^\-]*$/, // The regular expression to use to trim row IDs
104
serializeParamName: null, // If you want to specify another parameter name instead of the table ID
105
dragHandle: null // If you give the name of a class here, then only Cells with this class will be draggable
107
// Now make the rows draggable
108
jQuery.tableDnD.makeDraggable(this);
111
// Now we need to capture the mouse up and mouse move event
112
// We can use bind so that we don't interfere with other event handlers
114
.bind('mousemove', jQuery.tableDnD.mousemove)
115
.bind('mouseup', jQuery.tableDnD.mouseup);
117
// Don't break the chain
121
/** This function makes all the rows on the table draggable apart from those marked as "NoDrag" */
122
makeDraggable: function(table) {
123
var config = table.tableDnDConfig;
124
if (table.tableDnDConfig.dragHandle) {
125
// We only need to add the event to the specified cells
126
var cells = jQuery("td."+table.tableDnDConfig.dragHandle, table);
127
cells.each(function() {
128
// The cell is bound to "this"
129
jQuery(this).mousedown(function(ev) {
130
jQuery.tableDnD.dragObject = this.parentNode;
131
jQuery.tableDnD.currentTable = table;
132
jQuery.tableDnD.mouseOffset = jQuery.tableDnD.getMouseOffset(this, ev);
133
if (config.onDragStart) {
134
// Call the onDrop method if there is one
135
config.onDragStart(table, this);
141
// For backwards compatibility, we add the event to the whole row
142
var rows = jQuery("tr", table); // get all the rows as a wrapped set
143
rows.each(function() {
144
// Iterate through each row, the row is bound to "this"
145
var row = jQuery(this);
146
if (! row.hasClass("nodrag")) {
147
row.mousedown(function(ev) {
148
if (ev.target.tagName == "TD") {
149
jQuery.tableDnD.dragObject = this;
150
jQuery.tableDnD.currentTable = table;
151
jQuery.tableDnD.mouseOffset = jQuery.tableDnD.getMouseOffset(this, ev);
152
if (config.onDragStart) {
153
// Call the onDrop method if there is one
154
config.onDragStart(table, this);
158
}).css("cursor", "move"); // Store the tableDnD object
164
updateTables: function() {
165
this.each(function() {
166
// this is now bound to each matching table
167
if (this.tableDnDConfig) {
168
jQuery.tableDnD.makeDraggable(this);
173
/** Get the mouse coordinates from the event (allowing for browser differences) */
174
mouseCoords: function(ev){
175
if(ev.pageX || ev.pageY){
176
return {x:ev.pageX, y:ev.pageY};
179
x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
180
y:ev.clientY + document.body.scrollTop - document.body.clientTop
184
/** Given a target element and a mouse event, get the mouse offset from that element.
185
To do this we need the element's position and the mouse position */
186
getMouseOffset: function(target, ev) {
187
ev = ev || window.event;
189
var docPos = this.getPosition(target);
190
var mousePos = this.mouseCoords(ev);
191
return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
194
/** Get the position of an element by going up the DOM tree and adding up all the offsets */
195
getPosition: function(e){
198
/** Safari fix -- thanks to Luis Chato for this! */
199
if (e.offsetHeight == 0) {
200
/** Safari 2 doesn't correctly grab the offsetTop of a table row
201
this is detailed here:
202
http://jacob.peargrove.com/blog/2006/technical/table-row-offsettop-bug-in-safari/
203
the solution is likewise noted there, grab the offset of a table cell in the row - the firstChild.
204
note that firefox will return a text node as a first child, so designing a more thorough
205
solution may need to take that into account, for now this seems to work in firefox, safari, ie */
206
e = e.firstChild; // a table cell
209
while (e.offsetParent){
210
left += e.offsetLeft;
215
left += e.offsetLeft;
218
return {x:left, y:top};
221
mousemove: function(ev) {
222
if (jQuery.tableDnD.dragObject == null) {
226
var dragObj = jQuery(jQuery.tableDnD.dragObject);
227
var config = jQuery.tableDnD.currentTable.tableDnDConfig;
228
var mousePos = jQuery.tableDnD.mouseCoords(ev);
229
var y = mousePos.y - jQuery.tableDnD.mouseOffset.y;
230
//auto scroll the window
231
var yOffset = window.pageYOffset;
234
//yOffset=document.body.scrollTop;
235
if (typeof document.compatMode != 'undefined' &&
236
document.compatMode != 'BackCompat') {
237
yOffset = document.documentElement.scrollTop;
239
else if (typeof document.body != 'undefined') {
240
yOffset=document.body.scrollTop;
245
if (mousePos.y-yOffset < config.scrollAmount) {
246
window.scrollBy(0, -config.scrollAmount);
248
var windowHeight = window.innerHeight ? window.innerHeight
249
: document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
250
if (windowHeight-(mousePos.y-yOffset) < config.scrollAmount) {
251
window.scrollBy(0, config.scrollAmount);
256
if (y != jQuery.tableDnD.oldY) {
257
// work out if we're going up or down...
258
var movingDown = y > jQuery.tableDnD.oldY;
259
// update the old value
260
jQuery.tableDnD.oldY = y;
261
// update the style to show we're dragging
262
if (config.onDragClass) {
263
dragObj.addClass(config.onDragClass);
265
dragObj.css(config.onDragStyle);
267
// If we're over a row then move the dragged row to there so that the user sees the
268
// effect dynamically
269
var currentRow = jQuery.tableDnD.findDropTargetRow(dragObj, y);
271
// TODO worry about what happens when there are multiple TBODIES
272
if (movingDown && jQuery.tableDnD.dragObject != currentRow) {
273
jQuery.tableDnD.dragObject.parentNode.insertBefore(jQuery.tableDnD.dragObject, currentRow.nextSibling);
274
} else if (! movingDown && jQuery.tableDnD.dragObject != currentRow) {
275
jQuery.tableDnD.dragObject.parentNode.insertBefore(jQuery.tableDnD.dragObject, currentRow);
283
/** We're only worried about the y position really, because we can only move rows up and down */
284
findDropTargetRow: function(draggedRow, y) {
285
var rows = jQuery.tableDnD.currentTable.rows;
286
for (var i=0; i<rows.length; i++) {
288
var rowY = this.getPosition(row).y;
289
var rowHeight = parseInt(row.offsetHeight)/2;
290
if (row.offsetHeight == 0) {
291
rowY = this.getPosition(row.firstChild).y;
292
rowHeight = parseInt(row.firstChild.offsetHeight)/2;
294
// Because we always have to insert before, we need to offset the height a bit
295
if ((y > rowY - rowHeight) && (y < (rowY + rowHeight))) {
296
// that's the row we're over
297
// If it's the same as the current row, ignore it
298
if (row == draggedRow) {return null;}
299
var config = jQuery.tableDnD.currentTable.tableDnDConfig;
300
if (config.onAllowDrop) {
301
if (config.onAllowDrop(draggedRow, row)) {
307
// If a row has nodrop class, then don't allow dropping (inspired by John Tarr and Famic)
308
var nodrop = jQuery(row).hasClass("nodrop");
321
mouseup: function(e) {
322
if (jQuery.tableDnD.currentTable && jQuery.tableDnD.dragObject) {
323
var droppedRow = jQuery.tableDnD.dragObject;
324
var config = jQuery.tableDnD.currentTable.tableDnDConfig;
325
// If we have a dragObject, then we need to release it,
326
// The row will already have been moved to the right place so we just reset stuff
327
if (config.onDragClass) {
328
jQuery(droppedRow).removeClass(config.onDragClass);
330
jQuery(droppedRow).css(config.onDropStyle);
332
jQuery.tableDnD.dragObject = null;
334
// Call the onDrop method if there is one
335
config.onDrop(jQuery.tableDnD.currentTable, droppedRow);
337
jQuery.tableDnD.currentTable = null; // let go of the table too
341
serialize: function() {
342
if (jQuery.tableDnD.currentTable) {
343
return jQuery.tableDnD.serializeTable(jQuery.tableDnD.currentTable);
345
return "Error: No Table id set, you need to set an id on your table and every row";
349
serializeTable: function(table) {
351
var tableId = table.id;
352
var rows = table.rows;
353
for (var i=0; i<rows.length; i++) {
354
if (result.length > 0) result += "&";
355
var rowId = rows[i].id;
356
if (rowId && rowId && table.tableDnDConfig && table.tableDnDConfig.serializeRegexp) {
357
rowId = rowId.match(table.tableDnDConfig.serializeRegexp)[0];
360
result += tableId + '[]=' + rowId;
365
serializeTables: function() {
367
this.each(function() {
368
// this is now bound to each matching table
369
result += jQuery.tableDnD.serializeTable(this);
378
tableDnD : jQuery.tableDnD.build,
379
tableDnDUpdate : jQuery.tableDnD.updateTables,
380
tableDnDSerialize: jQuery.tableDnD.serializeTables
b'\\ No newline at end of file'