2014-10-10 18:54:07 +04:00
|
|
|
// # Foreach Helper
|
|
|
|
// Usage: `{{#foreach data}}{{/foreach}}`
|
|
|
|
//
|
|
|
|
// Block helper designed for looping through posts
|
2021-09-28 17:06:33 +03:00
|
|
|
const {checks} = require('../services/proxy');
|
|
|
|
const {hbs} = require('../services/rendering');
|
2021-09-26 23:01:13 +03:00
|
|
|
|
2019-04-22 18:54:42 +03:00
|
|
|
const _ = require('lodash');
|
2021-09-26 23:01:13 +03:00
|
|
|
const logging = require('@tryghost/logging');
|
|
|
|
const tpl = require('@tryghost/tpl');
|
|
|
|
|
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
|
|
|
|
2021-09-23 22:46:22 +03:00
|
|
|
const messages = {
|
|
|
|
iteratorNeeded: 'Need to pass an iterator to {{#foreach}}'
|
|
|
|
};
|
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
module.exports = function foreach(items, options) {
|
2015-06-27 18:40:37 +03:00
|
|
|
if (!options) {
|
2021-09-23 22:46:22 +03:00
|
|
|
logging.warn(tpl(messages.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);
|
|
|
|
}
|
2021-03-01 11:27:39 +03:00
|
|
|
let visibility = options.hash.visibility;
|
|
|
|
if (_.isArray(items) && items.length > 0 && checks.isPost(items[0])) {
|
|
|
|
visibility = visibility || 'all';
|
|
|
|
} else if (_.isObject(items) && _.isArray(Object.values(items))) {
|
|
|
|
if (Object.values(items).length > 0 && checks.isPost(Object.values(items)[0])) {
|
|
|
|
visibility = visibility || 'all';
|
|
|
|
}
|
|
|
|
}
|
2016-06-11 14:49:08 +03:00
|
|
|
// Exclude items which should not be visible in the theme
|
2021-03-01 11:27:39 +03:00
|
|
|
items = ghostHelperUtils.visibility.filter(items, 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
|
2020-10-20 02:02:56 +03:00
|
|
|
_.each(context, (value, 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
|
|
|
};
|