diff --git a/index.js b/index.js index b5a98ff..61d5de5 100644 --- a/index.js +++ b/index.js @@ -1,50 +1,9 @@ var gui = require('gui'); var fs = require('fs'); -var rh = 14; -var sp = 4; -var y = sp; - - -let projectData = [ - { - name: "Current", - entries: [ - { - date: 1704067200000, - subject: '(Savings) Initial Deposit', - amount: -500, - targetAccount: "Savings" - }, - { - date: 1704067200000, - subject: 'Salary', - amount: 2182.15 - }, - { - date: 1704153600000, - subject: 'Rent', - amount: -800 - }, - { - date: 1704240000000, - subject: 'Groceries', - amount: -24.19 - } - ], - }, - { - name: "Savings", - entries: [ - { - date: 1704067200000, - subject: 'Initial Deposit', - amount: 500 - } - ], - } - -]; + var rh = 14; + var sp = 4; + var y = sp; function getAccounts(data) { const resultData = []; @@ -101,7 +60,7 @@ function subWindow(data) { } let d = new Date(innerData.date); - let formattedDate = d.getDay() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear(); + let formattedDate = (""+d.getDay()).padStart(2, "0") + "-" + (""+(d.getMonth() + 1)).padStart(2, "0") + "-" + d.getFullYear(); gadgets.push({ kind: 'string', id: 1, label: 'Subject:', @@ -187,7 +146,11 @@ function subWindow(data) { } } -function mainWindow() { +function mainWindow(projectData) { + + let internalProjectData = projectData; + + let invalidate = false; let gadgets = []; @@ -201,7 +164,7 @@ function mainWindow() { gadgets.push({ kind: 'cycle', id: cycleId, left: sp, top: y, width: 120, height: rh, border: false, - items: getAccounts(projectData), value: 0 + items: getAccounts(internalProjectData), value: 0 }); gadgets.push({ kind: 'button', id: addButtonId, left: 500 - 80 - sp, top: y, width: 80, label: "+Transfer", height: rh }); @@ -209,18 +172,18 @@ function mainWindow() { gadgets.push({ kind: 'listview', id: listViewId, left: sp, top: y + rh + sp, width: 500 - sp - sp, height: 186, flex: true, - items: toListViewEntries(projectData[0].entries, cols), value: 0 + items: toListViewEntries(internalProjectData[0].entries, cols), value: 0 }); gadgets.push({ kind: 'text', id: balanceId, label: 'Balance:', left: 100, top: y + rh + sp + 186, width: 400 - sp, height: rh, - value: getSummation(projectData[0].entries).toFixed(2) + value: getSummation(internalProjectData[0].entries).toFixed(2) }); - var win = gui.createWindow({ + let win = gui.createWindow({ title: 'Budget', width: 500, height: 14 + 4 + 4 + 200 + 4, @@ -245,98 +208,104 @@ function mainWindow() { let currentAccountIndex = 0; - function updateView() { - gui.set(win, listViewId, toListViewEntries(projectData[currentAccountIndex].entries, cols)); - gui.set(win, balanceId, getSummation(projectData[currentAccountIndex].entries).toFixed(2)); + let updateView = (g, w, accountIndex, data) => { + g.set(w, 2, toListViewEntries(data[accountIndex].entries, cols)); + g.set(w, 3, getSummation(data[accountIndex].entries).toFixed(2)); } - function doNew() { - projectData = [ + let doNew = () => { + invalidate = true; + return [ { name: "Current", entries: [] } ]; - - - - currentAccountIndex = 0; - gui.set(win, cycleId, getAccounts(projectData)); - gui.set(win, cycleId, 0); - updateView(); } - function doOpen() { + let doOpen = () => { var r = gui.fileRequest({ title: 'Open File', pattern: '#?.json' }); if (r) { - projectData = JSON.parse(fs.readFileSync(r.file)); + return JSON.parse(fs.readFileSync(r.file)); } + invalidate = false; } function doSave() { var r = gui.fileRequest({ title: 'Save File', pattern: '#?.json', save: true }); if (r) { - fs.writeFileSync(r.file, JSON.stringify(projectData)); + fs.writeFileSync(r.file, JSON.stringify(internalProjectData)); } } - while (true) { + + + while (!invalidate) { var evt = gui.waitEvent(win); if (!evt) continue; - if (evt.type === 'close') break; + if (evt.type === 'close') { + invalidate = true; + internalProjectData = null; + } if (evt.type === 'menu') { - if (evt.id === 101) doNew(); - else if (evt.id === 102) doOpen(); + if (evt.id === 101) { + internalProjectData = doNew(); + break; + } + else if (evt.id === 102) { + internalProjectData = doOpen(); + break; + } else if (evt.id === 103) doSave(); - else if (evt.id === 199) break; + else if (evt.id === 199) { + invalidate = true; + internalProjectData = null; + } } if (evt.type === 'gadgetup') { if (evt.id === cycleId) { currentAccountIndex = evt.code; - updateView(); - } - - if (evt.id === listViewId) { - const entry = subWindow(projectData[currentAccountIndex].entries[evt.code]); + updateView(gui, win, currentAccountIndex, internalProjectData); + } else if (evt.id === listViewId) { + const entry = subWindow(internalProjectData[currentAccountIndex].entries[evt.code]); if (entry != null) { if (entry === -1) { - projectData[currentAccountIndex].entries.splice(evt.code, 1); + internalProjectData[currentAccountIndex].entries.splice(evt.code, 1); } else { - projectData[currentAccountIndex].entries[evt.code] = entry; + internalProjectData[currentAccountIndex].entries[evt.code] = entry; } if (entry.targetAccount) { - const targetAccount = projectData.find(d => d.name === entry.targetAccount); + const targetAccount = internalProjectData.find(d => d.name === entry.targetAccount); if (targetAccount) { targetAccount.entries.push({ date: entry.date, - subject: "(" + projectData[currentAccountIndex].name + ")" + entry.subject, + subject: "(" + internalProjectData[currentAccountIndex].name + ")" + entry.subject, amount: -entry.amount }); } } - updateView(); + updateView(gui, win, currentAccountIndex, internalProjectData); } - } - if (evt.id === addButtonId) { + } else if (evt.id === addButtonId) { const entry = subWindow(); if (entry != null) { - projectData[currentAccountIndex].entries.push(entry); + internalProjectData[currentAccountIndex].entries.push(entry); if (entry.targetAccount) { - const targetAccount = projectData.find(d => d.name === entry.targetAccount); + const targetAccount = internalProjectData.find(d => d.name === entry.targetAccount); if (targetAccount) { targetAccount.entries.push({ date: entry.date, - subject: "(" + projectData[currentAccountIndex].name + ")" + entry.subject, + subject: "(" + internalProjectData[currentAccountIndex].name + ")" + entry.subject, amount: -entry.amount }); } } - updateView(); + updateView(gui, win, currentAccountIndex, internalProjectData); } } } @@ -344,6 +313,52 @@ function mainWindow() { gui.closeWindow(win); + return internalProjectData; } -mainWindow(); \ No newline at end of file + + let projectData = [ + { + name: "Current", + entries: [ + { + date: 1704067200000, + subject: '(Savings) Initial Deposit', + amount: -500, + targetAccount: "Savings" + }, + { + date: 1704067200000, + subject: 'Salary', + amount: 2182.15 + }, + { + date: 1704153600000, + subject: 'Rent', + amount: -800 + }, + { + date: 1704240000000, + subject: 'Groceries', + amount: -24.19 + } + ], + }, + { + name: "Savings", + entries: [ + { + date: 1704067200000, + subject: 'Initial Deposit', + amount: 500 + } + ], + } + + ]; + + +do { + projectData = mainWindow(projectData); +} while(projectData != null); +console.log("bye!"); \ No newline at end of file