Skip to content

Commit 5844703

Browse files
committed
fix(alert): disable listeners until ready
Closes #5821
1 parent 355f6ee commit 5844703

File tree

3 files changed

+77
-20
lines changed

3 files changed

+77
-20
lines changed

ionic/components/action-sheet/action-sheet.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ class ActionSheetCmp {
193193
private descId: string;
194194
private hdrId: string;
195195
private id: number;
196+
private created: number;
196197

197198
constructor(
198199
private _viewCtrl: ViewController,
@@ -202,6 +203,7 @@ class ActionSheetCmp {
202203
renderer: Renderer
203204
) {
204205
this.d = params.data;
206+
this.created = Date.now();
205207

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

263265
@HostListener('body:keyup', ['$event'])
264266
private _keyUp(ev: KeyboardEvent) {
265-
if (this._viewCtrl.isLast()) {
267+
if (this.isEnabled() && this._viewCtrl.isLast()) {
266268
if (ev.keyCode === 27) {
267-
console.debug('actionsheet escape');
269+
console.debug('actionsheet, escape button');
268270
this.bdClick();
269271
}
270272
}
271273
}
272274

273275
click(button, dismissDelay?) {
276+
if (!this.isEnabled()) {
277+
return;
278+
}
279+
274280
let shouldDismiss = true;
275281

276282
if (button.handler) {
@@ -289,7 +295,7 @@ class ActionSheetCmp {
289295
}
290296

291297
bdClick() {
292-
if (this.d.enableBackdropDismiss) {
298+
if (this.isEnabled() && this.d.enableBackdropDismiss) {
293299
if (this.d.cancelButton) {
294300
this.click(this.d.cancelButton, 1);
295301

@@ -302,6 +308,10 @@ class ActionSheetCmp {
302308
dismiss(role): Promise<any> {
303309
return this._viewCtrl.dismiss(null, role);
304310
}
311+
312+
isEnabled() {
313+
return (this.created + 750 < Date.now());
314+
}
305315
}
306316

307317
export interface ActionSheetOptions {

ionic/components/alert/alert.ts

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -311,21 +311,22 @@ export class Alert extends ViewController {
311311
directives: [NgClass, NgSwitch, NgIf, NgFor]
312312
})
313313
class AlertCmp {
314-
activeId: string;
315-
descId: string;
316-
d: {
314+
private activeId: string;
315+
private descId: string;
316+
private d: {
317317
cssClass?: string;
318318
message?: string;
319319
subTitle?: string;
320320
buttons?: any[];
321321
inputs?: any[];
322322
enableBackdropDismiss?: boolean;
323323
};
324-
hdrId: string;
325-
id: number;
326-
subHdrId: string;
327-
msgId: string;
328-
inputType: string;
324+
private hdrId: string;
325+
private id: number;
326+
private subHdrId: string;
327+
private msgId: string;
328+
private inputType: string;
329+
private created: number;
329330

330331
constructor(
331332
private _viewCtrl: ViewController,
@@ -348,6 +349,7 @@ class AlertCmp {
348349
this.subHdrId = 'alert-subhdr-' + this.id;
349350
this.msgId = 'alert-msg-' + this.id;
350351
this.activeId = '';
352+
this.created = Date.now();
351353

352354
if (this.d.message) {
353355
this.descId = this.msgId;
@@ -414,7 +416,7 @@ class AlertCmp {
414416

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

442444
btnClick(button, dismissDelay?) {
445+
if (!this.isEnabled()) {
446+
return;
447+
}
448+
443449
let shouldDismiss = true;
444450

445451
if (button.handler) {
@@ -459,18 +465,22 @@ class AlertCmp {
459465
}
460466

461467
rbClick(checkedInput) {
462-
this.d.inputs.forEach(input => {
463-
input.checked = (checkedInput === input);
464-
});
465-
this.activeId = checkedInput.id;
468+
if (this.isEnabled()) {
469+
this.d.inputs.forEach(input => {
470+
input.checked = (checkedInput === input);
471+
});
472+
this.activeId = checkedInput.id;
473+
}
466474
}
467475

468476
cbClick(checkedInput) {
469-
checkedInput.checked = !checkedInput.checked;
477+
if (this.isEnabled()) {
478+
checkedInput.checked = !checkedInput.checked;
479+
}
470480
}
471481

472482
bdClick() {
473-
if (this.d.enableBackdropDismiss) {
483+
if (this.isEnabled() && this.d.enableBackdropDismiss) {
474484
let cancelBtn = this.d.buttons.find(b => b.role === 'cancel');
475485
if (cancelBtn) {
476486
this.btnClick(cancelBtn, 1);
@@ -507,6 +517,10 @@ class AlertCmp {
507517
});
508518
return values;
509519
}
520+
521+
isEnabled() {
522+
return (this.created + 750 < Date.now());
523+
}
510524
}
511525

512526
export interface AlertOptions {

ionic/components/alert/test/dismiss/index.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Alert, NavController, App, Page } from 'ionic-angular/index';
2+
import { FORM_DIRECTIVES, FormBuilder, ControlGroup, Validators } from 'angular2/common';
23

34

45
@Page({
@@ -37,13 +38,45 @@ export class E2EPage {
3738
<ion-title>Another Page</ion-title>
3839
</ion-navbar>
3940
<ion-content padding>
40-
Welcome!
41+
<form [ngFormModel]="form" (ngSubmit)="submit(form.value)">
42+
<ion-list>
43+
<ion-item [class.error]="!form.controls.name.valid && form.controls.name.touched">
44+
<ion-label>Name</ion-label>
45+
<ion-input type="text" [(ngFormControl)]="form.controls.name"></ion-input>
46+
</ion-item>
47+
</ion-list>
48+
<div padding style="padding-top: 0 !important;">
49+
<button list-item primary block>
50+
Submit
51+
</button>
52+
</div>
53+
</form>
4154
</ion-content>
4255
`
4356
})
4457
class AnotherPage {
58+
form: ControlGroup;
4559

46-
constructor(private nav: NavController) {}
60+
constructor(private nav: NavController, private builder: FormBuilder) {
61+
this.form = builder.group({
62+
name: builder.control('', Validators.compose([
63+
Validators.required,
64+
Validators.minLength(5)
65+
]))
66+
});
67+
}
68+
69+
submit(value: any): void {
70+
if (this.form.valid) {
71+
console.log(value);
72+
} else {
73+
this.nav.present(Alert.create({
74+
title: 'Invalid input data',
75+
subTitle: "Please correct the errors and resubmit the data.",
76+
buttons: [ 'OK' ]
77+
}));
78+
}
79+
}
4780

4881
onPageDidEnter() {
4982
this.showConfirm();

0 commit comments

Comments
 (0)