@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
197 lines • 10.7 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spread = (this && this.__spread) || function () {
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
return ar;
};
import { CollectionChangeType } from "@aurigma/design-atoms-model/ICollectionChangeEventArgs";
import * as _ from "underscore";
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 } from "@aurigma/design-atoms-model/Utils/Utils";
import { SurfaceContainer } from "@aurigma/design-atoms-model/Product";
var ViolationService = /** @class */ (function () {
function ViolationService(_violations, _eventManager, _productHandler) {
var _this = this;
this._violations = _violations;
this._eventManager = _eventManager;
this._productHandler = _productHandler;
this._globalCheck = function () {
FrontEndLogger.debugLog("<span style=\"color: white\">Start global check</span>", LogSource.ViolationService);
for (var 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 = function (item, forceUpdate) {
if (forceUpdate === void 0) { 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 = function (item) {
var oldInfo = _this._violationInfos.get(item.id);
var 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: __assign(__assign({}, newInfo), { item: item }),
old: oldInfo != null ? __assign(__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 = function () {
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 = function (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(function (v) { return v.prepareItem != null ? v.prepareItem(item) : null; });
if (!_this._itemsToCheck.contains(item))
_this._itemsToCheck.push(item);
};
this._onItemRemoved = function (args) { _this._removeViolationItemData(args.item); };
this._removeViolationItemData = function (item) {
if (_this._violationInfos.has(item.id))
_this._violationInfos.delete(item.id);
if (_this._itemsToCheck.contains(item))
_this._itemsToCheck.remove(item);
};
this._onItemAdded = function (args) { _this._scheduleItemCheck(args.item); };
this._onImageContentChanged = function (placeholder) {
if (placeholder == null)
return;
_this._scheduleItemCheck(placeholder);
};
this._onContainerCollectionChanged = function (args) {
if (args.type === CollectionChangeType.Move)
return;
var isRemove = args.type === CollectionChangeType.Remove;
var removeOrAddArgs = isRemove ? args : args;
if (removeOrAddArgs.item instanceof SurfaceContainer)
removeOrAddArgs.item.items.forEach(isRemove ? _this._removeViolationItemData : _this._scheduleItemCheck);
};
this._itemViolationStateChangedEvent = new EventObject();
this.dispose = function () {
var 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 = function (item, state) {
var 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;
}
var 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();
}
ViolationService.prototype._init = function () {
var 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._onItemAdded);
em.addItemRemoved(this._onItemRemoved);
em.addItemPropertyChanged(this._scheduleItemCheck);
em.addItemChanging(this._scheduleItemCheck);
em.addItemChanged(this._scheduleItemCheck);
em.addImageContentChanged(this._onImageContentChanged);
em.addContainerCollectionChanged(this._onContainerCollectionChanged);
this._itemsToCheck.add_itemAdded(this._globalCheckDebounce);
this._scheduleSurfaceItemsCheck();
this._isStarted = true;
};
ViolationService.prototype.extend = function (violations) {
var _a;
if (_.isEmpty(violations))
return;
FrontEndLogger.debugLog("Extend violations with " + violations.map(function (v) { return v.constructor.prototype.name; }).join(", "), LogSource.ViolationService);
(_a = this._violations).push.apply(_a, __spread(violations));
if (!this._isStarted)
this._init();
this._globalCheckDebounce();
};
ViolationService.prototype.addItemViolationStateChanged = function (listener) { this._itemViolationStateChangedEvent.add(listener); };
;
ViolationService.prototype.removeItemViolationStateChanged = function (listener) { this._itemViolationStateChangedEvent.remove(listener); };
;
return ViolationService;
}());
export { ViolationService };
//# sourceMappingURL=ViolationService.js.map