semi-functional
authorPaul J R <me@pjr.cc>
Sat, 29 Dec 2012 20:54:58 +0000 (07:54 +1100)
committerPaul J R <me@pjr.cc>
Sat, 29 Dec 2012 20:54:58 +0000 (07:54 +1100)
lib/app.js
lib/layouts.js
lib/myparse.js [deleted file]
lib/wsrequest.js
myapp.js [new file with mode: 0644]

index 07718dc..0f4348d 100644 (file)
@@ -6,7 +6,9 @@ var http = require("http"),
     req = require("./wsrequest.js"),
     localPort = 8888,
     defaultLayout = "standard",
-    defaultMain = null;
+    defaultMain = null,
+    addCss = null,
+    addJs = null;
 
 
 
@@ -16,21 +18,44 @@ exports.setPort = function(port) {
        localPort = port;
 }
 
-exports.start = function() {
-       // initialise req
-       req.setDefaultLayout(defaultLayout);
-       req.setDefaultMain(defaultMain);
-       
+exports.addCss = function(cssname) {
+       if(addCss == null) {
+               addCss = new Array();
+               // turn it into an array;
+               addCss.push(cssname);
+       } else {
+               addCss.push(cssname);
+       }
        
-       http.createServer(req.wsRequest).listen(localPort);
+}
 
-       console.log("Server running on port: ", localPort);
+exports.addJS = function(js) {
+       if(addJs == null) {
+               addJs = new Array();
+               addJs.push(js);
+       } else {
+               addJs.push(js);
+       }
 }
 
 exports.setDefaultLayout = function(layout) {
        defaultLayout = layout;
 }
 
-exports.setDefaultMain = function(main) {
+exports.setDefaultMainPurl = function(main) {
        defaultMain = main;
-}
\ No newline at end of file
+}
+
+
+exports.start = function() {
+       // initialise req
+       //req.setDefaultLayout(defaultLayout);
+       //req.setDefaultMain(defaultMain);
+       //req.setExtraCss(addCss);
+       //req.setExtraJs(assJs);
+       
+       
+       http.createServer(req.wsRequest).listen(localPort);
+
+       console.log("Server running on port: ", localPort);
+}
index a6c0a81..2679987 100644 (file)
@@ -1,8 +1,8 @@
 exports.standard = 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><td><?njs footer ?></td></tr></table>";
+       lay += "<tr valign=\"top\"><td width=\"120px\"><?njs sidebar ?></td><td><?njs body ?></td></tr>";
+       lay += "<tr><td><?njs footer ?></td></tr></table><?njs totalend ?></body></html>";
        
        return lay;
 }
@@ -12,7 +12,48 @@ exports.straight = function () {
        var lay = "<html><head><?njs header ?></head><body><?nfs 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><td><?njs footer ?></td></tr></table>";
+       lay += "<tr><td><?njs footer ?></td></tr></table><?njs totalend ?></body></html>";
        
        return lay;
-}
\ No newline at end of file
+}
+
+exports.header = function(request, response, callback) {
+       // this function is the default header builder.
+       response.write("<title>mainname</title>");
+       callback(request, response);
+}
+
+exports.title = function(request, response, callback) {
+       // this is the default title builder
+       response.write("<h1>mainname</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>");
+       callback(request, response);
+}
+
+exports.sidebar = function(request, response, callback) {
+       // this is the default title builder
+       response.write("Sidebar!");
+       callback(request, response);
+}
+
+exports.body = function(request, response, callback) {
+       // this is the default title builder
+       response.write("Body!");
+       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>");
+       callback(request, response);
+}
+
+exports.preResponse = function(request, response, callback) {
+       console.log("in preresponse - doing nothing");
+       callback();
+}
diff --git a/lib/myparse.js b/lib/myparse.js
deleted file mode 100644 (file)
index 088dd7a..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-var path = require("fs");
-
-function useBody() {
-       return;
-}
-
-function parse(base, purl, response, servicer) {
-       var pathCheck = "./lib/web_"+base+".js";
-       console.log("path check on ", pathCheck);
-       fs.exists(pathCheck, function (exists) {
-               if(!exists) console.log("not exists: ", base);
-               else {
-                       servicer(purl, response, parse);
-                       console.log("base exists: ", base);
-               }
-       });
-}
-
-exports.useBody = useBody;
-exports.parse = parse;
-exports.process = process;
index a7ad086..80e4d3c 100644 (file)
@@ -1,8 +1,15 @@
 var url = require("url");
 var path = require("path");
 var fs = require("fs");
