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.

90 lines 3.56 kB
import { Collection } from "@aurigma/design-atoms-model/Collection"; import { EventWithSenderArg } from "@aurigma/design-atoms-model/EventObject"; import { ProductParser } from "../Serialization/ProductParser"; export class WatermarkHandler { constructor(watermarkConfig, service) { this._productParser = new ProductParser(); this._watermarkCollectionChangedEvent = new EventWithSenderArg(); this._watermarkContainers = new Collection(); this._onSurfacePropertyChanged = (sender, propertyName) => { switch (propertyName) { case "width": case "height": this._updateWatermark(); break; } }; this._watermarkConfig = watermarkConfig; this._service = service; this._clearWatermarkContainer(); } get surface() { return this._surface; } set surface(value) { if (this._surface === value) return; if (this._surface != null) this.surface.removePropertyChanged(this._onSurfacePropertyChanged); this._surface = value; this._watermarkConfig = (this._surface != null && this._surface.parentProduct != null) ? this._surface.parentProduct.watermarkConfig : null; if (this._surface != null) this.surface.addPropertyChanged(this._onSurfacePropertyChanged); this._updateWatermark(); } get watermarkConfig() { return this._watermarkConfig; } set watermarkConfig(value) { if (this._watermarkConfig === value) return; this._watermarkConfig = value; this._updateWatermark(); } get watermarkContainers() { if (this._watermarkContainers == null) this._watermarkContainers = new Collection(); return this._watermarkContainers; } _setWatermarkContainers(value) { this._watermarkContainers = value; this._watermarkCollectionChangedEvent.notify(this); } add_watermarkCollectionChanged(h) { this._watermarkCollectionChangedEvent.add(h); } remove_watermarkCollectionChanged(h) { this._watermarkCollectionChangedEvent.remove(h); } _clearWatermarkContainer() { this._watermarkContainers = null; } _updateWatermark() { this._clearWatermarkContainer(); if (this.surface == null || this.surface.width === 0 || this.surface.height === 0) return; if (this.watermarkConfig == null || this.watermarkConfig.visibility == null || this.watermarkConfig.visibility.canvas === false) return; this._requestWatermarkItems(); } _requestWatermarkItems() { const id = Math.random(); this._requestWatermarkItemsId = id; this._service.getWatermarkItemsAsync(this.surface.width, this.surface.height, this._watermarkConfig) .then(container => { try { if (id !== this._requestWatermarkItemsId) return; var containers = new Collection([this._productParser.parseContainer(container)]); this._setWatermarkContainers(containers); } catch (e) { throw e; } }) .catch(error => { console.error("Error while updating watermark items.", error); }); } } //# sourceMappingURL=WatermarkHandler.js.map