123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- /**
- * @api
- */
- define([
- 'underscore',
- 'uiRegistry',
- 'mageUtils',
- 'Magento_Ui/js/lib/collapsible',
- 'Magento_Ui/js/modal/confirm',
- 'Magento_Ui/js/modal/alert',
- 'mage/translate'
- ], function (_, registry, utils, Collapsible, confirm, alert, $t) {
- 'use strict';
- return Collapsible.extend({
- defaults: {
- template: 'ui/grid/actions',
- stickyTmpl: 'ui/grid/sticky/actions',
- selectProvider: 'ns = ${ $.ns }, index = ids',
- actions: [],
- noItemsMsg: $t('You haven\'t selected any items!'),
- modules: {
- selections: '${ $.selectProvider }'
- }
- },
- /**
- * Initializes observable properties.
- *
- * @returns {Massactions} Chainable.
- */
- initObservable: function () {
- this._super()
- .observe('actions');
- return this;
- },
- /**
- * Applies specified action.
- *
- * @param {String} actionIndex - Actions' identifier.
- * @returns {Massactions} Chainable.
- */
- applyAction: function (actionIndex) {
- var data = this.getSelections(),
- action,
- callback;
- if (!data.total) {
- alert({
- content: this.noItemsMsg
- });
- return this;
- }
- action = this.getAction(actionIndex);
- callback = this._getCallback(action, data);
- action.confirm ?
- this._confirm(action, callback) :
- callback();
- return this;
- },
- /**
- * Retrieves selections data from the selections provider.
- *
- * @returns {Object|Undefined}
- */
- getSelections: function () {
- var provider = this.selections(),
- selections = provider && provider.getSelections();
- return selections;
- },
- /**
- * Retrieves action object associated with a specified index.
- *
- * @param {String} actionIndex - Actions' identifier.
- * @returns {Object} Action object.
- */
- getAction: function (actionIndex) {
- return _.findWhere(this.actions(), {
- type: actionIndex
- });
- },
- /**
- * Adds new action. If action with a specified identifier
- * already exists, than the original one will be overrided.
- *
- * @param {Object} action - Action object.
- * @returns {Massactions} Chainable.
- */
- addAction: function (action) {
- var actions = this.actions(),
- index = _.findIndex(actions, {
- type: action.type
- });
- ~index ?
- actions[index] = action :
- actions.push(action);
- this.actions(actions);
- return this;
- },
- /**
- * Creates action callback based on its' data. If action doesn't spicify
- * a callback function than the default one will be used.
- *
- * @private
- * @param {Object} action - Actions' object.
- * @param {Object} selections - Selections data.
- * @returns {Function} Callback function.
- */
- _getCallback: function (action, selections) {
- var callback = action.callback,
- args = [action, selections];
- if (utils.isObject(callback)) {
- args.unshift(callback.target);
- callback = registry.async(callback.provider);
- } else if (typeof callback != 'function') {
- callback = this.defaultCallback.bind(this);
- }
- return function () {
- callback.apply(null, args);
- };
- },
- /**
- * Default action callback. Sends selections data
- * via POST request.
- *
- * @param {Object} action - Action data.
- * @param {Object} data - Selections data.
- */
- defaultCallback: function (action, data) {
- var itemsType = data.excludeMode ? 'excluded' : 'selected',
- selections = {};
- if (itemsType === 'excluded' && data.selected && data.selected.length) {
- itemsType = 'selected';
- data[itemsType] = _.difference(data.selected, data.excluded);
- }
- selections[itemsType] = data[itemsType];
- if (!selections[itemsType].length) {
- selections[itemsType] = false;
- }
- _.extend(selections, data.params || {});
- utils.submit({
- url: action.url,
- data: selections
- });
- },
- /**
- * Shows actions' confirmation window.
- *
- * @param {Object} action - Actions' data.
- * @param {Function} callback - Callback that will be
- * invoked if action is confirmed.
- */
- _confirm: function (action, callback) {
- var confirmData = action.confirm,
- data = this.getSelections(),
- total = data.total ? data.total : 0,
- confirmMessage = confirmData.message + ' (' + total + ' record' + (total > 1 ? 's' : '') + ')';
- confirm({
- title: confirmData.title,
- content: confirmMessage,
- actions: {
- confirm: callback
- }
- });
- }
- });
- });
|