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-12-11 00:54:27 +03:00
|
|
|
_ = require('lodash'),
|
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,
|
|
|
|
columns = options.hash.columns,
|
2015-12-11 00:54:27 +03:00
|
|
|
length = _.size(context),
|
2015-12-18 17:06:37 +03:00
|
|
|
limit = parseInt(options.hash.limit, 10) || length,
|
2015-12-29 20:52:13 +03:00
|
|
|
from = parseInt(options.hash.from, 10) || 1,
|
|
|
|
to = parseInt(options.hash.to, 10) || (from - 1) + limit,
|
2015-12-11 00:54:27 +03:00
|
|
|
output = '',
|
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;
|
2015-12-29 20:52:13 +03:00
|
|
|
data.first = index === from - 1; // From uses 1-indexed, but array uses 0-indexed.
|
2015-06-27 18:40:37 +03:00
|
|
|
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
|
|
|
|
2015-12-11 00:54:27 +03:00
|
|
|
output = output + fn(context[field], {
|
2015-06-27 18:40:37 +03:00
|
|
|
data: data,
|
|
|
|
blockParams: hbsUtils.blockParams([context[field], field], [contextPath + field, null])
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-12-11 00:54:27 +03:00
|
|
|
function iterateCollection(context) {
|
2015-12-29 20:52:13 +03:00
|
|
|
var count = 1,
|
|
|
|
current = 1;
|
2015-06-27 18:40:37 +03:00
|
|
|
|
2015-12-11 00:54:27 +03:00
|
|
|
_.each(context, function (item, key) {
|
2015-12-29 20:52:13 +03:00
|
|
|
if (current < from) {
|
|
|
|
current += 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current <= to) {
|
|
|
|
execIteration(key, current - 1, current === to);
|
2015-06-27 18:40:37 +03:00
|
|
|
}
|
2015-12-11 00:54:27 +03:00
|
|
|
count += 1;
|
2015-12-29 20:52:13 +03:00
|
|
|
current += 1;
|
2015-12-11 00:54:27 +03:00
|
|
|
});
|
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-12-11 00:54:27 +03:00
|
|
|
iterateCollection(context);
|
2014-10-10 18:54:07 +04:00
|
|
|
}
|
|
|
|
|
2015-12-11 00:54:27 +03:00
|
|
|
if (length === 0) {
|
|
|
|
output = inverse(this);
|
2014-10-10 18:54:07 +04:00
|
|
|
}
|
|
|
|
|
2015-12-11 00:54:27 +03:00
|
|
|
return output;
|
2014-10-10 18:54:07 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = foreach;
|