summaryrefslogtreecommitdiff
path: root/frontend.js
blob: e60d572f469b8faf37d835300a0b27090afccfe7 (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
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>';
					}
					slideStr = slideStr + '</img></li>';
				}
				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; }
							eventStr = eventStr + '<div class="eventField ' + evF + '">' + slides['events'][date][ev][evF] + '</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 = '<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);
};