2014-10-10 18:54:07 +04:00
|
|
|
// # Foreach Helper
|
|
|
|
// Usage: `{{#foreach data}}{{/foreach}}`
|
|
|
|
//
|
|
|
|
// Block helper designed for looping through posts
|
2019-04-22 18:54:42 +03:00
|
|
|
const _ = require('lodash');
|
2019-06-07 09:45:26 +03:00
|
|
|
const {logging, i18n, hbs} = require('./proxy');
|
2019-04-22 18:54:42 +03:00
|
|
|
const {Utils: hbsUtils, handlebars: {createFrame}} = hbs;
|
2019-06-07 09:45:26 +03:00
|
|
|
const ghostHelperUtils = require('@tryghost/helpers').utils;
|
2016-06-11 14:49:08 +03:00
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
module.exports = function foreach(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
|
2019-06-07 09:45:26 +03:00
|
|
|
items = ghostHelperUtils.visibility.filter(items, options.hash.visibility);
|
2016-06-11 14:49:08 +03:00
|
|
|
|
2016-03-18 00:37:18 +03:00
|
|
|
// Initial values set based on parameters sent through. If nothing sent, set to defaults
|
2019-04-22 18:54:42 +03:00
|
|
|
const {fn, inverse, hash, data, ids} = options;
|
|
|
|
let {columns, limit, from, to} = hash;
|
|
|
|
let length = _.size(items);
|
|
|
|
let output = '';
|
|
|
|
let frame;
|
|
|
|
let contextPath;
|
|
|
|
|
|
|
|
limit = parseInt(limit, 10) || length;
|
|
|
|
from = parseInt(from, 10) || 1;
|
|
|
|
to = parseInt(to, 10) || length;
|
2015-06-27 18:40:37 +03:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-04-22 18:54:42 +03:00
|
|
|
if (data && ids) {
|
|
|
|
contextPath = hbsUtils.appendContextPath(data.contextPath, ids[0]) + '.';
|
2015-06-27 18:40:37 +03:00
|
|
|
}
|
|
|
|
|
2019-04-22 18:54:42 +03:00
|
|
|
if (data) {
|
|
|
|
frame = createFrame(data);
|
2014-10-10 18:54:07 +04:00
|
|
|
}
|
|
|
|
|
2015-06-27 18:40:37 +03:00
|
|
|
function execIteration(field, index, last) {
|
2019-04-22 18:54:42 +03:00
|
|
|
if (frame) {
|
|
|
|
frame.key = field;
|
|
|
|
frame.index = index;
|
|
|
|
frame.number = index + 1;
|
|
|
|
frame.first = index === from - 1; // From uses 1-indexed, but array uses 0-indexed
|
|
|
|
frame.last = !!last;
|
|
|
|
frame.even = index % 2 === 1;
|
|
|
|
frame.odd = !frame.even;
|
|
|
|
frame.rowStart = index % columns === 0;
|
|
|
|
frame.rowEnd = index % columns === (columns - 1);
|
2015-06-27 18:40:37 +03:00
|
|
|
if (contextPath) {
|
2019-04-22 18:54:42 +03:00
|
|
|
frame.contextPath = contextPath + field;
|
2015-06-27 18:40:37 +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
|
|
|
output = output + fn(items[field], {
|
2019-04-22 18:54:42 +03:00
|
|
|
data: frame,
|
2017-11-01 16:44:54 +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
|
2019-04-22 18:54:42 +03:00
|
|
|
let 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
|
2019-04-22 18:54:42 +03:00
|
|
|
_.each(context, (_, 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
|
|
|
}
|
2019-04-22 18:54:42 +03:00
|
|
|
|
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
|
|
|
};
|