From 674a66bbda7ceb7aed3c2e6aefddc2793207d94b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 14 Jan 2019 14:55:43 +0100 Subject: Add unload listener on video upload/update --- .../app/videos/+video-edit/video-update.component.ts | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'client/src/app/videos/+video-edit/video-update.component.ts') diff --git a/client/src/app/videos/+video-edit/video-update.component.ts b/client/src/app/videos/+video-edit/video-update.component.ts index d22ee540a..9e849014e 100644 --- a/client/src/app/videos/+video-edit/video-update.component.ts +++ b/client/src/app/videos/+video-edit/video-update.component.ts @@ -1,5 +1,5 @@ import { map, switchMap } from 'rxjs/operators' -import { Component, OnInit } from '@angular/core' +import { Component, HostListener, OnInit } from '@angular/core' import { ActivatedRoute, Router } from '@angular/router' import { LoadingBarService } from '@ngx-loading-bar/core' import { Notifier } from '@app/core' @@ -83,14 +83,26 @@ export class VideoUpdateComponent extends FormReactive implements OnInit { ) } - canDeactivate () { + @HostListener('window:beforeunload', [ '$event' ]) + onUnload (event: any) { + const { text, canDeactivate } = this.canDeactivate() + + if (canDeactivate) return + + event.returnValue = text + return text + } + + canDeactivate (): { canDeactivate: boolean, text?: string } { if (this.updateDone === true) return { canDeactivate: true } + const text = this.i18n('You have unsaved changes! If you leave, your changes will be lost.') + for (const caption of this.videoCaptions) { - if (caption.action) return { canDeactivate: false } + if (caption.action) return { canDeactivate: false, text } } - return { canDeactivate: this.formChanged === false } + return { canDeactivate: this.formChanged === false, text } } checkForm () { -- cgit v1.2.3