From 863ab8fadd264682de78e992bdfe6145a9f4bb18 Mon Sep 17 00:00:00 2001 From: simov Date: Tue, 3 Dec 2013 00:30:45 +0200 Subject: [PATCH 1/3] Directory Middleware Improvements: - details and mobile views - include icon images only once from css --- lib/middleware/directory.js | 70 +++++++++++++++++--- lib/public/directory.html | 4 +- lib/public/style.css | 123 ++++++++++++++++++++++++++++-------- 3 files changed, 158 insertions(+), 39 deletions(-) diff --git a/lib/middleware/directory.js b/lib/middleware/directory.js index 9b5cc918d..3a7894329 100644 --- a/lib/middleware/directory.js +++ b/lib/middleware/directory.js @@ -70,6 +70,7 @@ exports = module.exports = function directory(root, options){ if (!root) throw new Error('directory() root path required'); var hidden = options.hidden , icons = options.icons + , view = options.view || 'tiles' , filter = options.filter , root = normalize(root + sep); @@ -109,7 +110,7 @@ exports = module.exports = function directory(root, options){ // not acceptable if (!type) return next(utils.error(406)); - exports[mediaType[type]](req, res, files, next, originalDir, showUp, icons, path); + exports[mediaType[type]](req, res, files, next, originalDir, showUp, icons, path, view); }); }); }; @@ -119,7 +120,7 @@ exports = module.exports = function directory(root, options){ * Respond with text/html. */ -exports.html = function(req, res, files, next, dir, showUp, icons, path){ +exports.html = function(req, res, files, next, dir, showUp, icons, path, view){ fs.readFile(__dirname + '/../public/directory.html', 'utf8', function(err, str){ if (err) return next(err); fs.readFile(__dirname + '/../public/style.css', 'utf8', function(err, style){ @@ -131,7 +132,8 @@ exports.html = function(req, res, files, next, dir, showUp, icons, path){ if (showUp) files.unshift({ name: '..' }); str = str .replace('{style}', style) - .replace('{files}', html(files, dir, icons)) + .replace('{iconstyle}', iconStyle(files, icons)) + .replace('{files}', html(files, dir, icons, view)) .replace('{directory}', dir) .replace('{linked-path}', htmlPath(dir)); res.setHeader('Content-Type', 'text/html'); @@ -185,32 +187,80 @@ function htmlPath(dir) { }).join(' / '); } +/** + * Load icon images, return css string. + */ + +function iconStyle (files, useIcons) { + if (!useIcons) return ''; + var data = {}; + var views = { tiles: [], details: [], mobile: [] }; + + for (var i=0; i < files.length; i++) { + var file = files[i]; + if (file.name == '..') continue; + + var isDir = '..' == file.name || (file.stat && file.stat.isDirectory()); + var icon = isDir ? icons.folder : icons[extname(file.name)] || icons.default; + + var ext = extname(file.name); + ext = isDir ? '.directory' : (icons[ext] ? ext : '.default'); + + if (data[icon]) continue; + data[icon] = ext + ' .name{background-image: url(data:image/png;base64,' + load(icon)+');}'; + views.tiles.push('.view-tiles ' + data[icon]); + views.details.push('.view-details ' + data[icon]); + views.mobile.push('#files ' + data[icon]); + } + + var style = views.tiles.join('\n') + + '\n'+views.details.join('\n') + + '\n@media (max-width: 768px) {\n\t' + + views.mobile.join('\n\t') + + '\n}'; + return style; +} + /** * Map html `files`, returning an html unordered list. */ -function html(files, dir, useIcons) { - return '