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

58 lines
1.7 KiB
JavaScript
Raw Normal View History

import Telescope from 'meteor/nova:lib';
import PublicationUtils from 'meteor/utilities:smart-publications';
2016-06-23 11:40:35 +09:00
import Posts from "meteor/nova:posts";
2016-06-23 15:00:58 +09:00
import Users from 'meteor/nova:users';
2016-06-23 15:11:56 +09:00
import Categories from "./collection.js";
// import { client } from 'meteor/nova:base-apollo';
import gql from 'graphql-tag';
// check if user can create a new post
const canInsert = user => Users.canDo(user, "posts.new");
// check if user can edit a post
const canEdit = Users.canEdit;
const alwaysPublic = user => true;
2015-05-17 15:38:02 +09:00
Posts.addField(
{
fieldName: 'categories',
fieldSchema: {
type: [String],
2016-02-25 17:44:43 +09:00
control: "checkboxgroup",
optional: true,
insertableIf: canInsert,
editableIf: canEdit,
viewableIf: alwaysPublic,
form: {
noselect: true,
type: "bootstrap-category",
2015-10-20 14:34:24 +09:00
order: 50,
options: function () {
const apolloData = Telescope.graphQL.client.store.getState().apollo.data;
const categoryNames = apolloData.ROOT_QUERY.categories;
const categories = _.filter(Telescope.graphQL.client.store.getState().apollo.data, (object, key) => {return categoryNames.indexOf(key) !== -1});
const categoriesOptions = categories.map(function (category) {
return {
value: category._id,
label: category.name
};
});
return categoriesOptions;
}
},
// publish: true,
// join: {
// joinAs: "categoriesArray",
// collection: () => Categories
// },
resolveAs: 'categories: [Category]'
}
}
);
2016-02-17 11:28:00 +09:00
PublicationUtils.addToFields(Posts.publishedFields.list, ["categories"]);