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

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

exports.start = function(config) {
	var app = config.app;
	app.engine('html', cons[config.templateEngine]);

	// set .html as the default extension 
	app.set('view engine', 'html');
	app.set('views', path.join(__dirname, config.rootDir));
	app.get('/slides/:id', function(req, res) {
		slideset = req.params.id;
		model.getSlides(slideset, function(err, slides) {
			if(err) { throw err; }
			var slidesAr = [];
			for(var slide in slides['slides']) {
				if(!slides['slides'].hasOwnProperty(slide)) { continue; }
				if(slides['slides'][slide]['type'] === 'image') {
					slidesAr.push({
						image: '/' + slides['slides'][slide]['file']
					});
				}
				if(slides['slides'][slide]['head'].length > 0) {
					slidesAr[slidesAr.length - 1]['description'] = slides['slides'][slide]['head'];
					if(slides['slides'][slide]['text'].length > 0) {
						slidesAr[slidesAr.length - 1]['text'] = slides['slides'][slide]['text'];
					}
				}
			}
			var eventAr = [];
			for(var date in slides['events']) {
				if(!slides['events'].hasOwnProperty(date)) { continue; }
				eventAr.push({
					date: date,
					events: []
				});
				for(var ev in slides['events'][date]) {
					if(!slides['events'][date].hasOwnProperty(ev)) { continue; }
					var eventObj = {};
					for(var evF in slides['events'][date][ev]) {
						if(!slides['events'][date][ev].hasOwnProperty(evF)) { continue; }
						if(typeof slides['events'][date][ev][evF] == 'string') {
							eventObj[evF] = slides['events'][date][ev][evF];
						}
					}
					eventAr[eventAr.length - 1]['events'].push(eventObj);
				}
			}
			res.render(path.join(slideset, config.templateFile), {
				slides: slidesAr,
				events: eventAr
			});
		});
	});
	app.get('*', function(req, res){
		model.getAllSlides(function(slidesets) {
			res.render(config.listTemplate, {
				slides: slidesets
			});
		});
	});
	app.listen(config.port);
};