README.md 10.6 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
51
	ghostClass: "sortable-ghost",  // Class name for the drop placeholder
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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
#### `ghostClass` option
Class name for the drop placeholder.

Demo: http://jsbin.com/boqugumiqi/1/edit?css,js,output

```css
.ghost {
  opacity: 0.4;
}
```

```js
Sortable.create(list, {
  ghostClass: "ghost"
});
```


---


#### `scroll` option
If set to `true`, the page (or sortable-area) scrolls when coming to an edge.

Demo:
 - `window`: http://jsbin.com/boqugumiqi/1/edit?html,js,output 
 - `overflow: hidden`: http://jsbin.com/kohamakiwi/1/edit?html,js,output


---


#### `scrollSensitivity` option
Defines how near the mouse must be to an edge to start scrolling.


---


#### `scrollSpeed` option
The speed at which the window should scroll once the mouse pointer gets within the `scrollSensitivity` distance.


---


247 248 249 250
<a name="ng"></a>
### Support AngularJS
Include [ng-sortable.js](ng-sortable.js)

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

253
```html
R
RubaXa 已提交
254
<div ng-app="myApp" ng-controller="demo">
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
	<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 已提交
272 273 274 275 276 277
	.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 };
	}]);
278 279
```

R
RubaXa 已提交
280

R
* upd  
RubaXa 已提交
281 282
---

R
RubaXa 已提交
283

284 285
### Method

286 287 288 289 290

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


291

292 293 294 295
##### 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.


296
##### toArray():`String[]`
297
Serializes the sortable's item `data-id`'s into an array of string.
298 299


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

303
```js
R
* upd  
RubaXa 已提交
304 305
var order = sortable.toArray();
sortable.sort(order.reverse()); // apply
306 307 308 309
```


##### destroy()
310
Removes the sortable functionality completely.
311 312 313 314 315 316 317 318


---


### Store
Saving and restoring of the sort.

319 320 321 322 323 324 325 326
```html
<ul>
	<li data-id="1">order</li>
	<li data-id="2">save</li>
	<li data-id="3">restore</li>
</ul>
```

327
```js
328
Sortable.create(el, {
329 330
	group: "localStorage-example",
	store: {
R
* JSDoc  
RubaXa 已提交
331 332 333
		/**
		 * Get the order of elements. Called once during initialization.
		 * @param   {Sortable}  sortable
D
Dan Dascalescu 已提交
334
		 * @returns {Array}
R
* JSDoc  
RubaXa 已提交
335
		 */
336 337 338 339 340
		get: function (sortable) {
			var order = localStorage.getItem(sortable.options.group);
			return order ? order.split('|') : [];
		},

R
* JSDoc  
RubaXa 已提交
341
		/**
D
Dan Dascalescu 已提交
342
		 * Save the order of elements. Called onEnd (when the item is dropped).
R
* JSDoc  
RubaXa 已提交
343 344
		 * @param {Sortable}  sortable
		 */
345 346 347 348 349 350 351 352 353
		set: function (sortable) {
			var order = sortable.toArray();
			localStorage.setItem(sortable.options.group, order.join('|'));
		}
	}
})
```


R
RubaXa 已提交
354 355 356 357 358
---


<a name="bs"></a>
### Bootstrap
359
Demo: http://jsbin.com/luxero/2/edit?html,js,output
R
RubaXa 已提交
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382

```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 已提交
383
```
384

R
RubaXa 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
---



### 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 已提交
400

L
Lebedev Konstantin 已提交
401

R
RubaXa 已提交
402

L
Lebedev Konstantin 已提交
403 404 405 406 407
---



## MIT LICENSE
408
Copyright 2013-2014 Lebedev Konstantin <ibnRubaXa@gmail.com>
L
Lebedev Konstantin 已提交
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
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.