Skip to content

Commit 55a9261

Browse files
author
Lukas Oppermann
committed
Merge pull request #123 from lukasoppermann/master
add enable method
2 parents d9a0d86 + aeb9376 commit 55a9261

File tree

3 files changed

+110
-85
lines changed

3 files changed

+110
-85
lines changed

src/html.sortable.src.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,20 @@ var _destroySortable = function(sortable) {
132132
_removeSortableEvents(sortable);
133133
_removeSortableData(sortable);
134134
// remove event handlers & data from items
135-
_removeItemEvents(items);
136135
handles.off('selectstart.h5s');
136+
_removeItemEvents(items);
137137
_removeItemData(items);
138138
};
139+
/*
140+
* enable the sortable
141+
* @param [jquery Collection] a single sortable
142+
*/
143+
var _enableSortable = function(sortable) {
144+
var opts = sortable.data('opts');
145+
var items = sortable.children(opts.items);
146+
sortable.attr('aria-dropeffect', 'move');
147+
items.attr('draggable', true);
148+
};
139149
/*
140150
* public sortable object
141151
* @param [object|string] options|method
@@ -176,9 +186,8 @@ var sortable = function(options) {
176186

177187
// enable sortable when method is called
178188
// TODO: move into function (currently not inside if because it needs to run for init)
179-
items.attr('draggable', true);
180-
$sortable.attr('aria-dropeffect', 'move');
181189
if (method === 'enable') {
190+
_enableSortable($sortable);
182191
return;
183192
}
184193
// disable sortable when method is called
@@ -203,6 +212,7 @@ var sortable = function(options) {
203212
$(options.connectWith).add(this).data('connectWith', options.connectWith);
204213
}
205214

215+
_enableSortable($sortable);
206216
items.attr('role', 'option');
207217
items.attr('aria-grabbed', 'false');
208218

@@ -338,7 +348,8 @@ sortable.__testing = {
338348
_attachGhost: _attachGhost,
339349
_addGhostPos: _addGhostPos,
340350
_getGhost: _getGhost,
341-
_makeGhost: _makeGhost
351+
_makeGhost: _makeGhost,
352+
_enableSortable:_enableSortable
342353
};
343354
module.exports = sortable;
344355
/* end-testing */

test/ghost.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
describe('Testing ghost creation methods', function(){
2+
// testing ghost creation functions
3+
var assert = require('chai').assert;
4+
GLOBAL.document = require('jsdom').jsdom('<html lang="en-US"></html>');
5+
GLOBAL.window = GLOBAL.document.defaultView;
6+
GLOBAL.$ = GLOBAL.jQuery = require('../node_modules/jquery/dist/jquery.js');
7+
var sortable = require("../src/html.sortable.src.js");
8+
$('body').html('').append('<ul class="sortable"><li>item</li></ul>');
9+
// moch dragged item
10+
var dItem = $('<li>dItem Test</li>');
11+
dItem.offset = function(){
12+
return {
13+
left: 5,
14+
top: 5
15+
}
16+
};
17+
// mock event
18+
var e = {
19+
pageX: 100,
20+
pageY: 200,
21+
dataTransfer: {
22+
text: undefined,
23+
item: undefined,
24+
x: undefined,
25+
y: undefined,
26+
setData: function(type, val){
27+
e.dataTransfer[type] = val;
28+
},
29+
setDragImage: function(item, x, y){
30+
e.dataTransfer.item =item;
31+
e.dataTransfer.x = x;
32+
e.dataTransfer.y = y;
33+
}
34+
}
35+
};
36+
37+
it('sets the dataTransfer options correctly', function(){
38+
sortable.__testing._attachGhost(e, {
39+
item: 'test-item',
40+
x: 10,
41+
y: 20
42+
});
43+
44+
assert.equal(e.dataTransfer.effectAllowed, 'move');
45+
assert.equal(e.dataTransfer.text, '');
46+
assert.equal(e.dataTransfer.item, 'test-item');
47+
assert.equal(e.dataTransfer.x, 10);
48+
assert.equal(e.dataTransfer.y, 20);
49+
});
50+
51+
it('sets item correctly from dragged item', function(){
52+
var ghost = sortable.__testing._makeGhost(dItem);
53+
assert.equal(dItem[0].innerHTML, 'dItem Test');
54+
});
55+
56+
it('sets x & y correctly', function(){
57+
var ghost = sortable.__testing._addGhostPos(e, {
58+
item: 'test-item',
59+
draggedItem: dItem
60+
});
61+
62+
assert.equal(ghost.x, 95);
63+
assert.equal(ghost.y, 195);
64+
});
65+
66+
it('uses provided x & y correctly', function(){
67+
var ghost = sortable.__testing._addGhostPos(e, {
68+
item: 'test-item',
69+
draggedItem: dItem,
70+
x: 10,
71+
y: 20
72+
});
73+
74+
assert.equal(ghost.x, 10);
75+
assert.equal(ghost.y, 20);
76+
});
77+
78+
it('attaches ghost completly', function(){
79+
sortable.__testing._getGhost(e, dItem);
80+
81+
assert.equal(e.dataTransfer.effectAllowed, 'move');
82+
assert.equal(e.dataTransfer.text, '');
83+
assert.equal(e.dataTransfer.item, dItem[0]);
84+
assert.equal(e.dataTransfer.x, 95);
85+
assert.equal(e.dataTransfer.y, 195);
86+
});
87+
});

