[0c6b92a] | 1 | import MapNode from "../base/MapNode.js";
|
---|
| 2 | import ShapeQuery from "./ShapeQuery.js";
|
---|
| 3 | // TODO abstraktna klasa ova da stanit i za da napram za view registry, za da mozis search na floors globalno
|
---|
| 4 |
|
---|
| 5 | class ShapeRegistry {
|
---|
| 6 | constructor() {
|
---|
| 7 | if (!ShapeRegistry.instance) {
|
---|
| 8 | this.store = {
|
---|
| 9 | floors: {} // key brojka 0: shappes arr
|
---|
| 10 | };
|
---|
| 11 | ShapeRegistry.instance = this;
|
---|
| 12 | }
|
---|
| 13 |
|
---|
| 14 | return ShapeRegistry.instance;
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 | /**
|
---|
| 18 | * @description Get all the shapes from a floor if floorNumber is specified.
|
---|
| 19 | * @description If floorNumber is not specified, returns all the shapes from every floor.
|
---|
| 20 | * @param floorNumber
|
---|
| 21 | * @returns {[{}]} [shapes]
|
---|
| 22 | */
|
---|
| 23 | getShapes(floorNumber = null) {
|
---|
| 24 | if (!this.store.floors[floorNumber]) {
|
---|
| 25 | this.store.floors[floorNumber] = [];
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | if (floorNumber != null) {
|
---|
| 29 | return this.store.floors[floorNumber];
|
---|
| 30 | } else {
|
---|
| 31 | return Object.values(this.store.floors).flat();
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | /**
|
---|
| 38 | * Adds a shape to a floor that corresponds the shape's internally stored floor number.
|
---|
| 39 | * @param shape
|
---|
| 40 | */
|
---|
| 41 | add(shape) {
|
---|
| 42 | console.log("floor: " + shape.floorNum)
|
---|
| 43 | if (!this.store.floors[shape.floorNum]) {
|
---|
| 44 | this.store.floors[shape.floorNum] = [];
|
---|
| 45 | }
|
---|
| 46 | this.store.floors[shape.floorNum].push(shape);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | /**
|
---|
| 50 | * Deletes a shape from the floor corresponds the shape's internally stored floor number
|
---|
| 51 | * @param shape
|
---|
| 52 | */
|
---|
| 53 | delete(shape) {
|
---|
| 54 | const floorShapes = this.store.floors[shape.floorNum];
|
---|
| 55 | if (floorShapes) {
|
---|
| 56 | const index = floorShapes.indexOf(shape);
|
---|
| 57 | if (index !== -1) {
|
---|
| 58 | floorShapes.splice(index, 1);
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | * @param floorNumber Clears all shapes from the specified floor
|
---|
| 65 | */
|
---|
| 66 | clear(floorNumber = null) {
|
---|
| 67 |
|
---|
| 68 | if(floorNumber != null){
|
---|
| 69 | const floor = this.store.floors[floorNumber];
|
---|
| 70 | if (Array.isArray(floor)) {
|
---|
| 71 | floor.forEach(shape => shape.destroy());
|
---|
| 72 | this.store.floors[floorNumber] = [];
|
---|
| 73 | }
|
---|
| 74 | } else {
|
---|
| 75 | Object.keys(this.store.floors).forEach(floorNumber => {
|
---|
| 76 | const floor = this.store.floors[floorNumber];
|
---|
| 77 | if (Array.isArray(floor)) {
|
---|
| 78 | floor.forEach(shape => shape.destroy());
|
---|
| 79 | this.store.floors[floorNumber] = [];
|
---|
| 80 | }
|
---|
| 81 | })
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 |
|
---|
| 85 |
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | // ova mozit optimizacija da ne pret na site spratoj update celo vreme, samo ko ke povrzvis stairs da pret na site
|
---|
| 89 | updateConnections() {
|
---|
| 90 | Object.values(this.store.floors).forEach(floorShapes => {
|
---|
| 91 | floorShapes.forEach(shape => {
|
---|
| 92 | if (shape.className === "InfoPin" || shape.className === "Entrance" || shape.className === "Stairs") {
|
---|
| 93 | shape.info.selectedPins.forEach((connectedShapeName) => {
|
---|
| 94 | const connectedShape = floorShapes.find(s => s.info.name === connectedShapeName);
|
---|
| 95 | if (
|
---|
| 96 | connectedShape &&
|
---|
| 97 | (connectedShape.className === "InfoPin" || connectedShape.className === "Entrance" || connectedShape.className === "Stairs")
|
---|
| 98 | ) {
|
---|
| 99 | if (!connectedShape.info.selectedPins.includes(shape.info.name)) {
|
---|
| 100 | connectedShape.info.selectedPins.push(shape.info.name);
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | });
|
---|
| 104 | }
|
---|
| 105 | });
|
---|
| 106 | });
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | drawConnection(node1Name, node2Name) {
|
---|
| 110 | const node1 = Object.values(this.store.floors).flat()
|
---|
| 111 | .find(shape => shape instanceof MapNode && shape.info.name === node1Name);
|
---|
| 112 | const node2 = Object.values(this.store.floors).flat()
|
---|
| 113 | .find(shape => shape instanceof MapNode && shape.info.name === node2Name);
|
---|
| 114 |
|
---|
| 115 | console.log("NODE1: " + JSON.stringify(node1))
|
---|
| 116 | console.log("NODE2: " + JSON.stringify(node2));
|
---|
| 117 |
|
---|
| 118 | if (node1 && node2) {
|
---|
| 119 | node1.connect(node2);
|
---|
| 120 | console.log("N1",node1.info.selectedPins,"N2",node2.info.selectedPins)
|
---|
| 121 | } else {
|
---|
| 122 | console.error("Cant find node1 or node 2.","Node1: " + node1,"Node2: " + node2);
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 |
|
---|
| 127 | removeConnection(from, to) {
|
---|
| 128 | let shapes = Object.values(this.store.floors).flat();
|
---|
| 129 |
|
---|
| 130 | let node1 = ShapeQuery.findNodeByName(shapes, from);
|
---|
| 131 | let node2 = ShapeQuery.findNodeByName(shapes, to);
|
---|
| 132 |
|
---|
| 133 | if(node1.floorNum === node2.floorNum)
|
---|
| 134 | node1.removeConnectionLine(node2);
|
---|
| 135 |
|
---|
| 136 | //node1.removeConnection(node2);
|
---|
| 137 |
|
---|
| 138 | Object.values(this.store.floors).flat().filter(s => s.info.name === from || s.info.name === to)
|
---|
| 139 | .forEach(s => {
|
---|
| 140 | if(s.info.name === from){
|
---|
| 141 | s.info.selectedPins = s.info.selectedPins.filter(pin => pin !== to);
|
---|
| 142 | } else {
|
---|
| 143 | s.info.selectedPins = s.info.selectedPins.filter(pin => pin !== from);
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | });
|
---|
| 147 | console.log("Remove");
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | saveDetails() {
|
---|
| 151 | Object.values(this.store.floors).flat().forEach(shape => {
|
---|
| 152 | shape.saveShapeDetails();
|
---|
| 153 | console.log(shape.info);
|
---|
| 154 | });
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | /**
|
---|
| 159 | * A singleton instance that holds all the shapes for all the floors in the map builder.
|
---|
| 160 | * Every operation of adding, removing or updating drawn shapes should reference this instance.
|
---|
| 161 | */
|
---|
| 162 | const instance = new ShapeRegistry();
|
---|
| 163 | Object.freeze(instance);
|
---|
| 164 |
|
---|
| 165 | export default instance;
|
---|