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.rootDir, 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 +'
';
}
if(slides['slides'][slide]['head'].length > 0) {
var head = slides['slides'][slide]['head'];
slideStr = slideStr+''+head+'
';
if(slides['slides'][slide]['text'].length > 0) {
var text = slides['slides'][slide]['text'];
slideStr = slideStr+'
'+text+'
';
}
slideStr = slideStr + '
';
}
slideStr = slideStr + '\n';
}
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 + '- ';
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, '
');
eventStr = eventStr + '' + evField + '
';
}
}
eventStr = eventStr + ' ';
}
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]+'
';
}
body = body + '';
res.setHeader('Content-Length', body.length);
res.end(body);
});
});
config.app.listen(8080);
};