From 2c1cdce988cc56edde20ed446d8652bc7aba34f1 Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Tue, 13 Sep 2016 15:17:55 -0500 Subject: [PATCH] style(util): tslint updates --- src/util.ts | 4 --- src/util/datetime-util.ts | 4 +-- src/util/dom.ts | 54 +++++++++++++++++----------------- src/util/events.ts | 62 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 34 deletions(-) delete mode 100644 src/util.ts diff --git a/src/util.ts b/src/util.ts deleted file mode 100644 index f7cee16882..0000000000 --- a/src/util.ts +++ /dev/null @@ -1,4 +0,0 @@ -import * as domUtil from './util/dom'; -export const dom = domUtil; -export * from './util/util'; -export * from './util/datetime-util'; diff --git a/src/util/datetime-util.ts b/src/util/datetime-util.ts index a90a5fa966..264390da37 100644 --- a/src/util/datetime-util.ts +++ b/src/util/datetime-util.ts @@ -1,4 +1,4 @@ -import { isBlank, isPresent, isString, isObject, assign } from './util'; +import { assign, isBlank, isPresent, isString } from './util'; export function renderDateTime(template: string, value: DateTimeData, locale: LocaleData) { @@ -466,8 +466,6 @@ const FORMAT_KEYS = [ { f: FORMAT_a, k: 'ampm' }, ]; -const FORMAT_REGEX = /(\[[^\[]*\])|(\\)?([Hh]mm(ss)?|Mo|MM?M?M?|DD?D?D?|ddd?d?|YYYY|YY|a|A|hh?|HH?|mm?|ss?|.)/g; - const DAY_NAMES = [ 'Sunday', 'Monday', diff --git a/src/util/dom.ts b/src/util/dom.ts index 11b5b174e2..9e4022cc71 100644 --- a/src/util/dom.ts +++ b/src/util/dom.ts @@ -81,7 +81,7 @@ export let CSS: { '-moz-transform', 'moz-transform', 'MozTransform', 'mozTransform', 'msTransform']; for (i = 0; i < keys.length; i++) { - if (document.documentElement.style[keys[i]] !== undefined) { + if ((document.documentElement.style)[keys[i]] !== undefined) { CSS.transform = keys[i]; break; } @@ -90,7 +90,7 @@ export let CSS: { // transition keys = ['webkitTransition', 'mozTransition', 'msTransition', 'transition']; for (i = 0; i < keys.length; i++) { - if (document.documentElement.style[keys[i]] !== undefined) { + if ((document.documentElement.style)[keys[i]] !== undefined) { CSS.transition = keys[i]; break; } @@ -188,7 +188,7 @@ export function windowLoad(callback?: Function) { } } -export function pointerCoord(ev: any): Coordinates { +export function pointerCoord(ev: any): PointerCoordinates { // get coordinates for either a mouse click // or a touch depending on the given event let c = { x: 0, y: 0 }; @@ -203,7 +203,7 @@ export function pointerCoord(ev: any): Coordinates { return c; } -export function hasPointerMoved(threshold: number, startCoord: Coordinates, endCoord: Coordinates) { +export function hasPointerMoved(threshold: number, startCoord: PointerCoordinates, endCoord: PointerCoordinates) { let deltaX = (startCoord.x - endCoord.x); let deltaY = (startCoord.y - endCoord.y); let distance = deltaX * deltaX + deltaY * deltaY; @@ -247,30 +247,30 @@ export function copyInputAttributes(srcElement: HTMLElement, destElement: HTMLEl } } -let matchesFn: string; -let matchesMethods = ['matches', 'webkitMatchesSelector', 'mozMatchesSelector', 'msMatchesSelector']; -matchesMethods.some((fn: string) => { - if (typeof document.documentElement[fn] === 'function') { - matchesFn = fn; - return true; - } -}); - -export function closest(ele: HTMLElement, selector: string, checkSelf?: boolean) { - if (ele && matchesFn) { - - // traverse parents - ele = (checkSelf ? ele : ele.parentElement); - - while (ele !== null) { - if (ele[matchesFn](selector)) { - return ele; - } - ele = ele.parentElement; +// TODO: Add to external polyfill script +if (typeof Element.prototype.matches !== 'function') { + Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector || function matches(selector) { + var element = this; + var elements = (element.document || element.ownerDocument).querySelectorAll(selector); + var index = 0; + while (elements[index] && elements[index] !== element) { + ++index; } - } + return Boolean(elements[index]); + }; +} - return null; +if (typeof Element.prototype.closest !== 'function') { + Element.prototype.closest = function closest(selector) { + var element = this; + while (element && element.nodeType === 1) { + if (element.matches(selector)) { + return element; + } + element = element.parentNode; + } + return null; + }; } @@ -328,7 +328,7 @@ export function flushDimensionCache() { let dimensionCache: any = {}; -export interface Coordinates { +export interface PointerCoordinates { x?: number; y?: number; } diff --git a/src/util/events.ts b/src/util/events.ts index 08d88cb163..74503d6700 100644 --- a/src/util/events.ts +++ b/src/util/events.ts @@ -1,3 +1,9 @@ +import { APP_INITIALIZER } from '@angular/core'; + +import { nativeTimeout } from '../util/dom'; +import { Platform } from '../platform/platform'; +import { ScrollView } from '../util/scroll-view'; + /** * @name Events * @description @@ -101,3 +107,59 @@ export class Events { return responses; } } + +export function setupEvents(platform: Platform): Events { + const events = new Events(); + + // start listening for resizes XXms after the app starts + nativeTimeout(() => { + window.addEventListener('online', (ev) => { + events.publish('app:online', ev); + }, false); + + window.addEventListener('offline', (ev) => { + events.publish('app:offline', ev); + }, false); + + window.addEventListener('orientationchange', (ev) => { + events.publish('app:rotated', ev); + }); + + // When that status taps, we respond + window.addEventListener('statusTap', (ev) => { + // TODO: Make this more better + let el = document.elementFromPoint(platform.width() / 2, platform.height() / 2); + if (!el) { return; } + + let content = el.closest('.scroll-content'); + if (content) { + var scroll = new ScrollView(content); + scroll.scrollTo(0, 0, 300); + } + }); + + window.addEventListener('resize', () => { + platform.windowResize(); + }); + + }, 2000); + + return events; +} + +export function setupProvideEvents(platform: Platform) { + return function() { + return setupEvents(platform); + }; +} + +export function provideEvents() { + return { + provide: APP_INITIALIZER, + useFactory: setupProvideEvents, + deps: [ + Platform + ], + multi: true + }; +}