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.

55 lines 2.66 kB
import { ArgumentException } from "@aurigma/design-atoms-model/Exception"; import { ProductThemeApplier } from "./ProductThemeApplier"; import { ItemUtils, UpdatePhase } from "../../Utils/ItemUtils"; import { Item, ClipartItem } from "@aurigma/design-atoms-model/Product/Items"; import { SelectionCommand } from "@aurigma/design-atoms-interfaces"; export class ProductThemeManager { constructor(_productHandler, productThemes, _canvas, _eventManager) { this._productHandler = _productHandler; this._canvas = _canvas; this._eventManager = _eventManager; this.productThemes = productThemes; this._productThemeApplier = new ProductThemeApplier(this._canvas.viewer.colorPreviewService, this._canvas.viewer.colorParser); } applyProductTheme(theme, product, commandManager) { let themeObj = null; if (typeof theme === "string") { themeObj = this.productThemes[theme]; } else if (theme instanceof ProductThemeConfig) { themeObj = theme; } else if (theme instanceof Object) { themeObj = new ProductThemeConfig(theme); } else if (theme == null) throw new ArgumentException("Theme must be not null"); if (themeObj == null) throw new ArgumentException("Theme not found"); const items = product.getAllItems({ ignoreMockups: false, flatGroupItems: true }).filter((item) => !(item.parentGroupItem instanceof ClipartItem)); const updatedItems = []; this.currentProductTheme = themeObj; commandManager.execute(SelectionCommand.clearSelection); items.forEach(item => { if (item instanceof Item && this.applyToItem(item, themeObj)) { updatedItems.push(item); } }); if (updatedItems.length > 0) { ItemUtils.updateItems(updatedItems, this._productHandler, this._canvas, { addToHistory: true, redraw: true, updatePhase: UpdatePhase.Final }); } if (updatedItems.length === 0) console.warn("No colors were applied from theme. Please check that theme matches to the product."); this._eventManager.productThemeChangedEvent.notify(this.currentProductTheme); return updatedItems.length > 0; } async applyToItem(item, theme, themeBinding) { return await this._productThemeApplier.applyToItem(item, theme, themeBinding); } } export class ProductThemeConfig { constructor(rawObject) { Object.assign(this, rawObject); } } //# sourceMappingURL=ProductThemeManager.js.map