-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
83 lines (67 loc) · 2.75 KB
/
utils.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
const fileIO = require('./fileIO');
function fetchInTemplate(templateContext, templateKey, filename)
{
templateContext[templateKey] = fileIO.getFile(filename);
}
const PRIVILEGE = {NOBODY: 0, MEMBER: 1, ADMIN: 2};
/** Template engine */
const whiskers = require('whiskers');
module.exports =
{
renderHTML: function(request, result, templateFile, templateDependencyFunction)
{
var templateContext = Object();
var prom = [];
prom.push(fileIO.getFile("./html/mainTemplate.html"));
prom.push(fetchInTemplate(templateContext, "header", "./html/header.html"));
prom.push(fetchInTemplate(templateContext, "footer", "./html/footer.html"));
if(module.exports.checkPrivilege(request) >= PRIVILEGE.MEMBER)
{
templateContext.loggedIn = true;
if(module.exports.checkPrivilege(request) === PRIVILEGE.ADMIN)
templateContext.admin = true;
}
else
{
prom.push(fetchInTemplate(templateContext, "login","./html/login.html"));
}
if(templateDependencyFunction !== null)
prom.push(templateDependencyFunction(templateContext, request));
prom.push(fetchInTemplate(templateContext, "main","./html/" + templateFile));
Promise.all(prom).then(function(content)
{
result.write(whiskers.render(content[0], templateContext));
result.end();
});
},
PRIVILEGE:
{
NOBODY: 0,
MEMBER: 1,
ADMIN: 2
},
checkPrivilege: function(request)
{
if(request.session.login !== true)
return module.exports.PRIVILEGE.NOBODY;
else if(request.session.admin === true)
return module.exports.PRIVILEGE.ADMIN;
return module.exports.PRIVILEGE.MEMBER;
},
printError: function(result, errorMessage)
{
var templateContext = Object();
var prom = [];
prom.push(fileIO.getFile("./html/mainTemplate.html"));
prom.push(fetchInTemplate(templateContext, "header", "./html/header.html"));
prom.push(fetchInTemplate(templateContext, "footer", "./html/footer.html"));
prom.push(fetchInTemplate(templateContext, "main", "./html/error.html"));
prom.push(fetchInTemplate(templateContext, "login","./html/login.html"));
templateContext.errorMessage = errorMessage;
Promise.all(prom).then(function(content)
{
result.write(whiskers.render(content[0], templateContext));
result.end();
});
}
};