22 * Human Cell Atlas
33 * https://www.humancellatlas.org/
44 *
5- * Component for displaying project prepared expression matrices downloads inside modal. Displayed
5+ * Component for displaying project prepared expression matrices downloads inside modal. The modal closes automatically
6+ * on NavigationStart event. The follow actions causes a redirect to the projects page (and therefore closes the modal):
7+ *
8+ * 1. Hitting escape
9+ * 2. Clicking the close icon
10+ * 3. Clicking the HCA logo
611 */
712
813// Core dependencies
914import { Component , HostListener , Inject , OnDestroy , OnInit } from "@angular/core" ;
1015import { MAT_DIALOG_DATA , MatDialogRef } from "@angular/material" ;
11- import { Router } from "@angular/router" ;
16+ import { NavigationStart , Router , RouterEvent } from "@angular/router" ;
1217import { select , Store } from "@ngrx/store" ;
1318import { combineLatest , BehaviorSubject , Observable , Subject } from "rxjs" ;
1419import { filter , map , takeUntil } from "rxjs/operators" ;
@@ -23,6 +28,8 @@ import { ProjectDownloadMatrixModalState } from "./project-download-matrix-modal
2328import { FetchProjectRequestAction } from "../_ngrx/table/table.actions" ;
2429import { selectSelectedProject } from "../_ngrx/file.selectors" ;
2530import { Project } from "../shared/project.model" ;
31+ import { ModalOpenedAction } from "../../modal/_ngrx/modal-opened.action" ;
32+ import { ModalClosedAction } from "../../modal/_ngrx/modal-closed.action" ;
2633
2734@Component ( {
2835 selector : "project-download-matrix-modal" ,
@@ -47,32 +54,33 @@ export class ProjectDownloadMatrixModalComponent implements OnDestroy, OnInit {
4754 private dialogRef : MatDialogRef < ProjectDownloadMatrixModalComponent > ,
4855 @Inject ( MAT_DIALOG_DATA ) private data : any ,
4956 private router : Router ) {
50- }
51-
52- /**
53- * Close dialog on key up of escape key.
54- */
55- @HostListener ( "window:keyup.esc" ) onKeyUp ( ) {
5657
57- this . dialogRef . close ( ) ;
58- this . redirectToProjects ( )
58+ this . store . dispatch ( new ModalOpenedAction ( ) ) ;
5959 }
6060
6161 /**
62- * Close modal and redirect to projects list.
62+ * Redirect to projects list - called from template on click of close icon, or on keyup of escape key. The resulting
63+ * navigation event causes the modal to close. See initCloseOnNavigation.
6364 */
64- public onClosedClicked ( ) : void {
65+ @HostListener ( "window:keyup.esc" )
66+ public redirectToProjects ( ) : void {
6567
66- this . dialogRef . close ( ) ;
67- this . redirectToProjects ( ) ;
68+ this . router . navigateByUrl ( `/${ EntityName . PROJECTS } ` , { replaceUrl : true } ) ;
6869 }
6970
7071 /**
71- * Redirect to projects list .
72+ * Close the modal on any navigation event .
7273 */
73- public redirectToProjects ( ) : void {
74+ private initCloseOnNavigation ( ) {
7475
75- this . router . navigateByUrl ( `/${ EntityName . PROJECTS } ` , { replaceUrl : true } ) ;
76+ this . router . events . pipe (
77+ filter ( ( event : RouterEvent ) => event instanceof NavigationStart ) ,
78+ filter ( ( ) => ! ! this . dialogRef ) ,
79+ takeUntil ( this . ngDestroy$ )
80+ ) . subscribe ( ( ) => {
81+ this . store . dispatch ( new ModalClosedAction ( ) ) ;
82+ this . dialogRef . close ( ) ;
83+ } ) ;
7684 }
7785
7886 /**
@@ -115,11 +123,13 @@ export class ProjectDownloadMatrixModalComponent implements OnDestroy, OnInit {
115123 }
116124
117125 /**
118- * Grab the prepared matrix URLs for the selected project. Also listen for close events (click on backdrop or escape
119- * key) and redirect to projects list .
126+ * Grab the prepared matrix URLs for the selected project. Also listen for navigation events, in which case we must
127+ * close the modal .
120128 */
121129 public ngOnInit ( ) : void {
122-
130+
131+ this . initCloseOnNavigation ( ) ;
132+
123133 const projectId = this . data . projectId ;
124134
125135 // Request project details so we can display the project title
0 commit comments