Browse Source

+ 'animation' option and 'create' method

animation
RubaXa 10 years ago
parent
commit
1373a37083
  1. 9
      README.md
  2. 53
      Sortable.js
  3. 4
      Sortable.min.js
  4. 2
      bower.json
  5. 2
      component.json
  6. 2
      package.json

9
README.md

@ -3,12 +3,14 @@
## Features ## Features
* Support touch devices and [modern](http://caniuse.com/#search=drag) browsers * Support touch devices and [modern](http://caniuse.com/#search=drag) browsers
* Animation moving items when sorting (css animation)
* Built using native HTML5 drag and drop API * Built using native HTML5 drag and drop API
* Simple API * Simple API
* Lightweight, 2KB gzipped * Lightweight, 2KB gzipped
* No jQuery * No jQuery
### Usage ### Usage
```html ```html
<ul id="items"> <ul id="items">
@ -20,7 +22,7 @@
```js ```js
var el = document.getElementById('items'); var el = document.getElementById('items');
new Sortable(el); Sortable.create(el);
``` ```
@ -29,9 +31,10 @@ new Sortable(el);
### Options ### Options
```js ```js
new Sortable(el, { var sortabel = new Sortable(el, {
group: "name", group: "name",
store: null, // @see Store store: null, // @see Store
animation: 150, // msec, animation speed moving items when sorting, `0` — without animation
handle: ".my-handle", // Restricts sort start click/touch to the specified element handle: ".my-handle", // Restricts sort start click/touch to the specified element
filter: ".ignor-elements", // Selectors that do not lead to dragging (String or Function) filter: ".ignor-elements", // Selectors that do not lead to dragging (String or Function)
draggable: ".item", // Specifies which items inside the element should be sortable draggable: ".item", // Specifies which items inside the element should be sortable
@ -113,7 +116,7 @@ Saving and restoring of the sort.
``` ```
```js ```js
new Sortable(el, { Sortable.create(el, {
group: "localStorage-example", group: "localStorage-example",
store: { store: {
/** /**

53
Sortable.js

@ -83,7 +83,8 @@
draggable: el.children[0] && el.children[0].nodeName || (/[uo]l/i.test(el.nodeName) ? 'li' : '*'), draggable: el.children[0] && el.children[0].nodeName || (/[uo]l/i.test(el.nodeName) ? 'li' : '*'),
ghostClass: 'sortable-ghost', ghostClass: 'sortable-ghost',
ignore: 'a, img', ignore: 'a, img',
filter: null filter: null,
animation: 150
}; };
// Set default options // Set default options
@ -339,22 +340,21 @@
if( el.children.length === 0 || el.children[0] === ghostEl || (el === evt.target) && _ghostInBottom(el, evt) ){ if( el.children.length === 0 || el.children[0] === ghostEl || (el === evt.target) && _ghostInBottom(el, evt) ){
el.appendChild(dragEl); el.appendChild(dragEl);
} }
else if( target && target !== dragEl && (target.parentNode[expando] !== void 0) ){ else if( target && !target.animated && target !== dragEl && (target.parentNode[expando] !== void 0) ){
if( lastEl !== target ){ if( lastEl !== target ){
lastEl = target; lastEl = target;
lastCSS = _css(target); lastCSS = _css(target);
lastRect = target.getBoundingClientRect();
} }
var var dragRect = dragEl.getBoundingClientRect()
rect = lastRect , targetRect = target.getBoundingClientRect()
, width = rect.right - rect.left , width = targetRect.right - targetRect.left
, height = rect.bottom - rect.top , height = targetRect.bottom - targetRect.top
, floating = /left|right|inline/.test(lastCSS.cssFloat + lastCSS.display) , floating = /left|right|inline/.test(lastCSS.cssFloat + lastCSS.display)
, isWide = (target.offsetWidth > dragEl.offsetWidth) , isWide = (target.offsetWidth > dragEl.offsetWidth)
, isLong = (target.offsetHeight > dragEl.offsetHeight) , isLong = (target.offsetHeight > dragEl.offsetHeight)
, halfway = (floating ? (evt.clientX - rect.left)/width : (evt.clientY - rect.top)/height) > .5 , halfway = (floating ? (evt.clientX - targetRect.left)/width : (evt.clientY - targetRect.top)/height) > .5
, nextSibling = target.nextElementSibling , nextSibling = target.nextElementSibling
, after , after
; ;
@ -370,10 +370,37 @@
if( after && !nextSibling ){ if( after && !nextSibling ){
el.appendChild(dragEl); el.appendChild(dragEl);
this._animate(dragRect, dragEl);
} else { } else {
target.parentNode.insertBefore(dragEl, after ? nextSibling : target); target.parentNode.insertBefore(dragEl, after ? nextSibling : target);
this._animate(dragRect, dragEl);
this._animate(targetRect, target);
}
} }
} }
},
_animate: function (prevRect, target) {
var ms = this.options.animation;
if (ms) {
var currentRect = target.getBoundingClientRect();
_css(target, 'transform', 'translate3d('
+ (prevRect.left - currentRect.left) + 'px,'
+ (prevRect.top - currentRect.top) + 'px,0)'
);
target.offsetWidth; // repaint
target.animated = true;
_css(target, 'transition', 'transform ' + ms + 'ms');
_css(target, 'transform', 'translate3d(0,0,0)');
setTimeout(function () {
_css(target, 'transition', '');
target.animated = false;
}, ms);
} }
}, },
@ -680,8 +707,16 @@
}; };
Sortable.version = '0.5.1'; Sortable.version = '0.6.0';
/**
* Create sortable instance
* @param {HTMLElement} el
* @param {Object} [options]
*/
Sortable.create = function (el, options) {
return new Sortable(el, options)
};
// Export // Export
return Sortable; return Sortable;

4
Sortable.min.js vendored

File diff suppressed because one or more lines are too long

2
bower.json

@ -1,7 +1,7 @@
{ {
"name": "Sortable", "name": "Sortable",
"main": "Sortable.js", "main": "Sortable.js",
"version": "0.5.1", "version": "0.6.0",
"homepage": "http://rubaxa.github.io/Sortable/", "homepage": "http://rubaxa.github.io/Sortable/",
"authors": [ "authors": [
"RubaXa <ibnRubaXa@gmail.com>" "RubaXa <ibnRubaXa@gmail.com>"

2
component.json

@ -1,7 +1,7 @@
{ {
"name": "Sortable", "name": "Sortable",
"main": "Sortable.js", "main": "Sortable.js",
"version": "0.5.1", "version": "0.6.0",
"homepage": "http://rubaxa.github.io/Sortable/", "homepage": "http://rubaxa.github.io/Sortable/",
"repo": "RubaXa/Sortable", "repo": "RubaXa/Sortable",
"authors": [ "authors": [

2
package.json

@ -1,7 +1,7 @@
{ {
"name": "sortable", "name": "sortable",
"exportName": "Sortable", "exportName": "Sortable",
"version": "0.5.1", "version": "0.6.0",
"devDependencies": { "devDependencies": {
"grunt": "*", "grunt": "*",
"grunt-version": "*", "grunt-version": "*",

Loading…
Cancel
Save