No Description

index.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var uuid = require("uuid/v1");
  4. var SvgHelper_1 = require("../../renderer/SvgHelper");
  5. var BaseMarker = (function () {
  6. function BaseMarker() {
  7. var _this = this;
  8. this.id = uuid();
  9. this.type = 'base';
  10. this.onChange = function () { };
  11. this.defs = [];
  12. this.width = 200;
  13. this.height = 50;
  14. this.isActive = true;
  15. this.isDragging = false;
  16. this.isResizing = false;
  17. this.previousMouseX = 0;
  18. this.previousMouseY = 0;
  19. this.manipulate = function (ev) {
  20. var scale = _this.visual.getScreenCTM().a;
  21. var dx = (ev.screenX - _this.previousMouseX) / scale;
  22. var dy = (ev.screenY - _this.previousMouseY) / scale;
  23. if (_this.isDragging) {
  24. _this.onChange({ target: 'marker', id: _this.id, event: 'move', data: { dx: dx, dy: dy } });
  25. _this.move(dx, dy);
  26. }
  27. if (_this.isResizing) {
  28. _this.resize(dx, dy, function (pos) {
  29. _this.onChange({ target: 'marker', id: _this.id, event: 'resize', data: { dx: dx, dy: dy, pos: pos } });
  30. });
  31. }
  32. _this.previousMouseX = ev.screenX;
  33. _this.previousMouseY = ev.screenY;
  34. };
  35. this.addToVisual = function (el) {
  36. _this.visual.appendChild(el);
  37. };
  38. this.addToRenderVisual = function (el) {
  39. _this.renderVisual.appendChild(el);
  40. };
  41. this.mouseDown = function (ev) {
  42. ev.stopPropagation();
  43. if (_this.page && _this.page.mode === 'mirror') {
  44. return;
  45. }
  46. _this.select();
  47. _this.isDragging = true;
  48. _this.previousMouseX = ev.screenX;
  49. _this.previousMouseY = ev.screenY;
  50. };
  51. this.mouseUp = function (ev) {
  52. ev.stopPropagation();
  53. _this.endManipulation();
  54. };
  55. this.mouseMove = function (ev) {
  56. ev.stopPropagation();
  57. _this.manipulate(ev);
  58. };
  59. this.move = function (dx, dy) {
  60. var translate = _this.visual.transform.baseVal.getItem(0);
  61. translate.setMatrix(translate.matrix.translate(dx, dy));
  62. _this.visual.transform.baseVal.replaceItem(translate, 0);
  63. };
  64. }
  65. BaseMarker.prototype.reactToManipulation = function (type, _a) {
  66. var dx = _a.dx, dy = _a.dy, pos = _a.pos;
  67. if (type === 'move') {
  68. this.move(dx, dy);
  69. }
  70. if (type === 'resize') {
  71. this.resizeByEvent(dx, dy, pos);
  72. }
  73. };
  74. BaseMarker.prototype.endManipulation = function () {
  75. this.isDragging = false;
  76. this.isResizing = false;
  77. };
  78. BaseMarker.prototype.select = function () {
  79. this.isActive = true;
  80. if (this.onSelected) {
  81. this.onSelected(this);
  82. }
  83. return;
  84. };
  85. BaseMarker.prototype.deselect = function () {
  86. this.isActive = false;
  87. this.endManipulation();
  88. return;
  89. };
  90. BaseMarker.prototype.setup = function () {
  91. this.visual = SvgHelper_1.SvgHelper.createGroup();
  92. this.visual.transform.baseVal.appendItem(SvgHelper_1.SvgHelper.createTransform());
  93. this.visual.addEventListener('mousedown', this.mouseDown);
  94. this.visual.addEventListener('mouseup', this.mouseUp);
  95. this.visual.addEventListener('mousemove', this.mouseMove);
  96. this.visual.addEventListener('touchstart', this.onTouch, { passive: false });
  97. this.visual.addEventListener('touchend', this.onTouch, { passive: false });
  98. this.visual.addEventListener('touchmove', this.onTouch, { passive: false });
  99. this.renderVisual = SvgHelper_1.SvgHelper.createGroup([['class', 'render-visual']]);
  100. this.visual.appendChild(this.renderVisual);
  101. };
  102. BaseMarker.prototype.resize = function (x, y, cb) {
  103. return;
  104. };
  105. BaseMarker.prototype.resizeByEvent = function (x, y, pos) {
  106. return;
  107. };
  108. BaseMarker.prototype.onTouch = function (ev) {
  109. ev.preventDefault();
  110. var newEvt = document.createEvent('MouseEvents');
  111. var touch = ev.changedTouches[0];
  112. var type = null;
  113. switch (ev.type) {
  114. case 'touchstart':
  115. type = 'mousedown';
  116. break;
  117. case 'touchmove':
  118. type = 'mousemove';
  119. break;
  120. case 'touchend':
  121. type = 'mouseup';
  122. break;
  123. default:
  124. break;
  125. }
  126. 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);
  127. ev.target.dispatchEvent(newEvt);
  128. };
  129. BaseMarker.createMarker = function (page) {
  130. var marker = new BaseMarker();
  131. marker.page = page;
  132. marker.setup();
  133. return marker;
  134. };
  135. return BaseMarker;
  136. }());
  137. exports.BaseMarker = BaseMarker;