@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
338 lines • 18.8 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
import { Item, ImageItem, PlaceholderItem, DashedLineItem, LineItem, BaseTextItem, BarcodeItem, ShapeItem, ClipartItem } from "@aurigma/design-atoms-model/Product/Items";
import { ItemData } from "@aurigma/design-atoms-model/Product/ItemsData";
import { StrokeSettingsData as StrokeSettings } from "@aurigma/design-atoms-model/Product/ItemsData";
import { ShadowSettingsData as ShadowSettings } from "@aurigma/design-atoms-model/Product/ItemsData";
import { ItemDataHandler } from "../../ItemDataHandler";
import { ItemUtils } from "../../Utils/ItemUtils";
import { applyNullableValue } from "@aurigma/design-atoms-model/Utils/Utils";
var ProductThemeApplier = /** @class */ (function () {
function ProductThemeApplier(_colorPreviewService, _colorParser) {
this._colorPreviewService = _colorPreviewService;
this._colorParser = _colorParser;
}
ProductThemeApplier.prototype.apply = function (product, theme, themeBinding) {
return __awaiter(this, void 0, void 0, function () {
var changed, _a, _b, item, e_1_1;
var e_1, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
if (!product) {
return [2 /*return*/, false];
}
changed = false;
_d.label = 1;
case 1:
_d.trys.push([1, 6, 7, 8]);
_a = __values(product.getAllItems()), _b = _a.next();
_d.label = 2;
case 2:
if (!!_b.done) return [3 /*break*/, 5];
item = _b.value;
return [4 /*yield*/, this.applyToItem(item, theme, themeBinding)];
case 3:
changed = (_d.sent()) || changed;
_d.label = 4;
case 4:
_b = _a.next();
return [3 /*break*/, 2];
case 5: return [3 /*break*/, 8];
case 6:
e_1_1 = _d.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 8];
case 7:
try {
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
}
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 8: return [2 /*return*/, changed];
}
});
});
};
ProductThemeApplier.prototype.applyToItem = function (item, theme, themeBinding) {
return __awaiter(this, void 0, void 0, function () {
var targetThemeBinding, itemData, itemDataHandler;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
targetThemeBinding = themeBinding || item.themeBinding;
if (!item || !theme || !targetThemeBinding) {
return [2 /*return*/, false];
}
itemData = this._getItemDataByItemType(item, theme, targetThemeBinding);
itemDataHandler = new ItemDataHandler(itemData, this._colorPreviewService, this._colorParser);
if (item instanceof ImageItem) {
return [2 /*return*/, itemDataHandler.applyImage(item)];
}
if (item instanceof PlaceholderItem) {
return [2 /*return*/, this._applyToPlaceholder(item, theme, targetThemeBinding)];
}
if (item instanceof DashedLineItem) {
return [2 /*return*/, itemDataHandler.applyDashedLine(item)];
}
if (item instanceof LineItem) {
return [2 /*return*/, itemDataHandler.applyLine(item)];
}
if (!(item instanceof BaseTextItem)) return [3 /*break*/, 2];
return [4 /*yield*/, itemDataHandler.applyBaseText(item)];
case 1: return [2 /*return*/, _a.sent()];
case 2:
if (item instanceof BarcodeItem) {
return [2 /*return*/, itemDataHandler.applyBarcode(item)];
}
if (item instanceof ShapeItem) {
return [2 /*return*/, itemDataHandler.applyShape(item)];
}
if (item instanceof ClipartItem) {
return [2 /*return*/, itemDataHandler.applyClipart(item)];
}
return [2 /*return*/];
}
});
});
};
ProductThemeApplier.prototype._getItemDataByItemType = function (item, theme, themeBinding) {
if (item instanceof ImageItem)
return this._getMergedItemData(theme, themeBinding, themeBinding.img);
if (item instanceof LineItem)
return this._getMergedItemData(theme, themeBinding, themeBinding.line);
if (item instanceof BaseTextItem)
return this._getMergedItemData(theme, themeBinding, themeBinding.text);
if (item instanceof BarcodeItem)
return this._getMergedItemData(theme, themeBinding, themeBinding.barcode);
return this._getMergedItemData(theme, themeBinding);
};
ProductThemeApplier.prototype._applyToPlaceholder = function (placeholder, theme, themeBinding) {
return __awaiter(this, void 0, void 0, function () {
var changed, itemData, _a, _b, frame, e_2_1, _c, _d, frame, e_3_1;
var e_2, _e, e_3, _f;
return __generator(this, function (_g) {
switch (_g.label) {
case 0:
changed = false;
itemData = this._getMergedItemData(theme, themeBinding);
if (itemData.fillColor != null && ItemUtils.isBgContainerItem(placeholder)) {
ItemUtils.clearImageContent(placeholder);
}
if (!(placeholder.content instanceof Item)) return [3 /*break*/, 2];
return [4 /*yield*/, this.applyToItem(placeholder.content, theme, themeBinding)];
case 1:
changed = _g.sent();
_g.label = 2;
case 2:
if (!(placeholder.topFrames != null)) return [3 /*break*/, 10];
_g.label = 3;
case 3:
_g.trys.push([3, 8, 9, 10]);
_a = __values(placeholder.topFrames), _b = _a.next();
_g.label = 4;
case 4:
if (!!_b.done) return [3 /*break*/, 7];
frame = _b.value;
return [4 /*yield*/, this.applyToItem(frame, theme)];
case 5:
changed = (_g.sent()) || changed;
_g.label = 6;
case 6:
_b = _a.next();
return [3 /*break*/, 4];
case 7: return [3 /*break*/, 10];
case 8:
e_2_1 = _g.sent();
e_2 = { error: e_2_1 };
return [3 /*break*/, 10];
case 9:
try {
if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
}
finally { if (e_2) throw e_2.error; }
return [7 /*endfinally*/];
case 10:
if (!(placeholder.bottomFrames != null)) return [3 /*break*/, 18];
_g.label = 11;
case 11:
_g.trys.push([11, 16, 17, 18]);
_c = __values(placeholder.bottomFrames), _d = _c.next();
_g.label = 12;
case 12:
if (!!_d.done) return [3 /*break*/, 15];
frame = _d.value;
return [4 /*yield*/, this.applyToItem(frame, theme)];
case 13:
changed = (_g.sent()) || changed;
_g.label = 14;
case 14:
_d = _c.next();
return [3 /*break*/, 12];
case 15: return [3 /*break*/, 18];
case 16:
e_3_1 = _g.sent();
e_3 = { error: e_3_1 };
return [3 /*break*/, 18];
case 17:
try {
if (_d && !_d.done && (_f = _c.return)) _f.call(_c);
}
finally { if (e_3) throw e_3.error; }
return [7 /*endfinally*/];
case 18: return [2 /*return*/, new ItemDataHandler(itemData, this._colorPreviewService, this._colorParser).applyPlaceholder(placeholder) || changed];
}
});
});
};
ProductThemeApplier.prototype._getMergedItemData = function (theme, themeBinding, colorBindingName) {
var e_4, _a;
var _this = this;
if (colorBindingName === void 0) { colorBindingName = null; }
var mergedItemData = new ItemData();
if (themeBinding.styles) {
var stylesItemData = themeBinding.styles.map(function (style) { return _this._getThemeItemData(style, theme); });
try {
for (var stylesItemData_1 = __values(stylesItemData), stylesItemData_1_1 = stylesItemData_1.next(); !stylesItemData_1_1.done; stylesItemData_1_1 = stylesItemData_1.next()) {
var styleItemData = stylesItemData_1_1.value;
if (styleItemData) {
mergedItemData = this._applyStyle(mergedItemData, styleItemData);
}
}
}
catch (e_4_1) { e_4 = { error: e_4_1 }; }
finally {
try {
if (stylesItemData_1_1 && !stylesItemData_1_1.done && (_a = stylesItemData_1.return)) _a.call(stylesItemData_1);
}
finally { if (e_4) throw e_4.error; }
}
}
return this._applyTHBMColor(theme, themeBinding, colorBindingName, mergedItemData);
};
ProductThemeApplier.prototype._applyStyle = function (itemData, style) {
itemData.color = applyNullableValue(itemData.color, style.color);
itemData.fillColor = applyNullableValue(itemData.fillColor, style.fillColor);
itemData.borderColor = applyNullableValue(itemData.borderColor, style.borderColor);
itemData.borderWidth = applyNullableValue(itemData.borderWidth, style.borderWidth);
itemData.altColor = applyNullableValue(itemData.altColor, style.altColor);
itemData.leading = applyNullableValue(itemData.leading, style.leading);
itemData.tracking = applyNullableValue(itemData.tracking, style.tracking);
itemData.underline = applyNullableValue(itemData.underline, style.underline);
itemData.horizontalLineColor = applyNullableValue(itemData.horizontalLineColor, style.horizontalLineColor);
itemData.verticalLineColor = applyNullableValue(itemData.verticalLineColor, style.verticalLineColor);
itemData.barcodeColor = applyNullableValue(itemData.barcodeColor, style.barcodeColor);
itemData.opacity = applyNullableValue(itemData.opacity, style.opacity);
itemData.font = this._applySettings(itemData.font, style.font);
itemData.visible = applyNullableValue(itemData.visible, style.visible);
return itemData;
};
ProductThemeApplier.prototype._applyTHBMColor = function (theme, themeBinding, colorBindingName, itemData) {
var _this = this;
itemData.fillColor = applyNullableValue(itemData.fillColor, this._getTHMBColor(themeBinding.fill, theme));
itemData.textFillColor = applyNullableValue(itemData.textFillColor, this._getTHMBColor(themeBinding.textFill, theme));
itemData.borderColor = applyNullableValue(itemData.borderColor, this._getTHMBColor(themeBinding.border, theme));
itemData.color = applyNullableValue(itemData.color, this._getTHMBColor(colorBindingName, theme));
itemData.altColor = applyNullableValue(itemData.altColor, this._getTHMBColor(themeBinding.altline, theme));
itemData.horizontalLineColor = applyNullableValue(itemData.horizontalLineColor, this._getTHMBColor(themeBinding.horizontal, theme));
itemData.verticalLineColor = applyNullableValue(itemData.verticalLineColor, this._getTHMBColor(themeBinding.vertical, theme));
itemData.barcodeColor = applyNullableValue(itemData.barcodeColor, this._getTHMBColor(themeBinding.barcode, theme));
if (themeBinding.shadow != null) {
if (itemData.shadow == null)
itemData.shadow = new ShadowSettings();
var color = this._colorParser.parse(this._getTHMBColor(themeBinding.shadow, theme));
itemData.shadow.color = applyNullableValue(itemData.shadow.color, color.getData());
}
if (themeBinding.stroke != null) {
if (itemData.stroke == null)
itemData.stroke = new StrokeSettings();
var color = this._colorParser.parse(this._getTHMBColor(themeBinding.stroke, theme));
itemData.stroke.color = applyNullableValue(itemData.stroke.color, color.getData());
}
itemData.clipartColors = themeBinding.clipartColors != null
? themeBinding.clipartColors.map(function (colorGroupTheme) { return _this._getTHMBColor(colorGroupTheme, theme); })
: [];
return itemData;
};
ProductThemeApplier.prototype._applySettings = function (applied, notApplied) {
var e_5, _a;
if (notApplied == null)
return applied;
if (applied == null)
return notApplied;
try {
for (var _b = __values(Object.getOwnPropertyNames(notApplied)), _c = _b.next(); !_c.done; _c = _b.next()) {
var property = _c.value;
var value = notApplied[property];
if (value != null)
applied[property] = value;
}
}
catch (e_5_1) { e_5 = { error: e_5_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_5) throw e_5.error; }
}
return applied;
};
ProductThemeApplier.prototype._getThemeItemData = function (key, theme) {
if (!key)
return null;
return theme[key];
};
ProductThemeApplier.prototype._getTHMBColor = function (colorKey, theme) {
if (!colorKey)
return null;
return theme[colorKey];
};
return ProductThemeApplier;
}());
export { ProductThemeApplier };
//# sourceMappingURL=ProductThemeApplier.js.map