aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/collections/collectionFreeForm/CollectionFreeFormLinksView.tsx
blob: d4809ac1c5def1651fda3acb6d50d657c4a0c556 (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
103
104
105
106
107
108
109
110
111
import { computed, reaction, runInAction, trace } from "mobx";
import { observer } from "mobx-react";
import { Document } from "../../../../fields/Document";
import { FieldWaiting } from "../../../../fields/Field";
import { KeyStore } from "../../../../fields/KeyStore";
import { ListField } from "../../../../fields/ListField";
import { Utils } from "../../../../Utils";
import { DocumentManager } from "../../../util/DocumentManager";
import { DocumentView } from "../../nodes/DocumentView";
import { CollectionViewProps } from "../CollectionViewBase";
import "./CollectionFreeFormLinksView.scss";
import { CollectionFreeFormLinkView } from "./CollectionFreeFormLinkView";
import React = require("react");
import v5 = require("uuid/v5");
import { find } from "async";

@observer
export class CollectionFreeFormLinksView extends React.Component<CollectionViewProps> {

    componentDidMount() {
        reaction(() => {
            return DocumentManager.Instance.getAllDocumentViews(this.props.Document).map(dv => dv.props.Document.GetNumber(KeyStore.X, 0))
        }, () => {
            let views = DocumentManager.Instance.getAllDocumentViews(this.props.Document);
            for (let i = 0; i < views.length; i++) {
                for (let j = 0; j < views.length; j++) {
                    let srcDoc = views[j].props.Document;
                    let dstDoc = views[i].props.Document;
                    let x1 = srcDoc.GetNumber(KeyStore.X, 0);
                    let x1w = srcDoc.GetNumber(KeyStore.Width, -1);
                    let x2 = dstDoc.GetNumber(KeyStore.X, 0);
                    let x2w = dstDoc.GetNumber(KeyStore.Width, -1);
                    if (x1w < 0 || x2w < 0 || i == j)
                        continue;
                    let dstTarg = dstDoc;
                    let srcTarg = srcDoc;
                    let findBrush = (field: ListField<Document>) => field.Data.findIndex(brush => {
                        let bdocs = brush ? brush.GetList(KeyStore.BrushingDocs, [] as Document[]) : [];
                        return (bdocs.length && ((bdocs[0] == dstTarg && bdocs[1] == srcTarg)) ? true : false)
                    });
                    let brushAction = (field: ListField<Document>) => {
                        let found = findBrush(field);
                        if (found != -1) {
                            console.log("REMOVE BRUSH " + srcTarg.Title + " " + dstTarg.Title);
                            field.Data.splice(found, 1);
                        }
                    };
                    if (Math.abs(x1 + x1w - x2) < 20) {
                        let linkDoc: Document = new Document();
                        linkDoc.SetText(KeyStore.Title, "Histogram Brush");
                        linkDoc.SetText(KeyStore.LinkDescription, "Brush between " + srcTarg.Title + " and " + dstTarg.Title);
                        linkDoc.SetData(KeyStore.BrushingDocs, [dstTarg, srcTarg], ListField);

                        brushAction = brushAction = (field: ListField<Document>) => {
                            if (findBrush(field) == -1) {
                                console.log("ADD BRUSH " + srcTarg.Title + " " + dstTarg.Title);
                                (findBrush(field) == -1) && field.Data.push(linkDoc);
                            }
                        };
                    }
                    dstTarg.GetOrCreateAsync(KeyStore.BrushingDocs, ListField, brushAction);
                    srcTarg.GetOrCreateAsync(KeyStore.BrushingDocs, ListField, brushAction);

                }
            }
        })
    }
    documentAnchors(view: DocumentView) {
        let equalViews = [view];
        let containerDoc = view.props.Document.GetT(KeyStore.AnnotationOn, Document);
        if (containerDoc && containerDoc != FieldWaiting && containerDoc instanceof Document) {
            equalViews = DocumentManager.Instance.getDocumentViews(containerDoc.GetPrototype() as Document)
        }
        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: Document, b: Document, }[] = [];
            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 match = (possiblePair.a == drawnPair.a && possiblePair.b == drawnPair.b);
                    if (match) {
                        if (!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] as Document[] });
                }
            })
            return drawnPairs
        }, [] as { a: Document, b: Document, l: Document[] }[]);
        return connections.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">
                    {this.uniqueConnections}
                </svg>
                {this.props.children}
            </div>
        );
    }
}