Skip to content

Commit

Permalink
fix(alert): disable listeners until ready
Browse files Browse the repository at this point in the history
Closes #5821
  • Loading branch information
adamdbradley committed Mar 13, 2016
1 parent 355f6ee commit 5844703
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 20 deletions.
16 changes: 13 additions & 3 deletions ionic/components/action-sheet/action-sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ class ActionSheetCmp {
private descId: string;
private hdrId: string;
private id: number;
private created: number;

constructor(
private _viewCtrl: ViewController,
Expand All @@ -202,6 +203,7 @@ class ActionSheetCmp {
renderer: Renderer
) {
this.d = params.data;
this.created = Date.now();

if (this.d.cssClass) {
renderer.setElementClass(_elementRef.nativeElement, this.d.cssClass, true);
Expand Down Expand Up @@ -262,15 +264,19 @@ class ActionSheetCmp {

@HostListener('body:keyup', ['$event'])
private _keyUp(ev: KeyboardEvent) {
if (this._viewCtrl.isLast()) {
if (this.isEnabled() && this._viewCtrl.isLast()) {
if (ev.keyCode === 27) {
console.debug('actionsheet escape');
console.debug('actionsheet, escape button');
this.bdClick();
}
}
}

click(button, dismissDelay?) {
if (!this.isEnabled()) {
return;
}

let shouldDismiss = true;

if (button.handler) {
Expand All @@ -289,7 +295,7 @@ class ActionSheetCmp {
}

bdClick() {
if (this.d.enableBackdropDismiss) {
if (this.isEnabled() && this.d.enableBackdropDismiss) {
if (this.d.cancelButton) {
this.click(this.d.cancelButton, 1);

Expand All @@ -302,6 +308,10 @@ class ActionSheetCmp {
dismiss(role): Promise<any> {
return this._viewCtrl.dismiss(null, role);
}

isEnabled() {
return (this.created + 750 < Date.now());
}
}

export interface ActionSheetOptions {
Expand Down
44 changes: 29 additions & 15 deletions ionic/components/alert/alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,21 +311,22 @@ export class Alert extends ViewController {
directives: [NgClass, NgSwitch, NgIf, NgFor]
})
class AlertCmp {
activeId: string;
descId: string;
d: {
private activeId: string;
private descId: string;
private d: {
cssClass?: string;
message?: string;
subTitle?: string;
buttons?: any[];
inputs?: any[];
enableBackdropDismiss?: boolean;
};
hdrId: string;
id: number;
subHdrId: string;
msgId: string;
inputType: string;
private hdrId: string;
private id: number;
private subHdrId: string;
private msgId: string;
private inputType: string;
private created: number;

constructor(
private _viewCtrl: ViewController,
Expand All @@ -348,6 +349,7 @@ class AlertCmp {
this.subHdrId = 'alert-subhdr-' + this.id;
this.msgId = 'alert-msg-' + this.id;
this.activeId = '';
this.created = Date.now();

if (this.d.message) {
this.descId = this.msgId;
Expand Down Expand Up @@ -414,7 +416,7 @@ class AlertCmp {

@HostListener('body:keyup', ['$event'])
private _keyUp(ev: KeyboardEvent) {
if (this._viewCtrl.isLast()) {
if (this.isEnabled() && this._viewCtrl.isLast()) {
if (ev.keyCode === 13) {
console.debug('alert, enter button');
let button = this.d.buttons[this.d.buttons.length - 1];
Expand All @@ -440,6 +442,10 @@ class AlertCmp {
}

btnClick(button, dismissDelay?) {
if (!this.isEnabled()) {
return;
}

let shouldDismiss = true;

if (button.handler) {
Expand All @@ -459,18 +465,22 @@ class AlertCmp {
}

rbClick(checkedInput) {
this.d.inputs.forEach(input => {
input.checked = (checkedInput === input);
});
this.activeId = checkedInput.id;
if (this.isEnabled()) {
this.d.inputs.forEach(input => {
input.checked = (checkedInput === input);
});
this.activeId = checkedInput.id;
}
}

cbClick(checkedInput) {
checkedInput.checked = !checkedInput.checked;
if (this.isEnabled()) {
checkedInput.checked = !checkedInput.checked;
}
}

bdClick() {
if (this.d.enableBackdropDismiss) {
if (this.isEnabled() && this.d.enableBackdropDismiss) {
let cancelBtn = this.d.buttons.find(b => b.role === 'cancel');
if (cancelBtn) {
this.btnClick(cancelBtn, 1);
Expand Down Expand Up @@ -507,6 +517,10 @@ class AlertCmp {
});
return values;
}

isEnabled() {
return (this.created + 750 < Date.now());
}
}

export interface AlertOptions {
Expand Down
37 changes: 35 additions & 2 deletions ionic/components/alert/test/dismiss/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Alert, NavController, App, Page } from 'ionic-angular/index';
import { FORM_DIRECTIVES, FormBuilder, ControlGroup, Validators } from 'angular2/common';


@Page({
Expand Down Expand Up @@ -37,13 +38,45 @@ export class E2EPage {
<ion-title>Another Page</ion-title>
</ion-navbar>
<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>
`
})
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() {
this.showConfirm();
Expand Down

0 comments on commit 5844703

Please sign in to comment.