diff --git a/dist/amd/dragula.js b/dist/amd/dragula.js index 4ce811b..efc0a78 100644 --- a/dist/amd/dragula.js +++ b/dist/amd/dragula.js @@ -1,4 +1,4 @@ -define(['exports', 'babel-runtime/helpers/create-class', 'babel-runtime/helpers/class-call-check', 'aurelia-dependency-injection', './touchy', './options', './util', 'contra', 'crossvent', './classes'], function (exports, _babelRuntimeHelpersCreateClass, _babelRuntimeHelpersClassCallCheck, _aureliaDependencyInjection, _touchy, _options, _util, _contra, _crossvent, _classes) { +define(['exports', 'babel-runtime/helpers/create-class', 'babel-runtime/helpers/class-call-check', 'aurelia-dependency-injection', './touchy', './options', './util', './emitter', './classes'], function (exports, _babelRuntimeHelpersCreateClass, _babelRuntimeHelpersClassCallCheck, _aureliaDependencyInjection, _touchy, _options, _util, _emitter, _classes) { 'use strict'; Object.defineProperty(exports, '__esModule', { @@ -11,7 +11,8 @@ define(['exports', 'babel-runtime/helpers/create-class', 'babel-runtime/helpers/ var len = arguments.length; this.options = options || new _options.Options(); - this.drake = (0, _contra.emitter)({ + this.emitter = new _emitter.Emitter(); + this.drake = { containers: this.options.containers, start: this.manualStart.bind(this), end: this.end.bind(this), @@ -19,10 +20,11 @@ define(['exports', 'babel-runtime/helpers/create-class', 'babel-runtime/helpers/ remove: this.remove.bind(this), destroy: this.destroy.bind(this), dragging: false - }); + }; if (this.options.removeOnSpill === true) { - this.drake.on('over', spillOver).on('out', spillOut); + this.emitter.on('over', this.spillOver.bind(this)); + this.emitter.on('out', this.spillOut.bind(this)); } this._events(); @@ -45,7 +47,7 @@ define(['exports', 'babel-runtime/helpers/create-class', 'babel-runtime/helpers/ (0, _babelRuntimeHelpersCreateClass['default'])(Dragula, [{ key: 'on', value: function on(eventName, callback) { - this.drake.on(eventName, callback); + this.emitter.on(eventName, callback); } }, { key: 'isContainer', @@ -55,22 +57,21 @@ define(['exports', 'babel-runtime/helpers/create-class', 'babel-runtime/helpers/ }, { key: '_events', value: function _events(remove) { - var op = remove ? 'remove' : 'add'; + var op = remove ? 'removeEventListener' : 'addEventListener'; (0, _touchy.touchy)(document.documentElement, op, 'mousedown', this._grab.bind(this)); (0, _touchy.touchy)(document.documentElement, op, 'mouseup', this._release.bind(this)); } }, { key: '_eventualMovements', value: function _eventualMovements(remove) { - var op = remove ? 'remove' : 'add'; + var op = remove ? 'removeEventListener' : 'addEventListener'; (0, _touchy.touchy)(document.documentElement, op, 'mousemove', this._startBecauseMouseMoved.bind(this)); } }, { key: '_movements', value: function _movements(remove) { - var op = remove ? 'remove' : 'add'; - _crossvent[op](document.documentElement, 'selectstart', this._preventGrabbed); - _crossvent[op](document.documentElement, 'click', this._preventGrabbed); + var op = remove ? 'removeEventListener' : 'addEventListener'; + document.documentElement[op]('click', this._preventGrabbed); } }, { key: 'destroy', @@ -197,7 +198,7 @@ define(['exports', 'babel-runtime/helpers/create-class', 'babel-runtime/helpers/ value: function start(context) { if (this._isCopy(context.item, context.source)) { this._copy = context.item.cloneNode(true); - this.drake.emit('cloned', this._copy, context.item, 'copy'); + this.emitter.emit('cloned', this._copy, context.item, 'copy'); } this._source = context.source; @@ -205,7 +206,7 @@ define(['exports', 'babel-runtime/helpers/create-class', 'babel-runtime/helpers/ this._initialSibling = this._currentSibling = _util.Util.nextEl(context.item); this.drake.dragging = true; - this.drake.emit('drag', this._item, this._source); + this.emitter.emit('drag', this._item, this._source); } }, { key: 'end', @@ -252,9 +253,9 @@ define(['exports', 'babel-runtime/helpers/create-class', 'babel-runtime/helpers/ parent.removeChild(_item); } if (this._isInitialPlacement(target)) { - this.drake.emit('cancel', item, this._source, this._source); + this.emitter.emit('cancel', item, this._source, this._source); } else { - this.drake.emit('drop', item, target, this._source, this._currentSibling); + this.emitter.emit('drop', item, target, this._source, this._currentSibling); } this._cleanup(); } @@ -269,7 +270,7 @@ define(['exports', 'babel-runtime/helpers/create-class', 'babel-runtime/helpers/ if (parent) { parent.removeChild(item); } - this.drake.emit(this._copy ? 'cancel' : 'remove', item, parent, this._source); + this.emitter.emit(this._copy ? 'cancel' : 'remove', item, parent, this._source); this._cleanup(); } }, { @@ -289,9 +290,9 @@ define(['exports', 'babel-runtime/helpers/create-class', 'babel-runtime/helpers/ this._source.insertBefore(item, this._initialSibling); } if (initial || reverts) { - this.drake.emit('cancel', item, this._source, this._source); + this.emitter.emit('cancel', item, this._source, this._source); } else { - this.drake.emit('drop', item, parent, this._source, this._currentSibling); + this.emitter.emit('drop', item, parent, this._source, this._currentSibling); } this._cleanup(); } @@ -309,9 +310,9 @@ define(['exports', 'babel-runtime/helpers/create-class', 'babel-runtime/helpers/ } this.drake.dragging = false; if (this._lastDropTarget) { - this.drake.emit('out', item, this._lastDropTarget, this._source); + this.emitter.emit('out', item, this._lastDropTarget, this._source); } - this.drake.emit('dragend', item); + this.emitter.emit('dragend', item); this._source = this._item = this._copy = this._initialSibling = this._currentSibling = this._renderTimer = this._lastDropTarget = null; } }, { @@ -364,7 +365,7 @@ define(['exports', 'babel-runtime/helpers/create-class', 'babel-runtime/helpers/ e.preventDefault(); var moved = function moved(type) { - _this2.drake.emit(type, item, _this2._lastDropTarget, _this2._source); + _this2.emitter.emit(type, item, _this2._lastDropTarget, _this2._source); }; var over = function over() { if (changed) { @@ -417,7 +418,7 @@ define(['exports', 'babel-runtime/helpers/create-class', 'babel-runtime/helpers/ if (reference === null && changed || reference !== item && reference !== _util.Util.nextEl(item)) { this._currentSibling = reference; dropTarget.insertBefore(item, reference); - this.drake.emit('shadow', item, dropTarget, this._source); + this.emitter.emit('shadow', item, dropTarget, this._source); } } }, { @@ -445,16 +446,16 @@ define(['exports', 'babel-runtime/helpers/create-class', 'babel-runtime/helpers/ _classes.rm(this._mirror, 'gu-transit'); _classes.add(this._mirror, 'gu-mirror'); this.options.mirrorContainer.appendChild(this._mirror); - (0, _touchy.touchy)(document.documentElement, 'add', 'mousemove', this.drag.bind(this)); + (0, _touchy.touchy)(document.documentElement, 'addEventListener', 'mousemove', this.drag.bind(this)); _classes.add(this.options.mirrorContainer, 'gu-unselectable'); - this.drake.emit('cloned', this._mirror, this._item, 'mirror'); + this.emitter.emit('cloned', this._mirror, this._item, 'mirror'); } }, { key: 'removeMirrorImage', value: function removeMirrorImage() { if (this._mirror) { _classes.rm(this.options.mirrorContainer, 'gu-unselectable'); - (0, _touchy.touchy)(document.documentElement, 'remove', 'mousemove', this.drag.bind(this)); + (0, _touchy.touchy)(document.documentElement, 'removeEventListener', 'mousemove', this.drag.bind(this)); _util.Util.getParent(this._mirror).removeChild(this._mirror); this._mirror = null; } @@ -462,11 +463,7 @@ define(['exports', 'babel-runtime/helpers/create-class', 'babel-runtime/helpers/ }, { key: 'getReference', value: function getReference(dropTarget, target, x, y) { - var horizontal = this.options.direction === 'horizontal'; - var reference = target !== dropTarget ? inside() : outside(); - return reference; - - outside = function () { + var outside = function outside() { var len = dropTarget.children.length; var i = undefined; var el = undefined; @@ -484,17 +481,21 @@ define(['exports', 'babel-runtime/helpers/create-class', 'babel-runtime/helpers/ return null; }; - inside = function () { + var resolve = function resolve(after) { + return after ? _util.Util.nextEl(target) : target; + }; + + var inside = function inside() { var rect = target.getBoundingClientRect(); if (horizontal) { - return resolve(x > rect.left + getRectWidth(rect) / 2); + return resolve(x > rect.left + _util.Util.getRectWidth(rect) / 2); } - return resolve(y > rect.top + getRectHeight(rect) / 2); + return resolve(y > rect.top + _util.Util.getRectHeight(rect) / 2); }; - resolve = function (after) { - return after ? _util.Util.nextEl(target) : target; - }; + var horizontal = this.options.direction === 'horizontal'; + var reference = target !== dropTarget ? inside() : outside(); + return reference; } }, { key: '_isCopy', diff --git a/dist/amd/emitter.js b/dist/amd/emitter.js new file mode 100644 index 0000000..6a6bea5 --- /dev/null +++ b/dist/amd/emitter.js @@ -0,0 +1,91 @@ +define(['exports', 'babel-runtime/helpers/class-call-check', 'babel-runtime/helpers/create-class', 'babel-runtime/helpers/slice', 'babel-runtime/helpers/to-consumable-array'], function (exports, _babelRuntimeHelpersClassCallCheck, _babelRuntimeHelpersCreateClass, _babelRuntimeHelpersSlice, _babelRuntimeHelpersToConsumableArray) { + 'use strict'; + + Object.defineProperty(exports, '__esModule', { + value: true + }); + + var EventListener = function EventListener(func) { + var once = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1]; + (0, _babelRuntimeHelpersClassCallCheck['default'])(this, EventListener); + + this.func = func; + this.once = once; + }; + + var Emitter = (function () { + function Emitter(options) { + (0, _babelRuntimeHelpersClassCallCheck['default'])(this, Emitter); + + this.options = options || {}; + this.events = {}; + } + + (0, _babelRuntimeHelpersCreateClass['default'])(Emitter, [{ + key: 'on', + value: function on(type, fn) { + var once = arguments.length <= 2 || arguments[2] === undefined ? false : arguments[2]; + + var newEvent = new EventListener(fn, once); + if (this.events[type] === undefined) { + this.events[type] = []; + } + this.events[type].push(newEvent); + } + }, { + key: 'once', + value: function once(type, fn) { + this.on(type, fn, true); + } + }, { + key: 'off', + value: function off(type, fn) { + if (arguments.length === 1) { + delete this.events[type]; + } else if (arguments.length === 0) { + this.events = {}; + } else { + var eventList = this.events[type]; + if (eventList) { + var index = eventList.findIndex(function (x) { + return x.func === fn; + }); + if (index >= 0) eventList.splice(index, 1); + } + } + } + }, { + key: 'emit', + value: function emit() { + var args = [].concat(_babelRuntimeHelpersSlice['default'].call(arguments)); + return this._emitterSnapshot(args.shift()).apply(this, args); + } + }, { + key: '_emitterSnapshot', + value: function _emitterSnapshot(type) { + var et = (this.events[type] || []).slice(0); + return function () { + var _this = this; + + var args = arguments ? [].concat(_babelRuntimeHelpersSlice['default'].call(arguments)) : []; + if (type === 'error' && this.options.throws !== false && !et.length) { + throw args.length === 1 ? args[0] : args; + } + et.forEach(function (listener) { + if (_this.options.async) { + debounce.apply(undefined, [listener.func].concat((0, _babelRuntimeHelpersToConsumableArray['default'])(args))); + } else { + listener.func.apply(listener, (0, _babelRuntimeHelpersToConsumableArray['default'])(args)); + } + if (listener.once) { + _this.off(type, listener); + } + }); + }; + } + }]); + return Emitter; + })(); + + exports.Emitter = Emitter; +}); \ No newline at end of file diff --git a/dist/amd/index.js b/dist/amd/index.js index 2cd3253..ee73d40 100644 --- a/dist/amd/index.js +++ b/dist/amd/index.js @@ -6,6 +6,7 @@ define(['exports', './options', './dragula'], function (exports, _options, _drag }); exports.configure = configure; exports.Dragula = _dragula.Dragula; + exports.Options = _options.Options; function configure(config, callback) { var defaults = new _options.Options(); diff --git a/dist/amd/touchy.js b/dist/amd/touchy.js index cfc1263..1739c32 100644 --- a/dist/amd/touchy.js +++ b/dist/amd/touchy.js @@ -1,11 +1,10 @@ -define(['exports', 'crossvent'], function (exports, _crossvent) { +define(['exports'], function (exports) { 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); exports.touchy = touchy; - var touch = { mouseup: 'touchend', mousedown: 'touchstart', @@ -24,12 +23,12 @@ define(['exports', 'crossvent'], function (exports, _crossvent) { function touchy(el, op, type, fn) { if (window.navigator.pointerEnabled) { - _crossvent[op](el, pointers[type], fn); + el[op](pointers[type], fn); } else if (window.navigator.msPointerEnabled) { - _crossvent[op](el, microsoft[type], fn); + el[op](microsoft[type], fn); } else { - _crossvent[op](el, touch[type], fn); - _crossvent[op](el, type, fn); + el[op](touch[type], fn); + el[op](type, fn); } } }); \ No newline at end of file diff --git a/dist/commonjs/dragula.js b/dist/commonjs/dragula.js index 205e86f..840d71b 100644 --- a/dist/commonjs/dragula.js +++ b/dist/commonjs/dragula.js @@ -18,11 +18,7 @@ var _options = require('./options'); var _util = require('./util'); -var _contra = require('contra'); - -var _crossvent = require('crossvent'); - -var crossvent = _interopRequireWildcard(_crossvent); +var _emitter = require('./emitter'); var _classes = require('./classes'); @@ -34,7 +30,8 @@ var Dragula = (function () { var len = arguments.length; this.options = options || new _options.Options(); - this.drake = (0, _contra.emitter)({ + this.emitter = new _emitter.Emitter(); + this.drake = { containers: this.options.containers, start: this.manualStart.bind(this), end: this.end.bind(this), @@ -42,10 +39,11 @@ var Dragula = (function () { remove: this.remove.bind(this), destroy: this.destroy.bind(this), dragging: false - }); + }; if (this.options.removeOnSpill === true) { - this.drake.on('over', spillOver).on('out', spillOut); + this.emitter.on('over', this.spillOver.bind(this)); + this.emitter.on('out', this.spillOut.bind(this)); } this._events(); @@ -68,7 +66,7 @@ var Dragula = (function () { _createClass(Dragula, [{ key: 'on', value: function on(eventName, callback) { - this.drake.on(eventName, callback); + this.emitter.on(eventName, callback); } }, { key: 'isContainer', @@ -78,22 +76,21 @@ var Dragula = (function () { }, { key: '_events', value: function _events(remove) { - var op = remove ? 'remove' : 'add'; + var op = remove ? 'removeEventListener' : 'addEventListener'; (0, _touchy.touchy)(document.documentElement, op, 'mousedown', this._grab.bind(this)); (0, _touchy.touchy)(document.documentElement, op, 'mouseup', this._release.bind(this)); } }, { key: '_eventualMovements', value: function _eventualMovements(remove) { - var op = remove ? 'remove' : 'add'; + var op = remove ? 'removeEventListener' : 'addEventListener'; (0, _touchy.touchy)(document.documentElement, op, 'mousemove', this._startBecauseMouseMoved.bind(this)); } }, { key: '_movements', value: function _movements(remove) { - var op = remove ? 'remove' : 'add'; - crossvent[op](document.documentElement, 'selectstart', this._preventGrabbed); - crossvent[op](document.documentElement, 'click', this._preventGrabbed); + var op = remove ? 'removeEventListener' : 'addEventListener'; + document.documentElement[op]('click', this._preventGrabbed); } }, { key: 'destroy', @@ -220,7 +217,7 @@ var Dragula = (function () { value: function start(context) { if (this._isCopy(context.item, context.source)) { this._copy = context.item.cloneNode(true); - this.drake.emit('cloned', this._copy, context.item, 'copy'); + this.emitter.emit('cloned', this._copy, context.item, 'copy'); } this._source = context.source; @@ -228,7 +225,7 @@ var Dragula = (function () { this._initialSibling = this._currentSibling = _util.Util.nextEl(context.item); this.drake.dragging = true; - this.drake.emit('drag', this._item, this._source); + this.emitter.emit('drag', this._item, this._source); } }, { key: 'end', @@ -275,9 +272,9 @@ var Dragula = (function () { parent.removeChild(_item); } if (this._isInitialPlacement(target)) { - this.drake.emit('cancel', item, this._source, this._source); + this.emitter.emit('cancel', item, this._source, this._source); } else { - this.drake.emit('drop', item, target, this._source, this._currentSibling); + this.emitter.emit('drop', item, target, this._source, this._currentSibling); } this._cleanup(); } @@ -292,7 +289,7 @@ var Dragula = (function () { if (parent) { parent.removeChild(item); } - this.drake.emit(this._copy ? 'cancel' : 'remove', item, parent, this._source); + this.emitter.emit(this._copy ? 'cancel' : 'remove', item, parent, this._source); this._cleanup(); } }, { @@ -312,9 +309,9 @@ var Dragula = (function () { this._source.insertBefore(item, this._initialSibling); } if (initial || reverts) { - this.drake.emit('cancel', item, this._source, this._source); + this.emitter.emit('cancel', item, this._source, this._source); } else { - this.drake.emit('drop', item, parent, this._source, this._currentSibling); + this.emitter.emit('drop', item, parent, this._source, this._currentSibling); } this._cleanup(); } @@ -332,9 +329,9 @@ var Dragula = (function () { } this.drake.dragging = false; if (this._lastDropTarget) { - this.drake.emit('out', item, this._lastDropTarget, this._source); + this.emitter.emit('out', item, this._lastDropTarget, this._source); } - this.drake.emit('dragend', item); + this.emitter.emit('dragend', item); this._source = this._item = this._copy = this._initialSibling = this._currentSibling = this._renderTimer = this._lastDropTarget = null; } }, { @@ -387,7 +384,7 @@ var Dragula = (function () { e.preventDefault(); var moved = function moved(type) { - _this2.drake.emit(type, item, _this2._lastDropTarget, _this2._source); + _this2.emitter.emit(type, item, _this2._lastDropTarget, _this2._source); }; var over = function over() { if (changed) { @@ -440,7 +437,7 @@ var Dragula = (function () { if (reference === null && changed || reference !== item && reference !== _util.Util.nextEl(item)) { this._currentSibling = reference; dropTarget.insertBefore(item, reference); - this.drake.emit('shadow', item, dropTarget, this._source); + this.emitter.emit('shadow', item, dropTarget, this._source); } } }, { @@ -468,16 +465,16 @@ var Dragula = (function () { classes.rm(this._mirror, 'gu-transit'); classes.add(this._mirror, 'gu-mirror'); this.options.mirrorContainer.appendChild(this._mirror); - (0, _touchy.touchy)(document.documentElement, 'add', 'mousemove', this.drag.bind(this)); + (0, _touchy.touchy)(document.documentElement, 'addEventListener', 'mousemove', this.drag.bind(this)); classes.add(this.options.mirrorContainer, 'gu-unselectable'); - this.drake.emit('cloned', this._mirror, this._item, 'mirror'); + this.emitter.emit('cloned', this._mirror, this._item, 'mirror'); } }, { key: 'removeMirrorImage', value: function removeMirrorImage() { if (this._mirror) { classes.rm(this.options.mirrorContainer, 'gu-unselectable'); - (0, _touchy.touchy)(document.documentElement, 'remove', 'mousemove', this.drag.bind(this)); + (0, _touchy.touchy)(document.documentElement, 'removeEventListener', 'mousemove', this.drag.bind(this)); _util.Util.getParent(this._mirror).removeChild(this._mirror); this._mirror = null; } @@ -485,11 +482,7 @@ var Dragula = (function () { }, { key: 'getReference', value: function getReference(dropTarget, target, x, y) { - var horizontal = this.options.direction === 'horizontal'; - var reference = target !== dropTarget ? inside() : outside(); - return reference; - - outside = function () { + var outside = function outside() { var len = dropTarget.children.length; var i = undefined; var el = undefined; @@ -507,17 +500,21 @@ var Dragula = (function () { return null; }; - inside = function () { + var resolve = function resolve(after) { + return after ? _util.Util.nextEl(target) : target; + }; + + var inside = function inside() { var rect = target.getBoundingClientRect(); if (horizontal) { - return resolve(x > rect.left + getRectWidth(rect) / 2); + return resolve(x > rect.left + _util.Util.getRectWidth(rect) / 2); } - return resolve(y > rect.top + getRectHeight(rect) / 2); + return resolve(y > rect.top + _util.Util.getRectHeight(rect) / 2); }; - resolve = function (after) { - return after ? _util.Util.nextEl(target) : target; - }; + var horizontal = this.options.direction === 'horizontal'; + var reference = target !== dropTarget ? inside() : outside(); + return reference; } }, { key: '_isCopy', diff --git a/dist/commonjs/emitter.js b/dist/commonjs/emitter.js new file mode 100644 index 0000000..e491321 --- /dev/null +++ b/dist/commonjs/emitter.js @@ -0,0 +1,99 @@ +'use strict'; + +var _classCallCheck = require('babel-runtime/helpers/class-call-check')['default']; + +var _createClass = require('babel-runtime/helpers/create-class')['default']; + +var _slice = require('babel-runtime/helpers/slice')['default']; + +var _toConsumableArray = require('babel-runtime/helpers/to-consumable-array')['default']; + +Object.defineProperty(exports, '__esModule', { + value: true +}); + +var EventListener = function EventListener(func) { + var once = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1]; + + _classCallCheck(this, EventListener); + + this.func = func; + this.once = once; +}; + +var Emitter = (function () { + function Emitter(options) { + _classCallCheck(this, Emitter); + + this.options = options || {}; + this.events = {}; + } + + _createClass(Emitter, [{ + key: 'on', + value: function on(type, fn) { + var once = arguments.length <= 2 || arguments[2] === undefined ? false : arguments[2]; + + var newEvent = new EventListener(fn, once); + if (this.events[type] === undefined) { + this.events[type] = []; + } + this.events[type].push(newEvent); + } + }, { + key: 'once', + value: function once(type, fn) { + this.on(type, fn, true); + } + }, { + key: 'off', + value: function off(type, fn) { + if (arguments.length === 1) { + delete this.events[type]; + } else if (arguments.length === 0) { + this.events = {}; + } else { + var eventList = this.events[type]; + if (eventList) { + var index = eventList.findIndex(function (x) { + return x.func === fn; + }); + if (index >= 0) eventList.splice(index, 1); + } + } + } + }, { + key: 'emit', + value: function emit() { + var args = [].concat(_slice.call(arguments)); + return this._emitterSnapshot(args.shift()).apply(this, args); + } + }, { + key: '_emitterSnapshot', + value: function _emitterSnapshot(type) { + var et = (this.events[type] || []).slice(0); + return function () { + var _this = this; + + var args = arguments ? [].concat(_slice.call(arguments)) : []; + if (type === 'error' && this.options.throws !== false && !et.length) { + throw args.length === 1 ? args[0] : args; + } + et.forEach(function (listener) { + if (_this.options.async) { + debounce.apply(undefined, [listener.func].concat(_toConsumableArray(args))); + } else { + listener.func.apply(listener, _toConsumableArray(args)); + } + if (listener.once) { + _this.off(type, listener); + } + }); + }; + } + }]); + + return Emitter; +})(); + +exports.Emitter = Emitter; \ No newline at end of file diff --git a/dist/commonjs/index.js b/dist/commonjs/index.js index cdcd1ce..ed58524 100644 --- a/dist/commonjs/index.js +++ b/dist/commonjs/index.js @@ -10,6 +10,7 @@ var _options = require('./options'); var _dragula = require('./dragula'); exports.Dragula = _dragula.Dragula; +exports.Options = _options.Options; function configure(config, callback) { var defaults = new _options.Options(); diff --git a/dist/commonjs/touchy.js b/dist/commonjs/touchy.js index 461f607..d11108b 100644 --- a/dist/commonjs/touchy.js +++ b/dist/commonjs/touchy.js @@ -1,16 +1,9 @@ 'use strict'; -var _interopRequireWildcard = require('babel-runtime/helpers/interop-require-wildcard')['default']; - Object.defineProperty(exports, '__esModule', { value: true }); exports.touchy = touchy; - -var _crossvent = require('crossvent'); - -var crossvent = _interopRequireWildcard(_crossvent); - var touch = { mouseup: 'touchend', mousedown: 'touchstart', @@ -29,11 +22,11 @@ var microsoft = { function touchy(el, op, type, fn) { if (window.navigator.pointerEnabled) { - crossvent[op](el, pointers[type], fn); + el[op](pointers[type], fn); } else if (window.navigator.msPointerEnabled) { - crossvent[op](el, microsoft[type], fn); + el[op](microsoft[type], fn); } else { - crossvent[op](el, touch[type], fn); - crossvent[op](el, type, fn); + el[op](touch[type], fn); + el[op](type, fn); } } \ No newline at end of file diff --git a/dist/es6/dragula.js b/dist/es6/dragula.js index a5691df..f357a76 100644 --- a/dist/es6/dragula.js +++ b/dist/es6/dragula.js @@ -2,8 +2,7 @@ import {inject} from 'aurelia-dependency-injection'; import {touchy} from './touchy'; import {GLOBAL_OPTIONS, Options} from './options'; import {Util} from './util'; -import {emitter} from 'contra'; -import * as crossvent from 'crossvent'; +import {Emitter} from './emitter'; import * as classes from './classes'; @@ -13,7 +12,8 @@ export class Dragula { constructor(options) { let len = arguments.length; this.options = options || new Options(); - this.drake = emitter({ + this.emitter = new Emitter(); + this.drake = { containers: this.options.containers, start: ::this.manualStart, end: ::this.end, @@ -21,10 +21,11 @@ export class Dragula { remove: ::this.remove, destroy: ::this.destroy, dragging: false - }); + }; if (this.options.removeOnSpill === true) { - this.drake.on('over', spillOver).on('out', spillOut); + this.emitter.on('over', ::this.spillOver); + this.emitter.on('out', ::this.spillOut); } this._events(); @@ -45,7 +46,7 @@ export class Dragula { } on(eventName, callback) { - this.drake.on(eventName, callback); + this.emitter.on(eventName, callback); } get containers() { @@ -65,20 +66,19 @@ export class Dragula { } _events(remove) { - let op = remove ? 'remove' : 'add'; + let op = remove ? 'removeEventListener' : 'addEventListener'; touchy(document.documentElement, op, 'mousedown', ::this._grab); touchy(document.documentElement, op, 'mouseup', ::this._release); } _eventualMovements(remove) { - let op = remove ? 'remove' : 'add'; + let op = remove ? 'removeEventListener' : 'addEventListener'; touchy(document.documentElement, op, 'mousemove', ::this._startBecauseMouseMoved); } _movements(remove) { - let op = remove ? 'remove' : 'add'; - crossvent[op](document.documentElement, 'selectstart', this._preventGrabbed); // IE8 - crossvent[op](document.documentElement, 'click', this._preventGrabbed); + let op = remove ? 'removeEventListener' : 'addEventListener'; + document.documentElement[op]('click', this._preventGrabbed); } destroy() { @@ -198,7 +198,7 @@ export class Dragula { start(context) { if (this._isCopy(context.item, context.source)) { this._copy = context.item.cloneNode(true); - this.drake.emit('cloned', this._copy, context.item, 'copy'); + this.emitter.emit('cloned', this._copy, context.item, 'copy'); } this._source = context.source; @@ -206,7 +206,7 @@ export class Dragula { this._initialSibling = this._currentSibling = Util.nextEl(context.item); this.drake.dragging = true; - this.drake.emit('drag', this._item, this._source); + this.emitter.emit('drag', this._item, this._source); } end() { @@ -249,9 +249,9 @@ export class Dragula { parent.removeChild(_item); } if (this._isInitialPlacement(target)) { - this.drake.emit('cancel', item, this._source, this._source); + this.emitter.emit('cancel', item, this._source, this._source); } else { - this.drake.emit('drop', item, target, this._source, this._currentSibling); + this.emitter.emit('drop', item, target, this._source, this._currentSibling); } this._cleanup(); } @@ -265,7 +265,7 @@ export class Dragula { if (parent) { parent.removeChild(item); } - this.drake.emit(this._copy ? 'cancel' : 'remove', item, parent, this._source); + this.emitter.emit(this._copy ? 'cancel' : 'remove', item, parent, this._source); this._cleanup(); } @@ -284,9 +284,9 @@ export class Dragula { this._source.insertBefore(item, this._initialSibling); } if (initial || reverts) { - this.drake.emit('cancel', item, this._source, this._source); + this.emitter.emit('cancel', item, this._source, this._source); } else { - this.drake.emit('drop', item, parent, this._source, this._currentSibling); + this.emitter.emit('drop', item, parent, this._source, this._currentSibling); } this._cleanup(); } @@ -303,9 +303,9 @@ export class Dragula { } this.drake.dragging = false; if (this._lastDropTarget) { - this.drake.emit('out', item, this._lastDropTarget, this._source); + this.emitter.emit('out', item, this._lastDropTarget, this._source); } - this.drake.emit('dragend', item); + this.emitter.emit('dragend', item); this._source = this._item = this._copy = this._initialSibling = this._currentSibling = this._renderTimer = this._lastDropTarget = null; } @@ -350,7 +350,7 @@ export class Dragula { } e.preventDefault(); - let moved = (type) => { this.drake.emit(type, item, this._lastDropTarget, this._source); } + let moved = (type) => { this.emitter.emit(type, item, this._lastDropTarget, this._source); } let over = () => { if (changed) { moved('over'); } } let out = () => { if (this._lastDropTarget) { moved('out'); } } @@ -398,7 +398,7 @@ export class Dragula { ) { this._currentSibling = reference; dropTarget.insertBefore(item, reference); - this.drake.emit('shadow', item, dropTarget, this._source); + this.emitter.emit('shadow', item, dropTarget, this._source); } } @@ -421,26 +421,22 @@ export class Dragula { classes.rm(this._mirror, 'gu-transit'); classes.add(this._mirror, 'gu-mirror'); this.options.mirrorContainer.appendChild(this._mirror); - touchy(document.documentElement, 'add', 'mousemove', ::this.drag); + touchy(document.documentElement, 'addEventListener', 'mousemove', ::this.drag); classes.add(this.options.mirrorContainer, 'gu-unselectable'); - this.drake.emit('cloned', this._mirror, this._item, 'mirror'); + this.emitter.emit('cloned', this._mirror, this._item, 'mirror'); } removeMirrorImage() { if (this._mirror) { classes.rm(this.options.mirrorContainer, 'gu-unselectable'); - touchy(document.documentElement, 'remove', 'mousemove', ::this.drag); + touchy(document.documentElement, 'removeEventListener', 'mousemove', ::this.drag); Util.getParent(this._mirror).removeChild(this._mirror); this._mirror = null; } } getReference(dropTarget, target, x, y) { - let horizontal = this.options.direction === 'horizontal'; - let reference = target !== dropTarget ? inside() : outside(); - return reference; - - outside = () => { // slower, but able to figure out any position + let outside = () => { // slower, but able to figure out any position let len = dropTarget.children.length; let i; let el; @@ -454,17 +450,22 @@ export class Dragula { return null; } - inside = () => { // faster, but only available if dropped inside a child element + let resolve = (after) => { + return after ? Util.nextEl(target) : target; + } + + let inside = () => { // faster, but only available if dropped inside a child element let rect = target.getBoundingClientRect(); if (horizontal) { - return resolve(x > rect.left + getRectWidth(rect) / 2); + return resolve(x > rect.left + Util.getRectWidth(rect) / 2); } - return resolve(y > rect.top + getRectHeight(rect) / 2); + return resolve(y > rect.top + Util.getRectHeight(rect) / 2); } - resolve = (after) => { - return after ? Util.nextEl(target) : target; - } + + let horizontal = this.options.direction === 'horizontal'; + let reference = target !== dropTarget ? inside() : outside(); + return reference; } _isCopy(item, container) { diff --git a/dist/es6/emitter.js b/dist/es6/emitter.js new file mode 100644 index 0000000..932e704 --- /dev/null +++ b/dist/es6/emitter.js @@ -0,0 +1,70 @@ +//let debounce = require('./debounce'); + +class EventListener { + + constructor(func, once = false) { + this.func = func; + this.once = once; + } +} + +export class Emitter { + + constructor(options) { + this.options = options || {}; + this.events = {}; + } + + on(type, fn, once = false) { + let newEvent = new EventListener(fn, once); + if (this.events[type] === undefined) { + this.events[type] = []; + } + this.events[type].push(newEvent); + } + + once(type, fn) { + this.on(type, fn, true); + } + + off(type, fn) { + if (arguments.length === 1) { + delete this.events[type]; + } + else if (arguments.length === 0) { + this.events = {}; + } + else { + let eventList = this.events[type]; + if (eventList) { + let index = eventList.findIndex(x => x.func === fn); + if (index >= 0) + eventList.splice(index, 1); + } + } + } + + emit() { + let args = [...arguments]; + return this._emitterSnapshot(args.shift()).apply(this, args); + } + + _emitterSnapshot(type) { + let et = (this.events[type] || []).slice(0); + return function() { + let args = arguments ? [...arguments] : []; + if (type === 'error' && this.options.throws !== false && !et.length) { throw args.length === 1 ? args[0] : args; } + et.forEach(listener => { + if (this.options.async) { + debounce(listener.func, ...args); + } + else { + listener.func(...args); + } + if (listener.once) { + this.off(type, listener); + } + }); + } + } +} diff --git a/dist/es6/index.js b/dist/es6/index.js index da38ce1..2e0dd21 100644 --- a/dist/es6/index.js +++ b/dist/es6/index.js @@ -1,7 +1,7 @@ import {Options, GLOBAL_OPTIONS} from './options'; import {Dragula} from './dragula'; -export {Dragula}; +export {Dragula, Options}; export function configure(config, callback) { let defaults = new Options(); diff --git a/dist/es6/touchy.js b/dist/es6/touchy.js index 045ddd2..03c2d0e 100644 --- a/dist/es6/touchy.js +++ b/dist/es6/touchy.js @@ -1,5 +1,3 @@ -import * as crossvent from 'crossvent'; - const touch = { mouseup: 'touchend', mousedown: 'touchstart', @@ -18,11 +16,11 @@ const microsoft = { export function touchy(el, op, type, fn) { if (window.navigator.pointerEnabled) { - crossvent[op](el, pointers[type], fn); + el[op](pointers[type], fn); } else if (window.navigator.msPointerEnabled) { - crossvent[op](el, microsoft[type], fn); + el[op](microsoft[type], fn); } else { - crossvent[op](el, touch[type], fn); - crossvent[op](el, type, fn); + el[op](touch[type], fn); + el[op](type, fn); } } \ No newline at end of file diff --git a/dist/system/dragula.js b/dist/system/dragula.js index 8faa2f7..36aae3f 100644 --- a/dist/system/dragula.js +++ b/dist/system/dragula.js @@ -1,5 +1,5 @@ -System.register(['babel-runtime/helpers/create-class', 'babel-runtime/helpers/class-call-check', 'aurelia-dependency-injection', './touchy', './options', './util', 'contra', 'crossvent', './classes'], function (_export) { - var _createClass, _classCallCheck, inject, touchy, GLOBAL_OPTIONS, Options, Util, emitter, crossvent, classes, Dragula; +System.register(['babel-runtime/helpers/create-class', 'babel-runtime/helpers/class-call-check', 'aurelia-dependency-injection', './touchy', './options', './util', './emitter', './classes'], function (_export) { + var _createClass, _classCallCheck, inject, touchy, GLOBAL_OPTIONS, Options, Util, Emitter, classes, Dragula; return { setters: [function (_babelRuntimeHelpersCreateClass) { @@ -15,10 +15,8 @@ System.register(['babel-runtime/helpers/create-class', 'babel-runtime/helpers/cl Options = _options.Options; }, function (_util) { Util = _util.Util; - }, function (_contra) { - emitter = _contra.emitter; - }, function (_crossvent) { - crossvent = _crossvent; + }, function (_emitter) { + Emitter = _emitter.Emitter; }, function (_classes) { classes = _classes; }], @@ -31,7 +29,8 @@ System.register(['babel-runtime/helpers/create-class', 'babel-runtime/helpers/cl var len = arguments.length; this.options = options || new Options(); - this.drake = emitter({ + this.emitter = new Emitter(); + this.drake = { containers: this.options.containers, start: this.manualStart.bind(this), end: this.end.bind(this), @@ -39,10 +38,11 @@ System.register(['babel-runtime/helpers/create-class', 'babel-runtime/helpers/cl remove: this.remove.bind(this), destroy: this.destroy.bind(this), dragging: false - }); + }; if (this.options.removeOnSpill === true) { - this.drake.on('over', spillOver).on('out', spillOut); + this.emitter.on('over', this.spillOver.bind(this)); + this.emitter.on('out', this.spillOut.bind(this)); } this._events(); @@ -65,7 +65,7 @@ System.register(['babel-runtime/helpers/create-class', 'babel-runtime/helpers/cl _createClass(Dragula, [{ key: 'on', value: function on(eventName, callback) { - this.drake.on(eventName, callback); + this.emitter.on(eventName, callback); } }, { key: 'isContainer', @@ -75,22 +75,21 @@ System.register(['babel-runtime/helpers/create-class', 'babel-runtime/helpers/cl }, { key: '_events', value: function _events(remove) { - var op = remove ? 'remove' : 'add'; + var op = remove ? 'removeEventListener' : 'addEventListener'; touchy(document.documentElement, op, 'mousedown', this._grab.bind(this)); touchy(document.documentElement, op, 'mouseup', this._release.bind(this)); } }, { key: '_eventualMovements', value: function _eventualMovements(remove) { - var op = remove ? 'remove' : 'add'; + var op = remove ? 'removeEventListener' : 'addEventListener'; touchy(document.documentElement, op, 'mousemove', this._startBecauseMouseMoved.bind(this)); } }, { key: '_movements', value: function _movements(remove) { - var op = remove ? 'remove' : 'add'; - crossvent[op](document.documentElement, 'selectstart', this._preventGrabbed); - crossvent[op](document.documentElement, 'click', this._preventGrabbed); + var op = remove ? 'removeEventListener' : 'addEventListener'; + document.documentElement[op]('click', this._preventGrabbed); } }, { key: 'destroy', @@ -217,7 +216,7 @@ System.register(['babel-runtime/helpers/create-class', 'babel-runtime/helpers/cl value: function start(context) { if (this._isCopy(context.item, context.source)) { this._copy = context.item.cloneNode(true); - this.drake.emit('cloned', this._copy, context.item, 'copy'); + this.emitter.emit('cloned', this._copy, context.item, 'copy'); } this._source = context.source; @@ -225,7 +224,7 @@ System.register(['babel-runtime/helpers/create-class', 'babel-runtime/helpers/cl this._initialSibling = this._currentSibling = Util.nextEl(context.item); this.drake.dragging = true; - this.drake.emit('drag', this._item, this._source); + this.emitter.emit('drag', this._item, this._source); } }, { key: 'end', @@ -272,9 +271,9 @@ System.register(['babel-runtime/helpers/create-class', 'babel-runtime/helpers/cl parent.removeChild(_item); } if (this._isInitialPlacement(target)) { - this.drake.emit('cancel', item, this._source, this._source); + this.emitter.emit('cancel', item, this._source, this._source); } else { - this.drake.emit('drop', item, target, this._source, this._currentSibling); + this.emitter.emit('drop', item, target, this._source, this._currentSibling); } this._cleanup(); } @@ -289,7 +288,7 @@ System.register(['babel-runtime/helpers/create-class', 'babel-runtime/helpers/cl if (parent) { parent.removeChild(item); } - this.drake.emit(this._copy ? 'cancel' : 'remove', item, parent, this._source); + this.emitter.emit(this._copy ? 'cancel' : 'remove', item, parent, this._source); this._cleanup(); } }, { @@ -309,9 +308,9 @@ System.register(['babel-runtime/helpers/create-class', 'babel-runtime/helpers/cl this._source.insertBefore(item, this._initialSibling); } if (initial || reverts) { - this.drake.emit('cancel', item, this._source, this._source); + this.emitter.emit('cancel', item, this._source, this._source); } else { - this.drake.emit('drop', item, parent, this._source, this._currentSibling); + this.emitter.emit('drop', item, parent, this._source, this._currentSibling); } this._cleanup(); } @@ -329,9 +328,9 @@ System.register(['babel-runtime/helpers/create-class', 'babel-runtime/helpers/cl } this.drake.dragging = false; if (this._lastDropTarget) { - this.drake.emit('out', item, this._lastDropTarget, this._source); + this.emitter.emit('out', item, this._lastDropTarget, this._source); } - this.drake.emit('dragend', item); + this.emitter.emit('dragend', item); this._source = this._item = this._copy = this._initialSibling = this._currentSibling = this._renderTimer = this._lastDropTarget = null; } }, { @@ -384,7 +383,7 @@ System.register(['babel-runtime/helpers/create-class', 'babel-runtime/helpers/cl e.preventDefault(); var moved = function moved(type) { - _this2.drake.emit(type, item, _this2._lastDropTarget, _this2._source); + _this2.emitter.emit(type, item, _this2._lastDropTarget, _this2._source); }; var over = function over() { if (changed) { @@ -437,7 +436,7 @@ System.register(['babel-runtime/helpers/create-class', 'babel-runtime/helpers/cl if (reference === null && changed || reference !== item && reference !== Util.nextEl(item)) { this._currentSibling = reference; dropTarget.insertBefore(item, reference); - this.drake.emit('shadow', item, dropTarget, this._source); + this.emitter.emit('shadow', item, dropTarget, this._source); } } }, { @@ -465,16 +464,16 @@ System.register(['babel-runtime/helpers/create-class', 'babel-runtime/helpers/cl classes.rm(this._mirror, 'gu-transit'); classes.add(this._mirror, 'gu-mirror'); this.options.mirrorContainer.appendChild(this._mirror); - touchy(document.documentElement, 'add', 'mousemove', this.drag.bind(this)); + touchy(document.documentElement, 'addEventListener', 'mousemove', this.drag.bind(this)); classes.add(this.options.mirrorContainer, 'gu-unselectable'); - this.drake.emit('cloned', this._mirror, this._item, 'mirror'); + this.emitter.emit('cloned', this._mirror, this._item, 'mirror'); } }, { key: 'removeMirrorImage', value: function removeMirrorImage() { if (this._mirror) { classes.rm(this.options.mirrorContainer, 'gu-unselectable'); - touchy(document.documentElement, 'remove', 'mousemove', this.drag.bind(this)); + touchy(document.documentElement, 'removeEventListener', 'mousemove', this.drag.bind(this)); Util.getParent(this._mirror).removeChild(this._mirror); this._mirror = null; } @@ -482,11 +481,7 @@ System.register(['babel-runtime/helpers/create-class', 'babel-runtime/helpers/cl }, { key: 'getReference', value: function getReference(dropTarget, target, x, y) { - var horizontal = this.options.direction === 'horizontal'; - var reference = target !== dropTarget ? inside() : outside(); - return reference; - - outside = function () { + var outside = function outside() { var len = dropTarget.children.length; var i = undefined; var el = undefined; @@ -504,17 +499,21 @@ System.register(['babel-runtime/helpers/create-class', 'babel-runtime/helpers/cl return null; }; - inside = function () { + var resolve = function resolve(after) { + return after ? Util.nextEl(target) : target; + }; + + var inside = function inside() { var rect = target.getBoundingClientRect(); if (horizontal) { - return resolve(x > rect.left + getRectWidth(rect) / 2); + return resolve(x > rect.left + Util.getRectWidth(rect) / 2); } - return resolve(y > rect.top + getRectHeight(rect) / 2); + return resolve(y > rect.top + Util.getRectHeight(rect) / 2); }; - resolve = function (after) { - return after ? Util.nextEl(target) : target; - }; + var horizontal = this.options.direction === 'horizontal'; + var reference = target !== dropTarget ? inside() : outside(); + return reference; } }, { key: '_isCopy', diff --git a/dist/system/emitter.js b/dist/system/emitter.js new file mode 100644 index 0000000..0bb4d06 --- /dev/null +++ b/dist/system/emitter.js @@ -0,0 +1,104 @@ +System.register(['babel-runtime/helpers/class-call-check', 'babel-runtime/helpers/create-class', 'babel-runtime/helpers/slice', 'babel-runtime/helpers/to-consumable-array'], function (_export) { + var _classCallCheck, _createClass, _slice, _toConsumableArray, EventListener, Emitter; + + return { + setters: [function (_babelRuntimeHelpersClassCallCheck) { + _classCallCheck = _babelRuntimeHelpersClassCallCheck['default']; + }, function (_babelRuntimeHelpersCreateClass) { + _createClass = _babelRuntimeHelpersCreateClass['default']; + }, function (_babelRuntimeHelpersSlice) { + _slice = _babelRuntimeHelpersSlice['default']; + }, function (_babelRuntimeHelpersToConsumableArray) { + _toConsumableArray = _babelRuntimeHelpersToConsumableArray['default']; + }], + execute: function () { + 'use strict'; + + EventListener = function EventListener(func) { + var once = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1]; + + _classCallCheck(this, EventListener); + + this.func = func; + this.once = once; + }; + + Emitter = (function () { + function Emitter(options) { + _classCallCheck(this, Emitter); + + this.options = options || {}; + this.events = {}; + } + + _createClass(Emitter, [{ + key: 'on', + value: function on(type, fn) { + var once = arguments.length <= 2 || arguments[2] === undefined ? false : arguments[2]; + + var newEvent = new EventListener(fn, once); + if (this.events[type] === undefined) { + this.events[type] = []; + } + this.events[type].push(newEvent); + } + }, { + key: 'once', + value: function once(type, fn) { + this.on(type, fn, true); + } + }, { + key: 'off', + value: function off(type, fn) { + if (arguments.length === 1) { + delete this.events[type]; + } else if (arguments.length === 0) { + this.events = {}; + } else { + var eventList = this.events[type]; + if (eventList) { + var index = eventList.findIndex(function (x) { + return x.func === fn; + }); + if (index >= 0) eventList.splice(index, 1); + } + } + } + }, { + key: 'emit', + value: function emit() { + var args = [].concat(_slice.call(arguments)); + return this._emitterSnapshot(args.shift()).apply(this, args); + } + }, { + key: '_emitterSnapshot', + value: function _emitterSnapshot(type) { + var et = (this.events[type] || []).slice(0); + return function () { + var _this = this; + + var args = arguments ? [].concat(_slice.call(arguments)) : []; + if (type === 'error' && this.options.throws !== false && !et.length) { + throw args.length === 1 ? args[0] : args; + } + et.forEach(function (listener) { + if (_this.options.async) { + debounce.apply(undefined, [listener.func].concat(_toConsumableArray(args))); + } else { + listener.func.apply(listener, _toConsumableArray(args)); + } + if (listener.once) { + _this.off(type, listener); + } + }); + }; + } + }]); + + return Emitter; + })(); + + _export('Emitter', Emitter); + } + }; +}); \ No newline at end of file diff --git a/dist/system/index.js b/dist/system/index.js index eee5d3c..eed032c 100644 --- a/dist/system/index.js +++ b/dist/system/index.js @@ -23,6 +23,8 @@ System.register(['./options', './dragula'], function (_export) { }], execute: function () { _export('Dragula', Dragula); + + _export('Options', Options); } }; }); \ No newline at end of file diff --git a/dist/system/touchy.js b/dist/system/touchy.js index a942a02..f6560b2 100644 --- a/dist/system/touchy.js +++ b/dist/system/touchy.js @@ -1,25 +1,23 @@ -System.register(['crossvent'], function (_export) { +System.register([], function (_export) { 'use strict'; - var crossvent, touch, pointers, microsoft; + var touch, pointers, microsoft; _export('touchy', touchy); function touchy(el, op, type, fn) { if (window.navigator.pointerEnabled) { - crossvent[op](el, pointers[type], fn); + el[op](pointers[type], fn); } else if (window.navigator.msPointerEnabled) { - crossvent[op](el, microsoft[type], fn); + el[op](microsoft[type], fn); } else { - crossvent[op](el, touch[type], fn); - crossvent[op](el, type, fn); + el[op](touch[type], fn); + el[op](type, fn); } } return { - setters: [function (_crossvent) { - crossvent = _crossvent; - }], + setters: [], execute: function () { touch = { mouseup: 'touchend',