From d91e7eec9a62363b383b929166cdf600b124334c Mon Sep 17 00:00:00 2001
From: Fawn <fangrui_tong@brown.edu>
Date: Tue, 18 Jun 2019 15:09:21 -0400
Subject: links to nodes in different contexts render as a circle

---
 .../CollectionFreeFormLinksView.tsx                | 135 +++++++++++++++++----
 1 file changed, 109 insertions(+), 26 deletions(-)

(limited to 'src/client/views/collections/collectionFreeForm/CollectionFreeFormLinksView.tsx')

diff --git a/src/client/views/collections/collectionFreeForm/CollectionFreeFormLinksView.tsx b/src/client/views/collections/collectionFreeForm/CollectionFreeFormLinksView.tsx
index c4dd534ed..fc92c81d5 100644
--- a/src/client/views/collections/collectionFreeForm/CollectionFreeFormLinksView.tsx
+++ b/src/client/views/collections/collectionFreeForm/CollectionFreeFormLinksView.tsx
@@ -92,40 +92,123 @@ export class CollectionFreeFormLinksView extends React.Component<CollectionViewP
         return equalViews.filter(sv => sv.props.ContainingCollectionView && sv.props.ContainingCollectionView.props.Document === this.props.Document);
     }
 
-    @computed
-    get uniqueConnections() {
-        let connections = DocumentManager.Instance.LinkedDocumentViews.reduce((drawnPairs, connection) => {
-            let srcViews = this.documentAnchors(connection.a);
-            let targetViews = this.documentAnchors(connection.b);
-            let possiblePairs: { a: Doc, b: Doc, }[] = [];
-            srcViews.map(sv => targetViews.map(tv => possiblePairs.push({ a: sv.props.Document, b: tv.props.Document })));
-            possiblePairs.map(possiblePair => {
-                if (!drawnPairs.reduce((found, drawnPair) => {
-                    let match1 = (Doc.AreProtosEqual(possiblePair.a, drawnPair.a) && Doc.AreProtosEqual(possiblePair.b, drawnPair.b));
-                    let match2 = (Doc.AreProtosEqual(possiblePair.a, drawnPair.b) && Doc.AreProtosEqual(possiblePair.b, drawnPair.a));
-                    let match = match1 || match2;
-                    if (match && !drawnPair.l.reduce((found, link) => found || link[Id] === connection.l[Id], false)) {
-                        drawnPair.l.push(connection.l);
-                    }
-                    return match || found;
-                }, false)) {
-                    drawnPairs.push({ a: possiblePair.a, b: possiblePair.b, l: [connection.l] })
+    // @computed
+    // get uniqueConnections() {
+    //     // console.log("\n");
+    //     let connections = DocumentManager.Instance.LinkedDocumentViews.reduce((drawnPairs, connection) => {
+    //         // console.log("CONNECTION BETWEEN", StrCast(connection.anchor1View.props.Document.title), StrCast(connection.anchor2View.props.Document.title));
+    //         let srcViews = this.documentAnchors(connection.anchor1View);
+    //         // srcViews.forEach(sv => {
+    //         //     console.log("DOCANCHORS SRC", StrCast(connection.anchor1View.Document.title), StrCast(sv.Document.title));
+    //         // });
+
+    //         let targetViews = this.documentAnchors(connection.anchor2View);
+    //         // targetViews.forEach(sv => {
+    //         //     console.log("DOCANCHORS TARG", StrCast(connection.anchor2View.Document.title), StrCast(sv.Document.title));
+    //         // });
+
+    //         // console.log("lengths", srcViews.length, targetViews.length);
+
+    //         // srcViews.forEach(v => {
+    //         //     console.log("SOURCE VIEW", StrCast(v.props.Document.title));
+    //         // });
+    //         // targetViews.forEach(v => {
+    //         //     console.log("TARGET VIEW", StrCast(v.Document.title));
+    //         // });
+
+    //         let possiblePairs: { anchor1: Doc, anchor2: Doc }[] = [];
+    //         // srcViews.map(sv => {
+    //         //     console.log("SOURCE VIEW", StrCast(sv.props.Document.title));
+    //         //     targetViews.map(tv => {
+    //         //         console.log("TARGET VIEW", StrCast(tv.props.Document.title));
+    //         //         // console.log("PUSHING PAIR", StrCast(sv.props.Document.title), StrCast(tv.props.Document.title));
+    //         //         possiblePairs.push({ anchor1: sv.props.Document, anchor2: tv.props.Document });
+    //         //     });
+    //         //     console.log("END\n");
+    //         // });
+    //         srcViews.forEach(sv => {
+    //             // console.log("SOURCE VIEW", StrCast(sv.props.Document.title));
+    //             targetViews.forEach(tv => {
+    //                 // console.log("TARGET VIEW", StrCast(tv.props.Document.title));
+    //                 // console.log("PUSHING PAIR", StrCast(sv.props.Document.title), StrCast(tv.props.Document.title));
+    //                 possiblePairs.push({ anchor1: sv.props.Document, anchor2: tv.props.Document });
+    //             });
+    //             // console.log("END\n");
+    //         });
+    //         // console.log("POSSIBLE PAIRS LENGTH", possiblePairs.length);
+    //         possiblePairs.map(possiblePair => {
+    //             // console.log("POSSIBLEPAIR", StrCast(possiblePair.anchor1.title), StrCast(possiblePair.anchor2.title));
+    //             if (!drawnPairs.reduce((found, drawnPair) => {
+    //                 let match1 = (Doc.AreProtosEqual(possiblePair.anchor1, drawnPair.anchor1) && Doc.AreProtosEqual(possiblePair.anchor2, drawnPair.anchor2));
+    //                 let match2 = (Doc.AreProtosEqual(possiblePair.anchor1, drawnPair.anchor2) && Doc.AreProtosEqual(possiblePair.anchor2, drawnPair.anchor1));
+    //                 let match = match1 || match2;
+    //                 if (match && !drawnPair.linkDocs.reduce((found, link) => found || link[Id] === connection.linkDoc[Id], false)) {
+    //                     drawnPair.linkDocs.push(connection.linkDoc);
+    //                 }
+    //                 return match || found;
+    //             }, false)) {
+    //                 drawnPairs.push({ anchor1: possiblePair.anchor1, anchor2: possiblePair.anchor2, linkDocs: [connection.linkDoc] });
+    //             }
+    //         });
+    //         return drawnPairs;
+    //     }, [] as { anchor1: Doc, anchor2: Doc, linkDocs: Doc[] }[]);
+    //     return connections.map(c => {
+    //         let x = c.linkDocs.reduce((p, l) => p + l[Id], "");
+    //         return <CollectionFreeFormLinkView key={x} anchor1={c.anchor1} anchor2={c.anchor2} />;
+    //     });
+    // }
+
+    findUniquePairs = (): JSX.Element[] => {
+        // console.log("FIND UNIQUE PAIRS");
+        let connections = DocumentManager.Instance.LinkedDocumentViews;
+
+        let unique: Array<{ sourceView: DocumentView, targetView: DocumentView, sameContext: boolean }> = [];
+        connections.forEach(c => {
+            let match1Index = unique.findIndex(u => (c.anchor1View === u.sourceView) && (c.anchor2View === u.targetView));
+            let match2Index = unique.findIndex(u => (c.anchor1View === u.targetView) && (c.anchor2View === u.sourceView));
+            let sameContext = c.anchor1View.props.ContainingCollectionView === c.anchor2View.props.ContainingCollectionView;
+
+            if (!(match1Index > -1 || match2Index > -1)) {
+                // if docview pair does not already exist in unique, push
+                unique.push({ sourceView: c.anchor1View, targetView: c.anchor2View, sameContext: sameContext });
+            } else {
+                // if docview pair exists in unique, push if not in same context
+                if (!sameContext) {
+                    match1Index > -1 ? unique.push({ sourceView: c.anchor2View, targetView: c.anchor1View, sameContext: sameContext })
+                        : unique.push({ sourceView: c.anchor1View, targetView: c.anchor2View, sameContext: sameContext });
                 }
-            });
-            return drawnPairs;
-        }, [] as { a: Doc, b: Doc, l: Doc[] }[]);
-        return connections.map(c => {
-            let x = c.l.reduce((p, l) => p + l[Id], "");
-            return <CollectionFreeFormLinkView key={x} A={c.a} B={c.b} LinkDocs={c.l}
-                removeDocument={this.props.removeDocument} addDocument={this.props.addDocument} />;
+            }
+        });
+
+        console.log("\n UNIQUE");
+        unique.forEach(u => {
+            console.log(StrCast(u.sourceView.Document.title), StrCast(u.targetView.Document.title), u.sameContext);
+        });
+
+        // console.log("\n");
+
+        return unique.map(u => {
+            // TODO: make better key
+            let key = StrCast(u.sourceView.Document[Id]) + "-link-" + StrCast(u.targetView.Document[Id]) + "-" + Date.now() + Math.random();
+            let sourceIn = u.sourceView.props.ContainingCollectionView!.props.Document === this.props.Document;
+            let targetIn = u.targetView.props.ContainingCollectionView!.props.Document === this.props.Document;
+            let inContainer = u.sameContext ? sourceIn || targetIn : sourceIn;
+            if (inContainer) {
+                // console.log("key", key, StrCast(u.sourceView.Document.title), StrCast(u.targetView.Document.title));
+                return <CollectionFreeFormLinkView key={key} sourceView={u.sourceView} targetView={u.targetView} sameContext={u.sameContext} />;
+            } else {
+                return <div key={key}></div>;
+            }
         });
     }
 
     render() {
+        this.findUniquePairs();
         return (
             <div className="collectionfreeformlinksview-container">
                 <svg className="collectionfreeformlinksview-svgCanvas">
-                    {this.uniqueConnections}
+                    {/* {this.uniqueConnections} */}
+                    {this.findUniquePairs()}
                 </svg>
                 {this.props.children}
             </div>
-- 
cgit v1.2.3-70-g09d2

