2013-06-25 15:43:15 +04:00
|
|
|
var _ = require('underscore'),
|
|
|
|
moment = require('moment'),
|
2013-08-07 13:45:37 +04:00
|
|
|
downsize = require('downsize'),
|
2013-06-25 15:43:15 +04:00
|
|
|
when = require('when'),
|
|
|
|
hbs = require('express-hbs'),
|
2013-07-11 23:02:18 +04:00
|
|
|
errors = require('../errorHandling'),
|
2013-06-25 15:43:15 +04:00
|
|
|
coreHelpers;
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
coreHelpers = function (ghost) {
|
2013-07-11 02:45:13 +04:00
|
|
|
var navHelper,
|
|
|
|
paginationHelper;
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
/**
|
|
|
|
* [ description]
|
|
|
|
* @todo ghost core helpers + a way for themes to register them
|
|
|
|
* @param {Object} context date object
|
2013-07-11 20:21:42 +04:00
|
|
|
* @param {*} options
|
2013-06-25 15:43:15 +04:00
|
|
|
* @return {Object} A Moment time / date object
|
|
|
|
*/
|
2013-07-11 20:21:42 +04:00
|
|
|
ghost.registerThemeHelper('dateFormat', function (context, options) {
|
|
|
|
var f = options.hash.format || "MMM Do, YYYY",
|
|
|
|
timeago = options.hash.timeago,
|
2013-07-04 22:42:49 +04:00
|
|
|
date;
|
|
|
|
if (timeago) {
|
|
|
|
date = moment(context).fromNow();
|
|
|
|
} else {
|
|
|
|
date = moment(context).format(f);
|
|
|
|
}
|
|
|
|
return date;
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2013-07-11 20:21:42 +04:00
|
|
|
// ### Content Helper
|
2013-08-07 13:45:37 +04:00
|
|
|
//
|
|
|
|
// *Usage example:*
|
|
|
|
// `{{content}}`
|
|
|
|
// `{{content words=20}}`
|
|
|
|
// `{{content characters=256}}`
|
|
|
|
//
|
|
|
|
// Turns content html into a safestring so that the user doesn't have to
|
|
|
|
// escape it or tell handlebars to leave it alone with a triple-brace.
|
|
|
|
//
|
|
|
|
// Enables tag-safe truncation of content by characters or words.
|
|
|
|
//
|
|
|
|
// **returns** SafeString content html, complete or truncated.
|
|
|
|
//
|
2013-07-11 20:21:42 +04:00
|
|
|
ghost.registerThemeHelper('content', function (options) {
|
2013-08-07 13:45:37 +04:00
|
|
|
var truncateOptions = (options || {}).hash || {};
|
|
|
|
truncateOptions = _.pick(truncateOptions, ["words", "characters"]);
|
|
|
|
|
|
|
|
if (truncateOptions.words || truncateOptions.characters) {
|
|
|
|
return new hbs.handlebars.SafeString(
|
|
|
|
downsize(this.content, truncateOptions)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-07-11 20:21:42 +04:00
|
|
|
return new hbs.handlebars.SafeString(this.content);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
/**
|
|
|
|
* [ description]
|
|
|
|
*
|
|
|
|
* @param String key
|
|
|
|
* @param String default translation
|
|
|
|
* @param {Object} options
|
|
|
|
* @return String A correctly internationalised string
|
|
|
|
*/
|
|
|
|
ghost.registerThemeHelper('e', function (key, defaultString, options) {
|
|
|
|
var output;
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
if (ghost.config().defaultLang === 'en' && _.isEmpty(options.hash) && !ghost.config().forceI18n) {
|
|
|
|
output = defaultString;
|
|
|
|
} else {
|
|
|
|
output = ghost.polyglot().t(key, options.hash);
|
|
|
|
}
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
return output;
|
|
|
|
});
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
ghost.registerThemeHelper('json', function (object, options) {
|
|
|
|
return JSON.stringify(object);
|
|
|
|
});
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
ghost.registerThemeHelper('foreach', function (context, options) {
|
|
|
|
var fn = options.fn,
|
|
|
|
inverse = options.inverse,
|
|
|
|
i = 0,
|
|
|
|
j = 0,
|
|
|
|
columns = options.hash.columns,
|
|
|
|
key,
|
|
|
|
ret = "",
|
|
|
|
data;
|
2013-06-09 20:45:17 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
if (options.data) {
|
|
|
|
data = hbs.handlebars.createFrame(options.data);
|
|
|
|
}
|
2013-06-16 17:12:28 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
function setKeys(_data, _i, _j, _columns) {
|
|
|
|
if (_i === 0) {
|
|
|
|
_data.first = true;
|
2013-06-16 17:12:28 +04:00
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
if (_i === _j - 1) {
|
|
|
|
_data.last = true;
|
|
|
|
}
|
|
|
|
// first post is index zero but still needs to be odd
|
|
|
|
if (_i % 2 === 1) {
|
|
|
|
_data.even = true;
|
|
|
|
} else {
|
|
|
|
_data.odd = true;
|
|
|
|
}
|
|
|
|
if (_i % _columns === 0) {
|
|
|
|
_data.rowStart = true;
|
|
|
|
} else if (_i % _columns === (_columns - 1)) {
|
|
|
|
_data.rowEnd = true;
|
|
|
|
}
|
|
|
|
return _data;
|
|
|
|
}
|
|
|
|
if (context && typeof context === 'object') {
|
|
|
|
if (context instanceof Array) {
|
|
|
|
for (j = context.length; i < j; i += 1) {
|
|
|
|
if (data) {
|
|
|
|
data.index = i;
|
|
|
|
data.first = data.rowEnd = data.rowStart = data.last = data.even = data.odd = false;
|
|
|
|
data = setKeys(data, i, j, columns);
|
|
|
|
}
|
|
|
|
ret = ret + fn(context[i], { data: data });
|
2013-06-23 19:46:02 +04:00
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
} else {
|
|
|
|
for (key in context) {
|
|
|
|
if (context.hasOwnProperty(key)) {
|
|
|
|
j += 1;
|
|
|
|
}
|
2013-06-23 19:46:02 +04:00
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
for (key in context) {
|
|
|
|
if (context.hasOwnProperty(key)) {
|
2013-06-16 17:12:28 +04:00
|
|
|
if (data) {
|
2013-06-25 15:43:15 +04:00
|
|
|
data.key = key;
|
2013-06-23 19:46:02 +04:00
|
|
|
data.first = data.rowEnd = data.rowStart = data.last = data.even = data.odd = false;
|
|
|
|
data = setKeys(data, i, j, columns);
|
2013-06-16 17:12:28 +04:00
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
ret = ret + fn(context[key], {data: data});
|
|
|
|
i += 1;
|
2013-06-16 17:12:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
2013-06-16 17:12:28 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
if (i === 0) {
|
|
|
|
ret = inverse(this);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
});
|
2013-07-11 02:45:13 +04:00
|
|
|
|
|
|
|
// ## Template driven helpers
|
|
|
|
// Template driven helpers require that their template is loaded before they can be registered.
|
|
|
|
|
|
|
|
// ###Nav Helper
|
|
|
|
// `{{nav}}`
|
|
|
|
// Outputs a navigation menu built from items in config.js
|
|
|
|
navHelper = ghost.loadTemplate('nav').then(function (templateFn) {
|
|
|
|
ghost.registerThemeHelper('nav', function (options) {
|
|
|
|
if (!_.isObject(this.navItems) || _.isFunction(this.navItems)) {
|
|
|
|
errors.logAndThrowError('navItems data is not an object or is a function');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return new hbs.handlebars.SafeString(templateFn({links: this.navItems}));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// ### Pagination Helper
|
|
|
|
// `{{paginate}}`
|
|
|
|
// Outputs previous and next buttons, along with info about the current page
|
|
|
|
paginationHelper = ghost.loadTemplate('pagination').then(function (templateFn) {
|
|
|
|
ghost.registerThemeHelper('paginate', function (options) {
|
|
|
|
if (!_.isObject(this.pagination) || _.isFunction(this.pagination)) {
|
|
|
|
errors.logAndThrowError('pagination data is not an object or is a function');
|
|
|
|
return;
|
|
|
|
}
|
2013-07-16 22:57:19 +04:00
|
|
|
if (_.isUndefined(this.pagination.page) || _.isUndefined(this.pagination.pages)
|
2013-07-17 17:37:52 +04:00
|
|
|
|| _.isUndefined(this.pagination.total) || _.isUndefined(this.pagination.limit)) {
|
|
|
|
errors.logAndThrowError('All values must be defined for page, pages, limit and total');
|
2013-07-16 22:57:19 +04:00
|
|
|
return;
|
|
|
|
}
|
2013-07-17 17:37:52 +04:00
|
|
|
if ((!_.isUndefined(this.pagination.next) && !_.isNumber(this.pagination.next))
|
|
|
|
|| (!_.isUndefined(this.pagination.prev) && !_.isNumber(this.pagination.prev))) {
|
|
|
|
errors.logAndThrowError('Invalid value, Next/Prev must be a number');
|
2013-07-16 22:57:19 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!_.isNumber(this.pagination.page) || !_.isNumber(this.pagination.pages)
|
|
|
|
|| !_.isNumber(this.pagination.total) || !_.isNumber(this.pagination.limit)) {
|
|
|
|
errors.logAndThrowError('Invalid value, check page, pages, limit and total are numbers');
|
|
|
|
return;
|
|
|
|
}
|
2013-07-11 02:45:13 +04:00
|
|
|
return new hbs.handlebars.SafeString(templateFn(this.pagination));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Return once the template-driven helpers have loaded
|
2013-06-25 19:13:19 +04:00
|
|
|
return when.join(
|
2013-07-11 02:45:13 +04:00
|
|
|
navHelper,
|
|
|
|
paginationHelper
|
2013-06-25 19:13:19 +04:00
|
|
|
);
|
2013-06-25 15:43:15 +04:00
|
|
|
};
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
module.exports.loadCoreHelpers = coreHelpers;
|