shlomiassaf / ngx-modialog

Modal / Dialog for Angular
http://shlomiassaf.github.io/ngx-modialog
MIT License
686 stars 242 forks source link

Prevent page changes when a dialog is open #322

Closed tlorenzetti closed 7 years ago

tlorenzetti commented 7 years ago
shlomiassaf commented 7 years ago

@tlorenzetti Yes, this is the expected behaviour.

To prevent a page from navigating the CanDeactivate hook from @angular/router needs to be registered.

This is done via a service that implements the appropriate router interface and is registered in the router configuration

Now, from a behaviour perspective some scenarios can allow page changes while a modal is open, others might be more strict and disallow that. Pure domain specific logic.
This is why its not the responsibility of the modal library.

More over, what if one doesn't event use the router package, does the library need to force him because it depends on it?

That was just the theoretical explanation, practically speaking it probably not doable since the library does not define routes, the application developer does.

For all of the above, and more... this is something that needs to be handled by the application developer.

The library does provide tools to help with that... each Model service comes with an overlay object that holds information about the stack of open modals.

A very simple implementation will be to use the modal service to get the current amount of open modals, if its more then 0 prevent navigation.


@Injectable()
export class CanDeactivateGuard implements CanDeactivate<any> {

 constructor(private modal: Modal) {}
  canDeactivate(component: any,  route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> | boolean {
    if (this.modal.overlay.stackLength > 0) {
      return false;
    }

    return true;
 }
}

Now this is a very simple example, you'll probably need some logic and metadata.

For example, having a modal notify to a service if it a would like to prevent page changes...