Skip to content

Commit

Permalink
Add drag'n drop support for mobile devices
Browse files Browse the repository at this point in the history
You need to tap and hold to be able to move elements
  • Loading branch information
oparoz committed Feb 17, 2016
1 parent 8a90e77 commit 093dedc
Show file tree
Hide file tree
Showing 9 changed files with 1,487 additions and 0 deletions.
8 changes: 8 additions & 0 deletions css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,16 @@ a.row-element {
vertical-align: top;
-webkit-transition: transform .2s ease-in-out;
transition: transform .2s ease-in-out;
-webkit-user-select:none;
-moz-user-select:none;
-ms-user-select:none;
user-select:none;
-webkit-touch-callout:none;
}

.album-droppable {
transform: scale(0.9);
-webkit-transform: scale(0.9);
}

.album-droppable .album {
Expand All @@ -128,10 +134,12 @@ a.row-element {

.album-droppable .album .cropped {
transform: scale(0.9);
-webkit-transform: scale(0.9);
}

.album-droppable-hover {
transform: scale(1.0);
-webkit-transform: scale(1.0);
-webkit-transition: transform .1s ease-in;
transition: transform .1s ease-in;
}
Expand Down
1 change: 1 addition & 0 deletions js/galleryview.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
* @private
*/
_initButtons: function () {
this.element.on("contextmenu", function(e) { e.preventDefault(); });
$('#filelist-button').click(Gallery.switchToFilesView);
$('#download').click(Gallery.download);
$('#share-button').click(Gallery.share);
Expand Down
184 changes: 184 additions & 0 deletions js/jquery.ui.touch-punch-custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/*!
* jQuery UI Touch Punch 0.2.3
*
* Copyright 2011–2014, Dave Furfero
* Dual licensed under the MIT or GPL Version 2 licenses.
*
* Modified for ownCloud Gallery by Olivier Paroz to convert taphold events into clicks instead of
* using touchstart
* @see https://stackoverflow.com/questions/34027761/jquery-ui-sortable-hold-and-drag-for-mobile
*
* Depends:
* jquery.ui.widget.js
* jquery.ui.mouse.js
*/
(function ($) {

// Detect touch support
$.support.touch = 'ontouchend' in document;

// Ignore browsers without touch support
if (!$.support.touch) {
return;
}

var mouseProto = $.ui.mouse.prototype,
_mouseInit = mouseProto._mouseInit,
_mouseDestroy = mouseProto._mouseDestroy,
touchHandled;

/**
* Simulate a mouse event based on a corresponding touch event
* @param {Object} event A touch event
* @param {String} simulatedType The corresponding mouse event
*/
function simulateMouseEvent (event, simulatedType) {

// Ignore multi-touch events
if (event.originalEvent.touches.length > 1) {
return;
}

event.preventDefault();

var touch = event.originalEvent.changedTouches[0],
simulatedEvent = document.createEvent('MouseEvents');

// Initialize the simulated mouse event using the touch event's coordinates
simulatedEvent.initMouseEvent(
simulatedType, // type
true, // bubbles
true, // cancelable
window, // view
1, // detail
touch.screenX, // screenX
touch.screenY, // screenY
touch.clientX, // clientX
touch.clientY, // clientY
false, // ctrlKey
false, // altKey
false, // shiftKey
false, // metaKey
0, // button
null // relatedTarget
);

// Dispatch the simulated event to the target element
event.target.dispatchEvent(simulatedEvent);
}

/**
* Handle the jQuery UI widget's touchstart events
* @param {Object} event The widget element's touchstart event
*/
mouseProto._touchStart = function (event) {

var self = this;

// Ignore the event if another widget is already being handled
if (touchHandled || !self._mouseCapture(event.originalEvent.changedTouches[0])) {
return;
}

// Set the flag to prevent other widgets from inheriting the touch event
touchHandled = true;

// Track movement to determine if interaction was a click
self._touchMoved = false;

// Simulate the mouseover event
simulateMouseEvent(event, 'mouseover');

// Simulate the mousemove event
simulateMouseEvent(event, 'mousemove');

// Simulate the mousedown event
simulateMouseEvent(event, 'mousedown');
};

/**
* Handle the jQuery UI widget's touchmove events
* @param {Object} event The document's touchmove event
*/
mouseProto._touchMove = function (event) {

// Ignore event if not handled
if (!touchHandled) {
return;
}

// Interaction was not a click
this._touchMoved = true;

// Simulate the mousemove event
simulateMouseEvent(event, 'mousemove');
};

/**
* Handle the jQuery UI widget's touchend events
* @param {Object} event The document's touchend event
*/
mouseProto._touchEnd = function (event) {

// Ignore event if not handled
if (!touchHandled) {
return;
}

// Simulate the mouseup event
simulateMouseEvent(event, 'mouseup');

// Simulate the mouseout event
simulateMouseEvent(event, 'mouseout');

// If the touch interaction did not move, it should trigger a click
if (!this._touchMoved) {

// Simulate the click event
simulateMouseEvent(event, 'click');
}

// Unset the flag to allow other widgets to inherit the touch event
touchHandled = false;
};

/**
* A duck punch of the $.ui.mouse _mouseInit method to support touch events.
* This method extends the widget with bound touch event handlers that
* translate touch events to mouse events and pass them to the widget's
* original mouse event handling methods.
*/
mouseProto._mouseInit = function () {

var self = this;

// Delegate the touch handlers to the widget's element
self.element.bind({
taphold: $.proxy(self, '_touchStart'),
touchmove: $.proxy(self, '_touchMove'),
touchend: $.proxy(self, '_touchEnd')
});

// Call the original $.ui.mouse init method
_mouseInit.call(self);
};

/**
* Remove the touch event handlers
*/
mouseProto._mouseDestroy = function () {

var self = this;

// Delegate the touch handlers to the widget's element
self.element.unbind({
taphold: $.proxy(self, '_touchStart'),
touchmove: $.proxy(self, '_touchMove'),
touchend: $.proxy(self, '_touchEnd')
});

// Call the original $.ui.mouse destroy method
_mouseDestroy.call(self);
};

})(jQuery);
Loading

0 comments on commit 093dedc

Please sign in to comment.