massactions.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. /**
  6. * @api
  7. */
  8. define([
  9. 'underscore',
  10. 'uiRegistry',
  11. 'mageUtils',
  12. 'Magento_Ui/js/lib/collapsible',
  13. 'Magento_Ui/js/modal/confirm',
  14. 'Magento_Ui/js/modal/alert',
  15. 'mage/translate'
  16. ], function (_, registry, utils, Collapsible, confirm, alert, $t) {
  17. 'use strict';
  18. return Collapsible.extend({
  19. defaults: {
  20. template: 'ui/grid/actions',
  21. stickyTmpl: 'ui/grid/sticky/actions',
  22. selectProvider: 'ns = ${ $.ns }, index = ids',
  23. actions: [],
  24. noItemsMsg: $t('You haven\'t selected any items!'),
  25. modules: {
  26. selections: '${ $.selectProvider }'
  27. }
  28. },
  29. /**
  30. * Initializes observable properties.
  31. *
  32. * @returns {Massactions} Chainable.
  33. */
  34. initObservable: function () {
  35. this._super()
  36. .observe('actions');
  37. return this;
  38. },
  39. /**
  40. * Applies specified action.
  41. *
  42. * @param {String} actionIndex - Actions' identifier.
  43. * @returns {Massactions} Chainable.
  44. */
  45. applyAction: function (actionIndex) {
  46. var data = this.getSelections(),
  47. action,
  48. callback;
  49. if (!data.total) {
  50. alert({
  51. content: this.noItemsMsg
  52. });
  53. return this;
  54. }
  55. action = this.getAction(actionIndex);
  56. callback = this._getCallback(action, data);
  57. action.confirm ?
  58. this._confirm(action, callback) :
  59. callback();
  60. return this;
  61. },
  62. /**
  63. * Retrieves selections data from the selections provider.
  64. *
  65. * @returns {Object|Undefined}
  66. */
  67. getSelections: function () {
  68. var provider = this.selections(),
  69. selections = provider && provider.getSelections();
  70. return selections;
  71. },
  72. /**
  73. * Retrieves action object associated with a specified index.
  74. *
  75. * @param {String} actionIndex - Actions' identifier.
  76. * @returns {Object} Action object.
  77. */
  78. getAction: function (actionIndex) {
  79. return _.findWhere(this.actions(), {
  80. type: actionIndex
  81. });
  82. },
  83. /**
  84. * Adds new action. If action with a specified identifier
  85. * already exists, than the original one will be overrided.
  86. *
  87. * @param {Object} action - Action object.
  88. * @returns {Massactions} Chainable.
  89. */
  90. addAction: function (action) {
  91. var actions = this.actions(),
  92. index = _.findIndex(actions, {
  93. type: action.type
  94. });
  95. ~index ?
  96. actions[index] = action :
  97. actions.push(action);
  98. this.actions(actions);
  99. return this;
  100. },
  101. /**
  102. * Creates action callback based on its' data. If action doesn't spicify
  103. * a callback function than the default one will be used.
  104. *
  105. * @private
  106. * @param {Object} action - Actions' object.
  107. * @param {Object} selections - Selections data.
  108. * @returns {Function} Callback function.
  109. */
  110. _getCallback: function (action, selections) {
  111. var callback = action.callback,
  112. args = [action, selections];
  113. if (utils.isObject(callback)) {
  114. args.unshift(callback.target);
  115. callback = registry.async(callback.provider);
  116. } else if (typeof callback != 'function') {
  117. callback = this.defaultCallback.bind(this);
  118. }
  119. return function () {
  120. callback.apply(null, args);
  121. };
  122. },
  123. /**
  124. * Default action callback. Sends selections data
  125. * via POST request.
  126. *
  127. * @param {Object} action - Action data.
  128. * @param {Object} data - Selections data.
  129. */
  130. defaultCallback: function (action, data) {
  131. var itemsType = data.excludeMode ? 'excluded' : 'selected',
  132. selections = {};
  133. if (itemsType === 'excluded' && data.selected && data.selected.length) {
  134. itemsType = 'selected';
  135. data[itemsType] = _.difference(data.selected, data.excluded);
  136. }
  137. selections[itemsType] = data[itemsType];
  138. if (!selections[itemsType].length) {
  139. selections[itemsType] = false;
  140. }
  141. _.extend(selections, data.params || {});
  142. utils.submit({
  143. url: action.url,
  144. data: selections
  145. });
  146. },
  147. /**
  148. * Shows actions' confirmation window.
  149. *
  150. * @param {Object} action - Actions' data.
  151. * @param {Function} callback - Callback that will be
  152. * invoked if action is confirmed.
  153. */
  154. _confirm: function (action, callback) {
  155. var confirmData = action.confirm,
  156. data = this.getSelections(),
  157. total = data.total ? data.total : 0,
  158. confirmMessage = confirmData.message + ' (' + total + ' record' + (total > 1 ? 's' : '') + ')';
  159. confirm({
  160. title: confirmData.title,
  161. content: confirmMessage,
  162. actions: {
  163. confirm: callback
  164. }
  165. });
  166. }
  167. });
  168. });