kettek2/acts/index.js

72 lines
2.6 KiB
JavaScript

// TODO: Index should likely show the index for the current location if it also contains a directory
var fs = require('fs');
module.exports = function(qwiki) {
qwiki.rule('index', '@@CONTENT@@', function(req, res, instance, next) {
var path = 'wiki/'+req.area;
qwiki.readDirectory(path, function(err, files) {
if (err) {
console.log('err: ' + err);
if (err.errno == -2) { // ENOENT
res.write('Cannot index "' + req.area + '" as it does not exist'); // FIXME: do actual errors
}
res.write('Error: ' + err);
next();
return;
}
files.sort(function(a, b) {
if (a.is_dir == true && b.is_dir == false) return -1;
else if (a.is_dir == false && b.is_dir == true) return 1;
else if (a.is_dir == false && b.is_dir == false) {
var a_ext = qwiki.getExt(a.name);
var b_ext = qwiki.getExt(b.name);
if (a_ext == 'qwk' && b_ext != 'qwk') return -1;
if (a_ext != 'qwk' && b_ext == 'qwk') return 1;
else return 0;
} else {
return 0;
}
});
/*files.sort(function(a, b) {
var a_ext = qwiki.getExt(a.name);
var b_ext = qwiki.getExt(b.name);
if (a_ext == 'qwk' && b_ext != 'qwk') return -1;
if (a_ext != 'qwk' && b_ext == 'qwk') return 1;
else return 0;
});*/
res.write('<h2>Index of '+req.area+'</h2>');
res.write('<ul id="act_index">')
if (req.area != '') {
res.write('<li><a class="file_dir" href="'+req.area+'/../index">..</a></li>');
}
for (var file in files) {
var name = '';
res.write('<li>');
if (qwiki.getExt(files[file].name) == 'qwk') {
name = files[file].name.slice(0, -4);
res.write('<a class="file_qwk" href="'+req.area+'/'+name+'">'+name+'</a>');
} else {
name = files[file].name;
if (files[file].is_dir) {
res.write('<a class="file_dir" href="'+req.area+'/'+name+'/index">'+name+'/</a>');
} else {
res.write('<a class="file_other" href="'+req.area+'/'+name+'">'+name+'</a>');
}
}
res.write('</li>');
}
res.write('</ul>');
next();
});
});
qwiki.rule('index', '@@CONTROLS@@', function(req, res, instance, next) {
res.write('<li><a href="'+(req.area == '' ? '/' : req.area)+'"><img src="/view.png">View</a></li><li><a href="'+req.area+'/upload"><img src="/upload.png">Upload</a></li>');
next();
});
qwiki.act('index', function(req, res) {
res.writeHead(200, "OK", {
"Content-Type": "text/html",
});
qwiki.parsePage('index', '', req, res);
});
};