forked from francheska-vicente/cssweng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
130 lines (110 loc) · 3.51 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//configure environment variables
process.env.HOSTNAME = "localhost"
process.env.PORT = "3000"
process.env.DB_URL = "mongodb+srv://admin:[email protected]/jaelle-residences-db?retryWrites=true&w=majority"
//import the necessary modules
const path = require('path');
const electron = require('electron');
const server = require('./server.js');
const { ipcMain } = require('electron');
const printEvent = require('./controllers/print-controller');
const fs = require('fs/promises')
const os = require('os')
//retrieves the necessary attributes from electron
const {app, BrowserWindow} = electron;
let mainWindow;
let addWindow;
//create new window once electron finishes initialization
app.on('ready', function() {
//create new window
mainWindow = new BrowserWindow({
title: "Jaelle Residences",
// icon: "",
show: false,
webPreferences: {
devTools: false,
nodeIntegration: true,
contextIsolation: false
}
});
//remove menu bar
mainWindow.setMenuBarVisibility(false);
//maximize and show the window
mainWindow.maximize();
mainWindow.show();
//opens the web application
mainWindow.loadURL(`http://${process.env.HOSTNAME}:${process.env.PORT}/`);
//terminate the electron application on window close
mainWindow.on('closed', function() {
app.quit();
});
});
ipcMain.on('print:goto', (event, bookingID) => {
createAddWindow(bookingID);
});
ipcMain.on('print:execute', (event) => {
print(event.sender);
});
printEvent.onPrintEvent(createAddWindow);
function createAddWindow(bookingID){
addWindow = new BrowserWindow({
width: 720,
height: 480,
title: 'Receipt Preview',
webPreferences:{
nodeIntegration: true,
contextIsolation: false
}
});
addWindow.loadURL(`http://localhost:3000/booking/${bookingID}/print`);
addWindow.webContents.on('did-finish-load', async () => {
const pdf = await addWindow.webContents.printToPDF({});
const dateOfPrint = new Date().toLocaleDateString().replaceAll('/', '-')
const filePath = `${os.homedir()}/Desktop/receipts/${dateOfPrint}__${bookingID}.pdf`;
try {
await fs.writeFile(filePath, pdf)
} catch(error) {
await fs.mkdir(`${os.homedir()}/Desktop/receipts/`)
await fs.writeFile(filePath, pdf)
}
})
addWindow.on('closed', () => addWindow = null);
}
function getDefaultPrinterIndex(window) {
let printers = window.getPrinters();
for (let [index, printer] of printers.entries()) {
if (printer.isDefault) {
return index;
}
}
}
function print(window, copies = 3, delay = 1500) {
let counter = 1;
let printDataPassed = false;
let printerIndex = getDefaultPrinterIndex(window);
_print(window);
//checks whether to print another copy or wait
//does nothing when the printer is printing the current copy, status === 1024
//if the printer is done printing, proceed to print next copy after a certain delay
//stops checking if all copies have been printed
let interval = setInterval(() => {
if (window.getPrinters()[printerIndex].status === 1024) {
printDataPassed = true;
} else if (
printDataPassed &&
window.getPrinters()[printerIndex].status === 0
) {
setTimeout( () => { _print(window) }, delay);
printDataPassed = false;
counter++;
}
if (counter >= copies) {
clearInterval(interval);
}
}, 10);
}
function _print(window) {
window.print({
silent: true,
});
}