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'),
|
2016-10-04 18:33:43 +03:00
|
|
|
logging = require('../logging'),
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n = require('../i18n'),
|
2016-10-10 11:51:03 +03:00
|
|
|
visibilityFilter = require('../utils/visibility-filter'),
|
2016-06-11 14:49:08 +03:00
|
|
|
utils = require('./utils'),
|
2015-06-27 18:40:37 +03:00
|
|
|
|
|
|
|
hbsUtils = hbs.handlebars.Utils,
|
2014-10-10 18:54:07 +04:00
|
|
|
foreach;
|
|
|
|
|
2016-06-11 14:49:08 +03:00
|
|
|
function filterItemsByVisibility(items, options) {
|
|
|
|
var visibility = utils.parseVisibility(options);
|
|
|
|
|
2016-10-10 11:51:03 +03:00
|
|
|
return visibilityFilter(items, visibility, !!options.hash.visibility);
|
2016-06-11 14:49:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach = function (items, options) {
|
2015-06-27 18:40:37 +03:00
|
|
|
if (!options) {
|
2016-10-04 18:33:43 +03:00
|
|
|
logging.warn(i18n.t('warnings.helpers.foreach.iteratorNeeded'));
|
2015-06-27 18:40:37 +03:00
|
|
|
}
|
|
|
|
|
2016-06-11 14:49:08 +03:00
|
|
|
if (hbsUtils.isFunction(items)) {
|
|
|
|
items = items.call(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exclude items which should not be visible in the theme
|
|
|
|
items = filterItemsByVisibility(items, options);
|
|
|
|
|
2016-03-18 00:37:18 +03:00
|
|
|
// Initial values set based on parameters sent through. If nothing sent, set to defaults
|
2014-10-10 18:54:07 +04:00
|
|
|
var fn = options.fn,
|
|
|
|
inverse = options.inverse,
|
|
|
|
columns = options.hash.columns,
|
2016-06-11 14:49:08 +03:00
|
|
|
length = _.size(items),
|
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,
|
2016-03-18 00:37:18 +03:00
|
|
|
to = parseInt(options.hash.to, 10) || length,
|
2015-12-11 00:54:27 +03:00
|
|
|
output = '',
|
2015-06-27 18:40:37 +03:00
|
|
|
data,
|
|
|
|
contextPath;
|
|
|
|
|
2016-03-18 00:37:18 +03:00
|
|
|
// If a limit option was sent through (aka not equal to default (length))
|
|
|
|
// and from plus limit is less than the length, set to to the from + limit
|
|
|
|
if ((limit < length) && ((from + limit) <= length)) {
|
|
|
|
to = (from - 1) + limit;
|
|
|
|
}
|
|
|
|
|
2015-06-27 18:40:37 +03:00
|
|
|
if (options.data && options.ids) {
|
|
|
|
contextPath = hbsUtils.appendContextPath(options.data.contextPath, options.ids[0]) + '.';
|
|
|
|
}
|
|
|
|
|
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;
|
2016-03-18 00:37:18 +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
|
|
|
|
2016-06-11 14:49:08 +03:00
|
|
|
output = output + fn(items[field], {
|
2015-06-27 18:40:37 +03:00
|
|
|
data: data,
|
2016-06-11 14:49:08 +03:00
|
|
|
blockParams: hbsUtils.blockParams([items[field], field], [contextPath + field, null])
|
2015-06-27 18:40:37 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-12-11 00:54:27 +03:00
|
|
|
function iterateCollection(context) {
|
2016-03-18 00:37:18 +03:00
|
|
|
// Context is all posts on the blog
|
2015-12-29 20:52:13 +03:00
|
|
|
var count = 1,
|
|
|
|
current = 1;
|
2015-06-27 18:40:37 +03:00
|
|
|
|
2016-03-18 00:37:18 +03:00
|
|
|
// For each post, if it is a post number that fits within the from and to
|
|
|
|
// send the key to execIteration to set to be added to the page
|
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
|
|
|
|
2016-06-11 14:49:08 +03:00
|
|
|
if (items && typeof items === 'object') {
|
|
|
|
iterateCollection(items);
|
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;
|