Skip to content

Commit 5a35900

Browse files
authored
Merge pull request #1589 from adumesny/develop
support mobile in jq version out of the box
2 parents 5ee5e60 + 0d13d94 commit 5a35900

File tree

11 files changed

+290
-12
lines changed

11 files changed

+290
-12
lines changed

Gruntfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ module.exports = function(grunt) {
5656
files: {
5757
'dist/jq/jquery.js': 'src/jq/jquery.js',
5858
'dist/jq/jquery-ui.js': 'src/jq/jquery-ui.js',
59+
'dist/jq/jquery.ui.touch-punch.js': 'src/jq/jquery.ui.touch-punch.js',
5960
}
6061
}
6162
},

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,12 @@ Note: It's not recommended to enable `nw`, `n`, `ne` resizing handles. Their beh
260260

261261
## Touch devices support
262262

263-
Please use [jQuery UI Touch Punch](https://github.com/furf/jquery-ui-touch-punch) to make jQuery UI Draggable/Resizable
264-
working on touch-based devices.
263+
NOTE: gridstack v3.2+ jq version now compile this in, so it works out of the box, so need for anything.
264+
265+
NOTE2: HTML5 v3+ does not currently support `touchmove` events. This will be added in a future release.
266+
267+
Use latest RWAP branch of [jQuery UI Touch Punch](https://github.com/RWAP/jquery-ui-touch-punch) to make jQuery UI Draggable/Resizable
268+
working on touch-based devices (which we now also include in v3.2 as `dist/jq/jquery.ui.touch-punch.js`).
265269

266270
```html
267271
<script src="gridstack-jq.js"></script>
@@ -277,7 +281,7 @@ let options = {
277281
GridStack.init(options);
278282
```
279283

280-
If you're still experiencing issues on touch devices please check [#444](https://github.com/gridstack/gridstack.js/issues/444)
284+
See [example](http://gridstack.github.io/gridstack.js/demo/mobile.html). If you're still experiencing issues on touch devices please check [#444](https://github.com/gridstack/gridstack.js/issues/444)
281285

282286
# gridstack.js for specific frameworks
283287

demo/advance.html

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,6 @@ <h1>Advanced Demo</h1>
5757
<script type="text/javascript">
5858

5959
let grid = GridStack.init({
60-
alwaysShowResizeHandle: /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
61-
navigator.userAgent
62-
),
63-
resizable: {
64-
handles: 'e, se, s, sw, w'
65-
},
6660
acceptWidgets: true,
6761
dragIn: '.newWidget', // class that can be dragged from outside
6862
dragInOptions: { revert: 'invalid', scroll: false, appendTo: 'body', helper: 'clone' },

demo/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ <h1>Demos</h1>
2020
<li><a href="serialization.html">Serialization</a></li>
2121
<li><a href="static.html">Static</a></li>
2222
<li><a href="two.html">Two grids</a></li>
23+
<li><a href="mobile.html">Mobile touch (JQ)</a></li>
2324
<li><a href="vue3js.html">Vue3.js</a></li>
2425
<li><a href="vue2js.html">Vue2.js</a></li>
2526
<li><a href="web-comp.html">Web Component</a></li>

demo/mobile.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<title>Simple mobile demo</title>
8+
9+
<link rel="stylesheet" href="demo.css"/>
10+
<script src="../dist/gridstack-jq.js"></script>
11+
12+
</head>
13+
<body>
14+
<h1>Simple mobile demo</h1>
15+
<p>uses v3.2+ jquery version which now includes jquery.ui.touch-punch by default (small 2k)</p>
16+
<div class="grid-stack"></div>
17+
<script type="text/javascript">
18+
let grid = GridStack.init({
19+
// column: 1, // will auto switch on smaller screens
20+
cellHeight: 150, // make sure we have a decent height and not width/12 for 1 column
21+
alwaysShowResizeHandle: /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
22+
});
23+
grid.load([{x:0, y:0, content: '1'}, {x:0, y:1, h:2, content: '2'}, {x:0, y:3, content: '3'}])
24+
</script>
25+
</body>
26+
</html>

doc/CHANGES.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ Change log
4848
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
4949
## 3.1.5-dev
5050

51-
- TBD
51+
- fix [1413](https://github.com/gridstack/gridstack.js/issues/1413) website & lib works on mobile. We now compile the latest v1.0.8 `jquery.ui.touch-punch`
52+
into the JQ version (only 2k) so mobile devices (android, iphone, ipad, touchpad) are supported out of the box.
53+
HTML5 version will require re-write to plain `mousemove` & mobile `touchmove` instead of drag events in a future release.
5254

5355
## 3.1.5 (2021-1-23)
5456

doc/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ gridstack.js API
6767
* `true` the resizing handles are always shown even if the user is not hovering over the widget
6868
* advance condition such as this mobile browser agent check:
6969
`alwaysShowResizeHandle: /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test( navigator.userAgent )`
70-
See [example](http://gridstack.github.io/gridstack.js/demo/advance.html)
70+
See [example](http://gridstack.github.io/gridstack.js/demo/mobile.html)
7171
- `animate` - turns animation on to smooth transitions (default: `true`)
7272
- `auto` - if `false` gridstack will not initialize existing items (default: `true`)
7373
- `cellHeight` - one cell height (default: `auto`). Can be:

src/jq/gridstack-dd-jqueryui.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { GridItemHTMLElement, DDDragInOpt } from '../types';
1717
import * as $ from './jquery'; // compile this in... having issues TS/ES6 app would include instead
1818
export { $ };
1919
import './jquery-ui';
20+
import './jquery.ui.touch-punch'; // include for touch mobile devices
2021

2122
// export our base class (what user should use) and all associated types
2223
export * from '../gridstack-dd';

src/jq/jquery.ui.touch-punch.js

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
/*!
2+
* jQuery UI Touch Punch 1.0.8 as modified by RWAP Software
3+
* based on original touchpunch v0.2.3 which has not been updated since 2014
4+
*
5+
* Updates by RWAP Software to take account of various suggested changes on the original code issues
6+
*
7+
* Original: https://github.com/furf/jquery-ui-touch-punch
8+
* Copyright 2011–2014, Dave Furfero
9+
* Dual licensed under the MIT or GPL Version 2 licenses.
10+
*
11+
* Fork: https://github.com/RWAP/jquery-ui-touch-punch
12+
*
13+
* Depends:
14+
* jquery.ui.widget.js
15+
* jquery.ui.mouse.js
16+
*/
17+
18+
(function( factory ) {
19+
if ( typeof define === "function" && define.amd ) {
20+
21+
// AMD. Register as an anonymous module.
22+
define([ "jquery", "jquery.ui" ], factory );
23+
} else {
24+
25+
// Browser globals
26+
factory( jQuery );
27+
}
28+
}(function ($) {
29+
30+
// Detect touch support - Windows Surface devices and other touch devices
31+
$.support.mspointer = window.navigator.msPointerEnabled;
32+
$.support.touch = ( 'ontouchstart' in document
33+
|| 'ontouchstart' in window
34+
|| window.TouchEvent
35+
|| (window.DocumentTouch && document instanceof DocumentTouch)
36+
|| navigator.maxTouchPoints > 0
37+
|| navigator.msMaxTouchPoints > 0
38+
);
39+
40+
// Ignore browsers without touch or mouse support
41+
if ((!$.support.touch && !$.support.mspointer) || !$.ui.mouse) {
42+
return;
43+
}
44+
45+
var mouseProto = $.ui.mouse.prototype,
46+
_mouseInit = mouseProto._mouseInit,
47+
_mouseDestroy = mouseProto._mouseDestroy,
48+
touchHandled;
49+
50+
/**
51+
* Get the x,y position of a touch event
52+
* @param {Object} event A touch event
53+
*/
54+
function getTouchCoords (event) {
55+
return {
56+
x: event.originalEvent.changedTouches[0].pageX,
57+
y: event.originalEvent.changedTouches[0].pageY
58+
};
59+
}
60+
61+
/**
62+
* Simulate a mouse event based on a corresponding touch event
63+
* @param {Object} event A touch event
64+
* @param {String} simulatedType The corresponding mouse event
65+
*/
66+
function simulateMouseEvent (event, simulatedType) {
67+
68+
// Ignore multi-touch events
69+
if (event.originalEvent.touches.length > 1) {
70+
return;
71+
}
72+
73+
// Prevent "Ignored attempt to cancel a touchmove event with cancelable=false" errors
74+
if (event.cancelable) {
75+
event.preventDefault();
76+
}
77+
78+
var touch = event.originalEvent.changedTouches[0],
79+
simulatedEvent = document.createEvent('MouseEvents');
80+
81+
// Initialize the simulated mouse event using the touch event's coordinates
82+
simulatedEvent.initMouseEvent(
83+
simulatedType, // type
84+
true, // bubbles
85+
true, // cancelable
86+
window, // view
87+
1, // detail
88+
touch.screenX, // screenX
89+
touch.screenY, // screenY
90+
touch.clientX, // clientX
91+
touch.clientY, // clientY
92+
false, // ctrlKey
93+
false, // altKey
94+
false, // shiftKey
95+
false, // metaKey
96+
0, // button
97+
null // relatedTarget
98+
);
99+
100+
// Dispatch the simulated event to the target element
101+
event.target.dispatchEvent(simulatedEvent);
102+
}
103+
104+
/**
105+
* Handle the jQuery UI widget's touchstart events
106+
* @param {Object} event The widget element's touchstart event
107+
*/
108+
mouseProto._touchStart = function (event) {
109+
110+
var self = this;
111+
112+
// Interaction time
113+
this._startedMove = event.timeStamp;
114+
115+
// Track movement to determine if interaction was a click
116+
self._startPos = getTouchCoords(event);
117+
118+
// Ignore the event if another widget is already being handled
119+
if (touchHandled || !self._mouseCapture(event.originalEvent.changedTouches[0])) {
120+
return;
121+
}
122+
123+
// Set the flag to prevent other widgets from inheriting the touch event
124+
touchHandled = true;
125+
126+
// Track movement to determine if interaction was a click
127+
self._touchMoved = false;
128+
129+
// Simulate the mouseover event
130+
simulateMouseEvent(event, 'mouseover');
131+
132+
// Simulate the mousemove event
133+
simulateMouseEvent(event, 'mousemove');
134+
135+
// Simulate the mousedown event
136+
simulateMouseEvent(event, 'mousedown');
137+
};
138+
139+
/**
140+
* Handle the jQuery UI widget's touchmove events
141+
* @param {Object} event The document's touchmove event
142+
*/
143+
mouseProto._touchMove = function (event) {
144+
145+
// Ignore event if not handled
146+
if (!touchHandled) {
147+
return;
148+
}
149+
150+
// Interaction was moved
151+
this._touchMoved = true;
152+
153+
// Simulate the mousemove event
154+
simulateMouseEvent(event, 'mousemove');
155+
};
156+
157+
/**
158+
* Handle the jQuery UI widget's touchend events
159+
* @param {Object} event The document's touchend event
160+
*/
161+
mouseProto._touchEnd = function (event) {
162+
163+
// Ignore event if not handled
164+
if (!touchHandled) {
165+
return;
166+
}
167+
168+
// Simulate the mouseup event
169+
simulateMouseEvent(event, 'mouseup');
170+
171+
// Simulate the mouseout event
172+
simulateMouseEvent(event, 'mouseout');
173+
174+
// If the touch interaction did not move, it should trigger a click
175+
// Check for this in two ways - length of time of simulation and distance moved
176+
// Allow for Apple Stylus to be used also
177+
var timeMoving = event.timeStamp - this._startedMove;
178+
if (!this._touchMoved || timeMoving < 500) {
179+
// Simulate the click event
180+
simulateMouseEvent(event, 'click');
181+
} else {
182+
var endPos = getTouchCoords(event);
183+
if ((Math.abs(endPos.x - this._startPos.x) < 10) && (Math.abs(endPos.y - this._startPos.y) < 10)) {
184+
185+
// If the touch interaction did not move, it should trigger a click
186+
if (!this._touchMoved || event.originalEvent.changedTouches[0].touchType === 'stylus') {
187+
// Simulate the click event
188+
simulateMouseEvent(event, 'click');
189+
}
190+
}
191+
}
192+
193+
// Unset the flag to determine the touch movement stopped
194+
this._touchMoved = false;
195+
196+
// Unset the flag to allow other widgets to inherit the touch event
197+
touchHandled = false;
198+
};
199+
200+
/**
201+
* A duck punch of the $.ui.mouse _mouseInit method to support touch events.
202+
* This method extends the widget with bound touch event handlers that
203+
* translate touch events to mouse events and pass them to the widget's
204+
* original mouse event handling methods.
205+
*/
206+
mouseProto._mouseInit = function () {
207+
208+
var self = this;
209+
210+
// Microsoft Surface Support = remove original touch Action
211+
if ($.support.mspointer) {
212+
self.element[0].style.msTouchAction = 'none';
213+
}
214+
215+
// Delegate the touch handlers to the widget's element
216+
self.element.on({
217+
touchstart: $.proxy(self, '_touchStart'),
218+
touchmove: $.proxy(self, '_touchMove'),
219+
touchend: $.proxy(self, '_touchEnd')
220+
});
221+
222+
// Call the original $.ui.mouse init method
223+
_mouseInit.call(self);
224+
};
225+
226+
/**
227+
* Remove the touch event handlers
228+
*/
229+
mouseProto._mouseDestroy = function () {
230+
231+
var self = this;
232+
233+
// Delegate the touch handlers to the widget's element
234+
self.element.off({
235+
touchstart: $.proxy(self, '_touchStart'),
236+
touchmove: $.proxy(self, '_touchMove'),
237+
touchend: $.proxy(self, '_touchEnd')
238+
});
239+
240+
// Call the original $.ui.mouse destroy method
241+
_mouseDestroy.call(self);
242+
};
243+
244+
}));

src/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ export interface GridStackOptions {
3838
*/
3939
acceptWidgets?: boolean | string | ((element: Element) => boolean);
4040

41-
/** if true the resizing handles are shown even if the user is not hovering over the widget (default?: false) */
41+
/** possible values (default: `false` only show on hover)
42+
* `true` the resizing handles are always shown even if the user is not hovering over the widget
43+
* advance condition such as this mobile browser agent check:
44+
`alwaysShowResizeHandle: /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test( navigator.userAgent )`
45+
See [example](http://gridstack.github.io/gridstack.js/demo/mobile.html) */
4246
alwaysShowResizeHandle?: boolean;
4347

4448
/** turns animation on (default?: true) */

0 commit comments

Comments
 (0)