@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
193 lines • 8.61 kB
JavaScript
import { RotatedRectangleF, PointF } from "@aurigma/design-atoms-model/Math";
import { Container, RenderingType } from "@aurigma/design-atoms-model/Product";
import { Item } from "@aurigma/design-atoms-model/Product/Items";
import { ISize } from "../Utils/Math";
import { SurfaceHandler } from "./SurfaceHandler";
import { CoordinatesConvertUtils } from "../Utils/CoordinatesConvertUtils";
import { QuerySelector } from "./QuerySelector";
export class ProductHandler {
constructor(_viewer, _eventManager) {
this._viewer = _viewer;
this._eventManager = _eventManager;
this.getHandler = (item) => {
return this._viewer.getHandler(item);
};
}
get currentSurface() {
return this._viewer.surface;
}
set currentSurface(surface) {
this._viewer.surface = surface;
}
get contentAngle() {
return this._viewer.contentAngle;
}
get regionForCurrentContainer() {
const userEditContainer = this._viewer.userEditContainer;
return userEditContainer != null ? userEditContainer.region : null;
}
get product() {
const currentSurface = this.currentSurface;
if (currentSurface == null)
return null;
return currentSurface.parentProduct;
}
get selectedItems() {
return this._viewer.canvas.getSelectedItemHandlers().toArray().map(i => i.item);
}
get userEditContainer() {
return this._viewer.userEditContainer;
}
get userEditPrintArea() {
return this._viewer.getUserEditPrintArea();
}
get suppressOutOfRegionManipulation() {
return this._viewer.canvas.suppressOutOfRegionManipulation;
}
/**
* Gets or sets interactive containers of the Product.
* @remarks Interactive containers allows to define product containers which items can be selected by user.
* If this property is null or empty then items from any containers can be selected by user.
*/
get interactiveContainers() {
return this._interactiveContainers;
}
set interactiveContainers(value) {
if (this._interactiveContainers == value)
return;
this._interactiveContainers = value;
this._eventManager.interactiveContainersChangedEvent.notify(this._interactiveContainers);
}
queryItems(selectors, queryOptions) {
var _a, _b, _c;
if (selectors == null)
return [];
const ignoreMockups = !((_a = queryOptions === null || queryOptions === void 0 ? void 0 : queryOptions.includeMockups) !== null && _a !== void 0 ? _a : false);
const flatGroupItems = (_b = queryOptions === null || queryOptions === void 0 ? void 0 : queryOptions.flatGroupItems) !== null && _b !== void 0 ? _b : false;
const includePlaceholderContents = (_c = queryOptions === null || queryOptions === void 0 ? void 0 : queryOptions.includePlaceholderContents) !== null && _c !== void 0 ? _c : false;
const getAllItemsOptions = {
ignoreMockups: ignoreMockups,
flatGroupItems: flatGroupItems,
includePlaceholderContents: includePlaceholderContents
};
const items = this._viewer.productHandler.product.getAllItems(getAllItemsOptions);
const querySelector = new QuerySelector();
const result = querySelector.process(items, selectors);
return result;
}
redraw() {
var _a, _b;
(_b = (_a = this._viewer) === null || _a === void 0 ? void 0 : _a.canvas) === null || _b === void 0 ? void 0 : _b.redraw();
}
beginMultiCall() {
if (this._viewer == null || this._viewer.canvas == null || this._viewer.canvas.service == null)
return;
this._viewer.canvas.service.beginMultiCall();
}
endMultiCall() {
if (this._viewer == null || this._viewer.canvas == null || this._viewer.canvas.service == null)
return;
this._viewer.canvas.service.endMultiCall();
}
async waitUpdate() {
var _a;
return await ((_a = this._viewer.canvas) === null || _a === void 0 ? void 0 : _a.waitUpdate());
}
getProductColors(initialProduct) {
const items = initialProduct.getAllItems();
const allColors = [];
for (let item of items) {
const handler = this._viewer.getHandler(item);
allColors.push(...handler.getColors());
}
return allColors;
}
checkSelectionByHitTest(x, y) {
const point = CoordinatesConvertUtils.pageToWorkspacePoint(new PointF(x, y), this._viewer);
const isHit = this._viewer.canvas.isSelectionByHitTest(point);
return isHit;
}
getPrintAreaVisualSize(targetContainer, printArea) {
const printAreaBounds = this.getPrintAreaBounds(targetContainer, printArea);
return this.getVisualSize(printAreaBounds);
}
getVisualSize(size) {
const angle = this._viewer.contentAngle;
if (angle === 0)
return ISize.from(size);
const boundsRect = new RotatedRectangleF(0, 0, size.width, size.height);
boundsRect.rotateAt(angle, new PointF());
return ISize.from(boundsRect.bounds);
}
getPrintAreaBounds(container, printArea) {
const targetContainer = container !== null && container !== void 0 ? container : this._viewer.userEditContainer;
const targetPrintArea = printArea !== null && printArea !== void 0 ? printArea : this._viewer.getUserEditPrintArea();
return targetContainer.region != null ? targetContainer.region : targetPrintArea.bounds;
}
isInteractive(object) {
if (object instanceof Container) {
if (this._interactiveContainers == null || this._interactiveContainers.length == 0)
return true;
return this._interactiveContainers.includes(object);
}
else if (object instanceof Item) {
return this.isInteractive(object.parentContainer);
}
throw new Error("Unexpected object type in isInteractive");
}
updateSelection() {
if (this._viewer.canvas == null)
return;
this._viewer.canvas.updateSelection();
}
updateMaskedContainers() {
if (this._viewer.canvas == null)
return;
const canvas = this._viewer.canvas;
const itemHandlersToKeepSelected = [];
canvas.getSelectedItemHandlers().forEach(element => {
if (this.userEditContainer == element.item.parentContainer)
itemHandlersToKeepSelected.push(element);
});
canvas.setSelectedItemHandlers(itemHandlersToKeepSelected);
const allContainers = this.currentSurface.containers;
let maskedContainers = [];
let channelContainers = allContainers.where(c => c.renderingType !== RenderingType.Normal).toArray();
if (this.userEditContainer != null) {
maskedContainers = channelContainers.slice(channelContainers.findIndex(c => this.userEditContainer.id === c.id) + 1, channelContainers.length);
}
else {
maskedContainers = channelContainers;
}
this._maskedContainers = maskedContainers;
this._eventManager.maskedContainersChangedEvent.notify(this._maskedContainers);
}
isMasked(container) {
if (this._maskedContainers == null || this._maskedContainers.length == 0)
return false;
return this._maskedContainers.includes(container);
}
exitEditMode() {
var _a;
(_a = this._viewer) === null || _a === void 0 ? void 0 : _a.exitEditMode();
}
isItemSelected(item) {
const handler = this.getHandler(item);
return this._viewer.canvas.isItemHandlerSelected(handler);
}
async applyLayout(product, surfaceFilterFunc) {
if (!product)
product = this.product;
const handlerFactory = this._viewer.canvas.handlerFactory;
const applying = product.surfaces.toArray().map((surface, i) => {
let skipSurface = false;
if (surfaceFilterFunc)
skipSurface = surfaceFilterFunc(i, surface);
if (skipSurface)
return null;
return SurfaceHandler.applyLayout(handlerFactory, this._viewer.canvas, surface);
}).filter((promise) => !!promise);
await Promise.all(applying);
}
}
//# sourceMappingURL=ProductHandler.js.map