-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
136 lines (125 loc) · 3.85 KB
/
index.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
var express = require("express");
var { draw } = require("./draw");
var sendJSON = (req, res, next) => {
res.sendJSON = data => {
res.setHeader("Content-Type", "application/json");
res.send(JSON.stringify(data));
};
next();
}
class Server {
constructor() {
this.variables = {
port: ":8501"
}
this.expressApp = express();
this.draw = draw;
this.expressApp.use(sendJSON);
}
/// to get a route
get(routePath, routeHandler) {
this.expressApp.get(routePath, (req, res) => {
var context = {
req,
res,
title: pageTitle => {
return `<title>${pageTitle}</title>`;
},
button: (label, onClick, styles) => {
this.expressApp.get(`/gamification_/btn-handlers/${label.replace(" ", "-")}`, (_, response) => {
onClick();
response.sendJSON({ message: "Finished executing button handler" });
});
return `
<button
type="button"
onclick="document.gamification_fetchDataFromAPI('/gamification_/btn-handlers/${label.replace(" ", "-")}')"
style="margin-top: ${styles?.marginTop || "30px"}; height: ${styles?.height || "50px"}; width: ${styles?.width || "145px"}; border-radius: ${styles?.borderRadius || "12px"}; border-color: ${styles?.borderColor || "darkred"}; background-color: ${styles?.bgColor || "var(--theme-bg)"}; color: ${styles?.fontColor || "var(--theme-fc)"}; font-weight: ${styles?.fontWeight || "bold"};"
>${label}</button>
<script>
document.gamification_fetchDataFromAPI = async function (url) {
try {
const response = await fetch(url);
if (!response.ok) {
console.warn("Network response was not ok");
}
const data = await response.json();
return data;
} catch (error) {
console.error("Error fetching data:", error);
return null;
}
}
</script>
`;
},
setTheme: theme => {
if (theme === "dark") {
return `
<style>
body {
background-color: black;
color: white;
}
:root {
--theme-bg: black;
--theme-fc: white;
}
</style>
`;
} else if (theme === "light") {
return `
<style>
body {
background-color: white;
color: black;
}
:root {
--theme-bg: white;
--theme-fc: black;
}
</style>
`;
}
return `
<style>
body {
background-color: white;
color: black;
}
:root {
--theme-bg: white;
--them-fc: black;
}
</style>
`;
},
setFontFamily: fontFamily => {
return `<style>body { font-family: ${fontFamily}; }</style>`
},
getExpressServer: () => {
return this.expressApp;
},
draw: objectProps => {
var drawing = this.draw(objectProps);
return `<style>${drawing.styles}</style>${drawing.HTML}`;
},
}
routeHandler(context);
});
}
/// to run the server
run(port, callback) {
let e = null;
try {
this.expressApp.listen(parseInt(port.replace(":", "")), () => {
callback();
});
}
catch (err) {
e = err;
}
return e;
}
}
module.exports = { Server };