+global.njspurls = new Object();
var http = require("http"),
url = require("url"),
defaultLayout = "standard",
defaultMain = null,
addCss = null,
- layouts = require("./layouts.js");
+ layouts = require("./layouts.js"),
addJs = null;
-
-global.njsAppName = "NJSPURLS Web App";
+global.njspurls.njsAppName = "NJSPURLS Web App";
exports.setAppName = function(name) {
- global.njsAppName = name;
+ global.njspurls.njsAppName = name;
+}
+
+exports.setPurlPath = function(setpath) {
+ // default purl path is set based on the module.parent if its relative
+ var basename = path.dirname(module.parent.filename);
+ var mainpath = path.resolve(basename, setpath);
+ console.log("setting purl path to ", mainpath);
+ global.njspurls.mainPath = mainpath;
}
exports.setFooterText = function(text) {
- global.footerText = text;
+ global.njspurls.footerText = text;
}
exports.setPort = function(port) {
}
exports.addMenu = function(name, link) {
- global.addMenu(name, link);
+ global.njspurls.addMenu(name, link);
}
-global.addMenu = layouts.addMenu;
+global.njspurls.addMenu = layouts.addMenu;
exports.addCss = function(cssname) {
if(addCss == null) {
exports.header = function(request, response, callback) {
// this function is the default header builder.
- response.write("<title>"+global.njsAppName+"</title>");
+ response.write("<title>"+global.njspurls.njsAppName+"</title>");
callback(request, response);
}
exports.title = function(request, response, callback) {
// this is the default title builder
- response.write("<h1>"+global.njsAppName+"</h1>");
+ response.write("<h1>"+global.njspurls.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>");
- if(typeof global.menu != "undefined") {
+ if(typeof global.njspurls.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]);
+ for(key in global.njspurls.menu) {
+ response.write("<td><a href=\""+global.njspurls.menu[key]+"\">"+key+"</a></td>");
+ console.log("menu: '%s', '%s'", key, global.njspurls.menu[key]);
}
response.write("</tr></table>");
} else {
exports.footer = function(request, response, callback) {
// this is the default title builder
- if(typeof global.footerText != "undefined") {
- response.write(global.footerText);
+ if(typeof global.njspurls.footerText != "undefined") {
+ response.write(global.njspurls.footerText);
} else {
response.write("<br><br><font size=\"-1\"><i>Copyright PJR.cc</i></font>");
}
// 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;
+ if(typeof global.njspurls.menu == "undefined") {
+ global.njspurls.menu = new Array();
+ global.njspurls.menu[menuname] = menulink;
} else {
- global.menu[menuname] = menulink;
+ global.njspurls.menu[menuname] = menulink;
}
}
delete global.menu;
}
-global.addMenu = exports.addMenu;
-global.delMenu = exports.delMenu;
-global.clearMenu = exports.clearMenu;
\ No newline at end of file
+global.njspurls.addMenu = exports.addMenu;
+global.njspurls.delMenu = exports.delMenu;
+global.njspurls.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/";
+ var mainPath = "";
+
+ if(typeof global.njspurls.mainPath != "undefined") {
+ mainPath = global.njspurls.mainPath;
+ console.log("gettingmainpath from global: ", mainPath);
+ } else {
+ mainPath = path.dirname(require.main.filename) + "/purls/";
+ }
console.log("main purl path is ", mainPath);
if(purl.pathname == "/") {
if(defaultMainPurl == null) {
console.log("set purl class to layouts");
- purlClass = layouts;
+
+ var mainPurlClassPath = mainPath+"/main.js";
+ console.log("attempting to load main.js as the main purl class");
+ try {
+ var mainPurlClass = require(mainPurlClassPath);
+ purlClass = mainPurlClass;
+ console.log("main.js exists");
+ } catch(err) {
+ console.log("main.js doesnt exist, using default");
+ }
} else {
- // find and load the purl
+ // find and load the purl, we'll use main.js
+ var mainPurlClassPath = mainPath+"/"+defaultMainPurl+".js";
+
+ console.log("attempting to load main.js as the main purl class");
+ try {
+ var mainPurlClass = require(mainPurlClassPath);
+ purlClass = mainPurlClass;
+ console.log("main.js exists");
+ } catch(err) {
+ console.log("main.js doesnt exist, using default");
+ }
}
-
} else {
// handle the purls
var newPurlPath = mainPath+"/"+path.basename(purl.pathname.replace(/\/$/, ""))+".js";
response.end();
return;
}
-
}
if(typeof purlClass.layout == "undefined") {
var app = require("./lib/app.js");
app.setPort(8888);
-app.setDefaultMainPurl("main.js");
+app.setPurlPath("./purls");
+app.setDefaultMainPurl("main");
app.addMenu("name1", "/linkto1");
app.addMenu("name2", "/linkto2");
app.addMenu("name3", "/linkto3");