| 1 |
/** |
| 2 |
* Booking Calendar — Page Delete Confirm controller (BFB Admin) |
| 3 |
* |
| 4 |
* Depends on: |
| 5 |
* - WPBC_BFB_Core (bfb-core.js) |
| 6 |
* - UI.Templates (bfb-templates.js) |
| 7 |
* - UI.Modals (modal show/hide helpers) |
| 8 |
* |
| 9 |
* @file: ../includes/page-form-builder/admin-page-tpl/_out/modal_page_delete.js |
| 10 |
*/ |
| 11 |
|
| 12 |
(function (w, d) { |
| 13 |
'use strict'; |
| 14 |
|
| 15 |
const Core = (w.WPBC_BFB_Core = w.WPBC_BFB_Core || {}); |
| 16 |
const UI = (Core.UI = Core.UI || {}); |
| 17 |
|
| 18 |
// ------------------------------------------------------------------------- |
| 19 |
// Constants (keep selectors in one place) |
| 20 |
// ------------------------------------------------------------------------- |
| 21 |
|
| 22 |
const ACTION_KEY = 'bfb_action__page_delete'; |
| 23 |
const MODAL_DOM_ID = 'wpbc_bfb_modal__page_delete'; |
| 24 |
const TPL_MODAL_ID = 'wpbc-bfb-tpl-modal-page-delete'; |
| 25 |
|
| 26 |
const SEL_TRIGGER = '[data-wpbc-bfb-action="' + ACTION_KEY + '"]'; |
| 27 |
const SEL_PAGE = '.wpbc_bfb__panel--preview'; |
| 28 |
const SEL_CONFIRM = '#' + MODAL_DOM_ID + ' [data-wpbc-bfb-confirm="1"][data-wpbc-bfb-action="' + ACTION_KEY + '"]'; |
| 29 |
|
| 30 |
// ------------------------------------------------------------------------- |
| 31 |
// Module: Page Delete Confirm |
| 32 |
// ------------------------------------------------------------------------- |
| 33 |
|
| 34 |
UI.WPBC_BFB_Page_Delete_Confirm = class extends UI.WPBC_BFB_Module { |
| 35 |
|
| 36 |
init() { |
| 37 |
if ( !this.builder || !this.builder.pages_container ) { |
| 38 |
return; |
| 39 |
} |
| 40 |
this._bind_events(); |
| 41 |
} |
| 42 |
|
| 43 |
destroy() { |
| 44 |
const b = this.builder; |
| 45 |
|
| 46 |
try { |
| 47 |
if ( this._on_trigger_click && b && b.pages_container ) { |
| 48 |
b.pages_container.removeEventListener( 'click', this._on_trigger_click, true ); |
| 49 |
} |
| 50 |
if ( this._on_confirm_click ) { |
| 51 |
d.removeEventListener( 'click', this._on_confirm_click, true ); |
| 52 |
} |
| 53 |
} catch ( _e ) { |
| 54 |
} |
| 55 |
|
| 56 |
this._on_trigger_click = null; |
| 57 |
this._on_confirm_click = null; |
| 58 |
} |
| 59 |
|
| 60 |
// ----------------------- |
| 61 |
// Event binding |
| 62 |
// ----------------------- |
| 63 |
|
| 64 |
_bind_events() { |
| 65 |
const b = this.builder; |
| 66 |
|
| 67 |
this._on_trigger_click = (e) => { |
| 68 |
const trigger = this._closest( e.target, SEL_TRIGGER ); |
| 69 |
if ( !trigger ) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
const page_el = this._closest( trigger, SEL_PAGE ); |
| 74 |
if ( !page_el ) { |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
e.preventDefault(); |
| 79 |
this._open_modal_for_page( page_el ); |
| 80 |
}; |
| 81 |
|
| 82 |
this._on_confirm_click = (e) => { |
| 83 |
const btn = this._closest( e.target, SEL_CONFIRM ); |
| 84 |
if ( !btn ) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
e.preventDefault(); |
| 89 |
this._confirm_delete(); |
| 90 |
}; |
| 91 |
|
| 92 |
b.pages_container.addEventListener( 'click', this._on_trigger_click, true ); |
| 93 |
d.addEventListener( 'click', this._on_confirm_click, true ); |
| 94 |
} |
| 95 |
|
| 96 |
_closest(el, selector) { |
| 97 |
return (el && el.closest) ? el.closest( selector ) : null; |
| 98 |
} |
| 99 |
|
| 100 |
// ----------------------- |
| 101 |
// Modal open / confirm |
| 102 |
// ----------------------- |
| 103 |
|
| 104 |
_open_modal_for_page(page_el) { |
| 105 |
const page_number = page_el.getAttribute( 'data-page' ) || ''; |
| 106 |
|
| 107 |
const ref = UI.Templates.ensure_dom_ref_from_wp_template( |
| 108 |
TPL_MODAL_ID, |
| 109 |
MODAL_DOM_ID, |
| 110 |
{ page_number: page_number } |
| 111 |
); |
| 112 |
|
| 113 |
if ( !ref || !ref.el ) { |
| 114 |
// No template/modal available => remove directly. |
| 115 |
this._remove_page_el( page_el ); |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
const modal_el = ref.el; |
| 120 |
|
| 121 |
// Store the target page on the modal instance (one modal, one active target). |
| 122 |
modal_el.__wpbc_bfb_page_el = page_el; |
| 123 |
|
| 124 |
// Update page number in modal header (optional, template already renders it too). |
| 125 |
const sup = modal_el.querySelector ? modal_el.querySelector( '.wpbc_bfb__modal_page_number' ) : null; |
| 126 |
if ( sup ) { |
| 127 |
sup.textContent = String( page_number ); |
| 128 |
} |
| 129 |
|
| 130 |
UI.Modals.show( ref.id ); |
| 131 |
} |
| 132 |
|
| 133 |
_confirm_delete() { |
| 134 |
const modal_el = d.getElementById( MODAL_DOM_ID ); |
| 135 |
if ( !modal_el ) { |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
const page_el = modal_el.__wpbc_bfb_page_el || null; |
| 140 |
modal_el.__wpbc_bfb_page_el = null; |
| 141 |
|
| 142 |
if ( page_el ) { |
| 143 |
this._remove_page_el( page_el ); |
| 144 |
} |
| 145 |
|
| 146 |
UI.Modals.hide( modal_el ); |
| 147 |
} |
| 148 |
|
| 149 |
// ----------------------- |
| 150 |
// Deletion implementation |
| 151 |
// ----------------------- |
| 152 |
|
| 153 |
_remove_page_el(page_el) { |
| 154 |
const b = this.builder; |
| 155 |
const selected = (b.get_selected_field) ? b.get_selected_field() : null; |
| 156 |
|
| 157 |
let neighbor = null; |
| 158 |
|
| 159 |
// If selection is inside the deleted page, try to select something nearby after deletion. |
| 160 |
if ( selected && page_el.contains( selected ) ) { |
| 161 |
const page_id = page_el.getAttribute( 'data-page' ) || ''; |
| 162 |
neighbor = b.pages_container.querySelector( |
| 163 |
'.wpbc_bfb__panel--preview:not([data-page="' + page_id + '"]) .wpbc_bfb__field:not(.is-invalid)' |
| 164 |
); |
| 165 |
} |
| 166 |
|
| 167 |
page_el.remove(); |
| 168 |
|
| 169 |
if ( b.usage && b.usage.update_palette_ui ) { |
| 170 |
b.usage.update_palette_ui(); |
| 171 |
} |
| 172 |
|
| 173 |
// Notify structure change. |
| 174 |
try { |
| 175 |
const EV = Core.WPBC_BFB_Events || {}; |
| 176 |
const structure = (typeof b.get_structure === 'function') ? b.get_structure() : null; |
| 177 |
|
| 178 |
if ( b.bus && b.bus.emit ) { |
| 179 |
b.bus.emit( EV.STRUCTURE_CHANGE, { |
| 180 |
source : 'page-remove', |
| 181 |
structure: structure, |
| 182 |
page_el : page_el |
| 183 |
} ); |
| 184 |
} |
| 185 |
} catch ( _e ) { |
| 186 |
} |
| 187 |
|
| 188 |
if ( b.select_field ) { |
| 189 |
b.select_field( neighbor || null ); |
| 190 |
} |
| 191 |
} |
| 192 |
}; |
| 193 |
|
| 194 |
// ------------------------------------------------------------------------- |
| 195 |
// Auto attach |
| 196 |
// ------------------------------------------------------------------------- |
| 197 |
|
| 198 |
function attach(builder) { |
| 199 |
if ( !builder || typeof builder.use_module !== 'function' ) { |
| 200 |
return; |
| 201 |
} |
| 202 |
|
| 203 |
if ( builder.__wpbc_bfb_has_page_delete_confirm ) { |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
builder.__wpbc_bfb_has_page_delete_confirm = true; |
| 208 |
builder.use_module( UI.WPBC_BFB_Page_Delete_Confirm ); |
| 209 |
} |
| 210 |
|
| 211 |
if ( w.wpbc_bfb_api && w.wpbc_bfb_api.ready && typeof w.wpbc_bfb_api.ready.then === 'function' ) { |
| 212 |
w.wpbc_bfb_api.ready.then( attach ); |
| 213 |
} |
| 214 |
|
| 215 |
}( window, document )); |
| 216 |
|