From 34ff4ef3705eab0fea8306f010b74a02153a70fd Mon Sep 17 00:00:00 2001 From: blueshadow2911 Date: Fri, 10 Aug 2012 17:10:04 +0800 Subject: [PATCH] make Draggable table works on touch screen devices --- misc/tabledrag.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/misc/tabledrag.js b/misc/tabledrag.js index 4ac3714..7dfa6ab 100644 --- a/misc/tabledrag.js +++ b/misc/tabledrag.js @@ -92,6 +92,40 @@ Drupal.tableDrag = function(table, tableSettings) { // as event handlers do not have direct access to the tableDrag object. $(document).bind('mousemove', function(event) { return self.dragRow(event, self); }); $(document).bind('mouseup', function(event) { return self.dropRow(event, self); }); + + // To stimulate MouseEvent in touch screen devices + $(document).bind('touchmove', function(event) { + if ($("body").hasClass("drag")) { + if (event.originalEvent.touches && event.originalEvent.touches.length) { + var touch = event.originalEvent.touches[0]; + } else if (event.originalEvent.changedTouches && event.originalEvent.changedTouches.length) { + var touch = event.originalEvent.changedTouches[0]; + } + + var simulatedEvent = document.createEvent("MouseEvent"); + simulatedEvent.initMouseEvent('mousemove', true, true, window, 1, + touch.screenX, touch.screenY, touch.clientX, touch.clientY, + false, false, false, false, 0/*left*/, null); + touch.target.dispatchEvent(simulatedEvent); + event.preventDefault(); + } + }); + $(document).bind('touchend', function(event) { + if ($("body").hasClass("drag")) { + if (event.originalEvent.touches && event.originalEvent.touches.length) { + var touch = event.originalEvent.touches[0]; + } else if (event.originalEvent.changedTouches && event.originalEvent.changedTouches.length) { + var touch = event.originalEvent.changedTouches[0]; + } + + var simulatedEvent = document.createEvent("MouseEvent"); + simulatedEvent.initMouseEvent('mouseup', true, true, window, 1, + touch.screenX, touch.screenY, touch.clientX, touch.clientY, + false, false, false, false, 0/*left*/, null); + touch.target.dispatchEvent(simulatedEvent); + event.preventDefault(); + } + }); }; /** @@ -368,6 +402,22 @@ Drupal.tableDrag.prototype.makeDraggable = function(item) { return false; } }); + + // To stimulate MouseEvent in touch screen devices + handle.bind('touchstart', function(event) { + if (event.originalEvent.touches && event.originalEvent.touches.length) { + var touch = event.originalEvent.touches[0]; + } else if (event.originalEvent.changedTouches && event.originalEvent.changedTouches.length) { + var touch = event.originalEvent.changedTouches[0]; + } + + var simulatedEvent = document.createEvent("MouseEvent"); + simulatedEvent.initMouseEvent('mousedown', true, true, window, 1, + touch.screenX, touch.screenY, touch.clientX, touch.clientY, + false, false, false, false, 0/*left*/, null); + touch.target.dispatchEvent(simulatedEvent); + event.preventDefault(); + }); }; /** -- 1.7.10.msysgit.1