@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.
205 lines • 8.21 kB
JavaScript
import { ModelComponent } from "./ModelComponent";
import { RectangleF } from "../Math/RectangleF";
import { Collection } from "../Collection";
import { Margin } from "../Math/Margin";
import { PrintAreaBoundsType } from "./PrintAreaBoundsType";
import { ArgumentException } from "../Exception";
import { equals } from "../Utils/Utils";
import { Path } from "../Math/Path";
export class PrintArea extends ModelComponent {
constructor(bounds, bleed, slug, shape) {
super();
this.parentSurface = null;
this._safetyLines = new Collection();
this._cropMarks = new Collection();
this._onShapePathChanged = (_t) => {
this._propertyChanged.notify(this, "shape");
this._propertyChanged.notify(this, "bounds");
};
this._onBleedPropertyChanged = (s, p) => {
this._validateMargin(this.bleed, "bleed");
this._propertyChanged.notify(this, "bleed");
};
this._onSlugPropertyChanged = (s, p) => {
this._validateMargin(this.slug, "slug");
this._propertyChanged.notify(this, "slug");
};
this._validateMargin(bleed, "bleed");
this._validateMargin(slug, "slug");
this.bleed = bleed !== null && bleed !== void 0 ? bleed : new Margin();
this.slug = slug !== null && slug !== void 0 ? slug : new Margin();
this._shape = this._ensureShape(shape, bounds);
this._subscribeShapeChanged();
}
get isRectangle() {
const { left, top, width, height } = this.bounds;
const rectanglePath = Path.rectangle(left, top, width, height);
return this.shape.equals(rectanglePath);
}
get bounds() {
return this._shape.calculateBounds();
}
set bounds(value) {
if (value == null)
throw new ArgumentException("PrintArea.bounds cannot be null.");
if (RectangleF.isEqual(this.bounds, value))
return;
this._resizeShape(value);
this._propertyChanged.notify(this, "bounds");
this._propertyChanged.notify(this, "shape");
}
get shape() {
return this._shape;
}
set shape(value) {
if (value == null)
throw new ArgumentException("PrintArea.shape cannot be null.");
if (this._shape != null && this._shape.isEqual(value))
return;
this._unsubscribeShapeChanged();
this._shape = value;
this._subscribeShapeChanged();
this._propertyChanged.notify(this, "shape");
this._propertyChanged.notify(this, "bounds");
}
get safetyLines() {
return this._safetyLines;
}
set safetyLines(value) {
if (this._safetyLines === value)
return;
this._safetyLines = value;
this._propertyChanged.notify(this, "safetyLines");
}
get cropMarks() {
return this._cropMarks;
}
set cropMarks(value) {
if (this._cropMarks === value)
return;
this._cropMarks = value;
this._propertyChanged.notify(this, "cropMarks");
}
get bleed() {
return this._bleed;
}
set bleed(value) {
if (equals(value, this._bleed))
return;
this._validateMargin(value, "bleed");
this._unsubscribeBleedPropertyChanged();
this._bleed = value;
this._subscribeBleedPropertyChanged();
this._propertyChanged.notify(this, "bleed");
}
get slug() {
return this._slug;
}
set slug(value) {
if (equals(value, this._slug))
return;
this._validateMargin(value, "slug");
this._unsubscribeSlugPropertyChanged();
this._slug = value;
this._subscribeSlugPropertyChanged();
this._propertyChanged.notify(this, "slug");
}
getBounds(type) {
switch (type) {
case PrintAreaBoundsType.Bleed:
return this.bounds.getExpanded(this.bleed);
case PrintAreaBoundsType.Total:
return this.bounds.getExpanded(new Margin({
left: this.bleed.left + this.slug.left,
top: this.bleed.top + this.slug.top,
right: this.bleed.right + this.slug.right,
bottom: this.bleed.bottom + this.slug.bottom
}));
default:
return this.bounds.clone();
}
}
_ensureShape(shape, bounds) {
if (shape != null) {
this._shape = shape;
if (bounds != null)
this._resizeShape(bounds);
return this._shape;
}
if (bounds != null)
return Path.rectangle(bounds.left, bounds.top, bounds.width, bounds.height);
return new Path();
}
_resizeShape(bounds) {
const current = this._shape.calculateBounds();
if (bounds == null || current.equals(bounds))
return;
if (current.isEmpty()) {
this._shape = Path.rectangle(bounds.left, bounds.top, bounds.width, bounds.height);
return;
}
const scaleX = bounds.width / current.width;
const scaleY = bounds.height / current.height;
if (!isFinite(scaleX) || !isFinite(scaleY)) {
this._unsubscribeShapeChanged();
this._shape = Path.rectangle(bounds.left, bounds.top, bounds.width, bounds.height);
this._subscribeShapeChanged();
return;
}
this._shape.translate(-current.left, -current.top);
this._shape.scale(scaleX, scaleY);
this._shape.translate(bounds.left, bounds.top);
}
_subscribeShapeChanged() {
if (this._shape != null)
this._shape.addPathChanged(this._onShapePathChanged);
}
_unsubscribeShapeChanged() {
if (this._shape != null)
this._shape.removePathChanged(this._onShapePathChanged);
}
_subscribeBleedPropertyChanged() {
if (this._bleed != null)
this._bleed.addPropertyChanged(this._onBleedPropertyChanged);
}
_unsubscribeBleedPropertyChanged() {
if (this._bleed != null)
this._bleed.removePropertyChanged(this._onBleedPropertyChanged);
}
_subscribeSlugPropertyChanged() {
if (this._slug != null)
this._slug.addPropertyChanged(this._onSlugPropertyChanged);
}
_unsubscribeSlugPropertyChanged() {
if (this._slug != null)
this._slug.removePropertyChanged(this._onSlugPropertyChanged);
}
_validateMargin(margin, marginName) {
if (margin != null && (margin.left < 0 || margin.top < 0 || margin.right < 0 || margin.bottom < 0))
throw new ArgumentException(`PrintArea.ts: ${marginName} margin members cannot be negative!`);
}
getSimplifiedObject() {
const simplified = super.getSimplifiedObject(["parentSurface", "boundsWithMargins", "boundsWithBleed", "isRectangle"]);
return simplified;
}
_copy(source, destination, generateNewIds) {
var _a, _b;
super._copy(source, destination, generateNewIds);
destination.bounds = source.bounds != null ? source.bounds.clone() : null;
destination.shape = source._shape != null ? source.shape.clone() : null;
destination.safetyLines.addRange(source.safetyLines.toArray().map(s => s.clone()));
destination.cropMarks.addRange(source.cropMarks.toArray().map(c => c.clone()));
destination.bleed = (_a = source.bleed) === null || _a === void 0 ? void 0 : _a.clone();
destination.slug = (_b = source.slug) === null || _b === void 0 ? void 0 : _b.clone();
}
clone(generateNewIds = false) {
var _a;
const printArea = new PrintArea(undefined, undefined, undefined, (_a = this.shape) === null || _a === void 0 ? void 0 : _a.clone());
this._copy(this, printArea, generateNewIds);
return printArea;
}
generateNewIds() {
this._generateNewId();
}
}
//# sourceMappingURL=PrintArea.js.map