From aeb4bb098477429e9df445336d73f04d0e8bf4e9 Mon Sep 17 00:00:00 2001 From: Raynos Date: Fri, 25 Nov 2011 20:45:11 +0000 Subject: [PATCH] Refactored file structure, now uses html, css and js folders --- README.md | 10 +- examples/simple/trinity/{ => css}/base.css | 0 examples/simple/trinity/{ => html}/base.html | 0 examples/simple/trinity/{ => html}/child.html | 0 .../simple/trinity/{ => html}/static.html | 0 examples/simple/trinity/{ => js}/base.js | 0 examples/simple/trinity/{ => js}/child.js | 0 package.json | 2 +- src/trinity.js | 91 ++++++++----------- tests/test-trinity.js | 6 +- tests/trinity/{ => css}/test.css | 0 tests/trinity/{ => html}/nested.html | 0 tests/trinity/{ => html}/other.html | 0 tests/trinity/{ => html}/simple.html | 0 tests/trinity/{ => html}/static.html | 0 tests/trinity/{ => html}/test.html | 0 tests/trinity/{ => js}/nested.js | 0 tests/trinity/{ => js}/test.js | 0 18 files changed, 48 insertions(+), 61 deletions(-) rename examples/simple/trinity/{ => css}/base.css (100%) rename examples/simple/trinity/{ => html}/base.html (100%) rename examples/simple/trinity/{ => html}/child.html (100%) rename examples/simple/trinity/{ => html}/static.html (100%) rename examples/simple/trinity/{ => js}/base.js (100%) rename examples/simple/trinity/{ => js}/child.js (100%) rename tests/trinity/{ => css}/test.css (100%) rename tests/trinity/{ => html}/nested.html (100%) rename tests/trinity/{ => html}/other.html (100%) rename tests/trinity/{ => html}/simple.html (100%) rename tests/trinity/{ => html}/static.html (100%) rename tests/trinity/{ => html}/test.html (100%) rename tests/trinity/{ => js}/nested.js (100%) rename tests/trinity/{ => js}/test.js (100%) diff --git a/README.md b/README.md index 02d98ea..06bbce8 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Heavy work in progress! Your HTML template - // base.html + // html/base.html

Some content

@@ -23,7 +23,7 @@ Your HTML template The associated CSS. This is a nice place to put your CSS, all the CSS will be minified for you - // base.css + // css/base.css p { color: blue } @@ -34,7 +34,7 @@ The associated CSS. This is a nice place to put your CSS, all the CSS will be mi Your view logic, using plain old DOM - // base.js + // js/base.js var p = frag.firstChild.getElementsByClassName("container")[0]; p.textContent = data.text; var fragment = load("child", data); @@ -43,12 +43,12 @@ Your view logic, using plain old DOM Another HTML template, this is a small shell - // child.html + // html/child.html

