-
Notifications
You must be signed in to change notification settings - Fork 239
Expand file tree
/
Copy pathnode.js
More file actions
1093 lines (1003 loc) · 34.6 KB
/
node.js
File metadata and controls
1093 lines (1003 loc) · 34.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable import/no-cycle */
import { drawCircle, drawLine, arc } from './canvasApi'
import { simulationArea } from './simulationArea'
import { distance, showError } from './utils'
import {
renderCanvas,
scheduleUpdate,
wireToBeCheckedSet,
updateSimulationSet,
updateCanvasSet,
forceResetNodesSet,
canvasMessageData,
} from './engine'
import Wire from './wire'
import { colors } from './themer/themer'
import ContentionMeta from './contention'
/**
* Constructs all the connections of Node node
* @param {Node} node - node to be constructed
* @param {JSON} data - the saved data which is used to load
* @category node
*/
export function constructNodeConnections(node, data) {
for (var i = 0; i < data.connections.length; i++) {
if (
!node.connections.includes(node.scope.allNodes[data.connections[i]])
)
node.connect(node.scope.allNodes[data.connections[i]])
}
}
/**
* Fn to replace node by node @ index in global Node List - used when loading
* @param {Node} node - node to be replaced
* @param {number} index - index of node to be replaced
* @category node
*/
export function replace(node, index) {
if (index == -1) {
return node
}
var { scope } = node
var { parent } = node
parent.nodeList = parent.nodeList.filter(x=> x !== node);
node.delete()
node = scope.allNodes[index]
node.parent = parent
parent.nodeList.push(node)
node.updateRotation()
node.scope.timeStamp = new Date().getTime()
return node
}
function rotate(x1, y1, dir) {
if (dir == 'LEFT') {
return [-x1, y1]
}
if (dir == 'DOWN') {
return [y1, x1]
}
if (dir == 'UP') {
return [y1, -x1]
}
return [x1, y1]
}
export function extractBits(num, start, end) {
return (num << (32 - end)) >>> (32 - (end - start + 1))
}
export function bin2dec(binString) {
return parseInt(binString, 2)
}
export function dec2bin(dec, bitWidth = undefined) {
// only for positive nos
var bin = dec.toString(2)
if (bitWidth == undefined) return bin
return '0'.repeat(bitWidth - bin.length) + bin
}
/**
* find Index of a node
* @param {Node} x - Node to be dound
* @category node
*/
export function findNode(x) {
return x.scope.allNodes.indexOf(x)
}
/**
* function makes a node according to data providede
* @param {JSON} data - the data used to load a Project
* @param {Scope} scope - scope to which node has to be loaded
* @category node
*/
export function loadNode(data, scope) {
var n = new Node(
data.x,
data.y,
data.type,
scope.root,
data.bitWidth,
data.label
)
}
/**
* get Node in index x in scope and set parent
* @param {Node} x - the desired node
* @param {Scope} scope - the scope
* @param {CircuitElement} parent - The parent of node
* @category node
*/
function extractNode(x, scope, parent) {
var n = scope.allNodes[x]
n.parent = parent
return n
}
// output node=1
// input node=0
// intermediate node =2
window.NODE_INPUT = 0
window.NODE_OUTPUT = 1
window.NODE_INTERMEDIATE = 2
/**
* used to give id to a node.
* @type {number}
* @category node
*/
var uniqueIdCounter = 10
/**
* This class is responsible for all the Nodes.Nodes are connected using Wires
* Nodes are of 3 types;
* NODE_INPUT = 0;
* NODE_OUTPUT = 1;
* NODE_INTERMEDIATE = 2;
* Input and output nodes belong to some CircuitElement(it's parent)
* @param {number} x - x coord of Node
* @param {number} y - y coord of Node
* @param {number} type - type of node
* @param {CircuitElement} parent - parent element
* @param {?number} bitWidth - the bits of node in input and output nodes
* @param {string=} label - label for a node
* @category node
*/
export default class Node {
constructor(x, y, type, parent, bitWidth = undefined, label = '') {
// Should never raise, but just in case
if (isNaN(x) || isNaN(y)) {
this.delete()
showError('Fatal error occurred')
return
}
forceResetNodesSet(true)
this.objectType = 'Node'
this.subcircuitOverride = false
this.id = `node${uniqueIdCounter}`
uniqueIdCounter++
this.parent = parent
if (type != NODE_INTERMEDIATE && this.parent.nodeList !== undefined) {
this.parent.nodeList.push(this)
}
if (bitWidth == undefined) {
this.bitWidth = parent.bitWidth
} else {
this.bitWidth = bitWidth
}
this.label = label
this.prevx = undefined
this.prevy = undefined
this.leftx = x
this.lefty = y
this.x = x
this.y = y
this.type = type
this.connections = new Array()
this.value = undefined
this.radius = 5
this.clicked = false
this.hover = false
this.wasClicked = false
this.scope = this.parent.scope
this.scope.timeStamp = new Date().getTime()
/**
* @type {string}
* value of this.prev is
* 'a' : whenever a node is not being dragged this.prev is 'a'
* 'x' : when node is being dragged horizontally
* 'y' : when node is being dragged vertically
*/
this.prev = 'a'
this.count = 0
this.highlighted = false
// This fn is called during rotations and setup
this.refresh()
if (this.type == NODE_INTERMEDIATE) {
this.parent.scope.nodes.push(this)
}
this.parent.scope.allNodes.push(this)
this.queueProperties = {
inQueue: false,
time: undefined,
index: undefined,
}
}
/**
* @param {string} - new label
* Function to set label
*/
setLabel(label) {
this.label = label // || "";
}
/**
* function to convert a node to intermediate node
*/
converToIntermediate() {
this.type = NODE_INTERMEDIATE
this.x = this.absX()
this.y = this.absY()
this.parent = this.scope.root
this.scope.nodes.push(this)
}
/**
* Helper fuction to move a node. Sets up some variable which help in changing node.
*/
startDragging() {
this.oldx = this.x
this.oldy = this.y
}
/**
* Helper fuction to move a node.
*/
drag() {
this.x = this.oldx + simulationArea.mouseX - simulationArea.mouseDownX
this.y = this.oldy + simulationArea.mouseY - simulationArea.mouseDownY
}
/**
* function for saving a node
*/
saveObject() {
if (this.type == NODE_INTERMEDIATE) {
this.leftx = this.x
this.lefty = this.y
}
var data = {
x: this.leftx,
y: this.lefty,
type: this.type,
bitWidth: this.bitWidth,
label: this.label,
connections: [],
}
for (var i = 0; i < this.connections.length; i++) {
data.connections.push(findNode(this.connections[i]))
}
return data
}
/**
* helper function to help rotating parent
*/
updateRotation() {
var x
var y
;[x, y] = rotate(this.leftx, this.lefty, this.parent.direction)
this.x = x
this.y = y
}
/**
* Refreshes a node after roation of parent
*/
refresh() {
this.updateRotation()
for (var i = 0; i < this.connections.length; i++) {
this.connections[i].connections = this.connections[i].connections.filter(x => x !== this)
}
this.scope.timeStamp = new Date().getTime()
this.connections = []
}
/**
* gives absolute x position of the node
*/
absX() {
return this.x + this.parent.x
}
/**
* gives absolute y position of the node
*/
absY() {
return this.y + this.parent.y
}
/**
* update the scope of a node
*/
updateScope(scope) {
this.scope = scope
if (this.type == NODE_INTERMEDIATE) this.parent = scope.root
}
/**
* return true if node is connected or not connected but false if undefined.
*/
isResolvable() {
return this.value != undefined
}
/**
* function used to reset the nodes
*/
reset() {
this.value = undefined
this.highlighted = false
}
/**
* function to connect two nodes.
*/
connect(n) {
if (n == this) return
if (n.connections.includes(this)) {
return
}
new Wire(this, n, this.parent.scope)
this.connections.push(n)
n.connections.push(this)
this.scope.timeStamp = new Date().getTime()
updateCanvasSet(true)
updateSimulationSet(true)
scheduleUpdate()
}
/**
* connects but doesnt draw the wire between nodes
*/
connectWireLess(n) {
if (n == this) return
if (n.connections.includes(this)) return
this.connections.push(n)
n.connections.push(this)
this.scope.timeStamp = new Date().getTime()
// updateCanvasSet(true)
updateSimulationSet(true)
scheduleUpdate()
}
/**
* disconnecting two nodes connected wirelessly
*/
disconnectWireLess(n) {
this.connections = this.connections.filter(x => x !== n)
n.connections = n.connections.filter(x => x !== this)
this.scope.timeStamp = new Date().getTime()
}
/**
* function to resolve a node
*/
resolve() {
if (this.type == NODE_OUTPUT) {
// Since output node forces its value on its neighbours, remove its contentions.
// An existing contention will now trickle to the other output node that was causing
// the contention.
simulationArea.contentionPending.removeAllContentionsForNode(this);
}
// Remove Propogation of values (TriState)
if (this.value == undefined) {
for (var i = 0; i < this.connections.length; i++) {
if (this.connections[i].value !== undefined) {
this.connections[i].value = undefined
simulationArea.simulationQueue.add(this.connections[i])
}
}
if (this.type == NODE_INPUT) {
if (this.parent.objectType == 'Splitter') {
this.parent.removePropagation()
} else if (this.parent.isResolvable()) {
simulationArea.simulationQueue.add(this.parent)
} else {
this.parent.removePropagation()
}
}
if (this.type == NODE_OUTPUT && !this.subcircuitOverride) {
if (
this.parent.isResolvable() &&
!this.parent.queueProperties.inQueue
) {
if (this.parent.objectType == 'TriState' || this.parent.objectType == 'ControlledInverter') {
if (this.parent.state.value) {
simulationArea.simulationQueue.add(this.parent)
}
} else {
simulationArea.simulationQueue.add(this.parent)
}
}
}
return
}
// For input nodes, resolve its parents if they are resolvable at this point.
if (this.type == NODE_INPUT) {
if (this.parent.isResolvable()) {
simulationArea.simulationQueue.add(this.parent)
}
}
else if (this.type == NODE_OUTPUT) {
// Since output node forces its value on its neighbours, remove its contentions.
// An existing contention will now trickle to the other output node that was causing
// the contention.
simulationArea.contentionPending.removeAllContentionsForNode(this);
}
for (var i = 0; i < this.connections.length; i++) {
const node = this.connections[i];
switch (node.type) {
// TODO: For an output node, a downstream value (value given by elements other than the parent)
// should be overwritten in contention check and should not cause contention.
case NODE_OUTPUT:
if (node.value != this.value || node.bitWidth != this.bitWidth) {
// Check contentions
if (node.value != undefined && node.parent.objectType != 'SubCircuit'
&& !(node.subcircuitOverride && node.scope != this.scope)) {
// Tristate has always been a pain in the ass.
if ((node.parent.objectType == 'TriState' || node.parent.objectType == 'ControlledInverter') && node.value != undefined) {
if (node.parent.state.value) {
simulationArea.contentionPending.add(node, this);
break;
}
}
else {
simulationArea.contentionPending.add(node, this);
break;
}
}
} else {
// Output node was given an agreeing value, so remove any contention
// entry between these two nodes if it exists.
simulationArea.contentionPending.remove(node, this);
}
// Fallthrough. NODE_OUTPUT propagates like a contention checked NODE_INPUT
case NODE_INPUT:
// Check bitwidths
if (this.bitWidth != node.bitWidth) {
this.highlighted = true;
node.highlighted = true;
showError(`BitWidth Error: ${this.bitWidth} and ${node.bitWidth}`);
break;
}
// Fallthrough. NODE_INPUT propagates like a bitwidth checked NODE_INTERMEDIATE
case NODE_INTERMEDIATE:
if (node.value != this.value || node.bitWidth != this.bitWidth) {
// Propagate
node.bitWidth = this.bitWidth;
node.value = this.value;
simulationArea.simulationQueue.add(node);
}
default:
break;
}
}
}
/**
* this function checks if hover over the node
*/
checkHover() {
if (!simulationArea.mouseDown) {
if (simulationArea.hover == this) {
this.hover = this.isHover()
if (!this.hover) {
simulationArea.hover = undefined
this.showHover = false
}
} else if (!simulationArea.hover) {
this.hover = this.isHover()
if (this.hover) {
simulationArea.hover = this
} else {
this.showHover = false
}
} else {
this.hover = false
this.showHover = false
}
}
}
/**
* this function draw a node
*/
draw() {
const ctx = simulationArea.context
//
const color = colors['color_wire_draw']
if (this.clicked) {
if (this.prev == 'x') {
drawLine(
ctx,
this.absX(),
this.absY(),
simulationArea.mouseX,
this.absY(),
color,
3
)
drawLine(
ctx,
simulationArea.mouseX,
this.absY(),
simulationArea.mouseX,
simulationArea.mouseY,
color,
3
)
} else if (this.prev == 'y') {
drawLine(
ctx,
this.absX(),
this.absY(),
this.absX(),
simulationArea.mouseY,
color,
3
)
drawLine(
ctx,
this.absX(),
simulationArea.mouseY,
simulationArea.mouseX,
simulationArea.mouseY,
color,
3
)
} else if (
Math.abs(this.x + this.parent.x - simulationArea.mouseX) >
Math.abs(this.y + this.parent.y - simulationArea.mouseY)
) {
drawLine(
ctx,
this.absX(),
this.absY(),
simulationArea.mouseX,
this.absY(),
color,
3
)
} else {
drawLine(
ctx,
this.absX(),
this.absY(),
this.absX(),
simulationArea.mouseY,
color,
3
)
}
}
var colorNode = colors['stroke']
const colorNodeConnect = colors['color_wire_con']
const colorNodePow = colors['color_wire_pow']
const colorNodeLose = colors['color_wire_lose']
const colorNodeSelected = colors['node']
if (this.bitWidth == 1)
colorNode = [colorNodeConnect, colorNodePow][this.value]
if (this.value == undefined) colorNode = colorNodeLose
if (this.type == NODE_INTERMEDIATE) this.checkHover()
if (this.type == NODE_INTERMEDIATE) {
drawCircle(ctx, this.absX(), this.absY(), 3, colorNode)
} else {
drawCircle(ctx, this.absX(), this.absY(), 3, colorNodeSelected)
}
if (
this.highlighted ||
simulationArea.lastSelected == this ||
(this.isHover() &&
!simulationArea.selected &&
!simulationArea.shiftDown) ||
simulationArea.multipleObjectSelections.includes(this)
) {
ctx.strokeStyle = colorNodeSelected
ctx.beginPath()
ctx.lineWidth = 3
arc(
ctx,
this.x,
this.y,
8,
0,
Math.PI * 2,
this.parent.x,
this.parent.y,
'RIGHT'
)
ctx.closePath()
ctx.stroke()
}
if (this.hover || simulationArea.lastSelected == this) {
if (this.showHover || simulationArea.lastSelected == this) {
canvasMessageData.x = this.absX()
canvasMessageData.y = this.absY() - 15
if (this.type == NODE_INTERMEDIATE) {
var v = 'X'
if (this.value !== undefined) {
v = this.value.toString(16)
}
if (this.label.length) {
canvasMessageData.string = `${this.label} : ${v}`
} else {
canvasMessageData.string = v
}
} else if (this.label.length) {
canvasMessageData.string = this.label
}
} else {
setTimeout(() => {
if (simulationArea.hover)
simulationArea.hover.showHover = true
updateCanvasSet(true)
renderCanvas(globalScope)
}, 400)
}
}
}
/**
* checks if a node has been deleted
*/
checkDeleted() {
if (this.deleted) this.delete()
if (this.connections.length == 0 && this.type == NODE_INTERMEDIATE) this.delete()
}
/**
* used to update nodes if there is a event like click or hover on the node.
* many booleans are used to check if certain properties are to be updated.
*/
update() {
if (embed) return
if (this == simulationArea.hover) simulationArea.hover = undefined
this.hover = this.isHover()
if (!simulationArea.mouseDown) {
if (this.absX() != this.prevx || this.absY() != this.prevy) {
// Connect to any node
this.prevx = this.absX()
this.prevy = this.absY()
this.nodeConnect()
}
}
if (this.hover) {
simulationArea.hover = this
}
if (
simulationArea.mouseDown &&
((this.hover && !simulationArea.selected) ||
simulationArea.lastSelected == this)
) {
simulationArea.selected = true
simulationArea.lastSelected = this
this.clicked = true
} else {
this.clicked = false
}
if (!this.wasClicked && this.clicked) {
this.wasClicked = true
this.prev = 'a'
if (this.type == NODE_INTERMEDIATE) {
if (
!simulationArea.shiftDown &&
simulationArea.multipleObjectSelections.includes(this)
) {
for (
var i = 0;
i < simulationArea.multipleObjectSelections.length;
i++
) {
simulationArea.multipleObjectSelections[
i
].startDragging()
}
}
if (simulationArea.shiftDown) {
simulationArea.lastSelected = undefined
if (
simulationArea.multipleObjectSelections.includes(this)
) {
simulationArea.multipleObjectSelections = simulationArea.multipleObjectSelections.filter(x=> x !== this);
} else {
simulationArea.multipleObjectSelections.push(this)
}
} else {
simulationArea.lastSelected = this
}
}
} else if (this.wasClicked && this.clicked) {
if (
!simulationArea.shiftDown &&
simulationArea.multipleObjectSelections.includes(this)
) {
for (
var i = 0;
i < simulationArea.multipleObjectSelections.length;
i++
) {
simulationArea.multipleObjectSelections[i].drag()
}
}
if (this.type == NODE_INTERMEDIATE) {
if (
this.connections.length == 1 &&
this.connections[0].absX() == simulationArea.mouseX &&
this.absX() == simulationArea.mouseX
) {
this.y = simulationArea.mouseY - this.parent.y
this.prev = 'a'
return
}
if (
this.connections.length == 1 &&
this.connections[0].absY() == simulationArea.mouseY &&
this.absY() == simulationArea.mouseY
) {
this.x = simulationArea.mouseX - this.parent.x
this.prev = 'a'
return
}
if (
this.connections.length == 1 &&
this.connections[0].absX() == this.absX() &&
this.connections[0].absY() == this.absY()
) {
this.connections[0].clicked = true
this.connections[0].wasClicked = true
simulationArea.lastSelected = this.connections[0]
this.delete()
return
}
}
if (
this.prev == 'a' &&
distance(
simulationArea.mouseX,
simulationArea.mouseY,
this.absX(),
this.absY()
) >= 10
) {
if (
Math.abs(this.x + this.parent.x - simulationArea.mouseX) >
Math.abs(this.y + this.parent.y - simulationArea.mouseY)
) {
this.prev = 'x'
} else {
this.prev = 'y'
}
} else if (
this.prev == 'x' &&
this.absY() == simulationArea.mouseY
) {
this.prev = 'a'
} else if (
this.prev == 'y' &&
this.absX() == simulationArea.mouseX
) {
this.prev = 'a'
}
} else if (this.wasClicked && !this.clicked) {
this.wasClicked = false
if (
simulationArea.mouseX == this.absX() &&
simulationArea.mouseY == this.absY()
) {
return // no new node situation
}
var x1
var y1
var x2
var y2
var flag = 0
var n1
var n2
// (x,y) present node, (x1,y1) node 1 , (x2,y2) node 2
// n1 - node 1, n2 - node 2
// node 1 may or may not be there
// flag = 0 - node 2 only
// flag = 1 - node 1 and node 2
x2 = simulationArea.mouseX
y2 = simulationArea.mouseY
const x = this.absX()
const y = this.absY()
if (x != x2 && y != y2) {
// Rare Exception Cases
if (
this.prev == 'a' &&
distance(
simulationArea.mouseX,
simulationArea.mouseY,
this.absX(),
this.absY()
) >= 10
) {
if (
Math.abs(
this.x + this.parent.x - simulationArea.mouseX
) >
Math.abs(this.y + this.parent.y - simulationArea.mouseY)
) {
this.prev = 'x'
} else {
this.prev = 'y'
}
}
flag = 1
if (this.prev == 'x') {
x1 = x2
y1 = y
} else if (this.prev == 'y') {
y1 = y2
x1 = x
}
}
if (flag == 1) {
for (var i = 0; i < this.parent.scope.allNodes.length; i++) {
if (
x1 == this.parent.scope.allNodes[i].absX() &&
y1 == this.parent.scope.allNodes[i].absY()
) {
n1 = this.parent.scope.allNodes[i]
break
}
}
if (n1 == undefined) {
n1 = new Node(x1, y1, 2, this.scope.root)
for (var i = 0; i < this.parent.scope.wires.length; i++) {
if (this.parent.scope.wires[i].checkConvergence(n1)) {
this.parent.scope.wires[i].converge(n1)
break
}
}
}
this.connect(n1)
}
for (var i = 0; i < this.parent.scope.allNodes.length; i++) {
if (
x2 == this.parent.scope.allNodes[i].absX() &&
y2 == this.parent.scope.allNodes[i].absY()
) {
n2 = this.parent.scope.allNodes[i]
break
}
}
if (n2 == undefined) {
n2 = new Node(x2, y2, 2, this.scope.root)
for (var i = 0; i < this.parent.scope.wires.length; i++) {
if (this.parent.scope.wires[i].checkConvergence(n2)) {
this.parent.scope.wires[i].converge(n2)
break
}
}
}
if (flag == 0) this.connect(n2)
else n1.connect(n2)
if (simulationArea.lastSelected == this)
simulationArea.lastSelected = n2
}
if (this.type == NODE_INTERMEDIATE && simulationArea.mouseDown == false) {
if (this.connections.length == 2) {
if (
this.connections[0].absX() == this.connections[1].absX() ||
this.connections[0].absY() == this.connections[1].absY()
) {
this.connections[0].connect(this.connections[1])
this.delete()
}
} else if (this.connections.length == 0) this.delete()
}
}
/**
* function delete a node
*
* Real-world impact if deletion fails:
* - Memory leaks: Orphaned nodes remain in nodeList, preventing garbage collection
* - Simulation errors: Deleted nodes still referenced, causing "node not found" errors
* - UI glitches: Deleted nodes may still appear in properties panel or cause crashes
* - Example scenario: User deletes a gate's input node, but it remains in parent.nodeList.
* Later, when the circuit simulates, it tries to access the deleted node, causing:
* * Simulation to fail or produce incorrect results
* * Browser console errors
* * Circuit becoming unresponsive
* * Need to reload the page to fix
*/
delete() {
updateSimulationSet(true)
this.deleted = true
this.parent.scope.allNodes = this.parent.scope.allNodes.filter(x => x !== this)
this.parent.scope.nodes = this.parent.scope.nodes.filter(x => x !== this)
// Remove node from parent's nodeList (where it was originally added)
// Input/output nodes (type 0, 1) are added to parent.nodeList in constructor (line 167)
// We need to remove from the correct parent's nodeList, not always from root.nodeList
//
// Previous bug: Code tried to remove from root.nodeList, but nodes are in parent.nodeList
// This mismatch meant nodes weren't properly removed, causing the issues above
if (this.parent && this.parent.nodeList && Array.isArray(this.parent.nodeList)) {
const index = this.parent.nodeList.indexOf(this)
if (index !== -1) {
this.parent.nodeList.splice(index, 1)
} else {
// Fallback: use filter if indexOf doesn't work (shouldn't happen, but safer)
this.parent.nodeList = this.parent.nodeList.filter(x => x !== this)
}
}
// Also remove from root.nodeList if present (for safety and backward compatibility)
// Some edge cases might have nodes in root.nodeList
if (this.parent && this.parent.scope && this.parent.scope.root &&
this.parent.scope.root.nodeList && Array.isArray(this.parent.scope.root.nodeList)) {
const rootIndex = this.parent.scope.root.nodeList.indexOf(this)
if (rootIndex !== -1) {
this.parent.scope.root.nodeList.splice(rootIndex, 1)
}
}
if (simulationArea.lastSelected == this)
simulationArea.lastSelected = undefined
for (var i = 0; i < this.connections.length; i++) {
this.connections[i].connections = this.connections[i].connections.filter(x => x !== this)
this.connections[i].checkDeleted()
}
this.scope.timeStamp = new Date().getTime()
wireToBeCheckedSet(1)
forceResetNodesSet(true)
scheduleUpdate()
}
isClicked() {
return (
this.absX() == simulationArea.mouseX &&
this.absY() == simulationArea.mouseY
)
}
isHover() {
return (
this.absX() == simulationArea.mouseX &&