@interactjs/utils
Advanced tools
Comparing version 1.4.0-alpha.20 to 1.4.0-alpha.21
18
arr.ts
export function contains (array, target) { | ||
return array.indexOf(target) !== -1; | ||
return array.indexOf(target) !== -1 | ||
} | ||
export function remove (array, target) { | ||
return array.splice(array.indexOf(target), 1); | ||
return array.splice(array.indexOf(target), 1) | ||
} | ||
@@ -11,10 +11,10 @@ | ||
for (const item of source) { | ||
target.push(item); | ||
target.push(item) | ||
} | ||
return target; | ||
return target | ||
} | ||
export function from (source) { | ||
return merge([], source); | ||
return merge([], source) | ||
} | ||
@@ -25,15 +25,15 @@ | ||
if (func(array[i], i, array)) { | ||
return i; | ||
return i | ||
} | ||
} | ||
return -1; | ||
return -1 | ||
} | ||
export function find (array, func) { | ||
return array[findIndex(array, func)]; | ||
return array[findIndex(array, func)] | ||
} | ||
export function some (array, func) { | ||
return findIndex(array, func) !== -1; | ||
return findIndex(array, func) !== -1 | ||
} |
@@ -0,4 +1,4 @@ | ||
import domObjects from './domObjects'; | ||
import * as is from './is'; | ||
import win from './window'; | ||
import * as is from './is'; | ||
import domObjects from './domObjects'; | ||
const browser = { | ||
@@ -20,4 +20,4 @@ init, | ||
// Does the browser support touch input? | ||
browser.supportsTouch = !!(('ontouchstart' in window) || is.func(window.DocumentTouch) | ||
&& domObjects.document instanceof window.DocumentTouch); | ||
browser.supportsTouch = !!(('ontouchstart' in window) || is.func(window.DocumentTouch)) && | ||
domObjects.document instanceof window.DocumentTouch; | ||
// Does the browser support PointerEvents | ||
@@ -27,9 +27,9 @@ browser.supportsPointerEvent = !!domObjects.PointerEvent; | ||
// scrolling doesn't change the result of getClientRects on iOS 7 | ||
browser.isIOS7 = (/iP(hone|od|ad)/.test(navigator.platform) | ||
&& /OS 7[^\d]/.test(navigator.appVersion)); | ||
browser.isIOS7 = (/iP(hone|od|ad)/.test(navigator.platform) && | ||
/OS 7[^\d]/.test(navigator.appVersion)); | ||
browser.isIe9 = /MSIE 9/.test(navigator.userAgent); | ||
// Opera Mobile must be handled differently | ||
browser.isOperaMobile = (navigator.appName === 'Opera' | ||
&& browser.supportsTouch | ||
&& navigator.userAgent.match('Presto')); | ||
browser.isOperaMobile = (navigator.appName === 'Opera' && | ||
browser.supportsTouch && | ||
navigator.userAgent.match('Presto')); | ||
// prefix matchesSelector | ||
@@ -36,0 +36,0 @@ browser.prefixedMatchesSelector = 'matches' in Element.prototype |
@@ -1,4 +0,4 @@ | ||
import win from './window'; | ||
import * as is from './is'; | ||
import domObjects from './domObjects'; | ||
import domObjects from './domObjects' | ||
import * as is from './is' | ||
import win from './window' | ||
@@ -23,27 +23,27 @@ const browser = { | ||
wheelEvent: null as string, | ||
}; | ||
} | ||
function init (window) { | ||
const Element = domObjects.Element as any; | ||
const navigator = win.window.navigator; | ||
const Element = domObjects.Element as any | ||
const navigator = win.window.navigator | ||
// Does the browser support touch input? | ||
browser.supportsTouch = !!(('ontouchstart' in window) || is.func(window.DocumentTouch) | ||
&& domObjects.document instanceof window.DocumentTouch); | ||
browser.supportsTouch = !!(('ontouchstart' in window) || is.func(window.DocumentTouch)) && | ||
domObjects.document instanceof window.DocumentTouch | ||
// Does the browser support PointerEvents | ||
browser.supportsPointerEvent = !!domObjects.PointerEvent; | ||
browser.supportsPointerEvent = !!domObjects.PointerEvent | ||
browser.isIOS = (/iP(hone|od|ad)/.test(navigator.platform)); | ||
browser.isIOS = (/iP(hone|od|ad)/.test(navigator.platform)) | ||
// scrolling doesn't change the result of getClientRects on iOS 7 | ||
browser.isIOS7 = (/iP(hone|od|ad)/.test(navigator.platform) | ||
&& /OS 7[^\d]/.test(navigator.appVersion)); | ||
browser.isIOS7 = (/iP(hone|od|ad)/.test(navigator.platform) && | ||
/OS 7[^\d]/.test(navigator.appVersion)) | ||
browser.isIe9 = /MSIE 9/.test(navigator.userAgent); | ||
browser.isIe9 = /MSIE 9/.test(navigator.userAgent) | ||
// Opera Mobile must be handled differently | ||
browser.isOperaMobile = (navigator.appName === 'Opera' | ||
&& browser.supportsTouch | ||
&& navigator.userAgent.match('Presto')); | ||
browser.isOperaMobile = (navigator.appName === 'Opera' && | ||
browser.supportsTouch && | ||
navigator.userAgent.match('Presto')) | ||
@@ -59,3 +59,3 @@ // prefix matchesSelector | ||
? 'oMatchesSelector' | ||
: 'msMatchesSelector'; | ||
: 'msMatchesSelector' | ||
@@ -80,8 +80,8 @@ browser.pEventTypes = (domObjects.PointerEvent | ||
}) | ||
: null); | ||
: null) | ||
// because Webkit and Opera still use 'mousewheel' event type | ||
browser.wheelEvent = 'onmousewheel' in domObjects.document? 'mousewheel': 'wheel'; | ||
browser.wheelEvent = 'onmousewheel' in domObjects.document ? 'mousewheel' : 'wheel' | ||
} | ||
export default browser; | ||
export default browser |
16
clone.ts
@@ -1,22 +0,22 @@ | ||
import * as arr from './arr'; | ||
import * as is from './is'; | ||
import * as arr from './arr' | ||
import * as is from './is' | ||
export default function clone<T extends { [key: string]: any }> (source: T): Partial<T> { | ||
const dest = {} as Partial<T>; | ||
const dest = {} as Partial<T> | ||
for (const prop in source) { | ||
const value = source[prop]; | ||
const value = source[prop] | ||
if (is.plainObject(value)) { | ||
dest[prop] = clone(value) as any; | ||
dest[prop] = clone(value) as any | ||
} | ||
else if (is.array(value)) { | ||
dest[prop] = arr.from(value); | ||
dest[prop] = arr.from(value) | ||
} | ||
else { | ||
dest[prop] = value; | ||
dest[prop] = value | ||
} | ||
} | ||
return dest; | ||
return dest | ||
} |
declare const domObjects: { | ||
init: typeof init; | ||
init: any; | ||
document: Document; | ||
DocumentFragment: { | ||
new (): DocumentFragment; | ||
prototype: DocumentFragment; | ||
}; | ||
SVGElement: { | ||
new (): SVGElement; | ||
prototype: SVGElement; | ||
}; | ||
SVGSVGElement: { | ||
new (): SVGSVGElement; | ||
prototype: SVGSVGElement; | ||
readonly SVG_ZOOMANDPAN_DISABLE: number; | ||
readonly SVG_ZOOMANDPAN_MAGNIFY: number; | ||
readonly SVG_ZOOMANDPAN_UNKNOWN: number; | ||
}; | ||
SVGElementInstance: { | ||
new (): SVGElementInstance; | ||
prototype: SVGElementInstance; | ||
}; | ||
Element: { | ||
new (): Element; | ||
prototype: Element; | ||
}; | ||
HTMLElement: { | ||
new (): HTMLElement; | ||
prototype: HTMLElement; | ||
}; | ||
Event: { | ||
new (type: string, eventInitDict?: EventInit): Event; | ||
prototype: Event; | ||
readonly AT_TARGET: number; | ||
readonly BUBBLING_PHASE: number; | ||
readonly CAPTURING_PHASE: number; | ||
readonly NONE: number; | ||
}; | ||
Touch: { | ||
new (touchInitDict: TouchInit): Touch; | ||
prototype: Touch; | ||
}; | ||
PointerEvent: { | ||
new (type: string, eventInitDict?: PointerEventInit): PointerEvent; | ||
prototype: PointerEvent; | ||
}; | ||
DocumentFragment: typeof DocumentFragment; | ||
SVGElement: typeof SVGElement; | ||
SVGSVGElement: typeof SVGSVGElement; | ||
SVGElementInstance: any; | ||
Element: typeof Element; | ||
HTMLElement: typeof HTMLElement; | ||
Event: typeof Event; | ||
Touch: typeof Touch; | ||
PointerEvent: typeof PointerEvent; | ||
}; | ||
export default domObjects; | ||
declare function init(window: Window): void; |
@@ -1,34 +0,47 @@ | ||
const domObjects = { | ||
const domObjects: { | ||
init: any, | ||
document: Document, | ||
DocumentFragment: typeof DocumentFragment, | ||
SVGElement: typeof SVGElement, | ||
SVGSVGElement: typeof SVGSVGElement, | ||
SVGElementInstance: any, | ||
Element: typeof Element, | ||
HTMLElement: typeof HTMLElement, | ||
Event: typeof Event, | ||
Touch: typeof Touch, | ||
PointerEvent: typeof PointerEvent, | ||
} = | ||
{ | ||
init, | ||
document: null as Document, | ||
DocumentFragment: null as typeof DocumentFragment, | ||
SVGElement: null as typeof SVGElement, | ||
SVGSVGElement: null as typeof SVGSVGElement, | ||
document: null as any, | ||
DocumentFragment: null as any, | ||
SVGElement: null as any, | ||
SVGSVGElement: null as any, | ||
// eslint-disable-next-line no-undef | ||
SVGElementInstance: null as typeof SVGElementInstance, | ||
Element: null as typeof Element, | ||
HTMLElement: null as typeof HTMLElement, | ||
Event: null as typeof Event, | ||
Touch: null as typeof Touch, | ||
PointerEvent: null as typeof PointerEvent, | ||
}; | ||
SVGElementInstance: null as any, | ||
Element: null as any, | ||
HTMLElement: null as any, | ||
Event: null as any, | ||
Touch: null as any, | ||
PointerEvent: null as any, | ||
} | ||
function blank () {} | ||
export default domObjects; | ||
export default domObjects | ||
function init (window: Window) { | ||
const win = window as any; | ||
const win = window as any | ||
domObjects.document = win.document; | ||
domObjects.DocumentFragment = win.DocumentFragment || blank; | ||
domObjects.SVGElement = win.SVGElement || blank; | ||
domObjects.SVGSVGElement = win.SVGSVGElement || blank; | ||
domObjects.SVGElementInstance = win.SVGElementInstance || blank; | ||
domObjects.Element = win.Element || blank; | ||
domObjects.HTMLElement = win.HTMLElement || domObjects.Element; | ||
domObjects.document = win.document | ||
domObjects.DocumentFragment = win.DocumentFragment || blank | ||
domObjects.SVGElement = win.SVGElement || blank | ||
domObjects.SVGSVGElement = win.SVGSVGElement || blank | ||
domObjects.SVGElementInstance = win.SVGElementInstance || blank | ||
domObjects.Element = win.Element || blank | ||
domObjects.HTMLElement = win.HTMLElement || domObjects.Element | ||
domObjects.Event = win.Event; | ||
domObjects.Touch = win.Touch || blank; | ||
domObjects.PointerEvent = (win.PointerEvent || win.MSPointerEvent); | ||
domObjects.Event = win.Event | ||
domObjects.Touch = win.Touch || blank | ||
domObjects.PointerEvent = (win.PointerEvent || win.MSPointerEvent) | ||
} |
@@ -1,5 +0,5 @@ | ||
import win from './window'; | ||
import browser from './browser'; | ||
import domObjects from './domObjects'; | ||
import * as is from './is'; | ||
import domObjects from './domObjects'; | ||
import win from './window'; | ||
export function nodeContains(parent, child) { | ||
@@ -27,2 +27,3 @@ while (child) { | ||
// skip past #shado-root fragments | ||
// tslint:disable-next-line | ||
while ((parent = parent.host) && is.docFrag(parent)) { | ||
@@ -84,5 +85,5 @@ continue; | ||
// an HTMLElement | ||
if (deepestZone instanceof domObjects.HTMLElement | ||
&& dropzone instanceof domObjects.SVGElement | ||
&& !(dropzone instanceof domObjects.SVGSVGElement)) { | ||
if (deepestZone instanceof domObjects.HTMLElement && | ||
dropzone instanceof domObjects.SVGElement && | ||
!(dropzone instanceof domObjects.SVGSVGElement)) { | ||
if (dropzone === deepestZone.parentNode) { | ||
@@ -89,0 +90,0 @@ continue; |
162
domUtils.ts
@@ -1,5 +0,5 @@ | ||
import win from './window'; | ||
import browser from './browser'; | ||
import * as is from './is'; | ||
import domObjects from './domObjects'; | ||
import browser from './browser' | ||
import domObjects from './domObjects' | ||
import * as is from './is' | ||
import win from './window' | ||
@@ -9,9 +9,9 @@ export function nodeContains (parent, child) { | ||
if (child === parent) { | ||
return true; | ||
return true | ||
} | ||
child = child.parentNode; | ||
child = child.parentNode | ||
} | ||
return false; | ||
return false | ||
} | ||
@@ -21,23 +21,24 @@ | ||
while (is.element(element)) { | ||
if (matchesSelector(element, selector)) { return element; } | ||
if (matchesSelector(element, selector)) { return element } | ||
element = parentNode(element); | ||
element = parentNode(element) | ||
} | ||
return null; | ||
return null | ||
} | ||
export function parentNode (node) { | ||
let parent = node.parentNode; | ||
let parent = node.parentNode | ||
if (is.docFrag(parent)) { | ||
// skip past #shado-root fragments | ||
// tslint:disable-next-line | ||
while ((parent = (parent as any).host) && is.docFrag(parent)) { | ||
continue; | ||
continue | ||
} | ||
return parent; | ||
return parent | ||
} | ||
return parent; | ||
return parent | ||
} | ||
@@ -48,6 +49,6 @@ | ||
if (win.window !== win.realWindow) { | ||
selector = selector.replace(/\/deep\//g, ' '); | ||
selector = selector.replace(/\/deep\//g, ' ') | ||
} | ||
return element[browser.prefixedMatchesSelector](selector); | ||
return element[browser.prefixedMatchesSelector](selector) | ||
} | ||
@@ -57,24 +58,24 @@ | ||
export function indexOfDeepestElement (elements) { | ||
let deepestZoneParents = []; | ||
let dropzoneParents = []; | ||
let dropzone; | ||
let deepestZone = elements[0]; | ||
let index = deepestZone? 0: -1; | ||
let parent; | ||
let child; | ||
let i; | ||
let n; | ||
let deepestZoneParents = [] | ||
let dropzoneParents = [] | ||
let dropzone | ||
let deepestZone = elements[0] | ||
let index = deepestZone ? 0 : -1 | ||
let parent | ||
let child | ||
let i | ||
let n | ||
for (i = 1; i < elements.length; i++) { | ||
dropzone = elements[i]; | ||
dropzone = elements[i] | ||
// an element might belong to multiple selector dropzones | ||
if (!dropzone || dropzone === deepestZone) { | ||
continue; | ||
continue | ||
} | ||
if (!deepestZone) { | ||
deepestZone = dropzone; | ||
index = i; | ||
continue; | ||
deepestZone = dropzone | ||
index = i | ||
continue | ||
} | ||
@@ -85,16 +86,16 @@ | ||
if (dropzone.parentNode === dropzone.ownerDocument) { | ||
continue; | ||
continue | ||
} | ||
// - if deepest is, update with the current dropzone and continue to next | ||
else if (deepestZone.parentNode === dropzone.ownerDocument) { | ||
deepestZone = dropzone; | ||
index = i; | ||
continue; | ||
deepestZone = dropzone | ||
index = i | ||
continue | ||
} | ||
if (!deepestZoneParents.length) { | ||
parent = deepestZone; | ||
parent = deepestZone | ||
while (parent.parentNode && parent.parentNode !== parent.ownerDocument) { | ||
deepestZoneParents.unshift(parent); | ||
parent = parent.parentNode; | ||
deepestZoneParents.unshift(parent) | ||
parent = parent.parentNode | ||
} | ||
@@ -105,28 +106,27 @@ } | ||
// an HTMLElement | ||
if (deepestZone instanceof domObjects.HTMLElement | ||
&& dropzone instanceof domObjects.SVGElement | ||
&& !(dropzone instanceof domObjects.SVGSVGElement)) { | ||
if (deepestZone instanceof domObjects.HTMLElement && | ||
dropzone instanceof domObjects.SVGElement && | ||
!(dropzone instanceof domObjects.SVGSVGElement)) { | ||
if (dropzone === deepestZone.parentNode) { | ||
continue; | ||
continue | ||
} | ||
parent = dropzone.ownerSVGElement; | ||
parent = dropzone.ownerSVGElement | ||
} | ||
else { | ||
parent = dropzone; | ||
parent = dropzone | ||
} | ||
dropzoneParents = []; | ||
dropzoneParents = [] | ||
while (parent.parentNode !== parent.ownerDocument) { | ||
dropzoneParents.unshift(parent); | ||
parent = parent.parentNode; | ||
dropzoneParents.unshift(parent) | ||
parent = parent.parentNode | ||
} | ||
n = 0; | ||
n = 0 | ||
// get (position of last common ancestor) + 1 | ||
while (dropzoneParents[n] && dropzoneParents[n] === deepestZoneParents[n]) { | ||
n++; | ||
n++ | ||
} | ||
@@ -138,23 +138,23 @@ | ||
deepestZoneParents[n], | ||
]; | ||
] | ||
child = parents[0].lastChild; | ||
child = parents[0].lastChild | ||
while (child) { | ||
if (child === parents[1]) { | ||
deepestZone = dropzone; | ||
index = i; | ||
deepestZoneParents = []; | ||
deepestZone = dropzone | ||
index = i | ||
deepestZoneParents = [] | ||
break; | ||
break | ||
} | ||
else if (child === parents[2]) { | ||
break; | ||
break | ||
} | ||
child = child.previousSibling; | ||
child = child.previousSibling | ||
} | ||
} | ||
return index; | ||
return index | ||
} | ||
@@ -165,13 +165,13 @@ | ||
if (matchesSelector(element, selector)) { | ||
return true; | ||
return true | ||
} | ||
element = parentNode(element); | ||
element = parentNode(element) | ||
if (element === limit) { | ||
return matchesSelector(element, selector); | ||
return matchesSelector(element, selector) | ||
} | ||
} | ||
return false; | ||
return false | ||
} | ||
@@ -182,11 +182,11 @@ | ||
? element.correspondingUseElement | ||
: element); | ||
: element) | ||
} | ||
export function getScrollXY (relevantWindow) { | ||
relevantWindow = relevantWindow || win.window; | ||
relevantWindow = relevantWindow || win.window | ||
return { | ||
x: relevantWindow.scrollX || relevantWindow.document.documentElement.scrollLeft, | ||
y: relevantWindow.scrollY || relevantWindow.document.documentElement.scrollTop, | ||
}; | ||
} | ||
} | ||
@@ -197,3 +197,3 @@ | ||
? element.getBoundingClientRect() | ||
: element.getClientRects()[0]); | ||
: element.getClientRects()[0]) | ||
@@ -207,37 +207,37 @@ return clientRect && { | ||
height: clientRect.height || clientRect.bottom - clientRect.top, | ||
}; | ||
} | ||
} | ||
export function getElementRect (element) { | ||
const clientRect = getElementClientRect(element); | ||
const clientRect = getElementClientRect(element) | ||
if (!browser.isIOS7 && clientRect) { | ||
const scroll = getScrollXY(win.getWindow(element)); | ||
const scroll = getScrollXY(win.getWindow(element)) | ||
clientRect.left += scroll.x; | ||
clientRect.right += scroll.x; | ||
clientRect.top += scroll.y; | ||
clientRect.bottom += scroll.y; | ||
clientRect.left += scroll.x | ||
clientRect.right += scroll.x | ||
clientRect.top += scroll.y | ||
clientRect.bottom += scroll.y | ||
} | ||
return clientRect; | ||
return clientRect | ||
} | ||
export function getPath (element) { | ||
const path = []; | ||
const path = [] | ||
while (element) { | ||
path.push(element); | ||
element = parentNode(element); | ||
path.push(element) | ||
element = parentNode(element) | ||
} | ||
return path; | ||
return path | ||
} | ||
export function trySelector (value) { | ||
if (!is.string(value)) { return false; } | ||
if (!is.string(value)) { return false } | ||
// an exception will be raised if it is invalid | ||
domObjects.document.querySelector(value); | ||
return true; | ||
domObjects.document.querySelector(value) | ||
return true | ||
} |
@@ -1,4 +0,5 @@ | ||
declare function add(element: EventTarget, type: string, listener: Function, optionalArg?: boolean | any): void; | ||
declare function remove(element: EventTarget, type: string, listener?: 'all' | Function, optionalArg?: boolean | any): void; | ||
declare function addDelegate(selector: string, context: EventTarget, type: string, listener: Function, optionalArg?: any): void; | ||
declare type Listener = (event: Event) => any; | ||
declare function add(element: EventTarget, type: string, listener: Listener, optionalArg?: boolean | any): void; | ||
declare function remove(element: EventTarget, type: string, listener?: 'all' | Listener, optionalArg?: boolean | any): void; | ||
declare function addDelegate(selector: string, context: EventTarget, type: string, listener: Listener, optionalArg?: any): void; | ||
declare function removeDelegate(selector: any, context: any, type: any, listener?: any, optionalArg?: any): void; | ||
@@ -14,10 +15,21 @@ declare function delegateListener(event: Event, optionalArg?: any): void; | ||
delegateUseCapture: typeof delegateUseCapture; | ||
delegatedEvents: {}; | ||
documents: any[]; | ||
delegatedEvents: { | ||
[type: string]: { | ||
selectors: string[]; | ||
contexts: EventTarget[]; | ||
listeners: [Listener, boolean, boolean][][]; | ||
}; | ||
}; | ||
documents: Document[]; | ||
supportsOptions: boolean; | ||
supportsPassive: boolean; | ||
_elements: EventTarget[]; | ||
_targets: any[]; | ||
_targets: { | ||
events: { | ||
[type: string]: Listener[]; | ||
}; | ||
typeCount: number; | ||
}[]; | ||
init(window: Window): void; | ||
}; | ||
export default events; |
@@ -8,9 +8,2 @@ import { contains } from './arr'; | ||
const targets = []; | ||
// { | ||
// type: { | ||
// selectors: ['selector', ...], | ||
// contexts : [document, ...], | ||
// listeners: [[listener, capture, passive], ...] | ||
// } | ||
// } | ||
const delegatedEvents = {}; | ||
@@ -85,5 +78,5 @@ const documents = []; | ||
delegatedEvents[type] = { | ||
selectors: [], | ||
contexts: [], | ||
listeners: [], | ||
selectors: [], | ||
}; | ||
@@ -99,4 +92,4 @@ // add delegate listener functions | ||
for (index = delegated.selectors.length - 1; index >= 0; index--) { | ||
if (delegated.selectors[index] === selector | ||
&& delegated.contexts[index] === context) { | ||
if (delegated.selectors[index] === selector && | ||
delegated.contexts[index] === context) { | ||
break; | ||
@@ -125,4 +118,4 @@ } | ||
// look for matching selector and context Node | ||
if (delegated.selectors[index] === selector | ||
&& delegated.contexts[index] === context) { | ||
if (delegated.selectors[index] === selector && | ||
delegated.contexts[index] === context) { | ||
const listeners = delegated.listeners[index]; | ||
@@ -178,9 +171,8 @@ // each item of the listeners array is an array: [function, capture, passive] | ||
const context = delegated.contexts[i]; | ||
if (domUtils.matchesSelector(element, selector) | ||
&& domUtils.nodeContains(context, eventTarget) | ||
&& domUtils.nodeContains(context, element)) { | ||
if (domUtils.matchesSelector(element, selector) && | ||
domUtils.nodeContains(context, eventTarget) && | ||
domUtils.nodeContains(context, element)) { | ||
const listeners = delegated.listeners[i]; | ||
fakeEvent.currentTarget = element; | ||
for (let j = 0; j < listeners.length; j++) { | ||
const [fn, capture, passive] = listeners[j]; | ||
for (const [fn, capture, passive] of listeners) { | ||
if (capture === !!options.capture && passive === options.passive) { | ||
@@ -187,0 +179,0 @@ fn(fakeEvent); |
217
events.ts
@@ -1,25 +0,28 @@ | ||
import { contains } from './arr'; | ||
import * as domUtils from './domUtils'; | ||
import * as is from './is'; | ||
import pExtend from './pointerExtend'; | ||
import pointerUtils from './pointerUtils'; | ||
import { contains } from './arr' | ||
import * as domUtils from './domUtils' | ||
import * as is from './is' | ||
import pExtend from './pointerExtend' | ||
import pointerUtils from './pointerUtils' | ||
type Listener = (event: Event) => any | ||
const elements: EventTarget[] = []; | ||
const targets = []; | ||
const elements: EventTarget[] = [] | ||
const targets: Array<{ | ||
events: { [type: string]: Listener[] }, | ||
typeCount: number, | ||
}> = [] | ||
// { | ||
// type: { | ||
// selectors: ['selector', ...], | ||
// contexts : [document, ...], | ||
// listeners: [[listener, capture, passive], ...] | ||
// } | ||
// } | ||
const delegatedEvents = {}; | ||
const documents = []; | ||
const delegatedEvents: { | ||
[type: string]: { | ||
selectors: string[], | ||
contexts: EventTarget[], | ||
listeners: Array<Array<[Listener, boolean, boolean]>>, | ||
}, | ||
} = {} | ||
const documents: Document[] = [] | ||
function add (element: EventTarget, type: string, listener: Function, optionalArg?: boolean | any) { | ||
const options = getOptions(optionalArg); | ||
let elementIndex = elements.indexOf(element); | ||
let target = targets[elementIndex]; | ||
function add (element: EventTarget, type: string, listener: Listener, optionalArg?: boolean | any) { | ||
const options = getOptions(optionalArg) | ||
let elementIndex = elements.indexOf(element) | ||
let target = targets[elementIndex] | ||
@@ -30,26 +33,26 @@ if (!target) { | ||
typeCount: 0, | ||
}; | ||
} | ||
elementIndex = elements.push(element) - 1; | ||
targets.push(target); | ||
elementIndex = elements.push(element) - 1 | ||
targets.push(target) | ||
} | ||
if (!target.events[type]) { | ||
target.events[type] = []; | ||
target.typeCount++; | ||
target.events[type] = [] | ||
target.typeCount++ | ||
} | ||
if (!contains(target.events[type], listener)) { | ||
element.addEventListener(type, listener as any, events.supportsOptions? options : !!options.capture); | ||
target.events[type].push(listener); | ||
element.addEventListener(type, listener as any, events.supportsOptions ? options : !!options.capture) | ||
target.events[type].push(listener) | ||
} | ||
} | ||
function remove (element: EventTarget, type: string, listener?: 'all' | Function, optionalArg?: boolean | any) { | ||
const options = getOptions(optionalArg); | ||
const elementIndex = elements.indexOf(element); | ||
const target = targets[elementIndex]; | ||
function remove (element: EventTarget, type: string, listener?: 'all' | Listener, optionalArg?: boolean | any) { | ||
const options = getOptions(optionalArg) | ||
const elementIndex = elements.indexOf(element) | ||
const target = targets[elementIndex] | ||
if (!target || !target.events) { | ||
return; | ||
return | ||
} | ||
@@ -60,16 +63,16 @@ | ||
if (target.events.hasOwnProperty(type)) { | ||
remove(element, type, 'all'); | ||
remove(element, type, 'all') | ||
} | ||
} | ||
return; | ||
return | ||
} | ||
if (target.events[type]) { | ||
const len = target.events[type].length; | ||
const len = target.events[type].length | ||
if (listener === 'all') { | ||
for (let i = 0; i < len; i++) { | ||
remove(element, type, target.events[type][i], options); | ||
remove(element, type, target.events[type][i], options) | ||
} | ||
return; | ||
return | ||
} | ||
@@ -79,6 +82,6 @@ else { | ||
if (target.events[type][i] === listener) { | ||
element.removeEventListener(type, listener as any, events.supportsOptions? options : !!options.capture); | ||
target.events[type].splice(i, 1); | ||
element.removeEventListener(type, listener as any, events.supportsOptions ? options : !!options.capture) | ||
target.events[type].splice(i, 1) | ||
break; | ||
break | ||
} | ||
@@ -89,4 +92,4 @@ } | ||
if (target.events[type] && target.events[type].length === 0) { | ||
target.events[type] = null; | ||
target.typeCount--; | ||
(target.events[type] as any) = null | ||
target.typeCount-- | ||
} | ||
@@ -96,30 +99,30 @@ } | ||
if (!target.typeCount) { | ||
targets.splice(elementIndex, 1); | ||
elements.splice(elementIndex, 1); | ||
targets.splice(elementIndex, 1) | ||
elements.splice(elementIndex, 1) | ||
} | ||
} | ||
function addDelegate (selector: string, context: EventTarget, type: string, listener: Function, optionalArg?: any) { | ||
const options = getOptions(optionalArg); | ||
function addDelegate (selector: string, context: EventTarget, type: string, listener: Listener, optionalArg?: any) { | ||
const options = getOptions(optionalArg) | ||
if (!delegatedEvents[type]) { | ||
delegatedEvents[type] = { | ||
selectors: [], | ||
contexts : [], | ||
listeners: [], | ||
}; | ||
selectors: [], | ||
} | ||
// add delegate listener functions | ||
for (const doc of documents) { | ||
add(doc, type, delegateListener); | ||
add(doc, type, delegateUseCapture, true); | ||
add(doc, type, delegateListener) | ||
add(doc, type, delegateUseCapture, true) | ||
} | ||
} | ||
const delegated = delegatedEvents[type]; | ||
let index; | ||
const delegated = delegatedEvents[type] | ||
let index | ||
for (index = delegated.selectors.length - 1; index >= 0; index--) { | ||
if (delegated.selectors[index] === selector | ||
&& delegated.contexts[index] === context) { | ||
break; | ||
if (delegated.selectors[index] === selector && | ||
delegated.contexts[index] === context) { | ||
break | ||
} | ||
@@ -129,20 +132,20 @@ } | ||
if (index === -1) { | ||
index = delegated.selectors.length; | ||
index = delegated.selectors.length | ||
delegated.selectors.push(selector); | ||
delegated.contexts .push(context); | ||
delegated.listeners.push([]); | ||
delegated.selectors.push(selector) | ||
delegated.contexts.push(context) | ||
delegated.listeners.push([]) | ||
} | ||
// keep listener and capture and passive flags | ||
delegated.listeners[index].push([listener, !!options.capture, options.passive]); | ||
delegated.listeners[index].push([listener, !!options.capture, options.passive]) | ||
} | ||
function removeDelegate (selector, context, type, listener?, optionalArg?: any) { | ||
const options = getOptions(optionalArg); | ||
const delegated = delegatedEvents[type]; | ||
let matchFound = false; | ||
let index; | ||
const options = getOptions(optionalArg) | ||
const delegated = delegatedEvents[type] | ||
let matchFound = false | ||
let index | ||
if (!delegated) { return; } | ||
if (!delegated) { return } | ||
@@ -152,10 +155,9 @@ // count from last index of delegated to 0 | ||
// look for matching selector and context Node | ||
if (delegated.selectors[index] === selector | ||
&& delegated.contexts[index] === context) { | ||
if (delegated.selectors[index] === selector && | ||
delegated.contexts[index] === context) { | ||
const listeners = delegated.listeners[index] | ||
const listeners = delegated.listeners[index]; | ||
// each item of the listeners array is an array: [function, capture, passive] | ||
for (let i = listeners.length - 1; i >= 0; i--) { | ||
const [fn, capture, passive] = listeners[i]; | ||
const [fn, capture, passive] = listeners[i] | ||
@@ -165,3 +167,3 @@ // check if the listener functions and capture and passive flags match | ||
// remove the listener from the array of listeners | ||
listeners.splice(i, 1); | ||
listeners.splice(i, 1) | ||
@@ -171,13 +173,13 @@ // if all listeners for this interactable have been removed | ||
if (!listeners.length) { | ||
delegated.selectors.splice(index, 1); | ||
delegated.contexts .splice(index, 1); | ||
delegated.listeners.splice(index, 1); | ||
delegated.selectors.splice(index, 1) | ||
delegated.contexts.splice(index, 1) | ||
delegated.listeners.splice(index, 1) | ||
// remove delegate function from context | ||
remove(context, type, delegateListener); | ||
remove(context, type, delegateUseCapture, true); | ||
remove(context, type, delegateListener) | ||
remove(context, type, delegateUseCapture, true) | ||
// remove the arrays if they are empty | ||
if (!delegated.selectors.length) { | ||
delegatedEvents[type] = null; | ||
delegatedEvents[type] = null | ||
} | ||
@@ -187,8 +189,8 @@ } | ||
// only remove one listener | ||
matchFound = true; | ||
break; | ||
matchFound = true | ||
break | ||
} | ||
} | ||
if (matchFound) { break; } | ||
if (matchFound) { break } | ||
} | ||
@@ -201,13 +203,13 @@ } | ||
function delegateListener (event: Event, optionalArg?: any) { | ||
const options = getOptions(optionalArg); | ||
const fakeEvent = {} as any; | ||
const delegated = delegatedEvents[event.type]; | ||
const [eventTarget] = (pointerUtils.getEventTargets(event)); | ||
let element = eventTarget; | ||
const options = getOptions(optionalArg) | ||
const fakeEvent = {} as any | ||
const delegated = delegatedEvents[event.type] | ||
const [eventTarget] = (pointerUtils.getEventTargets(event)) | ||
let element = eventTarget | ||
// duplicate the event so that currentTarget can be changed | ||
pExtend(fakeEvent, event); | ||
pExtend(fakeEvent, event) | ||
fakeEvent.originalEvent = event; | ||
fakeEvent.preventDefault = preventOriginalDefault; | ||
fakeEvent.originalEvent = event | ||
fakeEvent.preventDefault = preventOriginalDefault | ||
@@ -217,18 +219,15 @@ // climb up document tree looking for selector matches | ||
for (let i = 0; i < delegated.selectors.length; i++) { | ||
const selector = delegated.selectors[i]; | ||
const context = delegated.contexts[i]; | ||
const selector = delegated.selectors[i] | ||
const context = delegated.contexts[i] | ||
if (domUtils.matchesSelector(element, selector) | ||
&& domUtils.nodeContains(context, eventTarget) | ||
&& domUtils.nodeContains(context, element)) { | ||
if (domUtils.matchesSelector(element, selector) && | ||
domUtils.nodeContains(context, eventTarget) && | ||
domUtils.nodeContains(context, element)) { | ||
const listeners = delegated.listeners[i] | ||
const listeners = delegated.listeners[i]; | ||
fakeEvent.currentTarget = element | ||
fakeEvent.currentTarget = element; | ||
for (let j = 0; j < listeners.length; j++) { | ||
const [fn, capture, passive] = listeners[j]; | ||
for (const [fn, capture, passive] of listeners) { | ||
if (capture === !!options.capture && passive === options.passive) { | ||
fn(fakeEvent); | ||
fn(fakeEvent) | ||
} | ||
@@ -239,3 +238,3 @@ } | ||
element = domUtils.parentNode(element); | ||
element = domUtils.parentNode(element) | ||
} | ||
@@ -245,11 +244,11 @@ } | ||
function delegateUseCapture (event: Event) { | ||
return delegateListener.call(this, event, true); | ||
return delegateListener.call(this, event, true) | ||
} | ||
function preventOriginalDefault () { | ||
this.originalEvent.preventDefault(); | ||
this.originalEvent.preventDefault() | ||
} | ||
function getOptions (param) { | ||
return is.object(param)? param : { capture: param }; | ||
return is.object(param) ? param : { capture: param } | ||
} | ||
@@ -277,8 +276,8 @@ | ||
window.document.createElement('div').addEventListener('test', null, { | ||
get capture () { return (events.supportsOptions = true); }, | ||
get passive () { return (events.supportsPassive = true); }, | ||
}); | ||
get capture () { return (events.supportsOptions = true) }, | ||
get passive () { return (events.supportsPassive = true) }, | ||
}) | ||
}, | ||
}; | ||
} | ||
export default events; | ||
export default events |
export default function extend<T, U extends Partial<T>> (dest: U, source: T) { | ||
for (const prop in source) { | ||
dest[prop] = source[prop]; | ||
dest[prop] = source[prop] | ||
} | ||
return dest as T & U; | ||
return dest as T & U | ||
} |
@@ -1,11 +0,11 @@ | ||
import { rectToXY, resolveRectLike } from './rect'; | ||
import { rectToXY, resolveRectLike } from './rect' | ||
export default function (target, element, action?) { | ||
const actionOptions = target.options[action]; | ||
const actionOrigin = actionOptions && actionOptions.origin; | ||
const origin = actionOrigin || target.options.origin; | ||
const actionOptions = target.options[action] | ||
const actionOrigin = actionOptions && actionOptions.origin | ||
const origin = actionOrigin || target.options.origin | ||
const originRect = resolveRectLike(origin, target, element, [target && element]); | ||
const originRect = resolveRectLike(origin, target, element, [target && element]) | ||
return rectToXY(originRect) || { x: 0, y: 0 }; | ||
return rectToXY(originRect) || { x: 0, y: 0 } | ||
} |
@@ -1,1 +0,1 @@ | ||
export default (x, y) => Math.sqrt(x * x + y * y); | ||
export default (x, y) => Math.sqrt(x * x + y * y) |
@@ -5,9 +5,9 @@ import * as arr from './arr'; | ||
import win from './window'; | ||
export declare function warnOnce(method: any, message: any): () => any; | ||
export declare function _getQBezierValue(t: any, p1: any, p2: any, p3: any): number; | ||
export declare function getQuadraticCurvePoint(startX: any, startY: any, cpX: any, cpY: any, endX: any, endY: any, position: any): { | ||
export declare function warnOnce<T>(this: T, method: (...args: any) => any, message: string): (this: T) => any; | ||
export declare function _getQBezierValue(t: number, p1: number, p2: number, p3: number): number; | ||
export declare function getQuadraticCurvePoint(startX: number, startY: number, cpX: number, cpY: number, endX: number, endY: number, position: number): { | ||
x: number; | ||
y: number; | ||
}; | ||
export declare function easeOutQuad(t: any, b: any, c: any, d: any): any; | ||
export declare function easeOutQuad(t: number, b: number, c: number, d: number): number; | ||
export declare function copyAction(dest: any, src: any): any; | ||
@@ -25,2 +25,2 @@ export { default as browser } from './browser'; | ||
export { default as Signals } from './Signals'; | ||
export { win, arr, dom, is, }; | ||
export { win, arr, dom, is }; |
@@ -7,2 +7,3 @@ import * as arr from './arr'; | ||
let warned = false; | ||
// eslint-disable-next-line no-shadow | ||
return function () { | ||
@@ -49,3 +50,3 @@ if (!warned) { | ||
export { default as Signals } from './Signals'; | ||
export { win, arr, dom, is, }; | ||
export { win, arr, dom, is }; | ||
//# sourceMappingURL=index.js.map |
77
index.ts
@@ -1,60 +0,59 @@ | ||
import * as arr from './arr'; | ||
import * as dom from './domUtils'; | ||
import * as is from './is'; | ||
import win from './window'; | ||
import * as arr from './arr' | ||
import * as dom from './domUtils' | ||
import * as is from './is' | ||
import win from './window' | ||
export function warnOnce (method, message) { | ||
let warned = false; | ||
export function warnOnce<T> (this: T, method: (...args: any) => any, message: string) { | ||
let warned = false | ||
return function () { | ||
// eslint-disable-next-line no-shadow | ||
return function (this: T) { | ||
if (!warned) { | ||
win.window.console.warn(message); | ||
warned = true; | ||
(win as any).window.console.warn(message) | ||
warned = true | ||
} | ||
return method.apply(this, arguments); | ||
}; | ||
return method.apply(this, arguments) | ||
} | ||
} | ||
// http://stackoverflow.com/a/5634528/2280888 | ||
export function _getQBezierValue (t, p1, p2, p3) { | ||
const iT = 1 - t; | ||
return iT * iT * p1 + 2 * iT * t * p2 + t * t * p3; | ||
export function _getQBezierValue (t: number, p1: number, p2: number, p3: number) { | ||
const iT = 1 - t | ||
return iT * iT * p1 + 2 * iT * t * p2 + t * t * p3 | ||
} | ||
export function getQuadraticCurvePoint (startX, startY, cpX, cpY, endX, endY, position) { | ||
export function getQuadraticCurvePoint ( | ||
startX: number, startY: number, cpX: number, cpY: number, endX: number, endY: number, position: number) { | ||
return { | ||
x: _getQBezierValue(position, startX, cpX, endX), | ||
y: _getQBezierValue(position, startY, cpY, endY), | ||
}; | ||
} | ||
} | ||
// http://gizma.com/easing/ | ||
export function easeOutQuad (t, b, c, d) { | ||
t /= d; | ||
return -c * t*(t-2) + b; | ||
export function easeOutQuad (t: number, b: number, c: number, d: number) { | ||
t /= d | ||
return -c * t * (t - 2) + b | ||
} | ||
export function copyAction (dest, src) { | ||
dest.name = src.name; | ||
dest.axis = src.axis; | ||
dest.edges = src.edges; | ||
export function copyAction (dest: any, src: any) { | ||
dest.name = src.name | ||
dest.axis = src.axis | ||
dest.edges = src.edges | ||
return dest; | ||
return dest | ||
} | ||
export { default as browser } from './browser'; | ||
export { default as clone } from './clone'; | ||
export { default as events } from './events'; | ||
export { default as extend } from './extend'; | ||
export { default as getOriginXY } from './getOriginXY'; | ||
export { default as hypot } from './hypot'; | ||
export { default as normalizeListeners } from './normalizeListeners'; | ||
export { default as pointer } from './pointerUtils'; | ||
export { default as raf } from './raf'; | ||
export { default as rect } from './rect'; | ||
export { default as Signals } from './Signals'; | ||
export { win, arr, dom, is, }; | ||
export { default as browser } from './browser' | ||
export { default as clone } from './clone' | ||
export { default as events } from './events' | ||
export { default as extend } from './extend' | ||
export { default as getOriginXY } from './getOriginXY' | ||
export { default as hypot } from './hypot' | ||
export { default as normalizeListeners } from './normalizeListeners' | ||
export { default as pointer } from './pointerUtils' | ||
export { default as raf } from './raf' | ||
export { default as rect } from './rect' | ||
export { default as Signals } from './Signals' | ||
export { win, arr, dom, is } |
declare const finder: { | ||
methodOrder: string[]; | ||
search: (details: any) => any; | ||
simulationResume: ({ pointerType, eventType, eventTarget, scope }: { | ||
search(details: any): any; | ||
simulationResume({ pointerType, eventType, eventTarget, scope }: { | ||
pointerType: any; | ||
@@ -9,4 +9,4 @@ eventType: any; | ||
scope: any; | ||
}) => any; | ||
mouseOrPen: ({ pointerId, pointerType, eventType, scope }: { | ||
}): any; | ||
mouseOrPen({ pointerId, pointerType, eventType, scope }: { | ||
pointerId: any; | ||
@@ -16,12 +16,12 @@ pointerType: any; | ||
scope: any; | ||
}) => any; | ||
hasPointer: ({ pointerId, scope }: { | ||
}): any; | ||
hasPointer({ pointerId, scope }: { | ||
pointerId: any; | ||
scope: any; | ||
}) => any; | ||
idle: ({ pointerType, scope }: { | ||
}): any; | ||
idle({ pointerType, scope }: { | ||
pointerType: any; | ||
scope: any; | ||
}) => any; | ||
}): any; | ||
}; | ||
export default finder; |
import * as utils from './index'; | ||
const finder = { | ||
methodOrder: ['simulationResume', 'mouseOrPen', 'hasPointer', 'idle'], | ||
search: function (details) { | ||
search(details) { | ||
for (const method of finder.methodOrder) { | ||
@@ -13,3 +13,3 @@ const interaction = finder[method](details); | ||
// try to resume simulation with a new pointer | ||
simulationResume: function ({ pointerType, eventType, eventTarget, scope }) { | ||
simulationResume({ pointerType, eventType, eventTarget, scope }) { | ||
if (!/down|start/i.test(eventType)) { | ||
@@ -20,4 +20,4 @@ return null; | ||
let element = eventTarget; | ||
if (interaction.simulation && interaction.simulation.allowResume | ||
&& (interaction.pointerType === pointerType)) { | ||
if (interaction.simulation && interaction.simulation.allowResume && | ||
(interaction.pointerType === pointerType)) { | ||
while (element) { | ||
@@ -35,3 +35,3 @@ // if the element is the interaction element | ||
// if it's a mouse or pen interaction | ||
mouseOrPen: function ({ pointerId, pointerType, eventType, scope }) { | ||
mouseOrPen({ pointerId, pointerType, eventType, scope }) { | ||
if (pointerType !== 'mouse' && pointerType !== 'pen') { | ||
@@ -73,3 +73,3 @@ return null; | ||
// get interaction that has this pointer | ||
hasPointer: function ({ pointerId, scope }) { | ||
hasPointer({ pointerId, scope }) { | ||
for (const interaction of scope.interactions.list) { | ||
@@ -82,3 +82,3 @@ if (hasPointerId(interaction, pointerId)) { | ||
// get first idle interaction with a matching pointerType | ||
idle: function ({ pointerType, scope }) { | ||
idle({ pointerType, scope }) { | ||
for (const interaction of scope.interactions.list) { | ||
@@ -85,0 +85,0 @@ // if there's already a pointer held down |
@@ -1,2 +0,2 @@ | ||
import * as utils from './index'; | ||
import * as utils from './index' | ||
@@ -6,8 +6,8 @@ const finder = { | ||
search: function (details) { | ||
search (details) { | ||
for (const method of finder.methodOrder) { | ||
const interaction = finder[method](details); | ||
const interaction = finder[method](details) | ||
if (interaction) { | ||
return interaction; | ||
return interaction | ||
} | ||
@@ -18,18 +18,18 @@ } | ||
// try to resume simulation with a new pointer | ||
simulationResume: function ({ pointerType, eventType, eventTarget, scope }) { | ||
simulationResume ({ pointerType, eventType, eventTarget, scope }) { | ||
if (!/down|start/i.test(eventType)) { | ||
return null; | ||
return null | ||
} | ||
for (const interaction of scope.interactions.list) { | ||
let element = eventTarget; | ||
let element = eventTarget | ||
if (interaction.simulation && interaction.simulation.allowResume | ||
&& (interaction.pointerType === pointerType)) { | ||
if (interaction.simulation && interaction.simulation.allowResume && | ||
(interaction.pointerType === pointerType)) { | ||
while (element) { | ||
// if the element is the interaction element | ||
if (element === interaction.element) { | ||
return interaction; | ||
return interaction | ||
} | ||
element = utils.dom.parentNode(element); | ||
element = utils.dom.parentNode(element) | ||
} | ||
@@ -39,12 +39,12 @@ } | ||
return null; | ||
return null | ||
}, | ||
// if it's a mouse or pen interaction | ||
mouseOrPen: function ({ pointerId, pointerType, eventType, scope }) { | ||
mouseOrPen ({ pointerId, pointerType, eventType, scope }) { | ||
if (pointerType !== 'mouse' && pointerType !== 'pen') { | ||
return null; | ||
return null | ||
} | ||
let firstNonActive; | ||
let firstNonActive | ||
@@ -54,11 +54,11 @@ for (const interaction of scope.interactions.list) { | ||
// if it's a down event, skip interactions with running simulations | ||
if (interaction.simulation && !hasPointerId(interaction, pointerId)) { continue; } | ||
if (interaction.simulation && !hasPointerId(interaction, pointerId)) { continue } | ||
// if the interaction is active, return it immediately | ||
if (interaction.interacting()) { | ||
return interaction; | ||
return interaction | ||
} | ||
// otherwise save it and look for another active interaction | ||
else if (!firstNonActive) { | ||
firstNonActive = interaction; | ||
firstNonActive = interaction | ||
} | ||
@@ -71,3 +71,3 @@ } | ||
if (firstNonActive) { | ||
return firstNonActive; | ||
return firstNonActive | ||
} | ||
@@ -80,14 +80,14 @@ | ||
if (interaction.pointerType === pointerType && !(/down/i.test(eventType) && interaction.simulation)) { | ||
return interaction; | ||
return interaction | ||
} | ||
} | ||
return null; | ||
return null | ||
}, | ||
// get interaction that has this pointer | ||
hasPointer: function ({ pointerId, scope }) { | ||
hasPointer ({ pointerId, scope }) { | ||
for (const interaction of scope.interactions.list) { | ||
if (hasPointerId(interaction, pointerId)) { | ||
return interaction; | ||
return interaction | ||
} | ||
@@ -98,11 +98,11 @@ } | ||
// get first idle interaction with a matching pointerType | ||
idle: function ({ pointerType, scope }) { | ||
idle ({ pointerType, scope }) { | ||
for (const interaction of scope.interactions.list) { | ||
// if there's already a pointer held down | ||
if (interaction.pointers.length === 1) { | ||
const target = interaction.target; | ||
const target = interaction.target | ||
// don't add this pointer if there is a target interactable and it | ||
// isn't gesturable | ||
if (target && !target.options.gesture.enabled) { | ||
continue; | ||
continue | ||
} | ||
@@ -112,18 +112,18 @@ } | ||
else if (interaction.pointers.length >= 2) { | ||
continue; | ||
continue | ||
} | ||
if (!interaction.interacting() && (pointerType === interaction.pointerType)) { | ||
return interaction; | ||
return interaction | ||
} | ||
} | ||
return null; | ||
return null | ||
}, | ||
}; | ||
} | ||
function hasPointerId (interaction, pointerId) { | ||
return utils.arr.some(interaction.pointers, ({ id }) => id === pointerId); | ||
return utils.arr.some(interaction.pointers, ({ id }) => id === pointerId) | ||
} | ||
export default finder; | ||
export default finder |
@@ -6,3 +6,3 @@ export declare const window: (thing: any) => thing is Window; | ||
}; | ||
export declare const func: (thing: any) => thing is Function; | ||
export declare const func: (thing: any) => thing is (...args: any) => any; | ||
export declare const number: (thing: any) => thing is number; | ||
@@ -9,0 +9,0 @@ export declare const bool: (thing: any) => thing is boolean; |
@@ -0,1 +1,2 @@ | ||
// tslint:disable variable-name | ||
import isWindow from './isWindow'; | ||
@@ -16,3 +17,3 @@ import win from './window'; | ||
return (/object|function/.test(typeof _window.Element) | ||
? thing instanceof _window.Element //DOM2 | ||
? thing instanceof _window.Element // DOM2 | ||
: thing.nodeType === 1 && typeof thing.nodeName === 'string'); | ||
@@ -23,5 +24,5 @@ }; | ||
/function Object\b/.test(thing.constructor.toString()); | ||
export const array = (thing) => (object(thing) | ||
&& (typeof thing.length !== 'undefined') | ||
&& func(thing.splice)); | ||
export const array = (thing) => (object(thing) && | ||
(typeof thing.length !== 'undefined') && | ||
func(thing.splice)); | ||
//# sourceMappingURL=is.js.map |
40
is.ts
@@ -1,34 +0,36 @@ | ||
import isWindow from './isWindow'; | ||
import win from './window'; | ||
// tslint:disable variable-name | ||
import isWindow from './isWindow' | ||
import win from './window' | ||
export const window = (thing: any): thing is Window => | ||
thing === win.window || isWindow(thing); | ||
thing === win.window || isWindow(thing) | ||
export const docFrag = (thing: any): thing is DocumentFragment => | ||
object(thing) && thing.nodeType === 11; | ||
object(thing) && thing.nodeType === 11 | ||
export const object = (thing: any): thing is { [index: string]: any } => | ||
!!thing && (typeof thing === 'object'); | ||
!!thing && (typeof thing === 'object') | ||
export const func = (thing: any): thing is Function => | ||
typeof thing === 'function'; | ||
export const func = (thing: any): thing is (...args: any) => any => | ||
typeof thing === 'function' | ||
export const number = (thing: any): thing is number => | ||
typeof thing === 'number' ; | ||
typeof thing === 'number' | ||
export const bool = (thing: any): thing is boolean => | ||
typeof thing === 'boolean' ; | ||
typeof thing === 'boolean' | ||
export const string = (thing: any): thing is string => | ||
typeof thing === 'string' ; | ||
typeof thing === 'string' | ||
export const element = (thing: any): thing is Element => { | ||
if (!thing || (typeof thing !== 'object')) { return false; } | ||
if (!thing || (typeof thing !== 'object')) { return false } | ||
const _window = win.getWindow(thing) || win.window; | ||
const _window = win.getWindow(thing) || win.window | ||
return (/object|function/.test(typeof _window.Element) | ||
? thing instanceof _window.Element //DOM2 | ||
: thing.nodeType === 1 && typeof thing.nodeName === 'string'); | ||
}; | ||
? thing instanceof _window.Element // DOM2 | ||
: thing.nodeType === 1 && typeof thing.nodeName === 'string') | ||
} | ||
@@ -38,7 +40,7 @@ export const plainObject: typeof object = (thing: any): thing is { [index: string]: any } => | ||
!!thing.constructor && | ||
/function Object\b/.test(thing.constructor.toString()); | ||
/function Object\b/.test(thing.constructor.toString()) | ||
export const array = (thing: any): thing is any[] => | ||
(object(thing) | ||
&& (typeof thing.length !== 'undefined') | ||
&& func(thing.splice)); | ||
(object(thing) && | ||
(typeof thing.length !== 'undefined') && | ||
func(thing.splice)) |
@@ -1,1 +0,1 @@ | ||
export default (thing) => !!(thing && thing.Window) && (thing instanceof thing.Window); | ||
export default (thing) => !!(thing && thing.Window) && (thing instanceof thing.Window) |
@@ -1,1 +0,3 @@ | ||
export default function normalize(type: any, listener?: any, result?: any): any; | ||
export default function normalize(type: Interact.Listener | string, listener?: Interact.Listeners, result?: { | ||
[type: string]: Interact.Listener[]; | ||
}): any; |
@@ -0,3 +1,3 @@ | ||
import extend from './extend'; | ||
import * as is from './is'; | ||
import extend from './extend'; | ||
export default function normalize(type, listener, result) { | ||
@@ -27,3 +27,3 @@ result = result || {}; | ||
for (const prefix in listener) { | ||
const combinedTypes = split(prefix).map(p => `${type}${p}`); | ||
const combinedTypes = split(prefix).map((p) => `${type}${p}`); | ||
normalize(combinedTypes, listener[prefix], result); | ||
@@ -30,0 +30,0 @@ } |
@@ -1,13 +0,17 @@ | ||
import * as is from './is'; | ||
import extend from './extend'; | ||
import extend from './extend' | ||
import * as is from './is' | ||
export default function normalize (type, listener?, result?) { | ||
result = result || {}; | ||
export default function normalize ( | ||
type: Interact.Listener | string, | ||
listener?: Interact.Listeners, | ||
result?: { [type: string]: Interact.Listener[] | ||
}) { | ||
result = result || {} | ||
if (is.string(type) && type.search(' ') !== -1) { | ||
type = split(type); | ||
type = split(type) | ||
} | ||
if (is.array(type)) { | ||
return type.reduce((acc, t) => extend(acc, normalize(t, listener, result)), {}); | ||
return type.reduce((acc, t) => extend(acc, normalize(t, listener, result)), {}) | ||
} | ||
@@ -17,28 +21,28 @@ | ||
if (is.object(type)) { | ||
listener = type; | ||
type = ''; | ||
listener = type | ||
type = '' | ||
} | ||
if (is.func(listener)) { | ||
result[type] = result[type] || []; | ||
result[type].push(listener); | ||
result[type] = result[type] || [] | ||
result[type].push(listener) | ||
} | ||
else if (is.array(listener)) { | ||
for (const l of listener) { | ||
normalize(type, l, result); | ||
normalize(type, l, result) | ||
} | ||
} | ||
else if (is.object(listener)) { | ||
for (const prefix in listener) { | ||
const combinedTypes = split(prefix).map(p => `${type}${p}`); | ||
for (const prefix in listener as Interact.Listener) { | ||
const combinedTypes = split(prefix).map((p) => `${type}${p}`) | ||
normalize(combinedTypes, listener[prefix], result); | ||
normalize(combinedTypes, listener[prefix], result) | ||
} | ||
} | ||
return result; | ||
return result | ||
} | ||
function split (type) { | ||
return type.trim().split(/ +/); | ||
return type.trim().split(/ +/) | ||
} |
{ | ||
"name": "@interactjs/utils", | ||
"version": "1.4.0-alpha.20+sha.ce0da21", | ||
"version": "1.4.0-alpha.21+sha.cdc1d5f", | ||
"devDependencies": { | ||
"@interactjs/_dev": "1.4.0-alpha.20+sha.ce0da21" | ||
"@interactjs/_dev": "1.4.0-alpha.21+sha.cdc1d5f" | ||
}, | ||
@@ -7,0 +7,0 @@ "publishConfig": { |
export interface PointerExtend { | ||
webkit: RegExp; | ||
[prefix: string]: RegExp; | ||
webkit: RegExp | ||
[prefix: string]: RegExp | ||
} | ||
@@ -8,4 +8,4 @@ | ||
for (const prop in source) { | ||
const prefixedPropREs = pointerExtend.prefixedPropREs; | ||
let deprecated = false; | ||
const prefixedPropREs = pointerExtend.prefixedPropREs | ||
let deprecated = false | ||
@@ -15,4 +15,4 @@ // skip deprecated prefixed properties | ||
if (prop.indexOf(vendor) === 0 && prefixedPropREs[vendor].test(prop)) { | ||
deprecated = true; | ||
break; | ||
deprecated = true | ||
break | ||
} | ||
@@ -22,6 +22,6 @@ } | ||
if (!deprecated && typeof source[prop] !== 'function') { | ||
dest[prop] = source[prop]; | ||
dest[prop] = source[prop] | ||
} | ||
} | ||
return dest; | ||
return dest | ||
} | ||
@@ -31,4 +31,4 @@ | ||
webkit: /(Movement[XY]|Radius[XY]|RotationAngle|Force)$/, | ||
}; | ||
} | ||
export default pointerExtend; | ||
export default pointerExtend |
import pointerExtend from './pointerExtend'; | ||
declare const pointerUtils: { | ||
copyCoords: (dest: any, src: any) => void; | ||
setCoordDeltas: (targetObj: any, prev: any, cur: any) => void; | ||
copyCoords(dest: any, src: any): void; | ||
setCoordDeltas(targetObj: any, prev: any, cur: any): void; | ||
setCoordVelocity(targetObj: any, delta: any): void; | ||
isNativePointer: (pointer: any) => boolean; | ||
getXY: (type: any, pointer: any, xy: any) => any; | ||
getPageXY: (pointer: import("../interactjs/types").PointerType, page?: import("../interactjs/types").Point) => import("../interactjs/types").Point; | ||
getClientXY: (pointer: any, client: any) => any; | ||
getPointerId: (pointer: any) => any; | ||
setCoords: (targetObj: any, pointers: any[], timeStamp?: number) => void; | ||
isNativePointer(pointer: any): boolean; | ||
getXY(type: any, pointer: any, xy: any): any; | ||
getPageXY(pointer: import("../interactjs/types").PointerType, page?: import("../interactjs/types").Point): import("../interactjs/types").Point; | ||
getClientXY(pointer: any, client: any): any; | ||
getPointerId(pointer: any): any; | ||
setCoords(targetObj: any, pointers: any[], timeStamp?: number): void; | ||
pointerExtend: typeof pointerExtend; | ||
getTouchPair: (event: any) => any[]; | ||
pointerAverage: (pointers: PointerEvent[] | Event[]) => { | ||
getTouchPair(event: any): any[]; | ||
pointerAverage(pointers: PointerEvent[] | Event[]): { | ||
pageX: number; | ||
@@ -22,3 +22,3 @@ pageY: number; | ||
}; | ||
touchBBox: (event: Event | PointerEvent[]) => { | ||
touchBBox(event: Event | PointerEvent[]): { | ||
x: number; | ||
@@ -31,6 +31,6 @@ y: number; | ||
}; | ||
touchDistance: (event: any, deltaSource: any) => number; | ||
touchAngle: (event: any, deltaSource: any) => number; | ||
getPointerType: (pointer: any) => any; | ||
getEventTargets: (event: any) => any[]; | ||
touchDistance(event: any, deltaSource: any): number; | ||
touchAngle(event: any, deltaSource: any): number; | ||
getPointerType(pointer: any): any; | ||
getEventTargets(event: any): any[]; | ||
newCoords(): { | ||
@@ -47,7 +47,7 @@ page: { | ||
}; | ||
coordsToEvent: ({ page, client, timeStamp }: { | ||
coordsToEvent({ page, client, timeStamp }: { | ||
page: any; | ||
client: any; | ||
timeStamp: any; | ||
}) => { | ||
}): { | ||
page: any; | ||
@@ -54,0 +54,0 @@ client: any; |
import browser from './browser'; | ||
import { default as dom, default as domObjects } from './domObjects'; | ||
import dom from './domObjects'; | ||
import * as domUtils from './domUtils'; | ||
@@ -8,3 +8,3 @@ import hypot from './hypot'; | ||
const pointerUtils = { | ||
copyCoords: function (dest, src) { | ||
copyCoords(dest, src) { | ||
dest.page = dest.page || {}; | ||
@@ -18,3 +18,3 @@ dest.page.x = src.page.x; | ||
}, | ||
setCoordDeltas: function (targetObj, prev, cur) { | ||
setCoordDeltas(targetObj, prev, cur) { | ||
targetObj.page.x = cur.page.x - prev.page.x; | ||
@@ -34,7 +34,7 @@ targetObj.page.y = cur.page.y - prev.page.y; | ||
}, | ||
isNativePointer: function (pointer) { | ||
isNativePointer(pointer) { | ||
return (pointer instanceof dom.Event || pointer instanceof dom.Touch); | ||
}, | ||
// Get specified X/Y coords for mouse or event.touches[0] | ||
getXY: function (type, pointer, xy) { | ||
getXY(type, pointer, xy) { | ||
xy = xy || {}; | ||
@@ -46,3 +46,3 @@ type = type || 'page'; | ||
}, | ||
getPageXY: function (pointer, page) { | ||
getPageXY(pointer, page) { | ||
page = page || { x: 0, y: 0 }; | ||
@@ -60,3 +60,3 @@ // Opera Mobile handles the viewport and scrolling oddly | ||
}, | ||
getClientXY: function (pointer, client) { | ||
getClientXY(pointer, client) { | ||
client = client || {}; | ||
@@ -72,6 +72,6 @@ if (browser.isOperaMobile && pointerUtils.isNativePointer(pointer)) { | ||
}, | ||
getPointerId: function (pointer) { | ||
getPointerId(pointer) { | ||
return is.number(pointer.pointerId) ? pointer.pointerId : pointer.identifier; | ||
}, | ||
setCoords: function (targetObj, pointers, timeStamp) { | ||
setCoords(targetObj, pointers, timeStamp) { | ||
const pointer = (pointers.length > 1 | ||
@@ -89,4 +89,4 @@ ? pointerUtils.pointerAverage(pointers) | ||
}, | ||
pointerExtend: pointerExtend, | ||
getTouchPair: function (event) { | ||
pointerExtend, | ||
getTouchPair(event) { | ||
const touches = []; | ||
@@ -117,3 +117,3 @@ // array of touches is supplied | ||
}, | ||
pointerAverage: function (pointers) { | ||
pointerAverage(pointers) { | ||
const average = { | ||
@@ -137,3 +137,3 @@ pageX: 0, | ||
}, | ||
touchBBox: function (event) { | ||
touchBBox(event) { | ||
if (!event.length && | ||
@@ -158,3 +158,3 @@ !(event.touches && | ||
}, | ||
touchDistance: function (event, deltaSource) { | ||
touchDistance(event, deltaSource) { | ||
const sourceX = deltaSource + 'X'; | ||
@@ -167,3 +167,3 @@ const sourceY = deltaSource + 'Y'; | ||
}, | ||
touchAngle: function (event, deltaSource) { | ||
touchAngle(event, deltaSource) { | ||
const sourceX = deltaSource + 'X'; | ||
@@ -177,3 +177,3 @@ const sourceY = deltaSource + 'Y'; | ||
}, | ||
getPointerType: function (pointer) { | ||
getPointerType(pointer) { | ||
return is.string(pointer.pointerType) | ||
@@ -185,3 +185,3 @@ ? pointer.pointerType | ||
// be either a MouseEvent, TouchEvent, or Touch object | ||
: /touch/.test(pointer.type) || pointer instanceof domObjects.Touch | ||
: /touch/.test(pointer.type) || pointer instanceof dom.Touch | ||
? 'touch' | ||
@@ -191,3 +191,3 @@ : 'mouse'; | ||
// [ event.target, event.currentTarget ] | ||
getEventTargets: function (event) { | ||
getEventTargets(event) { | ||
const path = is.func(event.composedPath) ? event.composedPath() : event.path; | ||
@@ -206,3 +206,3 @@ return [ | ||
}, | ||
coordsToEvent: function ({ page, client, timeStamp }) { | ||
coordsToEvent({ page, client, timeStamp }) { | ||
return { | ||
@@ -209,0 +209,0 @@ page, |
@@ -1,116 +0,116 @@ | ||
import browser from './browser'; | ||
import { default as dom, default as domObjects } from './domObjects'; | ||
import * as domUtils from './domUtils'; | ||
import hypot from './hypot'; | ||
import * as is from './is'; | ||
import pointerExtend from './pointerExtend'; | ||
import browser from './browser' | ||
import dom from './domObjects' | ||
import * as domUtils from './domUtils' | ||
import hypot from './hypot' | ||
import * as is from './is' | ||
import pointerExtend from './pointerExtend' | ||
const pointerUtils = { | ||
copyCoords: function (dest, src) { | ||
dest.page = dest.page || {}; | ||
dest.page.x = src.page.x; | ||
dest.page.y = src.page.y; | ||
copyCoords (dest, src) { | ||
dest.page = dest.page || {} | ||
dest.page.x = src.page.x | ||
dest.page.y = src.page.y | ||
dest.client = dest.client || {}; | ||
dest.client.x = src.client.x; | ||
dest.client.y = src.client.y; | ||
dest.client = dest.client || {} | ||
dest.client.x = src.client.x | ||
dest.client.y = src.client.y | ||
dest.timeStamp = src.timeStamp; | ||
dest.timeStamp = src.timeStamp | ||
}, | ||
setCoordDeltas: function (targetObj, prev, cur) { | ||
targetObj.page.x = cur.page.x - prev.page.x; | ||
targetObj.page.y = cur.page.y - prev.page.y; | ||
targetObj.client.x = cur.client.x - prev.client.x; | ||
targetObj.client.y = cur.client.y - prev.client.y; | ||
targetObj.timeStamp = cur.timeStamp - prev.timeStamp; | ||
setCoordDeltas (targetObj, prev, cur) { | ||
targetObj.page.x = cur.page.x - prev.page.x | ||
targetObj.page.y = cur.page.y - prev.page.y | ||
targetObj.client.x = cur.client.x - prev.client.x | ||
targetObj.client.y = cur.client.y - prev.client.y | ||
targetObj.timeStamp = cur.timeStamp - prev.timeStamp | ||
}, | ||
setCoordVelocity (targetObj, delta) { | ||
const dt = Math.max(delta.timeStamp / 1000, 0.001); | ||
const dt = Math.max(delta.timeStamp / 1000, 0.001) | ||
targetObj.page.x = delta.page.x / dt; | ||
targetObj.page.y = delta.page.y / dt; | ||
targetObj.client.x = delta.client.x / dt; | ||
targetObj.client.y = delta.client.y / dt; | ||
targetObj.timeStamp = dt; | ||
targetObj.page.x = delta.page.x / dt | ||
targetObj.page.y = delta.page.y / dt | ||
targetObj.client.x = delta.client.x / dt | ||
targetObj.client.y = delta.client.y / dt | ||
targetObj.timeStamp = dt | ||
}, | ||
isNativePointer: function (pointer) { | ||
return (pointer instanceof dom.Event || pointer instanceof dom.Touch); | ||
isNativePointer (pointer) { | ||
return (pointer instanceof dom.Event || pointer instanceof dom.Touch) | ||
}, | ||
// Get specified X/Y coords for mouse or event.touches[0] | ||
getXY: function (type, pointer, xy) { | ||
xy = xy || {}; | ||
type = type || 'page'; | ||
getXY (type, pointer, xy) { | ||
xy = xy || {} | ||
type = type || 'page' | ||
xy.x = pointer[type + 'X']; | ||
xy.y = pointer[type + 'Y']; | ||
xy.x = pointer[type + 'X'] | ||
xy.y = pointer[type + 'Y'] | ||
return xy; | ||
return xy | ||
}, | ||
getPageXY: function (pointer: Interact.PointerType, page?: Interact.Point) { | ||
page = page || { x: 0, y: 0 }; | ||
getPageXY (pointer: Interact.PointerType, page?: Interact.Point) { | ||
page = page || { x: 0, y: 0 } | ||
// Opera Mobile handles the viewport and scrolling oddly | ||
if (browser.isOperaMobile && pointerUtils.isNativePointer(pointer)) { | ||
pointerUtils.getXY('screen', pointer, page); | ||
pointerUtils.getXY('screen', pointer, page) | ||
page.x += window.scrollX; | ||
page.y += window.scrollY; | ||
page.x += window.scrollX | ||
page.y += window.scrollY | ||
} | ||
else { | ||
pointerUtils.getXY('page', pointer, page); | ||
pointerUtils.getXY('page', pointer, page) | ||
} | ||
return page; | ||
return page | ||
}, | ||
getClientXY: function (pointer, client) { | ||
client = client || {}; | ||
getClientXY (pointer, client) { | ||
client = client || {} | ||
if (browser.isOperaMobile && pointerUtils.isNativePointer(pointer)) { | ||
// Opera Mobile handles the viewport and scrolling oddly | ||
pointerUtils.getXY('screen', pointer, client); | ||
pointerUtils.getXY('screen', pointer, client) | ||
} | ||
else { | ||
pointerUtils.getXY('client', pointer, client); | ||
pointerUtils.getXY('client', pointer, client) | ||
} | ||
return client; | ||
return client | ||
}, | ||
getPointerId: function (pointer) { | ||
return is.number(pointer.pointerId)? pointer.pointerId : pointer.identifier; | ||
getPointerId (pointer) { | ||
return is.number(pointer.pointerId) ? pointer.pointerId : pointer.identifier | ||
}, | ||
setCoords: function (targetObj, pointers: any[], timeStamp?: number) { | ||
setCoords (targetObj, pointers: any[], timeStamp?: number) { | ||
const pointer = (pointers.length > 1 | ||
? pointerUtils.pointerAverage(pointers) | ||
: pointers[0]); | ||
: pointers[0]) | ||
const tmpXY = {} as { x: number, y: number }; | ||
const tmpXY = {} as { x: number, y: number } | ||
pointerUtils.getPageXY(pointer, tmpXY); | ||
targetObj.page.x = tmpXY.x; | ||
targetObj.page.y = tmpXY.y; | ||
pointerUtils.getPageXY(pointer, tmpXY) | ||
targetObj.page.x = tmpXY.x | ||
targetObj.page.y = tmpXY.y | ||
pointerUtils.getClientXY(pointer, tmpXY); | ||
targetObj.client.x = tmpXY.x; | ||
targetObj.client.y = tmpXY.y; | ||
pointerUtils.getClientXY(pointer, tmpXY) | ||
targetObj.client.x = tmpXY.x | ||
targetObj.client.y = tmpXY.y | ||
targetObj.timeStamp = is.number(timeStamp) ? timeStamp :new Date().getTime(); | ||
targetObj.timeStamp = is.number(timeStamp) ? timeStamp : new Date().getTime() | ||
}, | ||
pointerExtend: pointerExtend, | ||
pointerExtend, | ||
getTouchPair: function (event) { | ||
const touches = []; | ||
getTouchPair (event) { | ||
const touches = [] | ||
// array of touches is supplied | ||
if (is.array(event)) { | ||
touches[0] = event[0]; | ||
touches[1] = event[1]; | ||
touches[0] = event[0] | ||
touches[1] = event[1] | ||
} | ||
@@ -121,20 +121,20 @@ // an event | ||
if (event.touches.length === 1) { | ||
touches[0] = event.touches[0]; | ||
touches[1] = event.changedTouches[0]; | ||
touches[0] = event.touches[0] | ||
touches[1] = event.changedTouches[0] | ||
} | ||
else if (event.touches.length === 0) { | ||
touches[0] = event.changedTouches[0]; | ||
touches[1] = event.changedTouches[1]; | ||
touches[0] = event.changedTouches[0] | ||
touches[1] = event.changedTouches[1] | ||
} | ||
} | ||
else { | ||
touches[0] = event.touches[0]; | ||
touches[1] = event.touches[1]; | ||
touches[0] = event.touches[0] | ||
touches[1] = event.touches[1] | ||
} | ||
} | ||
return touches; | ||
return touches | ||
}, | ||
pointerAverage: function (pointers: PointerEvent[] | Event[]) { | ||
pointerAverage (pointers: PointerEvent[] | Event[]) { | ||
const average = { | ||
@@ -147,28 +147,28 @@ pageX : 0, | ||
screenY: 0, | ||
}; | ||
} | ||
for (const pointer of pointers) { | ||
for (const prop in average) { | ||
average[prop] += pointer[prop]; | ||
average[prop] += pointer[prop] | ||
} | ||
} | ||
for (const prop in average) { | ||
average[prop] /= pointers.length; | ||
average[prop] /= pointers.length | ||
} | ||
return average; | ||
return average | ||
}, | ||
touchBBox: function (event: Event | PointerEvent[]) { | ||
touchBBox (event: Event | PointerEvent[]) { | ||
if (!(event as any).length && | ||
!((event as TouchEvent).touches && | ||
(event as TouchEvent).touches.length > 1)) { | ||
return null; | ||
return null | ||
} | ||
const touches = pointerUtils.getTouchPair(event); | ||
const minX = Math.min(touches[0].pageX, touches[1].pageX); | ||
const minY = Math.min(touches[0].pageY, touches[1].pageY); | ||
const maxX = Math.max(touches[0].pageX, touches[1].pageX); | ||
const maxY = Math.max(touches[0].pageY, touches[1].pageY); | ||
const touches = pointerUtils.getTouchPair(event) | ||
const minX = Math.min(touches[0].pageX, touches[1].pageX) | ||
const minY = Math.min(touches[0].pageY, touches[1].pageY) | ||
const maxX = Math.max(touches[0].pageX, touches[1].pageX) | ||
const maxY = Math.max(touches[0].pageY, touches[1].pageY) | ||
@@ -182,43 +182,42 @@ return { | ||
height: maxY - minY, | ||
}; | ||
} | ||
}, | ||
touchDistance: function (event, deltaSource) { | ||
const sourceX = deltaSource + 'X'; | ||
const sourceY = deltaSource + 'Y'; | ||
const touches = pointerUtils.getTouchPair(event); | ||
touchDistance (event, deltaSource) { | ||
const sourceX = deltaSource + 'X' | ||
const sourceY = deltaSource + 'Y' | ||
const touches = pointerUtils.getTouchPair(event) | ||
const dx = touches[0][sourceX] - touches[1][sourceX] | ||
const dy = touches[0][sourceY] - touches[1][sourceY] | ||
const dx = touches[0][sourceX] - touches[1][sourceX]; | ||
const dy = touches[0][sourceY] - touches[1][sourceY]; | ||
return hypot(dx, dy); | ||
return hypot(dx, dy) | ||
}, | ||
touchAngle: function (event, deltaSource) { | ||
const sourceX = deltaSource + 'X'; | ||
const sourceY = deltaSource + 'Y'; | ||
const touches = pointerUtils.getTouchPair(event); | ||
const dx = touches[1][sourceX] - touches[0][sourceX]; | ||
const dy = touches[1][sourceY] - touches[0][sourceY]; | ||
const angle = 180 * Math.atan2(dy , dx) / Math.PI; | ||
touchAngle (event, deltaSource) { | ||
const sourceX = deltaSource + 'X' | ||
const sourceY = deltaSource + 'Y' | ||
const touches = pointerUtils.getTouchPair(event) | ||
const dx = touches[1][sourceX] - touches[0][sourceX] | ||
const dy = touches[1][sourceY] - touches[0][sourceY] | ||
const angle = 180 * Math.atan2(dy, dx) / Math.PI | ||
return angle; | ||
return angle | ||
}, | ||
getPointerType: function (pointer) { | ||
getPointerType (pointer) { | ||
return is.string(pointer.pointerType) | ||
? pointer.pointerType | ||
: is.number(pointer.pointerType) | ||
? [undefined, undefined,'touch', 'pen', 'mouse'][pointer.pointerType] | ||
? [undefined, undefined, 'touch', 'pen', 'mouse'][pointer.pointerType] | ||
// if the PointerEvent API isn't available, then the "pointer" must | ||
// be either a MouseEvent, TouchEvent, or Touch object | ||
: /touch/.test(pointer.type) || pointer instanceof domObjects.Touch | ||
: /touch/.test(pointer.type) || pointer instanceof dom.Touch | ||
? 'touch' | ||
: 'mouse'; | ||
: 'mouse' | ||
}, | ||
// [ event.target, event.currentTarget ] | ||
getEventTargets: function (event) { | ||
const path = is.func(event.composedPath) ? event.composedPath() : event.path; | ||
getEventTargets (event) { | ||
const path = is.func(event.composedPath) ? event.composedPath() : event.path | ||
@@ -228,3 +227,3 @@ return [ | ||
domUtils.getActualElement(event.currentTarget), | ||
]; | ||
] | ||
}, | ||
@@ -237,6 +236,6 @@ | ||
timeStamp: 0, | ||
}; | ||
} | ||
}, | ||
coordsToEvent: function ({ page, client, timeStamp }) { | ||
coordsToEvent ({ page, client, timeStamp }) { | ||
return { | ||
@@ -246,10 +245,10 @@ page, | ||
timeStamp, | ||
get pageX () { return page.x; }, | ||
get pageY () { return page.y; }, | ||
get clientX () { return client.x; }, | ||
get clientY () { return client.y; }, | ||
}; | ||
get pageX () { return page.x }, | ||
get pageY () { return page.y }, | ||
get clientX () { return client.x }, | ||
get clientY () { return client.y }, | ||
} | ||
}, | ||
}; | ||
} | ||
export default pointerUtils; | ||
export default pointerUtils |
11
raf.js
@@ -15,17 +15,18 @@ let lastTime = 0; | ||
if (!request) { | ||
request = callback => { | ||
request = (callback) => { | ||
const currTime = new Date().getTime(); | ||
const timeToCall = Math.max(0, 16 - (currTime - lastTime)); | ||
const token = setTimeout(function () { callback(currTime + timeToCall); }, timeToCall); | ||
// eslint-disable-next-line standard/no-callback-literal | ||
const token = setTimeout(() => { callback(currTime + timeToCall); }, timeToCall); | ||
lastTime = currTime + timeToCall; | ||
return token; | ||
}; | ||
cancel = token => clearTimeout(token); | ||
cancel = (token) => clearTimeout(token); | ||
} | ||
} | ||
export default { | ||
request: callback => request(callback), | ||
cancel: token => cancel(token), | ||
request: (callback) => request(callback), | ||
cancel: (token) => cancel(token), | ||
init, | ||
}; | ||
//# sourceMappingURL=raf.js.map |
41
raf.ts
@@ -1,15 +0,15 @@ | ||
let lastTime = 0; | ||
let request; | ||
let cancel; | ||
let lastTime = 0 | ||
let request | ||
let cancel | ||
function init (window) { | ||
request = window.requestAnimationFrame; | ||
cancel = window.cancelAnimationFrame; | ||
request = window.requestAnimationFrame | ||
cancel = window.cancelAnimationFrame | ||
if (!request) { | ||
const vendors = ['ms', 'moz', 'webkit', 'o']; | ||
const vendors = ['ms', 'moz', 'webkit', 'o'] | ||
for (const vendor of vendors) { | ||
request = window[`${vendor}RequestAnimationFrame`]; | ||
cancel = window[`${vendor}CancelAnimationFrame`] || window[`${vendor}CancelRequestAnimationFrame`]; | ||
request = window[`${vendor}RequestAnimationFrame`] | ||
cancel = window[`${vendor}CancelAnimationFrame`] || window[`${vendor}CancelRequestAnimationFrame`] | ||
} | ||
@@ -19,13 +19,14 @@ } | ||
if (!request) { | ||
request = callback => { | ||
const currTime = new Date().getTime(); | ||
const timeToCall = Math.max(0, 16 - (currTime - lastTime)); | ||
const token = setTimeout(function () { callback(currTime + timeToCall); }, | ||
timeToCall); | ||
request = (callback) => { | ||
const currTime = new Date().getTime() | ||
const timeToCall = Math.max(0, 16 - (currTime - lastTime)) | ||
// eslint-disable-next-line standard/no-callback-literal | ||
const token = setTimeout(() => { callback(currTime + timeToCall) }, | ||
timeToCall) | ||
lastTime = currTime + timeToCall; | ||
return token; | ||
}; | ||
lastTime = currTime + timeToCall | ||
return token | ||
} | ||
cancel = token => clearTimeout(token); | ||
cancel = (token) => clearTimeout(token) | ||
} | ||
@@ -35,5 +36,5 @@ } | ||
export default { | ||
request: callback => request(callback), | ||
cancel: token => cancel(token), | ||
request: (callback) => request(callback), | ||
cancel: (token) => cancel(token), | ||
init, | ||
}; | ||
} |
52
rect.ts
@@ -1,35 +0,35 @@ | ||
import { closest, getElementRect, parentNode } from './domUtils'; | ||
import extend from './extend'; | ||
import * as is from './is'; | ||
import { closest, getElementRect, parentNode } from './domUtils' | ||
import extend from './extend' | ||
import * as is from './is' | ||
export function getStringOptionResult (value, interactable, element) { | ||
if (!is.string(value)) { | ||
return null; | ||
return null | ||
} | ||
if (value === 'parent') { | ||
value = parentNode(element); | ||
value = parentNode(element) | ||
} | ||
else if (value === 'self') { | ||
value = interactable.getRect(element); | ||
value = interactable.getRect(element) | ||
} | ||
else { | ||
value = closest(element, value); | ||
value = closest(element, value) | ||
} | ||
return value; | ||
return value | ||
} | ||
export function resolveRectLike (value, interactable?, element?, functionArgs?) { | ||
value = getStringOptionResult(value, interactable, element) || value; | ||
value = getStringOptionResult(value, interactable, element) || value | ||
if (is.func(value)) { | ||
value = value.apply(null, functionArgs); | ||
value = value.apply(null, functionArgs) | ||
} | ||
if (is.element(value)) { | ||
value = getElementRect(value); | ||
value = getElementRect(value) | ||
} | ||
return value; | ||
return value | ||
} | ||
@@ -41,3 +41,3 @@ | ||
y: 'y' in rect ? rect.y : rect.top, | ||
}; | ||
} | ||
} | ||
@@ -47,11 +47,11 @@ | ||
if (rect && !('left' in rect && 'top' in rect)) { | ||
rect = extend({}, rect); | ||
rect = extend({}, rect) | ||
rect.left = rect.x || 0; | ||
rect.top = rect.y || 0; | ||
rect.right = rect.right || (rect.left + rect.width); | ||
rect.bottom = rect.bottom || (rect.top + rect.height); | ||
rect.left = rect.x || 0 | ||
rect.top = rect.y || 0 | ||
rect.right = rect.right || (rect.left + rect.width) | ||
rect.bottom = rect.bottom || (rect.top + rect.height) | ||
} | ||
return rect; | ||
return rect | ||
} | ||
@@ -61,11 +61,11 @@ | ||
if (rect && !('x' in rect && 'y' in rect)) { | ||
rect = extend({}, rect); | ||
rect = extend({}, rect) | ||
rect.x = rect.left || 0; | ||
rect.y = rect.top || 0; | ||
rect.width = rect.width || (rect.right - rect.x); | ||
rect.height = rect.height || (rect.bottom - rect.y); | ||
rect.x = rect.left || 0 | ||
rect.y = rect.top || 0 | ||
rect.width = rect.width || (rect.right - rect.x) | ||
rect.height = rect.height || (rect.bottom - rect.y) | ||
} | ||
return rect; | ||
return rect | ||
} | ||
@@ -79,2 +79,2 @@ | ||
tlbrToXywh, | ||
}; | ||
} |
@@ -0,8 +1,10 @@ | ||
declare type SignalListener = (signalArg: any, sinalName: string) => (void | boolean); | ||
declare class Signals { | ||
listeners: {}; | ||
constructor(); | ||
on(name: any, listener: any): void; | ||
off(name: any, listener: any): void; | ||
fire(name: any, arg: any): void | false; | ||
listeners: { | ||
[signalName: string]: SignalListener[]; | ||
}; | ||
on(name: string, listener: SignalListener): void; | ||
off(name: string, listener: SignalListener): void; | ||
fire(name: string, arg: any): void | false; | ||
} | ||
export default Signals; |
class Signals { | ||
constructor() { | ||
this.listeners = { | ||
// signalName: [listeners], | ||
}; | ||
this.listeners = {}; | ||
} | ||
@@ -7,0 +5,0 @@ on(name, listener) { |
@@ -0,36 +1,35 @@ | ||
type SignalListener = (signalArg: any, sinalName: string) => (void | boolean) | ||
class Signals { | ||
listeners = { | ||
// signalName: [listeners], | ||
}; | ||
listeners: { | ||
[signalName: string]: SignalListener[], | ||
} = {} | ||
constructor () { | ||
} | ||
on (name, listener) { | ||
on (name: string, listener: SignalListener) { | ||
if (!this.listeners[name]) { | ||
this.listeners[name] = [listener]; | ||
return; | ||
this.listeners[name] = [listener] | ||
return | ||
} | ||
this.listeners[name].push(listener); | ||
this.listeners[name].push(listener) | ||
} | ||
off (name, listener) { | ||
if (!this.listeners[name]) { return; } | ||
off (name: string, listener: SignalListener) { | ||
if (!this.listeners[name]) { return } | ||
const index = this.listeners[name].indexOf(listener); | ||
const index = this.listeners[name].indexOf(listener) | ||
if (index !== -1) { | ||
this.listeners[name].splice(index, 1); | ||
this.listeners[name].splice(index, 1) | ||
} | ||
} | ||
fire (name, arg): void | false { | ||
const targetListeners = this.listeners[name]; | ||
fire (name: string, arg: any): void | false { | ||
const targetListeners = this.listeners[name] | ||
if (!targetListeners) { return; } | ||
if (!targetListeners) { return } | ||
for (const listener of targetListeners) { | ||
if (listener(arg, name) === false) { | ||
return false; | ||
return false | ||
} | ||
@@ -41,2 +40,2 @@ } | ||
export default Signals; | ||
export default Signals |
@@ -1,12 +0,8 @@ | ||
declare const _default: (grid: (import("../../interactjs/types").Point & { | ||
declare function createGrid(grid: (Interact.Rect | Interact.Point) & { | ||
range?: number; | ||
limits: import("../../interactjs/types").Rect; | ||
offset: import("../../interactjs/types").Point; | ||
}) | (import("../../interactjs/types").Rect & { | ||
range?: number; | ||
limits: import("../../interactjs/types").Rect; | ||
offset: import("../../interactjs/types").Point; | ||
})) => (x: any, y: any) => { | ||
limits: Interact.Rect; | ||
offset: Interact.Point; | ||
}): (x: any, y: any) => { | ||
range: number; | ||
}; | ||
export default _default; | ||
export default createGrid; |
@@ -1,2 +0,2 @@ | ||
export default (grid) => { | ||
function createGrid(grid) { | ||
const coordFields = [ | ||
@@ -24,3 +24,4 @@ ['x', 'y'], | ||
}; | ||
}; | ||
} | ||
export default createGrid; | ||
//# sourceMappingURL=grid.js.map |
@@ -1,2 +0,2 @@ | ||
export default ( grid: (Interact.Rect | Interact.Point) & { range?: number, limits: Interact.Rect, offset: Interact.Point }) => { | ||
function createGrid (grid: (Interact.Rect | Interact.Point) & { range?: number, limits: Interact.Rect, offset: Interact.Point }) { | ||
const coordFields = [ | ||
@@ -7,3 +7,3 @@ ['x', 'y'], | ||
['width', 'height'], | ||
].filter(([xField, yField]) => xField in grid || yField in grid); | ||
].filter(([xField, yField]) => xField in grid || yField in grid) | ||
@@ -20,16 +20,18 @@ return function (x, y) { | ||
offset = { x: 0, y: 0 }, | ||
} = grid; | ||
} = grid | ||
const result = { range }; | ||
const result = { range } | ||
for (const [xField, yField] of coordFields) { | ||
const gridx = Math.round((x - offset.x) / grid[xField]); | ||
const gridy = Math.round((y - offset.y) / grid[yField]); | ||
const gridx = Math.round((x - offset.x) / grid[xField]) | ||
const gridy = Math.round((y - offset.y) / grid[yField]) | ||
result[xField] = Math.max(limits.left, Math.min(limits.right , gridx * grid[xField] + offset.x)); | ||
result[yField] = Math.max(limits.top, Math.min(limits.bottom , gridy * grid[yField] + offset.y)); | ||
result[xField] = Math.max(limits.left, Math.min(limits.right, gridx * grid[xField] + offset.x)) | ||
result[yField] = Math.max(limits.top, Math.min(limits.bottom, gridy * grid[yField] + offset.y)) | ||
} | ||
return result; | ||
}; | ||
}; | ||
return result | ||
} | ||
} | ||
export default createGrid |
import grid from './grid'; | ||
export { grid, }; | ||
export { grid }; |
import grid from './grid'; | ||
export { grid, }; | ||
export { grid }; | ||
//# sourceMappingURL=index.js.map |
@@ -1,4 +0,3 @@ | ||
import grid from './grid'; | ||
import grid from './grid' | ||
export { grid, }; | ||
export { grid } |
@@ -1,8 +0,8 @@ | ||
import test from '@interactjs/_dev/test/test'; | ||
import normalizeListeners from '../normalizeListeners'; | ||
import test from '@interactjs/_dev/test/test' | ||
import normalizeListeners from '../normalizeListeners' | ||
test('utils/normalizeListeners', t => { | ||
const a = () => {}; | ||
const b = () => {}; | ||
const c = () => {}; | ||
const a = () => {} | ||
const b = () => {} | ||
const c = () => {} | ||
@@ -14,3 +14,3 @@ t.deepEqual( | ||
}, | ||
'single type, single listener function'); | ||
'single type, single listener function') | ||
@@ -23,3 +23,3 @@ t.deepEqual( | ||
}, | ||
'multiple types, single listener function'); | ||
'multiple types, single listener function') | ||
@@ -29,3 +29,3 @@ t.deepEqual( | ||
normalizeListeners(['type1', 'type2'], a), | ||
'array of types equivalent to space separated string'); | ||
'array of types equivalent to space separated string') | ||
@@ -37,3 +37,3 @@ t.deepEqual( | ||
}, | ||
'single type, multiple listener functions'); | ||
'single type, multiple listener functions') | ||
@@ -46,3 +46,3 @@ t.deepEqual( | ||
}, | ||
'single type prefix, object of { suffix: [fn, ...] }'); | ||
'single type prefix, object of { suffix: [fn, ...] }') | ||
@@ -57,3 +57,3 @@ t.deepEqual( | ||
}, | ||
'multiple type prefixes, single length array of { suffix: [fn, ...] }'); | ||
'multiple type prefixes, single length array of { suffix: [fn, ...] }') | ||
@@ -66,3 +66,3 @@ t.deepEqual( | ||
}, | ||
'object of { suffix: [fn, ...] } as type arg'); | ||
'object of { suffix: [fn, ...] } as type arg') | ||
@@ -76,3 +76,3 @@ t.deepEqual( | ||
}, | ||
'object of { "suffix1 suffix2": [fn, ...], ... } as type arg'); | ||
'object of { "suffix1 suffix2": [fn, ...], ... } as type arg') | ||
@@ -86,5 +86,5 @@ t.deepEqual( | ||
}, | ||
'single type prefix, object of { "suffix1 suffix2": [fn, ...], ... }'); | ||
'single type prefix, object of { "suffix1 suffix2": [fn, ...], ... }') | ||
t.end(); | ||
}); | ||
t.end() | ||
}) |
@@ -14,5 +14,5 @@ import isWindow from './isWindow'; | ||
// check if it's wrapped by a polyfill | ||
if (el.ownerDocument !== window.document | ||
&& typeof window.wrap === 'function' | ||
&& window.wrap(el) === el) { | ||
if (el.ownerDocument !== window.document && | ||
typeof window.wrap === 'function' && | ||
window.wrap(el) === el) { | ||
// use wrapped window | ||
@@ -19,0 +19,0 @@ window = window.wrap(window); |
@@ -1,2 +0,2 @@ | ||
import isWindow from './isWindow'; | ||
import isWindow from './isWindow' | ||
@@ -8,3 +8,3 @@ const win = { | ||
init, | ||
}; | ||
} | ||
@@ -14,24 +14,24 @@ export function init (window) { | ||
win.realWindow = window; | ||
win.realWindow = window | ||
// create a TextNode | ||
const el = window.document.createTextNode(''); | ||
const el = window.document.createTextNode('') | ||
// check if it's wrapped by a polyfill | ||
if (el.ownerDocument !== window.document | ||
&& typeof window.wrap === 'function' | ||
&& window.wrap(el) === el) { | ||
if (el.ownerDocument !== window.document && | ||
typeof window.wrap === 'function' && | ||
window.wrap(el) === el) { | ||
// use wrapped window | ||
window = window.wrap(window); | ||
window = window.wrap(window) | ||
} | ||
win.window = window; | ||
win.window = window | ||
} | ||
if (typeof window === 'undefined') { | ||
win.window = undefined; | ||
win.realWindow = undefined; | ||
win.window = undefined | ||
win.realWindow = undefined | ||
} | ||
else { | ||
init(window); | ||
init(window) | ||
} | ||
@@ -41,12 +41,12 @@ | ||
if (isWindow(node)) { | ||
return node; | ||
return node | ||
} | ||
const rootNode = (node.ownerDocument || node); | ||
const rootNode = (node.ownerDocument || node) | ||
return rootNode.defaultView || win.window; | ||
return rootNode.defaultView || win.window | ||
} | ||
win.init = init; | ||
win.init = init | ||
export default win; | ||
export default win |
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
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
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
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
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
136848
91
2775