2016-06-23 11:40:35 +09:00
|
|
|
import Posts from "meteor/nova:posts";
|
2016-06-23 15:11:56 +09:00
|
|
|
import Categories from "./collection.js";
|
2016-12-12 11:34:28 +09:00
|
|
|
import { Utils } from 'meteor/nova:core';
|
2015-08-20 10:30:34 +09:00
|
|
|
|
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
* @summary Get all of a category's parents
|
2015-08-20 10:30:34 +09:00
|
|
|
* @param {Object} category
|
|
|
|
*/
|
2017-02-06 10:50:48 +09:00
|
|
|
Categories.getParents = function (category, store) {
|
2016-11-15 09:02:30 +01:00
|
|
|
const categoriesArray = [];
|
|
|
|
const getParents = function recurse (category) {
|
2017-02-06 10:50:48 +09:00
|
|
|
if (category && category.parentId) {
|
|
|
|
const parent = store ? Categories.findOneInStore(store, category.parentId) : Categories.findOne(category.parentId);
|
|
|
|
if (parent) {
|
|
|
|
categoriesArray.push(parent);
|
|
|
|
recurse(parent);
|
|
|
|
}
|
2015-08-20 10:30:34 +09:00
|
|
|
}
|
2016-11-26 02:46:55 +08:00
|
|
|
};
|
|
|
|
getParents(category);
|
2015-08-20 10:30:34 +09:00
|
|
|
|
|
|
|
return categoriesArray;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
* @summary Get all of a category's children
|
2015-08-20 10:30:34 +09:00
|
|
|
* @param {Object} category
|
|
|
|
*/
|
|
|
|
Categories.getChildren = function (category) {
|
|
|
|
var categoriesArray = [];
|
|
|
|
|
|
|
|
var getChildren = function recurse (categories) {
|
|
|
|
var children = Categories.find({parentId: {$in: _.pluck(categories, "_id")}}).fetch()
|
|
|
|
if (children.length > 0) {
|
|
|
|
categoriesArray = categoriesArray.concat(children);
|
|
|
|
recurse(children);
|
|
|
|
}
|
2016-11-26 02:46:55 +08:00
|
|
|
};
|
|
|
|
getChildren([category]);
|
2015-08-20 10:30:34 +09:00
|
|
|
|
|
|
|
return categoriesArray;
|
|
|
|
};
|
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
* @summary Get all of a post's categories
|
2015-08-20 10:30:34 +09:00
|
|
|
* @param {Object} post
|
|
|
|
*/
|
|
|
|
Posts.getCategories = function (post) {
|
|
|
|
return !!post.categories ? Categories.find({_id: {$in: post.categories}}).fetch() : [];
|
|
|
|
};
|
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
* @summary Get a category's URL
|
2015-08-20 10:30:34 +09:00
|
|
|
* @param {Object} category
|
|
|
|
*/
|
2015-09-08 09:49:30 +09:00
|
|
|
Categories.getUrl = function (category, isAbsolute) {
|
2016-11-15 09:02:30 +01:00
|
|
|
isAbsolute = typeof isAbsolute === "undefined" ? false : isAbsolute; // default to false
|
2016-12-12 11:34:28 +09:00
|
|
|
const prefix = isAbsolute ? Utils.getSiteUrl().slice(0,-1) : "";
|
2015-09-23 10:23:39 +09:00
|
|
|
// return prefix + FlowRouter.path("postsCategory", category);
|
2016-06-14 10:01:44 +09:00
|
|
|
return `${prefix}/?cat=${category.slug}`;
|
2015-08-20 10:30:34 +09:00
|
|
|
};
|
2015-09-08 09:49:30 +09:00
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
* @summary Get a category's counter name
|
2015-09-08 09:49:30 +09:00
|
|
|
* @param {Object} category
|
|
|
|
*/
|
2017-01-25 13:58:02 +09:00
|
|
|
Categories.getCounterName = function (category) {
|
2016-01-08 08:18:59 +07:00
|
|
|
return category._id + "-postsCount";
|
2017-01-25 13:58:02 +09:00
|
|
|
}
|
|
|
|
|