2020-06-23 14:10:17 +02:00
|
|
|
import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'
|
2022-02-22 11:25:03 +01:00
|
|
|
import { HtmlRendererService } from '@app/core'
|
2019-01-16 16:05:40 +01:00
|
|
|
import { ConfirmService } from '@app/core/confirm/confirm.service'
|
2020-06-23 14:10:17 +02:00
|
|
|
import { POP_STATE_MODAL_DISMISS } from '@app/helpers'
|
2018-08-09 14:55:06 +02:00
|
|
|
import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
|
|
|
|
import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
|
2017-01-27 16:54:44 +01:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-confirm',
|
2017-12-07 10:33:16 +01:00
|
|
|
templateUrl: './confirm.component.html',
|
2018-02-22 15:29:32 +01:00
|
|
|
styleUrls: [ './confirm.component.scss' ]
|
2017-01-27 16:54:44 +01:00
|
|
|
})
|
|
|
|
export class ConfirmComponent implements OnInit {
|
2019-07-24 16:05:59 +02:00
|
|
|
@ViewChild('confirmModal', { static: true }) confirmModal: ElementRef
|
2017-01-27 16:54:44 +01:00
|
|
|
|
2017-06-16 14:32:15 +02:00
|
|
|
title = ''
|
|
|
|
message = ''
|
2018-02-22 15:29:32 +01:00
|
|
|
expectedInputValue = ''
|
|
|
|
inputLabel = ''
|
|
|
|
|
|
|
|
inputValue = ''
|
2018-02-28 15:33:45 +01:00
|
|
|
confirmButtonText = ''
|
2017-01-27 16:54:44 +01:00
|
|
|
|
2023-06-29 09:48:55 +02:00
|
|
|
errorMessage = ''
|
|
|
|
|
2022-10-07 11:06:28 +02:00
|
|
|
isPasswordInput = false
|
|
|
|
|
2018-08-09 14:55:06 +02:00
|
|
|
private openedModal: NgbModalRef
|
|
|
|
|
2018-06-04 16:21:17 +02:00
|
|
|
constructor (
|
2018-08-09 14:55:06 +02:00
|
|
|
private modalService: NgbModal,
|
2022-02-22 11:25:03 +01:00
|
|
|
private html: HtmlRendererService,
|
2020-08-12 10:40:04 +02:00
|
|
|
private confirmService: ConfirmService
|
2018-09-22 20:11:16 +02:00
|
|
|
) { }
|
2017-01-27 16:54:44 +01:00
|
|
|
|
2017-06-16 14:32:15 +02:00
|
|
|
ngOnInit () {
|
2017-01-27 16:54:44 +01:00
|
|
|
this.confirmService.showConfirm.subscribe(
|
2022-10-07 11:06:28 +02:00
|
|
|
payload => {
|
|
|
|
// Reinit fields
|
|
|
|
this.title = ''
|
|
|
|
this.message = ''
|
|
|
|
this.expectedInputValue = ''
|
|
|
|
this.inputLabel = ''
|
|
|
|
this.inputValue = ''
|
|
|
|
this.confirmButtonText = ''
|
|
|
|
this.isPasswordInput = false
|
2023-06-29 09:48:55 +02:00
|
|
|
this.errorMessage = ''
|
2022-10-07 11:06:28 +02:00
|
|
|
|
2023-06-29 09:48:55 +02:00
|
|
|
const { type, title, message, confirmButtonText, errorMessage } = payload
|
2022-10-07 11:06:28 +02:00
|
|
|
|
2017-06-16 14:32:15 +02:00
|
|
|
this.title = title
|
2017-01-27 16:54:44 +01:00
|
|
|
|
2022-10-07 11:06:28 +02:00
|
|
|
if (type === 'confirm-expected-input') {
|
|
|
|
this.inputLabel = payload.inputLabel
|
|
|
|
this.expectedInputValue = payload.expectedInputValue
|
|
|
|
} else if (type === 'confirm-password') {
|
|
|
|
this.inputLabel = $localize`Confirm your password`
|
|
|
|
this.isPasswordInput = true
|
2023-06-29 09:48:55 +02:00
|
|
|
this.errorMessage = errorMessage
|
2022-10-07 11:06:28 +02:00
|
|
|
}
|
2018-02-22 15:29:32 +01:00
|
|
|
|
2020-08-12 10:40:04 +02:00
|
|
|
this.confirmButtonText = confirmButtonText || $localize`Confirm`
|
2018-02-28 15:33:45 +01:00
|
|
|
|
2022-02-22 11:25:03 +01:00
|
|
|
this.html.toSafeHtml(message)
|
2024-04-04 13:24:41 +02:00
|
|
|
.then(html => {
|
|
|
|
this.message = html
|
2022-02-22 11:25:03 +01:00
|
|
|
|
|
|
|
this.showModal()
|
|
|
|
})
|
2017-01-27 16:54:44 +01:00
|
|
|
}
|
2017-06-16 14:32:15 +02:00
|
|
|
)
|
2017-01-27 16:54:44 +01:00
|
|
|
}
|
|
|
|
|
2017-06-16 14:32:15 +02:00
|
|
|
confirm () {
|
2018-08-09 14:55:06 +02:00
|
|
|
if (this.openedModal) this.openedModal.close()
|
2017-01-27 16:54:44 +01:00
|
|
|
}
|
|
|
|
|
2018-02-22 15:29:32 +01:00
|
|
|
isConfirmationDisabled () {
|
|
|
|
// No input validation
|
|
|
|
if (!this.inputLabel || !this.expectedInputValue) return false
|
|
|
|
|
|
|
|
return this.expectedInputValue !== this.inputValue
|
|
|
|
}
|
|
|
|
|
2023-06-29 09:48:55 +02:00
|
|
|
hasError () {
|
|
|
|
return this.errorMessage
|
|
|
|
}
|
2017-06-16 14:32:15 +02:00
|
|
|
showModal () {
|
2018-07-17 11:06:04 +02:00
|
|
|
this.inputValue = ''
|
2017-01-27 16:54:44 +01:00
|
|
|
|
2020-03-06 16:58:55 +01:00
|
|
|
this.openedModal = this.modalService.open(this.confirmModal, { centered: true })
|
2018-08-09 14:55:06 +02:00
|
|
|
|
|
|
|
this.openedModal.result
|
2022-10-07 11:06:28 +02:00
|
|
|
.then(() => {
|
|
|
|
this.confirmService.confirmResponse.next({ confirmed: true, value: this.inputValue })
|
|
|
|
})
|
2019-08-22 17:13:58 +02:00
|
|
|
.catch((reason: string) => {
|
|
|
|
// If the reason was that the user used the back button, we don't care about the confirm dialog result
|
|
|
|
if (!reason || reason !== POP_STATE_MODAL_DISMISS) {
|
2022-10-07 11:06:28 +02:00
|
|
|
this.confirmService.confirmResponse.next({ confirmed: false, value: this.inputValue })
|
2019-08-22 17:13:58 +02:00
|
|
|
}
|
|
|
|
})
|
2017-01-27 16:54:44 +01:00
|
|
|
}
|
|
|
|
}
|