From 45842e97cccbeffe43832f88ad97723e3a4bda2a Mon Sep 17 00:00:00 2001 From: paulr Date: Sun, 30 Dec 2012 20:28:29 +1100 Subject: [PATCH] moved globals into global.njspurls. --- lib/app.js | 22 +++++++++++++++------- lib/layouts.js | 30 +++++++++++++++--------------- lib/wsrequest.js | 34 +++++++++++++++++++++++++++++----- myapp.js | 3 ++- 4 files changed, 61 insertions(+), 28 deletions(-) diff --git a/lib/app.js b/lib/app.js index df9408b..4fc59b0 100644 --- a/lib/app.js +++ b/lib/app.js @@ -1,3 +1,4 @@ +global.njspurls = new Object(); var http = require("http"), url = require("url"), @@ -8,18 +9,25 @@ var http = require("http"), 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) { @@ -27,10 +35,10 @@ 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) { diff --git a/lib/layouts.js b/lib/layouts.js index fb4d503..7860919 100644 --- a/lib/layouts.js +++ b/lib/layouts.js @@ -19,24 +19,24 @@ exports.straight = function () { exports.header = function(request, response, callback) { // this function is the default header builder. - response.write(""+global.njsAppName+""); + response.write(""+global.njspurls.njsAppName+""); callback(request, response); } exports.title = function(request, response, callback) { // this is the default title builder - response.write("

"+global.njsAppName+"

"); + response.write("

"+global.njspurls.njsAppName+"

"); callback(request, response); } exports.menu = function(request, response, callback) { // this is the default title builder //response.write("
Menuitem 1item2
"); - if(typeof global.menu != "undefined") { + if(typeof global.njspurls.menu != "undefined") { response.write(""); - for(key in global.menu) { - response.write(""); - console.log("menu: '%s', '%s'", key, global.menu[key]); + for(key in global.njspurls.menu) { + response.write(""); + console.log("menu: '%s', '%s'", key, global.njspurls.menu[key]); } response.write("
"+key+""+key+"
"); } else { @@ -59,8 +59,8 @@ exports.body = function(request, response, callback) { 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("

Copyright PJR.cc"); } @@ -76,11 +76,11 @@ exports.preResponse = function(request, response, 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; + 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; } } @@ -98,6 +98,6 @@ exports.clearMenu = function() { 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 diff --git a/lib/wsrequest.js b/lib/wsrequest.js index c0bf4d1..20e71ba 100644 --- a/lib/wsrequest.js +++ b/lib/wsrequest.js @@ -99,18 +99,43 @@ function urlServicer(request, response, purl) { // 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"; @@ -124,7 +149,6 @@ function urlServicer(request, response, purl) { response.end(); return; } - } if(typeof purlClass.layout == "undefined") { diff --git a/myapp.js b/myapp.js index 9e5d7c1..da5713d 100644 --- a/myapp.js +++ b/myapp.js @@ -1,7 +1,8 @@ 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"); -- 1.7.0.4