| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- (function ($) {
- 'use strict';
- $(document).ready(function () {
- // ========================================
- // Product edit page: Free Photo tab
- // ========================================
- if ($('#studiou_fpp_product_data').length) {
- initProductTab();
- }
- // ========================================
- // Product edit page: Variation quantity discount tiers
- // ========================================
- initVariationQtyTiers();
- // ========================================
- // Admin summary page
- // ========================================
- if ($('.studiou-fpp-admin-wrap').length) {
- initSummaryPage();
- }
- function initVariationQtyTiers() {
- // Add tier row
- $(document).on('click', '.studiou-fpp-qty-tier-add', function (e) {
- e.preventDefault();
- var $btn = $(this);
- var loop = $btn.data('loop');
- var $tbody = $btn.closest('.studiou-fpp-qty-tiers-field').find('.studiou-fpp-qty-tiers-table tbody');
- var idx = Date.now() + '_' + Math.floor(Math.random() * 10000);
- var row =
- '<tr class="studiou-fpp-qty-tier-row">' +
- '<td><input type="number" min="1" step="1" name="studiou_fpp_qty_tiers[' + loop + '][' + idx + '][from_qty]" value="" /></td>' +
- '<td><input type="number" min="0" max="100" step="0.01" name="studiou_fpp_qty_tiers[' + loop + '][' + idx + '][percent]" value="" /></td>' +
- '<td><button type="button" class="button studiou-fpp-qty-tier-remove">×</button></td>' +
- '</tr>';
- $tbody.append(row);
- markVariationChanged($btn);
- });
- // Remove tier row
- $(document).on('click', '.studiou-fpp-qty-tier-remove', function (e) {
- e.preventDefault();
- var $btn = $(this);
- $btn.closest('tr').remove();
- markVariationChanged($btn);
- });
- // Mark variation as modified on any input change inside the tier table
- $(document).on('change input', '.studiou-fpp-qty-tiers-table input', function () {
- markVariationChanged($(this));
- });
- function markVariationChanged($el) {
- var $variation = $el.closest('.woocommerce_variation');
- if ($variation.length) {
- $variation.addClass('variation-needs-update');
- $('button.cancel-variation-changes, button.save-variation-changes').removeAttr('disabled');
- $('#variable_product_options').trigger('woocommerce_variations_input_changed');
- }
- }
- }
- function initProductTab() {
- // Show/hide new media category form
- $('.studiou-fpp-add-media-cat-btn').on('click', function () {
- $('.studiou-fpp-new-media-cat-form').toggle();
- });
- $('.studiou-fpp-cancel-media-cat-btn').on('click', function () {
- $('.studiou-fpp-new-media-cat-form').hide();
- $('#studiou_fpp_new_cat_name').val('');
- });
- // Save new media category
- $('.studiou-fpp-save-media-cat-btn').on('click', function () {
- var name = $('#studiou_fpp_new_cat_name').val().trim();
- if (!name) {
- return;
- }
- var $btn = $(this);
- $btn.prop('disabled', true);
- $.ajax({
- url: studiouWcfpp.ajaxUrl,
- type: 'POST',
- data: {
- action: 'studiou_wcfpp_add_media_category',
- nonce: studiouWcfpp.nonce,
- name: name
- },
- success: function (response) {
- if (response.success) {
- var $select = $('#_studiou_fpp_media_category');
- $select.append(
- $('<option>', {
- value: response.data.term_id,
- text: response.data.name,
- selected: true
- })
- );
- $('.studiou-fpp-new-media-cat-form').hide();
- $('#studiou_fpp_new_cat_name').val('');
- } else {
- alert(response.data ? response.data.message : studiouWcfpp.i18n.error);
- }
- },
- error: function () {
- alert(studiouWcfpp.i18n.error);
- },
- complete: function () {
- $btn.prop('disabled', false);
- }
- });
- });
- }
- function initSummaryPage() {
- var $noticeArea = $('.studiou-fpp-admin-notice-area');
- // Select all
- $('#studiou-fpp-select-all').on('change', function () {
- $('.studiou-fpp-file-cb').prop('checked', $(this).is(':checked'));
- });
- // Individual status change
- $('.studiou-fpp-status-select').on('change', function () {
- var fileId = $(this).data('file-id');
- var status = $(this).val();
- var $el = $(this);
- $.ajax({
- url: studiouWcfpp.ajaxUrl,
- type: 'POST',
- data: {
- action: 'studiou_wcfpp_update_status',
- nonce: studiouWcfpp.nonce,
- file_id: fileId,
- status: status
- },
- success: function (response) {
- if (response.success) {
- showAdminNotice(studiouWcfpp.i18n.statusUpdated, 'success');
- } else {
- showAdminNotice(response.data ? response.data.message : studiouWcfpp.i18n.error, 'error');
- }
- },
- error: function () {
- showAdminNotice(studiouWcfpp.i18n.error, 'error');
- }
- });
- });
- // Delete single
- $(document).on('click', '.studiou-fpp-delete-single', function () {
- var fileId = $(this).data('file-id');
- if (!confirm(studiouWcfpp.i18n.confirmDeleteSingle)) {
- return;
- }
- $.ajax({
- url: studiouWcfpp.ajaxUrl,
- type: 'POST',
- data: {
- action: 'studiou_wcfpp_delete_files',
- nonce: studiouWcfpp.nonce,
- file_ids: [fileId]
- },
- success: function (response) {
- if (response.success) {
- $('tr[data-file-id="' + fileId + '"]').fadeOut(300, function () {
- $(this).remove();
- });
- showAdminNotice(response.data.message, 'success');
- } else {
- showAdminNotice(response.data ? response.data.message : studiouWcfpp.i18n.error, 'error');
- }
- },
- error: function () {
- showAdminNotice(studiouWcfpp.i18n.error, 'error');
- }
- });
- });
- // Bulk action apply
- $('#studiou-fpp-bulk-apply').on('click', function () {
- var action = $('#studiou-fpp-bulk-action').val();
- if (!action) {
- return;
- }
- var fileIds = getSelectedFileIds();
- if (fileIds.length === 0) {
- showAdminNotice(studiouWcfpp.i18n.noFilesSelected, 'error');
- return;
- }
- var confirmMsg = '';
- if (action === 'delete') {
- confirmMsg = studiouWcfpp.i18n.confirmDelete;
- } else if (action === 'download_zip') {
- confirmMsg = studiouWcfpp.i18n.confirmDownloadZip;
- } else if (action.startsWith('status_')) {
- confirmMsg = studiouWcfpp.i18n.confirmStatusChange;
- }
- if (confirmMsg && !confirm(confirmMsg)) {
- return;
- }
- if (action === 'download_zip') {
- bulkDownloadZip(fileIds);
- } else if (action === 'download_csv') {
- bulkDownloadCsv(fileIds);
- } else if (action === 'delete') {
- bulkDelete(fileIds);
- } else if (action.startsWith('status_')) {
- var status = action.replace('status_', '');
- bulkUpdateStatus(fileIds, status);
- }
- });
- function getSelectedFileIds() {
- var ids = [];
- $('.studiou-fpp-file-cb:checked').each(function () {
- ids.push($(this).val());
- });
- return ids;
- }
- function bulkDownloadZip(fileIds) {
- showBulkSpinner(true);
- showAdminNotice(studiouWcfpp.i18n.generatingZip, 'info');
- $.ajax({
- url: studiouWcfpp.ajaxUrl,
- type: 'POST',
- data: {
- action: 'studiou_wcfpp_download_zip',
- nonce: studiouWcfpp.nonce,
- file_ids: fileIds
- },
- success: function (response) {
- if (response.success) {
- // Trigger download
- window.location.href = response.data.download_url;
- showAdminNotice(
- response.data.file_count + ' file(s) in archive.',
- 'success'
- );
- } else {
- showAdminNotice(response.data ? response.data.message : studiouWcfpp.i18n.error, 'error');
- }
- },
- error: function () {
- showAdminNotice(studiouWcfpp.i18n.error, 'error');
- },
- complete: function () {
- showBulkSpinner(false);
- }
- });
- }
- function bulkDownloadCsv(fileIds) {
- showBulkSpinner(true);
- showAdminNotice(studiouWcfpp.i18n.generatingCsv || studiouWcfpp.i18n.generatingZip, 'info');
- $.ajax({
- url: studiouWcfpp.ajaxUrl,
- type: 'POST',
- data: {
- action: 'studiou_wcfpp_download_csv',
- nonce: studiouWcfpp.nonce,
- file_ids: fileIds
- },
- success: function (response) {
- if (response.success) {
- window.location.href = response.data.download_url;
- showAdminNotice(
- response.data.row_count + ' row(s) exported.',
- 'success'
- );
- } else {
- showAdminNotice(response.data ? response.data.message : studiouWcfpp.i18n.error, 'error');
- }
- },
- error: function () {
- showAdminNotice(studiouWcfpp.i18n.error, 'error');
- },
- complete: function () {
- showBulkSpinner(false);
- }
- });
- }
- function bulkDelete(fileIds) {
- showBulkSpinner(true);
- $.ajax({
- url: studiouWcfpp.ajaxUrl,
- type: 'POST',
- data: {
- action: 'studiou_wcfpp_delete_files',
- nonce: studiouWcfpp.nonce,
- file_ids: fileIds
- },
- success: function (response) {
- if (response.success) {
- fileIds.forEach(function (id) {
- $('tr[data-file-id="' + id + '"]').fadeOut(300, function () {
- $(this).remove();
- });
- });
- showAdminNotice(response.data.message, 'success');
- } else {
- showAdminNotice(response.data ? response.data.message : studiouWcfpp.i18n.error, 'error');
- }
- },
- error: function () {
- showAdminNotice(studiouWcfpp.i18n.error, 'error');
- },
- complete: function () {
- showBulkSpinner(false);
- }
- });
- }
- function bulkUpdateStatus(fileIds, status) {
- showBulkSpinner(true);
- $.ajax({
- url: studiouWcfpp.ajaxUrl,
- type: 'POST',
- data: {
- action: 'studiou_wcfpp_bulk_status',
- nonce: studiouWcfpp.nonce,
- file_ids: fileIds,
- status: status
- },
- success: function (response) {
- if (response.success) {
- // Update dropdowns
- fileIds.forEach(function (id) {
- $('tr[data-file-id="' + id + '"] .studiou-fpp-status-select').val(status);
- });
- showAdminNotice(response.data.message, 'success');
- } else {
- showAdminNotice(response.data ? response.data.message : studiouWcfpp.i18n.error, 'error');
- }
- },
- error: function () {
- showAdminNotice(studiouWcfpp.i18n.error, 'error');
- },
- complete: function () {
- showBulkSpinner(false);
- }
- });
- }
- function showBulkSpinner(show) {
- $('#studiou-fpp-bulk-spinner').css('visibility', show ? 'visible' : 'hidden');
- }
- function showAdminNotice(message, type) {
- var cls = 'notice-' + (type === 'error' ? 'error' : (type === 'info' ? 'info' : 'success'));
- $noticeArea.html(
- '<div class="notice ' + cls + ' is-dismissible"><p>' + escHtml(message) + '</p>' +
- '<button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss</span></button></div>'
- );
- $noticeArea.find('.notice-dismiss').on('click', function () {
- $(this).closest('.notice').fadeOut(200, function () { $(this).remove(); });
- });
- }
- }
- function escHtml(str) {
- var div = document.createElement('div');
- div.appendChild(document.createTextNode(str));
- return div.innerHTML;
- }
- });
- })(jQuery);
|