123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- /**
- * @api
- */
- define([
- 'ko',
- 'underscore',
- 'Magento_Ui/js/grid/massactions'
- ], function (ko, _, Massactions) {
- 'use strict';
- return Massactions.extend({
- defaults: {
- template: 'ui/grid/tree-massactions',
- submenuTemplate: 'ui/grid/submenu',
- listens: {
- opened: 'hideSubmenus'
- }
- },
- /**
- * Initializes observable properties.
- *
- * @returns {Massactions} Chainable.
- */
- initObservable: function () {
- this._super()
- .recursiveObserveActions(this.actions());
- return this;
- },
- /**
- * Recursive initializes observable actions.
- *
- * @param {Array} actions - Action objects.
- * @param {String} [prefix] - An optional string that will be prepended
- * to the "type" field of all child actions.
- * @returns {Massactions} Chainable.
- */
- recursiveObserveActions: function (actions, prefix) {
- _.each(actions, function (action) {
- if (prefix) {
- action.type = prefix + '.' + action.type;
- }
- if (action.actions) {
- action.visible = ko.observable(false);
- action.parent = actions;
- this.recursiveObserveActions(action.actions, action.type);
- }
- }, this);
- return this;
- },
- /**
- * Applies specified action.
- *
- * @param {String} actionIndex - Actions' identifier.
- * @returns {Massactions} Chainable.
- */
- applyAction: function (actionIndex) {
- var action = this.getAction(actionIndex),
- visibility;
- if (action.visible) {
- visibility = action.visible();
- this.hideSubmenus(action.parent);
- action.visible(!visibility);
- return this;
- }
- return this._super(actionIndex);
- },
- /**
- * Retrieves action object associated with a specified index.
- *
- * @param {String} actionIndex - Actions' identifier.
- * @param {Array} actions - Action objects.
- * @returns {Object} Action object.
- */
- getAction: function (actionIndex, actions) {
- var currentActions = actions || this.actions(),
- result = false;
- _.find(currentActions, function (action) {
- if (action.type === actionIndex) {
- result = action;
- return true;
- }
- if (action.actions) {
- result = this.getAction(actionIndex, action.actions);
- return result;
- }
- }, this);
- return result;
- },
- /**
- * Recursive hide all sub folders in given array.
- *
- * @param {Array} actions - Action objects.
- * @returns {Massactions} Chainable.
- */
- hideSubmenus: function (actions) {
- var currentActions = actions || this.actions();
- _.each(currentActions, function (action) {
- if (action.visible && action.visible()) {
- action.visible(false);
- }
- if (action.actions) {
- this.hideSubmenus(action.actions);
- }
- }, this);
- return this;
- }
- });
- });
|