AlkantarClanX12
| Current Path : /www/capitalgmcbuickregina_830/public/wp-content/plugins/leadbox/lib/ |
| Current File : /www/capitalgmcbuickregina_830/public/wp-content/plugins/leadbox/lib/edit-listing-images.js |
jQuery(document).ready(function ($) {
function handleAjaxResponse(response) {
if (response.success) {
$("#response-message").html("<p class='msg'>" + response.data.message + "</p>");
if (response.data.reload) {
location.reload();
}
} else {
$("#response-message").html("<p class='msg-er'>Error: " + response.data + "</p>");
}
}
// --- Placement mode (LBT-1462) ---
// Grouped: all images cycle every `offset` cards; the per-entry Insert After /
// Repeat Every fields are irrelevant, so hide them and show the group fields.
function applyPlacementMode() {
var mode = $("input[name='li_mode']:checked").val() || "grouped";
var grouped = mode === "grouped";
$(".li-group-fields").toggle(grouped);
$(".li-position-field").toggle(!grouped);
}
applyPlacementMode();
$(document).on("change", "input[name='li_mode']", applyPlacementMode);
$("#save-listing-config").on("click", function () {
$.ajax({
url: editListingImages.ajax_url,
type: "POST",
data: {
action: "save_listing_images_config",
mode: $("input[name='li_mode']:checked").val() || "grouped",
offset: $("#li_group_offset").val(),
repeat: $("#li_group_repeat").val(),
nonce: editListingImages.nonce,
},
success: handleAjaxResponse,
error: function (xhr, status, error) {
$("#response-message").html("<p class='msg-er'>Error: " + error + "</p>");
},
});
});
// Open the WP media frame; write the URL into $input and show a thumb in $preview.
function openMediaPicker($input, $preview) {
var frame = wp.media({
title: "Select Image",
button: { text: "Use this image" },
multiple: false,
library: { type: "image" },
});
frame.on("select", function () {
var attachment = frame.state().get("selection").first().toJSON();
$input.val(attachment.url);
if ($preview && $preview.length) {
$preview.html('<img src="' + attachment.url + '" alt="">');
}
});
frame.open();
}
// --- Add-new-image form ---
$("#li_image_preview").on("click", function () {
openMediaPicker($("#li_image"), $(this));
});
$("#save-listing-image").on("click", function () {
var entry = {
image: $("#li_image").val(),
link: $("#li_link").val(),
target: $("#li_target").val(),
insert_after: $("#li_insert_after").val(),
repeat_every: $("#li_repeat_every").val(),
condition: $("#li_condition").val(),
language: $("#li_language").val(),
fit: $("#li_fit").val(),
limit_date: $("#li_limit_date").val(),
activate_limit_date: $("#li_activate").prop("checked") ? "Enabled" : "Disabled",
alt: $("#li_alt").val(),
};
if (!entry.image || $.trim(entry.image) === "") {
alert("An image is required.");
return;
}
var insertAfter = parseInt(entry.insert_after, 10) || 0;
var repeatEvery = parseInt(entry.repeat_every, 10) || 0;
if (insertAfter <= 0 && repeatEvery <= 0) {
if (!confirm("Both 'Insert After' and 'Repeat Every' are 0, so this image won't be shown. Add it anyway?")) {
return;
}
}
$.ajax({
url: editListingImages.ajax_url,
method: "POST",
data: { action: "save_listing_image", entry: entry, nonce: editListingImages.nonce },
success: handleAjaxResponse,
error: function (xhr, status, error) {
$("#response-message").html("<p class='msg-er'>Error: " + error + "</p>");
},
});
});
// --- Saved rows: collapse/expand on header click (ignore controls) ---
$(document).on("click", ".lbx-slide-header", function (e) {
// Ignore the interactive controls (move/delete buttons + the select
// checkbox) but let everything else — including the chevron span —
// toggle the accordion.
if ($(e.target).closest("button, .lbx-slide-select").length) {
return;
}
$(this).closest(".lbx-slide").toggleClass("lbx-slide--collapsed");
});
// --- Saved rows: per-row media picker ---
$(document).on("click", ".li-row-preview", function () {
var $col = $(this).closest(".lbx-slide-image-col");
openMediaPicker($col.find(".li-image-input"), $(this));
});
// --- Reorder rows (order is persisted on "Save Changes") ---
$(document).on("click", ".li-move-up", function (e) {
e.stopPropagation();
var $slide = $(this).closest(".lbx-slide");
var $prev = $slide.prev(".lbx-slide");
if ($prev.length) {
$prev.before($slide);
}
});
$(document).on("click", ".li-move-down", function (e) {
e.stopPropagation();
var $slide = $(this).closest(".lbx-slide");
var $next = $slide.next(".lbx-slide");
if ($next.length) {
$next.after($slide);
}
});
// --- Bulk update (rebuilds the whole list in current DOM order) ---
$(document).on("click", "#update-listing-images", function (e) {
e.preventDefault();
var entries = [];
$("#lbx-listing-rows .lbx-slide").each(function () {
var entry = {};
$(this)
.find(":input")
.each(function () {
var name = $(this).attr("name") || "";
var matches = name.match(/entries\[\d+\]\[(\w+)\]/);
if (!matches) return; // skips the unnamed select-entry checkbox
var field = matches[1];
if ($(this).attr("type") === "checkbox") {
entry[field] = $(this).prop("checked") ? $(this).val() : "";
} else {
entry[field] = $(this).val();
}
});
if (entry.image && $.trim(entry.image) !== "") {
entries.push(entry);
}
});
$.ajax({
url: editListingImages.ajax_url,
method: "POST",
data: { action: "update_listing_images", entries: entries, nonce: editListingImages.nonce },
success: handleAjaxResponse,
error: function (xhr, status, error) {
$("#response-message").html("<p class='msg-er'>Error: " + error + "</p>");
},
});
});
// --- Delete single row ---
$(document).on("click", ".delete-listing-image", function (e) {
e.stopPropagation();
var index = $(this).data("index");
if (confirm("Are you sure you want to delete this image?")) {
$.ajax({
url: editListingImages.ajax_url,
method: "POST",
data: { action: "delete_listing_image", index: index, nonce: editListingImages.nonce },
success: handleAjaxResponse,
error: function (xhr, status, error) {
$("#response-message").html("<p class='msg-er'>Error: " + error + "</p>");
},
});
}
});
// --- Delete selected rows ---
$("#delete-selected-listing-images").on("click", function () {
var selected = [];
$(".select-entry:checked").each(function () {
selected.push($(this).closest(".lbx-slide").find(".delete-listing-image").data("index"));
});
if (selected.length === 0) {
alert("Please select at least one image to delete.");
return;
}
if (confirm("Are you sure you want to delete the selected images?")) {
$.ajax({
url: editListingImages.ajax_url,
method: "POST",
data: { action: "delete_selected_listing_images", entries: selected, nonce: editListingImages.nonce },
success: handleAjaxResponse,
error: function (xhr, status, error) {
$("#response-message").html("<p class='msg-er'>Error: " + error + "</p>");
},
});
}
});
});