aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/DocumentManager.tsx
blob: ab54a795570b531bcb33c7f7fc61c709b4dda02e (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import React = require('react')
import { observer } from 'mobx-react';
import { observable, action } from 'mobx';
import { DocumentView } from './nodes/DocumentView';
import { Document } from "../../fields/Document"
import { CollectionFreeFormView } from './collections/CollectionFreeFormView';
import { KeyStore } from '../../fields/Key';
import { CollectionViewBase } from './collections/CollectionViewBase';


export class DocumentManager {

    //global holds all of the nodes (regardless of which collection they're in)
    @observable
    public DocumentViews: DocumentView[] = [];

    // singleton instance
    private static _instance: DocumentManager;

    // create one and only one instance of NodeManager
    public static get Instance(): DocumentManager {
        return this._instance || (this._instance = new this());
    }

    //private constructor so no other class can create a nodemanager
    private constructor() {
        // this.DocumentViews = new Array<DocumentView>();
    }

    public getDocumentView(toFind: Document): DocumentView | null {

        let toReturn: DocumentView | null;
        toReturn = null;

        //gets document view that is in a freeform canvas collection
        DocumentManager.Instance.DocumentViews.map(view => {
            let doc = view.props.Document;
            // if (view.props.ContainingCollectionView instanceof CollectionFreeFormView) {
            //     if (Object.is(doc, toFind)) {
            //         toReturn = view;
            //         return;
            //     }
            // }

            if (Object.is(doc, toFind)) {
                toReturn = view;
                return;
            }

        })

        return (toReturn);
    }

    public getDocumentViewFreeform(toFind: Document): DocumentView | null {

        let toReturn: DocumentView | null;
        toReturn = null;

        //gets document view that is in a freeform canvas collection
        DocumentManager.Instance.DocumentViews.map(view => {
            let doc = view.props.Document;
            if (view.props.ContainingCollectionView instanceof CollectionFreeFormView) {
                if (Object.is(doc, toFind)) {
                    toReturn = view;
                    return;
                }
            }
        })

        return (toReturn);
    }

    @action
    public centerNode(doc: Document | DocumentView): any {
        //console.log(doc.Title)
        //gets document view that is in freeform collection

        let docView: DocumentView | null;

        if (doc instanceof Document) {
            docView = DocumentManager.Instance.getDocumentViewFreeform(doc)
        }
        else {
            docView = doc
        }

        let scale: number;
        let XView: number;
        let YView: number;
        let width: number;
        let height: number;

        //if the view exists in a freeform collection
        if (docView && docView.MainContent.current) {
            width = docView.MainContent.current.clientWidth
            height = docView.MainContent.current.clientHeight

            //base case: parent of parent does not exist
            if (docView.props.ContainingCollectionView == null) {
                scale = docView.props.ScreenToLocalTransform().Scale

                let doc = docView.props.Document;

                XView = (-doc.GetNumber(KeyStore.X, 0) * scale) + (window.innerWidth / 2) - (width * scale / 2)
                YView = (-doc.GetNumber(KeyStore.Y, 0) * scale) + (window.innerHeight / 2) - (height * scale / 2)
                //set x and y view of parent
                if (docView instanceof CollectionFreeFormView) {
                    DocumentManager.Instance.setViewportXY(docView, XView, YView)
                }
            }
            //parent is not main, parent is centered and calls itself
            else {
                if (docView.props.ContainingCollectionView.props.ContainingDocumentView && docView.props.ContainingCollectionView.props.ContainingDocumentView.MainContent.current) {
                    //view of parent
                    let tempCollectionView = docView.props.ContainingCollectionView.props.ContainingDocumentView
                    let doc = docView.props.Document
                    let parentWidth = docView.props.ContainingCollectionView.props.ContainingDocumentView.MainContent.current.clientWidth
                    let parentHeight = docView.props.ContainingCollectionView.props.ContainingDocumentView.MainContent.current.clientHeight

                    //TODO: make sure to test if the parent view is a freeform view. if not, just skip to the next level
                    if (docView.props.ContainingCollectionView instanceof CollectionFreeFormView) {
                        //scale of parent
                        scale = tempCollectionView.props.ScreenToLocalTransform().Scale

                        XView = (-doc.GetNumber(KeyStore.X, 0) * scale) + (parentWidth / 2) - (width * scale / 2);
                        YView = (-doc.GetNumber(KeyStore.Y, 0) * scale) + (parentHeight / 2) - (height * scale / 2);
                        // //node.Parent.setViewportXY(XView, YView);
                        DocumentManager.Instance.setViewportXY(docView.props.ContainingCollectionView, XView, YView)

                        return DocumentManager.Instance.centerNode(docView.props.ContainingCollectionView.props.DocumentForCollection);
                    }
                }
                else {
                    return DocumentManager.Instance.centerNode(docView.props.ContainingCollectionView.props.DocumentForCollection)
                }
            }
        }
    }

    parentIsFreeform(node: any): boolean {
        return node.props.ContainingCollectionView instanceof CollectionFreeFormView
    }

    @action
    private setViewportXY(collection: CollectionFreeFormView, x: number, y: number) {
        console.log("viewport is setting")
        let doc = collection.props.DocumentForCollection;
        doc.SetNumber(KeyStore.PanX, x);
        doc.SetNumber(KeyStore.PanY, y);
    }
}