README.md 11.1 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
15 16
 * Supports [Meteor](meteor/README.md) and [AngularJS](#ng)
 * Supports any CSS library, e.g. [Bootstrap](#bs)
17 18
 * Simple API
 * No jQuery
R
RubaXa 已提交
19 20 21 22 23 24 25 26 27 28 29 30


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

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

35 36
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 已提交
37

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


R
RubaXa 已提交
41 42
### Options
```js
D
Dano Alexander 已提交
43
var sortable = new Sortable(el, {
44
	group: "name",  // or { name: "...", pull: [true, false, clone], put: [true, false, array] }
45
	sort: true,  // sorting inside list
R
RubaXa 已提交
46
	disabled: false, // Disables the sortable if set to true.
47 48 49
	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 已提交
50
	filter: ".ignore-elements",  // Selectors that do not lead to dragging (String or Function)
51
	draggable: ".item",  // Specifies which items inside the element should be sortable
52
	ghostClass: "sortable-ghost",  // Class name for the drop placeholder
R
RubaXa 已提交
53
	
R
RubaXa 已提交
54 55 56
	scroll: true, // or HTMLElement
	scrollSensitivity: 30, // px, how near the mouse must be to an edge to start scrolling.
	scrollSpeed: 10, // px
R
RubaXa 已提交
57
	
R
RubaXa 已提交
58 59 60
	setData: function (dataTransfer, dragEl) {
		dataTransfer.setData('Text', dragEl.textContent);
	},
61

62 63 64 65 66 67 68 69 70 71
	// 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 已提交
72

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

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

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

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

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

R
RubaXa 已提交
103

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

R
RubaXa 已提交
106

107 108 109
#### `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 已提交
110

111 112
 * name: `String` — group name
 * pull: `true|false|'clone'` — ability to move from the list. `clone` — copy the item, rather than move.
L
Lebedev Konstantin 已提交
113
 * 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 已提交
114 115 116 117


---

118

R
RubaXa 已提交
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 146
#### `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 已提交
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 177
#### `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 已提交
178 179 180 181 182 183 184
#### `filter` option


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

R
RubaXa 已提交
188 189
		if (Sortable.utils.is(ctrl, ".js-remove")) {  // Click on remove button
			item.parentNode.removeChild(item); // remove sortable item
R
RubaXa 已提交
190
		}
R
RubaXa 已提交
191
		else if (Sortable.utils.is(ctrl, ".js-edit")) {  // Click on edit link
R
RubaXa 已提交
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 247
#### `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.


---


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

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

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

R
RubaXa 已提交
281

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

R
RubaXa 已提交
284

285 286
### Method

287 288 289 290 291

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


292

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


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


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

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


R
RubaXa 已提交
310 311 312 313
##### save()
Save the current sorting (see [store](#store))


314
##### destroy()
315
Removes the sortable functionality completely.
316 317 318 319 320


---


R
RubaXa 已提交
321
<a name="store"></a>
322 323 324
### Store
Saving and restoring of the sort.

325 326 327 328 329 330 331 332
```html
<ul>
	<li data-id="1">order</li>
	<li data-id="2">save</li>
	<li data-id="3">restore</li>
</ul>
```

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

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


R
RubaXa 已提交
360 361 362 363 364
---


<a name="bs"></a>
### Bootstrap
365
Demo: http://jsbin.com/luxero/2/edit?html,js,output
R
RubaXa 已提交
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388

```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 已提交
389
```
390

R
RubaXa 已提交
391

R
RubaXa 已提交
392 393 394
---


R
RubaXa 已提交
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
### Static methods & properties



##### Sortable.create(el:`HTMLElement`[, options:`Object`]):`Sortable`
Create new instance.


---


##### Sortable.active:`Sortable`
Link to the active instance.


---

R
RubaXa 已提交
412

R
RubaXa 已提交
413
##### Sortable.utils
R
RubaXa 已提交
414 415 416 417 418 419 420 421
* 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
R
RubaXa 已提交
422
* is(el`:HTMLElement`, selector`:String`)`:Boolean` — check the current matched set of elements against a selector
R
RubaXa 已提交
423 424
* 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 已提交
425

L
Lebedev Konstantin 已提交
426

R
RubaXa 已提交
427

L
Lebedev Konstantin 已提交
428 429 430 431 432
---



## MIT LICENSE
W
Willson Mock 已提交
433
Copyright 2013-2015 Lebedev Konstantin <ibnRubaXa@gmail.com>
L
Lebedev Konstantin 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
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.