mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
Merge pull request #163 from SyncrowIOT/fix/duplication-flatten
Fix/duplication-flatten
This commit is contained in:
@ -407,21 +407,31 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
|||||||
|
|
||||||
List<SpaceModel> flattenSpaces(List<SpaceModel> spaces) {
|
List<SpaceModel> flattenSpaces(List<SpaceModel> spaces) {
|
||||||
List<SpaceModel> result = [];
|
List<SpaceModel> result = [];
|
||||||
|
Map<String, SpaceModel> idToSpace = {};
|
||||||
|
|
||||||
void flatten(SpaceModel space) {
|
void flatten(SpaceModel space) {
|
||||||
if (space.status == SpaceStatus.deleted || space.status == SpaceStatus.parentDeleted) {
|
if (space.status == SpaceStatus.deleted || space.status == SpaceStatus.parentDeleted) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
result.add(space);
|
result.add(space);
|
||||||
|
idToSpace[space.internalId] = space;
|
||||||
|
|
||||||
for (var child in space.children) {
|
for (var child in space.children) {
|
||||||
flatten(child);
|
flatten(child);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
for (var space in spaces) {
|
for (var space in spaces) {
|
||||||
flatten(space);
|
flatten(space);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (var space in result) {
|
||||||
|
if (space.parent != null) {
|
||||||
|
space.parent = idToSpace[space.parent!.internalId];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -698,7 +708,7 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
|||||||
final double verticalGap = 180.0;
|
final double verticalGap = 180.0;
|
||||||
final double nodeWidth = 200;
|
final double nodeWidth = 200;
|
||||||
final double nodeHeight = 100;
|
final double nodeHeight = 100;
|
||||||
final double breathingSpace = 300.0; // extra gap after original tree
|
final double breathingSpace = 300.0;
|
||||||
|
|
||||||
/// Helper to recursively duplicate a node and its children
|
/// Helper to recursively duplicate a node and its children
|
||||||
SpaceModel duplicateRecursive(SpaceModel original, SpaceModel? duplicatedParent) {
|
SpaceModel duplicateRecursive(SpaceModel original, SpaceModel? duplicatedParent) {
|
||||||
@ -706,7 +716,7 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
|||||||
final duplicated = SpaceModel(
|
final duplicated = SpaceModel(
|
||||||
name: duplicatedName,
|
name: duplicatedName,
|
||||||
icon: original.icon,
|
icon: original.icon,
|
||||||
position: Offset.zero,
|
position: original.position,
|
||||||
isPrivate: original.isPrivate,
|
isPrivate: original.isPrivate,
|
||||||
children: [],
|
children: [],
|
||||||
status: SpaceStatus.newSpace,
|
status: SpaceStatus.newSpace,
|
||||||
@ -725,11 +735,13 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
|||||||
direction: "down",
|
direction: "down",
|
||||||
);
|
);
|
||||||
connections.add(newConnection);
|
connections.add(newConnection);
|
||||||
|
|
||||||
duplicated.incomingConnection = newConnection;
|
duplicated.incomingConnection = newConnection;
|
||||||
duplicatedParent.addOutgoingConnection(newConnection);
|
duplicatedParent.addOutgoingConnection(newConnection);
|
||||||
duplicatedParent.children.add(duplicated);
|
duplicatedParent.children.add(duplicated);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Duplicate its children recursively
|
||||||
for (var child in original.children) {
|
for (var child in original.children) {
|
||||||
duplicateRecursive(child, duplicated);
|
duplicateRecursive(child, duplicated);
|
||||||
}
|
}
|
||||||
@ -737,48 +749,29 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
|||||||
return duplicated;
|
return duplicated;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Layout a subtree rooted at node
|
|
||||||
void layoutSubtree(SpaceModel node, double startX, double startY) {
|
|
||||||
double calculateSubtreeWidth(SpaceModel n) {
|
|
||||||
if (n.children.isEmpty) return nodeWidth;
|
|
||||||
double width = 0;
|
|
||||||
for (var child in n.children) {
|
|
||||||
width += calculateSubtreeWidth(child) + horizontalGap;
|
|
||||||
}
|
|
||||||
return width - horizontalGap;
|
|
||||||
}
|
|
||||||
|
|
||||||
void assignPositions(SpaceModel n, double x, double y) {
|
|
||||||
double subtreeWidth = calculateSubtreeWidth(n);
|
|
||||||
double centerX = x + subtreeWidth / 2 - nodeWidth / 2;
|
|
||||||
n.position = Offset(centerX, y);
|
|
||||||
|
|
||||||
if (n.children.length == 1) {
|
|
||||||
assignPositions(n.children.first, centerX, y + verticalGap);
|
|
||||||
} else {
|
|
||||||
double childX = x;
|
|
||||||
for (var child in n.children) {
|
|
||||||
double childWidth = calculateSubtreeWidth(child);
|
|
||||||
assignPositions(child, childX, y + verticalGap);
|
|
||||||
childX += childWidth + horizontalGap;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
double totalSubtreeWidth = calculateSubtreeWidth(node);
|
|
||||||
assignPositions(node, startX, startY);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Actual duplication process
|
/// Actual duplication process
|
||||||
setState(() {
|
setState(() {
|
||||||
if (space.parent == null) {
|
if (space.parent == null) {
|
||||||
// Duplicating a ROOT node
|
// Duplicating a ROOT node
|
||||||
SpaceModel duplicatedRoot = duplicateRecursive(space, null);
|
duplicateRecursive(space, null);
|
||||||
|
connections = createConnections(spaces);
|
||||||
realignTree();
|
realignTree();
|
||||||
} else {
|
} else {
|
||||||
// Duplicating a CHILD node inside its parent
|
final parent = space.parent!;
|
||||||
SpaceModel duplicated = duplicateRecursive(space, space.parent);
|
|
||||||
|
final duplicated = duplicateRecursive(space, parent);
|
||||||
|
final newConnection = Connection(
|
||||||
|
startSpace: parent,
|
||||||
|
endSpace: duplicated,
|
||||||
|
direction: "down",
|
||||||
|
);
|
||||||
|
connections.add(newConnection);
|
||||||
|
duplicated.incomingConnection = newConnection;
|
||||||
|
parent.addOutgoingConnection(newConnection);
|
||||||
|
parent.children.add(duplicated);
|
||||||
|
connections = createConnections(spaces);
|
||||||
realignTree();
|
realignTree();
|
||||||
|
//_realignSubtree(space.parent!);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user