add_module.js
1 year ago
checkbox_group.js
1 year ago
module.js
1 year ago
module_editor.js
1 year ago
module_list.js
1 year ago
module_picker.js
1 year ago
reorder_modules.js
1 year ago
module.js
132 lines
| 1 | import {Controller} from "@hotwired/stimulus" |
| 2 | import {getModuleMarkup} from "../../modules/module-markup"; |
| 3 | |
| 4 | export default class extends Controller { |
| 5 | static targets = [] |
| 6 | static values = { |
| 7 | moduleId: String, |
| 8 | hasDataset: Boolean |
| 9 | } |
| 10 | |
| 11 | connect() { |
| 12 | if(!this.hasDatasetValue) { |
| 13 | getModuleMarkup(this.moduleIdValue) |
| 14 | document.addEventListener('iawp:module-markup:' + this.moduleIdValue, this.handleMarkup) |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | disconnect() { |
| 19 | document.removeEventListener('iawp:module-markup:' + this.moduleIdValue, this.handleMarkup) |
| 20 | } |
| 21 | |
| 22 | delete(event) { |
| 23 | const button = event.currentTarget |
| 24 | const module = button.closest('.iawp-module') |
| 25 | const data = { |
| 26 | ...iawpActions.delete_module, |
| 27 | module_id: this.moduleIdValue |
| 28 | } |
| 29 | |
| 30 | button.disabled = true |
| 31 | button.classList.add('sending') |
| 32 | module.classList.add('will-be-refreshed') |
| 33 | |
| 34 | jQuery.post(ajaxurl, data, (response) => { |
| 35 | this.element.remove() |
| 36 | button.disabled = false |
| 37 | }).fail((error) => { |
| 38 | button.classList.remove('sending') |
| 39 | button.disabled = false |
| 40 | }) |
| 41 | } |
| 42 | |
| 43 | edit(event) { |
| 44 | const button = event.currentTarget |
| 45 | const data = { |
| 46 | ...iawpActions.get_markup_for_module, |
| 47 | id: this.moduleIdValue |
| 48 | } |
| 49 | |
| 50 | |
| 51 | this.signalEditingStarted() |
| 52 | button.classList.add('sending') |
| 53 | button.disabled = true |
| 54 | |
| 55 | jQuery.post(ajaxurl, data, (response) => { |
| 56 | this.element.outerHTML = response.data.editor_html |
| 57 | button.classList.remove('sending') |
| 58 | button.disabled = false |
| 59 | }).fail((error) => { |
| 60 | this.signalEditingFinished() |
| 61 | button.classList.remove('sending') |
| 62 | button.disabled = false |
| 63 | }) |
| 64 | } |
| 65 | |
| 66 | toggleWidth(event) { |
| 67 | const button = event.currentTarget |
| 68 | const module = button.closest('.iawp-module') |
| 69 | const shouldBeFullWidth = !module.classList.contains('full-width') |
| 70 | |
| 71 | const data = { |
| 72 | ...iawpActions.edit_module, |
| 73 | module_id: this.moduleIdValue, |
| 74 | fields: { |
| 75 | is_full_width: shouldBeFullWidth |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | module.classList.toggle('full-width', shouldBeFullWidth) |
| 80 | |
| 81 | if ((module.querySelector('.recent-views') || module.querySelector('.recent-conversions')) && module.querySelector('.module-pagination')) { |
| 82 | module.querySelector(".current-page").textContent = "1" |
| 83 | module.querySelector(".current").classList.remove('current') |
| 84 | module.querySelector(".module-page").classList.add('current') |
| 85 | module.querySelector(".pagination-button.left").disabled = true |
| 86 | module.querySelector(".pagination-button.right").disabled = false |
| 87 | } |
| 88 | jQuery.post(ajaxurl, data, (response) => { |
| 89 | // |
| 90 | }).fail((error) => { |
| 91 | // module.classList.toggle('full-width', !shouldBeFullWidth) |
| 92 | }) |
| 93 | } |
| 94 | |
| 95 | handleMarkup = (event) => { |
| 96 | const html = event.detail.moduleHtml |
| 97 | const isDraggable = this.element.classList.contains('draggable-module') |
| 98 | document.removeEventListener('iawp:module-markup:' + this.moduleIdValue, this.handleMarkup) |
| 99 | this.element.outerHTML = html |
| 100 | |
| 101 | // Update the new module with whatever draggable state the replaced module had |
| 102 | setTimeout(() => { |
| 103 | const module = document.querySelector(`[data-module-module-id-value="${event.detail.id}"]`) |
| 104 | if(module) { |
| 105 | module.classList.toggle('draggable-module', isDraggable) |
| 106 | } |
| 107 | }, 0) |
| 108 | } |
| 109 | |
| 110 | |
| 111 | signalEditingStarted() { |
| 112 | document.dispatchEvent( |
| 113 | new CustomEvent('iawp:moduleEditingStarted', { |
| 114 | detail: { |
| 115 | moduleId: this.moduleIdValue |
| 116 | } |
| 117 | }) |
| 118 | ) |
| 119 | } |
| 120 | |
| 121 | signalEditingFinished() { |
| 122 | document.dispatchEvent( |
| 123 | new CustomEvent('iawp:moduleEditingFinished', { |
| 124 | detail: { |
| 125 | moduleId: this.moduleIdValue |
| 126 | } |
| 127 | }) |
| 128 | ) |
| 129 | } |
| 130 | |
| 131 | |
| 132 | } |