UNPKG

medsurf-draw

Version:

Draw annotations on jpg/zoomify images, based on PIXI.js

267 lines 12.1 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import * as MedsurfDraw from "../../public-api"; import { BaseGenerator, BaseGeneratorModel } from "../../bases/generators/BaseGenerator"; export var LineGeneratorStates; (function (LineGeneratorStates) { LineGeneratorStates[LineGeneratorStates["START"] = 0] = "START"; LineGeneratorStates[LineGeneratorStates["END"] = 1] = "END"; LineGeneratorStates[LineGeneratorStates["FINISH"] = 2] = "FINISH"; })(LineGeneratorStates || (LineGeneratorStates = {})); var LineGeneratorModel = (function (_super) { __extends(LineGeneratorModel, _super); function LineGeneratorModel() { return _super !== null && _super.apply(this, arguments) || this; } return LineGeneratorModel; }(BaseGeneratorModel)); export { LineGeneratorModel }; var LineGenerator = (function (_super) { __extends(LineGenerator, _super); function LineGenerator(model) { var _this = _super.call(this, model) || this; _this._enableKeyEvents = false; _this.target.modeInteraction.setModeItem("drawing"); MedsurfDraw.Keyboard.events.on("pressed_ShiftLeft", "onScaleElementSetBezierLeft", _this.onPressedShiftKey.bind(_this)); MedsurfDraw.Keyboard.events.on("released_ShiftLeft", "onScaleElementUnsetBezierLeft", _this.onReleaseShiftKey.bind(_this)); MedsurfDraw.Keyboard.events.on("pressed_ShiftRight", "onScaleElementSetBezierRight", _this.onPressedShiftKey.bind(_this)); MedsurfDraw.Keyboard.events.on("released_ShiftRight", "onScaleElementUnsetBezierRight", _this.onReleaseShiftKey.bind(_this)); if (!_this.startElement) { _this._state = LineGeneratorStates.START; _this._setupGenerator(); } else { _this._state = LineGeneratorStates.END; _this._setupGenerator(); } return _this; } LineGenerator.prototype.end = function () { MedsurfDraw.Keyboard.events.remove("pressed_ShiftLeft", "onScaleElementSetBezierLeft"); MedsurfDraw.Keyboard.events.remove("released_ShiftLeft", "onScaleElementUnsetBezierLeft"); MedsurfDraw.Keyboard.events.remove("pressed_ShiftRight", "onScaleElementSetBezierRight"); MedsurfDraw.Keyboard.events.remove("released_ShiftRight", "onScaleElementUnsetBezierRight"); if (this._startPositionPointGenerator) { this._startPositionPointGenerator.removeAllListeners(); } if (this._endPositionPointGenerator) { this._endPositionPointGenerator.removeAllListeners(); } _super.prototype.end.call(this); }; LineGenerator.prototype.destroy = function (options) { this.end(); if (this.lineElement && this.lineElement.modeInteraction.lastMode.endsWith('_new')) { this.lineElement.modeInteraction.setMode('delete_draw'); } if (this._startPositionPointGenerator) { this._startPositionPointGenerator.destroy(); } if (this._endPositionPointGenerator) { this._endPositionPointGenerator.destroy(); } }; LineGenerator.prototype.endGenerator = function () { _super.prototype.endGenerator.call(this); this.target.sortChildren(); this.target.modeInteraction.setMode(this.target.modeInteraction.defaultMode); }; LineGenerator.prototype.abortGenerator = function () { _super.prototype.abortGenerator.call(this); this.target.getImageObjects() .filter(function (imageObject) { return imageObject.modeInteraction.lastMode.startsWith('draw_') && imageObject.modeInteraction.lastMode.endsWith('_new'); }) .forEach(function (imageObject) { imageObject.modeInteraction.setMode("delete_draw"); }); this.target.sortChildren(); this.target.modeInteraction.setMode(this.target.modeInteraction.defaultMode); }; LineGenerator.prototype._setupGenerator = function () { if (this._state === LineGeneratorStates.START) { this._startPositionPointGenerator = new MedsurfDraw.PositionPointGenerator({ target: this.target, layerGroup: this.data.layerGroup, image: this.data.image, stickMode: this.stickMode }); this._startPositionPointGenerator.once("startGenerator", this.start, this); this._startPositionPointGenerator.once("endGenerator", this.endStartPositionPointGenerator, this); this._startPositionPointGenerator.once("updatePoint", this.endStartPositionPointGenerator, this); this._startPositionPointGenerator.once("abortGenerator", this.abortStartPositionPointGenerator, this); } else { this._endPositionPointGenerator = new MedsurfDraw.PositionPointGenerator({ target: this.target, layerGroup: this.data.layerGroup, image: this.data.image, stickMode: this.stickMode }); if (this.startElement) { this._endPositionPointGenerator.once("startGenerator", this.start, this); } this._endPositionPointGenerator.once("startGenerator", this.startEndPositionPointGenerator, this); this._endPositionPointGenerator.once("endGenerator", this.endEndPositionPointGenerator, this); this._endPositionPointGenerator.once("updatePoint", this.updatePointEndPositionPointGenerator, this); this._endPositionPointGenerator.once("abortGenerator", this.abortEndPositionPointGenerator, this); } }; LineGenerator.prototype.destroyEnd = function () { this.end(); if (this.lineElement && this.lineElement.modeInteraction.lastMode.endsWith('new')) { this.lineElement.modeInteraction.setMode('delete_draw'); } if (this._endPositionPointGenerator) { this._endPositionPointGenerator.destroy(); } }; LineGenerator.prototype.endStartPositionPointGenerator = function () { this._enableKeyEvents = true; this._state = LineGeneratorStates.END; this._setupGenerator(); }; LineGenerator.prototype.abortStartPositionPointGenerator = function () { this.emit("abortGenerator"); this.destroy(); }; LineGenerator.prototype.startEndPositionPointGenerator = function () { var startElement = this.startPositionPointElement; var sharedLine = this.target.getLineForElements(startElement, this.endPositionPointElement); if (sharedLine) { this._lineElement = sharedLine; this.lineElement.controlDrawItem(false); } else { this._lineElement = MedsurfDraw.Line.getInstance(this.target, startElement.name, this.endPositionPointElement.name, this.bezierCourve, this.target.dimensions.width); this.target.addChild(this.lineElement); this.lineElement.controlDrawItem(true); this.target.addImageObject(this.lineElement.model, this.data.layerGroup, this.data.image); } }; LineGenerator.prototype.endEndPositionPointGenerator = function () { this._state = LineGeneratorStates.FINISH; this.lineElement.modeInteraction.setModeItem('draw_select_new'); this.emit("endGenerator", this.endPositionPointElement); this.end(); }; LineGenerator.prototype.updatePointEndPositionPointGenerator = function () { this.lineElement.modeInteraction.setMode("delete_draw"); if (this.startPositionPointElement !== this.endPositionPointElement) { this.startEndPositionPointGenerator(); this._state = LineGeneratorStates.FINISH; this.lineElement.modeInteraction.setModeItem('draw_select'); this.emit("endGenerator", this.endPositionPointElement); this.end(); } else { this._setupGenerator(); } }; LineGenerator.prototype.abortEndPositionPointGenerator = function () { this.emit("abortGenerator"); this.destroy(); }; LineGenerator.prototype.onPressedShiftKey = function (keyCode, event) { event.preventDefault(); if (this._state === LineGeneratorStates.START || this._state === LineGeneratorStates.FINISH || !this.lineElement) { return; } if (this.lineElement) { this.lineElement.model.isBezier = true; this.lineElement.emit("debounceDraw"); } }; LineGenerator.prototype.onReleaseShiftKey = function (keyCode, event) { event.preventDefault(); if (this._state === LineGeneratorStates.START || this._state === LineGeneratorStates.FINISH || !this.lineElement) { return; } if (this.lineElement) { this.lineElement.model.isBezier = false; this.lineElement.emit("debounceDraw"); } }; Object.defineProperty(LineGenerator.prototype, "drawInteraction", { get: function () { if (this._state === LineGeneratorStates.START) { return this._startPositionPointGenerator.drawInteraction; } else { return this._endPositionPointGenerator.drawInteraction; } }, enumerable: false, configurable: true }); Object.defineProperty(LineGenerator.prototype, "startPositionPointElement", { get: function () { return this.startElement ? this.startElement : this._startPositionPointGenerator.positionPointElement; }, enumerable: false, configurable: true }); Object.defineProperty(LineGenerator.prototype, "endPositionPointElement", { get: function () { return this._endPositionPointGenerator.positionPointElement; }, enumerable: false, configurable: true }); Object.defineProperty(LineGenerator.prototype, "lineElement", { get: function () { return this._lineElement; }, enumerable: false, configurable: true }); Object.defineProperty(LineGenerator.prototype, "bezierCourve", { get: function () { if (this.data.bezierCourve) { return this.data.bezierCourve; } else { return MedsurfDraw.Keyboard.isKeyDown('ShiftLeft') || MedsurfDraw.Keyboard.isKeyDown('ShiftRight'); } }, set: function (value) { this.data.bezierCourve = value; }, enumerable: false, configurable: true }); Object.defineProperty(LineGenerator.prototype, "stickMode", { get: function () { return this.data.stickMode; }, set: function (value) { this.data.stickMode = value; }, enumerable: false, configurable: true }); Object.defineProperty(LineGenerator.prototype, "startElement", { get: function () { return this.data.startElement; }, set: function (value) { this.data.startElement = value; }, enumerable: false, configurable: true }); return LineGenerator; }(BaseGenerator)); export { LineGenerator }; //# sourceMappingURL=LineGenerator.js.map