Include module name in requests for node icons

This commit is contained in:
Nick O'Leary
2017-01-18 13:06:22 +00:00
parent 64daaeb310
commit 11f4ae019c
10 changed files with 70 additions and 77 deletions

View File

@@ -94,7 +94,7 @@ function init(_server,_runtime) {
});
}
editorApp.get("/",ensureRuntimeStarted,ui.ensureSlash,ui.editor);
editorApp.get("/icons/:icon",ui.icon);
editorApp.get("/icons/:module/:icon",ui.icon);
theme.init(runtime);
if (settings.editorTheme) {
editorApp.use("/theme",theme.app());

View File

@@ -21,7 +21,9 @@ var theme = require("./theme");
var Mustache = require("mustache");
var icon_paths = [path.resolve(__dirname + '/../../public/icons')];
var icon_paths = {
"node-red":[path.resolve(__dirname + '/../../public/icons')]
};
var iconCache = {};
//TODO: create a default icon
var defaultIcon = path.resolve(__dirname + '/../../public/icons/arrow-in.png');
@@ -29,7 +31,8 @@ var templateDir = path.resolve(__dirname+"/../../editor/templates");
var editorTemplate;
function nodeIconDir(dir) {
icon_paths.push(path.resolve(dir));
icon_paths[dir.name] = icon_paths[dir.name] || [];
icon_paths[dir.name].push(path.resolve(dir.path));
}
module.exports = {
@@ -54,18 +57,24 @@ module.exports = {
}
},
icon: function(req,res) {
if (iconCache[req.params.icon]) {
res.sendFile(iconCache[req.params.icon]); // if not found, express prints this to the console and serves 404
var icon = req.params.icon;
var module = req.params.module;
var iconName = module+"/"+icon;
if (iconCache[iconName]) {
res.sendFile(iconCache[iconName]); // if not found, express prints this to the console and serves 404
} else {
for (var p=0;p<icon_paths.length;p++) {
var iconPath = path.join(icon_paths[p],req.params.icon);
try {
fs.statSync(iconPath);
res.sendFile(iconPath);
iconCache[req.params.icon] = iconPath;
return;
} catch(err) {
// iconPath doesn't exist
var paths = icon_paths[module];
if (paths) {
for (var p=0;p<paths.length;p++) {
var iconPath = path.join(paths[p],icon);
try {
fs.statSync(iconPath);
res.sendFile(iconPath);
iconCache[iconName] = iconPath;
return;
} catch(err) {
// iconPath doesn't exist
}
}
}
res.sendFile(defaultIcon);