OrderAction.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /**
  4. * Licensed Materials - Property of IBM
  5. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2018, 2020
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. define(['../../../../lib/@waca/dashboard-common/dist/ui/interaction/Utils', '../../../../app/nls/StringResources', 'underscore', '../../../../lib/@waca/dashboard-common/dist/core/APIFactory', '../../../../lib/@waca/dashboard-common/dist/api/ContentActionsProviderAPI'], function (Util, stringResources, _, APIFactory, ContentActionsProviderAPI) {
  9. var OrderAction = function () {
  10. function OrderAction(_ref) {
  11. var features = _ref.features;
  12. _classCallCheck(this, OrderAction);
  13. if (features) {
  14. this.dashboard = features.API;
  15. features.ContentActions.registerProvider('order', this.getAPI());
  16. this._dashboardState = features.DashboardState;
  17. this._icons = features.Icons;
  18. }
  19. }
  20. OrderAction.prototype.getAPI = function getAPI() {
  21. if (!this._api) {
  22. this._api = APIFactory.createAPI(this, [ContentActionsProviderAPI]);
  23. }
  24. return this._api;
  25. };
  26. OrderAction.prototype.getLifeCycleHandlers = function getLifeCycleHandlers() {
  27. return [{
  28. name: 'post:dashboard.initialize',
  29. action: this.postDashboardInitialize.bind(this)
  30. }];
  31. };
  32. OrderAction.prototype.postDashboardInitialize = function postDashboardInitialize() {
  33. if (this.dashboard) {
  34. this.controller = this.dashboard.getFeature('InteractionController.internal');
  35. }
  36. return Promise.resolve();
  37. };
  38. OrderAction.prototype.destroy = function destroy() {
  39. this.dashboard = null;
  40. this.controller = null;
  41. };
  42. OrderAction.prototype.getNodes = function getNodes(idList) {
  43. return Util.getNodes(this.controller, idList);
  44. };
  45. OrderAction.prototype.getParentModel = function getParentModel(idList) {
  46. var nodes = this.getNodes(idList);
  47. if (nodes.length > 0) {
  48. return nodes[0]._layout.model.getParent();
  49. }
  50. };
  51. /**
  52. * Get the list of items that can be ordered and that belong to the same parent
  53. */
  54. OrderAction.prototype.getItems = function getItems(idList) {
  55. var parentModel = this.getParentModel(idList);
  56. return parentModel && parentModel.items.filter(function (item) {
  57. return item.type !== 'templateDropZone' && item.type !== 'templateIndicator';
  58. });
  59. };
  60. OrderAction.prototype._isAvailable = function _isAvailable(idList) {
  61. var nodes = this.getNodes(idList);
  62. var items = this.getItems(idList);
  63. return nodes.length && items.length > 1 && this.dashboard.getMode() === this.dashboard.MODES.EDIT && _.find(nodes, function (node) {
  64. return items.indexOf(node._layout.model) === -1;
  65. }) === undefined;
  66. };
  67. OrderAction.prototype._isDisabled = function _isDisabled() {
  68. return this._dashboardState.getUiState().focus;
  69. };
  70. OrderAction.prototype.getContentActionList = function getContentActionList(idList) {
  71. if (this._isAvailable(idList)) {
  72. return [{
  73. name: 'orderToFront',
  74. label: stringResources.get('toolbarActionOrderToFront'),
  75. icon: this._icons.getIcon('bring-to-front').id,
  76. type: 'Button',
  77. disabled: this._isDisabled.bind(this),
  78. actions: {
  79. apply: this.orderContent.bind(this, idList, this.getItems(idList).length - this.getNodes(idList).length)
  80. }
  81. }, {
  82. name: 'orderToBack',
  83. label: stringResources.get('toolbarActionOrderToBack'),
  84. icon: this._icons.getIcon('send-to-back').id,
  85. type: 'Button',
  86. disabled: this._isDisabled.bind(this),
  87. actions: {
  88. apply: this.orderContent.bind(this, idList, 0)
  89. }
  90. }];
  91. }
  92. return [];
  93. };
  94. /**
  95. * Set the order of the selected nodes.
  96. */
  97. OrderAction.prototype.orderContent = function orderContent(idList, order) {
  98. var items = this.getItems(idList);
  99. var nodes = this.getNodes(idList);
  100. var selectedModels = _.map(nodes, function (node) {
  101. return node._layout.model;
  102. });
  103. var parentModel = this.getParentModel(idList);
  104. var updateArray = [];
  105. var sortedSelection = [];
  106. var sortedLeftOver = [];
  107. var model = void 0,
  108. i = void 0,
  109. j = void 0,
  110. newOrder = void 0;
  111. for (i = 0, j = 0, newOrder = false; i < items.length; i++) {
  112. model = items[i];
  113. if (selectedModels.indexOf(model) === -1) {
  114. sortedLeftOver.push(model);
  115. j++;
  116. } else {
  117. sortedSelection.push(model);
  118. if (j !== order) {
  119. newOrder = true;
  120. }
  121. }
  122. }
  123. if (newOrder) {
  124. //Only update the model if this will result in a net change
  125. var modelToInsertBefore = sortedLeftOver[order];
  126. for (i = 0; i < selectedModels.length; i++) {
  127. model = selectedModels[i];
  128. updateArray.push({
  129. id: model.id,
  130. parentId: parentModel.id,
  131. insertBefore: modelToInsertBefore ? modelToInsertBefore.id : null
  132. });
  133. }
  134. parentModel.updateModel({ updateArray: updateArray });
  135. }
  136. };
  137. return OrderAction;
  138. }();
  139. return OrderAction;
  140. });
  141. //# sourceMappingURL=OrderAction.js.map