massactions.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'Magento_Ui/js/grid/massactions',
  7. 'Magento_Ui/js/modal/alert',
  8. 'underscore',
  9. 'jquery',
  10. 'mage/translate'
  11. ], function (Massactions, uiAlert, _, $, $t) {
  12. 'use strict';
  13. return Massactions.extend({
  14. defaults: {
  15. ajaxSettings: {
  16. method: 'POST',
  17. dataType: 'json'
  18. },
  19. listens: {
  20. massaction: 'onAction'
  21. }
  22. },
  23. /**
  24. * Reload customer addresses listing
  25. *
  26. * @param {Object} data
  27. */
  28. onAction: function (data) {
  29. if (data.action === 'delete') {
  30. this.source.reload({
  31. refresh: true
  32. });
  33. }
  34. },
  35. /**
  36. * Default action callback. Send selections data
  37. * via POST request.
  38. *
  39. * @param {Object} action - Action data.
  40. * @param {Object} data - Selections data.
  41. */
  42. defaultCallback: function (action, data) {
  43. var itemsType, selections;
  44. if (action.isAjax) {
  45. itemsType = data.excludeMode ? 'excluded' : 'selected';
  46. selections = {};
  47. selections[itemsType] = data[itemsType];
  48. if (!selections[itemsType].length) {
  49. selections[itemsType] = false;
  50. }
  51. _.extend(selections, data.params || {});
  52. this.request(action.url, selections).done(function (response) {
  53. if (!response.error) {
  54. this.trigger('massaction', {
  55. action: action.type,
  56. data: selections
  57. });
  58. }
  59. }.bind(this));
  60. } else {
  61. this._super();
  62. }
  63. },
  64. /**
  65. * Send customer address listing mass action ajax request
  66. *
  67. * @param {String} href
  68. * @param {Object} data
  69. */
  70. request: function (href, data) {
  71. var settings = _.extend({}, this.ajaxSettings, {
  72. url: href,
  73. data: data
  74. });
  75. $('body').trigger('processStart');
  76. return $.ajax(settings)
  77. .done(function (response) {
  78. if (response.error) {
  79. uiAlert({
  80. content: response.message
  81. });
  82. }
  83. })
  84. .fail(function () {
  85. uiAlert({
  86. content: $t('Sorry, there has been an error processing your request. Please try again later.')
  87. });
  88. })
  89. .always(function () {
  90. $('body').trigger('processStop');
  91. });
  92. }
  93. });
  94. });