mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
Revert "Revert "Merge branch 'master' of https://github.com/driftyco/ionic""
This reverts commit 4a6086c1f8.
This commit is contained in:
@@ -190,8 +190,10 @@ export function pointerCoord(ev: any): Coordinates {
|
||||
}
|
||||
|
||||
export function hasPointerMoved(threshold: number, startCoord: Coordinates, endCoord: Coordinates) {
|
||||
return startCoord && endCoord &&
|
||||
(Math.abs(startCoord.x - endCoord.x) > threshold || Math.abs(startCoord.y - endCoord.y) > threshold);
|
||||
let deltaX = (startCoord.x - endCoord.x);
|
||||
let deltaY = (startCoord.y - endCoord.y);
|
||||
let distance = deltaX * deltaX + deltaY * deltaY;
|
||||
return distance > (threshold * threshold);
|
||||
}
|
||||
|
||||
export function isActive(ele: HTMLElement) {
|
||||
|
||||
171
src/util/mock-providers.ts
Normal file
171
src/util/mock-providers.ts
Normal file
@@ -0,0 +1,171 @@
|
||||
import { ChangeDetectorRef, ElementRef, NgZone, Renderer } from '@angular/core';
|
||||
import { Location } from '@angular/common';
|
||||
|
||||
import { App, Config, Form, GestureController, Keyboard, MenuController, NavOptions, Platform, Tab, Tabs, Transition, ViewController } from '../../src';
|
||||
import { NavControllerBase } from '../../src/components/nav/nav-controller-base';
|
||||
|
||||
|
||||
export const mockConfig = function(config?: any) {
|
||||
return new Config(config);
|
||||
};
|
||||
|
||||
export const mockPlatform = function(platforms?: string[]) {
|
||||
return new Platform(platforms);
|
||||
};
|
||||
|
||||
export const mockApp = function(config?: Config, platform?: Platform) {
|
||||
config = config || mockConfig();
|
||||
platform = platform || mockPlatform();
|
||||
return new App(config, platform);
|
||||
};
|
||||
|
||||
export const mockZone = function(): NgZone {
|
||||
let zone: any = {
|
||||
run: function(cb: any) {
|
||||
cb();
|
||||
},
|
||||
runOutsideAngular: function(cb: any) {
|
||||
cb();
|
||||
}
|
||||
};
|
||||
return zone;
|
||||
};
|
||||
|
||||
export const mockChangeDetectorRef = function(): ChangeDetectorRef {
|
||||
let cd: any = {
|
||||
reattach: () => {},
|
||||
detach: () => {}
|
||||
};
|
||||
return cd;
|
||||
};
|
||||
|
||||
export const mockElementRef = function(): ElementRef {
|
||||
return {
|
||||
nativeElement: document.createElement('div')
|
||||
};
|
||||
};
|
||||
|
||||
export const mockRenderer = function(): Renderer {
|
||||
let renderer: any = {
|
||||
setElementAttribute: () => {},
|
||||
setElementClass: () => {},
|
||||
setElementStyle: () => {}
|
||||
};
|
||||
return renderer;
|
||||
};
|
||||
|
||||
export const mockLocation = function(): Location {
|
||||
let location: any = {
|
||||
path: () => { return ''; },
|
||||
subscribe: () => {},
|
||||
go: () => {},
|
||||
back: () => {}
|
||||
};
|
||||
return location;
|
||||
};
|
||||
|
||||
export const mockTransition = function(playCallback: Function, duration: number) {
|
||||
return function _createTrans(enteringView: ViewController, leavingView: ViewController, transitionOpts: any): Transition {
|
||||
let transition: any = {
|
||||
play: () => {
|
||||
playCallback();
|
||||
},
|
||||
getDuration: () => { return duration; },
|
||||
onFinish: () => {}
|
||||
};
|
||||
return transition;
|
||||
};
|
||||
};
|
||||
|
||||
export const mockNavController = function(): NavControllerBase {
|
||||
let platform = mockPlatform();
|
||||
|
||||
let config = mockConfig();
|
||||
config.setPlatform(platform);
|
||||
|
||||
let app = mockApp(config, platform);
|
||||
|
||||
let form = new Form();
|
||||
|
||||
let zone = mockZone();
|
||||
|
||||
let keyboard = new Keyboard(config, form, zone);
|
||||
|
||||
let elementRef = mockElementRef();
|
||||
|
||||
let renderer = mockRenderer();
|
||||
|
||||
let compiler: any = null;
|
||||
|
||||
let gestureCtrl = new GestureController(app);
|
||||
|
||||
let location = mockLocation();
|
||||
|
||||
return new NavControllerBase(
|
||||
null,
|
||||
app,
|
||||
config,
|
||||
keyboard,
|
||||
elementRef,
|
||||
zone,
|
||||
renderer,
|
||||
compiler,
|
||||
gestureCtrl
|
||||
);
|
||||
};
|
||||
|
||||
export const mockTab = function(parentTabs: Tabs): Tab {
|
||||
let platform = mockPlatform();
|
||||
|
||||
let config = mockConfig();
|
||||
config.setPlatform(platform);
|
||||
|
||||
let app = (<any>parentTabs)._app || mockApp(config, platform);
|
||||
|
||||
let form = new Form();
|
||||
|
||||
let zone = mockZone();
|
||||
|
||||
let keyboard = new Keyboard(config, form, zone);
|
||||
|
||||
let elementRef = mockElementRef();
|
||||
|
||||
let renderer = mockRenderer();
|
||||
|
||||
let changeDetectorRef = mockChangeDetectorRef();
|
||||
|
||||
let compiler: any = null;
|
||||
|
||||
let gestureCtrl = new GestureController(app);
|
||||
|
||||
let location = mockLocation();
|
||||
|
||||
let tab = new Tab(
|
||||
parentTabs,
|
||||
app,
|
||||
config,
|
||||
keyboard,
|
||||
elementRef,
|
||||
zone,
|
||||
renderer,
|
||||
compiler,
|
||||
changeDetectorRef,
|
||||
gestureCtrl
|
||||
);
|
||||
|
||||
tab.load = (opts: any, cb: Function) => {
|
||||
cb();
|
||||
};
|
||||
|
||||
return tab;
|
||||
};
|
||||
|
||||
export const mockTabs = function(app?: App): Tabs {
|
||||
let config = mockConfig();
|
||||
let platform = mockPlatform();
|
||||
app = app || mockApp(config, platform);
|
||||
let elementRef = mockElementRef();
|
||||
let renderer = mockRenderer();
|
||||
|
||||
return new Tabs(null, null, app, config, elementRef, platform, renderer);
|
||||
};
|
||||
@@ -1,7 +1,14 @@
|
||||
import {ElementRef} from '@angular/core';
|
||||
|
||||
|
||||
|
||||
export interface PointerEventsConfig {
|
||||
element?: HTMLElement;
|
||||
elementRef?: ElementRef;
|
||||
pointerDown: (ev: any) => boolean;
|
||||
pointerMove: (ev: any) => void;
|
||||
pointerUp: (ev: any) => void;
|
||||
nativeOptions?: any;
|
||||
zone?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
@@ -10,11 +17,15 @@ export class PointerEvents {
|
||||
private rmTouchStart: Function = null;
|
||||
private rmTouchMove: Function = null;
|
||||
private rmTouchEnd: Function = null;
|
||||
private rmTouchCancel: Function = null;
|
||||
|
||||
private rmMouseStart: Function = null;
|
||||
private rmMouseMove: Function = null;
|
||||
private rmMouseUp: Function = null;
|
||||
|
||||
private bindTouchEnd: Function;
|
||||
private bindMouseUp: Function;
|
||||
|
||||
private lastTouchEvent: number = 0;
|
||||
|
||||
mouseWait: number = 2 * 1000;
|
||||
@@ -24,10 +35,14 @@ export class PointerEvents {
|
||||
private pointerMove: any,
|
||||
private pointerUp: any,
|
||||
private zone: boolean,
|
||||
private option: any) {
|
||||
private option: any
|
||||
) {
|
||||
|
||||
this.rmTouchStart = listenEvent(ele, 'touchstart', zone, option, (ev: any) => this.handleTouchStart(ev));
|
||||
this.rmMouseStart = listenEvent(ele, 'mousedown', zone, option, (ev: any) => this.handleMouseDown(ev));
|
||||
this.bindTouchEnd = this.handleTouchEnd.bind(this);
|
||||
this.bindMouseUp = this.handleMouseUp.bind(this);
|
||||
|
||||
this.rmTouchStart = listenEvent(ele, 'touchstart', zone, option, this.handleTouchStart.bind(this));
|
||||
this.rmMouseStart = listenEvent(ele, 'mousedown', zone, option, this.handleMouseDown.bind(this));
|
||||
}
|
||||
|
||||
private handleTouchStart(ev: any) {
|
||||
@@ -39,7 +54,10 @@ export class PointerEvents {
|
||||
this.rmTouchMove = listenEvent(this.ele, 'touchmove', this.zone, this.option, this.pointerMove);
|
||||
}
|
||||
if (!this.rmTouchEnd) {
|
||||
this.rmTouchEnd = listenEvent(this.ele, 'touchend', this.zone, this.option, (ev: any) => this.handleTouchEnd(ev));
|
||||
this.rmTouchEnd = listenEvent(this.ele, 'touchend', this.zone, this.option, this.bindTouchEnd);
|
||||
}
|
||||
if (!this.rmTouchCancel) {
|
||||
this.rmTouchCancel = listenEvent(this.ele, 'touchcancel', this.zone, this.option, this.bindTouchEnd);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,40 +73,43 @@ export class PointerEvents {
|
||||
this.rmMouseMove = listenEvent(window, 'mousemove', this.zone, this.option, this.pointerMove);
|
||||
}
|
||||
if (!this.rmMouseUp) {
|
||||
this.rmMouseUp = listenEvent(window, 'mouseup', this.zone, this.option, (ev: any) => this.handleMouseUp(ev));
|
||||
this.rmMouseUp = listenEvent(window, 'mouseup', this.zone, this.option, this.bindMouseUp);
|
||||
}
|
||||
}
|
||||
|
||||
private handleTouchEnd(ev: any) {
|
||||
this.rmTouchMove && this.rmTouchMove();
|
||||
this.rmTouchMove = null;
|
||||
this.rmTouchEnd && this.rmTouchEnd();
|
||||
this.rmTouchEnd = null;
|
||||
|
||||
this.stopTouch();
|
||||
this.pointerUp(ev);
|
||||
}
|
||||
|
||||
private handleMouseUp(ev: any) {
|
||||
this.rmMouseMove && this.rmMouseMove();
|
||||
this.rmMouseMove = null;
|
||||
this.rmMouseUp && this.rmMouseUp();
|
||||
this.rmMouseUp = null;
|
||||
|
||||
this.stopMouse();
|
||||
this.pointerUp(ev);
|
||||
}
|
||||
|
||||
stop() {
|
||||
private stopTouch() {
|
||||
this.rmTouchMove && this.rmTouchMove();
|
||||
this.rmTouchEnd && this.rmTouchEnd();
|
||||
this.rmTouchCancel && this.rmTouchCancel();
|
||||
|
||||
this.rmTouchMove = null;
|
||||
this.rmTouchEnd = null;
|
||||
this.rmTouchCancel = null;
|
||||
}
|
||||
|
||||
private stopMouse() {
|
||||
this.rmMouseMove && this.rmMouseMove();
|
||||
this.rmMouseUp && this.rmMouseUp();
|
||||
|
||||
this.rmMouseMove = null;
|
||||
this.rmMouseUp = null;
|
||||
}
|
||||
|
||||
stop() {
|
||||
this.stopTouch();
|
||||
this.stopMouse();
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this.rmTouchStart && this.rmTouchStart();
|
||||
this.rmTouchStart = null;
|
||||
@@ -120,21 +141,26 @@ export class UIEventManager {
|
||||
return this.listen(ref.nativeElement, eventName, callback, option);
|
||||
}
|
||||
|
||||
pointerEventsRef(ref: ElementRef, pointerStart: any, pointerMove: any, pointerEnd: any, option?: any): PointerEvents {
|
||||
return this.pointerEvents(ref.nativeElement, pointerStart, pointerMove, pointerEnd, option);
|
||||
}
|
||||
|
||||
pointerEvents(element: any, pointerDown: any, pointerMove: any, pointerUp: any, option: any = false): PointerEvents {
|
||||
pointerEvents(config: PointerEventsConfig): PointerEvents {
|
||||
let element = config.element;
|
||||
if (!element) {
|
||||
element = config.elementRef.nativeElement;
|
||||
}
|
||||
|
||||
if (!element || !config.pointerDown || !config.pointerMove || !config.pointerUp) {
|
||||
console.error('PointerEvents config is invalid');
|
||||
return;
|
||||
}
|
||||
let zone = config.zone || this.zoneWrapped;
|
||||
let options = config.nativeOptions || false;
|
||||
|
||||
let submanager = new PointerEvents(
|
||||
element,
|
||||
pointerDown,
|
||||
pointerMove,
|
||||
pointerUp,
|
||||
this.zoneWrapped,
|
||||
option);
|
||||
config.pointerDown,
|
||||
config.pointerMove,
|
||||
config.pointerUp,
|
||||
zone,
|
||||
options);
|
||||
|
||||
let removeFunc = () => submanager.destroy();
|
||||
this.events.push(removeFunc);
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
|
||||
export function noop() {}
|
||||
|
||||
/**
|
||||
* Given a min and max, restrict the given number
|
||||
* to the range.
|
||||
|
||||
Reference in New Issue
Block a user