Permalink
Browse files

Refactored theming system

  • Loading branch information...
1 parent 06c676c commit 109ba2a754f352067dcd51b0cd68ebb3f3b558e5 @cliftonc committed Apr 14, 2011
View
45 app.js
@@ -57,51 +57,20 @@ function bootApplication(app) {
app.use(express.static(path + '/themes/' + theme + '/public')); // Before router to enable dynamic routing
app.use(calipso.calipsoRouter(app,app.set('config')));
- // Setup ejs views as default, with .html as the extension
- app.set('views', path + '/themes/' + theme);
- app.register('.html', require('ejs'));
- app.set('view engine', 'html');
-
- // Some dynamic view helpers
- app.dynamicHelpers({
-
- request: function(req){
- return req;
- },
- user: function(req){
- return req.session.user;
- },
- showDebug: function(req,res){
- return "Raw: \r\n\r\n" + sys.inspect(res.blocks,false,10,false) + "\r\n\r\nRendered:\r\n\r\n" + sys.inspect(res.renderedBlocks,false,10,false);
- },
- hasMessages: function(req){
- return Object.keys(req.session.flash || {}).length;
- },
-
- messages: function(req){
- return function(){
- var msgs = req.flash();
- return Object.keys(msgs).reduce(function(arr, type){
- return arr.concat(msgs[type]);
- }, []);
- }
- }
- });
}
// allow normal node loading if appropriate
if (!module.parent) {
console.log("");
- console.log("\x1b[36m ### ## ## ## ##### #### #### \x1b[0m");
- console.log("\x1b[36m ### #### ## ## # ## ## ## \x1b[0m");
- console.log("\x1b[36m ## ## ## ## ## ## # ## ## ## \x1b[0m");
- console.log("\x1b[36m ## ## ## ## ## ##### ### ## ## \x1b[0m");
- console.log("\x1b[36m ## ###### ## ## ## ## ## ## \x1b[0m");
- console.log("\x1b[36m ### ## ## ## ## ## ## ## ## \x1b[0m");
- console.log("\x1b[36m ### ## ## #### ## ## #### #### \x1b[0m");
+ console.log("\x1b[36m _ _ \x1b[0m");
+ console.log("\x1b[36m ___ __ _| (_)_ __ ___ ___ \x1b[0m");
+ console.log("\x1b[36m / __|/ _` | | | '_ \\/ __|/ _ \\ \x1b[0m");
+ console.log("\x1b[36m| (__| (_| | | | |_) \\__ \\ (_) | \x1b[0m");
+ console.log("\x1b[36m \\___|\\__,_|_|_| .__/|___/\\___/ \x1b[0m");
+ console.log("\x1b[36m |_| \x1b[0m");
console.log("");
-
+
exports.boot(function(app) {
app.listen(3000);
View
@@ -1,8 +1,3 @@
-Place any dependent libraries in here
-
-
-* inflection.js : http://code.google.com/p/inflection-js/
-
- Modified to remove the link to window.
+Core Calipso libraries
View
@@ -13,15 +13,17 @@ module.exports = {
*/
lib: {
fs: require('fs'),
+ path: require('path'),
express: require('express'),
step: require('step'),
sys: require('sys'),
mongoose: require('mongoose'),
url: require('url'),
ejs: require('ejs'),
pager: require("../utils/pager.js")
- },
- data: {}, // Holds temporary config data
+ },
+ theme: {},
+ data: {}, // Holds global config data
modules: {},
/**
@@ -41,25 +43,21 @@ module.exports = {
// Load all the modules
loadModules(calipso,options);
- initModules(calipso,app,function() {
+ // Theme
+ configureTheme(calipso, function() {
+ // Do nothing
+ });
+
+ initModules(calipso, function() {
// Do nothing
});
return function(req,res,next) {
res.menu = {admin:[],primary:[],secondary:[]};
- res.blocks = {header:[],
- footer:[],
- left:[],
- body:[],
- right:[]};
-
- res.renderedBlocks = {header:[],
- footer:[],
- left:[],
- body:[],
- right:[]};
+ res.blocks = {};
+ res.renderedBlocks = {};
routeModules(req,res,next,calipso,app);
@@ -82,13 +80,18 @@ function routeModules(req,res,next,calipso,app) {
},
function reloadConfig() {
if(res.reloadConfig) {
+
+ var self = this;
app.set('config',calipso.config);
calipso.config = null;
configureLogging(calipso,app.set('config'));
loadModules(calipso,app.set('config'));
res.reloadConfig = null;
- initModules(calipso,app,this);
- } else {
+ initModules(calipso,function() {
+ configureTheme(calipso, self);
+ });
+
+ } else {
this();
}
@@ -104,16 +107,18 @@ function routeModules(req,res,next,calipso,app) {
app.install = false;
res.redirect("/admin/install");
} else {
+
// If we have now reached the end!
switch(res.statusCode) {
case 404:
- res.render("404",{layout:'error'});
+ res.send("404");
break;
case 500:
- res.render("500",{menu:res.menu,blocks:res.blocks,renderedBlocks:res.renderedBlocks});
+ res.send("500");
break;
- case 200:
- res.render("pages/index",{menu:res.menu,blocks:res.blocks,renderedBlocks:res.renderedBlocks});
+ case 200:
+ calipso.theme.render(req,res,next);
+ // res.render("pages/index",{menu:res.menu,blocks:res.blocks,renderedBlocks:res.renderedBlocks});
break;
default:
// Do Nothing
@@ -169,14 +174,14 @@ function configureLogging(calipso,options) {
}
-function initModules(calipso,app,next) {
+function initModules(calipso,next) {
calipso.lib.step(
function loadAllModules() {
var group = this.group();
for(var module in calipso.modules) {
if(calipso.modules[module].enabled) {
- calipso.modules[module].fn.init(calipso.modules[module],app,group());
+ calipso.modules[module].fn.init(calipso.modules[module],calipso.app,group());
}
};
},
@@ -224,12 +229,55 @@ function loadModules(calipso,options) {
var enabled = configuredModules[name] ? configuredModules[name].enabled : false;
calipso.modules[name] = {name:name,
- enabled:enabled,
- fn:require('../modules/' + name + '/' + name + '.module'),
- router:require('./moduleRouter').Router()};
+ enabled:enabled,
+ fn:require('../modules/' + name + '/' + name + '.module'),
+ router:require('./calipsoRouter').Router(name),
+ templates:loadModuleTemplates(calipso,__dirname + '/../modules/' + name + '/templates')};
}
});
return calipso.modules;
+};
+
+/**
+ * Pre load all the templates in a module, synch, but only happens on app start up and config reload
+ * This is attached to the templates attribute so used later.
+ *
+ * @param calipso
+ * @param moduleTemplatePath
+ * @returns template object
+ */
+function loadModuleTemplates(calipso,moduleTemplatePath) {
+
+ var templates = {};
+
+ if(calipso.lib.path.existsSync(moduleTemplatePath)) {
+
+ calipso.lib.fs.readdirSync(moduleTemplatePath).forEach(function(name){
+
+ var template=calipso.lib.fs.readFileSync(moduleTemplatePath + "/" + name, 'utf8');
+ if(template) {
+ templates[name.replace(/\.html$/,'')] = calipso.lib.ejs.compile(template);
+ }
+
+ });
+
+ return templates;
+
+ } else {
+ return;
+ }
+
+};
+
+function configureTheme(calipso, next) {
+
+ var themeName = calipso.app.set('config').theme;
+
+ require('./calipsoTheme').Theme(themeName,function(theme) {
+ calipso.theme = theme;
+ next();
+ });
+
};
@@ -1,4 +1,3 @@
-
/*!
* Connect - content loader
* Copyright(c) 2011 Clifton Cunningham
@@ -9,34 +8,28 @@
*/
var url = require('url'),Step = require('step'),fs = require('fs'),path = require('path'), calipso = require("./calipso");
-module.exports.Router = function() {
+module.exports.Router = function(moduleName) {
return {
-
+
+ moduleName: moduleName,
routes: [],
configured: false,
addRoute: function(path, fn, options, next) {
var router = this;
- options = options ? options : {end:true, admin:false, templatePath:''};
+ // Default options
+ options = options ? options : {end:true, admin:false};
+
+ // Can't do any real checking here as everything is initialised in parallel.
+ router.routes.push({path: path, fn: fn, options: options});
+ next();
- if(options.templatePath) {
- loadTemplate(options.templatePath, function(data) {
- if(data) {
- options.templateData = data;
- }
- router.routes.push({path: path, fn: fn, options: options});
- next();
- });
- } else {
- options.templateData = '';
- router.routes.push({path: path, fn: fn, options: options});
- next();
- }
},
route: function(req, res, next) {
+ var router = this;
var requestUrl = url.parse(req.url);
var routes = this.routes;
@@ -50,7 +43,8 @@ module.exports.Router = function() {
var keys = [];
var route = routes[i];
- var template = route.options.templateData;
+ var template = null, block = "";
+
var routeMethod = "GET";
var routeRegEx;
@@ -67,7 +61,21 @@ module.exports.Router = function() {
// Check to see if we matched a non /* route to flag a 404 later
res.routeMatched = !(routeRegEx.toString() === "/.*/") || res.routeMatched;
+
+ // Lookup the template for this route
+ if(route.options.template) {
+ template = calipso.modules[router.moduleName].templates[route.options.template];
+ if(!template && route.options.template) {
+ calipso.error("The specified template: " + route.options.template + " does not exist in the module: " + router.moduleName);
+ }
+ }
+ // Initialise the block if it doesn't exist
+ if(route.options.block && !res.renderedBlocks[block]) {
+ block = route.options.block;
+ res.renderedBlocks[block] = [];
+ }
+
// Copy over any params that make sense
req.moduleParams = [];
@@ -87,7 +95,7 @@ module.exports.Router = function() {
// Check to see if it requires admin access
if(!route.options.admin || (route.options.admin && req.session.user && req.session.user.isAdmin)) {
- route.fn(req,res,group(),template);
+ route.fn(req,res,template,block,group());
} else {
res.statusCode = 401;
res.redirect("/");
@@ -112,30 +120,6 @@ module.exports.Router = function() {
}
-
-/**
- * Load a template
- */
-
-function loadTemplate(templatePath, next) {
-
- path.exists(templatePath,function(exists) {
- if(exists) {
- fs.readFile(templatePath, 'utf8', function(err,data) {
- if(!err) {
- next(data);
- } else {
- next('');
- }
- });
- } else {
- calipso.error("CANT FIND " + templatePath);
- next('');
- }
- });
-
-}
-
/**
* Normalize the given path string,
* returning a regular expression.
Oops, something went wrong.

0 comments on commit 109ba2a

Please sign in to comment.