@aurigma/design-atoms-model
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
253 lines • 11.9 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
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 { GroupItem } from "./GroupItem";
import { Color } from "../../Colors";
import { ShapeItem } from "./ShapeItem";
var ClipartItem = /** @class */ (function (_super) {
__extends(ClipartItem, _super);
function ClipartItem(items, colorGroups) {
if (colorGroups === void 0) { colorGroups = null; }
var _this = _super.call(this, items) || this;
_this.type = ClipartItem.type;
_this._colorGroups = [];
_this._onItemAdded = function (data) {
var newColorGroups = ClipartItem._generateColorGroups([data.item]);
newColorGroups.forEach(function (newColorGroup) {
var _a;
newColorGroup.targets = newColorGroup.targets.filter(function (newTarget) { return !_this.colorGroups.some(function (_a) {
var targets = _a.targets;
return targets.some(function (_a) {
var itemId = _a.itemId, prop = _a.prop;
return itemId === newTarget.itemId && prop === newTarget.prop;
});
}); });
if (newColorGroup.targets.length < 1)
return;
var existingColorGroup = _this.colorGroups.find(function (_a) {
var color = _a.color;
return color.equals(newColorGroup.color);
});
if (existingColorGroup != null)
(_a = existingColorGroup.targets).push.apply(_a, __spread(newColorGroup.targets));
else {
if (_this.themeBinding.clipartColors == null)
_this.themeBinding.clipartColors = new Array(_this.colorGroups.length).fill(null);
_this.colorGroups.push(newColorGroup);
_this.themeBinding.clipartColors.push(null);
}
});
};
_this._onItemRemoved = function (data) {
var id = data.item.id;
_this.colorGroups.forEach(function (colorGroup) {
if (!colorGroup.targets.some(function (_a) {
var itemId = _a.itemId;
return itemId === id;
}))
return;
colorGroup.targets = colorGroup.targets.filter(function (_a) {
var itemId = _a.itemId;
return itemId !== id;
});
});
if (_this.colorGroups.some(function (colorGroup) { return colorGroup.targets.length === 0; })) {
var emptyColorGroupsIndexes = _this.colorGroups
.filter(function (colorGroup) { return colorGroup.targets.length === 0; })
.map(function (emptyColorGroup) { return _this.colorGroups.indexOf(emptyColorGroup); });
emptyColorGroupsIndexes.forEach(function (emptyColorGroupIndex) {
_this.colorGroups.splice(emptyColorGroupIndex, 1);
if (_this.themeBinding.clipartColors != null)
_this.themeBinding.clipartColors.splice(emptyColorGroupIndex, 1);
});
}
};
_this._ignorePermissionsChange = true;
_this.groupItemPermissions.allowSelectNestedItems = false;
_this.groupItemPermissions.allowUngroup = false;
_this._ignorePermissionsChange = false;
_this.colorGroups = colorGroups != null
? colorGroups
: ClipartItem._generateColorGroups(_this.items.toArray());
_this.items.add_itemAdded(_this._onItemAdded);
_this.items.add_itemRemoved(_this._onItemRemoved);
return _this;
}
ClipartItem.prototype.getSimplifiedObject = function (omitProperties) {
if (omitProperties === void 0) { omitProperties = []; }
var simplified = _super.prototype.getSimplifiedObject.call(this, omitProperties);
return simplified;
};
ClipartItem.prototype.clone = function (generateNewIds, appropriateParentContainer) {
if (generateNewIds === void 0) { generateNewIds = false; }
if (appropriateParentContainer === void 0) { appropriateParentContainer = false; }
var item = new ClipartItem();
this._copy(this, item, generateNewIds, appropriateParentContainer);
return item;
};
ClipartItem.prototype.applyPermissionsConstrain = function () {
_super.prototype.applyPermissionsConstrain.call(this);
if (this.groupItemPermissions == null)
return;
this.groupItemPermissions.allowSelectNestedItemsContraint = false;
this.groupItemPermissions.allowUngroupContraint = false;
};
Object.defineProperty(ClipartItem.prototype, "colorGroups", {
get: function () {
return this._colorGroups;
},
set: function (value) {
this._colorGroups = value;
},
enumerable: true,
configurable: true
});
ClipartItem.prototype.setColor = function (colorGroup, targetColor) {
var _this = this;
if (!this.colorGroups.includes(colorGroup))
throw Error("Unexpected colorgroup");
var newColor = (targetColor instanceof Color ? targetColor : targetColor.color);
if (colorGroup.color.equals(newColor))
return;
if (!(targetColor instanceof Color)) {
if (this.themeBinding.clipartColors == null)
this.themeBinding.clipartColors = new Array(this.colorGroups.length).fill(null);
this.themeBinding.clipartColors[this.colorGroups.indexOf(colorGroup)] = targetColor.title;
}
colorGroup.targets.forEach(function (_a) {
var prop = _a.prop, itemId = _a.itemId;
var targetItem = _this.items.first(function (_a) {
var id = _a.id;
return id === itemId;
});
if (targetItem instanceof ShapeItem)
_this._setShapeItemColor(prop, targetItem, newColor.clone());
else
throw new Error("Unexpected item in colorgroup");
});
colorGroup.color = newColor.clone();
this._propertyChanged.notify(this, "colorGroups");
};
ClipartItem.prototype._copy = function (source, destination, generateNewIds, appropriateParentContainer) {
_super.prototype._copy.call(this, source, destination, generateNewIds, appropriateParentContainer);
var colorGroups = source.colorGroups.map(function (colorGroup) { return colorGroup.clone(); });
if (generateNewIds) {
var newItemIds = destination.items.toArray().map(function (item) { return item.id; });
var oldItemIds_1 = source.items.toArray().map(function (item) { return item.id; });
var allTargets_1 = colorGroups.reduce(function (allTargets, colorGroup) { return (__spread(allTargets, colorGroup.targets)); }, []);
newItemIds.forEach(function (newItemId, i) { return allTargets_1.filter(function (_a) {
var itemId = _a.itemId;
return itemId === oldItemIds_1[i];
}).forEach(function (target) { return target.itemId = newItemId; }); });
}
destination.colorGroups = colorGroups;
};
ClipartItem._generateColorGroups = function (items) {
var shapeItems = items.filter(function (item) { return item instanceof ShapeItem; });
var colorGroups = shapeItems.reduce(function (colorGroups, shapeItem) {
var fillColorGroup = colorGroups.find(function (_a) {
var color = _a.color;
return color.equals(shapeItem.fillColor);
});
if (fillColorGroup == null) {
fillColorGroup = new ColorGroup(shapeItem.fillColor);
colorGroups.push(fillColorGroup);
}
fillColorGroup.targets.push({ itemId: shapeItem.id, prop: ItemColorProperty.FillColor });
if (shapeItem.borderWidth > 0) {
var borderColorGroup = colorGroups.find(function (_a) {
var color = _a.color;
return color.equals(shapeItem.borderColor);
});
if (borderColorGroup == null) {
borderColorGroup = new ColorGroup(shapeItem.borderColor);
colorGroups.push(borderColorGroup);
}
borderColorGroup.targets.push({ itemId: shapeItem.id, prop: ItemColorProperty.BorderColor });
}
return colorGroups;
}, []);
return colorGroups;
};
ClipartItem.prototype._setShapeItemColor = function (prop, item, color) {
switch (prop) {
case ItemColorProperty.BorderColor:
item.borderColor = color;
break;
case ItemColorProperty.FillColor:
item.fillColor = color;
break;
default:
break;
}
};
ClipartItem.type = "ClipartItem";
return ClipartItem;
}(GroupItem));
export { ClipartItem };
;
var ColorGroup = /** @class */ (function () {
function ColorGroup(color, targets) {
if (targets === void 0) { targets = []; }
this.color = color;
this.targets = targets;
}
ColorGroup.prototype.clone = function () {
return new ColorGroup(this.color.clone(), __spread((this.targets.map(function (target) { return (__assign({}, target)); }))));
};
return ColorGroup;
}());
export { ColorGroup };
var ColorGroupTarget = /** @class */ (function () {
function ColorGroupTarget() {
}
return ColorGroupTarget;
}());
export { ColorGroupTarget };
export var ItemColorProperty;
(function (ItemColorProperty) {
ItemColorProperty[ItemColorProperty["BorderColor"] = 0] = "BorderColor";
ItemColorProperty[ItemColorProperty["FillColor"] = 1] = "FillColor";
})(ItemColorProperty || (ItemColorProperty = {}));
//# sourceMappingURL=ClipartItem.js.map