]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/modal/custom-modal.component.ts
Correctly unsubscribe on menu destroy
[github/Chocobozzz/PeerTube.git] / client / src / app / modal / custom-modal.component.ts
CommitLineData
42b40636 1import { Component, ElementRef, Input, ViewChild } from '@angular/core'
437e8e06 2import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
42b40636 3import { logger } from '@root-helpers/logger'
437e8e06
K
4
5@Component({
6 selector: 'my-custom-modal',
7 templateUrl: './custom-modal.component.html',
8 styleUrls: [ './custom-modal.component.scss' ]
9})
10export class CustomModalComponent {
11 @ViewChild('modal', { static: true }) modal: ElementRef
12
13 @Input() title: string
14 @Input() content: string
15 @Input() close?: boolean
16 @Input() cancel?: { value: string, action?: () => void }
17 @Input() confirm?: { value: string, action?: () => void }
18
19 private modalRef: NgbModalRef
20
21 constructor (
22 private modalService: NgbModal
23 ) { }
24
25 show (input: {
9df52d66
C
26 title: string
27 content: string
28 close?: boolean
29 cancel?: { value: string, action?: () => void }
437e8e06
K
30 confirm?: { value: string, action?: () => void }
31 }) {
32 if (this.modalRef instanceof NgbModalRef && this.modalService.hasOpenModals()) {
42b40636 33 logger.error('Cannot open another custom modal, one is already opened.')
437e8e06
K
34 return
35 }
36
37 const { title, content, close, cancel, confirm } = input
38
39 this.title = title
40 this.content = content
41 this.close = close
42 this.cancel = cancel
43 this.confirm = confirm
44
45 this.modalRef = this.modalService.open(this.modal, {
46 centered: true,
47 backdrop: 'static',
48 keyboard: false,
49 size: 'lg'
50 })
51 }
52
53 onCancelClick () {
54 this.modalRef.close()
55
56 if (typeof this.cancel.action === 'function') {
57 this.cancel.action()
58 }
59
60 this.destroy()
61 }
62
63 onCloseClick () {
64 this.modalRef.close()
65 this.destroy()
66 }
67
68 onConfirmClick () {
69 this.modalRef.close()
70
71 if (typeof this.confirm.action === 'function') {
72 this.confirm.action()
73 }
74
75 this.destroy()
76 }
77
78 hasCancel () {
79 return typeof this.cancel !== 'undefined'
80 }
81
82 hasConfirm () {
83 return typeof this.confirm !== 'undefined'
84 }
85
86 private destroy () {
87 delete this.modalRef
88 delete this.title
89 delete this.content
90 delete this.close
91 delete this.cancel
92 delete this.confirm
93 }
94}