|
| 1 | +import { logicInput, logicOutput, gate, flipflop, logicClock, srLatch, wireMng } from "./simulator.js" |
| 2 | +import { LogicInput } from "./circuit_components/LogicInput.js" |
| 3 | +import { LogicOutput } from "./circuit_components/LogicOutput.js"; |
| 4 | +import { Clock } from "./circuit_components/Clock.js"; |
| 5 | +import { Gate } from "./circuit_components/Gate.js"; |
| 6 | +import { Integrated } from "./circuit_components/Integrated.js"; |
| 7 | +import { IC_type } from "./circuit_components/Enums.js"; |
| 8 | +import { FF_D_Single, FF_D_MasterSlave } from "./circuit_components/FF_D.js"; |
| 9 | +import { FF_T } from "./circuit_components/FF_T.js"; |
| 10 | +import { FF_JK } from "./circuit_components/FF_JK.js"; |
| 11 | +import { SR_LatchAsync, SR_LatchSync, SR_Latch } from "./circuit_components/SR_Latch.js"; |
| 12 | +import { nodeList } from "./circuit_components/Node.js"; |
| 13 | + |
| 14 | +let eventHistory = []; |
| 15 | + |
| 16 | +/** |
| 17 | + * @todo TODO |
| 18 | + */ |
| 19 | +export class FileManager { |
| 20 | + |
| 21 | + /** |
| 22 | + * @todo TODO |
| 23 | + */ |
| 24 | + constructor() |
| 25 | + { |
| 26 | + this.isLoadingState = false; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @todo TODO |
| 31 | + */ |
| 32 | + saveState() { |
| 33 | + /* TODO |
| 34 | + if(this.isLoadingState) |
| 35 | + return; |
| 36 | + |
| 37 | + eventHistory.unshift(FileManager.getJSON_Workspace()); |
| 38 | + if (eventHistory.length > 10) { |
| 39 | + delete eventHistory[10]; |
| 40 | + eventHistory.length = 10; |
| 41 | + } |
| 42 | + console.log(eventHistory);*/ |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @todo TODO |
| 47 | + */ |
| 48 | + loadFile(e) { |
| 49 | + this.isLoadingState = true; |
| 50 | + |
| 51 | + flipflop.splice(0, flipflop.length); |
| 52 | + srLatch.splice(0, srLatch.length); |
| 53 | + gate.splice(0, gate.length); |
| 54 | + wireMng.wire.splice(0, wireMng.wire.length); |
| 55 | + logicClock.splice(0, logicClock.length); |
| 56 | + logicInput.splice(0, logicInput.length); |
| 57 | + logicOutput.splice(0, logicOutput.length); |
| 58 | + nodeList.splice(0, nodeList.length); |
| 59 | + |
| 60 | + //this.e = e; |
| 61 | + |
| 62 | + let file = e.target.files.item(0); |
| 63 | + |
| 64 | + let reader = new FileReader(); |
| 65 | + reader.onload = function () { |
| 66 | + |
| 67 | + let contentFile = reader.result; |
| 68 | + //console.log(contentFile); |
| 69 | + |
| 70 | + // logic input |
| 71 | + if ("logicInput" in JSON.parse(contentFile)) { |
| 72 | + for (let i = 0; i < contentFile.length; i++) { |
| 73 | + let objectParsed = JSON.parse(contentFile).logicInput[i]; |
| 74 | + |
| 75 | + if (objectParsed == undefined) |
| 76 | + break; |
| 77 | + |
| 78 | + console.log(objectParsed); |
| 79 | + logicInput.push(new LogicInput()); |
| 80 | + Object.assign(logicInput[i], objectParsed); |
| 81 | + logicInput[i].refreshNodes(); |
| 82 | + } |
| 83 | + } |
| 84 | + // logic output |
| 85 | + //console.log(logicOutput); |
| 86 | + if ("logicOutput" in JSON.parse(contentFile)) { |
| 87 | + for (let i = 0; i < contentFile.length; i++) { |
| 88 | + |
| 89 | + let objectParsed = JSON.parse(contentFile).logicOutput[i]; |
| 90 | + |
| 91 | + if (objectParsed == undefined) |
| 92 | + break; |
| 93 | + |
| 94 | + console.log(objectParsed); |
| 95 | + logicOutput.push(new LogicOutput()); |
| 96 | + Object.assign(logicOutput[i], objectParsed); |
| 97 | + logicOutput[i].refreshNodes(); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if ("logicClock" in JSON.parse(contentFile)) { |
| 102 | + for (let i = 0; i < contentFile.length; i++) { |
| 103 | + |
| 104 | + let objectParsed = JSON.parse(contentFile).logicClock[i]; |
| 105 | + |
| 106 | + if (objectParsed == undefined) |
| 107 | + break; |
| 108 | + |
| 109 | + console.log(objectParsed); |
| 110 | + logicClock.push(new Clock()); |
| 111 | + Object.assign(logicClock[i], objectParsed); |
| 112 | + logicClock[i].refreshNodes(); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if ("gate" in JSON.parse(contentFile)) { |
| 117 | + for (let i = 0; i < contentFile.length; i++) { |
| 118 | + |
| 119 | + let objectParsed = JSON.parse(contentFile).gate[i]; |
| 120 | + |
| 121 | + if (objectParsed == undefined) |
| 122 | + break; |
| 123 | + |
| 124 | + console.log(objectParsed); |
| 125 | + gate.push(new Gate(JSON.parse(contentFile).gate[i].strType)); |
| 126 | + Object.assign(gate[i], objectParsed); |
| 127 | + gate[i].refreshNodes(); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + if ("srLatch" in JSON.parse(contentFile)) { |
| 132 | + for (let i = 0; i < contentFile.length; i++) { |
| 133 | + |
| 134 | + let objectParsed = JSON.parse(contentFile).srLatch[i]; |
| 135 | + |
| 136 | + if (objectParsed == undefined) |
| 137 | + break; |
| 138 | + |
| 139 | + console.log(objectParsed); |
| 140 | + |
| 141 | + switch (JSON.parse(contentFile).srLatch[i].type) { |
| 142 | + case IC_type.SR_LATCH_ASYNC: |
| 143 | + srLatch.push(new SR_LatchAsync(JSON.parse(contentFile).srLatch[i].gateType, |
| 144 | + JSON.parse(contentFile).srLatch[i].stabilize)); |
| 145 | + break; |
| 146 | + case IC_type.SR_LATCH_SYNC: |
| 147 | + srLatch.push(new SR_LatchSync(JSON.parse(contentFile).srLatch[i].gateType, |
| 148 | + JSON.parse(contentFile).srLatch[i].stabilize)); |
| 149 | + break; |
| 150 | + } |
| 151 | + Object.assign(srLatch[i], objectParsed); |
| 152 | + srLatch[i].refreshNodes(); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + if ("flipflop" in JSON.parse(contentFile)) { |
| 157 | + for (let i = 0; i < contentFile.length; i++) { |
| 158 | + |
| 159 | + let objectParsed = JSON.parse(contentFile).flipflop[i]; |
| 160 | + |
| 161 | + if (objectParsed == undefined) |
| 162 | + break; |
| 163 | + |
| 164 | + console.log(objectParsed); |
| 165 | + |
| 166 | + switch (JSON.parse(contentFile).flipflop[i].type) { |
| 167 | + case IC_type.FF_D_SINGLE: |
| 168 | + flipflop.push(new FF_D_Single(JSON.parse(contentFile).flipflop[i].type)); |
| 169 | + break; |
| 170 | + case IC_type.FF_D_MASTERSLAVE: |
| 171 | + flipflop.push(new FF_D_MasterSlave(JSON.parse(contentFile).flipflop[i].type)); |
| 172 | + break; |
| 173 | + case IC_type.FF_T: |
| 174 | + flipflop.push(new FF_T(JSON.parse(contentFile).flipflop[i].type)); |
| 175 | + break; |
| 176 | + case IC_type.FF_JK: |
| 177 | + flipflop.push(new FF_JK(JSON.parse(contentFile).flipflop[i].type)); |
| 178 | + break; |
| 179 | + } |
| 180 | + Object.assign(flipflop[i], objectParsed); |
| 181 | + flipflop[i].refreshNodes(); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + if ("wire" in JSON.parse(contentFile)) { |
| 186 | + for (let i = 0; i < contentFile.length; i++) { |
| 187 | + let objectParsed = JSON.parse(contentFile).wire[i]; |
| 188 | + |
| 189 | + if (objectParsed == undefined) |
| 190 | + break; |
| 191 | + |
| 192 | + console.log(objectParsed); |
| 193 | + |
| 194 | + wireMng.addNode(nodeList[objectParsed.startID]); |
| 195 | + wireMng.addNode(nodeList[objectParsed.endID]); |
| 196 | + //Object.assign(gate[i], objectParsed); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + }; |
| 201 | + reader.readAsText(file); |
| 202 | + } |
| 203 | + |
| 204 | + |
| 205 | + /** |
| 206 | + * @todo TODO |
| 207 | + */ |
| 208 | + saveFile(e) { |
| 209 | + |
| 210 | + let jsonWorkspace = FileManager.getJSON_Workspace(); |
| 211 | + let blob = new Blob([jsonWorkspace], { type: 'application/json' }); |
| 212 | + saveProjectFile.href = URL.createObjectURL(blob); |
| 213 | + //console.log(jsonWorkspace); |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * @todo TODO |
| 218 | + */ |
| 219 | + static getJSON_Workspace() { |
| 220 | + let workspace = new Object(); |
| 221 | + |
| 222 | + workspace["logicInput"] = logicInput; |
| 223 | + workspace["logicOutput"] = logicOutput; |
| 224 | + workspace["flipflop"] = flipflop; |
| 225 | + workspace["logicClock"] = logicClock; |
| 226 | + workspace["gate"] = gate; |
| 227 | + workspace["srLatch"] = srLatch; |
| 228 | + workspace["wire"] = wireMng.wire; |
| 229 | + |
| 230 | + let jsonWorkspace = JSON.stringify(workspace, |
| 231 | + function (key, value) { |
| 232 | + switch (key) { |
| 233 | + case "output": |
| 234 | + case "input": |
| 235 | + case "nodeSet": |
| 236 | + case "nodeReset": |
| 237 | + case "nodeClock": |
| 238 | + case "nodeD": |
| 239 | + case "nodeT": |
| 240 | + case "nodeJ": |
| 241 | + case "nodeK": |
| 242 | + case "nodeQ": |
| 243 | + case "nodeNotQ": |
| 244 | + case "andGate_NotQ": |
| 245 | + case "andGate_Q": |
| 246 | + case "ff_D": |
| 247 | + case "orGate": |
| 248 | + case "gateSet": |
| 249 | + case "gateReset": |
| 250 | + case "asyncLatch": |
| 251 | + case "master": |
| 252 | + case "slave": |
| 253 | + case "srLatchSync": |
| 254 | + case "startNode": |
| 255 | + case "endNode": |
| 256 | + return undefined; |
| 257 | + } |
| 258 | + |
| 259 | + // other things which is not possible to export on JSON |
| 260 | + return value; |
| 261 | + }, '\t'); |
| 262 | + return jsonWorkspace; |
| 263 | + } |
| 264 | +} |
0 commit comments