Browse Source

+ dev

pull/1191/head
RubaXa 7 years ago
parent
commit
56022568d9
  1. 18
      Gruntfile.js
  2. 18
      README.md
  3. 51
      Sortable.js
  4. 2
      Sortable.min.js
  5. 10
      package.json
  6. 14
      test/e2e/index-page-model.js
  7. 22
      test/e2e/index.js

18
Gruntfile.js

@ -39,7 +39,20 @@ module.exports = function (grunt) {
} }
}, },
jquery: {} jquery: {},
testcafe: {
test: {
options: {
files: ['test/e2e/index.js'],
browsers: ['chrome'],
startApp: {
command: 'npm run http-server'
},
skipJsErrors: true //https://github.com/RubaXa/Sortable/issues/1041
}
}
}
}); });
@ -82,7 +95,8 @@ module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-version'); grunt.loadNpmTasks('grunt-version');
grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-testcafe');
grunt.registerTask('tests', ['jshint']); grunt.registerTask('tests', ['jshint']);
grunt.registerTask('default', ['tests', 'version', 'uglify:dist']); grunt.registerTask('default', ['tests', 'version', 'uglify:dist', 'testcafe']);
}; };

18
README.md

@ -121,31 +121,31 @@ var sortable = new Sortable(el, {
// Element dragging ended // Element dragging ended
onEnd: function (/**Event*/evt) { onEnd: function (/**Event*/evt) {
evt.oldIndex; // element's old index within parent var itemEl = evt.item; // dragged HTMLElement
evt.newIndex; // element's new index within parent evt.to; // target list
evt.from; // previous list
evt.oldIndex; // element's old index within old parent
evt.newIndex; // element's new index within new parent
}, },
// Element is dropped into the list from another list // Element is dropped into the list from another list
onAdd: function (/**Event*/evt) { onAdd: function (/**Event*/evt) {
var itemEl = evt.item; // dragged HTMLElement // same properties as onEnd
evt.from; // previous list
// + indexes from onEnd
}, },
// Changed sorting within list // Changed sorting within list
onUpdate: function (/**Event*/evt) { onUpdate: function (/**Event*/evt) {
var itemEl = evt.item; // dragged HTMLElement // same properties as onEnd
// + indexes from onEnd
}, },
// Called by any change to the list (add / update / remove) // Called by any change to the list (add / update / remove)
onSort: function (/**Event*/evt) { onSort: function (/**Event*/evt) {
// same properties as onUpdate // same properties as onEnd
}, },
// Element is removed from the list into another list // Element is removed from the list into another list
onRemove: function (/**Event*/evt) { onRemove: function (/**Event*/evt) {
// same properties as onUpdate // same properties as onEnd
}, },
// Attempt to drag a filtered element // Attempt to drag a filtered element

51
Sortable.js

@ -73,7 +73,7 @@
supportDraggable = !!('draggable' in document.createElement('div')), supportDraggable = !!('draggable' in document.createElement('div')),
supportCssPointerEvents = (function (el) { supportCssPointerEvents = (function (el) {
// false when IE11 // false when IE11
if (!!navigator.userAgent.match(/Trident.*rv[ :]?11\./)) { if (!!navigator.userAgent.match(/(?:Trident.*rv[ :]?11\.|msie)/i)) {
return false; return false;
} }
el = document.createElement('x'); el = document.createElement('x');
@ -326,6 +326,10 @@
return; // only left button or enabled return; // only left button or enabled
} }
// cancel dnd if original target is content editable
if (originalTarget.isContentEditable) {
return;
}
target = _closest(target, options.draggable, el); target = _closest(target, options.draggable, el);
@ -344,7 +348,7 @@
// Check filter // Check filter
if (typeof filter === 'function') { if (typeof filter === 'function') {
if (filter.call(this, evt, target, this)) { if (filter.call(this, evt, target, this)) {
_dispatchEvent(_this, originalTarget, 'filter', target, el, startIndex); _dispatchEvent(_this, originalTarget, 'filter', target, el, el, startIndex);
preventOnFilter && evt.preventDefault(); preventOnFilter && evt.preventDefault();
return; // cancel dnd return; // cancel dnd
} }
@ -354,7 +358,7 @@
criteria = _closest(originalTarget, criteria.trim(), el); criteria = _closest(originalTarget, criteria.trim(), el);
if (criteria) { if (criteria) {
_dispatchEvent(_this, criteria, 'filter', target, el, startIndex); _dispatchEvent(_this, criteria, 'filter', target, el, el, startIndex);
return true; return true;
} }
}); });
@ -411,7 +415,7 @@
_this._triggerDragStart(evt, touch); _this._triggerDragStart(evt, touch);
// Drag start event // Drag start event
_dispatchEvent(_this, rootEl, 'choose', dragEl, rootEl, oldIndex); _dispatchEvent(_this, rootEl, 'choose', dragEl, rootEl, rootEl, oldIndex);
}; };
// Disable "draggable" // Disable "draggable"
@ -502,7 +506,7 @@
Sortable.active = this; Sortable.active = this;
// Drag start event // Drag start event
_dispatchEvent(this, rootEl, 'start', dragEl, rootEl, oldIndex); _dispatchEvent(this, rootEl, 'start', dragEl, rootEl, rootEl, oldIndex);
} else { } else {
this._nulling(); this._nulling();
} }
@ -915,21 +919,21 @@
_toggleClass(dragEl, this.options.chosenClass, false); _toggleClass(dragEl, this.options.chosenClass, false);
// Drag stop event // Drag stop event
_dispatchEvent(this, rootEl, 'unchoose', dragEl, rootEl, oldIndex); _dispatchEvent(this, rootEl, 'unchoose', dragEl, parentEl, rootEl, oldIndex);
if (rootEl !== parentEl) { if (rootEl !== parentEl) {
newIndex = _index(dragEl, options.draggable); newIndex = _index(dragEl, options.draggable);
if (newIndex >= 0) { if (newIndex >= 0) {
// Add event // Add event
_dispatchEvent(null, parentEl, 'add', dragEl, rootEl, oldIndex, newIndex); _dispatchEvent(null, parentEl, 'add', dragEl, parentEl, rootEl, oldIndex, newIndex);
// Remove event // Remove event
_dispatchEvent(this, rootEl, 'remove', dragEl, rootEl, oldIndex, newIndex); _dispatchEvent(this, rootEl, 'remove', dragEl, parentEl, rootEl, oldIndex, newIndex);
// drag from one list and drop into another // drag from one list and drop into another
_dispatchEvent(null, parentEl, 'sort', dragEl, rootEl, oldIndex, newIndex); _dispatchEvent(null, parentEl, 'sort', dragEl, parentEl, rootEl, oldIndex, newIndex);
_dispatchEvent(this, rootEl, 'sort', dragEl, rootEl, oldIndex, newIndex); _dispatchEvent(this, rootEl, 'sort', dragEl, parentEl, rootEl, oldIndex, newIndex);
} }
} }
else { else {
@ -939,8 +943,8 @@
if (newIndex >= 0) { if (newIndex >= 0) {
// drag & drop within the same list // drag & drop within the same list
_dispatchEvent(this, rootEl, 'update', dragEl, rootEl, oldIndex, newIndex); _dispatchEvent(this, rootEl, 'update', dragEl, parentEl, rootEl, oldIndex, newIndex);
_dispatchEvent(this, rootEl, 'sort', dragEl, rootEl, oldIndex, newIndex); _dispatchEvent(this, rootEl, 'sort', dragEl, parentEl, rootEl, oldIndex, newIndex);
} }
} }
} }
@ -951,7 +955,7 @@
newIndex = oldIndex; newIndex = oldIndex;
} }
_dispatchEvent(this, rootEl, 'end', dragEl, rootEl, oldIndex, newIndex); _dispatchEvent(this, rootEl, 'end', dragEl, parentEl, rootEl, oldIndex, newIndex);
// Save sorting // Save sorting
this.save(); this.save();
@ -1256,7 +1260,7 @@
function _dispatchEvent(sortable, rootEl, name, targetEl, fromEl, startIndex, newIndex) { function _dispatchEvent(sortable, rootEl, name, targetEl, toEl, fromEl, startIndex, newIndex) {
sortable = (sortable || rootEl[expando]); sortable = (sortable || rootEl[expando]);
var evt = document.createEvent('Event'), var evt = document.createEvent('Event'),
@ -1265,7 +1269,7 @@
evt.initEvent(name, true, true); evt.initEvent(name, true, true);
evt.to = rootEl; evt.to = toEl || rootEl;
evt.from = fromEl || rootEl; evt.from = fromEl || rootEl;
evt.item = targetEl || rootEl; evt.item = targetEl || rootEl;
evt.clone = cloneEl; evt.clone = cloneEl;
@ -1421,12 +1425,15 @@
} }
function _clone(el) { function _clone(el) {
return $ if (Polymer && Polymer.dom) {
? $(el).clone(true)[0] return Polymer.dom(el).cloneNode(true);
: (Polymer && Polymer.dom }
? Polymer.dom(el).cloneNode(true) else if ($) {
: el.cloneNode(true) return $(el).clone(true)[0];
); }
else {
return el.cloneNode(true);
}
} }
function _saveInputCheckedState(root) { function _saveInputCheckedState(root) {
@ -1439,7 +1446,7 @@
} }
} }
// Fixed #973: // Fixed #973:
_on(document, 'touchmove', function (evt) { _on(document, 'touchmove', function (evt) {
if (Sortable.active) { if (Sortable.active) {
evt.preventDefault(); evt.preventDefault();

2
Sortable.min.js vendored

File diff suppressed because one or more lines are too long

10
package.json

@ -4,15 +4,19 @@
"version": "1.6.1", "version": "1.6.1",
"devDependencies": { "devDependencies": {
"grunt": "*", "grunt": "*",
"grunt-version": "*",
"grunt-contrib-jshint": "*", "grunt-contrib-jshint": "*",
"grunt-contrib-uglify": "*" "grunt-contrib-uglify": "*",
"grunt-testcafe": "^0.15.0",
"grunt-version": "*",
"http-server": "^0.9.0",
"testcafe": "^0.16.0"
}, },
"description": "Minimalist JavaScript library for reorderable drag-and-drop lists on modern browsers and touch devices. No jQuery. Supports AngularJS and any CSS library, e.g. Bootstrap.", "description": "Minimalist JavaScript library for reorderable drag-and-drop lists on modern browsers and touch devices. No jQuery. Supports AngularJS and any CSS library, e.g. Bootstrap.",
"main": "Sortable.js", "main": "Sortable.js",
"scripts": { "scripts": {
"test": "./node_modules/grunt/bin/grunt", "test": "./node_modules/grunt/bin/grunt",
"prepublish": "./node_modules/grunt/bin/grunt" "prepublish": "./node_modules/grunt/bin/grunt",
"http-server": "http-server -s ./"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

14
test/e2e/index-page-model.js

@ -0,0 +1,14 @@
import { Selector } from 'testcafe';
export default class IndexPage {
constructor () {
const listAContainer = Selector('#foo');
const listAItems = listAContainer.find('li');
this.listA = {
container: listAContainer,
items: listAItems,
getItem: text => listAItems.withText(text)
};
}
}

22
test/e2e/index.js

@ -0,0 +1,22 @@
import IndexPage from './index-page-model';
const indexPage = new IndexPage();
fixture `Tests`
.page('http://localhost:8080/index.html');
test('List A', async t => {
const listA = indexPage.listA;
const firstItem = listA.items.nth(0);
const secondItem = listA.items.nth(1);
const hippoText = 'Бегемот';
const foodText = 'Корм';
await t
.expect(firstItem.innerText).eql(hippoText)
.expect(secondItem.innerText).eql(foodText)
.dragToElement(firstItem, secondItem, { speed: 0.5 })
.expect(firstItem.innerText).eql(foodText)
.expect(secondItem.innerText).eql(hippoText);
});
Loading…
Cancel
Save