From 6beaec1f162befb123cc29c60566cd15da515750 Mon Sep 17 00:00:00 2001 From: paulr Date: Sun, 30 Dec 2012 19:33:20 +1100 Subject: [PATCH] functional --- lib/app.js | 14 ++++++++++++ lib/layouts.js | 60 ++++++++++++++++++++++++++++++++++++++++++++++------- lib/wsrequest.js | 37 +++++++++++++++++++++++++-------- myapp.js | 6 ++++- 4 files changed, 99 insertions(+), 18 deletions(-) diff --git a/lib/app.js b/lib/app.js index 0f4348d..df9408b 100644 --- a/lib/app.js +++ b/lib/app.js @@ -8,16 +8,30 @@ var http = require("http"), 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(); diff --git a/lib/layouts.js b/lib/layouts.js index 2679987..fb4d503 100644 --- a/lib/layouts.js +++ b/lib/layouts.js @@ -1,7 +1,7 @@ exports.standard = function () { var lay = ""; lay += ""; - lay += ""; + lay += ""; lay += "
"; return lay; @@ -9,29 +9,39 @@ exports.standard = function () { exports.straight = function () { - var lay = ""; + var lay = ""; lay += ""; - lay += ""; + lay += ""; lay += "
"; - + return lay; } exports.header = function(request, response, callback) { // this function is the default header builder. - response.write("mainname"); + response.write(""+global.njsAppName+""); callback(request, response); } exports.title = function(request, response, callback) { // this is the default title builder - response.write("

mainname

"); + response.write("

"+global.njsAppName+"

"); callback(request, response); } exports.menu = function(request, response, callback) { // this is the default title builder - response.write("
Menuitem 1item2
"); + //response.write("
Menuitem 1item2
"); + if(typeof global.menu != "undefined") { + response.write(""); + for(key in global.menu) { + response.write(""); + console.log("menu: '%s', '%s'", key, global.menu[key]); + } + response.write("
"+key+"
"); + } else { + response.write("No Menu Defined"); + } callback(request, response); } @@ -49,7 +59,11 @@ exports.body = function(request, response, callback) { exports.footer = function(request, response, callback) { // this is the default title builder - response.write("

Copyright PJR.cc"); + if(typeof global.footerText != "undefined") { + response.write(global.footerText); + } else { + response.write("

Copyright PJR.cc"); + } callback(request, response); } @@ -57,3 +71,33 @@ exports.preResponse = function(request, response, callback) { 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 diff --git a/lib/wsrequest.js b/lib/wsrequest.js index 80e4d3c..c0bf4d1 100644 --- a/lib/wsrequest.js +++ b/lib/wsrequest.js @@ -99,6 +99,9 @@ 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/"; + + console.log("main purl path is ", mainPath); if(purl.pathname == "/") { if(defaultMainPurl == null) { @@ -108,14 +111,28 @@ function urlServicer(request, response, purl) { // 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 @@ -143,7 +160,7 @@ function serviceLayout(request, response, purlClass, purlLayout) { 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) { @@ -180,12 +197,14 @@ function serviceLayout(request, response, purlClass, purlLayout) { 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("

Undefined layout section

"); + response.write(""); callback(); } //callback(); diff --git a/myapp.js b/myapp.js index dfc44a0..9e5d7c1 100644 --- a/myapp.js +++ b/myapp.js @@ -1,7 +1,11 @@ var app = require("./lib/app.js"); app.setPort(8888); -//app.setDefaultLayout("standard"); app.setDefaultMainPurl("main.js"); +app.addMenu("name1", "/linkto1"); +app.addMenu("name2", "/linkto2"); +app.addMenu("name3", "/linkto3"); +app.setAppName("MyExampleApp"); +app.setFooterText("

Copyright PJR.cc 2012 ©"); app.start(); -- 1.7.0.4