PluginProbe
Independent Analytics – WordPress Analytics Plugin / 2.14.1
Independent Analytics – WordPress Analytics Plugin v2.14.1
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 All 120 releases
independent-analytics / javascript-source / controllers / copy_report_controller.js

copy_report_controller.js in Independent Analytics – WordPress Analytics Plugin 2.14.1, at javascript-source/controllers/copy_report_controller.js

86 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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