-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDaggerPinCollection.js
More file actions
232 lines (201 loc) · 5.88 KB
/
Copy pathDaggerPinCollection.js
File metadata and controls
232 lines (201 loc) · 5.88 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
'use strict'
const DaggerBase = require('./DaggerBase.js').DaggerBase;
const DaggerSignal = require('./DaggerBase.js').DaggerSignal;
/**
* Class that acts as a container for pins of one particular direction.
* @extends DaggerBase
*/
class DaggerPinCollection extends DaggerBase {
/**
* DaggerPinCollection ctor
* @param {DaggerNode} parentNode
* @param {DaggerBasePin.PinDirection} direction
* @param {number} topology_system
*/
constructor(parentNode, direction, topology_system) {
super();
this._direction = direction;
this._parentNode = parentNode;
this._topology_system = topology_system;
this._pinCollection = {};
this._orderedCollection = [];
// signals
this.pinRemoved = new DaggerSignal(); // void pinRemoved(QUuid pinID);
this.pinAdded = new DaggerSignal(); // void pinAdded(QDaggerBasePin * pin);
}
/**
* Get the topology system this pin collection belongs to.
* @returns {number}
*/
get topologySystem() {
return this._topology_system;
}
/**
* Find and return a pin with the given name.
* @param {string} withName
* @returns {DaggerBasePin}
*/
pin(withName) {
if(this._pinCollection.hasOwnProperty(withName)) {
return this._pinCollection[withName];
}
return null;
}
/**
* Add a given pin to this collection.
* @param {DaggerBasePin} pin
* @param {string} name
* @returns {boolean}
*/
addPin(pin, name) {
if(!pin)
return false;
// set me as the pin's parent
pin.parent = this;
if(name != "") {
pin.pinName = name;
} else if(pin.pinName != "") {
// the MUST have a name
pin.pinName = pin.instanceID;
}
if(this._pinCollection.hasOwnProperty(pin.pinName)) {
// remove all numerals and try to find a pin name that doesn't exist
let nn = pin.pinName.replace(/[0123456789]/g, '');
let cc = 0;
while(1) {
let an = nn + cc;
if(!this._pinCollection.hasOwnProprty(an))
break;
cc++;
}
pin.pinName = an = nn + cc;;
}
pin.parentNode = this._parentNode;
// just stick it at the end
this._pinCollection[pin.pinName] = pin;
this._orderedCollection.push(pin);
// signal that we added a pin
this.pinAdded.emit(pin);
return true;
}
/**
* Set the name for a given pin. If the pin's name has already been set, and the pin isn't allowed to be renamed,
* this will return 'false'
* @param {DaggerBasePin} pin
* @param {string} name
* @returns {boolean}
*/
setPinName(pin, name) {
if(pin.name == name)
return true;
if(this.pin(name) != null) {
// we already have a pin with this name
return false;
}
// change the pin name in the collection map first
delete this._pinCollection[pin.pinName];
this._pinCollection[name] = pin;
// change the pin's name
pin.pinName = name;
return true;
}
/**
* Remove a given pin from this collection.
* @param {DaggerBasePin} pin
*/
removePin(pin) {
if(this._orderedCollection.includes(pin) && !pin.isConnected) {
// ask my parent node if I can be removed first
if(this._parentNode && !this._parentNode.canRemovePin(pin)) {
return false;
}
let pinid = pin.instanceID;
delete this._pinCollection[pinid];
let index = this._orderedCollection.indexOf(pin);
if(index > -1) {
this._orderedCollection.splice(index, 1);
}
}
return false;
}
/**
* Get the index of the given pin in the collection
* @param {DaggerBasePin} pin
* @returns {number}
*/
index(pin) {
return this._orderedCollection.indexOf(pin);
}
/**
* get the parent node (same result as DaggerBase.parent)
*/
get parentNode() {
return this.parent;
}
/**
* get the direction of pins in this collection
*/
get pinDirection() {
return this._direction;
}
/**
* Get list of all the pins in the collection
* @returns {array}
*/
get allPins() {
// return a copy, not the actual array
return this._orderedCollection.slice();
}
/**
* Get list of all pins in the collection that are not connected.
* @returns {array}
*/
get allNonConnectedPins() {
let retv = [];
for(let pin of this._orderedCollection) {
if(!pin.isConnected) {
retv.push(pin);
}
}
return retv;
}
/**
* Get list of all pins in the collection that are connected.
* @returns {array}
*/
get allConnectedPins() {
let retv = [];
for(let pin of this._orderedCollection) {
if(pin.isConnected) {
retv.push(pin);
}
}
return retv;
}
/**
* Clean up when object hierarchy is unraveled. Subclasses should always super.
*/
purgeAll() {
for(let pin of this._orderedCollection)
{
pin.purgeAll();
}
this._pinCollection = [];
this._orderedCollection = [];
super.purgeAll();
}
/**
* Find the first unconnected pin or null if no pins are unconnected.
* @returns {DaggerBasePin}
*/
get firstUnconnectedPin() {
for(let p of this._orderedCollection) {
if(!p.isConnected)
return p;
}
return null;
}
}
module.exports = {
DaggerPinCollection: DaggerPinCollection
};