Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.

Commit 409cfce

Browse files
committed
Merge pull request #80 from thgreasi/angular1.2
Testing if sortable works after a reverted (not dropped) drag. As per the case mentioned in #61 by @bcronje..
2 parents fd750f3 + 59b32b0 commit 409cfce

File tree

4 files changed

+105
-2
lines changed

4 files changed

+105
-2
lines changed

gruntFile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports = function(grunt) {
2626
}
2727
},
2828
jshint: {
29-
files: ['src/**/*.js', 'test/**/*.js', 'demo/**/*.js'],
29+
files: ['src/**/*.js', 'test/**/*.js', 'demo/**/*.js', '!test/libs/*.js'],
3030
options: {
3131
curly: true,
3232
eqeqeq: true,
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
;(function($, undefined) {
2+
function findCenter(elem) {
3+
var offset,
4+
document = $(elem.ownerDocument);
5+
elem = $(elem);
6+
offset = elem.offset();
7+
8+
return {
9+
x: offset.left + elem.outerWidth() / 2 - document.scrollLeft(),
10+
y: offset.top + elem.outerHeight() / 2 - document.scrollTop()
11+
};
12+
}
13+
14+
$.extend($.simulate.prototype, {
15+
simulateDragAndRevert: function() {
16+
var i = 0,
17+
target = this.target,
18+
options = this.options,
19+
center = findCenter(target),
20+
x = Math.floor(center.x),
21+
y = Math.floor(center.y),
22+
dx = options.dx || 0,
23+
dy = options.dy || 0,
24+
moves = options.moves || 3,
25+
coord = {
26+
clientX: x,
27+
clientY: y
28+
};
29+
30+
this.simulateEvent(target, "mousedown", coord);
31+
32+
for (; i < moves; i++) {
33+
x += dx / moves;
34+
y += dy / moves;
35+
36+
coord = {
37+
clientX: Math.round(x),
38+
clientY: Math.round(y)
39+
};
40+
41+
this.simulateEvent(document, "mousemove", coord);
42+
}
43+
44+
for (i = 0; i < moves; i++) {
45+
x -= dx / moves;
46+
y -= dy / moves;
47+
48+
coord = {
49+
clientX: Math.round(x),
50+
clientY: Math.round(y)
51+
};
52+
53+
this.simulateEvent(document, "mousemove", coord);
54+
}
55+
56+
this.simulateEvent(target, "mouseup", coord);
57+
this.simulateEvent(target, "click", coord);
58+
}
59+
});
60+
})(jQuery);

test/sortable.spec.js

+41
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,47 @@ describe('uiSortable', function() {
189189
});
190190
});
191191

192+
it('should continue to work after a drag is reverted', function() {
193+
inject(function($compile, $rootScope) {
194+
var element;
195+
element = $compile('<ul ui-sortable ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}" class="sortable-item">{{ item }}</li></ul>')($rootScope);
196+
$rootScope.$apply(function() {
197+
$rootScope.opts = {
198+
placeholder: "sortable-item"
199+
};
200+
$rootScope.items = ["One", "Two", "Three"];
201+
});
202+
203+
host.append(element);
204+
205+
var li = element.find(':eq(0)');
206+
var dy = (2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
207+
li.simulate('dragAndRevert', { dy: dy });
208+
expect($rootScope.items).toEqual(["One", "Two", "Three"]);
209+
expect($rootScope.items).toEqualListContent(element);
210+
211+
li = element.find(':eq(0)');
212+
dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
213+
li.simulate('drag', { dy: dy });
214+
expect($rootScope.items).toEqual(["Two", "One", "Three"]);
215+
expect($rootScope.items).toEqualListContent(element);
216+
217+
li = element.find(':eq(1)');
218+
dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
219+
li.simulate('drag', { dy: dy });
220+
expect($rootScope.items).toEqual(["Two", "Three", "One"]);
221+
expect($rootScope.items).toEqualListContent(element);
222+
223+
li = element.find(':eq(1)');
224+
dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
225+
li.simulate('drag', { dy: dy });
226+
expect($rootScope.items).toEqual(["Two", "One", "Three"]);
227+
expect($rootScope.items).toEqualListContent(element);
228+
229+
$(element).remove();
230+
});
231+
});
232+
192233
});
193234

194235
describe('Multiple sortables related', function() {

test/test.conf.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ files = [
44
JASMINE_ADAPTER,
55
'bower_components/jquery/jquery.js',
66
'bower_components/jquery-simulate/jquery.simulate.js',
7+
'test/libs/jquery.simulate.dragandrevert.js',
78
'bower_components/jquery-ui/ui/jquery-ui.js',
89
'bower_components/angular/angular.js',
910
'bower_components/angular-mocks/angular-mocks.js',
@@ -13,9 +14,10 @@ files = [
1314
singleRun = true;
1415
autoWatch = false;
1516
browsers = [ 'Chrome' ];
17+
reporters = [ 'dots' ];
1618

1719
if (singleRun) {
18-
reporters = [ 'coverage' ];
20+
reporters.push('coverage');
1921
preprocessors = { '**/src/*.js': 'coverage' };
2022
coverageReporter = {
2123
type : 'html',

0 commit comments

Comments
 (0)