| 1 |
(function ($) { |
| 2 |
$(document).ready(function () { |
| 3 |
"use strict"; |
| 4 |
|
| 5 |
/** |
| 6 |
* Initialize DataTable for Categories |
| 7 |
*/ |
| 8 |
var table = $("#wgm-categories-table").DataTable({ |
| 9 |
responsive: true, |
| 10 |
processing: true, |
| 11 |
ajax: { |
| 12 |
url: ajaxurl, |
| 13 |
data: { |
| 14 |
action: "wgm_get_categories", |
| 15 |
_wpnonce: wgm_l.nonces.wgm_get_categories, |
| 16 |
}, |
| 17 |
}, |
| 18 |
columns: [ |
| 19 |
{ data: "id" }, |
| 20 |
{ data: "icon" }, |
| 21 |
{ data: "name" }, |
| 22 |
{ data: "parent" }, |
| 23 |
{ data: "action" }, |
| 24 |
], |
| 25 |
order: [[0, "desc"]], |
| 26 |
}); |
| 27 |
|
| 28 |
/** |
| 29 |
* Handle Category Form Submission |
| 30 |
* Adds or updates a category via AJAX. |
| 31 |
*/ |
| 32 |
$("#wgm-category-form").on("submit", function (e) { |
| 33 |
e.preventDefault(); |
| 34 |
var $spinner = $("#wgm-category-spinner"); |
| 35 |
var $submit = $("#wgm-category-submit"); |
| 36 |
|
| 37 |
$spinner.addClass("is-active"); |
| 38 |
$submit.prop("disabled", true); |
| 39 |
|
| 40 |
var categoryId = parseInt($("#wgm-category-id").val(), 10) || 0; |
| 41 |
var action = categoryId > 0 ? "wgm_update_category" : "wgm_save_category"; |
| 42 |
var nonce = |
| 43 |
categoryId > 0 |
| 44 |
? wgm_l.nonces.wgm_update_category |
| 45 |
: wgm_l.nonces.wgm_save_category; |
| 46 |
|
| 47 |
var categoryData = { |
| 48 |
id: categoryId, |
| 49 |
name: $("#wgm-category-name").val(), |
| 50 |
icon: $("#wgm-category-icon-url").val(), |
| 51 |
parent_id: $("#wgm-category-parent").val(), |
| 52 |
}; |
| 53 |
|
| 54 |
$.post(ajaxurl, { |
| 55 |
action: action, |
| 56 |
category_data: categoryData, |
| 57 |
_wpnonce: nonce, |
| 58 |
}) |
| 59 |
.done(function (response) { |
| 60 |
$spinner.removeClass("is-active"); |
| 61 |
$submit.prop("disabled", false); |
| 62 |
|
| 63 |
try { |
| 64 |
if (typeof response === "string") { |
| 65 |
response = JSON.parse(response); |
| 66 |
} |
| 67 |
|
| 68 |
if (response.success) { |
| 69 |
Swal.fire({ |
| 70 |
icon: "success", |
| 71 |
title: "Success!", |
| 72 |
text: response.data.message, |
| 73 |
timer: 2000, |
| 74 |
showConfirmButton: false, |
| 75 |
}); |
| 76 |
resetForm(); |
| 77 |
table.ajax.reload(); |
| 78 |
|
| 79 |
// Dynamic Dropdown Update |
| 80 |
if (categoryId === 0) { |
| 81 |
// Add new category to parent dropdown |
| 82 |
if (response.data.id && categoryData.name) { |
| 83 |
var newOption = new Option( |
| 84 |
categoryData.name, |
| 85 |
response.data.id |
| 86 |
); |
| 87 |
$("#wgm-category-parent").append(newOption); |
| 88 |
} |
| 89 |
} else { |
| 90 |
// Update existing category name in parent dropdown |
| 91 |
$( |
| 92 |
"#wgm-category-parent option[value='" + categoryId + "']" |
| 93 |
).text(categoryData.name); |
| 94 |
} |
| 95 |
} else { |
| 96 |
Swal.fire({ |
| 97 |
icon: "error", |
| 98 |
title: "Error", |
| 99 |
text: response.data.message || "Something went wrong", |
| 100 |
}); |
| 101 |
} |
| 102 |
} catch (err) { |
| 103 |
console.error("WGM: Error parsing category save response", err); |
| 104 |
Swal.fire({ |
| 105 |
icon: "error", |
| 106 |
title: "Error", |
| 107 |
text: "Invalid server response.", |
| 108 |
}); |
| 109 |
} |
| 110 |
}) |
| 111 |
.fail(function (xhr, status, error) { |
| 112 |
$spinner.removeClass("is-active"); |
| 113 |
$submit.prop("disabled", false); |
| 114 |
Swal.fire({ |
| 115 |
icon: "error", |
| 116 |
title: "Network Error", |
| 117 |
text: "An error occurred while processing your request: " + error, |
| 118 |
}); |
| 119 |
}); |
| 120 |
}); |
| 121 |
|
| 122 |
/** |
| 123 |
* Handle Edit Category Click |
| 124 |
* Fetches category data and populates the form for editing. |
| 125 |
*/ |
| 126 |
$(document.body).on("click", ".wgm-edit-category", function () { |
| 127 |
var id = $(this).data("id"); |
| 128 |
var $spinner = $("#wgm-category-spinner"); |
| 129 |
$spinner.addClass("is-active"); |
| 130 |
|
| 131 |
$.post(ajaxurl, { |
| 132 |
action: "wgm_get_category_data", |
| 133 |
id: id, |
| 134 |
_wpnonce: wgm_l.nonces.wgm_get_category_data, |
| 135 |
}) |
| 136 |
.done(function (response) { |
| 137 |
$spinner.removeClass("is-active"); |
| 138 |
|
| 139 |
try { |
| 140 |
if (typeof response === "string") { |
| 141 |
response = JSON.parse(response); |
| 142 |
} |
| 143 |
|
| 144 |
if (response.success) { |
| 145 |
var data = response.data; |
| 146 |
$("#wgm-category-id").val(data.id); |
| 147 |
$("#wgm-category-name").val(data.name); |
| 148 |
$("#wgm-category-parent").val(data.parent_id); |
| 149 |
$("#wgm-category-icon-url").val(data.icon); |
| 150 |
|
| 151 |
if (data.icon) { |
| 152 |
$("#wgm-category-icon-preview").attr("src", data.icon); |
| 153 |
$("#wgm-category-icon-remove").show(); |
| 154 |
} else { |
| 155 |
$("#wgm-category-icon-preview").attr( |
| 156 |
"src", |
| 157 |
wgm_l.plugin_url + "admin/assets/images/markers/default.png" |
| 158 |
); |
| 159 |
$("#wgm-category-icon-remove").hide(); |
| 160 |
} |
| 161 |
|
| 162 |
$("#wgm-category-form-title").text("Edit Category: " + data.name); |
| 163 |
$("#wgm-category-submit").text("Update Category"); |
| 164 |
$("#wgm-category-cancel").show(); |
| 165 |
|
| 166 |
// Disable itself in parent dropdown to prevent circular reference |
| 167 |
$("#wgm-category-parent option").prop("disabled", false); |
| 168 |
$("#wgm-category-parent option[value='" + data.id + "']").prop( |
| 169 |
"disabled", |
| 170 |
true |
| 171 |
); |
| 172 |
|
| 173 |
$("html, body").animate( |
| 174 |
{ scrollTop: $("#wgm-category-form").offset().top - 100 }, |
| 175 |
500 |
| 176 |
); |
| 177 |
} else { |
| 178 |
Swal.fire({ |
| 179 |
icon: "error", |
| 180 |
title: "Error", |
| 181 |
text: response.data.message || "Could not fetch category data", |
| 182 |
}); |
| 183 |
} |
| 184 |
} catch (err) { |
| 185 |
console.error("WGM: Error parsing category data response", err); |
| 186 |
Swal.fire({ |
| 187 |
icon: "error", |
| 188 |
title: "Error", |
| 189 |
text: "Invalid server response.", |
| 190 |
}); |
| 191 |
} |
| 192 |
}) |
| 193 |
.fail(function (xhr, status, error) { |
| 194 |
$spinner.removeClass("is-active"); |
| 195 |
Swal.fire({ |
| 196 |
icon: "error", |
| 197 |
title: "Network Error", |
| 198 |
text: "Failed to fetch category data: " + error, |
| 199 |
}); |
| 200 |
}); |
| 201 |
}); |
| 202 |
|
| 203 |
/** |
| 204 |
* Handle Delete Category Click |
| 205 |
* Confirms and deletes a category via AJAX. |
| 206 |
*/ |
| 207 |
$(document.body).on("click", ".wgm-delete-category", function () { |
| 208 |
var id = $(this).data("id"); |
| 209 |
|
| 210 |
Swal.fire({ |
| 211 |
title: "Are you sure?", |
| 212 |
text: "You won't be able to revert this!", |
| 213 |
icon: "warning", |
| 214 |
showCancelButton: true, |
| 215 |
confirmButtonColor: "#d33", |
| 216 |
cancelButtonColor: "#3085d6", |
| 217 |
confirmButtonText: "Yes, delete it!", |
| 218 |
}).then((result) => { |
| 219 |
if (result.isConfirmed) { |
| 220 |
$.post(ajaxurl, { |
| 221 |
action: "wgm_delete_category", |
| 222 |
id: id, |
| 223 |
_wpnonce: wgm_l.nonces.wgm_delete_category, |
| 224 |
}) |
| 225 |
.done(function (response) { |
| 226 |
try { |
| 227 |
if (typeof response === "string") { |
| 228 |
response = JSON.parse(response); |
| 229 |
} |
| 230 |
|
| 231 |
if (response.success) { |
| 232 |
Swal.fire("Deleted!", response.data.message, "success"); |
| 233 |
table.ajax.reload(); |
| 234 |
|
| 235 |
// Remove deleted category from parent dropdown |
| 236 |
$("#wgm-category-parent option[value='" + id + "']").remove(); |
| 237 |
} else { |
| 238 |
Swal.fire("Error", response.data.message, "error"); |
| 239 |
} |
| 240 |
} catch (err) { |
| 241 |
console.error( |
| 242 |
"WGM: Error parsing category delete response", |
| 243 |
err |
| 244 |
); |
| 245 |
Swal.fire({ |
| 246 |
icon: "error", |
| 247 |
title: "Error", |
| 248 |
text: "Invalid server response.", |
| 249 |
}); |
| 250 |
} |
| 251 |
}) |
| 252 |
.fail(function (xhr, status, error) { |
| 253 |
Swal.fire({ |
| 254 |
icon: "error", |
| 255 |
title: "Network Error", |
| 256 |
text: "Failed to delete category: " + error, |
| 257 |
}); |
| 258 |
}); |
| 259 |
} |
| 260 |
}); |
| 261 |
}); |
| 262 |
|
| 263 |
/** |
| 264 |
* Handle Cancel Button Click |
| 265 |
* Resets the form to its initial state. |
| 266 |
*/ |
| 267 |
$("#wgm-category-cancel").on("click", function () { |
| 268 |
resetForm(); |
| 269 |
}); |
| 270 |
|
| 271 |
/** |
| 272 |
* Reset Form |
| 273 |
* Clears all fields and resets UI elements. |
| 274 |
*/ |
| 275 |
function resetForm() { |
| 276 |
$("#wgm-category-id").val(0); |
| 277 |
$("#wgm-category-name").val(""); |
| 278 |
$("#wgm-category-parent").val(0); |
| 279 |
$("#wgm-category-parent option").prop("disabled", false); |
| 280 |
$("#wgm-category-icon-url").val(""); |
| 281 |
$("#wgm-category-icon-preview").attr( |
| 282 |
"src", |
| 283 |
wgm_l.plugin_url + "admin/assets/images/markers/default.png" |
| 284 |
); |
| 285 |
$("#wgm-category-icon-remove").hide(); |
| 286 |
$("#wgm-category-form-title").text("Add New Category"); |
| 287 |
$("#wgm-category-submit").text("Save Category"); |
| 288 |
$("#wgm-category-cancel").hide(); |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Media Uploader for Category Icon |
| 293 |
* Handles opening the WP Media Library to select an icon. |
| 294 |
*/ |
| 295 |
var mediaUploader; |
| 296 |
$("#wgm-category-icon-upload").on("click", function (e) { |
| 297 |
e.preventDefault(); |
| 298 |
if (mediaUploader) { |
| 299 |
mediaUploader.open(); |
| 300 |
return; |
| 301 |
} |
| 302 |
mediaUploader = wp.media.frames.file_frame = wp.media({ |
| 303 |
title: "Choose Category Icon", |
| 304 |
button: { text: "Choose Icon" }, |
| 305 |
multiple: false, |
| 306 |
}); |
| 307 |
mediaUploader.on("select", function () { |
| 308 |
var attachment = mediaUploader |
| 309 |
.state() |
| 310 |
.get("selection") |
| 311 |
.first() |
| 312 |
.toJSON(); |
| 313 |
$("#wgm-category-icon-url").val(attachment.url); |
| 314 |
$("#wgm-category-icon-preview").attr("src", attachment.url); |
| 315 |
$("#wgm-category-icon-remove").show(); |
| 316 |
}); |
| 317 |
mediaUploader.open(); |
| 318 |
}); |
| 319 |
|
| 320 |
/** |
| 321 |
* Remove Category Icon |
| 322 |
* Clears the selected icon. |
| 323 |
*/ |
| 324 |
$("#wgm-category-icon-remove").on("click", function () { |
| 325 |
$("#wgm-category-icon-url").val(""); |
| 326 |
$("#wgm-category-icon-preview").attr( |
| 327 |
"src", |
| 328 |
wgm_l.plugin_url + "admin/assets/images/markers/default.png" |
| 329 |
); |
| 330 |
$(this).hide(); |
| 331 |
}); |
| 332 |
}); |
| 333 |
})(jQuery); |
| 334 |
|