| 1 |
/* eslint-disable no-undef */ |
| 2 |
/** |
| 3 |
* GutSlider Admin JavaScript |
| 4 |
*/ |
| 5 |
|
| 6 |
(function ($) { |
| 7 |
'use strict'; |
| 8 |
|
| 9 |
$(document).ready(function () { |
| 10 |
// Handle block toggle |
| 11 |
$('.block-toggle').on('change', function () { |
| 12 |
const $toggle = $(this); |
| 13 |
const blockKey = $toggle.data('key'); |
| 14 |
// const blockName = $toggle.data('block'); |
| 15 |
const isChecked = $toggle.is(':checked'); |
| 16 |
|
| 17 |
// Disable toggle during request |
| 18 |
$toggle.prop('disabled', true); |
| 19 |
|
| 20 |
// Show loading indicator |
| 21 |
const $card = $toggle.closest('.gutslider-block-card'); |
| 22 |
$card.css('opacity', '0.6'); |
| 23 |
|
| 24 |
// Send AJAX request |
| 25 |
$.ajax({ |
| 26 |
url: gutslider.ajaxUrl, |
| 27 |
type: 'POST', |
| 28 |
data: { |
| 29 |
action: 'gutslider_toggle_block', |
| 30 |
nonce: gutslider.nonce, |
| 31 |
block_key: blockKey, |
| 32 |
status: isChecked |
| 33 |
}, |
| 34 |
success(response) { |
| 35 |
if (response.success) { |
| 36 |
// Show success notification |
| 37 |
showNotification('success', response.data.message); |
| 38 |
} else { |
| 39 |
// Revert toggle on error |
| 40 |
$toggle.prop('checked', !isChecked); |
| 41 |
showNotification('error', response.data.message); |
| 42 |
} |
| 43 |
}, |
| 44 |
error() { |
| 45 |
// Revert toggle on error |
| 46 |
$toggle.prop('checked', !isChecked); |
| 47 |
showNotification('error', 'An error occurred. Please try again.'); |
| 48 |
}, |
| 49 |
complete() { |
| 50 |
// Re-enable toggle and remove loading state |
| 51 |
$toggle.prop('disabled', false); |
| 52 |
$card.css('opacity', '1'); |
| 53 |
} |
| 54 |
}); |
| 55 |
}); |
| 56 |
|
| 57 |
// Show notification function |
| 58 |
function showNotification(type, message) { |
| 59 |
const notificationClass = type === 'success' ? 'notice-success' : 'notice-error'; |
| 60 |
const $notification = $('<div>', { |
| 61 |
class: `notice ${notificationClass} is-dismissible gutslider-notification`, |
| 62 |
html: `<p>${message}</p>` |
| 63 |
}); |
| 64 |
|
| 65 |
// Add close button functionality |
| 66 |
$notification.append( |
| 67 |
'<button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button>' |
| 68 |
); |
| 69 |
|
| 70 |
// Insert notification |
| 71 |
if ($('.gutslider-admin-wrap').length) { |
| 72 |
$('.gutslider-admin-wrap').prepend($notification); |
| 73 |
} else { |
| 74 |
$('.wrap').prepend($notification); |
| 75 |
} |
| 76 |
|
| 77 |
// Handle dismiss button |
| 78 |
$notification.find('.notice-dismiss').on('click', function () { |
| 79 |
$notification.fadeOut(300, function () { |
| 80 |
$(this).remove(); |
| 81 |
}); |
| 82 |
}); |
| 83 |
|
| 84 |
// Auto remove after 3 seconds |
| 85 |
setTimeout(function () { |
| 86 |
$notification.fadeOut(300, function () { |
| 87 |
$(this).remove(); |
| 88 |
}); |
| 89 |
}, 1000); |
| 90 |
} |
| 91 |
|
| 92 |
// Handle pro block clicks |
| 93 |
$('.pro-block .toggle-switch input:disabled').on('click', function (e) { |
| 94 |
e.preventDefault(); |
| 95 |
showProUpgradeNotice(); |
| 96 |
}); |
| 97 |
|
| 98 |
$('.pro-block').on('click', function (e) { |
| 99 |
if ($(e.target).closest('.toggle-switch, .demo-link').length === 0) { |
| 100 |
showProUpgradeNotice(); |
| 101 |
} |
| 102 |
}); |
| 103 |
|
| 104 |
// Show pro upgrade notice |
| 105 |
function showProUpgradeNotice() { |
| 106 |
if (!gutslider.isPro) { |
| 107 |
const upgradeUrl = 'https://gutslider.com/pricing/'; |
| 108 |
const message = `This is a Pro feature. <a href="${upgradeUrl}" target="_blank" rel="noopener noreferrer">Upgrade to Pro</a> to unlock this block.`; |
| 109 |
showNotification('error', message); |
| 110 |
} |
| 111 |
} |
| 112 |
}); |
| 113 |
})(jQuery); |
| 114 |
|