Skip to content

Commit f077494

Browse files
committed
Adding ability to specifiy a drag handle for items. Fixes #6.
1 parent 00d38fd commit f077494

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ $('.sortable').sortable({
3535
items: ':not(.disabled)'
3636
});
3737
```
38+
Use `handle` option to restrict drag start to the specified element:
39+
40+
``` javascript
41+
$('.sortable').sortable({
42+
handle: 'h2'
43+
});
44+
```
3845

3946
Use `connectWith` option to create connected lists:
4047

index.html

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
width: 460px;
1919
font-size: 0.9em;
2020
}
21-
.connected, .sortable, .exclude {
21+
.connected, .sortable, .exclude, .handles {
2222
margin: auto;
2323
padding: 0;
2424
width: 310px;
@@ -32,7 +32,7 @@
3232
.sortable.grid {
3333
overflow: hidden;
3434
}
35-
.connected li, .sortable li, .exclude li {
35+
.connected li, .sortable li, .exclude li, .handles li {
3636
list-style: none;
3737
border: 1px solid #CCC;
3838
background: #F6F6F6;
@@ -42,6 +42,9 @@
4242
padding: 5px;
4343
height: 22px;
4444
}
45+
.handles span {
46+
cursor: move;
47+
}
4548
li.disabled {
4649
opacity: 0.5;
4750
}
@@ -122,6 +125,17 @@ <h2>Exclude Items</h2>
122125
<li class="disabled">Item 6</li>
123126
</ul>
124127
</section>
128+
<section>
129+
<h2>Sortable List With Handles</h2>
130+
<ul class="handles list">
131+
<li><span>::</span> Item 1</li>
132+
<li><span>::</span> Item 2</li>
133+
<li><span>::</span> Item 3</li>
134+
<li><span>::</span> Item 4</li>
135+
<li><span>::</span> Item 5</li>
136+
<li><span>::</span> Item 6</li>
137+
</ul>
138+
</section>
125139
<section id="connected">
126140
<h2>Connected Sortable Lists</h2>
127141
<ul class="connected list">
@@ -147,6 +161,9 @@ <h2>Connected Sortable Lists</h2>
147161
<script>
148162
$(function() {
149163
$('.sortable').sortable();
164+
$('.handles').sortable({
165+
handle: 'span'
166+
});
150167
$('.connected').sortable({
151168
connectWith: '.connected'
152169
});

jquery.sortable.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,22 @@ $.fn.sortable = function(options) {
1919
}
2020
var index, items = $(this).children(options.items), connectWith = options.connectWith || false;
2121
var placeholder = $('<' + items[0].tagName + ' class="sortable-placeholder">');
22+
var handle = options.handle, isHandle;
23+
items.find(handle).mousedown(function() {
24+
isHandle = true;
25+
}).mouseup(function() {
26+
isHandle = false;
27+
});
2228
$(this).data('items', options.items)
2329
placeholders = placeholders.add(placeholder);
24-
connectWith && $(connectWith).add(this).data('connectWith', connectWith);
30+
if (connectWith) {
31+
$(connectWith).add(this).data('connectWith', connectWith);
32+
}
2533
items.attr('draggable', 'true').bind('dragstart.h5s', function(e) {
34+
if (handle && !isHandle) {
35+
return false;
36+
}
37+
isHandle = false;
2638
var dt = e.originalEvent.dataTransfer;
2739
dt.effectAllowed = 'move';
2840
dt.setData('Text', 'dummy');

0 commit comments

Comments
 (0)