| 1 |
(function ($) { |
| 2 |
$(document).ready(function () { |
| 3 |
"use strict"; |
| 4 |
|
| 5 |
/** Constraints */ |
| 6 |
|
| 7 |
/** Common Functions */ |
| 8 |
function isGuttenbergActive() { |
| 9 |
return document.body.classList.contains("block-editor-page"); |
| 10 |
} |
| 11 |
|
| 12 |
/** Classic Editor Google Map Select Popup box */ |
| 13 |
if (!isGuttenbergActive()) { |
| 14 |
// To load Maps List |
| 15 |
var loadSrmGmapsList = function () { |
| 16 |
$("#wgm_all_maps").find(".spinner").addClass("is-active"); |
| 17 |
$("#wpgmapembed_list").html(""); |
| 18 |
var data = { |
| 19 |
action: "wpgmapembed_popup_load_map_data", |
| 20 |
_wpnonce: wgm_l.nonces.wpgmapembed_popup_load_map_data, |
| 21 |
}; |
| 22 |
jQuery |
| 23 |
.post(ajaxurl, data, function (response) { |
| 24 |
$("#wgm_all_maps").find(".spinner").removeClass("is-active"); |
| 25 |
$("#wpgmapembed_list").html(response); |
| 26 |
}) |
| 27 |
.fail(function (xhr, status, error) { |
| 28 |
$("#wgm_all_maps").find(".spinner").removeClass("is-active"); |
| 29 |
console.error("WGM: Failed to load maps list", error); |
| 30 |
}); |
| 31 |
}; |
| 32 |
|
| 33 |
// Loading Map/List on window load |
| 34 |
$(window).on("load", loadSrmGmapsList); |
| 35 |
|
| 36 |
// Removing Popup Box |
| 37 |
var removeSrmGmapPopup = function () { |
| 38 |
if (typeof self.parent !== "undefined" && self.parent.tb_remove) { |
| 39 |
self.parent.tb_remove(); |
| 40 |
} |
| 41 |
}; |
| 42 |
|
| 43 |
// Inserting ShortCode From List on click insert button |
| 44 |
$(document.body).on("click", ".wpgmap-insert-shortcode", function () { |
| 45 |
var shortcode = $(this) |
| 46 |
.parent() |
| 47 |
.parent() |
| 48 |
.find(".wpgmap-shortcode") |
| 49 |
.val(); |
| 50 |
if (!tinyMCE.activeEditor || tinyMCE.activeEditor.isHidden()) { |
| 51 |
$("textarea#content").val(shortcode); |
| 52 |
} else { |
| 53 |
tinyMCE.execCommand("mceInsertContent", false, shortcode); |
| 54 |
} |
| 55 |
removeSrmGmapPopup(); |
| 56 |
}); |
| 57 |
|
| 58 |
// On Click fire removing Popup Box(removeSrmGmapPopup) |
| 59 |
$(document.body).on("click", ".wp_gmap_close_btn", removeSrmGmapPopup); |
| 60 |
|
| 61 |
// On Escape fire removing Popup Box(removeSrmGmapPopup) |
| 62 |
$(document).keyup(function (e) { |
| 63 |
if (e.which === 27) { |
| 64 |
removeSrmGmapPopup(); |
| 65 |
} |
| 66 |
}); |
| 67 |
} |
| 68 |
|
| 69 |
/** Map related functions/event listeners */ |
| 70 |
|
| 71 |
// ******* Remove Single Map ******* |
| 72 |
$(document.body).on("click", ".wgm_wpgmap_delete", function () { |
| 73 |
if (!confirm("Are you sure to Delete")) { |
| 74 |
return false; |
| 75 |
} |
| 76 |
$("#wp-gmap-nav").find(".spinner").addClass("is-active"); |
| 77 |
var btn_class = $(this); |
| 78 |
btn_class.prop("disabled", true); |
| 79 |
var post_id = parseInt($(this).data("id"), 10); |
| 80 |
|
| 81 |
// Validate post_id is numeric |
| 82 |
if (isNaN(post_id) || post_id <= 0) { |
| 83 |
alert("Invalid map ID."); |
| 84 |
$("#wp-gmap-nav").find(".spinner").removeClass("is-active"); |
| 85 |
btn_class.prop("disabled", false); |
| 86 |
return false; |
| 87 |
} |
| 88 |
var data = { |
| 89 |
action: "wpgmapembed_remove_wpgmap", |
| 90 |
post_id: post_id, |
| 91 |
_wpnonce: wgm_l.nonces.wpgmapembed_remove_wpgmap, |
| 92 |
}; |
| 93 |
|
| 94 |
jQuery |
| 95 |
.post(ajaxurl, data, function (response) { |
| 96 |
$("#wp-gmap-nav").find(".spinner").removeClass("is-active"); |
| 97 |
btn_class.prop("disabled", false); |
| 98 |
|
| 99 |
try { |
| 100 |
if (typeof response === "string") { |
| 101 |
response = JSON.parse(response); |
| 102 |
} |
| 103 |
if (response.responseCode === 1) { |
| 104 |
window.location.reload(); |
| 105 |
} else { |
| 106 |
alert( |
| 107 |
response.message || |
| 108 |
"Something went wrong, could not delete, please try again." |
| 109 |
); |
| 110 |
} |
| 111 |
} catch (e) { |
| 112 |
console.error("WGM: Error parsing delete response", e); |
| 113 |
alert("Unexpected server response."); |
| 114 |
} |
| 115 |
}) |
| 116 |
.fail(function (xhr, status, error) { |
| 117 |
$("#wp-gmap-nav").find(".spinner").removeClass("is-active"); |
| 118 |
btn_class.prop("disabled", false); |
| 119 |
alert("Network error: " + error); |
| 120 |
}); |
| 121 |
}); |
| 122 |
|
| 123 |
// ******* Handle Placement Button Clicks ******* |
| 124 |
$(document.body).on("click", ".wgm-placement-btn", function () { |
| 125 |
var $btn = $(this); |
| 126 |
var value = $btn.data("value"); |
| 127 |
|
| 128 |
// Update active state in UI |
| 129 |
$btn.siblings().removeClass("active"); |
| 130 |
$btn.addClass("active"); |
| 131 |
|
| 132 |
// Update hidden input value |
| 133 |
$("#wpgmap_marker_listing_placement").val(value); |
| 134 |
}); |
| 135 |
|
| 136 |
/** |
| 137 |
* Get Checkbox Value Helper |
| 138 |
*/ |
| 139 |
function getCheckboxValue($parent, selector) { |
| 140 |
return $parent.find(selector).is(":checked") ? 1 : 0; |
| 141 |
} |
| 142 |
|
| 143 |
// ************** Save, Update and Insert Button |
| 144 |
$(document.body).on( |
| 145 |
"click", |
| 146 |
"#wp-gmap-embed-save,#wp-gmap-embed-update", |
| 147 |
function () { |
| 148 |
var $btn = $(this); |
| 149 |
var $parentMsg = $("body .wpgmap_msg_error"); |
| 150 |
var $spinner = $btn.parent().find(".spinner"); |
| 151 |
|
| 152 |
$parentMsg.html(""); |
| 153 |
$btn.prop("disabled", true); |
| 154 |
$spinner.addClass("is-active"); |
| 155 |
|
| 156 |
var btn_id = $btn.attr("id"); |
| 157 |
var $container = |
| 158 |
btn_id === "wp-gmap-embed-save" |
| 159 |
? $("#wp-gmap-new") |
| 160 |
: $("#wp-gmap-edit"); |
| 161 |
|
| 162 |
if (!$container.length) { |
| 163 |
$btn.prop("disabled", false); |
| 164 |
$spinner.removeClass("is-active"); |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
var map_data = { |
| 169 |
wpgmap_title: $container.find("#wpgmap_title").val(), |
| 170 |
wpgmap_heading_class: $container.find("#wpgmap_heading_class").val(), |
| 171 |
wpgmap_show_heading: getCheckboxValue( |
| 172 |
$container, |
| 173 |
"#wpgmap_show_heading" |
| 174 |
), |
| 175 |
wpgmap_latlng: $container.find("#wpgmap_latlng").val(), |
| 176 |
wpgmap_map_zoom: $container.find("#wpgmap_map_zoom").val(), |
| 177 |
wpgmap_disable_zoom_scroll: getCheckboxValue( |
| 178 |
$container, |
| 179 |
"#wpgmap_disable_zoom_scroll" |
| 180 |
), |
| 181 |
wpgmap_map_width: $container.find("#wpgmap_map_width").val(), |
| 182 |
wpgmap_map_height: $container.find("#wpgmap_map_height").val(), |
| 183 |
wpgmap_map_type: $container.find("#wpgmap_map_type").val(), |
| 184 |
wpgmap_show_infowindow: getCheckboxValue( |
| 185 |
$container, |
| 186 |
"#wpgmap_show_infowindow" |
| 187 |
), |
| 188 |
wpgmap_enable_direction: getCheckboxValue( |
| 189 |
$container, |
| 190 |
"#wpgmap_enable_direction" |
| 191 |
), |
| 192 |
wpgmap_enable_modern_direction: getCheckboxValue( |
| 193 |
$container, |
| 194 |
"#wpgmap_enable_modern_direction" |
| 195 |
), |
| 196 |
wpgmap_direction_drawer_width: $container |
| 197 |
.find("#wpgmap_direction_drawer_width") |
| 198 |
.val(), |
| 199 |
wpgmap_marker_listing_width: $container |
| 200 |
.find("#wpgmap_marker_listing_width") |
| 201 |
.val(), |
| 202 |
wpgmap_center_lat_lng: $container |
| 203 |
.find("#wpgmap_center_lat_lng") |
| 204 |
.val(), |
| 205 |
wgm_theme_json: $container.find("#wgm_theme_json").val(), |
| 206 |
wpgmap_marker_listing_style: $container |
| 207 |
.find("#wpgmap_marker_listing_style input[type='radio']:checked") |
| 208 |
.val(), |
| 209 |
wpgmap_marker_listing_placement: $container |
| 210 |
.find("#wpgmap_marker_listing_placement") |
| 211 |
.val(), |
| 212 |
|
| 213 |
// Store Locator |
| 214 |
wgm_enable_store_locator: getCheckboxValue( |
| 215 |
$container, |
| 216 |
"#wgm_enable_store_locator" |
| 217 |
), |
| 218 |
wgm_enable_title_search: getCheckboxValue( |
| 219 |
$container, |
| 220 |
"#wgm_enable_title_search" |
| 221 |
), |
| 222 |
wgm_enable_category_filter: getCheckboxValue( |
| 223 |
$container, |
| 224 |
"#wgm_enable_category_filter" |
| 225 |
), |
| 226 |
wgm_title_search_placeholder: $container |
| 227 |
.find("#wgm_title_search_placeholder") |
| 228 |
.val(), |
| 229 |
wgm_address_search_placeholder: $container |
| 230 |
.find("#wgm_address_search_placeholder") |
| 231 |
.val(), |
| 232 |
wgm_default_address: $container.find("#wgm_default_address").val(), |
| 233 |
wgm_default_radius: $container.find("#wgm_default_radius").val(), |
| 234 |
wgm_not_found_message: $container |
| 235 |
.find("#wgm_not_found_message") |
| 236 |
.val(), |
| 237 |
wgm_hide_markers_until_search: getCheckboxValue( |
| 238 |
$container, |
| 239 |
"#wgm_hide_markers_until_search" |
| 240 |
), |
| 241 |
wgm_show_center_icon: getCheckboxValue( |
| 242 |
$container, |
| 243 |
"#wgm_show_center_icon" |
| 244 |
), |
| 245 |
wgm_show_distance: getCheckboxValue($container, "#wgm_show_distance"), |
| 246 |
wgm_distance_unit: $container.find("#wgm_distance_unit").val(), |
| 247 |
wgm_store_locator_placement: $container |
| 248 |
.find("#wgm_store_locator_placement") |
| 249 |
.val(), |
| 250 |
wgm_sort_by_distance: getCheckboxValue( |
| 251 |
$container, |
| 252 |
"#wgm_sort_by_distance" |
| 253 |
), |
| 254 |
wgm_enable_show_on_map_icon: getCheckboxValue( |
| 255 |
$container, |
| 256 |
"#wgm_enable_show_on_map_icon" |
| 257 |
), |
| 258 |
wgm_enable_get_direction_icon: getCheckboxValue( |
| 259 |
$container, |
| 260 |
"#wgm_enable_get_direction_icon" |
| 261 |
), |
| 262 |
wgm_enable_direction_link: getCheckboxValue( |
| 263 |
$container, |
| 264 |
"#wgm_enable_direction_link" |
| 265 |
), |
| 266 |
|
| 267 |
// Circle Settings |
| 268 |
wgm_radius_circle_stroke_color: $container |
| 269 |
.find("#wgm_radius_circle_stroke_color") |
| 270 |
.val(), |
| 271 |
wgm_radius_circle_stroke_opacity: $container |
| 272 |
.find("#wgm_radius_circle_stroke_opacity") |
| 273 |
.val(), |
| 274 |
wgm_radius_circle_stroke_weight: $container |
| 275 |
.find("#wgm_radius_circle_stroke_weight") |
| 276 |
.val(), |
| 277 |
wgm_radius_circle_fill_color: $container |
| 278 |
.find("#wgm_radius_circle_fill_color") |
| 279 |
.val(), |
| 280 |
wgm_radius_circle_fill_opacity: $container |
| 281 |
.find("#wgm_radius_circle_fill_opacity") |
| 282 |
.val(), |
| 283 |
|
| 284 |
// Map Controls |
| 285 |
wpgmap_zoom_control: $container.find("#wpgmap_zoom_control").val(), |
| 286 |
wpgmap_zoom_control_pos: $container |
| 287 |
.find("#wpgmap_zoom_control_pos") |
| 288 |
.val(), |
| 289 |
wpgmap_map_type_control: $container |
| 290 |
.find("#wpgmap_map_type_control") |
| 291 |
.val(), |
| 292 |
wpgmap_map_type_control_pos: $container |
| 293 |
.find("#wpgmap_map_type_control_pos") |
| 294 |
.val(), |
| 295 |
wpgmap_street_view_control: $container |
| 296 |
.find("#wpgmap_street_view_control") |
| 297 |
.val(), |
| 298 |
wpgmap_street_view_control_pos: $container |
| 299 |
.find("#wpgmap_street_view_control_pos") |
| 300 |
.val(), |
| 301 |
wpgmap_fullscreen_control: $container |
| 302 |
.find("#wpgmap_fullscreen_control") |
| 303 |
.val(), |
| 304 |
wpgmap_fullscreen_control_pos: $container |
| 305 |
.find("#wpgmap_fullscreen_control_pos") |
| 306 |
.val(), |
| 307 |
wpgmap_rotate_control: $container |
| 308 |
.find("#wpgmap_rotate_control") |
| 309 |
.val(), |
| 310 |
wpgmap_rotate_control_pos: $container |
| 311 |
.find("#wpgmap_rotate_control_pos") |
| 312 |
.val(), |
| 313 |
wpgmap_scale_control: $container.find("#wpgmap_scale_control").val(), |
| 314 |
wpgmap_scale_control_pos: $container |
| 315 |
.find("#wpgmap_scale_control_pos") |
| 316 |
.val(), |
| 317 |
marker_orderby_field: $container |
| 318 |
.find("#wpgmap_marker_orderby_field") |
| 319 |
.val(), |
| 320 |
marker_orderby_dir: $container |
| 321 |
.find("#wpgmap_marker_orderby_dir") |
| 322 |
.val(), |
| 323 |
}; |
| 324 |
|
| 325 |
// Validation |
| 326 |
if (!map_data.wpgmap_latlng || map_data.wpgmap_latlng.trim() === "") { |
| 327 |
$parentMsg.html( |
| 328 |
'<div class="error notice notice-error is-dismissible"><p>Please input Latitude and Longitude</p></div>' |
| 329 |
); |
| 330 |
$btn.prop("disabled", false); |
| 331 |
$spinner.removeClass("is-active"); |
| 332 |
return false; |
| 333 |
} |
| 334 |
|
| 335 |
if (btn_id === "wp-gmap-embed-save") { |
| 336 |
map_data.action_type = "save"; |
| 337 |
} else { |
| 338 |
map_data.action_type = "update"; |
| 339 |
map_data.post_id = $container.find("#wpgmap_map_id").val(); |
| 340 |
} |
| 341 |
|
| 342 |
var ajaxParams = { |
| 343 |
action: "wpgmapembed_save_map_data", |
| 344 |
map_data: map_data, |
| 345 |
_wpnonce: wgm_l.nonces.wpgmapembed_save_map_data, |
| 346 |
}; |
| 347 |
|
| 348 |
jQuery |
| 349 |
.post(ajaxurl, ajaxParams, function (response) { |
| 350 |
$spinner.removeClass("is-active"); |
| 351 |
$btn.prop("disabled", false); |
| 352 |
|
| 353 |
try { |
| 354 |
if (typeof response === "string") { |
| 355 |
response = JSON.parse(response); |
| 356 |
} |
| 357 |
|
| 358 |
if (response.responseCode === 0) { |
| 359 |
$parentMsg.html( |
| 360 |
'<div class="error notice notice-error is-dismissible"><p>' + |
| 361 |
$("<div>").text(response.message).html() + |
| 362 |
"</p></div>" |
| 363 |
); |
| 364 |
} else { |
| 365 |
if (btn_id === "wp-gmap-embed-save") { |
| 366 |
window.location.href = |
| 367 |
"?page=wpgmapembed&tag=edit&id=" + |
| 368 |
encodeURIComponent(response.post_id) + |
| 369 |
"&message=1&wgm_map_create_nonce=" + |
| 370 |
encodeURIComponent(wgm_l.wgm_map_create_nonce); |
| 371 |
} else { |
| 372 |
$parentMsg.html( |
| 373 |
'<div class="success notice notice-success is-dismissible"><p>' + |
| 374 |
$("<div>").text(response.message).html() + |
| 375 |
"</p></div>" |
| 376 |
); |
| 377 |
} |
| 378 |
} |
| 379 |
} catch (e) { |
| 380 |
console.error("WGM: Error parsing save response", e); |
| 381 |
$parentMsg.html( |
| 382 |
'<div class="error notice notice-error is-dismissible"><p>Unexpected server response.</p></div>' |
| 383 |
); |
| 384 |
} |
| 385 |
}) |
| 386 |
.fail(function (xhr, status, error) { |
| 387 |
$spinner.removeClass("is-active"); |
| 388 |
$btn.prop("disabled", false); |
| 389 |
$parentMsg.html( |
| 390 |
'<div class="error notice notice-error is-dismissible"><p>Network error: ' + |
| 391 |
error + |
| 392 |
"</p></div>" |
| 393 |
); |
| 394 |
}); |
| 395 |
} |
| 396 |
); |
| 397 |
|
| 398 |
/** |
| 399 |
* To view premium notice |
| 400 |
* |
| 401 |
* @since 1.7.5 |
| 402 |
*/ |
| 403 |
// Delegated so it also fires for .wgm_enable_premium buttons rendered |
| 404 |
// later by DataTables (e.g. the locked Clone icon on map/marker rows). |
| 405 |
$(document.body).on("click", ".wgm_enable_premium", function () { |
| 406 |
var wgm_notice_text = $(this).attr("data-notice"); |
| 407 |
Swal.fire({ |
| 408 |
icon: "info", |
| 409 |
showCloseButton: true, |
| 410 |
title: wgm_l.locales.sweet_alert.oops, |
| 411 |
html: |
| 412 |
wgm_notice_text + |
| 413 |
'<br><br><span style="font-size:25px;font-weight:bold;">Starting from $19 only</span><br><br><a target="_blank" href="' + |
| 414 |
wgm_l.get_p_v_url + |
| 415 |
'">Upgrade to Pro</a>', |
| 416 |
confirmButtonText: "Close", |
| 417 |
}); |
| 418 |
return false; |
| 419 |
}); |
| 420 |
|
| 421 |
/** |
| 422 |
* Map Controls Customization Toggle |
| 423 |
*/ |
| 424 |
$(document.body).on("click", "#wgm-map-controls-toggle", function () { |
| 425 |
var container = $("#wgm-map-controls-container"); |
| 426 |
var header = $(this); |
| 427 |
|
| 428 |
header.toggleClass("active"); |
| 429 |
container.slideToggle(300); |
| 430 |
}); |
| 431 |
|
| 432 |
/** |
| 433 |
* Settings tab active/inactive rendering |
| 434 |
*/ |
| 435 |
$(document.body).on("click", ".wgm-settings-menu li", function (e) { |
| 436 |
e.preventDefault(); |
| 437 |
var $this = $(this); |
| 438 |
$this.siblings().removeClass("active"); |
| 439 |
$this.addClass("active"); |
| 440 |
var wgm_tab_id = $this.attr("data-tab"); |
| 441 |
$(".wgm_settings_tabs").hide(); |
| 442 |
$("#" + wgm_tab_id).show(); |
| 443 |
}); |
| 444 |
|
| 445 |
/** |
| 446 |
* Toggle Direction Drawer Width Row visibility based on Modern Direction Checkbox |
| 447 |
*/ |
| 448 |
$(document.body).on( |
| 449 |
"change", |
| 450 |
"#wpgmap_enable_modern_direction", |
| 451 |
function () { |
| 452 |
if ($(this).is(":checked")) { |
| 453 |
$("#wgm_direction_drawer_width_row").show(); |
| 454 |
} else { |
| 455 |
$("#wgm_direction_drawer_width_row").hide(); |
| 456 |
} |
| 457 |
} |
| 458 |
); |
| 459 |
|
| 460 |
/** |
| 461 |
* Popup window for create Google Map API key |
| 462 |
* |
| 463 |
* @since 1.9.0 |
| 464 |
*/ |
| 465 |
$(document.body) |
| 466 |
.find(".wgm_api_create_gplatform") |
| 467 |
.on("click", function (e) { |
| 468 |
e.preventDefault(); |
| 469 |
const wgmDimensions = { |
| 470 |
width: 570, |
| 471 |
height: 700, |
| 472 |
}; |
| 473 |
|
| 474 |
var wgmPopupLeft = (screen.width - wgmDimensions.width) / 2; |
| 475 |
var wgmPopupTop = (screen.height - wgmDimensions.height) / 2; |
| 476 |
|
| 477 |
window.open( |
| 478 |
"https://console.cloud.google.com/google/maps-hosted", |
| 479 |
"WP Google Map - Create your API key", |
| 480 |
"resizable=yes,width=" + |
| 481 |
wgmDimensions.width + |
| 482 |
",height=" + |
| 483 |
wgmDimensions.height + |
| 484 |
",left=" + |
| 485 |
wgmPopupLeft + |
| 486 |
",top=" + |
| 487 |
wgmPopupTop |
| 488 |
); |
| 489 |
return false; |
| 490 |
}); |
| 491 |
}); |
| 492 |
})(jQuery); |
| 493 |
|