// =================================================================== // Author: Denis Howlett // WWW: http://www.isocra.com/ // // NOTICE: You may use this code for any purpose, commercial or // private, without any further permission from the author. You may // remove this notice from your final code if you wish, however we // would appreciate it if at least the web site address is kept. // // You may *NOT* re-distribute this code in any way except through its // use. That means, you can include it in your product, or your web // site, or any other form where the code is actually being used. You // may not put the plain javascript up on your site for download or // include it in your javascript libraries for download. // If you wish to share this code with others, please just point them // to the URL instead. // // Please DO NOT link directly to this .js files from your site. Copy // the files to your server and use them there. Thank you. // =================================================================== /** Keep hold of the current table being dragged */ var currenttable = null; /** Capture the onmousemove so that we can see if a row from the current * table if any is being dragged. * @param ev the event (for Firefox and Safari, otherwise we use window.event for IE) */ document.onmousemove = function(ev){ if (currenttable && currenttable.dragObject) { ev = ev || window.event; var mousePos = currenttable.mouseCoords(ev); var y = mousePos.y - currenttable.mouseOffset.y; if (y != currenttable.oldY) { // work out if we're going up or down... var movingDown = y > currenttable.oldY; // update the old value currenttable.oldY = y; // update the style to show we're dragging currenttable.dragObject.style.backgroundColor = "#eee"; // If we're over a row then move the dragged row to there so that the user sees the // effect dynamically var currentRow = currenttable.findDropTargetRow(y); if (currentRow) { if (movingDown && currenttable.dragObject != currentRow) { currenttable.dragObject.parentNode.insertBefore(currenttable.dragObject, currentRow.nextSibling); } else if (! movingDown && currenttable.dragObject != currentRow) { currenttable.dragObject.parentNode.insertBefore(currenttable.dragObject, currentRow); } } } return false; } } // Similarly for the mouseup document.onmouseup = function(ev){ if (currenttable && currenttable.dragObject) { var droppedRow = currenttable.dragObject; // If we have a dragObject, then we need to release it, // The row will already have been moved to the right place so we just reset stuff droppedRow.style.backgroundColor = 'transparent'; currenttable.dragObject = null; // And then call the onDrop method in case anyone wants to do any post processing currenttable.onDrop(currenttable.table, droppedRow); currenttable = null; // let go of the table too } } /** get the source element from an event in a way that works for IE and Firefox and Safari * @param evt the source event for Firefox (but not IE--IE uses window.event) */ function getEventSource(evt) { if (window.event) { evt = window.event; // For IE return evt.srcElement; } else { return evt.target; // For Firefox } } /** * Encapsulate table Drag and Drop in a class. We'll have this as a Singleton * so we don't get scoping problems. */ function TableDnD() { /** Keep hold of the current drag object if any */ this.dragObject = null; /** The current mouse offset */ this.mouseOffset = null; /** The current table */ this.table = null; /** Remember the old value of Y so that we don't do too much processing */ this.oldY = 0; /** Initialise the drag and drop by capturing mouse move events */ this.init = function(table) { this.table = table; var rows = table.tBodies[0].rows; //getElementsByTagName("tr") for (var i=0; i rowY - rowHeight) && (y < (rowY + rowHeight))) { // that's the row we're over return row; } } } return null; } }