Skip to content
This repository was archived by the owner on Feb 4, 2023. It is now read-only.

Commit 2b56ce5

Browse files
committed
feat: various bugs, enabled touch
#8, #2
1 parent b350483 commit 2b56ce5

18 files changed

+299
-148
lines changed

dist/jquery.gridstrap.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
.gridstrap-cell-hidden{
1414
opacity: 0;
1515
pointer-events: none;
16+
touch-action: none;
1617
}
1718
.gridstrap-cell-drag{
1819
transition:width 0.05s, height 0.05s, left 0.05s, top 0.05s ;

dist/jquery.gridstrap.js

Lines changed: 76 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ exports['default'] = {
2222
EVENT_MOUSEOVER: 'mouseover',
2323
EVENT_MOUSEMOVE: 'mousemove',
2424
EVENT_MOUSEUP: 'mouseup',
25+
EVENT_TOUCHSTART: 'touchstart',
26+
EVENT_TOUCHMOVE: 'touchmove',
27+
EVENT_TOUCHEND: 'touchend',
2528
EVENT_RESIZE: 'resize',
2629
EVENT_CELL_RESIZE: 'cellresize',
2730
EVENT_CELL_DRAG: 'celldrag',
@@ -140,7 +143,7 @@ var _methods = require('./methods');
140143
autoPadNonContiguousCells: true, // toggle adding non-contiguous cells automatically on drag or as needed.
141144
updateCoordinatesOnWindowResize: true, // enable window resize event handler.
142145
debug: false, // toggle console output.
143-
dragMouseoverThrottle: 500, // throttle cell mouseover events for rearranging.
146+
dragMouseoverThrottle: 150, // throttle cell mouseover events for rearranging.
144147
windowResizeDebounce: 50, // debounce redraw on window resize.
145148
mousemoveDebounce: 0 // debounce mousemove for dragging cells.
146149
};
@@ -183,6 +186,17 @@ var Handlers = (function () {
183186
}
184187
};
185188

189+
Handlers.prototype.onTouchStart = function onTouchStart(touchEvent, $cell, gridstrapContext) {
190+
var $ = this.setup.jQuery;
191+
var options = this.setup.Options;
192+
193+
touchEvent.preventDefault();
194+
195+
if (touchEvent.touches.length) {
196+
this.onMousedown(_utils.Utils.ConvertTouchToMouseEvent(touchEvent), $cell, gridstrapContext);
197+
}
198+
};
199+
186200
Handlers.prototype.onMousedown = function onMousedown(mouseEvent, $cell, gridstrapContext) {
187201
var $ = this.setup.jQuery;
188202
var context = this.setup.Context;
@@ -218,6 +232,8 @@ var Handlers = (function () {
218232
};
219233

220234
Handlers.prototype.onMouseover = function onMouseover(mouseEvent, $cell, gridstrapContext) {
235+
var _this = this;
236+
221237
var $ = this.setup.jQuery;
222238
var context = this.setup.Context;
223239
var options = this.setup.Options;
@@ -238,21 +254,24 @@ var Handlers = (function () {
238254

239255
this.internal.LastMouseOverCellTarget = $cell;
240256

241-
if (!_utils.Utils.IsElementThrottled($, $cell, options.dragMouseoverThrottle)) {
242-
// do not move two cells that have recently already moved.
243-
257+
_utils.Utils.Limit(function () {
244258
if (gridstrapContext.options.rearrangeOnDrag) {
245259

246-
this.internal.MoveCell($draggedCell, $cell, gridstrapContext);
260+
_this.internal.MoveCell($draggedCell, $cell, gridstrapContext);
247261

248262
// reset dragged object to mouse pos, not pos of hidden cells.
249-
this.internal.MoveDraggedCell(mouseEvent, $draggedCell);
263+
_this.internal.MoveDraggedCell(mouseEvent, $draggedCell);
250264
}
251-
}
265+
}, options.dragMouseoverThrottle);
252266
}
253267
}
254268
};
255269

270+
Handlers.prototype.onTouchmove = function onTouchmove(touchEvent) {
271+
272+
this.onMousemove(_utils.Utils.ConvertTouchToMouseEvent(touchEvent));
273+
};
274+
256275
Handlers.prototype.onMousemove = function onMousemove(mouseEvent) {
257276
var $ = this.setup.jQuery;
258277
var context = this.setup.Context;
@@ -294,6 +313,11 @@ var Handlers = (function () {
294313
}
295314
};
296315

316+
Handlers.prototype.onTouchend = function onTouchend(touchEvent) {
317+
// don't convert to mouseEVent becuase there are no touches.
318+
this.onMouseup(touchEvent);
319+
};
320+
297321
Handlers.prototype.onMouseup = function onMouseup(mouseEvent) {
298322
var $ = this.setup.jQuery;
299323
var context = this.setup.Context;
@@ -409,11 +433,14 @@ var Internal = (function () {
409433
this.HandleCellMouseEvent(context, '' + appendNamespace(_constants2['default'].EVENT_DRAGSTART), true, eventHandlers.onDragstart.bind(eventHandlers));
410434

411435
this.HandleCellMouseEvent(context, '' + appendNamespace(_constants2['default'].EVENT_MOUSEDOWN), true, eventHandlers.onMousedown.bind(eventHandlers));
436+
437+
this.HandleCellMouseEvent(context, '' + appendNamespace(_constants2['default'].EVENT_TOUCHSTART), true, eventHandlers.onTouchStart.bind(eventHandlers));
438+
412439
// pass false as param because we need to do non-contiguous stuff in there.
413440
this.HandleCellMouseEvent(context, '' + appendNamespace(_constants2['default'].EVENT_MOUSEOVER), false, eventHandlers.onMouseover.bind(eventHandlers));
414441

415442
// it is not appropriate to confine the events to the visible cell wrapper.
416-
$(options.mouseMoveSelector).on('' + appendNamespace(_constants2['default'].EVENT_MOUSEMOVE), _utils.Utils.Debounce(eventHandlers.onMousemove.bind(eventHandlers), options.mousemoveDebounce)).on('' + appendNamespace(_constants2['default'].EVENT_MOUSEUP), eventHandlers.onMouseup.bind(eventHandlers));
443+
$(options.mouseMoveSelector).on('' + appendNamespace(_constants2['default'].EVENT_MOUSEMOVE), _utils.Utils.Debounce(eventHandlers.onMousemove.bind(eventHandlers), options.mousemoveDebounce)).on('' + appendNamespace(_constants2['default'].EVENT_TOUCHMOVE), _utils.Utils.Debounce(eventHandlers.onTouchmove.bind(eventHandlers), options.mousemoveDebounce)).on('' + appendNamespace(_constants2['default'].EVENT_MOUSEUP), eventHandlers.onMouseup.bind(eventHandlers)).on('' + appendNamespace(_constants2['default'].EVENT_TOUCHEND), eventHandlers.onTouchend.bind(eventHandlers));
417444

418445
if (options.updateCoordinatesOnWindowResize) {
419446
$(window).on('' + appendNamespace(_constants2['default'].EVENT_RESIZE), _utils.Utils.Debounce(context.updateVisibleCellCoordinates, options.windowResizeDebounce));
@@ -500,24 +527,23 @@ var Internal = (function () {
500527
});
501528
};
502529

503-
Internal.prototype.$GetNonDraggedCellFromPoint = function $GetNonDraggedCellFromPoint($draggedCell, mouseEvent) {
530+
Internal.prototype.GetNonDraggedElementFromPoint = function GetNonDraggedElementFromPoint($draggedCell, mouseEvent) {
504531
var document = this.setup.Document;
505532
var $ = this.setup.jQuery;
506533

507534
//remove mouse events from dragged cell, because we need to test for overlap of underneath things.
508535
var oldPointerEvents = $draggedCell.css('pointer-events');
536+
var oldTouchAction = $draggedCell.css('touch-action');
509537
$draggedCell.css('pointer-events', 'none');
538+
$draggedCell.css('touch-action', 'none');
510539

511540
var element = document.elementFromPoint(mouseEvent.clientX, mouseEvent.clientY);
512-
var cellAndIndex = this.GetCellAndInternalIndex(element);
513541

514542
// restore pointer-events css.
515543
$draggedCell.css('pointer-events', oldPointerEvents);
544+
$draggedCell.css('touch-action', oldTouchAction);
516545

517-
if (!cellAndIndex) {
518-
return $();
519-
}
520-
return cellAndIndex.$cell;
546+
return element;
521547
};
522548

523549
Internal.prototype.MoveDraggedCell = function MoveDraggedCell(mouseEvent, $cell) {
@@ -553,7 +579,8 @@ var Internal = (function () {
553579
}));
554580
};
555581

556-
var $overlappedCell = this.$GetNonDraggedCellFromPoint($cell, mouseEvent);
582+
var overlappedElement = this.GetNonDraggedElementFromPoint($cell, mouseEvent);
583+
var $overlappedCell = context.$getCellOfElement(overlappedElement);
557584

558585
if ($overlappedCell.length) {
559586
// have to create event here like this other mouse coords are missing.
@@ -568,7 +595,7 @@ var Internal = (function () {
568595
var additionalContext = $(this).data(_constants2['default'].DATA_GRIDSTRAP);
569596
if (additionalContext) {
570597
// $getCellOfElement is a 'public' method.
571-
var $additionalContextCell = additionalContext.$getCellOfElement(element);
598+
var $additionalContextCell = additionalContext.$getCellOfElement(overlappedElement);
572599
if ($additionalContextCell.length) {
573600
// have to create event here like this other mouse coords are missing.
574601
triggerMouseOverEvent($additionalContextCell);
@@ -1427,46 +1454,42 @@ var Utils = (function () {
14271454
return cssClass.replace(/(^ *| +)/g, '.');
14281455
};
14291456

1430-
Utils.Debounce = function Debounce(callback, milliseconds, leading) {
1431-
var timeout = undefined;
1457+
Utils.Debounce = function Debounce(callback, milliseconds, leading, timeout) {
1458+
if (typeof timeout === 'undefined') {
1459+
timeout = null;
1460+
}
14321461
return function () {
14331462
var context = this;
14341463
var args = arguments;
1435-
var callNow = leading || !milliseconds;
14361464
var later = function later() {
14371465
timeout = null;
1438-
if (!callNow) {
1466+
if (!leading) {
14391467
callback.apply(context, args);
14401468
}
14411469
};
1470+
var callNow = leading && !timeout;
1471+
1472+
if (milliseconds == 500) console.log('callNow: ' + callNow);
1473+
14421474
clearTimeout(timeout);
14431475
timeout = setTimeout(later, milliseconds);
14441476
if (callNow) {
14451477
callback.apply(context, args);
14461478
}
1479+
1480+
return timeout;
14471481
};
14481482
};
14491483

1450-
Utils.IsElementThrottled = function IsElementThrottled($, element, milliseconds) {
1451-
1452-
Utils.recentDragMouseOvers = Utils.recentDragMouseOvers || [];
1453-
1484+
Utils.Limit = function Limit(callback, milliseconds) {
14541485
var d = new Date();
14551486
var n = d.getTime();
1456-
for (var i = 0; i < Utils.recentDragMouseOvers.length; i++) {
1457-
if (Utils.recentDragMouseOvers[i].n + milliseconds < n) {
1458-
// expired.
1459-
Utils.recentDragMouseOvers.splice(i, 1);
1460-
}
1461-
if (i < Utils.recentDragMouseOvers.length && $(Utils.recentDragMouseOvers[i].e).is(element)) {
1462-
return true;
1463-
}
1487+
if (n - (Utils.limit || 0) > milliseconds) {
1488+
1489+
callback();
1490+
1491+
Utils.limit = n;
14641492
}
1465-
Utils.recentDragMouseOvers.push({
1466-
n: n,
1467-
e: element
1468-
});
1469-
return false;
14701493
};
14711494

14721495
Utils.SwapJQueryElements = function SwapJQueryElements($a, $b) {
@@ -1565,6 +1588,22 @@ var Utils = (function () {
15651588
};
15661589
};
15671590

1591+
Utils.ConvertTouchToMouseEvent = function ConvertTouchToMouseEvent(touchEvent) {
1592+
var touch = null;
1593+
for (var i = 0; !touch && i < touchEvent.changedTouches.length; i++) {
1594+
if (touchEvent.changedTouches[i].identifier === 0) {
1595+
touch = touchEvent.changedTouches[i];
1596+
}
1597+
}
1598+
1599+
touchEvent.pageX = touch.pageX;
1600+
touchEvent.pageY = touch.pageY;
1601+
touchEvent.clientX = touch.clientX;
1602+
touchEvent.clientY = touch.clientY;
1603+
1604+
return touchEvent;
1605+
};
1606+
15681607
return Utils;
15691608
})();
15701609

dist/jquery.gridstrap.min.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
* Made by Ross P
77
* Under MIT License
88
*/
9-
.gridstrap-cell-visible{position:absolute!important;transition:width .2s,height .2s,left .2s,top .2s}.gridstrap-cell-hidden{opacity:0;pointer-events:none}.gridstrap-cell-drag{transition:width 50ms,height 50ms,left 50ms,top 50ms;z-index:10000}.gridstrap-cell-resize{transition:width 50ms,height 50ms,left 50ms,top 50ms;z-index:10000}.gridstack-noncontiguous{opacity:.02}
9+
.gridstrap-cell-visible{position:absolute!important;transition:width .2s,height .2s,left .2s,top .2s}.gridstrap-cell-hidden{opacity:0;pointer-events:none;touch-action:none}.gridstrap-cell-drag{transition:width 50ms,height 50ms,left 50ms,top 50ms;z-index:10000}.gridstrap-cell-resize{transition:width 50ms,height 50ms,left 50ms,top 50ms;z-index:10000}.gridstack-noncontiguous{opacity:0}

dist/jquery.gridstrap.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/jquery.gridstrap.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,6 @@ <h1 id="header-1"><a href="#header-1"></a>Demo</h1>
320320
src="//code.jquery.com/jquery-3.2.1.js"
321321
integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
322322
crossorigin="anonymous"></script>
323-
<script src="//cdnjs.cloudflare.com/ajax/libs/jqueryui-touch-punch/0.2.3/jquery.ui.touch-punch.min.js"></script>
324323

325324
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
326325
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/prism/1.6.0/themes/prism.css" />

docs/indexLocal.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,6 @@ <h1 id="header-1"><a href="#header-1"></a>Demo</h1>
320320
src="http://code.jquery.com/jquery-3.2.1.js"
321321
integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
322322
crossorigin="anonymous"></script>
323-
<script src="http://cdnjs.cloudflare.com/ajax/libs/jqueryui-touch-punch/0.2.3/jquery.ui.touch-punch.min.js"></script>
324323

325324
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
326325
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/prism/1.6.0/themes/prism.css" />

docs/indexscript.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@ var popGrid = function ($grid, html, quantity) {
22
var computed = html;
33
for (var i = 0; i < quantity; i++) {
44
if (typeof (html) == 'function') {
5-
computed = html();
5+
computed = html(i);
66
}
77
$($grid).append(computed);
88
}
99
};
1010

1111
$(function () {
12-
popGrid('#basic-grid', '<div class="col-xs-4 col-sm-2 col-md-1 cell"><div class="inner"></div></div>', 36);
12+
popGrid('#basic-grid', function (i) {
13+
return '<div class="col-xs-4 col-sm-2 col-md-1 cell"><div class="inner"></div></div>';
14+
}, 36);
1315

1416
popGrid('#nested-grid', '<div class="col-xs-4 col-sm-2 cell"><div class="inner"><div class="nested-inner-grid"></div></div></div>', 1);
1517
popGrid('#nested-grid', '<div class="col-xs-4 col-sm-2 cell"><div class="inner"></div></div>', 10);
1618
popGrid('.nested-inner-grid', '<div class="col-xs-4 col-sm-2 cell nested"><div class="inner nested"></div></div>', 9);
1719

1820
popGrid('#resize-grid', '<div class="col-xs-2 cell"><div class="inner"><div class="resize"></div></div></div>', 24);
1921

20-
popGrid('#noncontiguous-grid', '<div class="col-xs-1 cell"><div class="inner"></div></div>', 12);
22+
popGrid('#noncontiguous-grid', '<div class="col-xs-4 col-sm-2 col-md-1 cell"><div class="inner"></div></div>', 12);
2123

2224
popGrid('#dual1-grid', '<div class="col-xs-1 cell"><div class="inner"></div></div>', 24);
2325
popGrid('#dual2-grid', '<div class="col-xs-2 cell"><div class="inner"></div></div>', 12);
@@ -31,7 +33,8 @@ $(function () {
3133
}, 36);
3234

3335
$('#basic-grid').gridstrap({
34-
36+
dragMouseoverThrottle: 150,
37+
//swapMode: true
3538
});
3639

3740
$('a[href="#responsive-demo"]').on('shown.bs.tab', function () {

docs/jquery.gridstrap.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
.gridstrap-cell-hidden{
1414
opacity: 0;
1515
pointer-events: none;
16+
touch-action: none;
1617
}
1718
.gridstrap-cell-drag{
1819
transition:width 0.05s, height 0.05s, left 0.05s, top 0.05s ;

0 commit comments

Comments
 (0)