Vulcan/packages/nova-categories/lib/parameters.js

34 lines
1.1 KiB
JavaScript
Raw Normal View History

2016-06-23 15:11:56 +09:00
import Categories from "./collection.js";
2015-09-17 14:51:14 +09:00
// Category Parameter
// Add a "categories" property to terms which can be used to filter *all* existing Posts views.
function addCategoryParameter (parameters, terms) {
var cat = terms.cat || terms["cat[]"];
2015-09-17 14:51:14 +09:00
// filter by category if category slugs are provided
if (cat) {
2015-09-17 14:51:14 +09:00
var categoriesIds = [];
var selector = {};
if (typeof cat === "string") { // cat is a string
selector = {slug: cat};
} else if (Array.isArray(cat)) { // cat is an array
selector = {slug: {$in: cat}};
2015-09-17 14:51:14 +09:00
}
2015-09-17 14:51:14 +09:00
// get all categories passed in terms
var categories = Categories.find(selector).fetch();
2015-09-17 14:51:14 +09:00
// for each category, add its ID and the IDs of its children to categoriesId array
categories.forEach(function (category) {
categoriesIds.push(category._id);
categoriesIds = categoriesIds.concat(_.pluck(Categories.getChildren(category), "_id"));
2015-09-17 14:51:14 +09:00
});
parameters.selector.categories = {$in: categoriesIds};
}
return parameters;
}
Telescope.callbacks.add("postsParameters", addCategoryParameter);