| 1 |
(function () { |
| 2 |
var el = wp.element.createElement; |
| 3 |
var registerBlockType = wp.blocks.registerBlockType; |
| 4 |
var InspectorControls = wp.blockEditor.InspectorControls; |
| 5 |
var PanelBody = wp.components.PanelBody; |
| 6 |
var TextControl = wp.components.TextControl; |
| 7 |
var RangeControl = wp.components.RangeControl; |
| 8 |
var SelectControl = wp.components.SelectControl; |
| 9 |
var ToggleControl = wp.components.ToggleControl; |
| 10 |
var __ = wp.i18n.__; |
| 11 |
|
| 12 |
function TourEdit(props) { |
| 13 |
var attributes = props.attributes; |
| 14 |
var setAttributes = props.setAttributes; |
| 15 |
|
| 16 |
return el( |
| 17 |
wp.element.Fragment, |
| 18 |
null, |
| 19 |
el( |
| 20 |
InspectorControls, |
| 21 |
null, |
| 22 |
el( |
| 23 |
PanelBody, |
| 24 |
{ |
| 25 |
title: __("Trip Settings", "yatra"), |
| 26 |
initialOpen: true, |
| 27 |
}, |
| 28 |
el(TextControl, { |
| 29 |
label: __("Title", "yatra"), |
| 30 |
value: attributes.title, |
| 31 |
onChange: function (value) { |
| 32 |
setAttributes({ title: value }); |
| 33 |
}, |
| 34 |
__next40pxDefaultSize: true, |
| 35 |
__nextHasNoMarginBottom: true, |
| 36 |
}), |
| 37 |
el(SelectControl, { |
| 38 |
label: __("Order", "yatra"), |
| 39 |
value: attributes.order, |
| 40 |
options: [ |
| 41 |
{ label: __("Descending", "yatra"), value: "desc" }, |
| 42 |
{ label: __("Ascending", "yatra"), value: "asc" }, |
| 43 |
], |
| 44 |
onChange: function (value) { |
| 45 |
setAttributes({ order: value }); |
| 46 |
}, |
| 47 |
__next40pxDefaultSize: true, |
| 48 |
__nextHasNoMarginBottom: true, |
| 49 |
}), |
| 50 |
el(RangeControl, { |
| 51 |
label: __("Number of Trips", "yatra"), |
| 52 |
value: attributes.per_page, |
| 53 |
onChange: function (value) { |
| 54 |
setAttributes({ per_page: value || 10 }); |
| 55 |
}, |
| 56 |
min: 1, |
| 57 |
max: 50, |
| 58 |
__next40pxDefaultSize: true, |
| 59 |
__nextHasNoMarginBottom: true, |
| 60 |
}), |
| 61 |
el(RangeControl, { |
| 62 |
label: __("Columns", "yatra"), |
| 63 |
value: attributes.columns, |
| 64 |
onChange: function (value) { |
| 65 |
setAttributes({ columns: value || 3 }); |
| 66 |
}, |
| 67 |
min: 1, |
| 68 |
max: 6, |
| 69 |
__next40pxDefaultSize: true, |
| 70 |
__nextHasNoMarginBottom: true, |
| 71 |
}), |
| 72 |
el(ToggleControl, { |
| 73 |
label: __("Show only featured trips", "yatra"), |
| 74 |
checked: attributes.featured, |
| 75 |
onChange: function (value) { |
| 76 |
setAttributes({ featured: value }); |
| 77 |
}, |
| 78 |
__nextHasNoMarginBottom: true, |
| 79 |
}), |
| 80 |
el(ToggleControl, { |
| 81 |
label: __("Show pagination", "yatra"), |
| 82 |
checked: attributes.show_pagination, |
| 83 |
onChange: function (value) { |
| 84 |
setAttributes({ show_pagination: value }); |
| 85 |
}, |
| 86 |
__nextHasNoMarginBottom: true, |
| 87 |
}), |
| 88 |
), |
| 89 |
), |
| 90 |
el(wp.serverSideRender, { |
| 91 |
block: "yatra/tour", |
| 92 |
attributes: attributes, |
| 93 |
urlQueryArgs: { |
| 94 |
preview: true, |
| 95 |
}, |
| 96 |
}), |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
registerBlockType("yatra/tour", { |
| 101 |
apiVersion: 3, |
| 102 |
title: "Trip", |
| 103 |
icon: "palmtree", |
| 104 |
category: "yatra", |
| 105 |
attributes: { |
| 106 |
order: { |
| 107 |
type: "string", |
| 108 |
default: "desc", |
| 109 |
}, |
| 110 |
featured: { |
| 111 |
type: "boolean", |
| 112 |
default: false, |
| 113 |
}, |
| 114 |
per_page: { |
| 115 |
type: "number", |
| 116 |
default: 10, |
| 117 |
}, |
| 118 |
columns: { |
| 119 |
type: "number", |
| 120 |
default: 3, |
| 121 |
}, |
| 122 |
title: { |
| 123 |
type: "string", |
| 124 |
default: "Our Trips", |
| 125 |
}, |
| 126 |
show_pagination: { |
| 127 |
type: "boolean", |
| 128 |
default: true, |
| 129 |
}, |
| 130 |
}, |
| 131 |
edit: TourEdit, |
| 132 |
save: function () { |
| 133 |
return null; // Server-side rendered |
| 134 |
}, |
| 135 |
}); |
| 136 |
|
| 137 |
// Initialize tour functionality when block is rendered in editor |
| 138 |
function initializeTourBlock() { |
| 139 |
// Prevent trip.js errors by checking if we're in block editor |
| 140 |
if (window.jQuery) { |
| 141 |
// Remove trip.js error handlers that look for non-existent elements |
| 142 |
window.jQuery(document).off("ready", function () { |
| 143 |
// Prevent trip.js from running in block editor context |
| 144 |
}); |
| 145 |
} |
| 146 |
|
| 147 |
// Re-initialize tour shortcode JavaScript for dynamically loaded content |
| 148 |
if (window.jQuery && window.jQuery.fn.yatraTourShortcode) { |
| 149 |
window.jQuery(".yatra-tour-shortcode").each(function () { |
| 150 |
if (!window.jQuery(this).data("yatra-initialized")) { |
| 151 |
window.jQuery(this).yatraTourShortcode(); |
| 152 |
window.jQuery(this).data("yatra-initialized", true); |
| 153 |
} |
| 154 |
}); |
| 155 |
} |
| 156 |
|
| 157 |
// Re-bind pagination events |
| 158 |
if (window.jQuery) { |
| 159 |
window |
| 160 |
.jQuery(document) |
| 161 |
.off("click", ".yatra-tour-shortcode .yatra-pagination a") |
| 162 |
.on("click", ".yatra-tour-shortcode .yatra-pagination a", function (e) { |
| 163 |
e.preventDefault(); |
| 164 |
var $this = window.jQuery(this); |
| 165 |
var page = $this.data("page"); |
| 166 |
var $container = $this.closest(".yatra-tour-shortcode"); |
| 167 |
|
| 168 |
if (page && $container.length) { |
| 169 |
// Show loading |
| 170 |
$container.addClass("yatra-loading"); |
| 171 |
|
| 172 |
// Load new page |
| 173 |
window.jQuery.ajax({ |
| 174 |
url: yatra_ajax.ajax_url, |
| 175 |
type: "POST", |
| 176 |
data: { |
| 177 |
action: "yatra_load_tours", |
| 178 |
page: page, |
| 179 |
attributes: $container.data("attributes") || {}, |
| 180 |
}, |
| 181 |
success: function (response) { |
| 182 |
$container.html(response); |
| 183 |
$container.removeClass("yatra-loading"); |
| 184 |
initializeTourBlock(); // Re-initialize for new content |
| 185 |
}, |
| 186 |
error: function () { |
| 187 |
$container.removeClass("yatra-loading"); |
| 188 |
}, |
| 189 |
}); |
| 190 |
} |
| 191 |
}); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
// Initialize when DOM is ready and when ServerSideRender updates |
| 196 |
if (document.readyState === "loading") { |
| 197 |
document.addEventListener("DOMContentLoaded", initializeTourBlock); |
| 198 |
} else { |
| 199 |
initializeTourBlock(); |
| 200 |
} |
| 201 |
|
| 202 |
// Also initialize when block content changes |
| 203 |
if (window.wp && window.wp.data && window.wp.data.subscribe) { |
| 204 |
window.wp.data.subscribe(function () { |
| 205 |
setTimeout(initializeTourBlock, 100); |
| 206 |
}); |
| 207 |
} |
| 208 |
})(); |
| 209 |
|