UNPKG

@aurigma/design-atoms

Version:

Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.

447 lines 23.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 (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; import { BaseInputHandler } from "../BaseInputHandler"; import { InputState } from "./../../../Input/InputManager/IInputManager"; import { CreateInputHandlerStateMachine, CreateInputHandlerState } from "./CreateInputHandlerStateMachine"; import { ArgumentException } from "@aurigma/design-atoms-model/Exception"; import { Cursor } from "./../../../Utils/Common"; import { EventObject } from "@aurigma/design-atoms-model/EventObject"; import { RectangleF, RotatedRectangleF, PointF, Path } from "@aurigma/design-atoms-model/Math"; import { BarcodeFormat } from "@aurigma/design-atoms-model/Product/Items"; import { Deferred } from "./../../../Utils/Deferred"; import * as _ from "underscore"; import { ItemsCommand, ItemType, OriginPointType, SurfaceCommand } from "@aurigma/design-atoms-interfaces"; var CreateInputHandler = /** @class */ (function (_super) { __extends(CreateInputHandler, _super); function CreateInputHandler(_args, _viewer, inputManager) { var _this = _super.call(this, inputManager) || this; _this._args = _args; _this._viewer = _viewer; _this._onCompleted = new EventObject(); _this._onStateChanged = function (e) { switch (e.state) { case CreateInputHandlerState.Initial: break; case CreateInputHandlerState.PointSelected: _this._onPointSelectedState(e.point); break; case CreateInputHandlerState.RectangleSelectionStarted: _this._startPoint = e.point; _this._onRectangleSelectionStarted(e.point); break; case CreateInputHandlerState.RectangleSelectionChanged: _this._endPoint = e.point; _this._onRectangleSelectionChanged(); break; case CreateInputHandlerState.RectangleSelected: _this._endPoint = e.point; _this._onRectangleSelected(); break; } }; if (_this._args == null) throw new ArgumentException("CreateInputHandler: CreateInputHandler args cannot be null"); if (_this._args.item == null) throw new ArgumentException("CreateInputHandler: args.createdItem cannot be null!"); var extendedCreateArgs = _this._args.item; if (extendedCreateArgs.selection != null || extendedCreateArgs.click != null) { if (extendedCreateArgs.click == null || extendedCreateArgs.selection == null) throw new ArgumentException("CreateInputHandler: Point and Rectangle create args must be exist both or must be null"); if (extendedCreateArgs.click.type == null || extendedCreateArgs.selection.type == null) throw new ArgumentException("CreateInputHandler: Item type cannot be null"); } else { var createItemArgs = _this._args.item; if (createItemArgs.type == null) throw new ArgumentException("CreateInputHandler: Item type cannot be null"); } _this._stateMachine = new CreateInputHandlerStateMachine(); _this._stateMachine.addStateChanged(_this._onStateChanged); _this._selection.lock(); _this._viewer.setCursor(Cursor.cross); _this._selection.ignoreSimpleMode = true; return _this; } CreateInputHandler.prototype.addOnCompleted = function (handler) { this._onCompleted.add(handler); }; CreateInputHandler.prototype.removeOnCompleted = function (handler) { this._onCompleted.remove(handler); }; CreateInputHandler.prototype.dispose = function () { _super.prototype.dispose.call(this); this._stateMachine.removeStateChanged(this._onStateChanged); this._selection.ignoreSimpleMode = false; }; CreateInputHandler.prototype._onClick = function (params) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { this._currentPointerParams = params; this._stateMachine.onClick(params); return [2 /*return*/]; }); }); }; CreateInputHandler.prototype._onMove = function (params) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { this._currentPointerParams = params; this._stateMachine.onMove(params); return [2 /*return*/]; }); }); }; CreateInputHandler.prototype._onLongTap = function (params) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { this._currentPointerParams = params; this._stateMachine.onLongTap(params); return [2 /*return*/]; }); }); }; CreateInputHandler.prototype._onTransform = function (params) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/]; }); }); }; CreateInputHandler.prototype._onWheel = function (params) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/]; }); }); }; CreateInputHandler.prototype._onPointerDown = function (params) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/]; }); }); }; CreateInputHandler.prototype._onHover = function (params) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/]; }); }); }; CreateInputHandler.prototype._onKey = function (params) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/]; }); }); }; CreateInputHandler.prototype._onDoubleClick = function (params) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/]; }); }); }; Object.defineProperty(CreateInputHandler.prototype, "_rubberbandHandler", { get: function () { return this._viewer.canvas.rubberbandHandler; }, enumerable: true, configurable: true }); Object.defineProperty(CreateInputHandler.prototype, "_selection", { get: function () { return this._viewer.canvas.selection; }, enumerable: true, configurable: true }); Object.defineProperty(CreateInputHandler.prototype, "_selectionRect", { get: function () { var startPoint = this._applyPrintAreaMargins(this._startPoint); var endPoint = this._applyPrintAreaMargins(this._endPoint); return new RectangleF(Math.min(startPoint.x, endPoint.x), Math.min(startPoint.y, endPoint.y), Math.abs(startPoint.x - endPoint.x), Math.abs(startPoint.y - endPoint.y)); }, enumerable: true, configurable: true }); Object.defineProperty(CreateInputHandler.prototype, "_selectAfterCreate", { get: function () { return this._args.selectAfterCreate != null ? this._args.selectAfterCreate : true; }, enumerable: true, configurable: true }); CreateInputHandler.prototype._onRectangleSelectionStarted = function (point) { return __awaiter(this, void 0, void 0, function () { var args, selectionFirst, selectionArgs; return __generator(this, function (_a) { switch (_a.label) { case 0: this._viewer.history.pause(); args = this._getCreateItemArgs(point, true); this._selection.unlock(); this._arbitraryResize = args.type !== ItemType.BarcodeItem; selectionFirst = args.type === ItemType.BoundedTextItem || this._isDatabar(args); if (!!selectionFirst) return [3 /*break*/, 2]; this._itemAdded = new Deferred(); return [4 /*yield*/, this._addItem(args)]; case 1: _a.sent(); selectionArgs = { arbitraryResize: this._arbitraryResize, finished: false, point: point.clone(), resizeIndex: 3 }; this._selection.resizeByPoint(selectionArgs, InputState.Started, point.clone()); this._itemAdded.resolve(); return [3 /*break*/, 3]; case 2: this._rubberbandHandler.updateRubberband(point.clone(), point.clone(), InputState.Started); _a.label = 3; case 3: this._selection.update(); return [2 /*return*/]; } }); }); }; CreateInputHandler.prototype._onRectangleSelectionChanged = function () { return __awaiter(this, void 0, void 0, function () { var selectionArgs; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!(this._itemAdded != null)) return [3 /*break*/, 2]; return [4 /*yield*/, this._itemAdded.promise]; case 1: _a.sent(); _a.label = 2; case 2: if (this._rubberbandHandler.isActive) { this._rubberbandHandler.updateRubberband(this._currentPointerParams.workspace, this._startPoint, InputState.InProgress); } else { selectionArgs = { arbitraryResize: this._arbitraryResize, finished: false, point: this._currentPointerParams.workspace.clone(), resizeIndex: 3 }; this._selection.resizeByPoint(selectionArgs, InputState.InProgress, this._startPoint); } this._viewer.canvas.redrawDesign(); return [2 /*return*/]; } }); }); }; CreateInputHandler.prototype._onRectangleSelected = function () { return __awaiter(this, void 0, void 0, function () { var currentPointerParams, createItemArgs, isBounded, isDatabar, rect, boundedTextItem, data, barcodeItem, handler, selectionArgs; return __generator(this, function (_a) { switch (_a.label) { case 0: currentPointerParams = _.clone(this._currentPointerParams); if (!(this._itemAdded != null)) return [3 /*break*/, 2]; return [4 /*yield*/, this._itemAdded.promise]; case 1: _a.sent(); _a.label = 2; case 2: createItemArgs = this._getCreateItemArgs(this._startPoint, true); isBounded = createItemArgs.type === ItemType.BoundedTextItem; isDatabar = this._isDatabar(createItemArgs); rect = this._selectionRect; if (!isBounded) return [3 /*break*/, 6]; return [4 /*yield*/, this._viewer.commandManager.execute(ItemsCommand.createItem, createItemArgs)]; case 3: boundedTextItem = _a.sent(); return [4 /*yield*/, this._viewer.commandManager.execute(SurfaceCommand.addItems, { items: [boundedTextItem] })]; case 4: _a.sent(); return [4 /*yield*/, this._textWorkaround(rect, boundedTextItem)]; case 5: _a.sent(); return [3 /*break*/, 11]; case 6: if (!isDatabar) return [3 /*break*/, 9]; data = createItemArgs; data.itemData.location.x = rect.left; data.itemData.location.y = rect.top; data.itemData.width = rect.width; data.itemData.height = rect.height; return [4 /*yield*/, this._viewer.commandManager.execute(ItemsCommand.createItem, data)]; case 7: barcodeItem = _a.sent(); return [4 /*yield*/, this._viewer.commandManager.execute(SurfaceCommand.addItems, { items: [barcodeItem] })]; case 8: _a.sent(); return [3 /*break*/, 11]; case 9: handler = this._viewer.getHandler(this._item); return [4 /*yield*/, handler.waitUpdate()]; case 10: _a.sent(); _a.label = 11; case 11: if (this._rubberbandHandler.isActive) { this._rubberbandHandler.updateRubberband(currentPointerParams.workspace, this._startPoint, InputState.Finished); } else { selectionArgs = { arbitraryResize: this._arbitraryResize, finished: true, point: this._currentPointerParams.workspace.clone(), resizeIndex: 3 }; this._selection.resizeByPoint(selectionArgs, InputState.Finished, this._startPoint); } this._onCompleted.notify(rect); if (!this._selectAfterCreate) { this._selection.clearSelectedItemHandlers(); } this._viewer.history.resume(); return [2 /*return*/]; } }); }); }; CreateInputHandler.prototype._onPointSelectedState = function (pt) { return __awaiter(this, void 0, void 0, function () { var args; return __generator(this, function (_a) { switch (_a.label) { case 0: args = this._getCreateItemArgs(pt, false); if (!(args != null)) return [3 /*break*/, 2]; return [4 /*yield*/, this._addItem(args)]; case 1: _a.sent(); _a.label = 2; case 2: this._onCompleted.notify(pt); if (!this._selectAfterCreate) this._selection.clearSelectedItemHandlers(); return [2 /*return*/]; } }); }); }; //bad workaround, wait for inplace text CreateInputHandler.prototype._textWorkaround = function (rect, item) { return __awaiter(this, void 0, void 0, function () { var handler; return __generator(this, function (_a) { switch (_a.label) { case 0: handler = this._viewer.getHandler(item); return [4 /*yield*/, handler.waitUpdate()]; case 1: _a.sent(); item.sourceRectangle = rect; item.textRectangle = rect; item.sourcePath = Path.rectangle(rect.left, rect.top, rect.width, rect.height); item.transform = RotatedRectangleF.fromRectangleF(rect) .getTransform(RotatedRectangleF.fromRectangleF(item.sourceRectangle)); handler.update(); return [2 /*return*/]; } }); }); }; CreateInputHandler.prototype._addItem = function (args) { return __awaiter(this, void 0, void 0, function () { var item; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._viewer.commandManager.execute(ItemsCommand.createItem, args)]; case 1: item = _a.sent(); this._item = item; return [4 /*yield*/, this._viewer.commandManager.execute(SurfaceCommand.addItems, { items: [item] })]; case 2: _a.sent(); return [4 /*yield*/, this._viewer.canvas.waitUpdate()]; case 3: _a.sent(); return [2 /*return*/]; } }); }); }; CreateInputHandler.prototype._getCreateItemArgs = function (point, isRectangle) { if (isRectangle === void 0) { isRectangle = false; } var extendedCreateArgs = this._args.item; if (extendedCreateArgs.click == null && extendedCreateArgs.selection == null) return this._getUpdatedArgs(this._args.item, point, isRectangle); return this._getUpdatedArgs(isRectangle ? extendedCreateArgs.selection : extendedCreateArgs.click, point, isRectangle); }; CreateInputHandler.prototype._getUpdatedArgs = function (args, point, isRectangle) { if (isRectangle === void 0) { isRectangle = false; } var argsClone = Object.assign({}, args); argsClone.itemData = Object.assign({}, args.itemData || {}); var data = argsClone.itemData; //clone if (data.location == null) data.location = {}; point = this._applyPrintAreaMargins(point); data.location.x = point.x; data.location.y = point.y; if (isRectangle) { data.width = 1; data.height = 1; data.location.originX = OriginPointType.Left; data.location.originY = OriginPointType.Top; } argsClone.relativeToPrintArea = false; return argsClone; }; CreateInputHandler.prototype._isDatabar = function (args) { var _a, _b; return args.type === ItemType.BarcodeItem && ((_b = (_a = args.itemData) === null || _a === void 0 ? void 0 : _a.barcodeContent) === null || _b === void 0 ? void 0 : _b.barcodeFormat) == BarcodeFormat.DATABAR_EXPANDED_STACKED; }; CreateInputHandler.prototype._applyPrintAreaMargins = function (point) { var offset = this._viewer.canvas.offset; return new PointF(point.x - offset.x, point.y - offset.y); }; return CreateInputHandler; }(BaseInputHandler)); export { CreateInputHandler }; //# sourceMappingURL=CreateInputHandler.js.map