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.

324 lines 15.5 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 { ContentItemHandler } from "./ContentItemHandler"; import { BarcodeFormat } from "@aurigma/design-atoms-model/Product/Items"; import { BarcodeHandlerData } from "./BarcodeHandlerData"; import { ImageUtils } from "../Utils/ImageUtils"; import { ImageContainer } from "./ImageContainer"; import { Graphics } from "../Graphics"; import { Path, PointF, Transform } from "@aurigma/design-atoms-model/Math"; import Environment from "@aurigma/design-atoms-model/Utils/Environment"; // ReSharper disable InconsistentNaming var BarcodeItemHandler = /** @class */ (function (_super) { __extends(BarcodeItemHandler, _super); function BarcodeItemHandler(item, textWhizz, apiClient, colorPreviewService) { if (textWhizz === void 0) { textWhizz = null; } var _this = _super.call(this, item, textWhizz, apiClient, colorPreviewService) || this; _this._currentFileId = null; _this._stubTextImageFileId = null; _this._errorTextImageFileId = null; _this._errorMessage = null; _this._maxImageSize = 2000 * 2000; _this._fallbackFileId = null; _this._stubImageContainer = new ImageContainer(_this._onStubImageLoaded.bind(_this)); return _this; } Object.defineProperty(BarcodeItemHandler.prototype, "item", { get: function () { return this._getItem(); }, set: function (item) { _super.prototype._setItem.call(this, item); }, enumerable: true, configurable: true }); Object.defineProperty(BarcodeItemHandler.prototype, "currentFileId", { get: function () { return this._currentFileId; }, set: function (value) { this._currentFileId = value; }, enumerable: true, configurable: true }); Object.defineProperty(BarcodeItemHandler.prototype, "stubTextImageFileId", { get: function () { return this._stubTextImageFileId; }, set: function (value) { this._stubTextImageFileId = value; }, enumerable: true, configurable: true }); Object.defineProperty(BarcodeItemHandler.prototype, "errorTextImageFileId", { get: function () { return this._errorTextImageFileId; }, set: function (value) { this._errorTextImageFileId = value; }, enumerable: true, configurable: true }); Object.defineProperty(BarcodeItemHandler.prototype, "errorMessage", { get: function () { return this._errorMessage; }, set: function (value) { this._errorMessage = value; }, enumerable: true, configurable: true }); BarcodeItemHandler.prototype.quickUpdate = function () { if (this.isVisible() && this.canvas && !this.canvas.service.imageLoaderService.hasImageContainerFor(this._fileId)) { _super.prototype.quickUpdate.call(this); } }; BarcodeItemHandler.prototype.drawItemHandler = function (itemHandlerCtx) { if (itemHandlerCtx == null) { return; } if (!this._isReadyToDraw) { this.canvas.drawWaitClock(itemHandlerCtx, this.rectangle.center); return; } var placeholder = this.parentPlaceholder != null ? this.parentPlaceholder.rectangle : this.rectangle; var content = this.rectangle; var opacity = this.isNormalRenderingType ? this.item.opacity : 1; var _a = this._getItemColorPreviews(), fillColorPreview = _a.fillColorPreview, borderColorPreview = _a.borderColorPreview, altBorderColorPreview = _a.altBorderColorPreview; Graphics.fillRectangle(itemHandlerCtx, content, fillColorPreview === null || fillColorPreview === void 0 ? void 0 : fillColorPreview.toString(), opacity); if (this._imageContainer.isLoaded) this._drawImage(itemHandlerCtx, this.canvas.disableSmoothing); if (this._stubImageContainer.isLoaded) this._drawImage(itemHandlerCtx, this.canvas.disableSmoothing, 1, this._stubImageContainer.image); if (this._imageContainer.isLoading || this._stubImageContainer.isLoading) this.canvas.drawWaitClock(itemHandlerCtx, new PointF(placeholder.centerX, placeholder.centerY)); var borderWidth = this._getActualBorderWidth(); content.width += borderWidth; content.height += borderWidth; var path = Path.rotatedRectangle(content); Graphics.drawStroke(itemHandlerCtx, path, content.center, new Transform(), borderWidth, borderColorPreview === null || borderColorPreview === void 0 ? void 0 : borderColorPreview.toString(), altBorderColorPreview === null || altBorderColorPreview === void 0 ? void 0 : altBorderColorPreview.toString(), this.item.opacity, this.item.dash); }; BarcodeItemHandler.prototype.onResized = function () { this.update(); return _super.prototype.onResized.call(this); }; BarcodeItemHandler.prototype.dispose = function () { _super.prototype.dispose.call(this); this._stubImageContainer.dispose(); }; BarcodeItemHandler.prototype._isReady = function () { return _super.prototype._isReady.call(this) && !(this._showStubImage && !this._stubImageContainer.isLoaded); }; BarcodeItemHandler.prototype._updateImpl = function (beforeUpdate, afterUpdate) { return __awaiter(this, void 0, void 0, function () { var postUpdate; var _this = this; return __generator(this, function (_a) { if (!this.isVisible()) return [2 /*return*/]; if (this.currentFileId == null && this.errorMessage == null || !this._isSquareBarcodeFormat() && this.item.barcodeOptions.pureBarcode === false) { postUpdate = function () { var _a; if (typeof afterUpdate == "function") afterUpdate(); (_a = _this.canvas) === null || _a === void 0 ? void 0 : _a.updateSelection(); }; _super.prototype._update.call(this, null, beforeUpdate, postUpdate); } else { this.quickUpdate(); } return [2 /*return*/]; }); }); }; BarcodeItemHandler.prototype._createDataInstance = function (handler) { return new BarcodeHandlerData(handler); }; BarcodeItemHandler.prototype._onItemPropertyChanged = function (sender, propertyName) { var _this = this; this._fallbackFileId = this._fileId; switch (propertyName) { case "barcodeFormat": case "barcodeOptions": case "barcodeContent": case "color": if (propertyName == "barcodeFormat" || propertyName == "barcodeOptions") { this._fallbackFileId = null; } this.currentFileId = null; this.stubTextImageFileId = null; this.errorTextImageFileId = null; this.errorMessage = null; this.update(null, function () { if (_this.errorMessage == null) _this._fallbackFileId = null; }); break; default: } _super.prototype._onItemPropertyChanged.call(this, sender, propertyName); }; BarcodeItemHandler.prototype._getDefaultPermissions = function () { var permissions = _super.prototype._getDefaultPermissions.call(this); permissions.manipulation.resizeGrips.setCornerArbitrary(false); permissions.manipulation.resizeGrips.edge = false; return permissions; }; BarcodeItemHandler.prototype._createImageUrl = function () { if (this._fileId == null || this.canvas == null) return null; var pixelWidth, pixelHeight; var zoom = this.canvas.zoom; var XDpi = Environment.screenDpi; var YDpi = Environment.screenDpi; var pttopx_x = zoom * XDpi / 72; var pttopx_y = zoom * YDpi / 72; pixelWidth = this.rectangle.width * pttopx_x; pixelHeight = this.rectangle.height * pttopx_y; var f = this._maxImageSize / pixelWidth / pixelHeight; if (f < 1) { f = Math.sqrt(f); pixelWidth = pixelWidth * f; pixelHeight = pixelHeight * f; } pixelWidth = Math.round(pixelWidth); pixelHeight = Math.round(pixelHeight); this._pixelWidth = pixelWidth; this._pixelHeight = pixelHeight; return ImageUtils.getImageUrl(this.canvas.renderingConfigProvider, this.canvas.designAtomsApiClient, this._fileId, this._pixelWidth, this._pixelHeight, false); }; BarcodeItemHandler.prototype._isLoadingImage = function () { return this._imageContainer.isLoading || !(this._showStubImage && !this._stubImageContainer.isLoading); }; BarcodeItemHandler.prototype._setIsLoadingImage = function (value) { this._imageContainer.isLoading = value; if (this._showStubImage) this._stubImageContainer.isLoading = value; }; BarcodeItemHandler.prototype._updateImageUrl = function () { _super.prototype._updateImageUrl.call(this); this._updateStubImageUrl(); }; BarcodeItemHandler.prototype._updateStubImageUrl = function () { if (!this.isVisible()) return; var cv = this.canvas; if (cv == null || !cv.isInitialized) return; var url = this._createStubImageUrl(); if (url === this._stubImageContainer.source) return; if (url == null) { this._stubImageContainer.clear(); cv.redraw(); } else this._stubImageContainer.updateUrl(url); }; BarcodeItemHandler.prototype._onImageLoaded = function (e, target) { this._imageContainer.isLoading = false; if (this._stubImageContainer.isLoading) return; if (this.canvas != null) { this.canvas.redraw(); this.canvas.updateSelection(); } this._dispatchReadyEvent(); }; Object.defineProperty(BarcodeItemHandler.prototype, "_fileId", { get: function () { var _a; return (_a = this.currentFileId) !== null && _a !== void 0 ? _a : this._fallbackFileId; }, enumerable: true, configurable: true }); Object.defineProperty(BarcodeItemHandler.prototype, "_showStubImage", { get: function () { return this.item.barcodeData.isEmpty || this.errorMessage != null; }, enumerable: true, configurable: true }); BarcodeItemHandler.prototype._createStubImageUrl = function () { var stubImageFileId = this.item.barcodeData.isEmpty ? this.stubTextImageFileId : this.errorTextImageFileId; if (stubImageFileId == null) return; return ImageUtils.getImageUrl(this.canvas.renderingConfigProvider, this.canvas.designAtomsApiClient, stubImageFileId, this._pixelWidth, this._pixelHeight, false); }; BarcodeItemHandler.prototype._onStubImageLoaded = function (e, target) { this._stubImageContainer.isLoading = false; if (this._imageContainer.isLoading) return; if (this.canvas != null) { this.canvas.redraw(); this.canvas.updateSelection(); } this._dispatchReadyEvent(); }; BarcodeItemHandler.prototype._isSquareBarcodeFormat = function () { var format = this.item.barcodeFormat; return format == BarcodeFormat.QR_CODE || format == BarcodeFormat.AZTEC || format == BarcodeFormat.DATA_MATRIX; }; BarcodeItemHandler.typeName = "BarcodeItemHandler"; return BarcodeItemHandler; }(ContentItemHandler)); export { BarcodeItemHandler }; //# sourceMappingURL=BarcodeItemHandler.js.map