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

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; }
					if(slides['slides'][slide]['type'] === 'image') {
						slideStr = slideStr +'<li><img src="/'+slides['slides'][slide]['file']+'">';
					}
					if(slides['slides'][slide]['text'].length > 0) {
						slideStr = slideStr+'<div class="slide_desc"><h2>'+slides['slides'][slide]['head']+' - '+slides['slides'][slide]['text']+'</h2></div></img></li>';
					}
				}
				var eventStr = '';
				for(var date in slides['events']) {
					eventStr = eventStr + '<h4>'+date+'</h4><hr><ul>';
					for(var ev in slides['events'][date]) {
						if(!slides['events'][date].hasOwnProperty(ev)) { continue; }
						eventStr = eventStr + '<li>'+slides['events'][date][ev]['time']+' - '+slides['events'][date][ev]['text']+'</li>';
					}
					eventStr = eventStr + '</ul>';
				}
				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 = '<h3>Slideshows:</h3>';
			for(var slideset in slidesets) {
				if(!slidesets.hasOwnProperty(slideset)) { continue; }
				body = body + '<a href="/slides/' + slidesets[slideset] + '">'+slidesets[slideset]+'</a><br/>';
			}
			res.setHeader('Content-Length', body.length);
			res.end(body);
		});
	});
	config.app.listen(8080);
};