2024-02-12 10:50:29 +01:00
|
|
|
import * as debug from 'debug'
|
2020-06-23 14:10:17 +02:00
|
|
|
import { Observable } from 'rxjs'
|
2018-01-25 18:40:23 +01:00
|
|
|
import { Injectable } from '@angular/core'
|
2020-06-23 14:10:17 +02:00
|
|
|
import { ConfirmService } from '@app/core/confirm'
|
2018-01-25 18:40:23 +01:00
|
|
|
|
2018-10-18 14:35:31 +02:00
|
|
|
export type CanComponentDeactivateResult = { text?: string, canDeactivate: Observable<boolean> | boolean }
|
|
|
|
|
2024-02-12 10:50:29 +01:00
|
|
|
const debugLogger = debug('peertube:routing:CanComponentDeactivate')
|
|
|
|
|
2018-01-25 18:40:23 +01:00
|
|
|
export interface CanComponentDeactivate {
|
2018-10-18 14:35:31 +02:00
|
|
|
canDeactivate: () => CanComponentDeactivateResult
|
2018-01-25 18:40:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Injectable()
|
2023-05-23 11:15:00 +02:00
|
|
|
export class CanDeactivateGuard {
|
2020-08-12 10:40:04 +02:00
|
|
|
|
|
|
|
constructor (private confirmService: ConfirmService) { }
|
2018-01-25 18:40:23 +01:00
|
|
|
|
2018-09-22 20:11:16 +02:00
|
|
|
canDeactivate (component: CanComponentDeactivate) {
|
2018-01-25 19:01:13 +01:00
|
|
|
const result = component.canDeactivate()
|
2024-02-12 10:50:29 +01:00
|
|
|
|
|
|
|
debugLogger('Checking if component can deactivate', result)
|
|
|
|
|
2020-08-12 10:40:04 +02:00
|
|
|
const text = result.text || $localize`All unsaved data will be lost, are you sure you want to leave this page?`
|
2018-01-25 19:01:13 +01:00
|
|
|
|
|
|
|
return result.canDeactivate || this.confirmService.confirm(
|
|
|
|
text,
|
2020-08-12 10:40:04 +02:00
|
|
|
$localize`Warning`
|
2018-01-25 18:40:23 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|