aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/collections/collectionFreeForm/CollectionFreeFormLinksView.tsx
blob: 49ca024a2bfa3a00d2f31f0d3b5e8b252899c92c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { computed } from "mobx";
import { observer } from "mobx-react";
import { Doc } from "../../../../new_fields/Doc";
import { Id } from "../../../../new_fields/FieldSymbols";
import { DocumentManager } from "../../../util/DocumentManager";
import { DocumentView } from "../../nodes/DocumentView";
import "./CollectionFreeFormLinksView.scss";
import { CollectionFreeFormLinkView } from "./CollectionFreeFormLinkView";
import React = require("react");
import { Utils, emptyFunction } from "../../../../Utils";
import { SelectionManager } from "../../../util/SelectionManager";
import { DocumentType } from "../../../documents/DocumentTypes";

@observer
export class CollectionFreeFormLinksView extends React.Component {
    @computed
    get uniqueConnections() {
        const connections = DocumentManager.Instance.LinkedDocumentViews.reduce((drawnPairs, connection) => {
            if (!drawnPairs.reduce((found, drawnPair) => {
                const match1 = (connection.a === drawnPair.a && connection.b === drawnPair.b);
                const match2 = (connection.a === drawnPair.b && connection.b === drawnPair.a);
                const 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: connection.a, b: connection.b, l: [connection.l] });
            }
            return drawnPairs;
        }, [] as { a: DocumentView, b: DocumentView, l: Doc[] }[]);
        return connections.filter(c =>
            c.a.props.layoutKey && c.b.props.layoutKey && c.a.props.Document.type === DocumentType.LINK &&
            c.a.props.bringToFront !== emptyFunction && c.b.props.bringToFront !== emptyFunction // this prevents links to be drawn to anchors in CollectionTree views -- this is a hack that should be fixed
        ).map(c => <CollectionFreeFormLinkView key={Utils.GenerateGuid()} A={c.a} B={c.b} LinkDocs={c.l} />);
    }

    render() {
        return <div className="collectionfreeformlinksview-container">
            <svg className="collectionfreeformlinksview-svgCanvas">
                {SelectionManager.GetIsDragging() ? (null) : this.uniqueConnections}
            </svg>
            {this.props.children}
        </div>;
    }
    // _brushReactionDisposer?: IReactionDisposer;
    // componentDidMount() {
    //     this._brushReactionDisposer = reaction(
    //         () => {
    //             let doclist = DocListCast(this.props.Document[this.props.fieldKey]);
    //             return { doclist: doclist ? doclist : [], xs: doclist.map(d => d.x) };
    //         },
    //         () => {
    //             let doclist = DocListCast(this.props.Document[this.props.fieldKey]);
    //             let views = doclist ? doclist.filter(doc => StrCast(doc.backgroundLayout).indexOf("istogram") !== -1) : [];
    //             views.forEach((dstDoc, i) => {
    //                 views.forEach((srcDoc, j) => {
    //                     let dstTarg = dstDoc;
    //                     let srcTarg = srcDoc;
    //                     let x1 = NumCast(srcDoc.x);
    //                     let x2 = NumCast(dstDoc.x);
    //                     let x1w = NumCast(srcDoc.width, -1);
    //                     let x2w = NumCast(dstDoc.width, -1);
    //                     if (x1w < 0 || x2w < 0 || i === j) { }
    //                     else {
    //                         let findBrush = (field: (Doc | Promise<Doc>)[]) => field.findIndex(brush => {
    //                             let bdocs = brush instanceof Doc ? Cast(brush.brushingDocs, listSpec(Doc), []) : undefined;
    //                             return bdocs && bdocs.length && ((bdocs[0] === dstTarg && bdocs[1] === srcTarg)) ? true : false;
    //                         });
    //                         let brushAction = (field: (Doc | Promise<Doc>)[]) => {
    //                             let found = findBrush(field);
    //                             if (found !== -1) {
    //                                 field.splice(found, 1);
    //                             }
    //                         };
    //                         if (Math.abs(x1 + x1w - x2) < 20) {
    //                             let linkDoc: Doc = new Doc();
    //                             linkDoc.title = "Histogram Brush";
    //                             linkDoc.linkDescription = "Brush between " + StrCast(srcTarg.title) + " and " + StrCast(dstTarg.Title);
    //                             linkDoc.brushingDocs = new List([dstTarg, srcTarg]);

    //                             brushAction = (field: (Doc | Promise<Doc>)[]) => {
    //                                 if (findBrush(field) === -1) {
    //                                     field.push(linkDoc);
    //                                 }
    //                             };
    //                         }
    //                         if (dstTarg.brushingDocs === undefined) dstTarg.brushingDocs = new List<Doc>();
    //                         if (srcTarg.brushingDocs === undefined) srcTarg.brushingDocs = new List<Doc>();
    //                         let dstBrushDocs = Cast(dstTarg.brushingDocs, listSpec(Doc), []);
    //                         let srcBrushDocs = Cast(srcTarg.brushingDocs, listSpec(Doc), []);
    //                         brushAction(dstBrushDocs);
    //                         brushAction(srcBrushDocs);
    //                     }
    //                 });
    //             });
    //         });
    // }
    // componentWillUnmount() {
    //     this._brushReactionDisposer?.();
    // }
}