aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/collections/CollectionView.tsx
diff options
context:
space:
mode:
authorBob Zeleznik <zzzman@gmail.com>2019-03-21 22:22:25 -0400
committerBob Zeleznik <zzzman@gmail.com>2019-03-21 22:22:25 -0400
commitbc59ea805f32568f0835bd55d39575236c24a066 (patch)
treefecdc5314866c41908ca3a5fa4b56ee0d5b6981a /src/client/views/collections/CollectionView.tsx
parent1cf618563838f4ce7d8a98c8a0c8d94670bc4e18 (diff)
added very basic cycle detection when adding to collections
Diffstat (limited to 'src/client/views/collections/CollectionView.tsx')
-rw-r--r--src/client/views/collections/CollectionView.tsx22
1 files changed, 18 insertions, 4 deletions
diff --git a/src/client/views/collections/CollectionView.tsx b/src/client/views/collections/CollectionView.tsx
index 7e1d31018..2e93e6bb8 100644
--- a/src/client/views/collections/CollectionView.tsx
+++ b/src/client/views/collections/CollectionView.tsx
@@ -37,7 +37,7 @@ export class CollectionView extends React.Component<CollectionViewProps> {
@observable
public SelectedDocs: FieldId[] = [];
public active: () => boolean = () => CollectionView.Active(this);
- addDocument = (doc: Document, allowDuplicates: boolean): void => { CollectionView.AddDocument(this.props, doc, allowDuplicates); }
+ addDocument = (doc: Document, allowDuplicates: boolean): boolean => { return CollectionView.AddDocument(this.props, doc, allowDuplicates); }
removeDocument = (doc: Document): boolean => { return CollectionView.RemoveDocument(this.props, doc); }
get subView() { return CollectionView.SubView(this); }
@@ -48,17 +48,31 @@ export class CollectionView extends React.Component<CollectionViewProps> {
return isSelected || childSelected || topMost;
}
+ static createsCycle(doc: Document, props: CollectionViewProps): boolean {
+ let ldata = doc.GetList<Document>(KeyStore.Data, []);
+ for (let i = 0; i < ldata.length; i++) {
+ if (CollectionView.createsCycle(ldata[i], props))
+ return true;
+ }
+ return doc.Id == props.Document.Id;
+ }
+
@action
- public static AddDocument(props: CollectionViewProps, doc: Document, allowDuplicates: boolean) {
+ public static AddDocument(props: CollectionViewProps, doc: Document, allowDuplicates: boolean): boolean {
doc.SetNumber(KeyStore.Page, props.Document.GetNumber(KeyStore.CurPage, -1));
if (props.Document.Get(props.fieldKey) instanceof Field) {
//TODO This won't create the field if it doesn't already exist
const value = props.Document.GetData(props.fieldKey, ListField, new Array<Document>())
- if (!value.some(v => v.Id == doc.Id) || allowDuplicates)
- value.push(doc);
+ if (!CollectionView.createsCycle(doc, props)) {
+ if (!value.some(v => v.Id == doc.Id) || allowDuplicates)
+ value.push(doc);
+ }
+ else
+ return false;
} else {
props.Document.SetOnPrototype(props.fieldKey, new ListField([doc]));
}
+ return true;
}
@action