Skip to content

Commit 0d13d94

Browse files
committed
support mobile in jq version out of the box
* fix #1413 #444 * we are now using the latest v1.0.8 touch-punch branch from https://github.com/RWAP/jquery-ui-touch-punch and compiling in JQ version (only 188k -> 190k) so mobiles work out of the box * HTML5 v3+ does not currently support `touchmove` events. This will be added in a future release. * added a mobile.html demo showing usage, updated doc/readme
1 parent ba7707a commit 0d13d94

File tree

11 files changed

+145
-47
lines changed

11 files changed

+145
-47
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: 99 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,63 @@
11
/*!
2-
* jQuery UI Touch Punch 0.2.3
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
34
*
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
48
* Copyright 2011–2014, Dave Furfero
59
* Dual licensed under the MIT or GPL Version 2 licenses.
610
*
11+
* Fork: https://github.com/RWAP/jquery-ui-touch-punch
12+
*
713
* Depends:
8-
* jquery.ui.widget.js
9-
* jquery.ui.mouse.js
14+
* jquery.ui.widget.js
15+
* jquery.ui.mouse.js
1016
*/
11-
(function ($) {
1217

13-
// Detect touch support
14-
$.support.touch = 'ontouchend' in document;
18+
(function( factory ) {
19+
if ( typeof define === "function" && define.amd ) {
1520

16-
// Ignore browsers without touch support
17-
if (!$.support.touch) {
18-
return;
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;
1943
}
2044

2145
var mouseProto = $.ui.mouse.prototype,
2246
_mouseInit = mouseProto._mouseInit,
2347
_mouseDestroy = mouseProto._mouseDestroy,
2448
touchHandled;
2549

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+
2661
/**
2762
* Simulate a mouse event based on a corresponding touch event
2863
* @param {Object} event A touch event
@@ -35,28 +70,31 @@
3570
return;
3671
}
3772

38-
event.preventDefault();
73+
// Prevent "Ignored attempt to cancel a touchmove event with cancelable=false" errors
74+
if (event.cancelable) {
75+
event.preventDefault();
76+
}
3977

4078
var touch = event.originalEvent.changedTouches[0],
4179
simulatedEvent = document.createEvent('MouseEvents');
42-
80+
4381
// Initialize the simulated mouse event using the touch event's coordinates
4482
simulatedEvent.initMouseEvent(
4583
simulatedType, // type
46-
true, // bubbles
47-
true, // cancelable
48-
window, // view
49-
1, // detail
50-
touch.screenX, // screenX
51-
touch.screenY, // screenY
52-
touch.clientX, // clientX
53-
touch.clientY, // clientY
54-
false, // ctrlKey
55-
false, // altKey
56-
false, // shiftKey
57-
false, // metaKey
58-
0, // button
59-
null // relatedTarget
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
6098
);
6199

62100
// Dispatch the simulated event to the target element
@@ -71,6 +109,12 @@
71109

72110
var self = this;
73111

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+
74118
// Ignore the event if another widget is already being handled
75119
if (touchHandled || !self._mouseCapture(event.originalEvent.changedTouches[0])) {
76120
return;
@@ -103,7 +147,7 @@
103147
return;
104148
}
105149

106-
// Interaction was not a click
150+
// Interaction was moved
107151
this._touchMoved = true;
108152

109153
// Simulate the mousemove event
@@ -128,12 +172,27 @@
128172
simulateMouseEvent(event, 'mouseout');
129173

130174
// If the touch interaction did not move, it should trigger a click
131-
if (!this._touchMoved) {
132-
133-
// Simulate the click event
134-
simulateMouseEvent(event, '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+
}
135191
}
136192

193+
// Unset the flag to determine the touch movement stopped
194+
this._touchMoved = false;
195+
137196
// Unset the flag to allow other widgets to inherit the touch event
138197
touchHandled = false;
139198
};
@@ -145,11 +204,16 @@
145204
* original mouse event handling methods.
146205
*/
147206
mouseProto._mouseInit = function () {
148-
207+
149208
var self = this;
209+
210+
// Microsoft Surface Support = remove original touch Action
211+
if ($.support.mspointer) {
212+
self.element[0].style.msTouchAction = 'none';
213+
}
150214

151215
// Delegate the touch handlers to the widget's element
152-
self.element.bind({
216+
self.element.on({
153217
touchstart: $.proxy(self, '_touchStart'),
154218
touchmove: $.proxy(self, '_touchMove'),
155219
touchend: $.proxy(self, '_touchEnd')
@@ -163,11 +227,11 @@
163227
* Remove the touch event handlers
164228
*/
165229
mouseProto._mouseDestroy = function () {
166-
230+
167231
var self = this;
168232

169233
// Delegate the touch handlers to the widget's element
170-
self.element.unbind({
234+
self.element.off({
171235
touchstart: $.proxy(self, '_touchStart'),
172236
touchmove: $.proxy(self, '_touchMove'),
173237
touchend: $.proxy(self, '_touchEnd')
@@ -177,4 +241,4 @@
177241
_mouseDestroy.call(self);
178242
};
179243

180-
})(jQuery);
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)