test/internal.js

Lines changed: 8 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ describe('internal function tests', function(){
88

99
beforeEach(function(){
1010
$('body').html('').append('<ul class="sortable"><li>item</li></ul>');
11+
$('.sortable').sortable('destroy');
1112
$ul = $('.sortable').sortable();
1213
$li = $ul.find('li').first();
1314
});
@@ -51,6 +52,13 @@ describe('internal function tests', function(){
5152
assert.isDefined(opts);
5253
assert.equal(opts.setting,'test2');
5354
});
55+
56+
it('enables the sortable', function(){
57+
$ul.sortable('disable');
58+
sortable.__testing._enableSortable($ul);
59+
assert.equal($ul.attr('aria-dropeffect'),'move');
60+
assert.equal($li.attr('draggable'),'true');
61+
});
5462
});
5563

5664
describe('destruction', function(){
@@ -119,85 +127,4 @@ describe('internal function tests', function(){
119127

120128
});
121129

122-
describe('ghost', function(){
123-
// moch dragged item
124-
var dItem = $('<li>dItem Test</li>');
125-
dItem.offset = function(){
126-
return {
127-
left: 5,
128-
top: 5
129-
}
130-
};
131-
// mock event
132-
var e = {
133-
pageX: 100,
134-
pageY: 200,
135-
dataTransfer: {
136-
text: undefined,
137-
item: undefined,
138-
x: undefined,
139-
y: undefined,
140-
setData: function(type, val){
141-
e.dataTransfer[type] = val;
142-
},
143-
setDragImage: function(item, x, y){
144-
e.dataTransfer.item =item;
145-
e.dataTransfer.x = x;
146-
e.dataTransfer.y = y;
147-
}
148-
}
149-
};
150-
151-
it('sets the dataTransfer options correctly', function(){
152-
sortable.__testing._attachGhost(e, {
153-
item: 'test-item',
154-
x: 10,
155-
y: 20
156-
});
157-
158-
assert.equal(e.dataTransfer.effectAllowed, 'move');
159-
assert.equal(e.dataTransfer.text, '');
160-
assert.equal(e.dataTransfer.item, 'test-item');
161-
assert.equal(e.dataTransfer.x, 10);
162-
assert.equal(e.dataTransfer.y, 20);
163-
});
164-
165-
it('sets item correctly from dragged item', function(){
166-
var ghost = sortable.__testing._makeGhost(dItem);
167-
assert.equal(dItem[0].innerHTML, 'dItem Test');
168-
});
169-
170-
it('sets x & y correctly', function(){
171-
var ghost = sortable.__testing._addGhostPos(e, {
172-
item: 'test-item',
173-
draggedItem: dItem
174-
});
175-
176-
assert.equal(ghost.x, 95);
177-
assert.equal(ghost.y, 195);
178-
});
179-
180-
it('uses provided x & y correctly', function(){
181-
var ghost = sortable.__testing._addGhostPos(e, {
182-
item: 'test-item',
183-
draggedItem: dItem,
184-
x: 10,
185-
y: 20
186-
});
187-
188-
assert.equal(ghost.x, 10);
189-
assert.equal(ghost.y, 20);
190-
});
191-
192-
it('attaches ghost completly', function(){
193-
sortable.__testing._getGhost(e, dItem);
194-
195-
assert.equal(e.dataTransfer.effectAllowed, 'move');
196-
assert.equal(e.dataTransfer.text, '');
197-
assert.equal(e.dataTransfer.item, dItem[0]);
198-
assert.equal(e.dataTransfer.x, 95);
199-
assert.equal(e.dataTransfer.y, 195);
200-
});
201-
});
202-
203130
});

0 commit comments

Comments
 (0)