README.md 9.9 KB
Newer Older
R
RubaXa 已提交
1
# Sortable
D
Dan Dascalescu 已提交
2
Sortable is a minimalist JavaScript library for reorderable drag-and-drop lists.
R
RubaXa 已提交
3

D
Dan Dascalescu 已提交
4 5
Demo: http://rubaxa.github.io/Sortable/

R
RubaXa 已提交
6

R
RubaXa 已提交
7
## Features
R
RubaXa 已提交
8

R
RubaXa 已提交
9 10
 * Supports touch devices and [modern](http://caniuse.com/#search=drag) browsers
 * Can drag from one list to another or within the same list
11
 * CSS animation when moving items
R
RubaXa 已提交
12
 * Supports drag handles *and selectable text* (better than voidberg's html5sortable)
R
RubaXa 已提交
13
 * Smart auto-scrolling
14
 * Built using native HTML5 drag and drop API
R
RubaXa 已提交
15
 * Supports [AngularJS](#ng) and and any CSS library, e.g. [Bootstrap](#bs)
16 17
 * Simple API
 * No jQuery
R
RubaXa 已提交
18 19 20 21 22 23 24 25 26 27 28 29


### Usage
```html
<ul id="items">
	<li>item 1</li>
	<li>item 2</li>
	<li>item 3</li>
</ul>
```

```js
R
* upd  
RubaXa 已提交
30
var el = document.getElementById('items');
R
RubaXa 已提交
31
var sortable = Sortable.create(el);
R
RubaXa 已提交
32 33
```

34 35
You can use any element for the list and its elements, not just `ul`/`li`. Here is an [example with `div`s](http://jsbin.com/luxero/2/edit?html,js,output).

R
RubaXa 已提交
36

R
* upd  
RubaXa 已提交
37 38 39
---


R
RubaXa 已提交
40 41
### Options
```js
D
Dano Alexander 已提交
42
var sortable = new Sortable(el, {
43
	group: "name",  // or { name: "...", pull: [true, false, clone], put: [true, false, array] }
44
	sort: true,  // sorting inside list
R
RubaXa 已提交
45
	disabled: false, // Disables the sortable if set to true.
46 47 48
	store: null,  // @see Store
	animation: 150,  // ms, animation speed moving items when sorting, `0` — without animation
	handle: ".my-handle",  // Drag handle selector within list items
D
Dan Dascalescu 已提交
49
	filter: ".ignore-elements",  // Selectors that do not lead to dragging (String or Function)
50
	draggable: ".item",  // Specifies which items inside the element should be sortable
D
Dan Dascalescu 已提交
51
	ghostClass: "sortable-ghost",  // Class name for the drop placeholder - jsbin.com/luxero/3
R
RubaXa 已提交
52
	
R
RubaXa 已提交
53 54 55
	scroll: true, // or HTMLElement
	scrollSensitivity: 30, // px, how near the mouse must be to an edge to start scrolling.
	scrollSpeed: 10, // px
R
RubaXa 已提交
56
	
R
RubaXa 已提交
57 58 59
	setData: function (dataTransfer, dragEl) {
		dataTransfer.setData('Text', dragEl.textContent);
	},
60

61 62 63 64 65 66 67 68 69 70
	// dragging started
	onStart: function (/**Event*/evt) {
		evt.oldIndex;  // element index within parent
	},
	
	// dragging ended
	onEnd: function (/**Event*/evt) {
		evt.oldIndex;  // element's old index within parent
		evt.newIndex;  // element's new index within parent
	},
R
RubaXa 已提交
71

72
	// Element is dropped into the list from another list
73 74 75 76
	onAdd: function (/**Event*/evt) {
		var itemEl = evt.item;  // dragged HTMLElement
		itemEl.from;  // previous list
		// + indexes from onEnd
R
RubaXa 已提交
77 78
	},

79
	// Changed sorting within list
80 81 82
	onUpdate: function (/**Event*/evt) {
		var itemEl = evt.item;  // dragged HTMLElement
		// + indexes from onEnd
R
RubaXa 已提交
83 84
	},

85
	// Called by any change to the list (add / update / remove)
86 87
	onSort: function (/**Event*/evt) {
		// same properties as onUpdate
88 89
	},

90
	// Element is removed from the list into another list
91 92
	onRemove: function (/**Event*/evt) {
		// same properties as onUpdate
93 94
	},

95
	// Attempt to drag a filtered element
96 97
	onFilter: function (/**Event*/evt) {
		var itemEl = evt.item;  // HTMLElement receiving the `mousedown|tapstart` event.
R
RubaXa 已提交
98 99 100
	}
});
```
R
RubaXa 已提交
101

R
RubaXa 已提交
102

R
* upd  
RubaXa 已提交
103 104
---

R
RubaXa 已提交
105

106 107 108
#### `group` option
To drag elements from one list into another, both lists must have the same `group` value.
You can also define whether lists can give away, give and keep a copy (`clone`), and receive elements.
R
RubaXa 已提交
109

110 111
 * name: `String` — group name
 * pull: `true|false|'clone'` — ability to move from the list. `clone` — copy the item, rather than move.
L
Lebedev Konstantin 已提交
112
 * put: `true|false|["foo", "bar"]` — whether elements can be added from other lists, or an array of group names from which elements can be taken. Demo: http://jsbin.com/naduvo/2/edit?html,js,output
R
RubaXa 已提交
113 114 115 116


---

117

R
RubaXa 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
#### `sort` option
Sorting inside list

Demo: http://jsbin.com/xizeh/2/edit?html,js,output


---


#### `disabled` options
Disables the sortable if set to `true`.

Demo: http://jsbin.com/xiloqu/1/edit?html,js,output

```js
var sortable = Sortable.create(list);

document.getElementById("switcher").onclick = function () {
	var state = sortable.option("disabled"); // get

	sortable.option("disabled", !state); // set
};
```


---


R
RubaXa 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
#### `handle` option
To make list items draggable, Sortable disables text selection by the user.
That's not always desirable. To allow text selection, define a drag handler,
which is an area of every list element that allows it to be dragged around.

Demo: http://jsbin.com/newize/1/edit?html,js,output

```js
Sortable.create(el, {
	handle: ".my-handle"
});
```

```html
<ul>
	<li><span class="my-handle">::</span> list item text one
	<li><span class="my-handle">::</span> list item text two
</ul>
```

```css
.my-handle {
	cursor: move;
	cursor: -webkit-grabbing;
}
```


---


R
RubaXa 已提交
177 178 179 180 181 182 183
#### `filter` option


```js
Sortable.create(list, {
	filter: ".js-remove, .js-edit",
	onFilter: function (evt) {
R
RubaXa 已提交
184 185
		var item = el.item,
			ctrl = evt.target;
R
RubaXa 已提交
186

R
RubaXa 已提交
187 188
		if (Sortable.utils.is(ctrl, ".js-remove")) {  // Click on remove button
			item.parentNode.removeChild(item); // remove sortable item
R
RubaXa 已提交
189
		}
R
RubaXa 已提交
190
		else if (Sortable.utils.is(ctrl, ".js-edit")) {  // Click on edit link
R
RubaXa 已提交
191 192 193 194 195 196 197 198 199 200
			// ...
		}
	}
})
```


---


201 202 203 204
<a name="ng"></a>
### Support AngularJS
Include [ng-sortable.js](ng-sortable.js)

R
RubaXa 已提交
205 206
Demo: http://jsbin.com/naduvo/1/edit?html,js,output

207
```html
R
RubaXa 已提交
208
<div ng-app="myApp" ng-controller="demo">
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
	<ul ng-sortable>
		<li ng-repeat="item in items">{{item}}</li>
	</ul>

	<ul ng-sortable="{ group: 'foobar' }">
		<li ng-repeat="item in foo">{{item}}</li>
	</ul>

	<ul ng-sortable="barConfig">
		<li ng-repeat="item in bar">{{item}}</li>
	</ul>
</div>
```


```js
angular.module('myApp', ['ng-sortable'])
R
RubaXa 已提交
226 227 228 229 230 231
	.controller('demo', ['$scope', function ($scope) {
		$scope.items = ['item 1', 'item 2'];
		$scope.foo = ['foo 1', '..'];
		$scope.bar = ['bar 1', '..'];
		$scope.barConfig = { group: 'foobar', animation: 150 };
	}]);
232 233
```

R
RubaXa 已提交
234

R
* upd  
RubaXa 已提交
235 236
---

R
RubaXa 已提交
237

238 239
### Method

240 241 242 243 244

##### option(name:`String`[, value:`*`]):`*`
Get or set the option.


245

246 247 248 249
##### closest(el:`String`[, selector:`HTMLElement`]):`HTMLElement|null`
For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.


250
##### toArray():`String[]`
251
Serializes the sortable's item `data-id`'s into an array of string.
252 253


R
* jsdoc  
RubaXa 已提交
254
##### sort(order:`String[]`)
255
Sorts the elements according to the array.
256

257
```js
R
* upd  
RubaXa 已提交
258 259
var order = sortable.toArray();
sortable.sort(order.reverse()); // apply
260 261 262 263
```


##### destroy()
264
Removes the sortable functionality completely.
265 266 267 268 269 270 271 272


---


### Store
Saving and restoring of the sort.

273 274 275 276 277 278 279 280
```html
<ul>
	<li data-id="1">order</li>
	<li data-id="2">save</li>
	<li data-id="3">restore</li>
</ul>
```

281
```js
282
Sortable.create(el, {
283 284
	group: "localStorage-example",
	store: {
R
* JSDoc  
RubaXa 已提交
285 286 287
		/**
		 * Get the order of elements. Called once during initialization.
		 * @param   {Sortable}  sortable
D
Dan Dascalescu 已提交
288
		 * @returns {Array}
R
* JSDoc  
RubaXa 已提交
289
		 */
290 291 292 293 294
		get: function (sortable) {
			var order = localStorage.getItem(sortable.options.group);
			return order ? order.split('|') : [];
		},

R
* JSDoc  
RubaXa 已提交
295
		/**
D
Dan Dascalescu 已提交
296
		 * Save the order of elements. Called onEnd (when the item is dropped).
R
* JSDoc  
RubaXa 已提交
297 298
		 * @param {Sortable}  sortable
		 */
299 300 301 302 303 304 305 306 307
		set: function (sortable) {
			var order = sortable.toArray();
			localStorage.setItem(sortable.options.group, order.join('|'));
		}
	}
})
```


R
RubaXa 已提交
308 309 310 311 312
---


<a name="bs"></a>
### Bootstrap
313
Demo: http://jsbin.com/luxero/2/edit?html,js,output
R
RubaXa 已提交
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336

```html
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"/>


<!-- Latest Sortable -->
<script src="http://rubaxa.github.io/Sortable/Sortable.js"></script>


<!-- Simple List -->
<ul id="simpleList" class="list-group">
	<li class="list-group-item">This is <a href="http://rubaxa.github.io/Sortable/">Sortable</a></li>
	<li class="list-group-item">It works with Bootstrap...</li>
	<li class="list-group-item">...out of the box.</li>
	<li class="list-group-item">It has support for touch devices.</li>
	<li class="list-group-item">Just drag some elements around.</li>
</ul>

<script>
    // Simple list
    Sortable.create(simpleList, { /* options */ });
</script>
D
Dano Alexander 已提交
337
```
338

R
RubaXa 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
---



### Sortable.utils
* on(el`:HTMLElement`, event`:String`, fn`:Function`) — attach an event handler function
* off(el`:HTMLElement`, event`:String`, fn`:Function`) — remove an event handler
* css(el`:HTMLElement`)`:Object` — get the values of all the CSS properties
* css(el`:HTMLElement`, prop`:String`)`:Mixed` — get the value of style properties
* css(el`:HTMLElement`, prop`:String`, value`:String`) — set one CSS properties
* css(el`:HTMLElement`, props`:Object`) — set more CSS properties
* find(ctx`:HTMLElement`, tagName`:String`[, iterator`:Function`])`:Array` — get elements by tag name
* bind(ctx`:Mixed`, fn`:Function`)`:Function` — Takes a function and returns a new one that will always have a particular context
* closest(el`:HTMLElement`, selector`:String`[, ctx`:HTMLElement`])`:HTMLElement|Null` — for each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree
* toggleClass(el`:HTMLElement`, name`:String`, state`:Boolean`) — add or remove one classes from each element
B
Bitdeli Chef 已提交
354

L
Lebedev Konstantin 已提交
355

R
RubaXa 已提交
356

L
Lebedev Konstantin 已提交
357 358 359 360 361
---



## MIT LICENSE
362
Copyright 2013-2014 Lebedev Konstantin <ibnRubaXa@gmail.com>
L
Lebedev Konstantin 已提交
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
http://rubaxa.github.io/Sortable/

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.