defaultLayout = "standard",
defaultMain = null,
addCss = null,
+ layouts = require("./layouts.js");
addJs = null;
+global.njsAppName = "NJSPURLS Web App";
+exports.setAppName = function(name) {
+ global.njsAppName = name;
+}
+exports.setFooterText = function(text) {
+ global.footerText = text;
+}
exports.setPort = function(port) {
localPort = port;
}
+exports.addMenu = function(name, link) {
+ global.addMenu(name, link);
+}
+
+global.addMenu = layouts.addMenu;
+
exports.addCss = function(cssname) {
if(addCss == null) {
addCss = new Array();
exports.standard = function () {
var lay = "<html><head><?njs header ?></head><body><?njs title ?>";
lay += "<table width=\"100%\"><tr><td><?njs menu ?></td></tr>";
- lay += "<tr valign=\"top\"><td width=\"120px\"><?njs sidebar ?></td><td><?njs body ?></td></tr>";
+ lay += "<tr valign=\"top\"><td width=\"15%\"><?njs sidebar ?></td><td><?njs body ?></td></tr>";
lay += "<tr><td><?njs footer ?></td></tr></table><?njs totalend ?></body></html>";
return lay;
exports.straight = function () {
- var lay = "<html><head><?njs header ?></head><body><?nfs title ?>";
+ var lay = "<html><head><?njs header ?></head><body><?njs title ?>";
lay += "<table width=\"100%\"><tr><td><?njs menu ?></td></tr>";
- lay += "<tr valign=\"top\"><td></td><?njs sidebar ?><td><?njs body ?></td></tr>";
+ lay += "<tr valign=\"top\"><td width=\"15%\"><?njs sidebar ?></td><td><?njs body ?></td></tr>";
lay += "<tr><td><?njs footer ?></td></tr></table><?njs totalend ?></body></html>";
-
+
return lay;
}
exports.header = function(request, response, callback) {
// this function is the default header builder.
- response.write("<title>mainname</title>");
+ response.write("<title>"+global.njsAppName+"</title>");
callback(request, response);
}
exports.title = function(request, response, callback) {
// this is the default title builder
- response.write("<h1>mainname</h1>");
+ response.write("<h1>"+global.njsAppName+"</h1>");
callback(request, response);
}
exports.menu = function(request, response, callback) {
// this is the default title builder
- response.write("<table><tr><th>Menu</th><td>item 1</td><td>item2</td></tr></table>");
+ //response.write("<table><tr><th>Menu</th><td>item 1</td><td>item2</td></tr></table>");
+ if(typeof global.menu != "undefined") {
+ response.write("<table><tr>");
+ for(key in global.menu) {
+ response.write("<td><a href=\""+global.menu[key]+"\">"+key+"</a></td>");
+ console.log("menu: '%s', '%s'", key, global.menu[key]);
+ }
+ response.write("</tr></table>");
+ } else {
+ response.write("No Menu Defined");
+ }
callback(request, response);
}
exports.footer = function(request, response, callback) {
// this is the default title builder
- response.write("<br><br><font size=\"-1\"><i>Copyright PJR.cc</i></font>");
+ if(typeof global.footerText != "undefined") {
+ response.write(global.footerText);
+ } else {
+ response.write("<br><br><font size=\"-1\"><i>Copyright PJR.cc</i></font>");
+ }
callback(request, response);
}
console.log("in preresponse - doing nothing");
callback();
}
+
+
+// global menu management functions
+exports.addMenu = function(menuname, menulink) {
+ console.log("add menu item, '%s' -> '%s'", menuname, menulink);
+ if(typeof global.menu == "undefined") {
+ global.menu = new Array();
+ global.menu[menuname] = menulink;
+ } else {
+ global.menu[menuname] = menulink;
+ }
+}
+
+exports.delMenu = function(menuname) {
+ var t = new Array();
+ for(key in global.menu) {
+ if(key != menuname) {
+ t[key] = global.menu[key];
+ }
+ }
+ global.menu = t;
+}
+
+exports.clearMenu = function() {
+ delete global.menu;
+}
+
+global.addMenu = exports.addMenu;
+global.delMenu = exports.delMenu;
+global.clearMenu = exports.clearMenu;
\ No newline at end of file
// first resolve the module making the call and determine layout/purl
var purlClass = layouts;
var purlLayout = layouts.standard();
+ var mainPath = path.dirname(require.main.filename) + "/purls/";
+
+ console.log("main purl path is ", mainPath);
if(purl.pathname == "/") {
if(defaultMainPurl == null) {
// find and load the purl
}
- if(typeof purlClass.layout == "undefined") {
- console.log("set via undefined");
- purlLayout = layouts.standard();
- } else {
- // find and resolve the layout
- }
} else {
// handle the purls
+ var newPurlPath = mainPath+"/"+path.basename(purl.pathname.replace(/\/$/, ""))+".js";
+ console.log("attempting to require: ", newPurlPath);
+ try {
+ var newPurlClass = require(newPurlPath);
+ purlClass = newPurlClass;
+ } catch(err) {
+ console.log("tried to load '%s' for request '%s', but this has failed, returning 302 to /", newPurlPath, purl.pathname);
+ response.writeHead("302", { 'Location': '/' });
+ response.end();
+ return;
+ }
+
+ }
+
+ if(typeof purlClass.layout == "undefined") {
+ console.log("set via undefined");
+ purlLayout = layouts.standard();
+ } else {
+ // find and resolve the layout
+ purlLayout = purlClass.layout();
}
// now we should have a layout and a class
console.log("begin process layout");
if(offset >= splitup.length) {
- response.write("end of caller");
+ //response.write("end of caller");
response.end();
return;
} else if(offset == 0) {
function resolveAndCall(request, response, caller, purlClass, callback) {
//response.write("resolving: \"" + caller + "\"");
+
+ // TODO: do this properly.
if(typeof purlClass[caller] != "undefined") {
purlClass[caller](request, response, callback);
- } else if(typeof purlClass[caller] != "undefined"){
+ } else if(typeof layouts[caller] != "undefined"){
layouts[caller](request, response, callback);
} else {
- response.write("<hr><br>Undefined layout section<br><hr>");
+ response.write("<!-- ERROR:Undefined layout section -->");
callback();
}
//callback();