No Description

LineMarkerBase.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. var SvgHelper_1 = require("../../utils/SvgHelper");
  17. var MarkerBase_1 = require("../base/MarkerBase");
  18. var ResizeGrip_1 = require("../base/ResizeGrip");
  19. var LineMarkerBase = (function (_super) {
  20. __extends(LineMarkerBase, _super);
  21. function LineMarkerBase() {
  22. var _this = _super !== null && _super.apply(this, arguments) || this;
  23. _this.MIN_LENGTH = 20;
  24. _this.x1 = 0;
  25. _this.y1 = 0;
  26. _this.x2 = _this.width;
  27. _this.y2 = 0;
  28. _this.getLineLength = function (x1, y1, x2, y2) {
  29. var dx = Math.abs(x1 - x2);
  30. var dy = Math.abs(y1 - y2);
  31. return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
  32. };
  33. _this.addControlBox = function () {
  34. _this.controlBox = SvgHelper_1.SvgHelper.createGroup([['class', 'fc-whiteboardline-control-box']]);
  35. _this.addToVisual(_this.controlBox);
  36. _this.addControlGrips();
  37. };
  38. _this.adjustControlBox = function () {
  39. _this.positionGrips();
  40. };
  41. _this.addControlGrips = function () {
  42. _this.controlGrip1 = _this.createGrip();
  43. _this.controlGrip2 = _this.createGrip();
  44. _this.positionGrips();
  45. };
  46. _this.createGrip = function () {
  47. var grip = new ResizeGrip_1.ResizeGrip();
  48. grip.visual.transform.baseVal.appendItem(SvgHelper_1.SvgHelper.createTransform());
  49. _this.controlBox.appendChild(grip.visual);
  50. grip.visual.addEventListener('mousedown', _this.gripMouseDown);
  51. grip.visual.addEventListener('mousemove', _this.gripMouseMove);
  52. grip.visual.addEventListener('mouseup', _this.gripMouseUp);
  53. grip.visual.addEventListener('touchstart', _this.onTouch, { passive: false });
  54. grip.visual.addEventListener('touchend', _this.onTouch, { passive: false });
  55. grip.visual.addEventListener('touchmove', _this.onTouch, { passive: false });
  56. return grip;
  57. };
  58. _this.positionGrips = function () {
  59. var gripSize = _this.controlGrip1.GRIP_SIZE;
  60. var x1 = _this.x1 - gripSize / 2;
  61. var y1 = _this.y1 - gripSize / 2;
  62. var x2 = _this.x2 - gripSize / 2;
  63. var y2 = _this.y2 - gripSize / 2;
  64. _this.positionGrip(_this.controlGrip1.visual, x1, y1);
  65. _this.positionGrip(_this.controlGrip2.visual, x2, y2);
  66. };
  67. _this.positionGrip = function (grip, x, y) {
  68. var translate = grip.transform.baseVal.getItem(0);
  69. translate.setTranslate(x, y);
  70. grip.transform.baseVal.replaceItem(translate, 0);
  71. };
  72. _this.gripMouseDown = function (ev) {
  73. _this.isResizing = true;
  74. _this.activeGrip =
  75. ev.target === _this.controlGrip1.visual
  76. ? _this.controlGrip1
  77. : _this.controlGrip2;
  78. _this.previousMouseX = ev.screenX;
  79. _this.previousMouseY = ev.screenY;
  80. ev.stopPropagation();
  81. };
  82. _this.gripMouseUp = function (ev) {
  83. _this.isResizing = false;
  84. _this.activeGrip = null;
  85. ev.stopPropagation();
  86. };
  87. _this.gripMouseMove = function (ev) {
  88. if (_this.isResizing) {
  89. _this.resize(ev.movementX, ev.movementY);
  90. }
  91. };
  92. return _this;
  93. }
  94. LineMarkerBase.prototype.endManipulation = function () {
  95. _super.prototype.endManipulation.call(this);
  96. this.isResizing = false;
  97. this.activeGrip = null;
  98. };
  99. LineMarkerBase.prototype.select = function () {
  100. _super.prototype.select.call(this);
  101. this.controlBox.style.display = '';
  102. };
  103. LineMarkerBase.prototype.deselect = function () {
  104. _super.prototype.deselect.call(this);
  105. this.controlBox.style.display = 'none';
  106. };
  107. LineMarkerBase.prototype.setup = function () {
  108. _super.prototype.setup.call(this);
  109. this.markerBgLine = SvgHelper_1.SvgHelper.createLine(0, 0, this.x2, 0, [
  110. ['stroke', 'transparent'],
  111. ['stroke-width', '30']
  112. ]);
  113. this.addToRenderVisual(this.markerBgLine);
  114. this.markerLine = SvgHelper_1.SvgHelper.createLine(0, 0, this.x2, 0);
  115. this.addToRenderVisual(this.markerLine);
  116. this.addControlBox();
  117. };
  118. LineMarkerBase.prototype.resize = function (x, y) {
  119. if (this.activeGrip) {
  120. if (this.activeGrip === this.controlGrip1 &&
  121. this.getLineLength(this.x1 + x, this.y1 + 1, this.x2, this.y2) >= this.MIN_LENGTH) {
  122. this.x1 += x;
  123. this.y1 += y;
  124. this.markerBgLine.setAttribute('x1', this.x1.toString());
  125. this.markerBgLine.setAttribute('y1', this.y1.toString());
  126. this.markerLine.setAttribute('x1', this.x1.toString());
  127. this.markerLine.setAttribute('y1', this.y1.toString());
  128. }
  129. else if (this.activeGrip === this.controlGrip2 &&
  130. this.getLineLength(this.x1, this.y1, this.x2 + x, this.y2 + y) >= this.MIN_LENGTH) {
  131. this.x2 += x;
  132. this.y2 += y;
  133. this.markerBgLine.setAttribute('x2', this.x2.toString());
  134. this.markerBgLine.setAttribute('y2', this.y2.toString());
  135. this.markerLine.setAttribute('x2', this.x2.toString());
  136. this.markerLine.setAttribute('y2', this.y2.toString());
  137. }
  138. }
  139. this.adjustControlBox();
  140. };
  141. LineMarkerBase.createMarker = function () {
  142. var marker = new LineMarkerBase();
  143. marker.setup();
  144. return marker;
  145. };
  146. return LineMarkerBase;
  147. }(MarkerBase_1.MarkerBase));
  148. exports.LineMarkerBase = LineMarkerBase;