-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
180 lines (161 loc) · 4.55 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const screen = electron.screen;
const path = require('path')
const ipcMain = electron.ipcMain;
const Store = require('electron-store');
const store = new Store();
const ratio_screen = 1.7;
const scale_screen = 0.7;
let mainWindow;
function createWindow (width,height) {
mainWindow = new BrowserWindow({
width: width,
height: height,
/*minHeight: 400,
maxHeight: 800,
minWidth: 400,
maxWidth: 1500,*/
webPreferences: {
/*nodeIntegration: false, // is default value after Electron v5
contextIsolation: true, // protect against prototype pollution
enableRemoteModule: false, // turn off remote*/
preload: path.join(__dirname, 'preload.js')
}
});
mainWindow.loadFile('index.html');
mainWindow.on('closed', () => {
mainWindow = null;
});
}
app.whenReady().then(() => {
const primaryDisplay = screen.getPrimaryDisplay();
const { width, height } = primaryDisplay.workAreaSize;
createWindow(Math.round(ratio_screen*height*scale_screen), Math.round(height*scale_screen));
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
ipcMain.on("requestPoints", (event, etab_id) => {
let data = store.get('data');
if (data == null){
data = {};
store.set('data',data);
}
if (etab_id=="NaN" || !data.hasOwnProperty(etab_id.toString())){
mainWindow.webContents.send("sendPoints", null);
}
mainWindow.webContents.send("sendPoints",{"name":data[etab_id.toString()]["name"],"points": data[etab_id.toString()]["points"]});
});
ipcMain.on("addRank", (event, args) => {
let etab_id = args["etab_id"];
let point = args["point"];
let data = store.get('data');
if (data == null || !data.hasOwnProperty(etab_id.toString())){
return;
}
let points = store.get("data."+etab_id+".points");
let ndays = point[0];
let rank = point[1];
let i=0;
if (points.length>0){
for (i=0; i<points.length; i++){
if (points[i][0] > ndays){
if (points[i][1] > rank){
return;
}
break;
}else if(points[i][0] == ndays){
if (!((i+1<points.length && points[i+1][1] > rank)
||(i>0 && points[i-1][1] < rank))){
points[i][1] = rank;
store.set("data."+etab_id+".points",points);
}
return;
}
}
if (i>0 && points[i-1][1] < rank){
return;
}
}
points.splice(i, 0, [ndays,rank]);
store.set("data."+etab_id+".points",points);
});
ipcMain.on("deletePoint", (event, args) => {
let etab_id = args["etab_id"];
let ndays = args["ndays"];
let data = store.get('data');
if (data == null || !data.hasOwnProperty(etab_id.toString())){
return;
}
let points = store.get("data."+etab_id+".points");
for (let i=0; i<points.length; i++){
if (points[i][0] == ndays){
points.splice(i, 1);
store.set("data."+etab_id+".points",points);
return;
}
}
});
ipcMain.on("addEtab", (event, etab_name) => {
if (etab_name.replace(/\s/g, '').length > 0){
let data = store.get('data');
if (data == null){
data = {};
store.set('data',data);
}
let etab_id = 0;
while (data.hasOwnProperty(etab_id.toString())){
etab_id++;
}
//store.set('data.'+etab_id.toString(),{});
data[etab_id.toString()] = {"name": etab_name, "points": []};
store.set('data',data);
mainWindow.webContents.send("sendEtabId", etab_id);
}else{
mainWindow.webContents.send("sendEtabId", -1);
}
});
ipcMain.on("delEtab", (event, etab_id) => {
let data = store.get('data');
if (data == null){
data = {};
store.set('data',data);
}
if(data.hasOwnProperty(etab_id.toString())){
delete data[etab_id.toString()];
store.set('data',data);
}
});
ipcMain.on("requestEtabs", (event,arg) => {
//reset for the tests
//store.set('data',null);
let data = store.get('data');
if (data == null){
data = {};
store.set('data',data);
}
//console.log(data);
let etabs = [];
let keys = Object.keys(data);
for (let i=0; i<keys.length; i++){
let key = keys[i];
etabs.push([key,data[key]['name']]);
}
mainWindow.webContents.send("sendEtabs", etabs);
});
ipcMain.on("changeEtabName", (event,args) => {
let etab_id = args["etab_id"];
let etab_name = args["name"];
if (etab_name.replace(/\s/g, '').length > 0){
store.set('data.'+etab_id.toString()+'.name',etab_name);
}
});