diff options
Diffstat (limited to 'frontend.js')
-rw-r--r-- | frontend.js | 80 |
1 files changed, 45 insertions, 35 deletions
diff --git a/frontend.js b/frontend.js index 5b2d486..0dba5b4 100644 --- a/frontend.js +++ b/frontend.js @@ -1,10 +1,6 @@ -var Connect = require('connect'); -var http = require('http'); -var url = require('url'); var path = require('path'); var fs = require('fs'); -var configuration; var model; exports.setModel = function(m) { @@ -12,37 +8,51 @@ exports.setModel = function(m) { }; exports.start = function(config) { - configuration = config; - Connect.createServer( - Connect.logger(), // Log responses to the terminal using Common Log Format. - Connect.responseTime(), // Add a special header with timing information. - Connect.staticCache(), - Connect.static(path.join(__dirname, configuration.dir)), - function(req, res) { - var purl = url.parse(req.url, true); - var slideset = 'default'; - if(purl['query'].hasOwnProperty('slides')) { - slideset = purl['query']['slides']; - } - model.getSlides(slideset, function(slides) { - fs.readFile(path.join(__dirname, configuration.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="transover"><h2>'+slides['slides'][slide]['head']+' - '+slides['slides'][slide]['text']+'</h2></div></img></li>'; - } + 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="transover"><h2>'+slides['slides'][slide]['head']+' - '+slides['slides'][slide]['text']+'</h2></div></img></li>'; } - body = body.replace("$IMAGES", slideStr); - res.setHeader('Content-Length', body.length); - res.end(body); - }); + } + var eventStr = ''; + for(var date in slides['events']) { + if(!slides['events'].hasOwnProperty(date)) { continue; } + eventStr = eventStr + '<h4>'+slides['events'][date]['date']+'</h4><hr><ul>'; + for(var ev in slides['events'][date]['events']) { + if(!slides['events'][date]['events'].hasOwnProperty(ev)) { continue; } + eventStr = eventStr + '<li>'+slides['events'][date]['events'][ev]['time']+' - '+slides['events'][date]['events'][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); }); - } - ).listen(8080); + }); + }); + 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); }; |