-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
123 lines (100 loc) · 3.01 KB
/
app.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
const electron = require('electron')
const Datastore = require('nedb')
const url = require('url')
const path = require('path')
const menuBar = require('./src/components/MenuBar')
const { app, BrowserWindow, Menu, ipcMain, Notification } = electron
let mainWindow
const loadMainWindow = () => {
mainWindow = new BrowserWindow({
width : 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true
}
});
mainWindow.loadFile(path.join(__dirname, "src/index.html"));
}
app.on('ready', () => {
loadMainWindow();
mainWindow.on('closed', () => app.quit())
const mainMenu = Menu.buildFromTemplate(menuBar)
Menu.setApplicationMenu(mainMenu)
})
//mac-os fix
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
//if other windows active?
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
loadMainWindow();
}
});
const db = new Datastore({
filename: './items.db',
autoload: true
})
//Get all items from db and send them to the client
ipcMain.on('loadAll', () => db.find({}, (err, items) => mainWindow.webContents.send('loaded', items)))
//Saves item and returns it to client
ipcMain.on('addItem', (e, item) => {
db.insert(item, (err,doc) => {
if (err) throw new Error(err)
mainWindow.webContents.send('added', doc)
})
})
//Updates each item and returns it to client
ipcMain.on('updateItem', (e, id,item) => {
db.update(id, item,(err) => {
if (err) throw new Error(err)
mainWindow.webContents.send('updated', id,item)
})
})
//Deletes each item and return deleted itemID
ipcMain.on('deleteItem', (e, itemid) => {
db.remove(itemid, (err,num) => {
if (err) throw new Error(err)
mainWindow.webContents.send('deleted', itemid)
})
})
//Clears database and send event to client if successful
ipcMain.on('clearAll', () => {
db.remove({}, { multi: true }, (err) => {
if (err) throw new Error(err)
mainWindow.webContents.send('cleared')
})
})
//Notifications for add update delete and clear
ipcMain.handle('show-add-notification', (event, ...args) => {
const notification = {
title: 'New Task',
body: `Added: ${args[0]} to list`
}
new Notification(notification).show()
});
ipcMain.handle('show-update-notification', (event, ...args) => {
const notification = {
title: 'Edit Task',
body: `Updated: ${args[0]}`
}
new Notification(notification).show()
});
ipcMain.handle('show-delete-notification', (event, ...args) => {
const notification = {
title: 'Remove Task',
body: `Deleted: ${args[0]} from list`
}
new Notification(notification).show()
});
ipcMain.handle('show-clear-notification', (event, ...args) => {
const notification = {
title: 'Clear List',
body: `Deleted whole database!`
}
new Notification(notification).show()
});