aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/DocumentManager.tsx
blob: a53ed4a91bb4cda13e30d816ece8d5dad0d2a072 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
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/KeyStore';
import { CollectionViewBase } from './collections/CollectionViewBase';
import { CollectionViewType, CollectionView } from './collections/CollectionView';


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 getDocumentViewFreeform2(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 && view.props.ContainingCollectionView.collectionViewType == CollectionViewType.Freeform) {
    //             if (Object.is(doc, toFind)) {
    //                 console.log("finding view")
    //                 toReturn = view;
    //                 return;
    //             }
    //         }
    //     })

    //     return (toReturn);
    // }

    public getCollectionView(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 instanceof CollectionView) {
                console.log("finding view")
                if (Object.is(doc, toFind)) {
                    toReturn = view;
                    return;
                }
            }
        })

        return (toReturn);
    }

    // @action
    // public centerNode2(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;

    //     //if the view exists in a freeform collection
    //     if (docView) {
    //         let { width, height } = docView.size();

    //         //base case: parent of parent does not exist
    //         if (!docView.props.ContainingCollectionView) {
    //             scale = docView.props.ScreenToLocalTransform().Scale
    //             let doc = docView.props.Document;
    //             console.log("hello")
    //             XView = (-doc.GetNumber(KeyStore.X, 0) * scale) - (width * scale / 2)
    //             YView = (-doc.GetNumber(KeyStore.Y, 0) * scale) - (height * scale / 2)
    //             //set x and y view of parent
    //             if (docView instanceof CollectionView) {
    //                 console.log("here")
    //                 DocumentManager.Instance.setViewportXY(docView, XView, YView)
    //             }
    //         }
    //         //parent is not main, parent is centered and calls itself
    //         else {
    //             if (true) {
    //                 //view of parent
    //                 let scale = docView.props.ContainingCollectionView.props.Document.GetNumber(KeyStore.Scale, 1)
    //                 let doc = docView.props.Document

    //                 //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.collectionViewType == CollectionViewType.Freeform) {
    //                     //scale of parent
    //                     console.log("scale: " + scale)
    //                     XView = (-doc.GetNumber(KeyStore.X, 0) * scale) - (width * scale / 2);
    //                     YView = (-doc.GetNumber(KeyStore.Y, 0) * scale) - (height * scale / 2);
    //                     // //node.Parent.setViewportXY(XView, YView);
    //                     DocumentManager.Instance.setViewportXY(docView.props.ContainingCollectionView, XView, YView)
    //                     return DocumentManager.Instance.centerNode2(docView.props.ContainingCollectionView.props.Document)
    //                 }
    //                 else { return DocumentManager.Instance.centerNode2(docView.props.ContainingCollectionView.props.Document) }
    //             }
    //             else {
    //                 // return DocumentManager.Instance.centerNode2(docView.props.ContainingCollectionView.props.Document)
    //             }
    //         }
    //     }
    // }

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

    //     console.log("things are happening")

    //     let docView: DocumentView | null;

    //     if (doc instanceof Document) {
    //         console.log(doc.Title)
    //         docView = DocumentManager.Instance.getDocumentViewFreeform(doc)
    //     }
    //     else {
    //         docView = doc
    //         console.log(docView.props.Document.Title)
    //     }

    //     let scale: number;
    //     let XView: number;
    //     let YView: number;

    //     //if the view exists in a freeform collection
    //     if (docView) {
    //         let { width, height } = docView.size();

    //         if (docView.props.ContainingCollectionView) {
    //             //view of parent
    //             let scale = docView.props.ContainingCollectionView.props.Document.GetNumber(KeyStore.Scale, 1)
    //             let doc = docView.props.Document

    //             if (docView.props.ContainingCollectionView.collectionViewType == CollectionViewType.Freeform) {
    //                 //scale of parent
    //                 XView = (-doc.GetNumber(KeyStore.X, 0) * scale) - (width * scale / 2);
    //                 YView = (-doc.GetNumber(KeyStore.Y, 0) * scale) - (height * scale / 2);
    //                 DocumentManager.Instance.setViewportXY(docView.props.ContainingCollectionView, XView, YView)
    //                 return DocumentManager.Instance.centerNode4(docView.props.ContainingCollectionView.props.Document)
    //             }
    //             else { return DocumentManager.Instance.centerNode4(docView.props.ContainingCollectionView.props.Document) }
    //         }
    //     }
    // }

    @action
    public centerNode(doc: Document | DocumentView, collection: Document): void {
        //console.log(doc.Title)
        //gets document view that is in freeform collection 
        let docView: DocumentView | null;

        if (doc instanceof Document) {
            console.log(doc.Title)
            docView = DocumentManager.Instance.getDocumentView(doc)
        }
        else {
            docView = doc
            console.log(docView.props.Document.Title)
        }

        let scale: number;
        let XView: number;
        let YView: number;

        if (docView) {
            let { width, height } = docView.size();
            let scale = docView.props.Document.GetNumber(KeyStore.Scale, 1);
            let doc = docView.props.Document;
            let x = doc.GetNumber(KeyStore.X, 0);
            let y = doc.GetNumber(KeyStore.X, 0);

            if (x && y) {
                XView = (-x * scale) - (width * scale / 2);
                YView = (-y * scale) - (height * scale / 2);
                DocumentManager.Instance.setViewportXY(collection, XView, YView)
            }

        }
    }

    // @action
    // public centerNode3(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;

    //     //if the view exists in a freeform collection
    //     if (docView) {
    //         let { width, height } = docView.size();

    //         //parent is not main, parent is centered and calls itself
    //         if (docView.props.ContainingCollectionView) {
    //             //view of parent
    //             let scale = docView.props.ContainingCollectionView.props.Document.GetNumber(KeyStore.Scale, 1)
    //             let doc = docView.props.Document

    //             if (docView.props.ContainingCollectionView.collectionViewType == CollectionViewType.Freeform) {
    //                 //scale of parent
    //                 XView = doc.GetNumber(KeyStore.X, 0) - width / 2
    //                 YView = doc.GetNumber(KeyStore.Y, 0) - height / 2
    //                 // console.log("X location: " + XView)
    //                 // console.log("Y location: " + YView)
    //                 DocumentManager.Instance.setViewportXY(docView.props.ContainingCollectionView, XView, YView)
    //                 return DocumentManager.Instance.centerNode3(docView.props.ContainingCollectionView.props.Document)
    //             }
    //             else { return DocumentManager.Instance.centerNode3(docView.props.ContainingCollectionView.props.Document) }
    //         }
    //     }
    // }


    @action
    private setViewportXY(collection: Document, x: number, y: number) {
        collection.SetNumber(KeyStore.PanX, x);
        collection.SetNumber(KeyStore.PanY, y);
    }
}