@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.
361 lines • 16.3 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 __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 { ModelComponent } from "./ModelComponent";
import { RenderingType } from "./";
import { Configuration } from "../Configuration";
import * as _ from "underscore";
import { GroupItem, PlaceholderItem } from "./Items";
import { SurfaceMockup } from "./SurfaceMockup";
import { ArgumentException } from "../Exception";
import { Collection } from "../Collection";
import { SurfaceContainer } from "./Container";
import { EventWithSenderArg } from "../EventObject";
import { normalizeAngle } from "../Math";
import * as Enumerable from "linq/linq";
var Surface = /** @class */ (function (_super) {
__extends(Surface, _super);
function Surface(width, height, mockup, printAreas, id, name, initDefaultContainers) {
if (printAreas === void 0) { printAreas = []; }
var _this = _super.call(this, id, name) || this;
_this._rotateAngle = 0;
_this._mockup = new SurfaceMockup();
_this._printAreas = new Collection();
_this._previewMockups = new Collection();
_this._interactiveZones = new Collection();
_this._printAreasCollectionChangedEvent = new EventWithSenderArg();
_this._printAreaAddedEvent = new EventWithSenderArg();
_this._printAreaRemovedEvent = new EventWithSenderArg();
_this._activityZoneChangedEvent = new EventWithSenderArg();
_this._onContainersItemAdded = function (data) {
data.item.parentComponent = _this;
};
_this._onContainersItemRemoved = function (data) {
data.item.parentComponent = null;
};
_this.watermark = null;
_this._containers = new Collection();
_this._onPrintAreaAdded = function (_a) {
var printArea = _a.item;
printArea.parentSurface = _this;
_this._printAreaAddedEvent.notify(_this, printArea);
};
_this._onPrintAreaRemoved = function (_a) {
var printArea = _a.item;
printArea.parentSurface = null;
_this._printAreaRemovedEvent.notify(_this, printArea);
};
_this._onInteractiveZoneCollectionChanged = function (args) {
_this._propertyChanged.notify(_this, "interactiveZones");
};
_this.width = width != null ? width : 100;
_this.height = height != null ? height : 100;
_this.parentProduct = null;
_this.mockup = mockup != null ? mockup : new SurfaceMockup();
_this.printAreas.add_itemAdded(_this._onPrintAreaAdded);
_this.printAreas.add_itemRemoved(_this._onPrintAreaRemoved);
_this.printAreas.setRange(printAreas);
_this._containers.add_itemAdded(_this._onContainersItemAdded);
_this._containers.add_itemRemoved(_this._onContainersItemRemoved);
_this._subscribeInteractiveZonesEvents();
if (initDefaultContainers) {
var bgContainer = new SurfaceContainer();
bgContainer.name = Configuration.BG_CONTAINER_NAME;
_this.containers.push(bgContainer);
var mainContainer = new SurfaceContainer();
mainContainer.name = Configuration.MAIN_CONTAINER_NAME;
_this.containers.push(mainContainer);
var fgContainer = new SurfaceContainer();
fgContainer.name = Configuration.FG_CONTAINER_NAME;
_this.containers.push(fgContainer);
}
return _this;
}
Object.defineProperty(Surface.prototype, "rotateAngle", {
get: function () {
return this._rotateAngle;
},
set: function (value) {
var normalizedValue = normalizeAngle(value);
if (normalizedValue === this._rotateAngle)
return;
this._rotateAngle = normalizedValue;
this._propertyChanged.notify(this, "rotateAngle");
},
enumerable: true,
configurable: true
});
Object.defineProperty(Surface.prototype, "width", {
get: function () {
return this._width;
},
set: function (value) {
if (value === this._width)
return;
this._width = value;
this._propertyChanged.notify(this, "width");
},
enumerable: true,
configurable: true
});
Object.defineProperty(Surface.prototype, "height", {
get: function () {
return this._height;
},
set: function (value) {
if (value === this._height)
return;
this._height = value;
this._propertyChanged.notify(this, "height");
},
enumerable: true,
configurable: true
});
Object.defineProperty(Surface.prototype, "mockup", {
get: function () {
return this._mockup;
},
set: function (value) {
if (value == null)
throw new ArgumentException("mockup can`t be null");
if (value === this._mockup)
return;
this._mockup = value;
if (this._mockup != null)
this._mockup.parentSurface = this;
this._propertyChanged.notify(this, "mockup");
},
enumerable: true,
configurable: true
});
Object.defineProperty(Surface.prototype, "previewMockups", {
get: function () {
return this._previewMockups;
},
set: function (value) {
var e_1, _a;
if (this._previewMockups != null) {
try {
for (var _b = __values(this._previewMockups), _c = _b.next(); !_c.done; _c = _b.next()) {
var it = _c.value;
it.parentSurface = null;
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
}
this._previewMockups = value;
if (this._previewMockups == null)
return;
for (var i = 0; i < this._previewMockups.length; ++i) {
this._previewMockups.get(i).parentSurface = this;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(Surface.prototype, "containers", {
get: function () {
return this._containers;
},
set: function (value) {
if (value == null || value === this._containers)
return;
if (this._containers != null) {
this._containers.clear();
this._containers.remove_itemAdded(this._onContainersItemAdded);
this._containers.remove_itemRemoved(this._onContainersItemRemoved);
}
this._containers = value;
for (var i = 0; i < this._containers.length; i++)
this._onContainersItemAdded({ index: i, item: this._containers.get(i) });
this._containers.add_itemAdded(this._onContainersItemAdded);
this._containers.add_itemRemoved(this._onContainersItemRemoved);
},
enumerable: true,
configurable: true
});
Object.defineProperty(Surface.prototype, "printAreas", {
get: function () {
return this._printAreas;
},
set: function (value) {
var e_2, _a;
if (value == null || value === this._printAreas)
return;
if (this._printAreas != null) {
this._printAreas.clear();
this._printAreas.remove_itemAdded(this._onPrintAreaAdded);
this._printAreas.remove_itemRemoved(this._onPrintAreaRemoved);
}
this._printAreas = value;
try {
for (var _b = __values(this._printAreas), _c = _b.next(); !_c.done; _c = _b.next()) {
var pa = _c.value;
this._onPrintAreaAdded({ item: pa });
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_2) throw e_2.error; }
}
this._printAreas.add_itemAdded(this._onPrintAreaAdded);
this._printAreas.add_itemRemoved(this._onPrintAreaRemoved);
},
enumerable: true,
configurable: true
});
Object.defineProperty(Surface.prototype, "interactiveZones", {
get: function () {
return this._interactiveZones;
},
set: function (value) {
if (value == null || value === this._interactiveZones)
return;
this._unsubscribeInteractiveZonesEvents();
this._interactiveZones = value;
this._subscribeInteractiveZonesEvents();
this._propertyChanged.notify(this, "interactiveZones");
},
enumerable: true,
configurable: true
});
Surface.prototype.add_printAreaAdded = function (h) {
this._printAreaAddedEvent.add(h);
};
Surface.prototype.remove_printAreaAdded = function (h) {
this._printAreaAddedEvent.remove(h);
};
Surface.prototype.add_printAreaRemoved = function (h) {
this._printAreaRemovedEvent.add(h);
};
Surface.prototype.remove_printAreaRemoved = function (h) {
this._printAreaRemovedEvent.remove(h);
};
Surface.prototype.getSimplifiedObject = function () {
var simplified = _super.prototype.getSimplifiedObject.call(this, ["parentProduct", "nonChannelContainerItems"]);
return simplified;
};
Surface.prototype.getAllItems = function (options) {
if (options === void 0) { options = { ignoreMockups: false }; }
var containers = options.ignoreMockups
? Enumerable.from(this.containers)
: Enumerable.empty()
.concat(this.mockup.underContainers.cast(), this.containers.cast(), this.mockup.overContainers.cast());
if (options.containerFilterFunc != null)
containers = containers.where(options.containerFilterFunc);
var items = containers.selectMany(function (c) { return c.items.cast(); });
if (options.flatGroupItems || options.includePlaceholderContents)
items = items.aggregate(new Collection(), function (collection, item) {
if (item instanceof GroupItem) {
collection.addRange(item.getNestedItems());
if (!options.excludeGroupItems) {
collection.add(item);
}
}
else {
collection.add(item);
}
if (options.includePlaceholderContents && item instanceof PlaceholderItem) {
if (item.content != null)
collection.add(item.content);
item.topFrames.concat(item.bottomFrames).forEach(function (c) { return collection.add(c); });
}
return collection;
});
return items;
};
Surface.getItems = function (surfaces, options) {
if (surfaces instanceof Array)
surfaces = Enumerable.from(surfaces);
return surfaces.selectMany(function (s) { return s.getAllItems(options); }).toArray();
};
Surface.prototype.getBackgroundItem = function () {
var bgContainer = this.containers.firstOrDefault(function (c) { return c.name === Configuration.BG_CONTAINER_NAME; });
return bgContainer != null
? bgContainer.items.get(0)
: null;
};
Surface.prototype._copy = function (source, destination, generateNewIds) {
_super.prototype._copy.call(this, source, destination, generateNewIds);
destination.width = source.width;
destination.height = source.height;
destination.rotateAngle = source.rotateAngle;
destination.mockup = source.mockup.clone(generateNewIds);
destination.previewMockups.addRange(source.previewMockups.toArray().map(function (m) { return m.clone(generateNewIds); }));
destination.printAreas.addRange(source.printAreas.toArray().map(function (p) { return p.clone(generateNewIds); }));
destination.containers.addRange(source.containers.toArray().map(function (c) { return c.clone(generateNewIds); }));
destination.watermark = source.watermark != null ? source.watermark.clone() : null;
if (source.interactiveZones) {
var clonedInteractiveZones = source.interactiveZones.toArray().map(function (az) { return az.clone(generateNewIds); });
destination.interactiveZones = new Collection(clonedInteractiveZones);
}
};
Surface.prototype.clone = function (generateNewIds) {
if (generateNewIds === void 0) { generateNewIds = false; }
var surface = new Surface();
this._copy(this, surface, generateNewIds);
return surface;
};
Surface.prototype.generateNewIds = function () {
this._generateNewId();
this._mockup.generateNewIds();
this._previewMockups.forEach(function (i) { return i.generateNewIds(); });
this._printAreas.forEach(function (i) { return i.generateNewIds(); });
this._containers.forEach(function (i) { return i.generateNewIds(); });
this._interactiveZones.forEach(function (i) { return i.generateNewIds(); });
};
Surface.getAllItems = function (surfaces, options) {
if (options === void 0) { options = { ignoreMockups: false }; }
if (surfaces instanceof Array)
surfaces = Enumerable.from(surfaces);
return surfaces.selectMany(function (s) { return s.getAllItems(options); });
};
Object.defineProperty(Surface.prototype, "nonChannelContainerItems", {
get: function () {
return _.flatten(this._containers.toArray().filter(function (c) { return c.renderingType === RenderingType.Normal; }).map(function (c) { return c.items.toArray(); }));
},
enumerable: true,
configurable: true
});
Surface.prototype._subscribeInteractiveZonesEvents = function () {
var _a;
(_a = this._interactiveZones) === null || _a === void 0 ? void 0 : _a.add_collectionChanged(this._onInteractiveZoneCollectionChanged);
};
Surface.prototype._unsubscribeInteractiveZonesEvents = function () {
var _a;
(_a = this._interactiveZones) === null || _a === void 0 ? void 0 : _a.remove_collectionChanged(this._onInteractiveZoneCollectionChanged);
};
return Surface;
}(ModelComponent));
export { Surface };
//# sourceMappingURL=Surface.js.map