@packtrack/layout 1.0.2 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/.built/district.d.ts +1 -1
  2. package/.built/district.js +2 -2
  3. package/.built/source/device/channel.d.ts +8 -0
  4. package/.built/source/device/channel.js +16 -0
  5. package/.built/source/device/device.d.ts +11 -0
  6. package/.built/source/device/device.js +24 -0
  7. package/.built/source/district.d.ts +20 -0
  8. package/.built/source/district.js +86 -0
  9. package/.built/source/layout.d.ts +29 -0
  10. package/.built/source/layout.js +286 -0
  11. package/.built/source/position.d.ts +10 -0
  12. package/.built/source/position.js +33 -0
  13. package/.built/source/power-district.d.ts +8 -0
  14. package/.built/source/power-district.js +18 -0
  15. package/.built/source/route.d.ts +10 -0
  16. package/.built/source/route.js +17 -0
  17. package/.built/source/router.d.ts +14 -0
  18. package/.built/source/router.js +40 -0
  19. package/.built/source/section.d.ts +40 -0
  20. package/.built/source/section.js +217 -0
  21. package/.built/source/tile.d.ts +57 -0
  22. package/.built/source/tile.js +78 -0
  23. package/.built/source/track.d.ts +12 -0
  24. package/.built/source/track.js +29 -0
  25. package/index.ts +9 -399
  26. package/package.json +1 -1
  27. package/{district.ts → source/district.ts} +1 -1
  28. package/source/layout.ts +399 -0
  29. package/tsconfig.json +2 -1
  30. package/.built/{index.d.ts → layout.d.ts} +0 -0
  31. package/.built/{index.js → layout.js} +1 -1
  32. /package/{device → source/device}/channel.ts +0 -0
  33. /package/{device → source/device}/device.ts +0 -0
  34. /package/{position.ts → source/position.ts} +0 -0
  35. /package/{positioner → source/positioner}/index.ts +0 -0
  36. /package/{positioner → source/positioner}/point.ts +0 -0
  37. /package/{positioner → source/positioner}/responder-type.ts +0 -0
  38. /package/{power-district.ts → source/power-district.ts} +0 -0
  39. /package/{route.ts → source/route.ts} +0 -0
  40. /package/{router.ts → source/router.ts} +0 -0
  41. /package/{section.ts → source/section.ts} +0 -0
  42. /package/{tile.ts → source/tile.ts} +0 -0
  43. /package/{track.ts → source/track.ts} +0 -0
