Skip to content

Commit

Permalink
Fix for incorrect setting of calipso as a global var in lib/Menu (was…
Browse files Browse the repository at this point in the history
… hiding some issues in other modules such as lib/Helper)
  • Loading branch information
cliftonc committed Aug 11, 2011
1 parent acb8e41 commit 2e4c9db
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 166 deletions.
5 changes: 5 additions & 0 deletions lib/Blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
*
*/

var rootpath = process.cwd() + '/',
path = require('path');

/**
* Holder for rendered blocks (get / set)
* Idea is that this will give us an opportunity
Expand All @@ -25,7 +28,9 @@ function RenderedBlocks(cache) {
*/
RenderedBlocks.prototype.set = function (block, content, layout, next) {

var calipso = require(path.join(rootpath, 'lib/calipso'));
var cacheKey = calipso.helpers.getCacheKey('block',block,true);

this.content[block] = this.content[block] || [];
this.content[block].push(content);

Expand Down
1 change: 1 addition & 0 deletions lib/Event.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var rootpath = process.cwd() + '/',
sys = require('sys'),
util = require('util'),
events = require('events'),
calipso = require(path.join(rootpath, 'lib/calipso')),
CalipsoHook = require(path.join(rootpath, 'lib/Hook')).CalipsoHook;


Expand Down
50 changes: 27 additions & 23 deletions lib/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ exports.CalipsoForm = new CalipsoForm();
*
* @param item : the json object representing the form
* @param values : The values to initialise the form with.
* @param next : Callback when done, pass markup as return val.
* @param next : Callback when done, pass markup as return val (TODO : deprecate this, then can use it in views)
*/
CalipsoForm.prototype.render = function(item, values, req, next) {

Expand All @@ -116,13 +116,17 @@ CalipsoForm.prototype.render = function(item, values, req, next) {
// Store local reference to the request for use during translation
t = req.t;

next(
this.start_form(item) +
var form = this.start_form(item) +
this.render_sections(item, values) +
this.render_fields(item.fields, values) +
this.render_buttons(item.buttons) +
this.end_form(item)
);
this.end_form(item);

if(typeof next === "function") {
next(form); // intention is to deprecate the asynch version
} else {
return form;
}

};

Expand All @@ -138,7 +142,7 @@ CalipsoForm.prototype.formTabs = function(sections) {
numSections = sections.length;
sections.forEach( function(section, index) {
var classes = 'form-tab';
if (index === 0) {
if (index === 0) {
classes += ' first';
}
if ((index + 1) === numSections) {
Expand Down Expand Up @@ -230,42 +234,42 @@ CalipsoForm.prototype.render_fields = function(fields, values) {
var objectName = field.name && field.name.split('[')[0] || '';

if(!field.noValue) { // We set this field to a value

if(field.name && field.name.split('[').length > 1) {

var fieldName = field.name.split('[')[1].replace(']', '');

// Check to see if it looks valid
if(!objectName || !fieldName) {
calipso.error('Field name incorrect: ' + field.name);
value = '';
} else {

if(values && values[objectName]) {
if(values[objectName][fieldName]) {
value = values[objectName][fieldName];
} else {
try {
// Use get to get dynamic schema elements (e.g. fields)
value = values[objectName].get(fieldName);
value = values[objectName].get(fieldName);
} catch(ex) {
// Do nothing, leave value blank
}
if(value === undefined)
if(value === undefined)
value = '';
}
}
}

} else {
if(values && values[objectName]) {
value = values[objectName];
}
}

} else {
// Do not set the value
}
// Do not set the value
}

fieldOutput += self.render_field(field, value);

Expand Down Expand Up @@ -724,7 +728,7 @@ CalipsoForm.prototype.process = function(req, next) {
next(req.formData);
return;
}

// Process form
if (req.form) {

Expand Down Expand Up @@ -846,15 +850,15 @@ function removeSection(form, sectionId) {
* Simple object mapper, used to copy over form values to schemas
*/
CalipsoForm.prototype.mapFields = function(fields,record) {

var props = Object.getOwnPropertyNames(fields);
props.forEach( function(name) {
props.forEach( function(name) {
// If not private (e.g. _id), then copy
if(!name.match(/^_.*/)) {
record.set(name, fields[name]);
}
});

}

/**
Expand All @@ -863,13 +867,13 @@ CalipsoForm.prototype.mapFields = function(fields,record) {
* @param to
*/
function copyProperties(from, to) {

if(typeof from === 'object') {

var props = Object.getOwnPropertyNames(from);
props.forEach( function(name) {
// Else just copy over the property as is

// Else just copy over the property as is
if (name in to) {
copyProperties(from[name], to[name]);
} else {
Expand Down
49 changes: 34 additions & 15 deletions lib/Helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* view engine (so for example 'request' is accessible).
*/

/**
/**
* removes any trailing query string or hash values
* @method stripUrlToConvert
* @param url {string} The url to convert
Expand All @@ -33,21 +33,21 @@ exports = module.exports = {
/**
* Request shortcut
*/
request: function(req,res){
request: function(req, res, calipso){
return req;
},

/**
* Translation shortcut
*/
t: function(req,res){
t: function(req, res, calipso){
return req.t;
},

/**
* User shortcut
*/
user: function(req,res){
user: function(req, res, calipso){
return req.session && req.session.user || { username: '', anonymous: true };
},

Expand All @@ -65,7 +65,7 @@ exports = module.exports = {
/**
* Hot date helper
*/
hotDate: function(req,res,calipso) {
hotDate: function(req,res, calipso) {

var hotFn = calipso.lib.prettyDate.hotDate;
return hotFn;
Expand All @@ -75,7 +75,7 @@ exports = module.exports = {
/**
* Get block data not included preloaded in the theme configuration (in blockData)
*/
getBlock: function(req, res){
getBlock: function(req, res, calipso){

return function(block, next) {

Expand All @@ -98,7 +98,7 @@ exports = module.exports = {
/**
* Generate a cache key for current url - should only be used in theme templates.
*/
getCacheKey: function(req,res) {
getCacheKey: function(req, res, calipso) {
return function(type,name,params) {
var cacheKey = type + '::' + name, paramCount = 0;
if(params) {
Expand All @@ -117,7 +117,7 @@ exports = module.exports = {
/**
* Get a menu html, synchronous
*/
getMenu: function(req, res){
getMenu: function(req, res, calipso){

return function(menu) {

Expand All @@ -135,7 +135,7 @@ exports = module.exports = {
/**
* Directly call an exposed module function (e.g. over ride routing rules and inject it anywhere)
*/
getModuleFn: function(req, res){
getModuleFn: function(req, res, calipso){

return function(req,moduleFunction,options,next) {

Expand Down Expand Up @@ -174,15 +174,15 @@ exports = module.exports = {
/**
* Constructs individual classes based on the url request
*/
getPageClasses: function(req, res) {
getPageClasses: function(req, res, calipso) {
var url = stripUrlToConvert(req.url);
return url.split('/').join(' ');
},

/**
* Constructs a single id based on the url request
*/
getPageId: function(req, res) {
getPageId: function(req, res, calipso) {
var url = stripUrlToConvert(req.url),
urlFrags = url.split('/');
for (var i = 0, len = urlFrags.length; i < len; i++) {
Expand All @@ -202,7 +202,7 @@ exports = module.exports = {
* that in the response (e.g. in the header of the template via another helper function).
* If nothing has changed it will just include the link to the previously generated file?
*/
includeScript: function(req, res){
includeScript: function(req, res, calipso){

return function(block) {

Expand All @@ -213,7 +213,7 @@ exports = module.exports = {
/**
* TODO Retrieve the concatenated / minified js scripts to add to template.
*/
getScripts: function(req, res){
getScripts: function(req, res, calipso){

return function() {

Expand All @@ -225,10 +225,29 @@ exports = module.exports = {
/**
* Flash message helpers
*/
flashMessages: function(req, res){
flashMessages: function(req, res, calipso){
return function() {
return req.flash();
};
}
},

/**
* HTML helpers - form, table, link (for now)
*/
form: function(req, res, calipso){
return function(form) {
return calipso.form.render(form);
};
},
table: function(req, res, calipso){
return function(table) {
return calipso.table.render(table);
};
},
link: function(req, res, calipso){
return function(link) {
return calipso.link.render(link);
};
},

};
Loading

0 comments on commit 2e4c9db

Please sign in to comment.