2017-01-29 09:51:38 +09:00
|
|
|
import Categories from './collection.js';
|
2016-12-13 11:40:24 +09:00
|
|
|
import { addCallback } from 'meteor/nova:core';
|
2017-01-29 09:51:38 +09:00
|
|
|
import { getCategories } from './schema.js';
|
2016-06-23 15:11:56 +09:00
|
|
|
|
2017-01-05 15:53:41 +01:00
|
|
|
// Category Default Sorting by Ascending order (1, 2, 3..)
|
2017-01-29 09:51:38 +09:00
|
|
|
function CategoriesAscOrderSorting(parameters, terms) {
|
2017-01-05 15:53:41 +01:00
|
|
|
parameters.options.sort = {order: 1};
|
|
|
|
|
|
|
|
return parameters;
|
2017-01-29 09:51:38 +09:00
|
|
|
}
|
2017-01-05 15:53:41 +01:00
|
|
|
|
|
|
|
addCallback('categories.parameters', CategoriesAscOrderSorting);
|
|
|
|
|
2016-10-05 08:06:34 +02:00
|
|
|
// Category Posts Parameters
|
2015-09-17 14:51:14 +09:00
|
|
|
// Add a "categories" property to terms which can be used to filter *all* existing Posts views.
|
2017-01-29 09:51:38 +09:00
|
|
|
function PostsCategoryParameter(parameters, terms, apolloClient) {
|
2015-11-19 15:20:55 +09:00
|
|
|
|
2017-01-05 15:53:41 +01:00
|
|
|
const cat = terms.cat || terms["cat[]"];
|
2015-11-19 15:20:55 +09:00
|
|
|
|
2015-09-17 14:51:14 +09:00
|
|
|
// filter by category if category slugs are provided
|
2015-11-19 15:20:55 +09:00
|
|
|
if (cat) {
|
2015-09-11 10:51:21 +09:00
|
|
|
|
2017-01-05 15:53:41 +01:00
|
|
|
let categoriesIds = [];
|
|
|
|
let selector = {};
|
2017-01-29 09:51:38 +09:00
|
|
|
let slugs;
|
2015-09-11 10:51:21 +09:00
|
|
|
|
2015-11-19 15:20:55 +09:00
|
|
|
if (typeof cat === "string") { // cat is a string
|
2016-02-17 19:39:43 +09:00
|
|
|
selector = {slug: cat};
|
2017-01-29 09:51:38 +09:00
|
|
|
slugs = [cat];
|
2015-11-19 15:20:55 +09:00
|
|
|
} else if (Array.isArray(cat)) { // cat is an array
|
2016-02-17 19:39:43 +09:00
|
|
|
selector = {slug: {$in: cat}};
|
2017-01-29 09:51:38 +09:00
|
|
|
slugs = cat;
|
2015-09-17 14:51:14 +09:00
|
|
|
}
|
2015-09-11 10:51:21 +09:00
|
|
|
|
2015-09-17 14:51:14 +09:00
|
|
|
// get all categories passed in terms
|
2017-01-29 09:51:38 +09:00
|
|
|
const categories = Meteor.isClient ? _.filter(getCategories(apolloClient), category => _.contains(slugs, category.slug) ) : 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);
|
2016-05-03 12:44:50 +09:00
|
|
|
categoriesIds = categoriesIds.concat(_.pluck(Categories.getChildren(category), "_id"));
|
2015-09-17 14:51:14 +09:00
|
|
|
});
|
2015-09-03 14:22:51 +09:00
|
|
|
|
2017-01-31 11:01:28 +09:00
|
|
|
parameters.selector = Meteor.isClient ? {'categories._id': {$in: categoriesIds}} : {categories: {$in: categoriesIds}};
|
2015-09-03 14:22:51 +09:00
|
|
|
}
|
2017-01-05 15:53:41 +01:00
|
|
|
|
2015-09-03 14:22:51 +09:00
|
|
|
return parameters;
|
|
|
|
}
|
2017-01-05 15:53:41 +01:00
|
|
|
|
|
|
|
addCallback("posts.parameters", PostsCategoryParameter);
|