import { Vector2 } from 'touchcontroller'; import { Anchor } from './Anchor'; import { Donor } from './Donor'; import { Rect } from './Rect'; export class Acceptor extends Anchor { public donors: Donor[]; constructor( rect: Rect, type: string, relativePosition: Vector2, public accepts: number, public order: 'UP' | 'DOWN' | 'KEEP' = 'DOWN', ) { super(rect, type, relativePosition); this.donors = []; } clone(): Acceptor { return new Acceptor( this.rect, this.type, this.relativePosition, this.accepts, this.order, ); } snapDonor(donor: Donor) { if (this.donors.indexOf(donor) === -1) { //console.log(donor); //console.log(`snapDonor`); donor.acceptor = this; this.donors.push(donor); if (this.order === 'UP') { //this.rect.svgElement.parentNode.insertBefore(donor.rect.svgElement,this.rect.svgElement); orderElements( this.rect.svgGroupElement, donor.rect.svgGroupElement, ); } else if (this.order === 'DOWN') { //this.rect.svgElement.parentNode.insertAfter(donor.rect.svgElement,this.rect.svgElement); orderElements( this.rect.svgGroupElement, donor.rect.svgGroupElement, ); orderElements( donor.rect.svgGroupElement, this.rect.svgGroupElement, ); } /**/ } } get full() { if (this.accepts === -1) { return false; } else { return this.donors.length >= this.accepts; } } /*fullInsteadOf(donors){ }*/ isAccepting(donor: Donor) { return this.type === donor.type; } get followingDonors() { return this.donors.filter((donor) => donor.follow); } } function orderElements(first: SVGElement, second: SVGElement) { if (first.parentNode) { first.parentNode!.insertBefore(second, first); } else { console.warn(`first.parentNode do do not exists.`); } } /**/