UNPKG

@aurigma/design-atoms-model

Version:

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

267 lines 11.5 kB
import { ModelComponent } from "./ModelComponent"; import { RenderingType } from "./RenderingType"; import { Configuration } from "../Configuration"; import { GroupItem, PlaceholderItem } from "./Items"; import { SurfaceMockup } from "./SurfaceMockup"; import { ArgumentException } from "../Exception"; import { Collection } from "../Collection"; import { SurfaceContainer } from "./Container"; import { EventWithSenderArg } from "../EventObject"; import { normalizeAngle } from "../Math"; import * as Enumerable from "linq/linq"; export class Surface extends ModelComponent { constructor(width, height, mockup, printAreas = [], id, name, initDefaultContainers) { super(id, name); this._rotateAngle = 0; this._mockup = new SurfaceMockup(); this._printAreas = new Collection(); this._previewMockups = new Collection(); this._interactiveZones = new Collection(); this._printAreasCollectionChangedEvent = new EventWithSenderArg(); this._printAreaAddedEvent = new EventWithSenderArg(); this._printAreaRemovedEvent = new EventWithSenderArg(); this._activityZoneChangedEvent = new EventWithSenderArg(); this._onContainersItemAdded = (data) => { data.item.parentComponent = this; }; this._onContainersItemRemoved = (data) => { data.item.parentComponent = null; }; this.watermark = null; this._containers = new Collection(); this._onPrintAreaAdded = ({ item: printArea }) => { printArea.parentSurface = this; this._printAreaAddedEvent.notify(this, printArea); }; this._onPrintAreaRemoved = ({ item: printArea }) => { printArea.parentSurface = null; this._printAreaRemovedEvent.notify(this, printArea); }; this._onInteractiveZoneCollectionChanged = (args) => { this._propertyChanged.notify(this, "interactiveZones"); }; this.width = width != null ? width : 100; this.height = height != null ? height : 100; this.parentProduct = null; this.mockup = mockup != null ? mockup : new SurfaceMockup(); this.printAreas.add_itemAdded(this._onPrintAreaAdded); this.printAreas.add_itemRemoved(this._onPrintAreaRemoved); this.printAreas.setRange(printAreas); this._containers.add_itemAdded(this._onContainersItemAdded); this._containers.add_itemRemoved(this._onContainersItemRemoved); this._subscribeInteractiveZonesEvents(); if (initDefaultContainers) { const bgContainer = new SurfaceContainer(); bgContainer.name = Configuration.BG_CONTAINER_NAME; this.containers.push(bgContainer); const mainContainer = new SurfaceContainer(); mainContainer.name = Configuration.MAIN_CONTAINER_NAME; this.containers.push(mainContainer); const fgContainer = new SurfaceContainer(); fgContainer.name = Configuration.FG_CONTAINER_NAME; this.containers.push(fgContainer); } } get rotateAngle() { return this._rotateAngle; } set rotateAngle(value) { const normalizedValue = normalizeAngle(value); if (normalizedValue === this._rotateAngle) return; this._rotateAngle = normalizedValue; this._propertyChanged.notify(this, "rotateAngle"); } get width() { return this._width; } set width(value) { if (value === this._width) return; this._width = value; this._propertyChanged.notify(this, "width"); } get height() { return this._height; } set height(value) { if (value === this._height) return; this._height = value; this._propertyChanged.notify(this, "height"); } get mockup() { return this._mockup; } set mockup(value) { if (value == null) throw new ArgumentException("mockup can`t be null"); if (value === this._mockup) return; this._mockup = value; if (this._mockup != null) this._mockup.parentSurface = this; this._propertyChanged.notify(this, "mockup"); } get previewMockups() { return this._previewMockups; } set previewMockups(value) { if (this._previewMockups != null) { for (let it of this._previewMockups) { it.parentSurface = null; } } this._previewMockups = value; if (this._previewMockups == null) return; for (let i = 0; i < this._previewMockups.length; ++i) { this._previewMockups.get(i).parentSurface = this; } } get containers() { return this._containers; } set containers(value) { if (value == null || value === this._containers) return; if (this._containers != null) { this._containers.clear(); this._containers.remove_itemAdded(this._onContainersItemAdded); this._containers.remove_itemRemoved(this._onContainersItemRemoved); } this._containers = value; for (let i = 0; i < this._containers.length; i++) this._onContainersItemAdded({ index: i, item: this._containers.get(i) }); this._containers.add_itemAdded(this._onContainersItemAdded); this._containers.add_itemRemoved(this._onContainersItemRemoved); } get printAreas() { return this._printAreas; } set printAreas(value) { if (value == null || value === this._printAreas) return; if (this._printAreas != null) { this._printAreas.clear(); this._printAreas.remove_itemAdded(this._onPrintAreaAdded); this._printAreas.remove_itemRemoved(this._onPrintAreaRemoved); } this._printAreas = value; for (let pa of this._printAreas) this._onPrintAreaAdded({ item: pa }); this._printAreas.add_itemAdded(this._onPrintAreaAdded); this._printAreas.add_itemRemoved(this._onPrintAreaRemoved); } get interactiveZones() { return this._interactiveZones; } set interactiveZones(value) { if (value == null || value === this._interactiveZones) return; this._unsubscribeInteractiveZonesEvents(); this._interactiveZones = value; this._subscribeInteractiveZonesEvents(); this._propertyChanged.notify(this, "interactiveZones"); } add_printAreaAdded(h) { this._printAreaAddedEvent.add(h); } remove_printAreaAdded(h) { this._printAreaAddedEvent.remove(h); } add_printAreaRemoved(h) { this._printAreaRemovedEvent.add(h); } remove_printAreaRemoved(h) { this._printAreaRemovedEvent.remove(h); } getSimplifiedObject() { var simplified = super.getSimplifiedObject(["parentProduct", "nonChannelContainerItems"]); return simplified; } getAllItems(options = { ignoreMockups: false }) { let containers = options.ignoreMockups ? Enumerable.from(this.containers) : Enumerable.empty() .concat(this.mockup.underContainers.cast(), this.containers.cast(), this.mockup.overContainers.cast()); if (options.containerFilterFunc != null) containers = containers.where(options.containerFilterFunc); let items = containers.selectMany((c) => c.items.cast()); if (options.flatGroupItems || options.includePlaceholderContents) items = items.aggregate(new Collection(), (collection, item) => { if (item instanceof GroupItem) { collection.addRange(item.getNestedItems()); if (!options.excludeGroupItems) { collection.add(item); } } else { collection.add(item); } if (options.includePlaceholderContents && item instanceof PlaceholderItem) { if (item.content != null) collection.add(item.content); item.topFrames.concat(item.bottomFrames).forEach(c => collection.add(c)); } return collection; }); return items; } static getItems(surfaces, options) { if (surfaces instanceof Array) surfaces = Enumerable.from(surfaces); return surfaces.selectMany(s => s.getAllItems(options)).toArray(); } getBackgroundItem() { const bgContainer = this.containers.firstOrDefault(c => c.name === Configuration.BG_CONTAINER_NAME); return bgContainer != null ? bgContainer.items.get(0) : null; } _copy(source, destination, generateNewIds) { super._copy(source, destination, generateNewIds); destination.width = source.width; destination.height = source.height; destination.rotateAngle = source.rotateAngle; destination.mockup = source.mockup.clone(generateNewIds); destination.previewMockups.addRange(source.previewMockups.toArray().map(m => m.clone(generateNewIds))); destination.printAreas.addRange(source.printAreas.toArray().map(p => p.clone(generateNewIds))); destination.containers.addRange(source.containers.toArray().map(c => c.clone(generateNewIds))); destination.watermark = source.watermark != null ? source.watermark.clone() : null; if (source.interactiveZones) { const clonedInteractiveZones = source.interactiveZones.toArray().map(az => az.clone(generateNewIds)); destination.interactiveZones = new Collection(clonedInteractiveZones); } } clone(generateNewIds = false) { const surface = new Surface(); this._copy(this, surface, generateNewIds); return surface; } generateNewIds() { this._generateNewId(); this._mockup.generateNewIds(); this._previewMockups.forEach(i => i.generateNewIds()); this._printAreas.forEach(i => i.generateNewIds()); this._containers.forEach(i => i.generateNewIds()); this._interactiveZones.forEach(i => i.generateNewIds()); } static getAllItems(surfaces, options = { ignoreMockups: false }) { if (surfaces instanceof Array) surfaces = Enumerable.from(surfaces); return surfaces.selectMany(s => s.getAllItems(options)); } get nonChannelContainerItems() { return this._containers.toArray().filter(c => c.renderingType === RenderingType.Normal).map(c => c.items.toArray()).flat(); } _subscribeInteractiveZonesEvents() { var _a; (_a = this._interactiveZones) === null || _a === void 0 ? void 0 : _a.add_collectionChanged(this._onInteractiveZoneCollectionChanged); } _unsubscribeInteractiveZonesEvents() { var _a; (_a = this._interactiveZones) === null || _a === void 0 ? void 0 : _a.remove_collectionChanged(this._onInteractiveZoneCollectionChanged); } } //# sourceMappingURL=Surface.js.map