From 954e1dfbb3744bcbc3c08ba847c2285edaba62a8 Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Tue, 2 May 2017 21:58:10 -0500 Subject: [PATCH] chore(gestures): update gestures --- src/components/gesture/gesture.ts | 2 +- src/util/helpers.ts | 110 ++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 src/util/helpers.ts diff --git a/src/components/gesture/gesture.ts b/src/components/gesture/gesture.ts index c02395440a..3c96e95a5c 100644 --- a/src/components/gesture/gesture.ts +++ b/src/components/gesture/gesture.ts @@ -1,4 +1,4 @@ -import { applyStyles, getElementReference, pointerCoordX, pointerCoordY } from '../../util/dom'; +import { applyStyles, getElementReference, pointerCoordX, pointerCoordY } from '../../util/helpers'; import { Component, Listen, Ionic, Prop } from '../index'; import { GestureCallback, GestureDetail } from '../../util/interfaces'; import { GestureController, GestureDelegate } from './gesture-controller'; diff --git a/src/util/helpers.ts b/src/util/helpers.ts new file mode 100644 index 0000000000..9dadcf6c03 --- /dev/null +++ b/src/util/helpers.ts @@ -0,0 +1,110 @@ + +export function isDef(s: any): boolean { return s !== undefined && s !== null; } + +export function isUndef(s: any): boolean { return s === undefined; } + +export function isArray(val: any): val is Array { return (!!val) && (val.constructor === Array); } + +export function isObject(val: any): val is Object { return typeof val === 'object'; } + +export function isBoolean(val: any): val is (boolean) { return typeof val === 'boolean'; } + +export function isString(val: any): val is (string) { return typeof val === 'string'; } + +export function isNumber(val: any): val is (number) { return typeof val === 'number'; } + +export function isFunction(val: any): val is (Function) { return typeof val === 'function'; } + +export function isStringOrNumber(s: any): s is (string | number) { + return isString(s) || isNumber(s); +} + +export function toCamelCase(str: string) { + return str.replace(/-([a-z])/g, (g) => g[1].toUpperCase()); +} + +export function toDashCase(str: string) { + return str.replace(/([A-Z])/g, (g) => '-' + g[0].toLowerCase()); +} + +export function noop() {} + +export function pointerCoordX(ev: any): number { + // get X coordinates for either a mouse click + // or a touch depending on the given event + if (ev) { + var changedTouches = ev.changedTouches; + if (changedTouches && changedTouches.length > 0) { + return changedTouches[0].clientX; + } + if (ev.pageX !== undefined) { + return ev.pageX; + } + } + return 0; +} + +export function pointerCoordY(ev: any): number { + // get Y coordinates for either a mouse click + // or a touch depending on the given event + if (ev) { + var changedTouches = ev.changedTouches; + if (changedTouches && changedTouches.length > 0) { + return changedTouches[0].clientY; + } + if (ev.pageY !== undefined) { + return ev.pageY; + } + } + return 0; +} + +export function getElementReference(elm: any, ref: string) { + if (ref === 'child') { + return elm.firstElementChild; + } + if (ref === 'parent') { + if (elm.parentElement ) { + // normal element with a parent element + return elm.parentElement; + } + if (elm.parentNode && elm.parentNode.host) { + // shadow dom's document fragment + return elm.parentNode.host; + } + } + if (ref === 'body') { + return elm.ownerDocument.body; + } + if (ref === 'document') { + return elm.ownerDocument; + } + if (ref === 'window') { + return elm.ownerDocument.defaultView; + } + return elm; +} + +export function getKeyCodeByName(keyName: string) { + if (keyName === 'enter') { + return 13; + } + if (keyName === 'escape') { + return 27; + } + if (keyName === 'space') { + return 32; + } + if (keyName === 'tab') { + return 9; + } + return null; +} + +export function applyStyles(elm: HTMLElement, styles: {[styleProp: string]: string|number}) { + const styleProps = Object.keys(styles); + + for (var i = 0; i < styleProps.length; i++) { + (elm.style)[styleProps[i]] = styles[styleProps[i]]; + } +}