2016-09-14 16:04:08 +03:00
|
|
|
import FieldNode from './fieldNode.js';
|
|
|
|
|
|
|
|
export default class CollectionNode {
|
|
|
|
constructor(collection, body, linkName) {
|
2016-09-28 18:30:12 +03:00
|
|
|
this.body = body;
|
2016-09-14 16:04:08 +03:00
|
|
|
this.linkName = linkName;
|
2016-09-22 12:01:47 +03:00
|
|
|
this.collection = collection;
|
2016-09-28 18:30:12 +03:00
|
|
|
|
|
|
|
this.nodes = [];
|
2016-09-14 16:04:08 +03:00
|
|
|
this.props = {};
|
|
|
|
this.parent = null;
|
2016-09-21 18:33:50 +03:00
|
|
|
this.linker = null;
|
2016-09-14 16:04:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get collectionNodes() {
|
|
|
|
return _.filter(this.nodes, n => n instanceof CollectionNode)
|
|
|
|
}
|
|
|
|
|
|
|
|
get fieldNodes() {
|
|
|
|
return _.filter(this.nodes, n => n instanceof FieldNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param node
|
2016-09-21 18:33:50 +03:00
|
|
|
* @param linker
|
2016-09-14 16:04:08 +03:00
|
|
|
*/
|
2016-09-21 18:33:50 +03:00
|
|
|
add(node, linker) {
|
2016-09-14 16:04:08 +03:00
|
|
|
node.parent = this;
|
2016-09-21 18:33:50 +03:00
|
|
|
node.linker = linker;
|
|
|
|
|
2016-09-14 16:04:08 +03:00
|
|
|
this.nodes.push(node);
|
|
|
|
}
|
|
|
|
|
2016-09-28 18:30:12 +03:00
|
|
|
/**
|
|
|
|
* @param _node
|
|
|
|
*/
|
|
|
|
remove(_node) {
|
|
|
|
this.nodes = _.filter(this.nodes, node => _node !== node);
|
|
|
|
}
|
|
|
|
|
2016-09-14 16:04:08 +03:00
|
|
|
/**
|
2016-09-16 19:22:12 +03:00
|
|
|
* @param filters
|
2016-09-14 16:04:08 +03:00
|
|
|
* @param options
|
|
|
|
*/
|
2016-09-16 19:22:12 +03:00
|
|
|
applyFields(filters, options) {
|
|
|
|
let hasAddedAnyField = false;
|
2016-09-14 16:04:08 +03:00
|
|
|
|
2016-09-16 19:22:12 +03:00
|
|
|
_.each(this.fieldNodes, n => {
|
|
|
|
hasAddedAnyField = true;
|
|
|
|
n.applyFields(options.fields)
|
|
|
|
});
|
|
|
|
|
|
|
|
// it will only get here if it has collectionNodes children
|
|
|
|
_.each(this.collectionNodes, (collectionNode) => {
|
|
|
|
let linker = collectionNode.linker;
|
2016-09-20 11:44:47 +03:00
|
|
|
|
2016-09-21 18:33:50 +03:00
|
|
|
if (linker && !linker.isVirtual()) {
|
2016-09-20 11:44:47 +03:00
|
|
|
options.fields[linker.linkStorageField] = 1;
|
2016-09-21 18:33:50 +03:00
|
|
|
hasAddedAnyField = true;
|
2016-09-20 11:44:47 +03:00
|
|
|
}
|
2016-09-16 19:22:12 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// if he selected filters, we should automatically add those fields
|
|
|
|
_.each(filters, (value, field) => {
|
|
|
|
hasAddedAnyField = true;
|
|
|
|
options.fields[field] = 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!hasAddedAnyField) {
|
|
|
|
options.fields = {_id: 1};
|
|
|
|
}
|
2016-09-15 10:37:10 +03:00
|
|
|
}
|
2016-09-22 17:06:05 +03:00
|
|
|
|
|
|
|
parentHasMyLinkStorageFieldSpecified() {
|
|
|
|
const storageField = this.linker.linkStorageField;
|
|
|
|
|
|
|
|
if (this.parent) {
|
|
|
|
return !!_.find(this.parent.fieldNodes, fieldNode => {
|
|
|
|
return fieldNode.name == storageField
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-28 18:30:12 +03:00
|
|
|
|
|
|
|
getName() {
|
|
|
|
return this.linkName
|
|
|
|
? this.linkName
|
|
|
|
: (this.collection ? this.collection._name : 'N/A');
|
|
|
|
}
|
2016-09-14 16:04:08 +03:00
|
|
|
}
|