Files
architype/LayoutGroup.js

103 lines
2.2 KiB
JavaScript
Raw Normal View History

class LayoutGroup {
2019-07-05 06:10:41 +00:00
constructor(graphGroup, nodesByPos, nodes) {
2019-07-04 06:42:05 +00:00
this.graphGroup_ = graphGroup;
2019-07-05 06:10:41 +00:00
this.nodesByPos_ = nodesByPos;
this.nodes = new Set(nodes);
this.tension = 0;
2019-07-09 20:34:36 +00:00
2019-07-13 03:41:36 +00:00
this.label = this.graphGroup_ ? this.graphGroup_.label : null;
2019-07-09 20:34:36 +00:00
for (let node of nodes) {
node.groups.add(this);
2019-07-09 20:34:36 +00:00
}
2019-07-03 01:42:17 +00:00
}
setTension() {
// Groups don't track tension, since we always want to sort last for total
// tension
2019-07-03 01:42:17 +00:00
this.vec = [0, 0];
2019-07-05 06:10:41 +00:00
for (let node of this.nodes) {
2019-07-03 01:42:17 +00:00
for (let i of [0, 1]) {
this.vec[i] += node.vec[i];
2019-07-11 18:14:06 +00:00
}
2019-07-03 01:42:17 +00:00
}
}
offsetCollides(offset) {
2019-07-05 06:10:41 +00:00
for (let node of this.nodes) {
let other = node.offsetCollides(offset);
if (other && !this.nodes.has(other)) {
2019-07-03 01:42:17 +00:00
return other;
}
}
return null;
}
savePos() {
2019-07-05 06:10:41 +00:00
for (let node of this.nodes) {
2019-07-03 01:42:17 +00:00
node.savePos();
}
}
restorePos() {
2019-07-05 06:10:41 +00:00
for (let node of this.nodes) {
node.restorePos();
2019-07-03 01:42:17 +00:00
}
2019-07-05 16:23:05 +00:00
for (let node of this.nodes) {
this.nodesByPos_.set(node.pos, node);
}
2019-07-03 01:42:17 +00:00
}
moveBy(offset) {
2019-07-05 06:10:41 +00:00
for (let node of this.nodes) {
node.moveBy(offset);
}
// Fix up nodesByPos, as intra-group collisions may have corrupted it
for (let node of this.nodes) {
2019-07-05 16:18:22 +00:00
this.nodesByPos_.set(node.pos, node);
2019-07-03 01:42:17 +00:00
}
}
2019-07-04 06:42:05 +00:00
getMinMax() {
let min = [Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER];
let max = [Number.MIN_SAFE_INTEGER, Number.MIN_SAFE_INTEGER];
for (let node of this.nodes) {
for (let i of [0, 1]) {
min[i] = Math.min(min[i], node.pos[i]);
max[i] = Math.max(max[i], node.pos[i]);
}
}
if (this.graphGroup_ && this.graphGroup_.label) {
// Room for the label
--min[1];
}
return [min, max];
}
2019-07-09 20:34:36 +00:00
isContained(pos) {
let [min, max] = this.getMinMax();
return (pos[0] >= min[0] && pos[0] <= max[0] &&
pos[1] >= min[1] && pos[1] <= max[1]);
}
2019-07-04 06:42:05 +00:00
getStep() {
if (!this.graphGroup_) {
return null;
}
let [min, max] = this.getMinMax();
return {
type: 'group',
min: min,
max: max,
2019-07-11 05:12:08 +00:00
id: this.graphGroup_.id,
2019-07-04 06:42:05 +00:00
label: this.graphGroup_.label,
2019-07-10 23:46:25 +00:00
highlight: this.graphGroup_.highlight,
2019-07-04 06:42:05 +00:00
};
}
2019-07-09 16:43:33 +00:00
hasGraphGroup() {
return this.graphGroup_ != null;
}
2019-07-03 01:42:17 +00:00
}