UNPKG

interactjs

Version:

Drag and drop, resizing and multi-touch gestures with inertia and snapping for modern browsers (and also IE9+)

162 lines (137 loc) 4.78 kB
const actions = require('./base'); const utils = require('../utils'); const InteractEvent = require('../InteractEvent'); /** @lends Interactable */ const Interactable = require('../Interactable'); const Interaction = require('../Interaction'); const defaultOptions = require('../defaultOptions'); const drag = { defaults: { enabled : false, mouseButtons: null, origin : null, snap : null, restrict : null, inertia : null, autoScroll: null, startAxis : 'xy', lockAxis : 'xy', }, checker: function (pointer, event, interactable) { const dragOptions = interactable.options.drag; return dragOptions.enabled ? { name: 'drag', axis: (dragOptions.lockAxis === 'start' ? dragOptions.startAxis : dragOptions.lockAxis)} : null; }, getCursor: function () { return 'move'; }, }; Interaction.signals.on('before-action-move', function ({ interaction }) { if (interaction.prepared.name !== 'drag') { return; } const axis = interaction.prepared.axis; if (axis === 'x') { interaction.curCoords.page.y = interaction.startCoords.page.y; interaction.curCoords.client.y = interaction.startCoords.client.y; interaction.pointerDelta.page.speed = Math.abs(interaction.pointerDelta.page.vx); interaction.pointerDelta.client.speed = Math.abs(interaction.pointerDelta.client.vx); interaction.pointerDelta.client.vy = 0; interaction.pointerDelta.page.vy = 0; } else if (axis === 'y') { interaction.curCoords.page.x = interaction.startCoords.page.x; interaction.curCoords.client.x = interaction.startCoords.client.x; interaction.pointerDelta.page.speed = Math.abs(interaction.pointerDelta.page.vy); interaction.pointerDelta.client.speed = Math.abs(interaction.pointerDelta.client.vy); interaction.pointerDelta.client.vx = 0; interaction.pointerDelta.page.vx = 0; } }); // dragmove InteractEvent.signals.on('new', function ({ iEvent, interaction }) { if (iEvent.type !== 'dragmove') { return; } const axis = interaction.prepared.axis; if (axis === 'x') { iEvent.pageY = interaction.startCoords.page.y; iEvent.clientY = interaction.startCoords.client.y; iEvent.dy = 0; } else if (axis === 'y') { iEvent.pageX = interaction.startCoords.page.x; iEvent.clientX = interaction.startCoords.client.x; iEvent.dx = 0; } }); /** * ```js * interact(element).draggable({ * onstart: function (event) {}, * onmove : function (event) {}, * onend : function (event) {}, * * // the axis in which the first movement must be * // for the drag sequence to start * // 'xy' by default - any direction * startAxis: 'x' || 'y' || 'xy', * * // 'xy' by default - don't restrict to one axis (move in any direction) * // 'x' or 'y' to restrict movement to either axis * // 'start' to restrict movement to the axis the drag started in * lockAxis: 'x' || 'y' || 'xy' || 'start', * * // max number of drags that can happen concurrently * // with elements of this Interactable. Infinity by default * max: Infinity, * * // max number of drags that can target the same element+Interactable * // 1 by default * maxPerElement: 2 * }); * * var isDraggable = interact('element').draggable(); // true * ``` * * Get or set whether drag actions can be performed on the target * * @param {boolean | object} [options] true/false or An object with event * listeners to be fired on drag events (object makes the Interactable * draggable) * @return {boolean | Interactable} boolean indicating if this can be the * target of drag events, or this Interctable */ Interactable.prototype.draggable = function (options) { if (utils.is.object(options)) { this.options.drag.enabled = options.enabled === false? false: true; this.setPerAction('drag', options); this.setOnEvents('drag', options); if (/^(xy|x|y|start)$/.test(options.lockAxis)) { this.options.drag.lockAxis = options.lockAxis; } if (/^(xy|x|y)$/.test(options.startAxis)) { this.options.drag.startAxis = options.startAxis; } return this; } if (utils.is.bool(options)) { this.options.drag.enabled = options; if (!options) { this.ondragstart = this.ondragstart = this.ondragend = null; } return this; } return this.options.drag; }; actions.drag = drag; actions.names.push('drag'); utils.merge(Interactable.eventTypes, [ 'dragstart', 'dragmove', 'draginertiastart', 'draginertiaresume', 'dragend', ]); actions.methodDict.drag = 'draggable'; defaultOptions.drag = drag.defaults; module.exports = drag;