Нет описания

admin-batch.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*******************************************************************************
  2. * Copyright (c) 2019, Code Atlantic LLC
  3. ******************************************************************************/
  4. "use strict";
  5. (function ($) {
  6. /**
  7. * Batch Processor.
  8. *
  9. * @since 1.7.0
  10. */
  11. var batch = {
  12. form: {
  13. beforeSubmit: function ($form) {
  14. var $submit = $form.find('.pum-field-submit input[type="submit"]'),
  15. $messages = $form.find('.pum-upgrade-messages'),
  16. $progress = $form.find('.pum-batch-progress'),
  17. // Handle the Are You Sure (AYS) if present on the form element.
  18. ays = $form.data('ays');
  19. if (!$submit.hasClass('button-disabled')) {
  20. if (ays !== undefined && !confirm(ays)) {
  21. return false;
  22. }
  23. $progress.removeClass('pum-batch-progress--active');
  24. $progress.find('progress').prop('value', null);
  25. // Clear messages.
  26. $messages.html('');
  27. // Disable the button.
  28. $submit.addClass('button-disabled');
  29. // Add the spinner.
  30. $('<span class="spinner is-active"></span>').insertAfter($submit);
  31. return true;
  32. }
  33. return false;
  34. }
  35. },
  36. complete: function ($form) {
  37. var $alert = $form.parents('.pum-alert');
  38. $form.find('.pum-field-submit, progress').hide();
  39. $('p.pum-upgrade-notice').hide();
  40. $alert.removeClass('pum-alert__warning').addClass('pum-alert__success');
  41. $alert.prepend('<h2>' + pum_batch_vars.complete + '</h2>');
  42. },
  43. action: 'pum_process_batch_request',
  44. /**
  45. * Processes a single batch of data.
  46. *
  47. * @param {integer|number|string} step Step in the process.
  48. * @param {object} data Form data.
  49. */
  50. process_step: function (step, data) {
  51. var self = this;
  52. $.ajax({
  53. type: 'POST',
  54. url: ajaxurl,
  55. data: {
  56. batch_id: data.batch_id,
  57. action: self.action,
  58. nonce: data.nonce,
  59. form: data.form,
  60. step: parseInt(step),
  61. data: data
  62. },
  63. dataType: "json",
  64. success: function (response) {
  65. if (response.data.done || response.data.error) {
  66. var batchSelector = response.data.mapping ? '.pum-batch-import-form' : '.pum-batch-form',
  67. // We need to get the actual in progress form, not all forms on the page
  68. $batchForm = $(batchSelector),
  69. spinner = $batchForm.find('.spinner'),
  70. notice_wrap = $batchForm.find('.notice-wrap');
  71. $batchForm.find('.button-disabled').removeClass('button-disabled');
  72. if (response.data.error) {
  73. spinner.remove();
  74. notice_wrap.html('<div class="updated error"><p>' + response.data.error + '</p></div>');
  75. } else if (response.data.done) {
  76. spinner.remove();
  77. notice_wrap.html('<div id="pum-batch-success" class="updated notice"><p class="pum-batch-success">' + response.data.message + '</p></div>');
  78. if (response.data.url) {
  79. window.location = response.data.url;
  80. }
  81. } else {
  82. notice_wrap.remove();
  83. }
  84. } else {
  85. $('.pum-batch-progress div').animate({
  86. width: response.data.percentage + '%'
  87. }, 50);
  88. self.process_step(response.data.step, data);
  89. }
  90. }
  91. }).fail(function (response) {
  92. if (window.console && window.console.log) {
  93. console.log(response);
  94. }
  95. });
  96. }
  97. },
  98. batch_upgrades = $.extend(true, {}, batch, {
  99. action: 'pum_process_upgrade_request',
  100. /**
  101. * Processes a que of batch upgrades.
  102. *
  103. * @param {integer|number|string} step Step in the process.
  104. * @param {object} data Form data.
  105. */
  106. process_step: function (step, data) {
  107. var self = this;
  108. $.ajax({
  109. type: 'POST',
  110. url: ajaxurl,
  111. data: {
  112. upgrade_id: data.upgrade_id,
  113. action: self.action,
  114. nonce: data.nonce,
  115. form: data.form,
  116. step: parseInt(step),
  117. data: data
  118. },
  119. dataType: "json",
  120. success: function (response) {
  121. var $form = $('.pum-upgrade-form'), // We need to get the actual in progress form, not all forms on the page
  122. $spinner = $form.find('.spinner'),
  123. $submit = $form.find('.button-disabled'),
  124. $messages = $form.find('.pum-upgrade-messages');
  125. if (response.data.done || response.data.error) {
  126. // Reset submit button.
  127. $submit.removeClass('button-disabled');
  128. if (response.data.error) {
  129. $spinner.remove();
  130. $messages.prepend('<div class="notice notice-error notice-alt"><p>' + response.data.error + '</p></div>');
  131. } else if (response.data.done) {
  132. $messages.prepend('<div class="notice notice-success"><p><strong>' + response.data.message + '</strong></p></div>');
  133. if (response.data.next) {
  134. $form
  135. .data('upgrade_id', response.data.next)
  136. .data('step', 1)
  137. .data('ays', false);
  138. self.process_step(1, {
  139. upgrade_id: response.data.next,
  140. nonce: data.nonce,
  141. form: data.form
  142. });
  143. } else {
  144. $submit.parent().hide();
  145. $spinner.remove();
  146. batch.complete($form);
  147. }
  148. if (response.data.url) {
  149. window.location = response.data.url;
  150. }
  151. } else {
  152. if (response.data.message !== '') {
  153. $messages.prepend('<div class="notice"><p class="">' + response.data.message + '</p></div>');
  154. }
  155. }
  156. } else {
  157. if (response.data.message !== '') {
  158. $messages.prepend('<div class="notice"><p class="">' + response.data.message + '</p></div>');
  159. }
  160. $('.pum-batch-progress').addClass('pum-batch-progress--active');
  161. $('.pum-batch-progress progress.pum-task-progress').addClass('active').val(response.data.percentage);
  162. self.process_step(response.data.step, data);
  163. }
  164. }
  165. }).fail(function (response) {
  166. if (window.console && window.console.log) {
  167. console.log(response);
  168. }
  169. });
  170. }
  171. });
  172. // Import this module.
  173. window.PUM_Admin = window.PUM_Admin || {};
  174. window.PUM_Admin.batch = batch;
  175. window.PUM_Admin.batch_upgrades = batch_upgrades;
  176. /**
  177. * Handles form submission preceding batch processing.
  178. */
  179. $(document)
  180. .on('submit', '.pum-batch-form[data-batch_id]', function (event) {
  181. var $this = $(this),
  182. submitButton = $this.find('input[type="submit"]'),
  183. // Handle the Are You Sure (AYS) if present on the form element.
  184. ays = $this.data('ays'),
  185. data = {
  186. batch_id: $this.data('batch_id'),
  187. nonce: $this.data('nonce'),
  188. form: $this.serializeAssoc(),
  189. test: $this.pumSerializeObject()
  190. };
  191. event.preventDefault();
  192. if (!submitButton.hasClass('button-disabled')) {
  193. if (ays !== undefined && !confirm(ays)) {
  194. return;
  195. }
  196. // Remove existing notice & progress bars.
  197. $this.find('.notice-wrap').remove();
  198. // Add the progress bar.
  199. $this.append($('<div class="notice-wrap"><div class="pum-batch-progress"><div></div>'));
  200. // Disable the button.
  201. submitButton.addClass('button-disabled');
  202. // Add the spinner.
  203. submitButton.parent().append('<span class="spinner is-active"></span>');
  204. // Start the process.
  205. batch.process_step(1, data);
  206. }
  207. })
  208. .on('submit', '.pum-batch-form.pum-upgrade-form[data-upgrade_id]', function (event) {
  209. var $form = $(this),
  210. data = {
  211. upgrade_id: $form.data('upgrade_id'),
  212. nonce: $form.data('nonce'),
  213. form: $form.serializeAssoc(),
  214. test: $form.pumSerializeObject()
  215. };
  216. event.preventDefault();
  217. // Process presubmit actions like showing progress data and validating info.
  218. if (batch_upgrades.form.beforeSubmit($form)) {
  219. // Start the process.
  220. batch_upgrades.process_step($form.data('step') || 1, data);
  221. }
  222. });
  223. }(jQuery));
  224. // Initiate when ready.
  225. jQuery(function ($) {
  226. $.extend({
  227. arrayMerge: function () {
  228. var a = {};
  229. var n = 0;
  230. var argv = $.arrayMerge.arguments;
  231. for (var i = 0; i < argv.length; i++) {
  232. if (Array.isArray(argv[i])) {
  233. for (var j = 0; j < argv[i].length; j++) {
  234. a[n++] = argv[i][j];
  235. }
  236. a = $.makeArray(a);
  237. } else {
  238. for (var k in argv[i]) {
  239. if (argv[i].hasOwnProperty(k)) {
  240. if (isNaN(k)) {
  241. var v = argv[i][k];
  242. if (typeof v === 'object' && a[k]) {
  243. v = $.arrayMerge(a[k], v);
  244. }
  245. a[k] = v;
  246. } else {
  247. a[n++] = argv[i][k];
  248. }
  249. }
  250. }
  251. }
  252. }
  253. return a;
  254. },
  255. count: function (arr) {
  256. return Array.isArray(arr) ? arr.length : typeof arr === 'object' ? Object.keys(arr).length : false;
  257. }
  258. });
  259. $.fn.extend({
  260. serializeAssoc: function () {
  261. var o = {
  262. aa: {},
  263. add: function (name, value) {
  264. var tmp = name.match(/^(.*)\[([^\]]*)]$/),
  265. v = {};
  266. if (tmp) {
  267. if (tmp[2])
  268. v[tmp[2]] = value;
  269. else
  270. v[$.count(v)] = value;
  271. this.add(tmp[1], v);
  272. } else if (typeof value === 'object') {
  273. if (typeof this.aa[name] !== 'object') {
  274. this.aa[name] = {};
  275. }
  276. this.aa[name] = $.arrayMerge(this.aa[name], value);
  277. } else {
  278. this.aa[name] = value;
  279. }
  280. }
  281. };
  282. var a = $(this).serializeArray();
  283. for (var i = 0; i < a.length; i++) {
  284. o.add(a[i].name, a[i].value);
  285. }
  286. return o.aa;
  287. }
  288. });
  289. });