tree-massactions.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. /**
  6. * @api
  7. */
  8. define([
  9. 'ko',
  10. 'underscore',
  11. 'Magento_Ui/js/grid/massactions'
  12. ], function (ko, _, Massactions) {
  13. 'use strict';
  14. return Massactions.extend({
  15. defaults: {
  16. template: 'ui/grid/tree-massactions',
  17. submenuTemplate: 'ui/grid/submenu',
  18. listens: {
  19. opened: 'hideSubmenus'
  20. }
  21. },
  22. /**
  23. * Initializes observable properties.
  24. *
  25. * @returns {Massactions} Chainable.
  26. */
  27. initObservable: function () {
  28. this._super()
  29. .recursiveObserveActions(this.actions());
  30. return this;
  31. },
  32. /**
  33. * Recursive initializes observable actions.
  34. *
  35. * @param {Array} actions - Action objects.
  36. * @param {String} [prefix] - An optional string that will be prepended
  37. * to the "type" field of all child actions.
  38. * @returns {Massactions} Chainable.
  39. */
  40. recursiveObserveActions: function (actions, prefix) {
  41. _.each(actions, function (action) {
  42. if (prefix) {
  43. action.type = prefix + '.' + action.type;
  44. }
  45. if (action.actions) {
  46. action.visible = ko.observable(false);
  47. action.parent = actions;
  48. this.recursiveObserveActions(action.actions, action.type);
  49. }
  50. }, this);
  51. return this;
  52. },
  53. /**
  54. * Applies specified action.
  55. *
  56. * @param {String} actionIndex - Actions' identifier.
  57. * @returns {Massactions} Chainable.
  58. */
  59. applyAction: function (actionIndex) {
  60. var action = this.getAction(actionIndex),
  61. visibility;
  62. if (action.visible) {
  63. visibility = action.visible();
  64. this.hideSubmenus(action.parent);
  65. action.visible(!visibility);
  66. return this;
  67. }
  68. return this._super(actionIndex);
  69. },
  70. /**
  71. * Retrieves action object associated with a specified index.
  72. *
  73. * @param {String} actionIndex - Actions' identifier.
  74. * @param {Array} actions - Action objects.
  75. * @returns {Object} Action object.
  76. */
  77. getAction: function (actionIndex, actions) {
  78. var currentActions = actions || this.actions(),
  79. result = false;
  80. _.find(currentActions, function (action) {
  81. if (action.type === actionIndex) {
  82. result = action;
  83. return true;
  84. }
  85. if (action.actions) {
  86. result = this.getAction(actionIndex, action.actions);
  87. return result;
  88. }
  89. }, this);
  90. return result;
  91. },
  92. /**
  93. * Recursive hide all sub folders in given array.
  94. *
  95. * @param {Array} actions - Action objects.
  96. * @returns {Massactions} Chainable.
  97. */
  98. hideSubmenus: function (actions) {
  99. var currentActions = actions || this.actions();
  100. _.each(currentActions, function (action) {
  101. if (action.visible && action.visible()) {
  102. action.visible(false);
  103. }
  104. if (action.actions) {
  105. this.hideSubmenus(action.actions);
  106. }
  107. }, this);
  108. return this;
  109. }
  110. });
  111. });