| 1 |
import {Controller} from "@hotwired/stimulus" |
| 2 |
|
| 3 |
export default class extends Controller { |
| 4 |
static targets = ['modal', 'modalButton', 'copyButton', 'input'] |
| 5 |
|
| 6 |
static values = { |
| 7 |
id: String, |
| 8 |
type: String |
| 9 |
} |
| 10 |
|
| 11 |
changes = {} |
| 12 |
|
| 13 |
connect() { |
| 14 |
document.addEventListener('iawp:changedOption', this.handleChangedOption) |
| 15 |
document.addEventListener('click', this.maybeClose) |
| 16 |
} |
| 17 |
|
| 18 |
disconnect() { |
| 19 |
document.removeEventListener('click', this.maybeClose) |
| 20 |
} |
| 21 |
|
| 22 |
handleChangedOption = ({ detail }) => { |
| 23 |
this.changes = { |
| 24 |
...this.changes, |
| 25 |
...detail |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
maybeClose = (event) => { |
| 30 |
const isOpen = this.modalTarget.classList.contains('show') |
| 31 |
const isInComponent = this.element.contains(event.target) |
| 32 |
|
| 33 |
|
| 34 |
if (isOpen && !isInComponent) { |
| 35 |
this.closeModal() |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
toggleModal(e) { |
| 40 |
e.preventDefault(); |
| 41 |
const isOpen = this.modalTarget.classList.contains('show') |
| 42 |
isOpen ? this.closeModal() : this.openModal() |
| 43 |
} |
| 44 |
|
| 45 |
openModal() { |
| 46 |
this.inputTarget.value = '' |
| 47 |
this.modalTarget.classList.add('show') |
| 48 |
this.modalButtonTarget.classList.add('open') |
| 49 |
document.getElementById('iawp-layout').classList.add('modal-open'); |
| 50 |
setTimeout(() => { |
| 51 |
this.inputTarget.focus() |
| 52 |
this.inputTarget.select() |
| 53 |
}, 200) |
| 54 |
} |
| 55 |
|
| 56 |
closeModal() { |
| 57 |
this.modalTarget.classList.remove('show') |
| 58 |
this.modalButtonTarget.classList.remove('open') |
| 59 |
document.getElementById('iawp-layout').classList.remove('modal-open'); |
| 60 |
} |
| 61 |
|
| 62 |
copy(e) { |
| 63 |
e.preventDefault() |
| 64 |
|
| 65 |
const name = this.inputTarget.value.trim() |
| 66 |
const data = { |
| 67 |
...iawpActions.copy_report, |
| 68 |
id: this.idValue, |
| 69 |
type: this.typeValue, |
| 70 |
name, |
| 71 |
changes: JSON.stringify(this.changes) |
| 72 |
}; |
| 73 |
|
| 74 |
this.copyButtonTarget.setAttribute('disabled', 'disabled') |
| 75 |
this.copyButtonTarget.classList.add('sending') |
| 76 |
jQuery.post(ajaxurl, data, (response) => { |
| 77 |
this.copyButtonTarget.classList.remove('sending') |
| 78 |
this.copyButtonTarget.classList.add('sent') |
| 79 |
this.copyButtonTarget.classList.remove('sent') |
| 80 |
this.copyButtonTarget.removeAttribute('disabled'); |
| 81 |
this.closeModal() |
| 82 |
window.location = response.data.url |
| 83 |
}) |
| 84 |
} |
| 85 |
} |
| 86 |
|