sheercms
Version:
Sheer Cliff CMS is a simple and powerful content management system (CMS) for Node JS.
191 lines (171 loc) • 7.01 kB
JavaScript
var logger = require('../logger');
var api = require('../api');
module.exports.menu = function(chunk, context, bodies, params) {
if (params.name) {
logger.debug("DUST Function: menu - params=" + params.name);
return chunk.map(function(chunk) {
api.menus.getMenu({name: params.name}, function(err, menu) {
if (err) {
chunk.end(err.message);
}
else if (menu) {
logger.debug("DUST Function: menu - found: " + menu.name + "; templateId: " + menu.templateId);
var model = {
menu: menu
};
var templateKey = params.template;
if (templateKey && templateKey.length > 0) {
api.templates.render(templateKey, model, function (err, result) {
if (err)
chunk.end(err.message);
else
chunk.end(result);
});
}
else if (menu.templateId) {
//using the template assigned to the menu
api.templates.render(menu.templateId, model, function(err, result) {
if (err)
chunk.end(err.message);
else
chunk.end(result);
});
}
else {
logger.warn("DUST Function: menu - Menu does not have a template: " + params.name);
chunk.end("");
}
}
else {
logger.warn("DUST Function: menu - Menu not found: " + params.name);
chunk.end("");
}
});
});
}
};
module.exports.scripts = function(chunk, context, bodies, params) {
logger.debug("DUST Function: scripts");
return chunk.map(function(chunk) {
//get the javascripts to include on the page
var cur = context.current();
if (cur.item && cur.info) {
//should we include jquery?
if (params.jquery === null || params.jquery === "true") {
chunk.write('<script src="/cms/static/js/jquery.min.js"></script>');
}
if (cur.info.mode === 'design' || cur.info.mode === 'preview') {
//include the designer script on the page
chunk.write('<script src="/cms/static/js/jquery.cookie.js"></script>');
chunk.write('<script src="/cms/plugins/ckeditor/ckeditor.js"></script>');
chunk.write('<script id="DesignerScript" src="/cms/static/js/designer.js" data-item="' + cur.item.contentId + '"></script>');
}
//TODO: include scripts from modules/plugins
chunk.end("");
}
else {
logger.error("DUST Function: scripts. Error: 'item' and 'info' not found on context.");
chunk.end("");
}
});
};
/*
Outputs the DUST context for debugging purposes
*/
module.exports.context = function(chunk, context, bodies, params) {
return chunk.map(function(chunk) {
try {
var cur = context.current();
chunk.end(JSON.stringify(cur));
}
catch(e) {
logger.error(e);
chunk.end("Error outputting DUST context: " + e.message);
}
});
};
/*
Outputs the stringified object
*/
module.exports.output = function(chunk, context, bodies, params) {
return chunk.map(function(chunk) {
try {
var cur = context.current();
var key = params.name;
var obj = (key && key.length > 0) ? cur[key] : null;
var s = obj ? JSON.stringify(obj) : "Output: " + key + " is null.";
chunk.end(s);
}
catch(e) {
logger.error(e);
chunk.end("Error outputting object string: " + e.message);
}
});
};
module.exports.field = function(chunk, context, bodies, params) {
return chunk.map(function(chunk) {
try {
var cur = context.current();
if (cur.item && cur.info) {
var name = params.name;
var rich = (params.rich === "true") ? "true" : "false";
var ph = params.placeholder || "";
var val = (cur.item && cur.item.fields && cur.item.fields[name]) ? cur.item.fields[name] : "";
var inline = (params.inline === "true") ? true : false;
if (name && name.length > 0) {
if (cur.info.mode === 'design' || cur.info.mode === 'preview') {
var cssClass = "design-field" + (inline ? " design-field-inline" : "");
chunk.write('<div class="' + cssClass + '" data-item="' + cur.item.contentId + '" data-field="' + name + '" data-rich="' + rich + '" data-placeholder="' + ph + '">');
chunk.write(val);
chunk.end('</div>');
}
else {
chunk.end(val);
}
}
else {
logger.warn("DUST Function: field. Warning: field not found on item: " + name);
chunk.end("");
}
}
else {
logger.error("DUST Function: field. Error: 'item' and 'info' not found on context.");
chunk.end("");
}
}
catch(e) {
logger.error(e);
chunk.end("");
}
});
};
module.exports.image = function(chunk, context, bodies, params) {
return chunk.map(function(chunk) {
try {
var cur = context.current();
if (cur.item && cur.info) {
var name = params.name;
var style = params.style;
var val = (cur.item && cur.item.fields && cur.item.fields[name]) ? cur.item.fields[name] : "";
if (name && name.length > 0) {
if (val && val.url && val.url.length > 0) {
style = style || "";
chunk.end('<img class="' + val.cssClass + '" src="' + val.url + '" title="' + val.tooltip + '" alt="' + val.alt + '" style="' + style + '">');
}
}
else {
logger.warn("DUST Function: field. Warning: field not found on item: " + name);
chunk.end("");
}
}
else {
logger.error("DUST Function: field. Error: 'item' and 'info' not found on context.");
chunk.end("");
}
}
catch(e) {
logger.error(e);
chunk.end("");
}
});
};