Skip to content

Commit 9450a8f

Browse files
committed
touch support
1 parent 82adedc commit 9450a8f

File tree

4 files changed

+30
-18
lines changed

4 files changed

+30
-18
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
Version 0.0.11 - 2014/11/28
2+
================
3+
4+
* touch support
5+
16
Version 0.0.9 - 2014/09/11
27
================
38

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-sortable-view",
3-
"version": "0.0.10",
3+
"version": "0.0.11",
44
"description": "Fully declarative (multi)sortable for AngularJS",
55
"homepage": "http://kamilkp.github.io/angular-sortable-view",
66
"author": {

src/angular-sortable-view.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// Copyright Kamil Pękala http://github.com/kamilkp
3-
// angular-sortable-view v0.0.10 2014/09/19
3+
// angular-sortable-view v0.0.11 2014/11/28
44
//
55

66
;(function(window, angular){
@@ -37,7 +37,6 @@
3737
var $helper; // helper element - the one thats being dragged around with the mouse pointer
3838
var $original; // original element
3939
var $target; // last best candidate
40-
var svOriginalNextSibling; // original element's original next sibling node
4140
var isGrid = false;
4241
var onSort = $parse($attrs.svOnSort);
4342

@@ -120,11 +119,8 @@
120119
});
121120
}
122121

123-
svOriginalNextSibling = svOriginal[0].nextSibling;
124122
svOriginal.after($placeholder);
125-
126-
svOriginal[0].parentNode.removeChild(svOriginal[0]);
127-
svOriginal.removeClass('sv-visibility-hidden');
123+
svOriginal.addClass('ng-hide');
128124

129125
// cache options, helper and original element reference
130126
$original = svOriginal;
@@ -137,8 +133,7 @@
137133
$index: originatingIndex,
138134
$item: originatingPart.model(originatingPart.scope)[originatingIndex]
139135
});
140-
if($scope.$root)
141-
$scope.$root.$$phase || $scope.$apply();
136+
$scope.$root && $scope.$root.$$phase || $scope.$apply();
142137
}
143138

144139
// ----- move the element
@@ -244,7 +239,7 @@
244239
sortingInProgress = false;
245240
$placeholder.remove();
246241
$helper.remove();
247-
svOriginalNextSibling.parentNode.insertBefore($original[0], svOriginalNextSibling);
242+
$original.removeClass('ng-hide');
248243

249244
candidates = void 0;
250245
$placeholder = void 0;
@@ -364,12 +359,12 @@
364359
});
365360

366361
var handle = $element;
367-
handle.on('mousedown', onMousedown);
362+
handle.on('mousedown touchstart', onMousedown);
368363
$scope.$watch('$ctrl.handle', function(customHandle){
369364
if(customHandle){
370-
handle.off('mousedown', onMousedown);
365+
handle.off('mousedown touchstart', onMousedown);
371366
handle = customHandle;
372-
handle.on('mousedown', onMousedown);
367+
handle.on('mousedown touchstart', onMousedown);
373368
}
374369
});
375370

@@ -393,8 +388,10 @@
393388
var moveExecuted;
394389

395390
function onMousedown(e){
391+
touchFix(e);
392+
396393
if($controllers[1].sortingInProgress()) return;
397-
if(e.button != 0) return;
394+
if(e.button != 0 && e.type === 'mousedown') return;
398395

399396
moveExecuted = false;
400397
var opts = $parse($attrs.svElement)($scope);
@@ -435,6 +432,7 @@
435432
var targetLeft = coords.x;
436433
var targetTop = coords.y;
437434
var helperRect = clone[0].getBoundingClientRect();
435+
438436
var body = document.body;
439437

440438
if(containmentRect){
@@ -456,9 +454,9 @@
456454
y: (e.clientY - clientRect.top)/clientRect.height
457455
};
458456
html.addClass('sv-sorting-in-progress');
459-
html.on('mousemove', onMousemove).on('mouseup', function mouseup(e){
460-
html.off('mousemove', onMousemove);
461-
html.off('mouseup', mouseup);
457+
html.on('mousemove touchmove', onMousemove).on('mouseup touchend touchcancel', function mouseup(e){
458+
html.off('mousemove touchmove', onMousemove);
459+
html.off('mouseup touchend', mouseup);
462460
html.removeClass('sv-sorting-in-progress');
463461
if(moveExecuted)
464462
$controllers[0].$drop($scope.$index, opts);
@@ -468,6 +466,7 @@
468466

469467
// onMousemove(e);
470468
function onMousemove(e){
469+
touchFix(e);
471470
if(!moveExecuted){
472471
$element.parent().prepend(clone);
473472
moveExecuted = true;
@@ -544,6 +543,14 @@
544543
'</style>'
545544
].join(''));
546545

546+
function touchFix(e){
547+
if(!('clientX' in e) && !('clientY' in e)){
548+
e.clientX = e.touches[0].clientX;
549+
e.clientY = e.touches[0].clientY;
550+
e.preventDefault();
551+
}
552+
}
553+
547554
function getPreviousSibling(element){
548555
element = element[0];
549556
if(element.previousElementSibling)

0 commit comments

Comments
 (0)