summaryrefslogtreecommitdiff
path: root/frontend.js
blob: 30b5457ff9b97a911bfb91bdc9ff2f54d076de90 (plain)
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
var path = require('path');
var fs = require('fs');
var sanitize =require('validator').sanitize;

var model;

exports.setModel = function(m) {
	model = m;
};

exports.start = function(config) {
	config.app.get('/slides/:id', function(req, res) {
		var slideset;
		slideset = req.params.id;
		model.getSlides(slideset, function(err, slides) {
			if(err) { throw err; }
			fs.readFile(path.join(__dirname, config.dir, slides['template']) , function(err,data) {
				if(err) { res.end(body); }
				var body = data.toString();
				var slideStr = '';
				for(var slide in slides['slides']) {
					if(!slides['slides'].hasOwnProperty(slide)) { continue; }
					var media = sanitize(slides['slides'][slide]['file']).entityEncode();
					if(slides['slides'][slide]['type'] === 'image') {
						slideStr = slideStr +'<li><img src="/'+media+'">';
					}
					if(slides['slides'][slide]['head'].length > 0) {
						var head = sanitize(slides['slides'][slide]['head']).xss();
						slideStr = slideStr+'<div class="slideDesc"><div class="slideHead">'+head+'</div>';
						if(slides['slides'][slide]['text'].length > 0) {
							var text = sanitize(slides['slides'][slide]['text']).xss();
							slideStr = slideStr+'<div class="slideText">'+text+'</div>';
						}
						slideStr = slideStr + '</div>';
					}
					slideStr = slideStr + '</img></li>\n';
				}
				var eventStr = '';
				for(var date in slides['events']) {
					if(!slides['events'].hasOwnProperty(date)) { continue; }
					eventStr = eventStr + '<div class="event"><div class="eventDate">'+date+'</div><ul class="eventList">';
					for(var ev in slides['events'][date]) {
						if(!slides['events'][date].hasOwnProperty(ev)) { continue; }
						eventStr = eventStr + '<li class="eventEl">';
						for(var evF in slides['events'][date][ev]) {
							if(!slides['events'][date][ev].hasOwnProperty(evF)) { continue; }
							if(typeof slides['events'][date][ev][evF] == 'string') {
								var evClass = evF;
								var evField = sanitize(slides['events'][date][ev][evF]).entityEncode();
								var evField = evField.replace(/\\n/g, '<br/>');
								eventStr = eventStr + '<div class="eventField ' + evClass + '">' + evField + '</div>';
							}
						}
						eventStr = eventStr + '</li>';
					}
					eventStr = eventStr + '</ul></div>';
				}
				body = body.replace("$IMAGES", slideStr);
				body = body.replace("$EVENTS", eventStr);
				res.setHeader('Content-Length', body.length);
				res.end(body);
			});
		});
	});
	config.app.get('*', function(req, res) {
		model.getAllSlides(function(slidesets) {
			var body = '<head></head><body><h3>Slideshows:</h3>';
			for(var slideset in slidesets) {
				if(!slidesets.hasOwnProperty(slideset)) { continue; }
				body = body + '<a href="/slides/' + slidesets[slideset] + '">'+slidesets[slideset]+'</a><br/>';
			}
			body = body + '</body>';
			res.setHeader('Content-Length', body.length);
			res.end(body);
		});
	});
	config.app.listen(8080);
};