AlignAction.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. 2019, 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/core/APIFactory', '../AlignAPI', '../../../../../lib/@waca/dashboard-common/dist/ui/interaction/Utils', '../../../../../app/nls/StringResources', 'jquery', '../../../../../lib/@waca/dashboard-common/dist/api/ContentActionsProviderAPI'], function (APIFactory, AlignAPI, utils, stringResources, $, ContentActionsProviderAPI) {
  9. var AlignAction = function () {
  10. function AlignAction(_ref) {
  11. var features = _ref.features;
  12. _classCallCheck(this, AlignAction);
  13. if (features) {
  14. this.dashboard = features.API;
  15. features.ContentActions.registerProvider('align', this.getAPI());
  16. this._icons = features.Icons;
  17. }
  18. }
  19. AlignAction.prototype.getAPI = function getAPI() {
  20. if (!this.api) {
  21. this.api = APIFactory.createAPI(this, [AlignAPI, ContentActionsProviderAPI]);
  22. }
  23. return this.api;
  24. };
  25. AlignAction.prototype.getLifeCycleHandlers = function getLifeCycleHandlers() {
  26. return [{
  27. name: 'post:dashboard.initialize',
  28. action: this.postDashboardInitialize.bind(this)
  29. }];
  30. };
  31. AlignAction.prototype.postDashboardInitialize = function postDashboardInitialize() {
  32. if (this.dashboard) {
  33. this.controller = this.dashboard.getFeature('InteractionController.internal');
  34. }
  35. return Promise.resolve();
  36. };
  37. AlignAction.prototype.destroy = function destroy() {
  38. this.dashboard = null;
  39. this.controller = null;
  40. };
  41. AlignAction.prototype.getNodes = function getNodes(idList) {
  42. return utils.getNodes(this.controller, idList);
  43. };
  44. AlignAction.prototype._isEnabled = function _isEnabled(idList) {
  45. return idList.length > 1 && this.dashboard.getMode() === this.dashboard.MODES.EDIT;
  46. };
  47. AlignAction.prototype.getContentActionList = function getContentActionList(idList) {
  48. if (this._isEnabled(idList)) {
  49. return [{
  50. name: 'align',
  51. label: stringResources.get('toolbarActionAlign'),
  52. icon: this._icons.getIcon('align-horizontally').id,
  53. type: 'NextView',
  54. viewModule: 'dashboard-core/js/features/dashboard/actions/alignAction/impl/AlignDialog',
  55. viewOptions: {
  56. height: 290,
  57. width: 110, // those are hints only; for the placement of the toolbar.
  58. actions: {
  59. apply: this.applyAlignment.bind(this, idList)
  60. }
  61. }
  62. }];
  63. }
  64. return [];
  65. };
  66. AlignAction.prototype.applyAction = function applyAction(selection) {
  67. if (selection && selection.length && selection[0].name) {
  68. var position = selection[0].name;
  69. var idList = selection[0].layoutIds;
  70. this.applyAlignment(idList, position);
  71. }
  72. };
  73. AlignAction.prototype.getWidgetBoxes = function getWidgetBoxes(node) {
  74. var widgetPos = utils.position(node);
  75. var widgetSize = utils.widgetSize(node);
  76. var result = {
  77. boundingBox: $(node).position(),
  78. widgetBox: {
  79. top: widgetPos.top,
  80. left: widgetPos.left,
  81. width: widgetSize.width,
  82. height: widgetSize.height
  83. }
  84. };
  85. var boundingRect = node.getBoundingClientRect();
  86. result.boundingBox.width = boundingRect.width;
  87. result.boundingBox.height = boundingRect.height;
  88. return result;
  89. };
  90. // TODO this function is called multiple times with same args when click align action
  91. // we need a better way to avoid this, because we only need to do once when click align action
  92. AlignAction.prototype.applyAlignment = function applyAlignment(idList, position) {
  93. var _this = this;
  94. var alignmentFns = {
  95. left: this.getLeftAlignmentStyle,
  96. center: this.getCenterAlignmentStyle,
  97. right: this.getRightAlignmentStyle,
  98. top: this.getTopAlignmentStyle,
  99. middle: this.getMiddleAlignmentStyle,
  100. bottom: this.getBottomAlignmentStyle
  101. };
  102. var widgetBoxes = [];
  103. var selectedNodes = this.getNodes(idList);
  104. var widgetBox = this.getWidgetBoxes(selectedNodes[0]);
  105. widgetBoxes.push(widgetBox);
  106. var bounds = {
  107. top: widgetBox.boundingBox.top,
  108. left: widgetBox.boundingBox.left,
  109. bottom: widgetBox.boundingBox.top + widgetBox.boundingBox.height,
  110. right: widgetBox.boundingBox.left + widgetBox.boundingBox.width
  111. };
  112. for (var i = 1; i < selectedNodes.length; i++) {
  113. var _widgetBox = this.getWidgetBoxes(selectedNodes[i]);
  114. widgetBoxes.push(_widgetBox);
  115. bounds.top = Math.min(bounds.top, _widgetBox.boundingBox.top);
  116. bounds.left = Math.min(bounds.left, _widgetBox.boundingBox.left);
  117. bounds.bottom = Math.max(bounds.bottom, _widgetBox.boundingBox.top + _widgetBox.boundingBox.height);
  118. bounds.right = Math.max(bounds.right, _widgetBox.boundingBox.left + _widgetBox.boundingBox.width);
  119. }
  120. var positionUpdateArray = [];
  121. for (var _i = 0; _i < selectedNodes.length; _i++) {
  122. var updatedStyle = alignmentFns[position](bounds, widgetBoxes[_i], selectedNodes[_i]);
  123. if (updatedStyle) {
  124. positionUpdateArray.push({
  125. style: updatedStyle,
  126. id: selectedNodes[_i]._layout.model.id
  127. });
  128. }
  129. }
  130. if (positionUpdateArray.length) {
  131. var transactionApi = this.dashboard.getFeature('Transaction');
  132. var transactionToken = transactionApi.startTransaction();
  133. positionUpdateArray.forEach(function (positionUpdate) {
  134. var content = _this.dashboard.getCanvas().getContent(positionUpdate.id);
  135. Object.keys(positionUpdate.style).forEach(function (propertyName) {
  136. content.setPropertyValue(propertyName, positionUpdate.style[propertyName], transactionToken);
  137. });
  138. });
  139. transactionApi.endTransaction(transactionToken);
  140. }
  141. };
  142. AlignAction.prototype.getLeftAlignmentStyle = function getLeftAlignmentStyle(bounds, widgetBox, node) {
  143. var leftPos = bounds.left;
  144. if (Math.round(widgetBox.boundingBox.left) !== Math.round(widgetBox.widgetBox.left)) {
  145. var offset = widgetBox.boundingBox.left - widgetBox.widgetBox.left;
  146. leftPos = leftPos - offset;
  147. }
  148. //Sanity check the position against the minimum
  149. leftPos = Math.max(leftPos, node._layout.parentLayout.getMinimumLeft());
  150. return {
  151. left: leftPos + 'px'
  152. };
  153. };
  154. AlignAction.prototype.getCenterAlignmentStyle = function getCenterAlignmentStyle(bounds, widgetBox, node) {
  155. var centerLine = bounds.left + (bounds.right - bounds.left) / 2;
  156. var leftPos = centerLine - widgetBox.widgetBox.width / 2;
  157. if (Math.round(widgetBox.boundingBox.left) !== Math.round(widgetBox.widgetBox.left)) {
  158. var offset = widgetBox.boundingBox.left + widgetBox.boundingBox.width / 2 - (widgetBox.widgetBox.left + widgetBox.widgetBox.width / 2);
  159. leftPos = leftPos - offset;
  160. }
  161. //Sanity check the position against the minimum
  162. leftPos = Math.max(leftPos, node._layout.parentLayout.getMinimumLeft());
  163. return {
  164. left: leftPos + 'px'
  165. };
  166. };
  167. AlignAction.prototype.getRightAlignmentStyle = function getRightAlignmentStyle(bounds, widgetBox, node) {
  168. var leftPos = bounds.right - widgetBox.widgetBox.width;
  169. if (Math.round(widgetBox.boundingBox.width) !== Math.round(widgetBox.widgetBox.width)) {
  170. var offset = widgetBox.boundingBox.left + widgetBox.boundingBox.width - (widgetBox.widgetBox.left + widgetBox.widgetBox.width);
  171. leftPos = leftPos - offset;
  172. }
  173. //Sanity check the position against the minimum
  174. leftPos = Math.max(leftPos, node._layout.parentLayout.getMinimumLeft());
  175. return {
  176. left: leftPos + 'px'
  177. };
  178. };
  179. AlignAction.prototype.getTopAlignmentStyle = function getTopAlignmentStyle(bounds, widgetBox, node) {
  180. var topPos = bounds.top;
  181. if (Math.round(widgetBox.boundingBox.top) !== Math.round(widgetBox.widgetBox.top)) {
  182. var offset = widgetBox.boundingBox.top - widgetBox.widgetBox.top;
  183. topPos = topPos - offset;
  184. }
  185. //Sanity check the position against the minimum
  186. topPos = Math.max(topPos, node._layout.parentLayout.getMinimumTop());
  187. return {
  188. top: topPos + 'px'
  189. };
  190. };
  191. AlignAction.prototype.getMiddleAlignmentStyle = function getMiddleAlignmentStyle(bounds, widgetBox, node) {
  192. var middleLine = bounds.top + (bounds.bottom - bounds.top) / 2;
  193. var topPos = middleLine - widgetBox.widgetBox.height / 2;
  194. if (Math.round(widgetBox.boundingBox.top) !== Math.round(widgetBox.widgetBox.top)) {
  195. var offset = widgetBox.boundingBox.top + widgetBox.boundingBox.height / 2 - (widgetBox.widgetBox.top + widgetBox.widgetBox.height / 2);
  196. topPos = topPos - offset;
  197. }
  198. //Sanity check the position against the minimum
  199. topPos = Math.max(topPos, node._layout.parentLayout.getMinimumTop());
  200. return {
  201. top: topPos + 'px'
  202. };
  203. };
  204. AlignAction.prototype.getBottomAlignmentStyle = function getBottomAlignmentStyle(bounds, widgetBox, node) {
  205. var topPos = bounds.bottom - widgetBox.widgetBox.height;
  206. if (Math.round(widgetBox.boundingBox.height) !== Math.round(widgetBox.widgetBox.height)) {
  207. var offset = widgetBox.boundingBox.top + widgetBox.boundingBox.height - (widgetBox.widgetBox.top + widgetBox.widgetBox.height);
  208. topPos = topPos - offset;
  209. }
  210. //Sanity check the position against the minimum
  211. topPos = Math.max(topPos, node._layout.parentLayout.getMinimumTop());
  212. return {
  213. top: topPos + 'px'
  214. };
  215. };
  216. return AlignAction;
  217. }();
  218. return AlignAction;
  219. });
  220. //# sourceMappingURL=AlignAction.js.map