moved globals into global.njspurls.
authorpaulr <paulr@tv.pjr.cc>
Sun, 30 Dec 2012 09:28:29 +0000 (20:28 +1100)
committerpaulr <paulr@tv.pjr.cc>
Sun, 30 Dec 2012 09:28:29 +0000 (20:28 +1100)
lib/app.js
lib/layouts.js
lib/wsrequest.js
myapp.js

index df9408b..4fc59b0 100644 (file)
@@ -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) {
index fb4d503..7860919 100644 (file)
@@ -19,24 +19,24 @@ exports.straight = function () {
 
 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 {
@@ -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("<br><br><font size=\"-1\"><i>Copyright PJR.cc</i></font>");
        }
@@ -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
index c0bf4d1..20e71ba 100644 (file)
@@ -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") {
index 9e5d7c1..da5713d 100644 (file)
--- 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");