From d26afdfea0c5f0ca1757c8f01ec75e44af067552 Mon Sep 17 00:00:00 2001 From: "Manu Mtz.-Almeida" Date: Fri, 23 Feb 2018 19:09:13 +0100 Subject: [PATCH] fix(overlays): prevent dismissing an overlay twice --- packages/core/package.json | 1 + .../src/components/action-sheet/action-sheet.tsx | 13 ++++++++++++- packages/core/src/components/alert/alert.tsx | 11 ++++++++++- packages/core/src/components/loading/loading.tsx | 11 ++++++++++- packages/core/src/components/modal/modal.tsx | 12 +++++++++++- packages/core/src/components/picker/picker.tsx | 12 +++++++++++- packages/core/src/components/popover/popover.tsx | 12 +++++++++++- packages/core/src/components/toast/toast.tsx | 13 ++++++++++++- 8 files changed, 78 insertions(+), 7 deletions(-) diff --git a/packages/core/package.json b/packages/core/package.json index fb8d475c2f..b58c2caf91 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -37,6 +37,7 @@ "deploy": "np --any-branch", "version": "npm run clean && stencil build --docs", "dev": "sd concurrent \"stencil build --dev --watch --docs\" \"stencil-dev-server\"", + "devapp": "sd concurrent \"stencil build --dev --watch --docs\" \"stencil-dev-server --broadcast\"", "e2e": "node ./scripts/e2e", "e2e-debug": "node --inspect --debug-brk ./scripts/e2e", "lint": "npm run tslint & npm run sass-lint", diff --git a/packages/core/src/components/action-sheet/action-sheet.tsx b/packages/core/src/components/action-sheet/action-sheet.tsx index 48ac1ae527..ada9df7fcb 100644 --- a/packages/core/src/components/action-sheet/action-sheet.tsx +++ b/packages/core/src/components/action-sheet/action-sheet.tsx @@ -26,6 +26,7 @@ export class ActionSheet implements OverlayInterface { mode: string; color: string; + private presented = false; private animation: Animation | null = null; @Element() private el: HTMLElement; @@ -137,7 +138,12 @@ export class ActionSheet implements OverlayInterface { * Present the action sheet overlay after it has been created. */ @Method() - present() { + present(): Promise { + if(this.presented) { + return Promise.reject('overlay already presented'); + } + this.presented = true; + this.ionActionSheetWillPresent.emit(); this.el.style.zIndex = `${20000 + this.overlayId}`; @@ -156,6 +162,11 @@ export class ActionSheet implements OverlayInterface { */ @Method() dismiss(data?: any, role?: string) { + if(!this.presented) { + return Promise.reject('overlay is not presented'); + } + this.presented = false; + this.ionActionSheetWillDismiss.emit({data, role}); const animationBuilder = this.leaveAnimation || this.config.get('actionSheetLeave', this.mode === 'ios' ? iosLeaveAnimation : mdLeaveAnimation); diff --git a/packages/core/src/components/alert/alert.tsx b/packages/core/src/components/alert/alert.tsx index 5e19d085f9..eaa3386c63 100644 --- a/packages/core/src/components/alert/alert.tsx +++ b/packages/core/src/components/alert/alert.tsx @@ -24,6 +24,7 @@ export class Alert implements OverlayInterface { mode: string; color: string; + private presented = false; private animation: Animation | null = null; private activeId: string; private inputType: string | null = null; @@ -143,7 +144,11 @@ export class Alert implements OverlayInterface { * Present the alert overlay after it has been created. */ @Method() - present() { + present(): Promise { + if(this.presented) { + return Promise.reject('overlay already presented'); + } + this.presented = true; this.ionAlertWillPresent.emit(); this.el.style.zIndex = `${20000 + this.overlayId}`; @@ -166,6 +171,10 @@ export class Alert implements OverlayInterface { */ @Method() dismiss(data?: any, role?: string) { + if(!this.presented) { + return Promise.reject('overlay is not presented'); + } + this.presented = false; this.ionAlertWillDismiss.emit({data, role}); // get the user's animation fn if one was provided diff --git a/packages/core/src/components/loading/loading.tsx b/packages/core/src/components/loading/loading.tsx index 497c83f0e1..a3d0b4d96c 100644 --- a/packages/core/src/components/loading/loading.tsx +++ b/packages/core/src/components/loading/loading.tsx @@ -24,6 +24,7 @@ export class Loading implements OverlayInterface { color: string; mode: string; + private presented = false; private animation: Animation; private durationTimeout: any; @@ -162,7 +163,11 @@ export class Loading implements OverlayInterface { * Present the loading overlay after it has been created. */ @Method() - present() { + present(): Promise { + if(this.presented) { + return Promise.reject('overlay already presented'); + } + this.presented = true; this.ionLoadingWillPresent.emit(); this.el.style.zIndex = `${20000 + this.overlayId}`; @@ -181,6 +186,10 @@ export class Loading implements OverlayInterface { */ @Method() dismiss(data?: any, role?: string) { + if(!this.presented) { + return Promise.reject('overlay is not presented'); + } + this.presented = false; clearTimeout(this.durationTimeout); this.ionLoadingWillDismiss.emit({data, role}); diff --git a/packages/core/src/components/modal/modal.tsx b/packages/core/src/components/modal/modal.tsx index 03c5169a24..182e400442 100644 --- a/packages/core/src/components/modal/modal.tsx +++ b/packages/core/src/components/modal/modal.tsx @@ -24,6 +24,7 @@ import mdLeaveAnimation from './animations/md.leave'; }) export class Modal implements OverlayInterface { + private presented = false; private animation: Animation; private usersComponentElement: HTMLElement; @@ -145,7 +146,12 @@ export class Modal implements OverlayInterface { * Present the modal overlay after it has been created. */ @Method() - present() { + present(): Promise { + if(this.presented) { + return Promise.reject('overlay already presented'); + } + this.presented = true; + if (this.animation) { this.animation.destroy(); this.animation = null; @@ -193,6 +199,10 @@ export class Modal implements OverlayInterface { */ @Method() dismiss(data?: any, role?: string) { + if(!this.presented) { + return Promise.reject('overlay is not presented'); + } + this.presented = false; if (this.animation) { this.animation.destroy(); this.animation = null; diff --git a/packages/core/src/components/picker/picker.tsx b/packages/core/src/components/picker/picker.tsx index 5ee15699c5..ddc343ad2d 100644 --- a/packages/core/src/components/picker/picker.tsx +++ b/packages/core/src/components/picker/picker.tsx @@ -20,6 +20,7 @@ import iosLeaveAnimation from './animations/ios.leave'; }) export class Picker implements OverlayInterface { + private presented = false; private animation: Animation; private durationTimeout: any; private mode: string; @@ -114,7 +115,12 @@ export class Picker implements OverlayInterface { * Present the picker overlay after it has been created. */ @Method() - present() { + present(): Promise { + if(this.presented) { + return Promise.reject('overlay already presented'); + } + this.presented = true; + if (this.animation) { this.animation.destroy(); this.animation = null; @@ -147,6 +153,10 @@ export class Picker implements OverlayInterface { */ @Method() dismiss(data?: any, role?: string) { + if(!this.presented) { + return Promise.reject('overlay is not presented'); + } + this.presented = false; clearTimeout(this.durationTimeout); if (this.animation) { diff --git a/packages/core/src/components/popover/popover.tsx b/packages/core/src/components/popover/popover.tsx index fe3f5d02f5..d25de9cbf2 100644 --- a/packages/core/src/components/popover/popover.tsx +++ b/packages/core/src/components/popover/popover.tsx @@ -23,6 +23,7 @@ import mdLeaveAnimation from './animations/md.leave'; }) export class Popover implements OverlayInterface { + private presented = false; private animation: Animation; private usersComponentElement: HTMLElement; @@ -158,7 +159,12 @@ export class Popover implements OverlayInterface { * Present the popover overlay after it has been created. */ @Method() - present() { + present(): Promise { + if(this.presented) { + return Promise.reject('overlay already presented'); + } + this.presented = true; + if (this.animation) { this.animation.destroy(); this.animation = null; @@ -206,6 +212,10 @@ export class Popover implements OverlayInterface { */ @Method() dismiss(data?: any, role?: string) { + if(!this.presented) { + return Promise.reject('overlay is not presented'); + } + this.presented = false; if (this.animation) { this.animation.destroy(); this.animation = null; diff --git a/packages/core/src/components/toast/toast.tsx b/packages/core/src/components/toast/toast.tsx index b5192bf7b9..23be932909 100644 --- a/packages/core/src/components/toast/toast.tsx +++ b/packages/core/src/components/toast/toast.tsx @@ -23,6 +23,7 @@ import mdLeaveAnimation from './animations/md.leave'; }) export class Toast implements OverlayInterface { + private presented = false; private animation: Animation | null; @Element() private el: HTMLElement; @@ -126,7 +127,12 @@ export class Toast implements OverlayInterface { * Present the toast overlay after it has been created. */ @Method() - present() { + present(): Promise { + if(this.presented) { + return Promise.reject('overlay already presented'); + } + this.presented = true; + if (this.animation) { this.animation.destroy(); this.animation = null; @@ -156,6 +162,11 @@ export class Toast implements OverlayInterface { */ @Method() dismiss(data?: any, role?: string) { + if(!this.presented) { + return Promise.reject('overlay is not presented'); + } + this.presented = false; + if (this.animation) { this.animation.destroy(); this.animation = null;