This repository has been archived by the owner on Oct 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pageBuilder.js
303 lines (244 loc) · 7.23 KB
/
pageBuilder.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/**
* Author : Thophile
* Project : Deskapp
*/
const fs = require('fs')
const path = require('path')
const { shell } = require('electron')
const { remote } = require('electron')
const $ = require('jquery');
require('popper.js');
require('bootstrap');
const api_url = "https://theophile.alexandresauner.fr/api_deskapp/";
var navToggled = false;
var logToggled = false;
var config = require('electron').remote.getGlobal("config")
var details = require('electron').remote.getGlobal("details")
//On Start
$(document).ready(function () {
//Load json local config
loadConfig(function () {
//Load navbar
$("#header").load("_navbar.html", () => {
addAction()
});
//Load home
$('#main').load("home.html")
//query the api for distant data
loadData()
})
});
//Load a page in the main section
function loadPage(str) {
$("#main").load(str);
log(str[0].toUpperCase() + "" + str.substring(1, str.length -5) + " loaded")
loadConfig();
try{
saveData(loadData());
}catch(e){
if (e.name !== "TypeError") throw e
}
}
//Window Action Init
function addAction() {
document.getElementById('minimize').addEventListener('click', (e) => {
e.preventDefault()
var window = remote.getCurrentWindow();
window.minimize();
})
document.getElementById('maximize').addEventListener('click', (e) => {
e.preventDefault()
var window = remote.getCurrentWindow();
if (!window.isMaximized()) {
window.maximize();
} else {
window.unmaximize();
}
})
document.getElementById('close').addEventListener('click', (e) => {
e.preventDefault()
var window = remote.getCurrentWindow();
window.close();
})
let str = config.jwt ? " back" : " ";
log("Welcome" + str + "!" )
}
//Toogle the side navbar and log pop-up
function toggleNav() {
if (navToggled) {
document.getElementById("sidebar").style.width = "0";
document.getElementById("main").style.marginLeft = "0";
document.getElementById("console").style.marginLeft = "0";
navToggled = false
}
else {
document.getElementById("sidebar").style.width = "200px";
document.getElementById("main").style.marginLeft = "200px";
document.getElementById("console").style.marginLeft = "200px";
navToggled = true
}
}
function toggleLog(){
if(logToggled){
document.getElementById('log_form').style.display="none"
logToggled=false
}else{
document.getElementById('log_form').style.display="block"
logToggled=true
}
}
//Output message with date in a custom console
function log(content) {
date = new Date()
document.getElementById("console").innerHTML = " <i class=\"fas fa-caret-right\"></i> " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() + " " + content;
}
function getDayName(locale) {
return new Date().toLocaleDateString(locale, { weekday: 'long' });
}
//** Data Storage **/
function createAccount(){
credentials = JSON.stringify(
{
email: document.getElementById('_email').value,
password: document.getElementById('_password').value,
details: {
watchList : [],
tasks : []
}
}
)
//making the api query
var params = {
headers: {
"content-type":"application/json; charset=UTF-8"
},
body: credentials,
method: "POST"
}
fetch(api_url + "create_user.php", params)
.then(data=>{
if(data.status == "400"){
toggleLog()
}
return data.json()
})
.then(res=>{
document.getElementById('log_error').innerHTML = res.message
})
.catch(error=>{console.log(error)})
}
function changeAccount(){
config.jwt= ""
saveConfig()
toggleLog()
}
function logIn(){
credentials = JSON.stringify({
email : document.getElementById('_email').value,
password: document.getElementById('_password').value
})
//making the api query
var params = {
headers: {
"content-type":"application/json"
},
body: credentials,
method: "POST"
}
fetch(api_url + "login.php", params)
.then(data=>{
if(data.status == "401"){
document.getElementById('log_error').innerHTML="Access denied"
}
return data.json()
})
.then(res=>{
console.log(res.message)
config.jwt = res.jwt
saveConfig()
loadData()
})
.catch(error=>{console.log(error)})
}
function saveData(callback) {
if(details !== undefined && (details.watchList.length !== 0 || details.tasks.length !== 0) ){
credentials = JSON.stringify({
details: details,
jwt: config.jwt
})
//making the api query
var params = {
headers: {
"content-type":"application/json; charset=UTF-8"
},
body: credentials,
method: "POST"
}
fetch(api_url + "update_user.php", params)
.then(data=>{
return data.json()
})
.then(res=>{
config.jwt = res.jwt
saveConfig()
if (typeof callback === 'function' && callback()) callback();
})
.catch(error=>{console.log(error)})
}
}
function loadData(callback) {
credentials = JSON.stringify({
jwt: config.jwt
})
var params = {
headers: {
"content-type":"application/json; charset=UTF-8"
},
body: credentials,
method: "POST"
}
fetch(api_url + "validate_token.php", params)
.then(data=>{
if(data.status == "401"){
toggleLog()
}
return data.json()
})
.then(res=>{
details = res.details;
config.email = res.data.email;
saveConfig()
})
.catch(error=>{console.log(error)})
if (typeof callback === 'function') callback();
}
/** Settings **/
//Reload the window
function refresh() {
remote.getGlobal("Start")()
remote.getCurrentWindow().close();
}
function loadConfig(callback) {
fs.readFile(path.join(remote.app.getPath('userData'), 'config.json'), (err, data) => {
if (err) throw err;
//Saving config object with the configuration
config = JSON.parse(data);
if (typeof callback === 'function' && callback()) callback();
})
}
function saveConfig() {
fs.writeFile(path.join(remote.app.getPath('userData'), 'config.json'), JSON.stringify(config), function (err) {
if (err) throw err;
});
}
function updateConfig(){
config.devTools = document.getElementById('_devTools').checked
config.openAtLogin = document.getElementById('_openAtLogin').checked
saveConfig()
}
function displayConfig(){
//toggle settings
document.getElementById('_devTools').checked = config.devTools
document.getElementById('_openAtLogin').checked = config.openAtLogin
document.getElementById('_username').placeholder = (config.email == undefined ? "Unlogged" : config.email)
}