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.

161 lines 9.12 kB
import { CollectionChangeType } from "@aurigma/design-atoms-model/ICollectionChangeEventArgs"; import { Violation, ViolationState } from "./Violations/Violation"; import { EventObject } from "@aurigma/design-atoms-model/EventObject"; import { Collection } from "@aurigma/design-atoms-model/Collection"; import { FrontEndLogger, LogSource } from "../FrontEndLogger"; import { arrayEquals, isEmpty } from "@aurigma/design-atoms-model/Utils/Utils"; import { SurfaceContainer } from "@aurigma/design-atoms-model/Product"; import { throttle } from "@aurigma/design-atoms-model"; export class ViolationService { constructor(_violations, _eventManager, _productHandler) { this._violations = _violations; this._eventManager = _eventManager; this._productHandler = _productHandler; this._globalCheck = () => { FrontEndLogger.debugLog(`<span style="color: white">Start global check</span>`, LogSource.ViolationService); for (let i = this._itemsToCheck.length - 1; i >= 0; i--) this._checkItem(this._itemsToCheck.get(i)); FrontEndLogger.debugLog(`<span style="color: white">End global check</span>`, LogSource.ViolationService); }; this._globalCheckPeriod = 700; this._globalCheckDebounce = throttle(this._globalCheck, this._globalCheckPeriod, { leading: false, trailing: true }); this._itemsToCheck = new Collection(); this._violationInfos = new Map(); this._isStarted = false; this.getItemInfo = (item, forceUpdate = false) => { FrontEndLogger.debugLog(`Get item info for <span style="color: rgb(0,255,255)">${item.name.substring(0, 20)}...</span> ${forceUpdate ? " with force" : ""}`, LogSource.ViolationService); if (forceUpdate || !this._violationInfos.has(item.id)) this._checkItem(item); return this._violationInfos.get(item.id); }; this._checkItem = (item) => { const oldInfo = this._violationInfos.get(item.id); const newInfo = Violation.computeTotalViolationInfo(this._violations, item, this._violationInfos.get(item.id)); this._violationInfos.set(item.id, newInfo); if ((oldInfo === null || oldInfo === void 0 ? void 0 : oldInfo.state) !== newInfo.state || !arrayEquals(oldInfo === null || oldInfo === void 0 ? void 0 : oldInfo.messages, newInfo.messages)) { if ((oldInfo === null || oldInfo === void 0 ? void 0 : oldInfo.state) !== newInfo.state) { this._logStateChanged(item, newInfo.state); } else { FrontEndLogger.debugLog(`Item <span style="color: rgb(0,255,255)">${item.name.substring(0, 20)}...</span> changed message`, LogSource.ViolationService); } this._itemViolationStateChangedEvent.notify({ new: Object.assign(Object.assign({}, newInfo), { item: item }), old: oldInfo != null ? Object.assign(Object.assign({}, oldInfo), { item: item }) : null }); } if (newInfo.state === ViolationState.None) { FrontEndLogger.debugLog(`Item <span style="color: rgb(0,255,255)">${item.name.substring(0, 20)}...</span> has undefined state`, LogSource.ViolationService); } if (this._itemsToCheck.contains(item)) this._itemsToCheck.remove(item); }; this._scheduleSurfaceItemsCheck = () => { var _a; FrontEndLogger.debugLog("Schedule all items", LogSource.ViolationService); this._itemsToCheck.clear(); this._violationInfos.clear(); (_a = this._productHandler.currentSurface) === null || _a === void 0 ? void 0 : _a.getAllItems({ ignoreMockups: true, excludeGroupItems: false, flatGroupItems: true }).forEach(this._scheduleItemCheck); }; this._scheduleItemCheck = (item) => { if (this._itemsToCheck.contains(item) || item.locked || !(item.parentContainer instanceof SurfaceContainer)) return; FrontEndLogger.debugLog(`Schedule item <span style="color: rgb(0,255,255)">${item.name.substring(0, 20)}...</span>`, LogSource.ViolationService); this._violations.forEach(v => v.prepareItem != null ? v.prepareItem(item) : null); if (!this._itemsToCheck.contains(item)) this._itemsToCheck.push(item); }; this._onItemRemoved = (args) => { this._removeViolationItemData(args.item); }; this._removeViolationItemData = (item) => { if (this._violationInfos.has(item.id)) this._violationInfos.delete(item.id); if (this._itemsToCheck.contains(item)) this._itemsToCheck.remove(item); }; this._onItemAdded = (args) => { this._scheduleItemCheck(args.item); }; this._onImageContentChanged = (placeholder) => { if (placeholder == null) return; this._scheduleItemCheck(placeholder); }; this._onContainerCollectionChanged = (args) => { if (args.type === CollectionChangeType.Move) return; const isRemove = args.type === CollectionChangeType.Remove; const removeOrAddArgs = isRemove ? args : args; if (removeOrAddArgs.item instanceof SurfaceContainer) removeOrAddArgs.item.items.forEach(isRemove ? this._removeViolationItemData : this._scheduleItemCheck); }; this._itemViolationStateChangedEvent = new EventObject(); this.dispose = () => { const em = this._eventManager; em.removeSurfaceChanged(this._scheduleSurfaceItemsCheck); em.removePrintAreaCollectionChanged(this._scheduleSurfaceItemsCheck); em.removePrintAreaPropertyChanged(this._scheduleSurfaceItemsCheck); em.removeItemAdded(this._onItemAdded); em.removeItemRemoved(this._onItemRemoved); em.removeItemPropertyChanged(this._scheduleItemCheck); em.removeItemChanging(this._scheduleItemCheck); em.removeItemChanged(this._scheduleItemCheck); this._itemsToCheck.remove_collectionChanged(this._globalCheckDebounce); }; this._logStateChanged = (item, state) => { let stateColor = ""; switch (state) { default: stateColor = "black"; break; case ViolationState.Good: stateColor = "green"; break; case ViolationState.Warning: stateColor = "yellow"; break; case ViolationState.Bad: stateColor = "red"; break; } const logMessage = `item with name <span style="color: rgb(0,255,255)">"${item.name.substring(0, 20)}..."</span> change violation on <span style="color: ${stateColor}">${ViolationState[state].toString()}</span>.`; FrontEndLogger.debugLog(logMessage, LogSource.ViolationService); }; if (this._violations.length === 0) return; this._init(); } _init() { const em = this._eventManager; em.addSurfaceChanged(this._scheduleSurfaceItemsCheck); em.addPrintAreaPropertyChanged(this._scheduleSurfaceItemsCheck); em.addPrintAreaCollectionChanged(this._scheduleSurfaceItemsCheck); em.addReady(this._scheduleSurfaceItemsCheck); em.addTextWhizzReady(this._scheduleSurfaceItemsCheck); em.addItemAdded(this._scheduleSurfaceItemsCheck); em.addItemRemoved(this._scheduleSurfaceItemsCheck); em.addItemPropertyChanged(this._scheduleSurfaceItemsCheck); em.addItemChanging(this._scheduleSurfaceItemsCheck); em.addItemChanged(this._scheduleSurfaceItemsCheck); em.addImageContentChanged(this._onImageContentChanged); em.addContainerCollectionChanged(this._onContainerCollectionChanged); this._itemsToCheck.add_itemAdded(this._globalCheckDebounce); this._scheduleSurfaceItemsCheck(); this._isStarted = true; } extend(violations) { if (isEmpty(violations)) return; FrontEndLogger.debugLog(`Extend violations with ${violations.map(v => v.constructor.prototype.name).join(", ")}`, LogSource.ViolationService); this._violations.push(...violations); if (!this._isStarted) this._init(); this._globalCheckDebounce(); } addItemViolationStateChanged(listener) { this._itemViolationStateChangedEvent.add(listener); } ; removeItemViolationStateChanged(listener) { this._itemViolationStateChangedEvent.remove(listener); } ; } //# sourceMappingURL=ViolationService.js.map