html outputter (basic)

This commit is contained in:
Meutel 2014-01-22 22:57:58 +01:00
parent 8be80743b4
commit 676df6f36b
2 changed files with 69 additions and 10 deletions

View File

@ -113,9 +113,65 @@ JSONOutputter.prototype = Object.create(Outputter.prototype, {
*/
var HtmlOutputter = function() {
Outputter.call(this);
logger.debug('HTML');
// TODO use templates
this.buffer = new Array();
this.colStarted = false;
};
// inherits Outputter
HtmlOutputter.prototype = Object.create(Outputter.prototype, {
output: {
value: function(resource) {
var self = this;
logger.debug('colStarted: '+this.colStarted);
if (!this.colStarted && this.buffer.length == 0) {
this.buffer.push(resource);
this.out.write('<html><head><title>Bouquins</title></head>');
} else {
this.buffer.push(resource);
if (!this.colStarted) {
this.out.write('<table><tr>');
this.colStarted = true;
} else
this.out.write('</tr>');
while (this.buffer.length>0) {
var r = this.buffer.shift();
Object.keys(r).forEach(function(key) {
self.out.write('<td title=\"'+key+'\">'+r[key]+'</td>');
});
if (this.buffer.length>0)
this.out.write('</tr><tr>');
}
}
}, enumerable: true, configurable: true, writable: true
},
end: {
value: function() {
var self = this;
if (this.buffer.length == 1) {
// single resource
this.out.write('<ul>');
var r = this.buffer[0];
Object.keys(r).forEach(function(key) {
self.out.write('<li>'+key+': '+r[key]+'</li>');
});
this.out.write('</ul>');
}
if (this.colStarted) {
//end collection
this.out.write('</table>');
}
this.out.write('</html>');
logger.debug('Action ended');
this.out.end();
}, enumerable: true, configurable: true, writable: true
},
init: {
value: function() {
this.addHeader('Content-Type', 'text/html');
}, enumerable: true, configurable: true, writable: true
}
});
/**

View File

@ -58,17 +58,20 @@ Router.prototype = {
* @param callback <function(err)> error callback
*/
buildOutputter : function(mime, callback) {
switch(mime) {
case 'application/json':
return new Outputter.JSONOutputter();
case 'text/html':
return new Outputter.HtmlOutputter();
case 'text/plain':
return new Outputter.TextOutputter();
default:
logger.error('Usupported type: '+mime);
return new Outputter.JSONOutputter();
var mimes = mime.split(',');
var outputter = null;
for (var i=0; i<mimes.length; i++) {
switch(mimes[i]) {
case 'application/json':
return new Outputter.JSONOutputter();
case 'text/html':
return new Outputter.HtmlOutputter();
case 'text/plain':
return new Outputter.TextOutputter();
}
}
logger.error('Usupported type: '+mime);
return new Outputter.JSONOutputter();
},
/**