| 1 |
/* Easy Invoice — Addons page interactions */ |
| 2 |
(function ($) { |
| 3 |
'use strict'; |
| 4 |
|
| 5 |
if (typeof window.EasyInvoiceAddons === 'undefined') return; |
| 6 |
var cfg = window.EasyInvoiceAddons; |
| 7 |
|
| 8 |
function setButtonLoading($btn, isLoading, loadingText) { |
| 9 |
if (isLoading) { |
| 10 |
$btn.data('orig-html', $btn.html()); |
| 11 |
$btn.text(loadingText); |
| 12 |
$btn.addClass('is-loading').prop('disabled', true); |
| 13 |
} else { |
| 14 |
$btn.removeClass('is-loading').prop('disabled', false); |
| 15 |
var orig = $btn.data('orig-html'); |
| 16 |
if (orig) $btn.html(orig); |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
function toast(message, type) { |
| 21 |
type = type || 'info'; |
| 22 |
if (typeof window.EasyInvoiceToast !== 'undefined' && window.EasyInvoiceToast[type]) { |
| 23 |
window.EasyInvoiceToast[type](message); |
| 24 |
return; |
| 25 |
} |
| 26 |
// Fallback: use WP admin notice if no toast helper |
| 27 |
alert(message); |
| 28 |
} |
| 29 |
|
| 30 |
function showUpgradeDialog($card, payload) { |
| 31 |
var featureName = $card.find('h3').first().text().trim(); |
| 32 |
var description = $card.find('p').last().text().trim(); |
| 33 |
var upgradeUrl = payload.upgrade_url |
| 34 |
|| $card.find('.ei-addons-btn--upgrade').data('upgrade-url') |
| 35 |
|| 'https://matrixaddons.com/plugins/easy-invoice/#pricing'; |
| 36 |
|
| 37 |
if (typeof window.EasyInvoiceConfirmation !== 'undefined' && |
| 38 |
typeof window.EasyInvoiceConfirmation.showUpgradeDialog === 'function') { |
| 39 |
window.EasyInvoiceConfirmation.showUpgradeDialog({ |
| 40 |
featureName: featureName + ' (' + (payload.required_plan_label || 'Pro') + ' plan)', |
| 41 |
description: description, |
| 42 |
onUpgrade: function () { |
| 43 |
window.open(upgradeUrl, '_blank'); |
| 44 |
} |
| 45 |
}); |
| 46 |
return; |
| 47 |
} |
| 48 |
if (confirm(featureName + ' requires the ' + (payload.required_plan_label || 'Pro') + ' plan.\nOpen the upgrade page?')) { |
| 49 |
window.open(upgradeUrl, '_blank'); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// Returns the jQuery deferred so bulk callers can wait on completion |
| 54 |
// before reloading the page. When a bulk operation is in flight (the |
| 55 |
// bulk handler sets window.EasyInvoiceAddons.bulkInFlight = true), we |
| 56 |
// skip the per-toggle reload — the bulk handler will issue ONE reload |
| 57 |
// after all of its toggles resolve. Without this guard, the first |
| 58 |
// single-toggle AJAX to come back would window.location.reload() and |
| 59 |
// cancel every other in-flight bulk request. |
| 60 |
function toggleAddon($card, enable) { |
| 61 |
var addonId = $card.data('addon-id'); |
| 62 |
var $btn = $card.find('.ei-addons-btn--toggle'); |
| 63 |
setButtonLoading($btn, true, enable ? cfg.strings.enabling : cfg.strings.disabling); |
| 64 |
|
| 65 |
return $.post(cfg.ajaxUrl, { |
| 66 |
action: 'easy_invoice_toggle_addon', |
| 67 |
_nonce: cfg.nonce, |
| 68 |
addon_id: addonId, |
| 69 |
enabled: enable ? 1 : 0 |
| 70 |
}).done(function (resp) { |
| 71 |
setButtonLoading($btn, false); |
| 72 |
|
| 73 |
if (resp && resp.success) { |
| 74 |
toast(resp.data.message, 'success'); |
| 75 |
if (!window.EasyInvoiceAddons || !window.EasyInvoiceAddons.bulkInFlight) { |
| 76 |
setTimeout(function () { window.location.reload(); }, 600); |
| 77 |
} |
| 78 |
return; |
| 79 |
} |
| 80 |
var payload = (resp && resp.data) || {}; |
| 81 |
if (payload.code === 'upgrade_required') { |
| 82 |
showUpgradeDialog($card, payload); |
| 83 |
return; |
| 84 |
} |
| 85 |
toast(payload.message || cfg.strings.genericError, 'error'); |
| 86 |
}).fail(function () { |
| 87 |
setButtonLoading($btn, false); |
| 88 |
toast(cfg.strings.genericError, 'error'); |
| 89 |
}); |
| 90 |
} |
| 91 |
|
| 92 |
$(document).on('click', '.ei-addons-btn--toggle', function (e) { |
| 93 |
e.preventDefault(); |
| 94 |
var $btn = $(this); |
| 95 |
var $card = $btn.closest('.ei-addons-card'); |
| 96 |
var action = $btn.data('action'); |
| 97 |
toggleAddon($card, action === 'enable'); |
| 98 |
}); |
| 99 |
|
| 100 |
$(document).on('click', '.ei-addons-btn--upgrade', function (e) { |
| 101 |
e.preventDefault(); |
| 102 |
var $btn = $(this); |
| 103 |
var $card = $btn.closest('.ei-addons-card'); |
| 104 |
showUpgradeDialog($card, { |
| 105 |
required_plan: $btn.data('required-plan'), |
| 106 |
required_plan_label: $btn.data('required-plan-label'), |
| 107 |
upgrade_url: $btn.data('upgrade-url') |
| 108 |
}); |
| 109 |
}); |
| 110 |
|
| 111 |
// Filter chips — swap Tailwind colour utilities to indicate active state |
| 112 |
var ACTIVE_CLASSES = ['bg-gray-900', 'text-white']; |
| 113 |
var INACTIVE_CLASSES = ['bg-white', 'text-gray-700', 'border', 'border-gray-300', 'hover:bg-gray-50']; |
| 114 |
|
| 115 |
$(document).on('click', '#ei-addons-filters .ei-addons-chip', function () { |
| 116 |
var $chip = $(this); |
| 117 |
var filter = $chip.data('filter'); |
| 118 |
|
| 119 |
$('#ei-addons-filters .ei-addons-chip') |
| 120 |
.removeClass('is-active') |
| 121 |
.removeClass(ACTIVE_CLASSES.join(' ')) |
| 122 |
.addClass(INACTIVE_CLASSES.join(' ')); |
| 123 |
|
| 124 |
$chip |
| 125 |
.addClass('is-active') |
| 126 |
.removeClass(INACTIVE_CLASSES.join(' ')) |
| 127 |
.addClass(ACTIVE_CLASSES.join(' ')); |
| 128 |
|
| 129 |
$('#ei-addons-grid .ei-addons-card').each(function () { |
| 130 |
var $card = $(this); |
| 131 |
var match = (filter === 'all') || ($card.data('category') === filter); |
| 132 |
$card.toggleClass('is-hidden', !match); |
| 133 |
}); |
| 134 |
}); |
| 135 |
|
| 136 |
})(jQuery); |
| 137 |
|