Another piece of view logic - // child.js + // js/child.js frag.firstChild.textContent = "Another p that was loaded!"; The express server diff --git a/examples/simple/trinity/base.css b/examples/simple/trinity/css/base.css similarity index 100% rename from examples/simple/trinity/base.css rename to examples/simple/trinity/css/base.css diff --git a/examples/simple/trinity/base.html b/examples/simple/trinity/html/base.html similarity index 100% rename from examples/simple/trinity/base.html rename to examples/simple/trinity/html/base.html diff --git a/examples/simple/trinity/child.html b/examples/simple/trinity/html/child.html similarity index 100% rename from examples/simple/trinity/child.html rename to examples/simple/trinity/html/child.html diff --git a/examples/simple/trinity/static.html b/examples/simple/trinity/html/static.html similarity index 100% rename from examples/simple/trinity/static.html rename to examples/simple/trinity/html/static.html diff --git a/examples/simple/trinity/base.js b/examples/simple/trinity/js/base.js similarity index 100% rename from examples/simple/trinity/base.js rename to examples/simple/trinity/js/base.js diff --git a/examples/simple/trinity/child.js b/examples/simple/trinity/js/child.js similarity index 100% rename from examples/simple/trinity/child.js rename to examples/simple/trinity/js/child.js diff --git a/package.json b/package.json index 40cd2db..f3e3697 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "trinity", - "version": "0.1.6", + "version": "0.1.7", "author": "Jake Verbaten ", "main": "src/trinity", "dependencies": { diff --git a/src/trinity.js b/src/trinity.js index 3ee88e7..49f94dd 100644 --- a/src/trinity.js +++ b/src/trinity.js @@ -42,13 +42,18 @@ var cachedRead = (function () { var cache = {}; function readFile(name, cb) { - var file = cache[name]; - if (file) { - return cb(null, file); + var obj = cache[name]; + if (!obj) { + cache[name] = {}; + } else if (obj.file) { + return cb(null, obj.file); + } else if (obj.err) { + return cb(obj.err); } fs.readFile(name, function (err, file) { - if (file) cache[name] = file; + if (file) cache[name].file = file; + if (err) cache[name].err = err; cb.apply(this, arguments); }); } @@ -65,10 +70,8 @@ var Trinity = { @param String uri - trinity to load @param Function cb - callback to fire when finished - @param optional Boolean sync - sync flag as to whether - the operation should be synchronous */ - create: function _create(uri, cb, sync) { + create: function _create(uri, cb) { var count = 3; function next() { @@ -87,23 +90,21 @@ var Trinity = { next(); } - this.createJavaScript(uri, swallowFileDoesNotExist, sync) + this.createJavaScript(uri, swallowFileDoesNotExist) - this.createDocumentFragment(uri, errorHandler, sync); + this.createDocumentFragment(uri, errorHandler); - this.createCSS(uri, swallowFileDoesNotExist, sync); + this.createCSS(uri, swallowFileDoesNotExist); }, /* adds the css text to the cssNode @param String uri - file to load @param Function cb - callback to fire when finished - @param optional Boolean sync - sync flag as to whether - the operation should be synchronous */ - createCSS: function _createCSS(uri, cb, sync) { + createCSS: function _createCSS(uri, cb) { var that = this, - url = path.join(config.path,uri) + ".css"; + url = path.join(config.path, "css", uri) + ".css"; function readFile(error, file) { if (error) return cb(error); @@ -112,7 +113,7 @@ var Trinity = { cb(null, file); } - this.readFile(url, readFile, sync); + this.readFile(url, readFile); }, /* creates a document fragment from the html at the ui @@ -120,12 +121,10 @@ var Trinity = { @param String uri - file to load @param Function cb - callback to fire when loaded. Get's passed the document fragment build from the HTML - @param optional Boolean sync - sync flag as to whether - the operation should be synchronous */ - createDocumentFragment: function _createDocumentFragment(uri, cb, sync) { + createDocumentFragment: function _createDocumentFragment(uri, cb) { var that = this, - url = path.join(config.path, uri) + ".html"; + url = path.join(config.path, "html", uri) + ".html"; function readFile(error, file) { if (error) return cb(error); @@ -140,11 +139,11 @@ var Trinity = { adoptNode(fragment, that.doc); that.frag = fragment; - + cb(null, fragment); } - this.readFile(url, readFile, sync); + this.readFile(url, readFile); }, /* loads the javascript file at the uri @@ -152,12 +151,10 @@ var Trinity = { @param String uri - file to load @param Function cb - callback to fire when loaded. get's passed the function f created from the files source code - @param optional Boolean sync - sync flag as to whether - the operation should be synchronous */ - createJavaScript: function _loadJavaScript(uri, cb, sync) { + createJavaScript: function _loadJavaScript(uri, cb) { var that = this, - url = path.join(config.path, uri) + ".js"; + url = path.join(config.path, "js", uri) + ".js"; function readFile(err, file) { if (err) return cb(err); @@ -170,7 +167,7 @@ var Trinity = { }); } - this.readFile(url, readFile, sync); + this.readFile(url, readFile); }, /* Make a new Trinity @@ -204,13 +201,14 @@ var Trinity = { node.label() === "load" ) { var module = node.value[1][0][1]; + modules.push(module); } }); var count = modules.length * 3 + 1; - function next() { + function next(err) { count--; count === 0 && cb(); } @@ -224,10 +222,12 @@ var Trinity = { } modules.forEach(function (module) { - var url = path.join(config.path, module); - cachedRead(url + ".js", recurseForJavaScript); - cachedRead(url + ".css", next); - cachedRead(url + ".html", next); + var jsurl = path.join(config.path, "js", module); + var cssurl = path.join(config.path, "css", module); + var htmlurl = path.join(config.path, "html", module); + cachedRead(jsurl + ".js", recurseForJavaScript); + cachedRead(cssurl + ".css", next); + cachedRead(htmlurl + ".html", next); }); next(); @@ -237,20 +237,9 @@ var Trinity = { @param String url - url to read @param Function cb - callback to fire on reading - @param Boolean sync - whether to use sync method */ - readFile: function _readFile(url, cb, sync) { - if (!sync) { - cachedRead(url, cb); - } else { - var file, err = null; - try { - file = fs.readFileSync(url); - } catch (e) { - err = e; - } - cb(err, file); - } + readFile: function _readFile(url, cb) { + cachedRead(url, cb); } }; @@ -264,10 +253,8 @@ var Trinity = { @param Function cb - callback to invoke when ready, passes the document fragment and another load function to invoke - @param optional Boolean sync - sync flag as to whether - the operation should be synchronous */ -function load(doc, cssNode, uri, json, cb, sync) { +function load(doc, cssNode, uri, json, cb) { var that = this, trinity = Trinity.make(doc, cssNode), _load = load.bind(null, doc, cssNode), @@ -293,14 +280,14 @@ function load(doc, cssNode, uri, json, cb, sync) { var ret; _load(uri, json, function _callbackProxy(err, frag) { ret = frag; - }, true); + }); return ret; }); cb(null, trinity.frag, _load); } - trinity.create(uri, next, sync); + trinity.create(uri, next); } @@ -314,7 +301,7 @@ var Statics = { addStaticCSS: function _addStaticCSS() { var link = this.doc.createElement("link"); link.rel = "stylesheet"; - link.href = config.publicPath + "/" + config.static + ".css"; + link.href = path.join(config.publicPath, "css", config.static) + ".css"; this.doc.head.appendChild(link); }, /* @@ -323,7 +310,7 @@ var Statics = { addStaticJS: function _addStaticJS() { var script = this.doc.createElement("script"); script.type = "text/javascript"; - script.src = config.publicPath + "/" + config.static + ".js"; + script.src = path.join(config.publicPath, "js", config.static) + ".js"; this.doc.body.appendChild(script); }, /* @@ -366,7 +353,7 @@ var Statics = { } // load static.html - jsdom.env(config.path + config.static + ".html", handlEnv); + jsdom.env(config.path + "/html/" + config.static + ".html", handlEnv); } }; diff --git a/tests/test-trinity.js b/tests/test-trinity.js index cdc75e2..3c7cafe 100644 --- a/tests/test-trinity.js +++ b/tests/test-trinity.js @@ -33,7 +33,7 @@ module.exports = { var children = [].slice.call(doc.head.childNodes); test.ok(children.some(function (node) { return node.tagName === "LINK" && - node.href === "other/static.css"; + node.href === "other/css/static.css"; })); trinity.set("publicPath", "trinity"); test.done(); @@ -83,7 +83,7 @@ module.exports = { var children = [].slice.call(doc.head.childNodes); test.ok(children.some(function (node) { return node.tagName === "LINK" && - node.href === "trinity/static.css"; + node.href === "trinity/css/static.css"; })); test.done(); }); @@ -95,7 +95,7 @@ module.exports = { var children = [].slice.call(doc.body.childNodes); test.ok(children.some(function (node) { return node.tagName === "SCRIPT" && - node.src === "trinity/static.js"; + node.src === "trinity/js/static.js"; })); test.done(); }); diff --git a/tests/trinity/test.css b/tests/trinity/css/test.css similarity index 100% rename from tests/trinity/test.css rename to tests/trinity/css/test.css diff --git a/tests/trinity/nested.html b/tests/trinity/html/nested.html similarity index 100% rename from tests/trinity/nested.html rename to tests/trinity/html/nested.html diff --git a/tests/trinity/other.html b/tests/trinity/html/other.html similarity index 100% rename from tests/trinity/other.html rename to tests/trinity/html/other.html diff --git a/tests/trinity/simple.html b/tests/trinity/html/simple.html similarity index 100% rename from tests/trinity/simple.html rename to tests/trinity/html/simple.html diff --git a/tests/trinity/static.html b/tests/trinity/html/static.html similarity index 100% rename from tests/trinity/static.html rename to tests/trinity/html/static.html diff --git a/tests/trinity/test.html b/tests/trinity/html/test.html similarity index 100% rename from tests/trinity/test.html rename to tests/trinity/html/test.html diff --git a/tests/trinity/nested.js b/tests/trinity/js/nested.js similarity index 100% rename from tests/trinity/nested.js rename to tests/trinity/js/nested.js diff --git a/tests/trinity/test.js b/tests/trinity/js/test.js similarity index 100% rename from tests/trinity/test.js rename to tests/trinity/js/test.js