No Description

index.js 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 uuid = require("uuid/v1");
  17. var SvgHelper_1 = require("../../renderer/SvgHelper");
  18. var index_1 = require("../../renderer/DomEventAware/index");
  19. var types_1 = require("../../utils/types");
  20. var BaseMarker = (function (_super) {
  21. __extends(BaseMarker, _super);
  22. function BaseMarker() {
  23. var _this = _super !== null && _super.apply(this, arguments) || this;
  24. _this.id = uuid();
  25. _this.type = 'base';
  26. _this.onChange = function () { };
  27. _this.defs = [];
  28. _this.width = 200;
  29. _this.height = 50;
  30. _this.isActive = true;
  31. _this.isDragging = false;
  32. _this.isResizing = false;
  33. _this.manipulate = function (ev) {
  34. var scale = _this.visual.getScreenCTM().a;
  35. var dx = (ev.screenX - _this.previousMouseX) / scale;
  36. var dy = (ev.screenY - _this.previousMouseY) / scale;
  37. if (_this.isDragging) {
  38. _this.move(dx, dy);
  39. }
  40. if (_this.isResizing) {
  41. _this.resize(dx, dy, function (pos) {
  42. _this.onChange({
  43. target: 'marker',
  44. id: _this.id,
  45. event: 'resizeMarker',
  46. marker: { dx: dx, dy: dy, pos: pos }
  47. });
  48. });
  49. }
  50. _this.previousMouseX = ev.screenX;
  51. _this.previousMouseY = ev.screenY;
  52. };
  53. _this.move = function (dx, dy) {
  54. var translate = _this.visual.transform.baseVal.getItem(0);
  55. translate.setMatrix(translate.matrix.translate(dx, dy));
  56. _this.visual.transform.baseVal.replaceItem(translate, 0);
  57. _this.x += dx;
  58. _this.y += dy;
  59. _this.onChange({ target: 'marker', id: _this.id, event: 'moveMarker', marker: { dx: dx, dy: dy } });
  60. };
  61. _this.moveTo = function (x, y) {
  62. var translate = _this.visual.transform.baseVal.getItem(0);
  63. translate.setMatrix(translate.matrix.translate(x - _this.x, y - _this.y));
  64. _this.visual.transform.baseVal.replaceItem(translate, 0);
  65. _this.x = x;
  66. _this.y = y;
  67. };
  68. _this.addToVisual = function (el) {
  69. _this.visual.appendChild(el);
  70. };
  71. _this.addToRenderVisual = function (el) {
  72. _this.renderVisual.appendChild(el);
  73. };
  74. _this.onMouseDown = function (ev) {
  75. ev.stopPropagation();
  76. if (_this.page && _this.page.mode === 'mirror') {
  77. return;
  78. }
  79. _this.select();
  80. _this.isDragging = true;
  81. _this.previousMouseX = ev.screenX;
  82. _this.previousMouseY = ev.screenY;
  83. };
  84. _this.onMouseUp = function (ev) {
  85. ev.stopPropagation();
  86. _this.endManipulation();
  87. };
  88. _this.onMouseMove = function (ev) {
  89. ev.stopPropagation();
  90. _this.manipulate(ev);
  91. };
  92. return _this;
  93. }
  94. BaseMarker.prototype.reactToManipulation = function (type, _a) {
  95. var _b = _a === void 0 ? {} : _a, dx = _b.dx, dy = _b.dy, pos = _b.pos;
  96. if (type === 'moveMarker') {
  97. if (types_1.isNil(dx) || types_1.isNil(dy)) {
  98. return;
  99. }
  100. this.move(dx, dy);
  101. }
  102. if (type === 'resizeMarker') {
  103. if (types_1.isNil(dx) || types_1.isNil(dy)) {
  104. return;
  105. }
  106. this.resizeByEvent(dx, dy, pos);
  107. }
  108. };
  109. BaseMarker.prototype.endManipulation = function () {
  110. this.isDragging = false;
  111. this.isResizing = false;
  112. };
  113. BaseMarker.prototype.select = function () {
  114. this.isActive = true;
  115. if (this.onSelected) {
  116. this.onSelected(this);
  117. }
  118. return;
  119. };
  120. BaseMarker.prototype.deselect = function () {
  121. this.isActive = false;
  122. this.endManipulation();
  123. return;
  124. };
  125. BaseMarker.prototype.captureSnap = function () {
  126. return {
  127. id: this.id,
  128. type: this.type,
  129. isActive: this.isActive,
  130. x: this.x,
  131. y: this.y
  132. };
  133. };
  134. BaseMarker.prototype.applySnap = function (snap) {
  135. this.id = snap.id;
  136. this.type = snap.type;
  137. if (snap.x && snap.y) {
  138. this.moveTo(snap.x, snap.y);
  139. }
  140. if (this.isActive) {
  141. this.select();
  142. }
  143. };
  144. BaseMarker.prototype.destroy = function () {
  145. this.visual.style.display = 'none';
  146. };
  147. BaseMarker.prototype.resize = function (x, y, cb) {
  148. return;
  149. };
  150. BaseMarker.prototype.resizeByEvent = function (x, y, pos) {
  151. return;
  152. };
  153. BaseMarker.prototype.init = function () {
  154. this.visual = SvgHelper_1.SvgHelper.createGroup();
  155. this.visual.transform.baseVal.appendItem(SvgHelper_1.SvgHelper.createTransform());
  156. _super.prototype.init.call(this, this.visual);
  157. this.renderVisual = SvgHelper_1.SvgHelper.createGroup([['class', 'render-visual']]);
  158. this.visual.appendChild(this.renderVisual);
  159. };
  160. BaseMarker.createMarker = function (page) {
  161. var marker = new BaseMarker();
  162. marker.page = page;
  163. marker.init();
  164. return marker;
  165. };
  166. return BaseMarker;
  167. }(index_1.DomEventAware));
  168. exports.BaseMarker = BaseMarker;