aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/PresentationView.tsx
blob: b4c12d0572905a7bccb295677fca7d424634f8ad (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
import { observer } from "mobx-react";
import React = require("react")
import { observable, action } from "mobx";
import "./PresentationView.scss"
import "./Main.tsx";
import { DocumentManager } from "../util/DocumentManager";
import { Utils } from "../../Utils";
import { Doc } from "../../new_fields/Doc";
import { listSpec } from "../../new_fields/Schema";
import { Cast, NumCast, FieldValue } from "../../new_fields/Types";
import { Id } from "../../new_fields/RefField";
import { List } from "../../new_fields/List";

export interface PresViewProps {
    Document: Doc;
}


@observer
/**
 * Component that takes in a document prop and a boolean whether it's collapsed or not.
 */
class PresentationViewItem extends React.Component<PresViewProps> {

    //look at CollectionFreeformView.focusDocument(d)
    @action
    openDoc = (doc: Doc) => {
        let docView = DocumentManager.Instance.getDocumentView(doc);
        if (docView) {
            docView.props.focus(docView.props.Document);
        }
    }

    /**
  * Removes a document from the presentation view
  **/
    @action
    public RemoveDoc(doc: Doc) {
        const value = Cast(this.props.Document.data, listSpec(Doc), []);
        let index = -1;
        for (let i = 0; i < value.length; i++) {
            if (value[i][Id] === doc[Id]) {
                index = i;
                break;
            }
        }
        if (index !== -1) {
            value.splice(index, 1);
        }
    }

    /**
     * Renders a single child document. It will just append a list element.
     * @param document The document to render.
     */
    renderChild(document: Doc) {
        let title = document.title;

        //to get currently selected presentation doc
        let selected = NumCast(this.props.Document.selectedDoc, 0);

        // finally, if it's a normal document, then render it as such.
        const children = Cast(this.props.Document.data, listSpec(Doc));
        const styles: any = {};
        if (children && children[selected] === document) {
            //this doc is selected
            styles.background = "gray";
        }
        return (
            <li className="presentationView-item" style={styles} key={Utils.GenerateGuid()}>
                <div className="presentationView-header" onClick={() => this.openDoc(document)}>{title}</div>
                <div className="presentation-icon" onClick={() => this.RemoveDoc(document)}>X</div>
            </li>
        );

    }

    render() {
        const children = Cast(this.props.Document.data, listSpec(Doc), []);

        return (
            <div>
                {children.map(value => this.renderChild(value))}
            </div>
        );
    }
}


@observer
export class PresentationView extends React.Component<PresViewProps>  {
    public static Instance: PresentationView;

    //observable means render is re-called every time variable is changed
    @observable
    collapsed: boolean = false;
    closePresentation = action(() => this.props.Document.width = 0);
    next = () => {
        const current = NumCast(this.props.Document.selectedDoc);
        const allDocs = FieldValue(Cast(this.props.Document.data, listSpec(Doc)));
        if (allDocs && current < allDocs.length + 1) {
            //can move forwards
            this.props.Document.selectedDoc = current + 1;
            const doc = allDocs[current + 1];
            let docView = DocumentManager.Instance.getDocumentView(doc);
            if (docView) {
                docView.props.focus(docView.props.Document);
            }
        }

    }
    back = () => {
        const current = NumCast(this.props.Document.selectedDoc);
        const allDocs = FieldValue(Cast(this.props.Document.data, listSpec(Doc)));
        if (allDocs && current - 1 >= 0) {
            //can move forwards
            this.props.Document.selectedDoc = current - 1;
            const doc = allDocs[current - 1];
            let docView = DocumentManager.Instance.getDocumentView(doc);
            if (docView) {
                docView.props.focus(docView.props.Document);
            }
        }
    }

    private ref = React.createRef<HTMLDivElement>();

    //initilize class variables
    constructor(props: PresViewProps) {
        super(props);
        PresentationView.Instance = this;
    }

    /**
     * Adds a document to the presentation view
     **/
    @action
    public PinDoc(doc: Doc) {
        //add this new doc to props.Document
        const data = Cast(this.props.Document.data, listSpec(Doc));
        if (data) {
            data.push(doc);
        } else {
            this.props.Document.data = new List([doc]);
        }

        this.props.Document.width = 300;
    }

    render() {
        let titleStr = this.props.Document.Title;
        let width = NumCast(this.props.Document.width);

        //TODO: next and back should be icons
        return (
            <div className="presentationView-cont" style={{ width: width, overflow: "hidden" }}>
                <div className="presentationView-heading">
                    <div className="presentationView-title">{titleStr}</div>
                    <div className='presentation-icon' onClick={this.closePresentation}>X</div></div>
                <div>
                    <div className="presentation-back" onClick={this.back}>back</div>
                    <div className="presentation-next" onClick={this.next}>next</div>

                </div>
                <ul>
                    <PresentationViewItem
                        Document={this.props.Document}
                    />
                </ul>
            </div>
        );
    }
}