Skip to content

Commit 4de2c5b

Browse files
committed
Adding ability to connect lists together.
1 parent a9b8468 commit 4de2c5b

File tree

3 files changed

+71
-16
lines changed

3 files changed

+71
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
HTML5 Sortable jQuery Plugin
22
============================
33

4-
**[See it in action](http://farhadi.github.com/html5sortable)**
4+
**[Demos & Documentation](http://farhadi.ir/projects/html5sortable)**
55

66
Features
77
--------

index.html

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
<meta charset="utf-8">
44
<title>HTML5 Sortable jQuery Plugin</title>
55
<style>
6+
header, section {
7+
display: block;
8+
}
69
body {
710
font-family: 'Droid Serif';
811
}
@@ -15,7 +18,7 @@
1518
width: 460px;
1619
font-size: 0.9em;
1720
}
18-
.sortable {
21+
.connected, .sortable {
1922
margin: auto;
2023
padding: 0;
2124
}
@@ -26,7 +29,7 @@
2629
width: 410px;
2730
overflow: hidden;
2831
}
29-
.sortable li {
32+
.connected li, .sortable li {
3033
list-style: none;
3134
border: 1px solid #CCC;
3235
background: #F6F6F6;
@@ -43,6 +46,21 @@
4346
height: 80px;
4447
text-align: center;
4548
}
49+
li.highlight {
50+
background: #FEE25F;
51+
}
52+
#connected {
53+
width: 440px;
54+
overflow: hidden;
55+
margin: auto;
56+
}
57+
.connected {
58+
float: left;
59+
width: 200px;
60+
}
61+
.connected.no2 {
62+
float: right;
63+
}
4664
li.sortable-placeholder {
4765
border: 1px dashed #CCC;
4866
background: none;
@@ -55,17 +73,17 @@
5573
<header>
5674
<h1>HTML5 Sortable jQuery Plugin</h1>
5775
</header>
58-
<article>
76+
<section>
5977
<h2>Features</h2>
6078
<ul id="features">
6179
<li>Less than 1KB (minified).
6280
<li>Built using native HTML5 drag and drop API.</li>
6381
<li>Supports both list and grid style layouts.</li>
6482
<li>Similar API and behaviour to jquery-ui sortable plugin.</li>
6583
<li>Works in IE 6+, Firefox 3.5+, Chrome 3+, Safari 3+ and, Opera 12+.</li>
66-
<ul>
67-
</article>
68-
<article>
84+
</ul>
85+
</section>
86+
<section>
6987
<h2>Sortable List</h2>
7088
<ul class="sortable list">
7189
<li>Item 1</li>
@@ -77,8 +95,8 @@ <h2>Sortable List</h2>
7795
<li>Item 7</li>
7896
<li>Item 8</li>
7997
</ul>
80-
</article>
81-
<article>
98+
</section>
99+
<section>
82100
<h2>Sortable Grid</h2>
83101
<ul class="sortable grid">
84102
<li>Item 1</li>
@@ -90,13 +108,39 @@ <h2>Sortable Grid</h2>
90108
<li>Item 7</li>
91109
<li>Item 8</li>
92110
</ul>
93-
</article>
111+
</section>
112+
<section id="connected">
113+
<h2>Connected Sortable Lists</h2>
114+
<ul class="connected list">
115+
<li>Item 1</li>
116+
<li>Item 2</li>
117+
<li>Item 3</li>
118+
<li>Item 4</li>
119+
<li>Item 5</li>
120+
<li>Item 6</li>
121+
<li>Item 7</li>
122+
<li>Item 8</li>
123+
</ul>
124+
<ul class="connected list no2">
125+
<li class="highlight">Item 1</li>
126+
<li class="highlight">Item 2</li>
127+
<li class="highlight">Item 3</li>
128+
<li class="highlight">Item 4</li>
129+
<li class="highlight">Item 5</li>
130+
<li class="highlight">Item 6</li>
131+
<li class="highlight">Item 7</li>
132+
<li class="highlight">Item 8</li>
133+
</ul>
134+
</section>
94135
<a href="http://github.com/farhadi/html5sortable"><img style="position: absolute; top: 0; left: 0; border: 0;" src="https://a248.e.akamai.net/assets.github.com/img/edc6dae7a1079163caf7f17c60495bbb6d027c93/687474703a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f6c6566745f677265656e5f3030373230302e706e67" alt="Fork me on GitHub"></a>
95136
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
96137
<script src="jquery.sortable.js"></script>
97138
<script>
98139
$(function() {
99140
$('.sortable').sortable();
141+
$('.connected').sortable({
142+
connectWith: '.connected'
143+
});
100144
});
101145
</script>
102146
</body>

jquery.sortable.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55
* Copyright 2012, Ali Farhadi
66
* Released under the MIT license.
77
*/
8-
jQuery.fn.sortable = function() {
8+
(function($) {
9+
var dragging, placeholders = $();
10+
$.fn.sortable = function(options) {
911
return this.each(function() {
10-
var $ = jQuery, index, dragging, items = $(this).children();
12+
var index, items = $(this).children(), connectWith = false;
1113
var placeholder = $('<' + items[0].tagName + '>').addClass('sortable-placeholder');
12-
14+
placeholders = placeholders.add(placeholder);
15+
if (options && options.connectWith) {
16+
$(connectWith = options.connectWith).add(this).data('connectWith', connectWith);
17+
}
1318
items.attr('draggable', 'true').bind('dragstart', function(e) {
1419
var dt = e.originalEvent.dataTransfer;
1520
dt.effectAllowed = 'move';
@@ -18,7 +23,7 @@ jQuery.fn.sortable = function() {
1823
index = dragging.index();
1924
}).bind('dragend', function() {
2025
dragging.removeClass('sortable-dragging').fadeIn();
21-
placeholder.detach();
26+
placeholders.detach();
2227
if (index != dragging.index()) {
2328
items.parent().trigger('sortupdate');
2429
}
@@ -27,19 +32,25 @@ jQuery.fn.sortable = function() {
2732
this.dragDrop && this.dragDrop();
2833
return false;
2934
}).end().add([this, placeholder]).bind('dragover dragenter', function(e) {
30-
if (!dragging) return true;
35+
if (!items.is(dragging) && connectWith !== $(dragging).parent().data('connectWith')) {
36+
return true;
37+
}
3138
e.preventDefault();
3239
e.originalEvent.dataTransfer.dropEffect = 'move';
3340
if (items.is(this)) {
3441
dragging.hide();
3542
$(this)[placeholder.index() < $(this).index() ? 'after' : 'before'](placeholder);
3643
}
44+
placeholders.not(placeholder).detach();
3745
return false;
3846
}).bind('drop', function(e) {
39-
if (!dragging) return true;
47+
if (!items.is(dragging) && connectWith !== $(dragging).parent().data('connectWith')) {
48+
return true;
49+
}
4050
e.stopPropagation();
4151
placeholder.after(dragging);
4252
return false;
4353
});
4454
});
4555
};
56+
})(jQuery);

0 commit comments

Comments
 (0)