AlignAction.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2018, 2020
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['./BasePropertyAction', 'jquery', 'underscore', '../../../../lib/@waca/dashboard-common/dist/ui/interaction/Utils', '../../../../app/nls/StringResources'], function (BaseClass, $, _, utils, stringResources) {
  8. var Align = null;
  9. Align = BaseClass.extend({
  10. init: function init(controller) {
  11. this.controller = controller;
  12. Align.inherited('init', this, arguments);
  13. this.selectedNodes = [];
  14. },
  15. /**
  16. * Called by the interaction manager when there is a new selection
  17. *
  18. * @param nodes
  19. */
  20. newSelection: function newSelection(nodes) {
  21. this.selectedNodes = nodes;
  22. },
  23. getProperties: function getProperties() {
  24. var icons = this.dashboardApi.getFeature('Icons');
  25. var isReadOnly = this.selectedNodes.length <= 1;
  26. return [{
  27. 'type': 'IconPicker',
  28. 'label': stringResources.get('toolbarActionAlign'),
  29. 'name': 'alignWidget',
  30. 'id': 'alignWidget',
  31. 'tabName': stringResources.get('tabName_general'),
  32. 'sectionName': stringResources.get('sectionName_layout'),
  33. 'sectionOpened': true,
  34. 'readOnly': isReadOnly,
  35. 'items': [{
  36. 'name': 'left',
  37. 'label': stringResources.get('alignWidgetLeft'),
  38. 'value': icons.getIcon('align-left').id
  39. }, {
  40. 'name': 'center',
  41. 'label': stringResources.get('alignWidgetCenter'),
  42. 'value': icons.getIcon('align-horizontally').id
  43. }, {
  44. 'name': 'right',
  45. 'label': stringResources.get('alignWidgetRight'),
  46. 'value': icons.getIcon('align-right').id
  47. }, {
  48. 'name': 'top',
  49. 'label': stringResources.get('alignWidgetTop'),
  50. 'value': icons.getIcon('align-top').id
  51. }, {
  52. 'name': 'middle',
  53. 'label': stringResources.get('alignWidgetMiddle'),
  54. 'value': icons.getIcon('align-vertically').id
  55. }, {
  56. 'name': 'bottom',
  57. 'label': stringResources.get('alignWidgetBottom'),
  58. 'value': icons.getIcon('align-bottom').id
  59. }],
  60. onChange: this.onAlignIconClick.bind(this),
  61. order: 10
  62. }];
  63. },
  64. onAlignIconClick: function onAlignIconClick(propertyId, selection) {
  65. if (selection && selection.length && selection[0].name && this.selectedNodes.length > 1) {
  66. var position = selection[0].name;
  67. this.applyAlignment(position);
  68. }
  69. },
  70. getWidgetBoxes: function getWidgetBoxes(node) {
  71. var widgetPos = utils.position(node);
  72. var widgetSize = utils.widgetSize(node);
  73. var result = {
  74. boundingBox: $(node).position(),
  75. widgetBox: {
  76. top: widgetPos.top,
  77. left: widgetPos.left,
  78. width: widgetSize.width,
  79. height: widgetSize.height
  80. }
  81. };
  82. var boundingRect = node.getBoundingClientRect();
  83. result.boundingBox.width = boundingRect.width;
  84. result.boundingBox.height = boundingRect.height;
  85. return result;
  86. },
  87. applyAlignment: function applyAlignment(position) {
  88. var alignmentFns = {
  89. left: this.getLeftAlignmentStyle,
  90. center: this.getCenterAlignmentStyle,
  91. right: this.getRightAlignmentStyle,
  92. top: this.getTopAlignmentStyle,
  93. middle: this.getMiddleAlignmentStyle,
  94. bottom: this.getBottomAlignmentStyle
  95. };
  96. var widgetBoxes = [];
  97. var widgetBox = this.getWidgetBoxes(this.selectedNodes[0]);
  98. widgetBoxes.push(widgetBox);
  99. var bounds = {
  100. top: widgetBox.boundingBox.top,
  101. left: widgetBox.boundingBox.left,
  102. bottom: widgetBox.boundingBox.top + widgetBox.boundingBox.height,
  103. right: widgetBox.boundingBox.left + widgetBox.boundingBox.width
  104. };
  105. for (var i = 1; i < this.selectedNodes.length; i++) {
  106. var _widgetBox = this.getWidgetBoxes(this.selectedNodes[i]);
  107. widgetBoxes.push(_widgetBox);
  108. bounds.top = Math.min(bounds.top, _widgetBox.boundingBox.top);
  109. bounds.left = Math.min(bounds.left, _widgetBox.boundingBox.left);
  110. bounds.bottom = Math.max(bounds.bottom, _widgetBox.boundingBox.top + _widgetBox.boundingBox.height);
  111. bounds.right = Math.max(bounds.right, _widgetBox.boundingBox.left + _widgetBox.boundingBox.width);
  112. }
  113. var modelInfoArray = [];
  114. for (var _i = 0; _i < this.selectedNodes.length; _i++) {
  115. var updatedStyle = alignmentFns[position](bounds, widgetBoxes[_i], this.selectedNodes[_i]);
  116. if (updatedStyle) {
  117. modelInfoArray.push({
  118. style: updatedStyle,
  119. id: this.selectedNodes[_i]._layout.model.id
  120. });
  121. }
  122. }
  123. if (modelInfoArray.length) {
  124. this.updateModel(modelInfoArray);
  125. this.refreshProperties();
  126. }
  127. },
  128. getLeftAlignmentStyle: function getLeftAlignmentStyle(bounds, widgetBox, node) {
  129. var leftPos = bounds.left;
  130. if (Math.round(widgetBox.boundingBox.left) !== Math.round(widgetBox.widgetBox.left)) {
  131. var offset = widgetBox.boundingBox.left - widgetBox.widgetBox.left;
  132. leftPos = leftPos - offset;
  133. }
  134. //Sanity check the position against the minimum
  135. leftPos = Math.max(leftPos, node._layout.parentLayout.getMinimumLeft());
  136. return {
  137. left: leftPos + 'px'
  138. };
  139. },
  140. getCenterAlignmentStyle: function getCenterAlignmentStyle(bounds, widgetBox, node) {
  141. var centerLine = bounds.left + (bounds.right - bounds.left) / 2;
  142. var leftPos = centerLine - widgetBox.widgetBox.width / 2;
  143. if (Math.round(widgetBox.boundingBox.left) !== Math.round(widgetBox.widgetBox.left)) {
  144. var offset = widgetBox.boundingBox.left + widgetBox.boundingBox.width / 2 - (widgetBox.widgetBox.left + widgetBox.widgetBox.width / 2);
  145. leftPos = leftPos - offset;
  146. }
  147. //Sanity check the position against the minimum
  148. leftPos = Math.max(leftPos, node._layout.parentLayout.getMinimumLeft());
  149. return {
  150. left: leftPos + 'px'
  151. };
  152. },
  153. getRightAlignmentStyle: function getRightAlignmentStyle(bounds, widgetBox, node) {
  154. var leftPos = bounds.right - widgetBox.widgetBox.width;
  155. if (Math.round(widgetBox.boundingBox.width) !== Math.round(widgetBox.widgetBox.width)) {
  156. var offset = widgetBox.boundingBox.left + widgetBox.boundingBox.width - (widgetBox.widgetBox.left + widgetBox.widgetBox.width);
  157. leftPos = leftPos - offset;
  158. }
  159. //Sanity check the position against the minimum
  160. leftPos = Math.max(leftPos, node._layout.parentLayout.getMinimumLeft());
  161. return {
  162. left: leftPos + 'px'
  163. };
  164. },
  165. getTopAlignmentStyle: function getTopAlignmentStyle(bounds, widgetBox, node) {
  166. var topPos = bounds.top;
  167. if (Math.round(widgetBox.boundingBox.top) !== Math.round(widgetBox.widgetBox.top)) {
  168. var offset = widgetBox.boundingBox.top - widgetBox.widgetBox.top;
  169. topPos = topPos - offset;
  170. }
  171. //Sanity check the position against the minimum
  172. topPos = Math.max(topPos, node._layout.parentLayout.getMinimumTop());
  173. return {
  174. top: topPos + 'px'
  175. };
  176. },
  177. getMiddleAlignmentStyle: function getMiddleAlignmentStyle(bounds, widgetBox, node) {
  178. var middleLine = bounds.top + (bounds.bottom - bounds.top) / 2;
  179. var topPos = middleLine - widgetBox.widgetBox.height / 2;
  180. if (Math.round(widgetBox.boundingBox.top) !== Math.round(widgetBox.widgetBox.top)) {
  181. var offset = widgetBox.boundingBox.top + widgetBox.boundingBox.height / 2 - (widgetBox.widgetBox.top + widgetBox.widgetBox.height / 2);
  182. topPos = topPos - offset;
  183. }
  184. //Sanity check the position against the minimum
  185. topPos = Math.max(topPos, node._layout.parentLayout.getMinimumTop());
  186. return {
  187. top: topPos + 'px'
  188. };
  189. },
  190. getBottomAlignmentStyle: function getBottomAlignmentStyle(bounds, widgetBox, node) {
  191. var topPos = bounds.bottom - widgetBox.widgetBox.height;
  192. if (Math.round(widgetBox.boundingBox.height) !== Math.round(widgetBox.widgetBox.height)) {
  193. var offset = widgetBox.boundingBox.top + widgetBox.boundingBox.height - (widgetBox.widgetBox.top + widgetBox.widgetBox.height);
  194. topPos = topPos - offset;
  195. }
  196. //Sanity check the position against the minimum
  197. topPos = Math.max(topPos, node._layout.parentLayout.getMinimumTop());
  198. return {
  199. top: topPos + 'px'
  200. };
  201. }
  202. });
  203. return Align;
  204. });
  205. //# sourceMappingURL=AlignAction.js.map