122 lines
4.2 KiB
JavaScript
122 lines
4.2 KiB
JavaScript
var gui = require('gui');
|
|
const { getAccounts, toDateByComponents } = require('./utils');
|
|
|
|
function waitForDataEntryWindow(data, projectData) {
|
|
var rh = 14;
|
|
var sp = 4;
|
|
var y = sp;
|
|
let gadgets = [];
|
|
let removable = false;
|
|
|
|
const subjectInputId = 401;
|
|
const amountInputId = 402;
|
|
const dateInputId = 403;
|
|
const transferCheckboxId = 404;
|
|
const transferAccountInputId = 405;
|
|
const executeButtonId = 406;
|
|
const deleteButtonId = 407;
|
|
|
|
const innerData = {
|
|
date: Date.now(),
|
|
subject: '',
|
|
amount: 0,
|
|
targetAccount: null
|
|
};
|
|
|
|
if (data) {
|
|
innerData.date = data.date;
|
|
innerData.subject = data.subject;
|
|
innerData.amount = data.amount;
|
|
innerData.targetAccount = data.targetAccount;
|
|
removable = true;
|
|
}
|
|
|
|
let d = new Date(innerData.date);
|
|
let formattedDate = ("" + d.getDate()).padStart(2, "0") + "-" + ("" + (d.getMonth() + 1)).padStart(2, "0") + "-" + d.getFullYear();
|
|
|
|
gadgets.push({
|
|
kind: 'string', id: subjectInputId, label: 'Subject:',
|
|
left: sp + 80, top: y, width: 400 - sp - 80 - sp, height: rh,
|
|
value: innerData.subject
|
|
});
|
|
gadgets.push({
|
|
kind: 'string', id: amountInputId, label: 'Amount:',
|
|
left: sp + 80, top: sp + rh + sp, width: 100, height: rh,
|
|
value: innerData.amount.toFixed(2)
|
|
});
|
|
gadgets.push({
|
|
kind: 'string', id: dateInputId, label: 'Date:',
|
|
left: sp + 80, top: sp + rh + sp + rh + sp, width: 150, height: rh,
|
|
value: formattedDate
|
|
});
|
|
gadgets.push({ kind: 'checkbox', id: transferCheckboxId, label: "Account:", left: sp + 80, top: sp + rh + sp + rh + sp + rh + sp + 2, width: rh, height: rh, value: 0 });
|
|
|
|
const accounts = getAccounts(projectData);
|
|
|
|
gadgets.push({ kind: 'cycle', id: transferAccountInputId, left: sp + 160 + rh + sp, top: sp + rh + sp + rh + sp + rh + sp, width: 120, height: rh, items: accounts, value: 0 });
|
|
|
|
gadgets.push({ kind: 'button', id: executeButtonId, left: sp + 80, top: sp + rh + sp + rh + sp + rh + sp + rh + sp, width: 80, label: "Save", height: rh });
|
|
gadgets.push({ kind: 'button', id: deleteButtonId, left: sp + 80 + sp + 80, top: sp + rh + sp + rh + sp + rh + sp + rh + sp, width: 80, label: "Remove", height: rh });
|
|
|
|
|
|
let win = gui.createWindow({
|
|
title: 'Entry Details',
|
|
width: 400,
|
|
height: sp + rh + sp + rh + sp + rh + sp + rh + sp + rh + sp,
|
|
left: 30,
|
|
top: 30,
|
|
gadgets: gadgets
|
|
});
|
|
|
|
if (innerData.targetAccount) {
|
|
gui.set(win, transferCheckboxId, 1);
|
|
let i = -1;
|
|
for (i = 0; i < accounts.length; i++) {
|
|
if (accounts[i] === innerData.targetAccount) {
|
|
break;
|
|
}
|
|
}
|
|
gui.set(win, 7, i);
|
|
gui.setDisabled(win, transferAccountInputId, false);
|
|
} else {
|
|
gui.setDisabled(win, transferAccountInputId, true);
|
|
}
|
|
|
|
gui.setDisabled(win, deleteButtonId, !removable);
|
|
|
|
|
|
while (true) {
|
|
var evt = gui.waitEvent(win);
|
|
if (!evt) continue;
|
|
|
|
if (evt.type === 'close') {
|
|
gui.closeWindow(win);
|
|
return null;
|
|
}
|
|
if (evt.type === 'gadgetup') {
|
|
if (evt.id === transferCheckboxId) {
|
|
gui.setDisabled(win, transferAccountInputId, !gui.get(win, transferAccountInputId));
|
|
}
|
|
if (evt.id === executeButtonId) {
|
|
innerData.subject = gui.get(win, subjectInputId);
|
|
innerData.amount = parseFloat(gui.get(win, amountInputId));
|
|
innerData.date = toDateByComponents(gui.get(win, dateInputId));
|
|
|
|
if (gui.get(win, transferCheckboxId)) {
|
|
const targetAccountIndex = gui.get(win, transferAccountInputId);
|
|
if (targetAccountIndex !== null && targetAccountIndex !== undefined) {
|
|
innerData.targetAccount = projectData[targetAccountIndex].name;
|
|
}
|
|
}
|
|
gui.closeWindow(win);
|
|
return innerData;
|
|
}
|
|
if (evt.id === deleteButtonId) {
|
|
gui.closeWindow(win);
|
|
return -1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
exports.waitForDataEntryWindow = waitForDataEntryWindow;
|