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 +'
';
}
if(slides['slides'][slide]['text'].length > 0) {
slideStr = slideStr+''+slides['slides'][slide]['head']+' - '+slides['slides'][slide]['text']+'
';
}
}
var eventStr = '';
for(var date in slides['events']) {
if(!slides['events'].hasOwnProperty(date)) { continue; }
eventStr = eventStr + ''+date+'
';
for(var ev in slides['events'][date]) {
if(!slides['events'][date].hasOwnProperty(ev)) { continue; }
eventStr = eventStr + '- '+slides['events'][date][ev]['time']+' - '+slides['events'][date][ev]['summary']+'
';
}
eventStr = eventStr + '
';
}
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 = 'Slideshows:
';
for(var slideset in slidesets) {
if(!slidesets.hasOwnProperty(slideset)) { continue; }
body = body + ''+slidesets[slideset]+'
';
}
res.setHeader('Content-Length', body.length);
res.end(body);
});
});
config.app.listen(8080);
};