LayoutGuidelines.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2018
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['jquery', 'underscore', '../../../../lib/@waca/core-client/js/core-client/ui/core/Class'], function ($, _, Class) {
  8. var Guidlines = Class.extend({
  9. THRESHOLD: 5,
  10. /**
  11. * Find widgets and create guidelines for them, to align with top,middle,bottom,left,center,right.
  12. * @param nPage {domNode} Container of the widgets to create guidelines for.
  13. * @param dragObject {object} A drag object used by move and resize. Essentially, we only needs the IDs of the widgets being resized/moved, so they are excluded from the guidelines
  14. */
  15. setup: function setup(nPage, dragObject, snapGuidelines) {
  16. this._aGuidelines = [];
  17. this.snapGuidelines = snapGuidelines;
  18. this.domNode = nPage;
  19. var $page = $(this.domNode);
  20. _.each($page.children('.widget,.pagegroup,.pagetemplateDropZone'), function (n) {
  21. if (dragObject && dragObject.dragBox && dragObject.dragBox.ids.indexOf(n.id) > -1) {
  22. return;
  23. }
  24. var $n = $(n),
  25. rect = $n.position();
  26. var leftOffset = 0,
  27. topOffset = 0,
  28. height,
  29. width;
  30. var isDropZone = $n.is('.pagetemplateDropZone');
  31. height = $n.outerHeight();
  32. width = $n.outerWidth();
  33. var left = Math.round(rect.left + nPage.scrollLeft + leftOffset),
  34. right = Math.round(left + width);
  35. var top = Math.round(rect.top + nPage.scrollTop + topOffset),
  36. bottom = Math.round(top + height);
  37. this._addGuideline('x', left, top, bottom, isDropZone ? 'left' : null);
  38. this._addGuideline('x', right, top, bottom, isDropZone ? 'right' : null);
  39. this._addGuideline('x', (left + right) / 2, top, bottom);
  40. this._addGuideline('y', top, left, right, isDropZone ? 'top' : null);
  41. this._addGuideline('y', bottom, left, right, isDropZone ? 'bottom' : null);
  42. this._addGuideline('y', (top + bottom) / 2, left, right);
  43. }.bind(this));
  44. this._addGuideline('x', nPage.scrollWidth / 2);
  45. this._addGuideline('y', nPage.scrollHeight / 2);
  46. },
  47. /**
  48. * Create a guideline object. Adds it to the internal array. A guideline object the position (x/y) and the dom node to represent it (axis_x/axis_y).
  49. * @param {string} sAxis Can be 'x' or 'y'. Sets the orientation of the guideline.
  50. * @param {int} iPos The x or y coordinates where to create a guideline.
  51. * @param {int} topOrLeft (Optional) Top or Left position of the marker, depending if sAxis is x or y.
  52. * @param {int} bottomOrRight (Optional) Bottom or Right position of the marker, depending if sAxis is x or y.
  53. * @param {string} restrictedTo Can be 'top', 'bottom', 'left' or 'right'. It specifies if the guideline must only be used with a specific edge
  54. */
  55. _addGuideline: function _addGuideline(sAxis, iPos, topOrLeft, bottomOrRight, restrictedTo) {
  56. var $page = $(this.domNode),
  57. h = this.domNode.scrollHeight - 10 + 'px',
  58. w = this.domNode.scrollWidth - 10 + 'px';
  59. var cssPosition = sAxis === 'x' ? { left: iPos + 'px', height: h } : { top: iPos + 'px', width: w };
  60. var $marker1 = $('<div class="guidelineMarker1">');
  61. var $marker2 = $('<div class="guidelineMarker2">');
  62. if (topOrLeft !== undefined) {
  63. $marker1.css(sAxis === 'x' ? 'top' : 'left', topOrLeft - 5 + 'px');
  64. }
  65. if (bottomOrRight !== undefined) {
  66. var css = {
  67. bottom: 'auto',
  68. right: 'auto'
  69. };
  70. css[sAxis === 'x' ? 'top' : 'left'] = bottomOrRight - 5 + 'px';
  71. $marker2.css(css);
  72. }
  73. var nAxis = $('<div class="guideline guideline' + sAxis + '">').css(cssPosition).append($marker1).append($marker2);
  74. var g = {};
  75. if (restrictedTo) {
  76. g.restricted = restrictedTo;
  77. }
  78. g[sAxis] = iPos;
  79. g['axis_' + sAxis] = nAxis;
  80. this._aGuidelines.push(g);
  81. $page.append(nAxis);
  82. },
  83. calculateSnapPoint: function calculateSnapPoint(dragEdge, axis) {
  84. var guidelineDistance = null;
  85. var guideLine = {};
  86. if (this._aGuidelines && this.snapGuidelines) {
  87. this._aGuidelines.forEach(function (guideline) {
  88. if (!isNaN(Math.abs(dragEdge - guideline[axis])) && Math.abs(dragEdge - guideline[axis]) < this.THRESHOLD) {
  89. if (guidelineDistance === null || guidelineDistance > Math.abs(dragEdge - guideline[axis])) {
  90. guidelineDistance = dragEdge - guideline[axis];
  91. guideLine = guideline;
  92. }
  93. } else if (guideline[axis]) {
  94. if (Math.abs(dragEdge - guideline[axis]) < this.THRESHOLD) {
  95. guidelineDistance = dragEdge - guideline[axis];
  96. guideLine = guideline;
  97. }
  98. }
  99. }.bind(this));
  100. }
  101. var showGuideline = function showGuideline() {
  102. guideLine['axis_' + axis].show();
  103. };
  104. return { 'distance': guidelineDistance, 'show': showGuideline };
  105. },
  106. /**
  107. * Returns current guidelines. If undefined, an empty array.
  108. * @returns {Array}
  109. */
  110. getAll: function getAll() {
  111. return this._aGuidelines || [];
  112. },
  113. clear: function clear() {
  114. $(this.domNode).children('.guideline').remove();
  115. this._aGuidelines = null;
  116. this.domNode = null;
  117. }
  118. });
  119. return Guidlines;
  120. });
  121. //# sourceMappingURL=LayoutGuidelines.js.map