fix(alert): disable listeners until ready

Closes #5821
This commit is contained in:
Adam Bradley
2016-03-12 21:34:08 -06:00
parent 355f6ee145
commit 584470337a
3 changed files with 77 additions and 20 deletions

View File

@ -193,6 +193,7 @@ class ActionSheetCmp {
private descId: string; private descId: string;
private hdrId: string; private hdrId: string;
private id: number; private id: number;
private created: number;
constructor( constructor(
private _viewCtrl: ViewController, private _viewCtrl: ViewController,
@ -202,6 +203,7 @@ class ActionSheetCmp {
renderer: Renderer renderer: Renderer
) { ) {
this.d = params.data; this.d = params.data;
this.created = Date.now();
if (this.d.cssClass) { if (this.d.cssClass) {
renderer.setElementClass(_elementRef.nativeElement, this.d.cssClass, true); renderer.setElementClass(_elementRef.nativeElement, this.d.cssClass, true);
@ -262,15 +264,19 @@ class ActionSheetCmp {
@HostListener('body:keyup', ['$event']) @HostListener('body:keyup', ['$event'])
private _keyUp(ev: KeyboardEvent) { private _keyUp(ev: KeyboardEvent) {
if (this._viewCtrl.isLast()) { if (this.isEnabled() && this._viewCtrl.isLast()) {
if (ev.keyCode === 27) { if (ev.keyCode === 27) {
console.debug('actionsheet escape'); console.debug('actionsheet, escape button');
this.bdClick(); this.bdClick();
} }
} }
} }
click(button, dismissDelay?) { click(button, dismissDelay?) {
if (!this.isEnabled()) {
return;
}
let shouldDismiss = true; let shouldDismiss = true;
if (button.handler) { if (button.handler) {
@ -289,7 +295,7 @@ class ActionSheetCmp {
} }
bdClick() { bdClick() {
if (this.d.enableBackdropDismiss) { if (this.isEnabled() && this.d.enableBackdropDismiss) {
if (this.d.cancelButton) { if (this.d.cancelButton) {
this.click(this.d.cancelButton, 1); this.click(this.d.cancelButton, 1);
@ -302,6 +308,10 @@ class ActionSheetCmp {
dismiss(role): Promise<any> { dismiss(role): Promise<any> {
return this._viewCtrl.dismiss(null, role); return this._viewCtrl.dismiss(null, role);
} }
isEnabled() {
return (this.created + 750 < Date.now());
}
} }
export interface ActionSheetOptions { export interface ActionSheetOptions {

View File

@ -311,9 +311,9 @@ export class Alert extends ViewController {
directives: [NgClass, NgSwitch, NgIf, NgFor] directives: [NgClass, NgSwitch, NgIf, NgFor]
}) })
class AlertCmp { class AlertCmp {
activeId: string; private activeId: string;
descId: string; private descId: string;
d: { private d: {
cssClass?: string; cssClass?: string;
message?: string; message?: string;
subTitle?: string; subTitle?: string;
@ -321,11 +321,12 @@ class AlertCmp {
inputs?: any[]; inputs?: any[];
enableBackdropDismiss?: boolean; enableBackdropDismiss?: boolean;
}; };
hdrId: string; private hdrId: string;
id: number; private id: number;
subHdrId: string; private subHdrId: string;
msgId: string; private msgId: string;
inputType: string; private inputType: string;
private created: number;
constructor( constructor(
private _viewCtrl: ViewController, private _viewCtrl: ViewController,
@ -348,6 +349,7 @@ class AlertCmp {
this.subHdrId = 'alert-subhdr-' + this.id; this.subHdrId = 'alert-subhdr-' + this.id;
this.msgId = 'alert-msg-' + this.id; this.msgId = 'alert-msg-' + this.id;
this.activeId = ''; this.activeId = '';
this.created = Date.now();
if (this.d.message) { if (this.d.message) {
this.descId = this.msgId; this.descId = this.msgId;
@ -414,7 +416,7 @@ class AlertCmp {
@HostListener('body:keyup', ['$event']) @HostListener('body:keyup', ['$event'])
private _keyUp(ev: KeyboardEvent) { private _keyUp(ev: KeyboardEvent) {
if (this._viewCtrl.isLast()) { if (this.isEnabled() && this._viewCtrl.isLast()) {
if (ev.keyCode === 13) { if (ev.keyCode === 13) {
console.debug('alert, enter button'); console.debug('alert, enter button');
let button = this.d.buttons[this.d.buttons.length - 1]; let button = this.d.buttons[this.d.buttons.length - 1];
@ -440,6 +442,10 @@ class AlertCmp {
} }
btnClick(button, dismissDelay?) { btnClick(button, dismissDelay?) {
if (!this.isEnabled()) {
return;
}
let shouldDismiss = true; let shouldDismiss = true;
if (button.handler) { if (button.handler) {
@ -459,18 +465,22 @@ class AlertCmp {
} }
rbClick(checkedInput) { rbClick(checkedInput) {
this.d.inputs.forEach(input => { if (this.isEnabled()) {
input.checked = (checkedInput === input); this.d.inputs.forEach(input => {
}); input.checked = (checkedInput === input);
this.activeId = checkedInput.id; });
this.activeId = checkedInput.id;
}
} }
cbClick(checkedInput) { cbClick(checkedInput) {
checkedInput.checked = !checkedInput.checked; if (this.isEnabled()) {
checkedInput.checked = !checkedInput.checked;
}
} }
bdClick() { bdClick() {
if (this.d.enableBackdropDismiss) { if (this.isEnabled() && this.d.enableBackdropDismiss) {
let cancelBtn = this.d.buttons.find(b => b.role === 'cancel'); let cancelBtn = this.d.buttons.find(b => b.role === 'cancel');
if (cancelBtn) { if (cancelBtn) {
this.btnClick(cancelBtn, 1); this.btnClick(cancelBtn, 1);
@ -507,6 +517,10 @@ class AlertCmp {
}); });
return values; return values;
} }
isEnabled() {
return (this.created + 750 < Date.now());
}
} }
export interface AlertOptions { export interface AlertOptions {

View File

@ -1,4 +1,5 @@
import { Alert, NavController, App, Page } from 'ionic-angular/index'; import { Alert, NavController, App, Page } from 'ionic-angular/index';
import { FORM_DIRECTIVES, FormBuilder, ControlGroup, Validators } from 'angular2/common';
@Page({ @Page({
@ -37,13 +38,45 @@ export class E2EPage {
<ion-title>Another Page</ion-title> <ion-title>Another Page</ion-title>
</ion-navbar> </ion-navbar>
<ion-content padding> <ion-content padding>
Welcome! <form [ngFormModel]="form" (ngSubmit)="submit(form.value)">
<ion-list>
<ion-item [class.error]="!form.controls.name.valid && form.controls.name.touched">
<ion-label>Name</ion-label>
<ion-input type="text" [(ngFormControl)]="form.controls.name"></ion-input>
</ion-item>
</ion-list>
<div padding style="padding-top: 0 !important;">
<button list-item primary block>
Submit
</button>
</div>
</form>
</ion-content> </ion-content>
` `
}) })
class AnotherPage { class AnotherPage {
form: ControlGroup;
constructor(private nav: NavController) {} constructor(private nav: NavController, private builder: FormBuilder) {
this.form = builder.group({
name: builder.control('', Validators.compose([
Validators.required,
Validators.minLength(5)
]))
});
}
submit(value: any): void {
if (this.form.valid) {
console.log(value);
} else {
this.nav.present(Alert.create({
title: 'Invalid input data',
subTitle: "Please correct the errors and resubmit the data.",
buttons: [ 'OK' ]
}));
}
}
onPageDidEnter() { onPageDidEnter() {
this.showConfirm(); this.showConfirm();