package/index.ts CHANGED
@@ -1,399 +1,9 @@
1
- import { District } from "./district";
2
- import { PowerDistrict } from "./power-district";
3
- import { Route } from "./route";
4
- import { Router } from "./router";
5
- import { Section } from "./section";
6
- import { TilePattern, Tile } from "./tile";
7
- import { Track } from "./track";
8
- import { PointPositioner } from "./positioner/point";
9
- import { Device } from "./device/device";
10
- import { ResponderType } from "./positioner/responder-type";
11
- import { Channel } from "./device/channel";
12
-
13
- export class Layout {
14
- name: string;
15
-
16
- districts: District[] = [];
17
-
18
- devices: Device[] = [];
19
- responderType: ResponderType[] = [];
20
-
21
- get allDistricts() {
22
- const districts: District[] = [];
23
-
24
- function walkDistrict(district: District) {
25
- districts.push(district);
26
-
27
- for (let child of district.children) {
28
- walkDistrict(child);
29
- }
30
- }
31
-
32
- for (let district of this.districts) {
33
- walkDistrict(district);
34
- }
35
-
36
- return districts;
37
- }
38
-
39
- static from(document: any) {
40
- const layout = new Layout();
41
-
42
- const railway = document.firstChild!;
43
- layout.name = railway.getAttribute('name');
44
-
45
- const version = railway.getAttribute('version');
46
-
47
- if (version == '1') {
48
- let district = railway.firstChild;
49
-
50
- while (district) {
51
- if (district.tagName == 'district') {
52
- layout.districts.push(layout.loadDistrict(district, layout));
53
- }
54
-
55
- district = district.nextSibling;
56
- }
57
-
58
- district = railway.firstChild;
59
- let index = 0;
60
-
61
- while (district) {
62
- if (district.tagName == 'district') {
63
- layout.linkDistrict(district, layout.districts[index]);
64
-
65
- index++;
66
- }
67
-
68
- district = district.nextSibling;
69
- }
70
- } else {
71
- throw new Error(`unsupported railway definition file version '${version}'`);
72
- }
73
-
74
- return layout;
75
- }
76
-
77
- loadDistrict(source, parent: District | Layout) {
78
- const district = new District(source.getAttribute('name'), parent);
79
-
80
- let child = source.firstChild;
81
-
82
- while (child) {
83
- if (child.tagName == 'power-districts') {
84
- let powerDistrict = child.firstChild;
85
-
86
- while (powerDistrict) {
87
- if (powerDistrict.tagName == 'power-district') {
88
- district.powerDistricts.push(this.loadPowerDistrict(powerDistrict, district));
89
- }
90
-
91
- powerDistrict = powerDistrict.nextSibling;
92
- }
93
- }
94
-
95
- if (child.tagName == 'section') {
96
- this.loadSection(child, district);
97
- }
98
-
99
- if (child.tagName == 'router') {
100
- district.routers.push(this.loadRouter(child, district));
101
- }
102
-
103
- if (child.tagName == 'district') {
104
- district.children.push(this.loadDistrict(child, district));
105
- }
106
-
107
- child = child.nextSibling;
108
- }
109
-
110
- return district;
111
- }
112
-
113
- linkDistrict(source, district: District) {
114
- let child = source.firstChild;
115
-
116
- let sectionIndex = 0;
117
- let childIndex = 0;
118
-
119
- while (child) {
120
- if (child.tagName == 'section') {
121
- this.linkSection(child, district.sections[sectionIndex]);
122
-
123
- sectionIndex++;
124
- }
125
-
126
- if (child.tagName == 'router') {
127
- this.linkRouter(child, district.routers.find(router => router.name == child.getAttribute('name'))!);
128
- }
129
-
130
- if (child.tagName == 'district') {
131
- this.linkDistrict(child, district.children[childIndex]);
132
-
133
- childIndex++;
134
- }
135
-
136
- child = child.nextSibling;
137
- }
138
- }
139
-
140
- loadSection(source, district: District) {
141
- const section = new Section(source.getAttribute('name'), district);
142
- district.sections.push(section);
143
-
144
- let child = source.firstChild;
145
-
146
- while (child) {
147
- if (child.tagName == 'tracks') {
148
- let trackNode = child.firstChild;
149
-
150
- while (trackNode) {
151
- if (trackNode.tagName == 'track') {
152
- const track = new Track(
153
- section,
154
- +trackNode.getAttribute('length'),
155
- trackNode.getAttribute('path')
156
- );
157
-
158
- section.tracks.push(track);
159
-
160
- let trackChild = trackNode.firstChild;
161
-
162
- while (trackChild) {
163
- if (trackChild.tagName == 'positioners') {
164
- let positioner = trackChild.firstChild;
165
-
166
- while (positioner) {
167
- if (positioner.tagName == 'point') {
168
- const device = this.findDevice(positioner.getAttribute('device'));
169
- const channel = this.findChannel(device, positioner.getAttribute('channel'));
170
- const responderType = this.findResponderType(positioner.getAttribute('responder'));
171
-
172
- track.positioners.push(new PointPositioner(
173
- track,
174
- +positioner.getAttribute('offset'),
175
- channel,
176
- responderType
177
- ));
178
- }
179
-
180
- positioner = positioner.nextSibling;
181
- }
182
- }
183
-
184
- trackChild = trackChild.nextSibling;
185
- }
186
- }
187
-
188
- trackNode = trackNode.nextSibling;
189
- }
190
- }
191
-
192
- if (child.tagName == 'tile') {
193
- const pattern = child.getAttribute('pattern');
194
-
195
- if (!(pattern in TilePattern.patterns)) {
196
- throw new Error(`Unknown tile pattern '${pattern}' in tile ${section.tiles.length + 1} in ${section.domainName}`);
197
- }
198
-
199
- section.tiles.push(new Tile(section, +child.getAttribute('x'), +child.getAttribute('y'), TilePattern.patterns[pattern]))
200
- }
201
-
202
- child = child.nextSibling;
203
- }
204
- }
205
-
206
- findDevice(identifier: string) {
207
- let device = this.devices.find(device => device.identifier == identifier);
208
-
209
- if (device) {
210
- return device;
211
- }
212
-
213
- device = new Device(identifier);
214
- this.devices.push(device);
215
-
216
- return device;
217
- }
218
-
219
- findChannel(device: Device, name: string) {
220
- let channel = device.channels.find(channel => channel.name == name);
221
-
222
- if (channel) {
223
- return channel;
224
- }
225
-
226
- channel = new Channel(device, name);
227
- device.channels.push(channel);
228
-
229
- return channel;
230
- }
231
-
232
- findResponderType(name: string) {
233
- let type = this.responderType.find(type => type.name == name);
234
-
235
- if (type) {
236
- return type;
237
- }
238
-
239
- type = new ResponderType(name);
240
- this.responderType.push(type);
241
-
242
- return type;
243
- }
244
-
245
- linkSection(source, section: Section) {
246
- let child = source.firstChild;
247
-
248
- while (child) {
249
- if (child.tagName == 'out') {
250
- const out = this.findSection(child.getAttribute('section'), section.district);
251
-
252
- section.out = out;
253
- out.in = section;
254
- }
255
-
256
- child = child.nextSibling;
257
- }
258
- }
259
-
260
- findSection(path: string, base: District, source = base) {
261
- const parts = path.split('.');
262
-
263
- if (parts.length == 0) {
264
- throw `section '${path}' not found from '${source.name}': invalid name`;
265
- }
266
-
267
- if (parts.length == 1) {
268
- const localSection = base.sections.find(section => section.name == parts[0]);
269
-
270
- if (!localSection) {
271
- throw new Error(`Section '${path}' not found from '${source.name}': section does not exist in '${base.name}'`);
272
- }
273
-
274
- return localSection;
275
- }
276
-
277
- const sectionName = parts.pop()!;
278
-
279
- let pool: District | Layout = base;
280
-
281
- for (let index = 0; index < parts.length; index++) {
282
- if (pool instanceof Layout || !pool.parent) {
283
- throw new Error(`Section '${path}' could not be found from '${source.name}': district '${pool.name}' does not have a parent`);
284
- }
285
-
286
- pool = pool.parent!;
287
- }
288
-
289
- for (let part of parts) {
290
- const child = (pool instanceof District ? pool.children : pool.districts).find(child => child.name == part);
291
-
292
- if (!child) {
293
- throw new Error(`Section '${path}' could not be found from '${source.name}': district '${pool.name}' does not have a child named '${part}'`);
294
- }
295
-
296
- pool = child;
297
- }
298
-
299
- if (pool instanceof Layout) {
300
- throw new Error(`Section '${path}' could not be found from '${source.name}': a layout cannot directly include a section`);
301
- }
302
-
303
- return this.findSection(sectionName, pool, source);
304
- }
305
-
306
- loadRouter(source, district: District) {
307
- const router = new Router(source.getAttribute('name'), district);
308
-
309
- return router;
310
- }
311
-
312
- linkRouter(source, router: Router) {
313
- let child = source.firstChild;
314
-
315
- while (child) {
316
- if (child.tagName == 'route') {
317
- const route = new Route(child.getAttribute('name'), router);
318
-
319
- route.in = this.findSection(child.getAttribute('in'), router.district);
320
- route.in.out = router;
321
-
322
- route.out = this.findSection(child.getAttribute('out'), router.district);
323
- route.out.in = router;
324
-
325
- router.routes.push(route);
326
- }
327
-
328
- child = child.nextSibling;
329
- }
330
- }
331
-
332
- loadPowerDistrict(source, district: District) {
333
- const powerDistrict = new PowerDistrict(source.getAttribute('name'), district);
334
-
335
- return powerDistrict;
336
- }
337
-
338
- toDot() {
339
- let dot = 'digraph G {';
340
-
341
- for (let district of this.districts) {
342
- dot += district.toDotDefinition();
343
- }
344
-
345
- for (let district of this.districts) {
346
- dot += district.toDotConnection();
347
- }
348
-
349
- return `${dot}}`;
350
- }
351
-
352
- toSVG(inject = '') {
353
- const positons = this.districts.map(district => district.findSVGPositions()).flat(Infinity);
354
-
355
- const width = Math.max(...positons.map(position => position.x));
356
- const height = Math.max(...positons.map(position => position.y));
357
-
358
- let svg = `<svg width="100vw" height="100vh" viewBox="0 0 ${width + 1} ${height + 1}" xmlns="http://www.w3.org/2000/svg">
359
- <style>
360
-
361
- path {
362
- fill: none;
363
- stroke: #000;
364
- stroke-width: 0.2;
365
- }
366
-
367
- </style>
368
- `;
369
-
370
- for (let district of this.districts) {
371
- svg += district.toSVG();
372
- }
373
-
374
- return `${svg}${inject}</svg>`;
375
- }
376
-
377
- dump() {
378
- console.group(`Layout ${this.name}`);
379
- console.log('devices');
380
-
381
- for (let device of this.devices) {
382
- device.dump();
383
- }
384
-
385
- console.log('responder types');
386
-
387
- for (let type of this.responderType) {
388
- type.dump();
389
- }
390
-
391
- console.log('districts');
392
-
393
- for (let district of this.districts) {
394
- district.dump();
395
- }
396
-
397
- console.groupEnd();
398
- }
399
- }
1
+ export * from './source/district';
2
+ export * from './source/layout';
3
+ export * from './source/position';
4
+ export * from './source/power-district';
5
+ export * from './source/route';
6
+ export * from './source/router';
7
+ export * from './source/section';
8
+ export * from './source/tile';
9
+ export * from './source/track';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@packtrack/layout",
3
- "version": "1.0.2",
3
+ "version": "1.0.5",
4
4
  "main": ".built/index.js",
5
5
  "typings": ".built/index.d.ts",
6
6
  "scripts": {
@@ -1,4 +1,4 @@
1
- import { Layout } from ".";
1
+ import { Layout } from "./layout";
2
2
  import { PowerDistrict } from "./power-district";
3
3
  import { Router } from "./router";
4
4
  import { Section } from "./section";