2014-10-10 18:54:07 +04:00
|
|
|
// # Foreach Helper
|
|
|
|
// Usage: `{{#foreach data}}{{/foreach}}`
|
|
|
|
//
|
|
|
|
// Block helper designed for looping through posts
|
|
|
|
var hbs = require('express-hbs'),
|
2015-06-27 18:40:37 +03:00
|
|
|
errors = require('../errors'),
|
|
|
|
|
|
|
|
hbsUtils = hbs.handlebars.Utils,
|
2014-10-10 18:54:07 +04:00
|
|
|
foreach;
|
|
|
|
|
|
|
|
foreach = function (context, options) {
|
2015-06-27 18:40:37 +03:00
|
|
|
if (!options) {
|
|
|
|
errors.logWarn('Need to pass an iterator to #foreach');
|
|
|
|
}
|
|
|
|
|
2014-10-10 18:54:07 +04:00
|
|
|
var fn = options.fn,
|
|
|
|
inverse = options.inverse,
|
|
|
|
i = 0,
|
|
|
|
columns = options.hash.columns,
|
|
|
|
ret = '',
|
2015-06-27 18:40:37 +03:00
|
|
|
data,
|
|
|
|
contextPath;
|
|
|
|
|
|
|
|
if (options.data && options.ids) {
|
|
|
|
contextPath = hbsUtils.appendContextPath(options.data.contextPath, options.ids[0]) + '.';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hbsUtils.isFunction(context)) {
|
|
|
|
context = context.call(this);
|
|
|
|
}
|
2014-10-10 18:54:07 +04:00
|
|
|
|
|
|
|
if (options.data) {
|
|
|
|
data = hbs.handlebars.createFrame(options.data);
|
|
|
|
}
|
|
|
|
|
2015-06-27 18:40:37 +03:00
|
|
|
function execIteration(field, index, last) {
|
|
|
|
if (data) {
|
|
|
|
data.key = field;
|
|
|
|
data.index = index;
|
|
|
|
data.number = index + 1;
|
|
|
|
data.first = index === 0;
|
|
|
|
data.last = !!last;
|
|
|
|
data.even = index % 2 === 1;
|
|
|
|
data.odd = !data.even;
|
|
|
|
data.rowStart = index % columns === 0;
|
|
|
|
data.rowEnd = index % columns === (columns - 1);
|
|
|
|
|
|
|
|
if (contextPath) {
|
|
|
|
data.contextPath = contextPath + field;
|
|
|
|
}
|
2014-10-10 18:54:07 +04:00
|
|
|
}
|
2015-06-27 18:40:37 +03:00
|
|
|
|
|
|
|
ret = ret + fn(context[field], {
|
|
|
|
data: data,
|
|
|
|
blockParams: hbsUtils.blockParams([context[field], field], [contextPath + field, null])
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function iterateArray(context) {
|
|
|
|
var j;
|
|
|
|
for (j = context.length; i < j; i += 1) {
|
|
|
|
execIteration(i, i, i === context.length - 1);
|
2014-10-10 18:54:07 +04:00
|
|
|
}
|
2015-06-27 18:40:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function iterateObject(context) {
|
|
|
|
var priorKey,
|
|
|
|
key;
|
|
|
|
|
|
|
|
for (key in context) {
|
|
|
|
if (context.hasOwnProperty(key)) {
|
|
|
|
// We're running the iterations one step out of sync so we can detect
|
|
|
|
// the last iteration without have to scan the object twice and create
|
|
|
|
// an itermediate keys array.
|
|
|
|
if (priorKey) {
|
|
|
|
execIteration(priorKey, i - 1);
|
|
|
|
}
|
|
|
|
priorKey = key;
|
|
|
|
i += 1;
|
|
|
|
}
|
2014-10-10 18:54:07 +04:00
|
|
|
}
|
2015-06-27 18:40:37 +03:00
|
|
|
if (priorKey) {
|
|
|
|
execIteration(priorKey, i - 1, true);
|
2014-10-10 18:54:07 +04:00
|
|
|
}
|
|
|
|
}
|
2015-06-27 18:40:37 +03:00
|
|
|
|
2014-10-10 18:54:07 +04:00
|
|
|
if (context && typeof context === 'object') {
|
2015-06-27 18:40:37 +03:00
|
|
|
if (hbsUtils.isArray(context)) {
|
|
|
|
iterateArray(context);
|
2014-10-10 18:54:07 +04:00
|
|
|
} else {
|
2015-06-27 18:40:37 +03:00
|
|
|
iterateObject(context);
|
2014-10-10 18:54:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i === 0) {
|
|
|
|
ret = inverse(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = foreach;
|