GuidelineManager.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2014, 2018
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. *
  7. * Guideline Manager
  8. */
  9. define(['jquery', 'underscore', '../../../../lib/@waca/core-client/js/core-client/ui/core/Class', './LayoutGuidelines', './GridGuidelines'], function ($, _, Class, LayoutGuidelines, GridGuidelines) {
  10. var Manager = Class.extend({
  11. LayoutGuidelines: {},
  12. init: function init(options) {
  13. this.layoutController = options.layoutController;
  14. this.layoutGuidelines = new LayoutGuidelines();
  15. },
  16. getReady: function getReady(page, dragObject, keyboard) {
  17. this.domNode = page;
  18. this.snapContributions = [];
  19. var layoutModel = this.layoutController.boardModel.layout.findModel(page.id);
  20. if (!layoutModel) {
  21. layoutModel = this.layoutController.boardModel.layout;
  22. }
  23. var snapObject = layoutModel.getValueFromSelfOrParent('snapObjects');
  24. snapObject = snapObject === undefined ? true : snapObject;
  25. this.gridGuidelines = new GridGuidelines({ 'page': page, 'controller': this.layoutController });
  26. if (!keyboard) {
  27. this.layoutGuidelines.setup(page, dragObject, snapObject);
  28. this.snapContributions.push(this.layoutGuidelines);
  29. }
  30. this.snapContributions.push(this.gridGuidelines);
  31. },
  32. finish: function finish() {
  33. $(this.domNode).children('.guideline').remove();
  34. this.domNode = null;
  35. },
  36. getSnapCoordinates: function getSnapCoordinates(dragBox) {
  37. var revisedDragBox = { 'top': null, 'right': null, 'bottom': null, 'left': null };
  38. var snapDistanceRight = [];
  39. var snapDistanceLeft = [];
  40. var snapDistanceTop = [];
  41. var snapDistanceBottom = [];
  42. if (this.snapContributions) {
  43. this.snapContributions.forEach(function (contribution) {
  44. snapDistanceRight.push(contribution.calculateSnapPoint(dragBox.right, 'x'));
  45. snapDistanceLeft.push(contribution.calculateSnapPoint(dragBox.left, 'x'));
  46. snapDistanceTop.push(contribution.calculateSnapPoint(dragBox.top, 'y'));
  47. snapDistanceBottom.push(contribution.calculateSnapPoint(dragBox.bottom, 'y'));
  48. }.bind(this));
  49. }
  50. revisedDragBox.right = this._calculateClosestSnapPoint(snapDistanceRight, 'x');
  51. revisedDragBox.left = this._calculateClosestSnapPoint(snapDistanceLeft, 'x');
  52. revisedDragBox.top = this._calculateClosestSnapPoint(snapDistanceTop, 'y');
  53. revisedDragBox.bottom = this._calculateClosestSnapPoint(snapDistanceBottom, 'y');
  54. return revisedDragBox;
  55. },
  56. _calculateClosestSnapPoint: function _calculateClosestSnapPoint(snapDistance) {
  57. var closestPoint = null;
  58. snapDistance.forEach(function (distance) {
  59. if (distance.distance && closestPoint) {
  60. if (closestPoint.distance > Math.abs(distance.distance)) {
  61. closestPoint = distance;
  62. }
  63. } else if (distance.distance && !closestPoint) {
  64. closestPoint = distance;
  65. }
  66. }.bind(this));
  67. if (closestPoint && closestPoint.distance) {
  68. if (closestPoint.show) {
  69. closestPoint.show();
  70. }
  71. return closestPoint.distance;
  72. } else {
  73. return closestPoint;
  74. }
  75. },
  76. hideAll: function hideAll() {
  77. $(this.domNode).children('.guideline').hide();
  78. }
  79. });
  80. return Manager;
  81. });
  82. //# sourceMappingURL=GuidelineManager.js.map