-var myparse = require("./myparse.js");
+//var myparse = require("./myparse.js");
 var webmain = require("./webmain.js");
+var layouts = require("./layouts.js");
+
+// global stuff
+var extraCss = new Array();
+var extraJs = new Array();
+//var defaultLayout = null;
+var defaultMainPurl = null;
 
 //var staticExtensions = ["html", "gif", "jpg", "css", "js", "ico"];
 
@@ -45,6 +52,10 @@ function wsRequest(request, response) {
 
        // now we need to find the extension used
        // to serve the request based no the first purl
+       
+       
+       /*
+        * 
        if(purl.pathname == "/") {
                console.log("Serv main");
                webmain.serveMain(request, response, function (request, response) {
@@ -74,8 +85,110 @@ function wsRequest(request, response) {
                }
                return;
        });
+       */
+       
+       urlServicer(request, response, purl);
+       
+       
+       return;
+}
+
+function urlServicer(request, response, purl) {
+       console.log("url servicer called for ", purl);
+       
+       // first resolve the module making the call and determine layout/purl
+       var purlClass = layouts;
+       var purlLayout = layouts.standard();
+       
+       if(purl.pathname == "/") {
+               if(defaultMainPurl == null) {
+                       console.log("set purl class to layouts");
+                       purlClass = layouts;
+               } else {
+                       // 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
+       }
+       
+       // now we should have a layout and a class
+       if(typeof purlClass.preResponse != "undefined") {
+               purlClass.preResponse(request, response, function() {
+                       serviceLayout(request, response, purlClass, purlLayout);                        
+               });
+       } else {
+               serviceLayout(request, response, purlClass, purlLayout);
+       }
        
+       return;
+}
+
+function serviceLayout(request, response, purlClass, purlLayout) {
+       var reallay = "start:"+purlLayout+":end";
+       var splitup = reallay.split("<?njs");
+       var offset=0;
+
+       console.log("inservicer: ", purlLayout);
        
+       function processLayout() {
+               var output = "";
+               
+               console.log("begin process layout");
+               
+               if(offset >= splitup.length) {
+                       response.write("end of caller");
+                       response.end();
+                       return;
+               } else if(offset == 0) {
+                       console.log("write for offset 0");
+                       output = splitup[0].replace(/^start:/g, "");
+                       if(splitup.length == 1) output = output.replace(/:end$/g, "");
+                       console.log("did write: ", output);
+                       response.write(output);
+                       offset++;
+                       processLayout();
+               } else {
+                       var thispart = splitup[offset].split("?>");
+                       var caller = thispart[0].trim();
+                       
+                       console.log("in parts for bits: ", thispart);
+                       console.log("and caller is: ", caller);
+                       
+                       // here we resolve and call the caller....
+                       //response.write("calling: " + caller);
+                       resolveAndCall(request, response, caller, purlClass, function () {
+                               output = thispart[1].replace(/:end$/g, "");
+                               response.write(output);
+                               offset++;
+                               processLayout();
+                       });
+               }
+       }
+       
+       processLayout();
+       
+       //response.write("servicer");
+       //response.end();
+}
+
+function resolveAndCall(request, response, caller, purlClass, callback) {
+       //response.write("resolving: \"" + caller + "\"");
+       if(typeof purlClass[caller] != "undefined") {
+               purlClass[caller](request, response, callback);
+       } else if(typeof purlClass[caller] != "undefined"){
+               layouts[caller](request, response, callback);
+       } else {
+               response.write("<hr><br>Undefined layout section<br><hr>");
+               callback();
+       }
+       //callback();
        return;
 }
 
diff --git a/myapp.js b/myapp.js
new file mode 100644 (file)
index 0000000..dfc44a0
--- /dev/null
+++ b/myapp.js
@@ -0,0 +1,7 @@
+var app = require("./lib/app.js");
+
+app.setPort(8888);
+//app.setDefaultLayout("standard");
+app.setDefaultMainPurl("main.js");
+
+app.start();