import { artefact, asset } from '../core/library.js';
import { mergeOver, pushUnique, removeItem, isa_boolean } from '../core/utilities.js';This mixin defines the attributes and functionality required by an artefact so that it can position and rotate itself using values set in another artefact.
import { artefact, asset } from '../core/library.js';
import { mergeOver, pushUnique, removeItem, isa_boolean } from '../core/utilities.js';export default function (P = {}) { let defaultAttributes = {pivot - reference artefact object. Can also be set using the artefact’s name-String.
pivot: '',pivotCorner - Element artefacts allow other artefacts to use their corner positions as pivots, by setting this attribute to topLeft, topRight, bottomRight or bottomLeft; default is '' to use the Element’s start coordinate.
pivotCorner: '',pivotPin - Polyline entitys are composed of a set of pin coordinates with the start being pin[0]; can reference other pins by setting this attribute to the appropriate index value (for example, the second pin will be pin[1]).
pivotPin: 0,addPivotHandle, addPivotOffset, addPivotRotation - Boolean flags. When set, the artifact will add its own values to the reference artefact’s values, rather than use them as replacement values.
addPivotHandle: false,
addPivotOffset: true,
addPivotRotation: false,
};
P.defs = mergeOver(P.defs, defaultAttributes);
mergeOver(P, defaultAttributes); P.packetObjects = pushUnique(P.packetObjects, ['pivot']); let G = P.getters,
S = P.setters,
D = P.deltaSetters;pivot
S.pivot = function (item) {
if (isa_boolean(item) && !item) {
this.pivot = null;
if (this.lockTo[0] === 'pivot') this.lockTo[0] = 'start';
if (this.lockTo[1] === 'pivot') this.lockTo[1] = 'start';
this.dirtyStampPositions = true;
this.dirtyStampHandlePositions = true;
}
else {
let oldPivot = this.pivot,
name = this.name;
let newPivot = (item.substring) ? artefact[item] : item;
if (!newPivot) {
newPivot = asset[item];
if (newPivot && newPivot.type !== 'Cell') newPivot = false;
}
if (newPivot && newPivot.name) {
if (oldPivot && oldPivot.name !== newPivot.name) removeItem(oldPivot.pivoted, name);
pushUnique(newPivot.pivoted, name);
this.pivot = newPivot;
this.dirtyStampPositions = true;
this.dirtyStampHandlePositions = true;
}
}
};pivotCorner
P.pivotCorners = ['topLeft', 'topRight', 'bottomRight', 'bottomLeft'];
S.pivotCorner = function (item) {
if (this.pivotCorners.indexOf(item) >= 0) this.pivotCorner = item;
};addPivotHandle, addPivotOffset, addPivotRotation
S.addPivotHandle = function (item) {
this.addPivotHandle = item;
this.dirtyHandle = true;
};
S.addPivotOffset = function (item) {
this.addPivotOffset = item;
this.dirtyOffset = true;
};
S.addPivotRotation = function (item) {
this.addPivotRotation = item;
this.dirtyRotation = true;
};updatePivotSubscribers
P.updatePivotSubscribers = function () {
this.pivoted.forEach(name => {
let instance = artefact[name];
if (!instance) {
instance = asset[name];
if (!instance || instance.type !== 'Cell') instance = false;
}
if (instance) {
instance.dirtyStart = true;
if (instance.addPivotHandle) instance.dirtyHandle = true;
if (instance.addPivotOffset) instance.dirtyOffset = true;
if (instance.addPivotRotation) instance.dirtyRotation = true;
if (instance.type === 'Polyline') instance.dirtyPins = true;
else if (instance.type === 'Line' || instance.type === 'Quadratic' || instance.type === 'Bezier') instance.dirtyPins.push(this.name);
}
}, this);
};Return the prototype
return P;
};