Skip to content
This repository has been archived by the owner on Jan 10, 2022. It is now read-only.

Commit

Permalink
if current theme is missing, fallback to default theme (cleanslate) s…
Browse files Browse the repository at this point in the history
…o site doesnt choke on server restart, added a general utility file (finding namespaced objects is a lot easier now)
  • Loading branch information
dtan committed Jul 25, 2011
1 parent 7160010 commit 27ef49d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 16 deletions.
1 change: 1 addition & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ function bootApplication(app, next) {
// Translation - after static, set to add mode if appropriate
app.use(translate.translate(app.set('config').language, app.set('language-add')));

calipso.defaultTheme = require(path + '/conf/configuration.js').getDefaultTheme();
// Core calipso router
app.use(calipso.calipsoRouter(app, app.set('config'), function() {
next();
Expand Down
9 changes: 8 additions & 1 deletion conf/configuration.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var mongoose = require('mongoose'),
Schema = mongoose.Schema;

var defaultTheme = 'cleanslate';

/**
* Default configuration manager This file controls the loading, and initial
Expand All @@ -18,7 +20,7 @@ module.exports = function(app, express, next) {
var defaultConfig = {
version:1, // Used to warn - e.g. structural changes require a config reset
cache: false,
theme: 'cleanslate',
theme: defaultTheme,
language: 'en',
install: true,
cryptoKey: createRandomString(),
Expand Down Expand Up @@ -152,6 +154,11 @@ module.exports = function(app, express, next) {

};

// prefer a getter since a property can be overwritten
module.exports.getDefaultTheme = function () {
return defaultTheme;
};

/**
* Load the configuration from the datbase, creating based on the default
* and setting Calipso into install mode if it doesn't exist.
Expand Down
58 changes: 58 additions & 0 deletions lib/Utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* General utility methods
*/

exports = module.exports = {
/**
* Basically like getProperty, different return
* @method hasProperty
* @param ns {string} A period delimited string of the namespace to find, sans root object
* @param obj {object} The root object to search
* @return {boolean} true if property exists, false otherwise
*/
hasProperty: function (ns, obj) {
if (!ns) {
return obj;
}
var nsArray = ns.split('.'),
nsLen = nsArray.length,
newNs;

// if nsLen === 0, then obj is just returned
while (nsLen > 0) {
newNs = nsArray.shift();
if (obj[newNs]) {
obj = obj[newNs];
} else {
return false;
}
nsLen = nsArray.length;
}
return true;
},
/**
* Find a namespaced property
* @method getProperty
* @param ns {string} A period delimited string of the namespace to find, sans root object
* @param obj {object} The root object to search
* @return {object} the object, either the namespaced obejct or the root object
*/
getProperty: function (ns, obj) {
if (!ns) {
return obj;
}
var nsArray = ns.split('.'),
nsLen = nsArray.length,
newNs;

// if nsLen === 0, then obj is just returned
while (nsLen > 0) {
newNs = nsArray.shift();
if (obj[newNs]) {
obj = obj[newNs];
}
nsLen = nsArray.length;
}
return obj;
}
}
40 changes: 25 additions & 15 deletions lib/calipso.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ calipso = exports = module.exports = {
table: require('./Table').CalipsoTable,
link: require('./Link').CalipsoLink,
menu: require('./Menu'),
event: require('./Event'),
event: require('./Event'),
utils: require('./Utils'),
notifyDependenciesOfInit: notifyDependenciesOfInit,
notifyDependenciesOfRoute: notifyDependenciesOfRoute,
e: {},
Expand Down Expand Up @@ -839,7 +840,9 @@ function loadModuleTemplates(module, moduleTemplatePath) {
var templateName = name.replace(/\.([^\.]+)$/,'');

// Load the template - only if not already loaded by theme (e.g. overriden)
if(calipso.theme.cache.modules && calipso.theme.cache.modules[module.name] && calipso.theme.cache.modules[module.name].templates[templateName]) {
var hasTemplate = calipso.utils.hasProperty('theme.cache.modules.' + module.name + '.templates.' + templateName, calipso);

if (hasTemplate) {

// Use the theme version
templates[templateName] = calipso.theme.cache.modules[module.name].templates[templateName];
Expand Down Expand Up @@ -876,26 +879,33 @@ function loadModuleTemplates(module, moduleTemplatePath) {
/**
* Configure a theme using the theme library.
*/
function configureTheme(next) {
function configureTheme(next, overrideTheme) {

var themeName = calipso.app.set('config').theme;
var themeName = overrideTheme ? overrideTheme : calipso.app.set('config').theme;

require('./Theme').Theme(themeName,function(theme) {

calipso.theme = theme;
if (!calipso.theme) {
var dt = calipso.defaultTheme;
calipso.error('THE `' + themeName + '` THEME IS MISSING, USING DEFAULT THEME `' + dt + '`')
configureTheme(next, dt);

// Search for middleware that already has themeStatic tag
calipso.app.stack.forEach(function(middleware,key) {
if(middleware.handle.tag === 'themeStatic') {
// Replace it
var oneDay = 86400000;
var themeStatic = calipso.lib.express.static(calipso.app.path + '/themes/' + themeName + '/public',{maxAge:oneDay});
themeStatic.tag = 'themeStatic';
calipso.app.stack[key].handle = themeStatic;
}
});
} else {

next();
// Search for middleware that already has themeStatic tag
calipso.app.stack.forEach(function(middleware,key) {
if(middleware.handle.tag === 'themeStatic') {
// Replace it
var oneDay = 86400000;
var themeStatic = calipso.lib.express.static(calipso.app.path + '/themes/' + themeName + '/public',{maxAge:oneDay});
themeStatic.tag = 'themeStatic';
calipso.app.stack[key].handle = themeStatic;
}
});

next();
}

});

Expand Down

0 comments on commit 27ef49d

Please sign in to comment.