No Description

MarkerBase.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var SvgHelper_1 = require("../../utils/SvgHelper");
  4. var MarkerBase = (function () {
  5. function MarkerBase() {
  6. var _this = this;
  7. this.defs = [];
  8. this.width = 200;
  9. this.height = 50;
  10. this.isActive = true;
  11. this.isResizing = false;
  12. this.previousMouseX = 0;
  13. this.previousMouseY = 0;
  14. this.isDragging = false;
  15. this.manipulate = function (ev) {
  16. var scale = _this.visual.getScreenCTM().a;
  17. var dx = (ev.screenX - _this.previousMouseX) / scale;
  18. var dy = (ev.screenY - _this.previousMouseY) / scale;
  19. if (_this.isDragging) {
  20. _this.move(dx, dy);
  21. }
  22. if (_this.isResizing) {
  23. _this.resize(dx, dy);
  24. }
  25. _this.previousMouseX = ev.screenX;
  26. _this.previousMouseY = ev.screenY;
  27. };
  28. this.addToVisual = function (el) {
  29. _this.visual.appendChild(el);
  30. };
  31. this.addToRenderVisual = function (el) {
  32. _this.renderVisual.appendChild(el);
  33. };
  34. this.mouseDown = function (ev) {
  35. ev.stopPropagation();
  36. _this.select();
  37. _this.isDragging = true;
  38. _this.previousMouseX = ev.screenX;
  39. _this.previousMouseY = ev.screenY;
  40. };
  41. this.mouseUp = function (ev) {
  42. ev.stopPropagation();
  43. _this.endManipulation();
  44. };
  45. this.mouseMove = function (ev) {
  46. ev.stopPropagation();
  47. _this.manipulate(ev);
  48. };
  49. this.move = function (dx, dy) {
  50. var translate = _this.visual.transform.baseVal.getItem(0);
  51. translate.setMatrix(translate.matrix.translate(dx, dy));
  52. _this.visual.transform.baseVal.replaceItem(translate, 0);
  53. };
  54. }
  55. MarkerBase.prototype.endManipulation = function () {
  56. this.isDragging = false;
  57. this.isResizing = false;
  58. };
  59. MarkerBase.prototype.select = function () {
  60. this.isActive = true;
  61. if (this.onSelected) {
  62. this.onSelected(this);
  63. }
  64. return;
  65. };
  66. MarkerBase.prototype.deselect = function () {
  67. this.isActive = false;
  68. this.endManipulation();
  69. return;
  70. };
  71. MarkerBase.prototype.setup = function () {
  72. this.visual = SvgHelper_1.SvgHelper.createGroup();
  73. this.visual.transform.baseVal.appendItem(SvgHelper_1.SvgHelper.createTransform());
  74. this.visual.addEventListener('mousedown', this.mouseDown);
  75. this.visual.addEventListener('mouseup', this.mouseUp);
  76. this.visual.addEventListener('mousemove', this.mouseMove);
  77. this.visual.addEventListener('touchstart', this.onTouch, { passive: false });
  78. this.visual.addEventListener('touchend', this.onTouch, { passive: false });
  79. this.visual.addEventListener('touchmove', this.onTouch, { passive: false });
  80. this.renderVisual = SvgHelper_1.SvgHelper.createGroup([['class', 'render-visual']]);
  81. this.visual.appendChild(this.renderVisual);
  82. };
  83. MarkerBase.prototype.resize = function (x, y) {
  84. return;
  85. };
  86. MarkerBase.prototype.onTouch = function (ev) {
  87. ev.preventDefault();
  88. var newEvt = document.createEvent('MouseEvents');
  89. var touch = ev.changedTouches[0];
  90. var type = null;
  91. switch (ev.type) {
  92. case 'touchstart':
  93. type = 'mousedown';
  94. break;
  95. case 'touchmove':
  96. type = 'mousemove';
  97. break;
  98. case 'touchend':
  99. type = 'mouseup';
  100. break;
  101. }
  102. newEvt.initMouseEvent(type, true, true, window, 0, touch.screenX, touch.screenY, touch.clientX, touch.clientY, ev.ctrlKey, ev.altKey, ev.shiftKey, ev.metaKey, 0, null);
  103. ev.target.dispatchEvent(newEvt);
  104. };
  105. MarkerBase.createMarker = function () {
  106. var marker = new MarkerBase();
  107. marker.setup();
  108. return marker;
  109. };
  110. return MarkerBase;
  111. }());
  112. exports.MarkerBase = MarkerBase;