Browse Source

feat(theme-shared): trigger unload confirmation in the modal component when form is dirty

pull/2613/head
mehmet-erim 7 years ago
parent
commit
f59a2945b2
  1. 2
      npm/ng-packs/packages/core/src/lib/states/session.state.ts
  2. 35
      npm/ng-packs/packages/theme-shared/src/lib/components/modal/modal.component.ts

2
npm/ng-packs/packages/core/src/lib/states/session.state.ts

@ -67,7 +67,7 @@ export class SessionState {
this.store.dispatch(new ModifyOpenedTabCount('increase')); this.store.dispatch(new ModifyOpenedTabCount('increase'));
fromEvent(window, 'beforeunload').subscribe(() => { fromEvent(window, 'unload').subscribe(event => {
this.store.dispatch(new ModifyOpenedTabCount('decrease')); this.store.dispatch(new ModifyOpenedTabCount('decrease'));
}); });
}); });

35
npm/ng-packs/packages/theme-shared/src/lib/components/modal/modal.component.ts

@ -10,6 +10,7 @@ import {
TemplateRef, TemplateRef,
ViewChild, ViewChild,
ViewChildren, ViewChildren,
HostListener,
} from '@angular/core'; } from '@angular/core';
import { fromEvent, Subject } from 'rxjs'; import { fromEvent, Subject } from 'rxjs';
import { debounceTime, filter, takeUntil } from 'rxjs/operators'; import { debounceTime, filter, takeUntil } from 'rxjs/operators';
@ -101,15 +102,7 @@ export class ModalComponent implements OnDestroy {
destroy$ = new Subject<void>(); destroy$ = new Subject<void>();
constructor(private renderer: Renderer2, private confirmationService: ConfirmationService) {} get isFormDirty(): boolean {
ngOnDestroy(): void {
this.destroy$.next();
}
close() {
if (this.busy) return;
let node: HTMLDivElement; let node: HTMLDivElement;
if (!this.modalContent) { if (!this.modalContent) {
node = document.getElementById('modal-container') as HTMLDivElement; node = document.getElementById('modal-container') as HTMLDivElement;
@ -120,7 +113,19 @@ export class ModalComponent implements OnDestroy {
.childNodes, .childNodes,
); );
if (hasNgDirty(nodes)) { return hasNgDirty(nodes);
}
constructor(private renderer: Renderer2, private confirmationService: ConfirmationService) {}
ngOnDestroy(): void {
this.destroy$.next();
}
close() {
if (this.busy) return;
if (this.isFormDirty) {
if (this.isConfirmationOpen) return; if (this.isConfirmationOpen) return;
this.isConfirmationOpen = true; this.isConfirmationOpen = true;
@ -151,6 +156,16 @@ export class ModalComponent implements OnDestroy {
this.close(); this.close();
}); });
fromEvent(window, 'beforeunload')
.pipe(takeUntil(this.destroy$))
.subscribe(event => {
if (this.isFormDirty) {
event.returnValue = true;
} else {
delete event.returnValue;
}
});
setTimeout(() => { setTimeout(() => {
if (!this.abpClose) return; if (!this.abpClose) return;
fromEvent(this.abpClose.nativeElement, 'click') fromEvent(this.abpClose.nativeElement, 'click')

Loading…
Cancel
Save