mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-20 04:14:21 +08:00
refactor(angular): update alert provider to support angular 3.x api
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { AlertOptions } from '@ionic/core';
|
||||
import { AlertDismissEvent, AlertOptions } from '@ionic/core';
|
||||
|
||||
import { ensureElementInBody, hydrateElement } from '../util/util';
|
||||
|
||||
@ -7,7 +7,7 @@ let alertId = 0;
|
||||
|
||||
@Injectable()
|
||||
export class AlertController {
|
||||
create(opts?: AlertOptions): any {
|
||||
create(opts?: AlertOptions): AlertProxy {
|
||||
return getAlertProxy(opts);
|
||||
}
|
||||
}
|
||||
@ -18,24 +18,55 @@ export function getAlertProxy(opts: AlertOptions){
|
||||
state: PRESENTING,
|
||||
opts: opts,
|
||||
present: function() { return present(this)},
|
||||
dismiss: function() { return dismiss(this)}
|
||||
dismiss: function() { return dismiss(this)},
|
||||
onDidDismiss: function(callback: (data: any, role: string) => void) {
|
||||
(this as AlertProxyInternal).onDidDismissHandler = callback;
|
||||
},
|
||||
onWillDismiss: function(callback: (data: any, role: string) => void) {
|
||||
(this as AlertProxyInternal).onWillDismissHandler = callback;
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export function present(alertProxy: AlertProxyInternal): Promise<void> {
|
||||
export function present(alertProxy: AlertProxyInternal): Promise<any> {
|
||||
alertProxy.state = PRESENTING;
|
||||
return loadOverlay(alertProxy.opts).then((alertElement: HTMLIonAlertElement) => {
|
||||
alertProxy.element = alertElement;
|
||||
|
||||
const onDidDismissHandler = (event: AlertDismissEvent) => {
|
||||
alertElement.removeEventListener(ION_ALERT_DID_DISMISS_EVENT, onDidDismissHandler);
|
||||
if (alertProxy.onDidDismissHandler) {
|
||||
alertProxy.onDidDismissHandler(event.detail.data, event.detail.role);
|
||||
}
|
||||
};
|
||||
|
||||
const onWillDismissHandler = (event: AlertDismissEvent) => {
|
||||
alertElement.removeEventListener(ION_ALERT_WILL_DISMISS_EVENT, onWillDismissHandler);
|
||||
if (alertProxy.onWillDismissHandler) {
|
||||
alertProxy.onWillDismissHandler(event.detail.data, event.detail.role);
|
||||
}
|
||||
};
|
||||
|
||||
alertElement.addEventListener(ION_ALERT_DID_DISMISS_EVENT, onDidDismissHandler);
|
||||
alertElement.addEventListener(ION_ALERT_WILL_DISMISS_EVENT, onWillDismissHandler);
|
||||
|
||||
if (alertProxy.state === PRESENTING) {
|
||||
return alertElement.present();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function dismiss(alertProxy: AlertProxyInternal): Promise<void> {
|
||||
return loadOverlay(alertProxy.opts).then((alertElement: HTMLIonAlertElement) => {
|
||||
export function dismiss(alertProxy: AlertProxyInternal): Promise<any> {
|
||||
alertProxy.state = DISMISSING;
|
||||
if (alertProxy.element) {
|
||||
if (alertProxy.state === DISMISSING) {
|
||||
return alertElement.dismiss();
|
||||
return alertProxy.element.dismiss();
|
||||
}
|
||||
});
|
||||
}
|
||||
// either we're not in the dismissing state
|
||||
// or we're calling this before the element is created
|
||||
// so just return a resolved promise
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
export function loadOverlay(opts: AlertOptions) {
|
||||
@ -48,13 +79,21 @@ export function loadOverlay(opts: AlertOptions) {
|
||||
export interface AlertProxy {
|
||||
present(): Promise<void>
|
||||
dismiss(): Promise<void>
|
||||
onDidDismiss(callback: (data: any, role: string) => void): void;
|
||||
onWillDismiss(callback: (data: any, role: string) => void): void;
|
||||
}
|
||||
|
||||
export interface AlertProxyInternal extends AlertProxy {
|
||||
id: number;
|
||||
opts: AlertOptions;
|
||||
state: number;
|
||||
element: HTMLIonAlertElement;
|
||||
onDidDismissHandler?: (data: any, role: string) => void;
|
||||
onWillDismissHandler?: (data: any, role: string) => void;
|
||||
}
|
||||
|
||||
export const PRESENTING = 1;
|
||||
export const DISMISSING = 2;
|
||||
export const DISMISSING = 2;
|
||||
|
||||
const ION_ALERT_DID_DISMISS_EVENT = 'ionAlertDidDismiss';
|
||||
const ION_ALERT_WILL_DISMISS_EVENT = 'ionAlertWillDismiss';
|
Reference in New Issue
Block a user