admin.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. (function ($) {
  2. 'use strict';
  3. $(document).ready(function () {
  4. // ========================================
  5. // Product edit page: Free Photo tab
  6. // ========================================
  7. if ($('#studiou_fpp_product_data').length) {
  8. initProductTab();
  9. }
  10. // ========================================
  11. // Admin summary page
  12. // ========================================
  13. if ($('.studiou-fpp-admin-wrap').length) {
  14. initSummaryPage();
  15. }
  16. function initProductTab() {
  17. // Show/hide new media category form
  18. $('.studiou-fpp-add-media-cat-btn').on('click', function () {
  19. $('.studiou-fpp-new-media-cat-form').toggle();
  20. });
  21. $('.studiou-fpp-cancel-media-cat-btn').on('click', function () {
  22. $('.studiou-fpp-new-media-cat-form').hide();
  23. $('#studiou_fpp_new_cat_name').val('');
  24. });
  25. // Save new media category
  26. $('.studiou-fpp-save-media-cat-btn').on('click', function () {
  27. var name = $('#studiou_fpp_new_cat_name').val().trim();
  28. if (!name) {
  29. return;
  30. }
  31. var $btn = $(this);
  32. $btn.prop('disabled', true);
  33. $.ajax({
  34. url: studiouWcfpp.ajaxUrl,
  35. type: 'POST',
  36. data: {
  37. action: 'studiou_wcfpp_add_media_category',
  38. nonce: studiouWcfpp.nonce,
  39. name: name
  40. },
  41. success: function (response) {
  42. if (response.success) {
  43. var $select = $('#_studiou_fpp_media_category');
  44. $select.append(
  45. $('<option>', {
  46. value: response.data.term_id,
  47. text: response.data.name,
  48. selected: true
  49. })
  50. );
  51. $('.studiou-fpp-new-media-cat-form').hide();
  52. $('#studiou_fpp_new_cat_name').val('');
  53. } else {
  54. alert(response.data ? response.data.message : studiouWcfpp.i18n.error);
  55. }
  56. },
  57. error: function () {
  58. alert(studiouWcfpp.i18n.error);
  59. },
  60. complete: function () {
  61. $btn.prop('disabled', false);
  62. }
  63. });
  64. });
  65. }
  66. function initSummaryPage() {
  67. var $noticeArea = $('.studiou-fpp-admin-notice-area');
  68. // Select all
  69. $('#studiou-fpp-select-all').on('change', function () {
  70. $('.studiou-fpp-file-cb').prop('checked', $(this).is(':checked'));
  71. });
  72. // Individual status change
  73. $('.studiou-fpp-status-select').on('change', function () {
  74. var fileId = $(this).data('file-id');
  75. var status = $(this).val();
  76. var $el = $(this);
  77. $.ajax({
  78. url: studiouWcfpp.ajaxUrl,
  79. type: 'POST',
  80. data: {
  81. action: 'studiou_wcfpp_update_status',
  82. nonce: studiouWcfpp.nonce,
  83. file_id: fileId,
  84. status: status
  85. },
  86. success: function (response) {
  87. if (response.success) {
  88. showAdminNotice(studiouWcfpp.i18n.statusUpdated, 'success');
  89. } else {
  90. showAdminNotice(response.data ? response.data.message : studiouWcfpp.i18n.error, 'error');
  91. }
  92. },
  93. error: function () {
  94. showAdminNotice(studiouWcfpp.i18n.error, 'error');
  95. }
  96. });
  97. });
  98. // Delete single
  99. $(document).on('click', '.studiou-fpp-delete-single', function () {
  100. var fileId = $(this).data('file-id');
  101. if (!confirm(studiouWcfpp.i18n.confirmDeleteSingle)) {
  102. return;
  103. }
  104. $.ajax({
  105. url: studiouWcfpp.ajaxUrl,
  106. type: 'POST',
  107. data: {
  108. action: 'studiou_wcfpp_delete_files',
  109. nonce: studiouWcfpp.nonce,
  110. file_ids: [fileId]
  111. },
  112. success: function (response) {
  113. if (response.success) {
  114. $('tr[data-file-id="' + fileId + '"]').fadeOut(300, function () {
  115. $(this).remove();
  116. });
  117. showAdminNotice(response.data.message, 'success');
  118. } else {
  119. showAdminNotice(response.data ? response.data.message : studiouWcfpp.i18n.error, 'error');
  120. }
  121. },
  122. error: function () {
  123. showAdminNotice(studiouWcfpp.i18n.error, 'error');
  124. }
  125. });
  126. });
  127. // Bulk action apply
  128. $('#studiou-fpp-bulk-apply').on('click', function () {
  129. var action = $('#studiou-fpp-bulk-action').val();
  130. if (!action) {
  131. return;
  132. }
  133. var fileIds = getSelectedFileIds();
  134. if (fileIds.length === 0) {
  135. showAdminNotice(studiouWcfpp.i18n.noFilesSelected, 'error');
  136. return;
  137. }
  138. var confirmMsg = '';
  139. if (action === 'delete') {
  140. confirmMsg = studiouWcfpp.i18n.confirmDelete;
  141. } else if (action === 'download_zip') {
  142. confirmMsg = studiouWcfpp.i18n.confirmDownloadZip;
  143. } else if (action.startsWith('status_')) {
  144. confirmMsg = studiouWcfpp.i18n.confirmStatusChange;
  145. }
  146. if (confirmMsg && !confirm(confirmMsg)) {
  147. return;
  148. }
  149. if (action === 'download_zip') {
  150. bulkDownloadZip(fileIds);
  151. } else if (action === 'delete') {
  152. bulkDelete(fileIds);
  153. } else if (action.startsWith('status_')) {
  154. var status = action.replace('status_', '');
  155. bulkUpdateStatus(fileIds, status);
  156. }
  157. });
  158. function getSelectedFileIds() {
  159. var ids = [];
  160. $('.studiou-fpp-file-cb:checked').each(function () {
  161. ids.push($(this).val());
  162. });
  163. return ids;
  164. }
  165. function bulkDownloadZip(fileIds) {
  166. showBulkSpinner(true);
  167. showAdminNotice(studiouWcfpp.i18n.generatingZip, 'info');
  168. $.ajax({
  169. url: studiouWcfpp.ajaxUrl,
  170. type: 'POST',
  171. data: {
  172. action: 'studiou_wcfpp_download_zip',
  173. nonce: studiouWcfpp.nonce,
  174. file_ids: fileIds
  175. },
  176. success: function (response) {
  177. if (response.success) {
  178. // Trigger download
  179. window.location.href = response.data.download_url;
  180. showAdminNotice(
  181. response.data.file_count + ' file(s) in archive.',
  182. 'success'
  183. );
  184. } else {
  185. showAdminNotice(response.data ? response.data.message : studiouWcfpp.i18n.error, 'error');
  186. }
  187. },
  188. error: function () {
  189. showAdminNotice(studiouWcfpp.i18n.error, 'error');
  190. },
  191. complete: function () {
  192. showBulkSpinner(false);
  193. }
  194. });
  195. }
  196. function bulkDelete(fileIds) {
  197. showBulkSpinner(true);
  198. $.ajax({
  199. url: studiouWcfpp.ajaxUrl,
  200. type: 'POST',
  201. data: {
  202. action: 'studiou_wcfpp_delete_files',
  203. nonce: studiouWcfpp.nonce,
  204. file_ids: fileIds
  205. },
  206. success: function (response) {
  207. if (response.success) {
  208. fileIds.forEach(function (id) {
  209. $('tr[data-file-id="' + id + '"]').fadeOut(300, function () {
  210. $(this).remove();
  211. });
  212. });
  213. showAdminNotice(response.data.message, 'success');
  214. } else {
  215. showAdminNotice(response.data ? response.data.message : studiouWcfpp.i18n.error, 'error');
  216. }
  217. },
  218. error: function () {
  219. showAdminNotice(studiouWcfpp.i18n.error, 'error');
  220. },
  221. complete: function () {
  222. showBulkSpinner(false);
  223. }
  224. });
  225. }
  226. function bulkUpdateStatus(fileIds, status) {
  227. showBulkSpinner(true);
  228. $.ajax({
  229. url: studiouWcfpp.ajaxUrl,
  230. type: 'POST',
  231. data: {
  232. action: 'studiou_wcfpp_bulk_status',
  233. nonce: studiouWcfpp.nonce,
  234. file_ids: fileIds,
  235. status: status
  236. },
  237. success: function (response) {
  238. if (response.success) {
  239. // Update dropdowns
  240. fileIds.forEach(function (id) {
  241. $('tr[data-file-id="' + id + '"] .studiou-fpp-status-select').val(status);
  242. });
  243. showAdminNotice(response.data.message, 'success');
  244. } else {
  245. showAdminNotice(response.data ? response.data.message : studiouWcfpp.i18n.error, 'error');
  246. }
  247. },
  248. error: function () {
  249. showAdminNotice(studiouWcfpp.i18n.error, 'error');
  250. },
  251. complete: function () {
  252. showBulkSpinner(false);
  253. }
  254. });
  255. }
  256. function showBulkSpinner(show) {
  257. $('#studiou-fpp-bulk-spinner').css('visibility', show ? 'visible' : 'hidden');
  258. }
  259. function showAdminNotice(message, type) {
  260. var cls = 'notice-' + (type === 'error' ? 'error' : (type === 'info' ? 'info' : 'success'));
  261. $noticeArea.html(
  262. '<div class="notice ' + cls + ' is-dismissible"><p>' + escHtml(message) + '</p>' +
  263. '<button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss</span></button></div>'
  264. );
  265. $noticeArea.find('.notice-dismiss').on('click', function () {
  266. $(this).closest('.notice').fadeOut(200, function () { $(this).remove(); });
  267. });
  268. }
  269. }
  270. function escHtml(str) {
  271. var div = document.createElement('div');
  272. div.appendChild(document.createTextNode(str));
  273. return div.innerHTML;
  274. }
  275. });
  276. })(jQuery);