@interactjs/auto-start
Advanced tools
Comparing version 1.4.0-alpha.20 to 1.4.0-alpha.21
@@ -16,3 +16,3 @@ import * as utils from '@interactjs/utils'; | ||
interface PerActionDefaults { | ||
manualStart?: false; | ||
manualStart?: boolean; | ||
max?: number; | ||
@@ -19,0 +19,0 @@ maxPerElement?: number; |
43
base.js
import * as utils from '@interactjs/utils'; | ||
import InteractableMethods from './InteractableMethods'; | ||
; | ||
function install(scope) { | ||
@@ -8,3 +7,3 @@ const { interact, interactions, defaults, } = scope; | ||
// set cursor style on mousedown | ||
interactions.signals.on('down', function ({ interaction, pointer, event, eventTarget }) { | ||
interactions.signals.on('down', ({ interaction, pointer, event, eventTarget }) => { | ||
if (interaction.interacting()) { | ||
@@ -17,6 +16,6 @@ return; | ||
// set cursor style on mousemove | ||
interactions.signals.on('move', function ({ interaction, pointer, event, eventTarget }) { | ||
if (interaction.pointerType !== 'mouse' | ||
|| interaction.pointerIsDown | ||
|| interaction.interacting()) { | ||
interactions.signals.on('move', ({ interaction, pointer, event, eventTarget }) => { | ||
if (interaction.pointerType !== 'mouse' || | ||
interaction.pointerIsDown || | ||
interaction.interacting()) { | ||
return; | ||
@@ -27,8 +26,8 @@ } | ||
}); | ||
interactions.signals.on('move', function (arg) { | ||
interactions.signals.on('move', (arg) => { | ||
const { interaction, event } = arg; | ||
if (!interaction.pointerIsDown | ||
|| interaction.interacting() | ||
|| !interaction.pointerWasMoved | ||
|| !interaction.prepared.name) { | ||
if (!interaction.pointerIsDown || | ||
interaction.interacting() || | ||
!interaction.pointerWasMoved || | ||
!interaction.prepared.name) { | ||
return; | ||
@@ -40,4 +39,4 @@ } | ||
// check manualStart and interaction limit | ||
if (target.options[interaction.prepared.name].manualStart | ||
|| !withinInteractionLimit(target, interaction.element, interaction.prepared, scope)) { | ||
if (target.options[interaction.prepared.name].manualStart || | ||
!withinInteractionLimit(target, interaction.element, interaction.prepared, scope)) { | ||
interaction.stop(event); | ||
@@ -50,3 +49,3 @@ } | ||
}); | ||
interactions.signals.on('stop', function ({ interaction }) { | ||
interactions.signals.on('stop', ({ interaction }) => { | ||
const target = interaction.target; | ||
@@ -80,3 +79,3 @@ if (target && target.options.styleCursor) { | ||
*/ | ||
interact /* FIXME */.maxInteractions = newValue => maxInteractions(newValue, scope); | ||
interact /* FIXME */.maxInteractions = (newValue) => maxInteractions(newValue, scope); | ||
scope.autoStart = { | ||
@@ -93,6 +92,6 @@ // Allow this many interactions to happen simultaneously | ||
function validateAction(action, interactable, element, eventTarget, scope) { | ||
if (utils.is.object(action) | ||
&& interactable.testIgnoreAllow(interactable.options[action.name], element, eventTarget) | ||
&& interactable.options[action.name].enabled | ||
&& withinInteractionLimit(interactable, element, action, scope)) { | ||
if (utils.is.object(action) && | ||
interactable.testIgnoreAllow(interactable.options[action.name], element, eventTarget) && | ||
interactable.options[action.name].enabled && | ||
withinInteractionLimit(interactable, element, action, scope)) { | ||
return action; | ||
@@ -130,4 +129,4 @@ } | ||
const actionInfo = validateSelector(interaction, pointer, event, matches, matchElements, eventTarget, scope); | ||
if (actionInfo.action | ||
&& !actionInfo.target.options[actionInfo.action.name].manualStart) { | ||
if (actionInfo.action && | ||
!actionInfo.target.options[actionInfo.action.name].manualStart) { | ||
return actionInfo; | ||
@@ -151,3 +150,3 @@ } | ||
} | ||
scope.autoStart.signals.fire('prepared', { interaction: interaction }); | ||
scope.autoStart.signals.fire('prepared', { interaction }); | ||
} | ||
@@ -154,0 +153,0 @@ function withinInteractionLimit(interactable, element, action, scope) { |
220
base.ts
@@ -1,8 +0,8 @@ | ||
import * as utils from '@interactjs/utils'; | ||
import InteractableMethods from './InteractableMethods'; | ||
type Scope = import('@interactjs/core/scope').Scope; | ||
import * as utils from '@interactjs/utils' | ||
import InteractableMethods from './InteractableMethods' | ||
type Scope = import ('@interactjs/core/scope').Scope | ||
declare module '@interactjs/interact/interact' { | ||
interface InteractStatic { | ||
maxInteractions: (newValue: any) => any; | ||
maxInteractions: (newValue: any) => any | ||
} | ||
@@ -20,11 +20,11 @@ } | ||
interface PerActionDefaults { | ||
manualStart?: false, | ||
max?: number, | ||
maxPerElement?: number, | ||
allowFrom?: string | Element, | ||
ignoreFrom?: string | Element, | ||
manualStart?: boolean | ||
max?: number | ||
maxPerElement?: number | ||
allowFrom?: string | Element | ||
ignoreFrom?: string | Element | ||
// only allow left button by default | ||
// see https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons#Return_value | ||
mouseButtons?: 0 | 1 | 2 | 4 | 16, | ||
mouseButtons?: 0 | 1 | 2 | 4 | 16 | ||
} | ||
@@ -35,7 +35,7 @@ } | ||
// Allow this many interactions to happen simultaneously | ||
maxInteractions: number, | ||
withinInteractionLimit: typeof withinInteractionLimit, | ||
cursorElement: Element, | ||
signals: utils.Signals, | ||
}; | ||
maxInteractions: number | ||
withinInteractionLimit: typeof withinInteractionLimit | ||
cursorElement: Element | ||
signals: utils.Signals | ||
} | ||
@@ -47,60 +47,60 @@ function install (scope: Scope) { | ||
defaults, | ||
} = scope; | ||
} = scope | ||
interact.use(InteractableMethods); | ||
interact.use(InteractableMethods) | ||
// set cursor style on mousedown | ||
interactions.signals.on('down', function ({ interaction, pointer, event, eventTarget }) { | ||
if (interaction.interacting()) { return; } | ||
interactions.signals.on('down', ({ interaction, pointer, event, eventTarget }) => { | ||
if (interaction.interacting()) { return } | ||
const actionInfo = getActionInfo(interaction, pointer, event, eventTarget, scope); | ||
prepare(interaction, actionInfo, scope); | ||
}); | ||
const actionInfo = getActionInfo(interaction, pointer, event, eventTarget, scope) | ||
prepare(interaction, actionInfo, scope) | ||
}) | ||
// set cursor style on mousemove | ||
interactions.signals.on('move', function ({ interaction, pointer, event, eventTarget }) { | ||
if (interaction.pointerType !== 'mouse' | ||
|| interaction.pointerIsDown | ||
|| interaction.interacting()) { return; } | ||
interactions.signals.on('move', ({ interaction, pointer, event, eventTarget }) => { | ||
if (interaction.pointerType !== 'mouse' || | ||
interaction.pointerIsDown || | ||
interaction.interacting()) { return } | ||
const actionInfo = getActionInfo(interaction, pointer, event, eventTarget, scope); | ||
prepare(interaction, actionInfo, scope); | ||
}); | ||
const actionInfo = getActionInfo(interaction, pointer, event, eventTarget, scope) | ||
prepare(interaction, actionInfo, scope) | ||
}) | ||
interactions.signals.on('move', function (arg) { | ||
const { interaction, event } = arg; | ||
interactions.signals.on('move', (arg) => { | ||
const { interaction, event } = arg | ||
if (!interaction.pointerIsDown | ||
|| interaction.interacting() | ||
|| !interaction.pointerWasMoved | ||
|| !interaction.prepared.name) { | ||
return; | ||
if (!interaction.pointerIsDown || | ||
interaction.interacting() || | ||
!interaction.pointerWasMoved || | ||
!interaction.prepared.name) { | ||
return | ||
} | ||
scope.autoStart.signals.fire('before-start', arg); | ||
scope.autoStart.signals.fire('before-start', arg) | ||
const target = interaction.target; | ||
const target = interaction.target | ||
if (interaction.prepared.name && target) { | ||
// check manualStart and interaction limit | ||
if (target.options[interaction.prepared.name].manualStart | ||
|| !withinInteractionLimit(target, interaction.element, interaction.prepared, scope)) { | ||
interaction.stop(event); | ||
if (target.options[interaction.prepared.name].manualStart || | ||
!withinInteractionLimit(target, interaction.element, interaction.prepared, scope)) { | ||
interaction.stop(event) | ||
} | ||
else { | ||
interaction.start(interaction.prepared, target, interaction.element); | ||
interaction.start(interaction.prepared, target, interaction.element) | ||
} | ||
} | ||
}); | ||
}) | ||
interactions.signals.on('stop', function ({ interaction }) { | ||
const target = interaction.target; | ||
interactions.signals.on('stop', ({ interaction }) => { | ||
const target = interaction.target | ||
if (target && target.options.styleCursor) { | ||
setCursor(interaction.element, '', scope); | ||
setCursor(interaction.element, '', scope) | ||
} | ||
}); | ||
}) | ||
defaults.base.actionChecker = null; | ||
defaults.base.styleCursor = true; | ||
defaults.base.actionChecker = null | ||
defaults.base.styleCursor = true | ||
@@ -130,3 +130,3 @@ utils.extend(defaults.perAction, { | ||
*/ | ||
(interact as any /* FIXME */).maxInteractions = newValue => maxInteractions(newValue, scope); | ||
(interact as any /* FIXME */).maxInteractions = (newValue) => maxInteractions(newValue, scope) | ||
@@ -139,3 +139,3 @@ scope.autoStart = { | ||
signals: new utils.Signals(), | ||
}; | ||
} | ||
} | ||
@@ -146,10 +146,10 @@ | ||
function validateAction (action, interactable, element, eventTarget, scope) { | ||
if (utils.is.object(action) | ||
&& interactable.testIgnoreAllow(interactable.options[action.name], element, eventTarget) | ||
&& interactable.options[action.name].enabled | ||
&& withinInteractionLimit(interactable, element, action, scope)) { | ||
return action; | ||
if (utils.is.object(action) && | ||
interactable.testIgnoreAllow(interactable.options[action.name], element, eventTarget) && | ||
interactable.options[action.name].enabled && | ||
withinInteractionLimit(interactable, element, action, scope)) { | ||
return action | ||
} | ||
return null; | ||
return null | ||
} | ||
@@ -159,4 +159,4 @@ | ||
for (let i = 0, len = matches.length; i < len; i++) { | ||
const match = matches[i]; | ||
const matchElement = matchElements[i]; | ||
const match = matches[i] | ||
const matchElement = matchElements[i] | ||
const action = validateAction( | ||
@@ -167,3 +167,3 @@ match.getAction(pointer, event, interaction, matchElement), | ||
eventTarget, | ||
scope); | ||
scope) | ||
@@ -175,94 +175,94 @@ if (action) { | ||
element: matchElement, | ||
}; | ||
} | ||
} | ||
} | ||
return { action: null, target: null, element: null }; | ||
return { action: null, target: null, element: null } | ||
} | ||
function getActionInfo (interaction, pointer, event, eventTarget, scope) { | ||
let matches = []; | ||
let matchElements = []; | ||
let matches = [] | ||
let matchElements = [] | ||
let element = eventTarget; | ||
let element = eventTarget | ||
function pushMatches (interactable) { | ||
matches.push(interactable); | ||
matchElements.push(element); | ||
matches.push(interactable) | ||
matchElements.push(element) | ||
} | ||
while (utils.is.element(element)) { | ||
matches = []; | ||
matchElements = []; | ||
matches = [] | ||
matchElements = [] | ||
scope.interactables.forEachMatch(element, pushMatches); | ||
scope.interactables.forEachMatch(element, pushMatches) | ||
const actionInfo = validateSelector(interaction, pointer, event, matches, matchElements, eventTarget, scope); | ||
const actionInfo = validateSelector(interaction, pointer, event, matches, matchElements, eventTarget, scope) | ||
if (actionInfo.action | ||
&& !actionInfo.target.options[actionInfo.action.name].manualStart) { | ||
return actionInfo; | ||
if (actionInfo.action && | ||
!actionInfo.target.options[actionInfo.action.name].manualStart) { | ||
return actionInfo | ||
} | ||
element = utils.dom.parentNode(element); | ||
element = utils.dom.parentNode(element) | ||
} | ||
return { action: null, target: null, element: null }; | ||
return { action: null, target: null, element: null } | ||
} | ||
function prepare (interaction, { action, target, element }, scope) { | ||
action = action || {}; | ||
action = action || {} | ||
if (interaction.target && interaction.target.options.styleCursor) { | ||
setCursor(interaction.element, '', scope); | ||
setCursor(interaction.element, '', scope) | ||
} | ||
interaction.target = target; | ||
interaction.element = element; | ||
utils.copyAction(interaction.prepared, action); | ||
interaction.target = target | ||
interaction.element = element | ||
utils.copyAction(interaction.prepared, action) | ||
if (target && target.options.styleCursor) { | ||
const cursor = action? scope.actions[action.name].getCursor(action) : ''; | ||
setCursor(interaction.element, cursor, scope); | ||
const cursor = action ? scope.actions[action.name].getCursor(action) : '' | ||
setCursor(interaction.element, cursor, scope) | ||
} | ||
scope.autoStart.signals.fire('prepared', { interaction: interaction }); | ||
scope.autoStart.signals.fire('prepared', { interaction }) | ||
} | ||
function withinInteractionLimit (interactable, element, action, scope) { | ||
const options = interactable.options; | ||
const maxActions = options[action.name].max; | ||
const maxPerElement = options[action.name].maxPerElement; | ||
const autoStartMax = scope.autoStart.maxInteractions; | ||
let activeInteractions = 0; | ||
let targetCount = 0; | ||
let targetElementCount = 0; | ||
const options = interactable.options | ||
const maxActions = options[action.name].max | ||
const maxPerElement = options[action.name].maxPerElement | ||
const autoStartMax = scope.autoStart.maxInteractions | ||
let activeInteractions = 0 | ||
let targetCount = 0 | ||
let targetElementCount = 0 | ||
// no actions if any of these values == 0 | ||
if (!(maxActions && maxPerElement && autoStartMax)) { return false; } | ||
if (!(maxActions && maxPerElement && autoStartMax)) { return false } | ||
for (const interaction of scope.interactions.list) { | ||
const otherAction = interaction.prepared.name; | ||
const otherAction = interaction.prepared.name | ||
if (!interaction.interacting()) { continue; } | ||
if (!interaction.interacting()) { continue } | ||
activeInteractions++; | ||
activeInteractions++ | ||
if (activeInteractions >= autoStartMax) { | ||
return false; | ||
return false | ||
} | ||
if (interaction.target !== interactable) { continue; } | ||
if (interaction.target !== interactable) { continue } | ||
targetCount += otherAction === action.name ? 1 : 0; | ||
targetCount += otherAction === action.name ? 1 : 0 | ||
if (targetCount >= maxActions) { | ||
return false; | ||
return false | ||
} | ||
if (interaction.element === element) { | ||
targetElementCount++; | ||
targetElementCount++ | ||
if (otherAction === action.name && targetElementCount >= maxPerElement) { | ||
return false; | ||
return false | ||
} | ||
@@ -272,3 +272,3 @@ } | ||
return autoStartMax > 0; | ||
return autoStartMax > 0 | ||
} | ||
@@ -278,8 +278,8 @@ | ||
if (utils.is.number(newValue)) { | ||
scope.autoStart.maxInteractions = newValue; | ||
scope.autoStart.maxInteractions = newValue | ||
return this; | ||
return this | ||
} | ||
return scope.autoStart.maxInteractions; | ||
return scope.autoStart.maxInteractions | ||
} | ||
@@ -289,8 +289,8 @@ | ||
if (scope.autoStart.cursorElement) { | ||
scope.autoStart.cursorElement.style.cursor = ''; | ||
scope.autoStart.cursorElement.style.cursor = '' | ||
} | ||
element.ownerDocument.documentElement.style.cursor = cursor; | ||
element.style.cursor = cursor; | ||
scope.autoStart.cursorElement = cursor ? element : null; | ||
element.ownerDocument.documentElement.style.cursor = cursor | ||
element.style.cursor = cursor | ||
scope.autoStart.cursorElement = cursor ? element : null | ||
} | ||
@@ -303,2 +303,2 @@ | ||
validateAction, | ||
}; | ||
} |
@@ -5,3 +5,3 @@ import { parentNode } from '@interactjs/utils/domUtils'; | ||
function install(scope) { | ||
scope.autoStart.signals.on('before-start', function ({ interaction, eventTarget, dx, dy }) { | ||
scope.autoStart.signals.on('before-start', ({ interaction, eventTarget, dx, dy }) => { | ||
if (interaction.prepared.name !== 'drag') { | ||
@@ -30,9 +30,9 @@ return; | ||
const options = interaction.target.options.drag; | ||
if (!options.manualStart | ||
&& interactable.testIgnoreAllow(options, element, eventTarget)) { | ||
if (!options.manualStart && | ||
interactable.testIgnoreAllow(options, element, eventTarget)) { | ||
const action = interactable.getAction(interaction.downPointer, interaction.downEvent, interaction, element); | ||
if (action | ||
&& action.name === 'drag' | ||
&& checkStartAxis(currentAxis, interactable) | ||
&& autoStart.validateAction(action, interactable, element, eventTarget, scope)) { | ||
if (action && | ||
action.name === 'drag' && | ||
checkStartAxis(currentAxis, interactable) && | ||
autoStart.validateAction(action, interactable, element, eventTarget, scope)) { | ||
return interactable; | ||
@@ -39,0 +39,0 @@ } |
@@ -1,21 +0,21 @@ | ||
import { parentNode } from '@interactjs/utils/domUtils'; | ||
import * as is from '@interactjs/utils/is'; | ||
import autoStart from './base'; | ||
import { parentNode } from '@interactjs/utils/domUtils' | ||
import * as is from '@interactjs/utils/is' | ||
import autoStart from './base' | ||
type Scope = import('@interactjs/core/scope').Scope; | ||
type Scope = import ('@interactjs/core/scope').Scope | ||
function install (scope: Scope) { | ||
scope.autoStart.signals.on('before-start', function ({ interaction, eventTarget, dx, dy }) { | ||
if (interaction.prepared.name !== 'drag') { return; } | ||
scope.autoStart.signals.on('before-start', ({ interaction, eventTarget, dx, dy }) => { | ||
if (interaction.prepared.name !== 'drag') { return } | ||
// check if a drag is in the correct axis | ||
const absX = Math.abs(dx); | ||
const absY = Math.abs(dy); | ||
const targetOptions = interaction.target.options.drag; | ||
const startAxis = targetOptions.startAxis; | ||
const currentAxis = (absX > absY ? 'x' : absX < absY ? 'y' : 'xy'); | ||
const absX = Math.abs(dx) | ||
const absY = Math.abs(dy) | ||
const targetOptions = interaction.target.options.drag | ||
const startAxis = targetOptions.startAxis | ||
const currentAxis = (absX > absY ? 'x' : absX < absY ? 'y' : 'xy') | ||
interaction.prepared.axis = targetOptions.lockAxis === 'start' | ||
? currentAxis[0] // always lock to one axis even if currentAxis === 'xy' | ||
: targetOptions.lockAxis; | ||
: targetOptions.lockAxis | ||
@@ -25,53 +25,51 @@ // if the movement isn't in the startAxis of the interactable | ||
// cancel the prepared action | ||
interaction.prepared.name = null; | ||
interaction.prepared.name = null | ||
// then try to get a drag from another ineractable | ||
let element = eventTarget; | ||
let element = eventTarget | ||
const getDraggable = function (interactable) { | ||
if (interactable === interaction.target) { return; } | ||
if (interactable === interaction.target) { return } | ||
const options = interaction.target.options.drag; | ||
const options = interaction.target.options.drag | ||
if (!options.manualStart | ||
&& interactable.testIgnoreAllow(options, element, eventTarget)) { | ||
if (!options.manualStart && | ||
interactable.testIgnoreAllow(options, element, eventTarget)) { | ||
const action = interactable.getAction( | ||
interaction.downPointer, interaction.downEvent, interaction, element); | ||
interaction.downPointer, interaction.downEvent, interaction, element) | ||
if (action | ||
&& action.name === 'drag' | ||
&& checkStartAxis(currentAxis, interactable) | ||
&& autoStart.validateAction(action, interactable, element, eventTarget, scope)) { | ||
return interactable; | ||
if (action && | ||
action.name === 'drag' && | ||
checkStartAxis(currentAxis, interactable) && | ||
autoStart.validateAction(action, interactable, element, eventTarget, scope)) { | ||
return interactable | ||
} | ||
} | ||
}; | ||
} | ||
// check all interactables | ||
while (is.element(element)) { | ||
const interactable = scope.interactables.forEachMatch(element, getDraggable); | ||
const interactable = scope.interactables.forEachMatch(element, getDraggable) | ||
if (interactable) { | ||
interaction.prepared.name = 'drag'; | ||
interaction.target = interactable; | ||
interaction.element = element; | ||
break; | ||
interaction.prepared.name = 'drag' | ||
interaction.target = interactable | ||
interaction.element = element | ||
break | ||
} | ||
element = parentNode(element); | ||
element = parentNode(element) | ||
} | ||
} | ||
}); | ||
}) | ||
function checkStartAxis (startAxis, interactable) { | ||
if (!interactable) { return false; } | ||
if (!interactable) { return false } | ||
const thisAxis = interactable.options.drag.startAxis; | ||
const thisAxis = interactable.options.drag.startAxis | ||
return (startAxis === 'xy' || thisAxis === 'xy' || thisAxis === startAxis); | ||
return (startAxis === 'xy' || thisAxis === 'xy' || thisAxis === startAxis) | ||
} | ||
} | ||
export default { install }; | ||
export default { install } |
@@ -5,6 +5,6 @@ function install(scope) { | ||
defaults.perAction.delay = 0; | ||
interactions.signals.on('new', function (interaction) { | ||
interactions.signals.on('new', (interaction) => { | ||
interaction.autoStartHoldTimer = null; | ||
}); | ||
autoStart.signals.on('prepared', function ({ interaction }) { | ||
autoStart.signals.on('prepared', ({ interaction }) => { | ||
const hold = getHoldDuration(interaction); | ||
@@ -17,3 +17,3 @@ if (hold > 0) { | ||
}); | ||
interactions.signals.on('move', function ({ interaction, duplicate }) { | ||
interactions.signals.on('move', ({ interaction, duplicate }) => { | ||
if (interaction.pointerWasMoved && !duplicate) { | ||
@@ -24,3 +24,3 @@ clearTimeout(interaction.autoStartHoldTimer); | ||
// prevent regular down->move autoStart | ||
autoStart.signals.on('before-start', function ({ interaction }) { | ||
autoStart.signals.on('before-start', ({ interaction }) => { | ||
const hold = getHoldDuration(interaction); | ||
@@ -27,0 +27,0 @@ if (hold > 0) { |
48
hold.ts
@@ -1,2 +0,2 @@ | ||
type Scope = import('@interactjs/core/scope').Scope; | ||
type Scope = import ('@interactjs/core/scope').Scope | ||
@@ -8,45 +8,45 @@ function install (scope: Scope) { | ||
defaults, | ||
} = scope; | ||
} = scope | ||
defaults.perAction.hold = 0; | ||
defaults.perAction.delay = 0; | ||
defaults.perAction.hold = 0 | ||
defaults.perAction.delay = 0 | ||
interactions.signals.on('new', function (interaction) { | ||
interaction.autoStartHoldTimer = null; | ||
}); | ||
interactions.signals.on('new', (interaction) => { | ||
interaction.autoStartHoldTimer = null | ||
}) | ||
autoStart.signals.on('prepared', function ({ interaction }) { | ||
const hold = getHoldDuration(interaction); | ||
autoStart.signals.on('prepared', ({ interaction }) => { | ||
const hold = getHoldDuration(interaction) | ||
if (hold > 0) { | ||
interaction.autoStartHoldTimer = setTimeout(() => { | ||
interaction.start(interaction.prepared, interaction.target, interaction.element); | ||
}, hold); | ||
interaction.start(interaction.prepared, interaction.target, interaction.element) | ||
}, hold) | ||
} | ||
}); | ||
}) | ||
interactions.signals.on('move', function ({ interaction, duplicate }) { | ||
interactions.signals.on('move', ({ interaction, duplicate }) => { | ||
if (interaction.pointerWasMoved && !duplicate) { | ||
clearTimeout(interaction.autoStartHoldTimer); | ||
clearTimeout(interaction.autoStartHoldTimer) | ||
} | ||
}); | ||
}) | ||
// prevent regular down->move autoStart | ||
autoStart.signals.on('before-start', function ({ interaction }) { | ||
const hold = getHoldDuration(interaction); | ||
autoStart.signals.on('before-start', ({ interaction }) => { | ||
const hold = getHoldDuration(interaction) | ||
if (hold > 0) { | ||
interaction.prepared.name = null; | ||
interaction.prepared.name = null | ||
} | ||
}); | ||
}) | ||
} | ||
function getHoldDuration (interaction) { | ||
const actionName = interaction.prepared && interaction.prepared.name; | ||
const actionName = interaction.prepared && interaction.prepared.name | ||
if (!actionName) { return null; } | ||
if (!actionName) { return null } | ||
const options = interaction.target.options; | ||
const options = interaction.target.options | ||
return options[actionName].hold || options[actionName].delay; | ||
return options[actionName].hold || options[actionName].delay | ||
} | ||
@@ -57,2 +57,2 @@ | ||
getHoldDuration, | ||
}; | ||
} |
import autoStart from './base'; | ||
import dragAxis from './dragAxis'; | ||
import hold from './hold'; | ||
import dragAxis from './dragAxis'; | ||
declare function install(scope: any): void; | ||
export { autoStart, hold, dragAxis, install, }; |
import autoStart from './base'; | ||
import dragAxis from './dragAxis'; | ||
import hold from './hold'; | ||
import dragAxis from './dragAxis'; | ||
function install(scope) { | ||
@@ -5,0 +5,0 @@ autoStart.install(scope); |
14
index.ts
@@ -1,9 +0,9 @@ | ||
import autoStart from './base'; | ||
import hold from './hold'; | ||
import dragAxis from './dragAxis'; | ||
import autoStart from './base' | ||
import dragAxis from './dragAxis' | ||
import hold from './hold' | ||
function install (scope) { | ||
autoStart.install(scope); | ||
hold.install(scope); | ||
dragAxis.install(scope); | ||
autoStart.install(scope) | ||
hold.install(scope) | ||
dragAxis.install(scope) | ||
} | ||
@@ -16,2 +16,2 @@ | ||
install, | ||
}; | ||
} |
declare type Scope = import('@interactjs/core/scope').Scope; | ||
type Interaction = import('@interactjs/core/Interaction').default; | ||
type Interactable = import('@interactjs/core/Interactable').default; | ||
type IgnoreValue = string | Element | boolean; | ||
declare module '@interactjs/core/Interactable' { | ||
@@ -21,8 +24,11 @@ interface Interactable { | ||
declare function install(scope: Scope): void; | ||
declare function getAction(pointer: any, event: any, interaction: any, element: any): any; | ||
declare function styleCursor(newValue: any): any; | ||
declare function actionChecker(checker: any): any; | ||
declare function testIgnoreAllow(options: any, interactableElement: any, eventTarget: any): any; | ||
declare function testAllow(allowFrom: any, interactableElement: any, element: any): any; | ||
declare function testIgnore(ignoreFrom: any, interactableElement: any, element: any): any; | ||
declare function getAction(this: Interactable, pointer: Interact.PointerType, event: Interact.PointerEventType, interaction: Interaction, element: Element): any; | ||
declare function styleCursor(this: Interactable, newValue?: boolean): any; | ||
declare function actionChecker(this: Interactable, checker: any): any; | ||
declare function testIgnoreAllow(this: Interactable, options: { | ||
ignoreFrom: IgnoreValue; | ||
allowFrom: IgnoreValue; | ||
}, interactableElement: Element, eventTarget: Element): any; | ||
declare function testAllow(this: Interactable, allowFrom: IgnoreValue, interactableElement: Element, element: Element): any; | ||
declare function testIgnore(this: Interactable, ignoreFrom: IgnoreValue, interactableElement: Element, element: Element): any; | ||
declare const _default: { | ||
@@ -29,0 +35,0 @@ install: typeof install; |
@@ -7,3 +7,4 @@ import { warnOnce } from '@interactjs/utils'; | ||
/** @lends Interactable */ | ||
Interactable, actions, } = scope; | ||
Interactable, // tslint:disable-line no-shadowed-variable | ||
actions, } = scope; | ||
Interactable.prototype.getAction = getAction; | ||
@@ -132,5 +133,5 @@ /** | ||
// check mouseButton setting if the pointer is down | ||
if (interaction.pointerIsDown | ||
&& /mouse|pointer/.test(interaction.pointerType) | ||
&& (buttons & interactable.options[actionName].mouseButtons) === 0) { | ||
if (interaction.pointerIsDown && | ||
/mouse|pointer/.test(interaction.pointerType) && | ||
(buttons & interactable.options[actionName].mouseButtons) === 0) { | ||
continue; | ||
@@ -167,4 +168,4 @@ } | ||
function testIgnoreAllow(options, interactableElement, eventTarget) { | ||
return (!this.testIgnore(options.ignoreFrom, interactableElement, eventTarget) | ||
&& this.testAllow(options.allowFrom, interactableElement, eventTarget)); | ||
return (!this.testIgnore(options.ignoreFrom, interactableElement, eventTarget) && | ||
this.testAllow(options.allowFrom, interactableElement, eventTarget)); | ||
} | ||
@@ -171,0 +172,0 @@ function testAllow(allowFrom, interactableElement, element) { |
@@ -1,12 +0,14 @@ | ||
import { warnOnce } from '@interactjs/utils'; | ||
import * as domUtils from '@interactjs/utils/domUtils'; | ||
import * as is from '@interactjs/utils/is'; | ||
import { warnOnce } from '@interactjs/utils' | ||
import * as domUtils from '@interactjs/utils/domUtils' | ||
import * as is from '@interactjs/utils/is' | ||
// TODO: there seems to be a @babel/preset-typescript bug causing regular import | ||
// syntax to remain in js output | ||
type Scope = import('@interactjs/core/scope').Scope; | ||
type Actions = import('@interactjs/core/scope').Actions; | ||
type Interaction = import('@interactjs/core/Interaction').default; | ||
type Interactable = import('@interactjs/core/Interactable').default; | ||
type Scope = import ('@interactjs/core/scope').Scope | ||
type Actions = import ('@interactjs/core/scope').Actions | ||
type Interaction = import ('@interactjs/core/Interaction').default | ||
type Interactable = import ('@interactjs/core/Interactable').default | ||
type IgnoreValue = string | Element | boolean | ||
declare module '@interactjs/core/Interactable' { | ||
@@ -35,7 +37,7 @@ interface Interactable { | ||
/** @lends Interactable */ | ||
Interactable, | ||
Interactable, // tslint:disable-line no-shadowed-variable | ||
actions, | ||
} = scope; | ||
} = scope | ||
Interactable.prototype.getAction = getAction; | ||
Interactable.prototype.getAction = getAction | ||
@@ -70,5 +72,5 @@ /** | ||
*/ | ||
Interactable.prototype.ignoreFrom = warnOnce(function (newValue) { | ||
return this._backCompatOption('ignoreFrom', newValue); | ||
}, 'Interactable.ignoreFrom() has been deprecated. Use Interactble.draggable({ignoreFrom: newValue}).'); | ||
Interactable.prototype.ignoreFrom = warnOnce(function (this: Interactable, newValue) { | ||
return this._backCompatOption('ignoreFrom', newValue) | ||
}, 'Interactable.ignoreFrom() has been deprecated. Use Interactble.draggable({ignoreFrom: newValue}).') | ||
@@ -98,11 +100,11 @@ /** | ||
*/ | ||
Interactable.prototype.allowFrom = warnOnce(function (newValue) { | ||
return this._backCompatOption('allowFrom', newValue); | ||
}, 'Interactable.allowFrom() has been deprecated. Use Interactble.draggable({allowFrom: newValue}).'); | ||
Interactable.prototype.allowFrom = warnOnce(function (this: Interactable, newValue) { | ||
return this._backCompatOption('allowFrom', newValue) | ||
}, 'Interactable.allowFrom() has been deprecated. Use Interactble.draggable({allowFrom: newValue}).') | ||
Interactable.prototype.testIgnore = testIgnore; | ||
Interactable.prototype.testIgnore = testIgnore | ||
Interactable.prototype.testAllow = testAllow; | ||
Interactable.prototype.testAllow = testAllow | ||
Interactable.prototype.testIgnoreAllow = testIgnoreAllow; | ||
Interactable.prototype.testIgnoreAllow = testIgnoreAllow | ||
@@ -140,3 +142,3 @@ /** | ||
*/ | ||
Interactable.prototype.actionChecker = actionChecker; | ||
Interactable.prototype.actionChecker = actionChecker | ||
@@ -150,21 +152,21 @@ /** | ||
*/ | ||
Interactable.prototype.styleCursor = styleCursor; | ||
Interactable.prototype.styleCursor = styleCursor | ||
Interactable.prototype.defaultActionChecker = function (pointer, event, interaction, element) { | ||
return defaultActionChecker(this, pointer, event, interaction, element, actions); | ||
}; | ||
Interactable.prototype.defaultActionChecker = function (this: Interactable, pointer, event, interaction, element) { | ||
return defaultActionChecker(this, pointer, event, interaction, element, actions) | ||
} | ||
} | ||
function getAction (pointer, event, interaction, element) { | ||
const action = this.defaultActionChecker(pointer, event, interaction, element); | ||
function getAction (this: Interactable, pointer: Interact.PointerType, event: Interact.PointerEventType, interaction: Interaction, element: Element) { | ||
const action = this.defaultActionChecker(pointer, event, interaction, element) | ||
if (this.options.actionChecker) { | ||
return this.options.actionChecker(pointer, event, action, this, element, interaction); | ||
return this.options.actionChecker(pointer, event, action, this, element, interaction) | ||
} | ||
return action; | ||
return action | ||
} | ||
function defaultActionChecker (interactable: Interactable, pointer: Interact.PointerType, event: Interact.PointerEventType, interaction: Interaction, element: Element, actions: Actions) { | ||
const rect = interactable.getRect(element); | ||
const rect = interactable.getRect(element) | ||
const buttons = (event as MouseEvent).buttons || ({ | ||
@@ -175,17 +177,17 @@ 0: 1, | ||
4: 16, | ||
})[(event as MouseEvent).button]; | ||
let action = null; | ||
})[(event as MouseEvent).button as 0 | 1 | 3 | 4] | ||
let action = null | ||
for (const actionName of actions.names) { | ||
// check mouseButton setting if the pointer is down | ||
if (interaction.pointerIsDown | ||
&& /mouse|pointer/.test(interaction.pointerType) | ||
&& (buttons & interactable.options[actionName].mouseButtons) === 0) { | ||
continue; | ||
if (interaction.pointerIsDown && | ||
/mouse|pointer/.test(interaction.pointerType) && | ||
(buttons & interactable.options[actionName].mouseButtons) === 0) { | ||
continue | ||
} | ||
action = actions[actionName].checker(pointer, event, interactable, element, interaction, rect); | ||
action = (actions[actionName as keyof Actions] as any).checker(pointer, event, interactable, element, interaction, rect) | ||
if (action) { | ||
return action; | ||
return action | ||
} | ||
@@ -195,67 +197,67 @@ } | ||
function styleCursor (newValue) { | ||
function styleCursor (this: Interactable, newValue?: boolean) { | ||
if (is.bool(newValue)) { | ||
this.options.styleCursor = newValue; | ||
this.options.styleCursor = newValue | ||
return this; | ||
return this | ||
} | ||
if (newValue === null) { | ||
delete this.options.styleCursor; | ||
delete this.options.styleCursor | ||
return this; | ||
return this | ||
} | ||
return this.options.styleCursor; | ||
return this.options.styleCursor | ||
} | ||
function actionChecker (checker) { | ||
function actionChecker (this: Interactable, checker: any) { | ||
if (is.func(checker)) { | ||
this.options.actionChecker = checker; | ||
this.options.actionChecker = checker | ||
return this; | ||
return this | ||
} | ||
if (checker === null) { | ||
delete this.options.actionChecker; | ||
delete this.options.actionChecker | ||
return this; | ||
return this | ||
} | ||
return this.options.actionChecker; | ||
return this.options.actionChecker | ||
} | ||
function testIgnoreAllow (options, interactableElement, eventTarget) { | ||
return (!this.testIgnore(options.ignoreFrom, interactableElement, eventTarget) | ||
&& this.testAllow(options.allowFrom, interactableElement, eventTarget)); | ||
function testIgnoreAllow (this: Interactable, options: { ignoreFrom: IgnoreValue, allowFrom: IgnoreValue }, interactableElement: Element, eventTarget: Element) { | ||
return (!this.testIgnore(options.ignoreFrom, interactableElement, eventTarget) && | ||
this.testAllow(options.allowFrom, interactableElement, eventTarget)) | ||
} | ||
function testAllow (allowFrom, interactableElement, element) { | ||
if (!allowFrom) { return true; } | ||
function testAllow (this: Interactable, allowFrom: IgnoreValue, interactableElement: Element, element: Element) { | ||
if (!allowFrom) { return true } | ||
if (!is.element(element)) { return false; } | ||
if (!is.element(element)) { return false } | ||
if (is.string(allowFrom)) { | ||
return domUtils.matchesUpTo(element, allowFrom, interactableElement); | ||
return domUtils.matchesUpTo(element, allowFrom, interactableElement) | ||
} | ||
else if (is.element(allowFrom)) { | ||
return domUtils.nodeContains(allowFrom, element); | ||
return domUtils.nodeContains(allowFrom, element) | ||
} | ||
return false; | ||
return false | ||
} | ||
function testIgnore (ignoreFrom, interactableElement, element) { | ||
if (!ignoreFrom || !is.element(element)) { return false; } | ||
function testIgnore (this: Interactable, ignoreFrom: IgnoreValue, interactableElement: Element, element: Element) { | ||
if (!ignoreFrom || !is.element(element)) { return false } | ||
if (is.string(ignoreFrom)) { | ||
return domUtils.matchesUpTo(element, ignoreFrom, interactableElement); | ||
return domUtils.matchesUpTo(element, ignoreFrom, interactableElement) | ||
} | ||
else if (is.element(ignoreFrom)) { | ||
return domUtils.nodeContains(ignoreFrom, element); | ||
return domUtils.nodeContains(ignoreFrom, element) | ||
} | ||
return false; | ||
return false | ||
} | ||
export default { install }; | ||
export default { install } |
{ | ||
"name": "@interactjs/auto-start", | ||
"version": "1.4.0-alpha.20+sha.ce0da21", | ||
"version": "1.4.0-alpha.21+sha.cdc1d5f", | ||
"peerDependencies": { | ||
"@interactjs/core": "1.4.0-alpha.20+sha.ce0da21", | ||
"@interactjs/utils": "1.4.0-alpha.20+sha.ce0da21" | ||
"@interactjs/core": "1.4.0-alpha.21+sha.cdc1d5f", | ||
"@interactjs/utils": "1.4.0-alpha.21+sha.cdc1d5f" | ||
}, | ||
"devDependencies": { | ||
"@interactjs/_dev": "1.4.0-alpha.20+sha.ce0da21", | ||
"@interactjs/core": "1.4.0-alpha.20+sha.ce0da21", | ||
"@interactjs/utils": "1.4.0-alpha.20+sha.ce0da21" | ||
"@interactjs/_dev": "1.4.0-alpha.21+sha.cdc1d5f", | ||
"@interactjs/core": "1.4.0-alpha.21+sha.cdc1d5f", | ||
"@interactjs/utils": "1.4.0-alpha.21+sha.cdc1d5f" | ||
}, | ||
@@ -13,0 +13,0 @@ "publishConfig": { |
@@ -1,5 +0,5 @@ | ||
import test from '@interactjs/_dev/test/test'; | ||
import * as helpers from '@interactjs/core/tests/helpers'; | ||
import Signals from '@interactjs/utils/Signals'; | ||
import hold from '../hold'; | ||
import test from '@interactjs/_dev/test/test' | ||
import * as helpers from '@interactjs/core/tests/helpers' | ||
import Signals from '@interactjs/utils/Signals' | ||
import hold from '../hold' | ||
@@ -14,15 +14,15 @@ test('autoStart/hold', t => { | ||
}, | ||
}); | ||
const autoStartHold = hold; | ||
autoStartHold.install(scope); | ||
}) | ||
const autoStartHold = hold | ||
autoStartHold.install(scope) | ||
t.equal(scope.defaults.perAction.hold, 0, 'sets scope.defaults.perAction.hold'); | ||
t.equal(scope.defaults.perAction.delay, 0, 'backwards compatible "delay" alias.'); | ||
t.equal(scope.defaults.perAction.hold, 0, 'sets scope.defaults.perAction.hold') | ||
t.equal(scope.defaults.perAction.delay, 0, 'backwards compatible "delay" alias.') | ||
const holdDuration = 1000; | ||
const actionName = 'TEST_ACTION'; | ||
const holdDuration = 1000 | ||
const actionName = 'TEST_ACTION' | ||
const interaction = { | ||
target: { options: { [actionName]: { hold: holdDuration } } }, | ||
prepared: { name: actionName }, | ||
}; | ||
} | ||
@@ -32,8 +32,8 @@ t.equal( | ||
holdDuration, | ||
'gets holdDuration'); | ||
'gets holdDuration') | ||
const delayDuration = 500; | ||
const delayDuration = 500 | ||
interaction.target.options[actionName].delay = delayDuration; | ||
delete interaction.target.options[actionName].hold; | ||
interaction.target.options[actionName].delay = delayDuration | ||
delete interaction.target.options[actionName].hold | ||
@@ -43,5 +43,5 @@ t.equal( | ||
delayDuration, | ||
'gets holdDuration from "delay" value'); | ||
'gets holdDuration from "delay" value') | ||
t.end(); | ||
}); | ||
t.end() | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
61814
1201
23