Skip to content

Commit 8e1de32

Browse files
authored
Undo redo bugs (#118)
1 parent c4bf4b1 commit 8e1de32

File tree

14 files changed

+168
-125
lines changed

14 files changed

+168
-125
lines changed

src/context.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class GlobalContext {
3939
this.datagraph = datagraph;
4040
this.viewport.clear();
4141
if (this.viewgraph) {
42-
this.viewgraph.destroy();
42+
this.viewgraph.clear();
4343
}
4444
this.viewgraph = new ViewGraph(this.datagraph, this.viewport, layer);
4545
this.viewgraph.setSpeed(speedMultiplier?.value || 1);
@@ -96,7 +96,7 @@ export class GlobalContext {
9696
changeViewGraph(selectedLayer: string) {
9797
const layer = layerFromName(selectedLayer);
9898
const speedMultiplier = this.getCurrentSpeed();
99-
urManager.reset();
99+
// urManager.reset();
100100
this.setNetwork(this.datagraph, layer, speedMultiplier);
101101
}
102102

@@ -142,4 +142,11 @@ export class GlobalContext {
142142
const mask = "255.255.255.255";
143143
this.ipGenerator = new IpAddressGenerator(baseIp, mask);
144144
}
145+
146+
print() {
147+
console.log("VieGraph:");
148+
console.log(this.viewgraph);
149+
console.log("DataGraph");
150+
console.log(this.datagraph);
151+
}
145152
}

src/graphics/renderables/device_info.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ export class DeviceInfo extends StyledInfo {
4343
"Delete device",
4444
() => {
4545
const deviceData = this.device.getCreateDevice();
46+
const currLayer = this.device.viewgraph.getLayer();
4647
const move = new RemoveDeviceMove(
48+
currLayer,
4749
deviceData,
48-
this.device.getConnections(),
4950
this.device.viewgraph,
5051
);
5152
this.device.delete();

src/index.ejs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<button id="new-button" class="new-button" title="New">New</button>
1010
<button id="save-button" class="save-button" title="Save">Save</button>
1111
<button id="load-button" class="load-button" title="Load">Load</button>
12+
<button id="print-button" class="load-button" title="Print">Print</button>
1213
<div id="app-title">GEduNet</div>
1314
</div>
1415
<div id="bottom-screen" class="row container">
@@ -28,7 +29,7 @@
2829
max="4"
2930
step="0.1"
3031
value="1"
31-
>
32+
/>
3233
</div>
3334
<div class="value-display">1x</div>
3435
</div>

src/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ async function loadAssets(otherPromises: Promise<void>[]) {
9898
const newButton = document.getElementById("new-button");
9999
const loadButton = document.getElementById("load-button");
100100
const saveButton = document.getElementById("save-button");
101+
const printButton = document.getElementById("print-button");
101102

102103
newButton.onclick = () => {
103104
deselectElement();
@@ -111,6 +112,9 @@ async function loadAssets(otherPromises: Promise<void>[]) {
111112
deselectElement();
112113
loadFromFile(ctx);
113114
};
115+
printButton.onclick = () => {
116+
ctx.print();
117+
};
114118
// Undo button’s logic
115119
const undoButton = document.getElementById(
116120
"undo-button",
@@ -214,7 +218,7 @@ async function loadAssets(otherPromises: Promise<void>[]) {
214218
valueDisplay.textContent = `${value}x`;
215219
}
216220

217-
// (!) For layer abstraction functionality
221+
// For layer abstraction logic
218222
const selectNewLayer = (event: Event) => {
219223
const selectedLayer = (event.target as HTMLSelectElement).value;
220224
console.log(`Layer selected: ${selectedLayer}`);
@@ -224,11 +228,16 @@ async function loadAssets(otherPromises: Promise<void>[]) {
224228
saveToLocalStorage(ctx);
225229
// LeftBar is reset
226230
leftBar.setButtonsByLayer(selectedLayer);
227-
deselectElement();
231+
deselectElement(); // not needed
228232
}
229233
};
230234

231235
layerSelect.onchange = selectNewLayer;
236+
layerSelect.addEventListener("layerChanged", () => {
237+
const currLayer = layerToName(ctx.getCurrentLayer());
238+
layerSelect.value = currLayer;
239+
leftBar.setButtonsByLayer(currLayer);
240+
});
232241

233242
document.body.onkeyup = function (e) {
234243
if (e.key === " " || e.code === "Space") {

src/types/devices/device.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ export abstract class Device extends Container {
137137
return { id: this.id, node };
138138
}
139139

140-
addConnection(adyacentId: DeviceId) {
141-
this.connections.add(adyacentId);
140+
addConnection(adjacentId: DeviceId) {
141+
this.connections.add(adjacentId);
142142
}
143143

144144
removeConnection(id: DeviceId) {
@@ -166,9 +166,6 @@ export abstract class Device extends Container {
166166

167167
delete(): void {
168168
this.viewgraph.removeDevice(this.id);
169-
// Clear connections
170-
this.connections.clear();
171-
deselectElement();
172169
console.log(`Device ${this.id} deleted`);
173170
this.destroy();
174171
}
@@ -188,16 +185,21 @@ export abstract class Device extends Container {
188185
this.parent.on("pointerup", onPointerUp);
189186
}
190187

191-
connectTo(adyacentId: DeviceId): boolean {
188+
connectTo(adjacentId: DeviceId): boolean {
192189
// Connects both devices with an edge.
193-
const edgeId = this.viewgraph.addEdge(this.id, adyacentId);
190+
// console.log("Entered connectTo");
191+
192+
const edgeId = this.viewgraph.addEdge(this.id, adjacentId);
194193
if (edgeId) {
195-
const adyacentDevice = this.viewgraph.getDevice(adyacentId);
196-
this.addConnection(adyacentId);
197-
adyacentDevice.addConnection(this.id);
194+
const adjacentDevice = this.viewgraph.getDevice(adjacentId);
195+
this.addConnection(adjacentId);
196+
adjacentDevice.addConnection(this.id);
198197

199198
// Register move
200-
const move = new AddEdgeMove({ n1: this.id, n2: adyacentId });
199+
const move = new AddEdgeMove(this.viewgraph.getLayer(), {
200+
n1: this.id,
201+
n2: adjacentId,
202+
});
201203
urManager.push(move);
202204

203205
return true;
@@ -277,16 +279,18 @@ export abstract class Device extends Container {
277279
Device.connectionTarget = null;
278280
}
279281

280-
// Cleans up related resources
281-
destroy() {
282-
// do nothing
283-
}
284-
285282
// Return the device’s type.
286283
abstract getType(): DeviceType;
287284

288285
// Return the device’s layer.
289286
abstract getLayer(): Layer;
287+
288+
destroy() {
289+
// Clear connections
290+
this.connections.clear();
291+
deselectElement();
292+
super.destroy();
293+
}
290294
}
291295

292296
function onPointerMove(event: FederatedPointerEvent): void {
@@ -323,6 +327,7 @@ function onPointerUp(): void {
323327
);
324328
} else {
325329
const move = new DragDeviceMove(
330+
Device.dragTarget.viewgraph.getLayer(),
326331
Device.dragTarget.id,
327332
Device.startPosition,
328333
endPosition,

src/types/devices/layer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function layerToName(layer: Layer): string {
2727
return layerToNameMap.get(layer);
2828
}
2929

30-
export function layerIncluded(layer1: Layer, layer2: Layer) {
31-
// Determines whether layer1 is included within layer2’s abstraction.
32-
return layer1 >= layer2;
30+
export function layerIncluded(minorLayer: Layer, majorLayer: Layer) {
31+
// Determines whether minorLayer is included within majorLayer’s abstraction.
32+
return minorLayer >= majorLayer;
3333
}

src/types/edge.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ export class Edge extends Graphics {
131131

132132
// Crear el movimiento de eliminación de la arista con la información adicional
133133
const move = new RemoveEdgeMove(
134+
this.viewgraph.getLayer(),
134135
this.connectedNodes,
135136
new Map([
136137
[this.connectedNodes.n1, routingTable1],
@@ -148,12 +149,15 @@ export class Edge extends Graphics {
148149
// Method to delete the edge
149150
delete() {
150151
// Remove the edge from the viewgraph and datagraph
151-
deselectElement();
152152
const id = Edge.generateConnectionKey(this.connectedNodes);
153153
this.viewgraph.removeEdge(id);
154+
console.log(`Edge ${id} deleted.`);
154155
this.destroy();
156+
}
155157

156-
console.log(`Edge ${id} deleted.`);
158+
destroy() {
159+
deselectElement();
160+
super.destroy();
157161
}
158162

159163
public updatePosition(device1: Device, device2: Device) {

src/types/graphs/datagraph.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -345,10 +345,10 @@ export class DataGraph {
345345
if (!isRouter(router)) return;
346346

347347
router.routingTable = this.generateRoutingTable(id, true);
348-
console.log(
349-
`Routing table regenerated for router ID ${id}:`,
350-
router.routingTable,
351-
);
348+
// console.log(
349+
// `Routing table regenerated for router ID ${id}:`,
350+
// router.routingTable,
351+
// );
352352
}
353353

354354
private generateRoutingTable(
@@ -360,9 +360,9 @@ export class DataGraph {
360360
return [];
361361
}
362362

363-
console.log(
364-
`Regenerating ${preserveEdits ? "full" : "clean"} routing table for ID ${id}`,
365-
);
363+
// console.log(
364+
// `Regenerating ${preserveEdits ? "full" : "clean"} routing table for ID ${id}`,
365+
// );
366366
const parents = new Map<DeviceId, DeviceId>();
367367
parents.set(id, id);
368368
const queue = [id];

src/types/graphs/viewgraph.ts

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -126,20 +126,6 @@ export class ViewGraph {
126126
return null;
127127
}
128128

129-
// Check if an edge already exists between these two devices
130-
for (const edge of this.edges.values()) {
131-
const { n1, n2 } = edge.connectedNodes;
132-
if (
133-
(n1 === device1Id && n2 === device2Id) ||
134-
(n1 === device2Id && n2 === device1Id)
135-
) {
136-
console.warn(
137-
`Connection between ID ${device1Id} and ID ${device2Id} already exists.`,
138-
);
139-
return null;
140-
}
141-
}
142-
143129
const device1 = this.devices.get(device1Id);
144130
const device2 = this.devices.get(device2Id);
145131

@@ -160,19 +146,19 @@ export class ViewGraph {
160146

161147
deviceMoved(deviceId: DeviceId) {
162148
const device: Device = this.devices.get(deviceId);
163-
device.getConnections().forEach((adyacentId) => {
149+
device.getConnections().forEach((adjacentId) => {
164150
const edge = this.edges.get(
165-
Edge.generateConnectionKey({ n1: deviceId, n2: adyacentId }),
151+
Edge.generateConnectionKey({ n1: deviceId, n2: adjacentId }),
166152
);
167153
// Get start and end devices directly
168154
const startDevice =
169155
edge.connectedNodes.n1 === device.id
170156
? device
171-
: this.devices.get(adyacentId);
157+
: this.devices.get(adjacentId);
172158

173159
const endDevice =
174160
edge.connectedNodes.n1 === device.id
175-
? this.devices.get(adyacentId)
161+
? this.devices.get(adjacentId)
176162
: device;
177163

178164
if (startDevice && endDevice) {
@@ -188,6 +174,17 @@ export class ViewGraph {
188174
return this.layer;
189175
}
190176

177+
changeCurrLayer(newLayer: Layer) {
178+
this.layer = newLayer;
179+
this.clear();
180+
this.constructView();
181+
const layerSelect = document.getElementById(
182+
"layer-select",
183+
) as HTMLSelectElement;
184+
const event = new CustomEvent("layerChanged");
185+
layerSelect.dispatchEvent(event);
186+
}
187+
191188
getSpeed(): SpeedMultiplier {
192189
if (!this.speedMultiplier) {
193190
this.speedMultiplier = new SpeedMultiplier(1);
@@ -211,8 +208,8 @@ export class ViewGraph {
211208
}
212209
const connections = device
213210
.getConnections()
214-
.map((adyacentId) =>
215-
this.edges.get(Edge.generateConnectionKey({ n1: id, n2: adyacentId })),
211+
.map((adjacentId) =>
212+
this.edges.get(Edge.generateConnectionKey({ n1: id, n2: adjacentId })),
216213
);
217214
return connections;
218215
}
@@ -250,11 +247,9 @@ export class ViewGraph {
250247
return;
251248
}
252249

253-
device.destroy();
254-
255-
// Remove connection from adyacent’s devices
256-
device.getConnections().forEach((adyacentId) => {
257-
const edgeId = Edge.generateConnectionKey({ n1: id, n2: adyacentId });
250+
// Remove connection from adjacent’s devices
251+
device.getConnections().forEach((adjacentId) => {
252+
const edgeId = Edge.generateConnectionKey({ n1: id, n2: adjacentId });
258253
const edge = this.edges.get(edgeId);
259254
if (edge) {
260255
edge.delete();
@@ -346,14 +341,14 @@ export class ViewGraph {
346341
const unvisitedNodes = [];
347342
const connectingEdges = new Map<DeviceId, EdgeId>([[a.id, null]]);
348343
while (current.id !== idB) {
349-
for (const adyacentId of current.connections) {
344+
for (const adjacentId of current.connections) {
350345
const edgeId = Edge.generateConnectionKey({
351346
n1: current.id,
352-
n2: adyacentId,
347+
n2: adjacentId,
353348
});
354-
if (!connectingEdges.has(adyacentId)) {
355-
connectingEdges.set(adyacentId, edgeId);
356-
unvisitedNodes.push(this.devices.get(adyacentId));
349+
if (!connectingEdges.has(adjacentId)) {
350+
connectingEdges.set(adjacentId, edgeId);
351+
unvisitedNodes.push(this.devices.get(adjacentId));
357352
}
358353
}
359354
if (unvisitedNodes.length === 0) {
@@ -393,12 +388,12 @@ export class ViewGraph {
393388
if (visited.has(w)) {
394389
return;
395390
}
396-
const adyacent = this.datagraph.getDevice(w);
391+
const adjacent = this.datagraph.getDevice(w);
397392
// mark node as visited
398393
visited.add(w);
399394

400-
if (layerIncluded(layerFromType(adyacent.type), this.layer)) {
401-
// add connection between v and w
395+
if (layerIncluded(layerFromType(adjacent.type), this.layer)) {
396+
// add connection between s and w
402397
const connectionKey: string = Edge.generateConnectionKey({
403398
n1: w,
404399
n2: s,
@@ -413,7 +408,11 @@ export class ViewGraph {
413408
});
414409
}
415410

416-
destroy() {
411+
clear() {
412+
this.viewport.clear();
417413
this.devices.forEach((device) => device.destroy());
414+
this.devices.clear();
415+
this.edges.forEach((edge) => edge.destroy());
416+
this.edges.clear();
418417
}
419418
}

0 commit comments

Comments
 (0)