admin.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. if (action === 'download_zip') {
  139. bulkDownloadZip(fileIds);
  140. } else if (action === 'delete') {
  141. if (!confirm(studiouWcfpp.i18n.confirmDelete)) {
  142. return;
  143. }
  144. bulkDelete(fileIds);
  145. } else if (action.startsWith('status_')) {
  146. var status = action.replace('status_', '');
  147. bulkUpdateStatus(fileIds, status);
  148. }
  149. });
  150. function getSelectedFileIds() {
  151. var ids = [];
  152. $('.studiou-fpp-file-cb:checked').each(function () {
  153. ids.push($(this).val());
  154. });
  155. return ids;
  156. }
  157. function bulkDownloadZip(fileIds) {
  158. showBulkSpinner(true);
  159. showAdminNotice(studiouWcfpp.i18n.generatingZip, 'info');
  160. $.ajax({
  161. url: studiouWcfpp.ajaxUrl,
  162. type: 'POST',
  163. data: {
  164. action: 'studiou_wcfpp_download_zip',
  165. nonce: studiouWcfpp.nonce,
  166. file_ids: fileIds
  167. },
  168. success: function (response) {
  169. if (response.success) {
  170. // Trigger download
  171. window.location.href = response.data.download_url;
  172. showAdminNotice(
  173. response.data.file_count + ' file(s) in archive.',
  174. 'success'
  175. );
  176. } else {
  177. showAdminNotice(response.data ? response.data.message : studiouWcfpp.i18n.error, 'error');
  178. }
  179. },
  180. error: function () {
  181. showAdminNotice(studiouWcfpp.i18n.error, 'error');
  182. },
  183. complete: function () {
  184. showBulkSpinner(false);
  185. }
  186. });
  187. }
  188. function bulkDelete(fileIds) {
  189. showBulkSpinner(true);
  190. $.ajax({
  191. url: studiouWcfpp.ajaxUrl,
  192. type: 'POST',
  193. data: {
  194. action: 'studiou_wcfpp_delete_files',
  195. nonce: studiouWcfpp.nonce,
  196. file_ids: fileIds
  197. },
  198. success: function (response) {
  199. if (response.success) {
  200. fileIds.forEach(function (id) {
  201. $('tr[data-file-id="' + id + '"]').fadeOut(300, function () {
  202. $(this).remove();
  203. });
  204. });
  205. showAdminNotice(response.data.message, 'success');
  206. } else {
  207. showAdminNotice(response.data ? response.data.message : studiouWcfpp.i18n.error, 'error');
  208. }
  209. },
  210. error: function () {
  211. showAdminNotice(studiouWcfpp.i18n.error, 'error');
  212. },
  213. complete: function () {
  214. showBulkSpinner(false);
  215. }
  216. });
  217. }
  218. function bulkUpdateStatus(fileIds, status) {
  219. showBulkSpinner(true);
  220. $.ajax({
  221. url: studiouWcfpp.ajaxUrl,
  222. type: 'POST',
  223. data: {
  224. action: 'studiou_wcfpp_bulk_status',
  225. nonce: studiouWcfpp.nonce,
  226. file_ids: fileIds,
  227. status: status
  228. },
  229. success: function (response) {
  230. if (response.success) {
  231. // Update dropdowns
  232. fileIds.forEach(function (id) {
  233. $('tr[data-file-id="' + id + '"] .studiou-fpp-status-select').val(status);
  234. });
  235. showAdminNotice(response.data.message, 'success');
  236. } else {
  237. showAdminNotice(response.data ? response.data.message : studiouWcfpp.i18n.error, 'error');
  238. }
  239. },
  240. error: function () {
  241. showAdminNotice(studiouWcfpp.i18n.error, 'error');
  242. },
  243. complete: function () {
  244. showBulkSpinner(false);
  245. }
  246. });
  247. }
  248. function showBulkSpinner(show) {
  249. $('#studiou-fpp-bulk-spinner').css('visibility', show ? 'visible' : 'hidden');
  250. }
  251. function showAdminNotice(message, type) {
  252. var cls = 'notice-' + (type === 'error' ? 'error' : (type === 'info' ? 'info' : 'success'));
  253. $noticeArea.html(
  254. '<div class="notice ' + cls + ' is-dismissible"><p>' + escHtml(message) + '</p>' +
  255. '<button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss</span></button></div>'
  256. );
  257. $noticeArea.find('.notice-dismiss').on('click', function () {
  258. $(this).closest('.notice').fadeOut(200, function () { $(this).remove(); });
  259. });
  260. }
  261. }
  262. function escHtml(str) {
  263. var div = document.createElement('div');
  264. div.appendChild(document.createTextNode(str));
  265. return div.innerHTML;
  266. }
  267. });
  268. })(jQuery);