aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/FieldView.tsx
blob: 2e79ba7b004f0526571beee8cdcc03a955aa9e83 (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
import React = require("react");
import { computed } from "mobx";
import { observer } from "mobx-react";
import { DateField } from "../../../fields/DateField";
import { Doc, Field, FieldResult, Opt } from "../../../fields/Doc";
import { List } from "../../../fields/List";
import { VideoField, WebField } from "../../../fields/URLField";
import { DocumentViewSharedProps } from "./DocumentView";
import { VideoBox } from "./VideoBox";

//
// these properties get assigned through the render() method of the DocumentView when it creates this node.
// However, that only happens because the properties are "defined" in the markup for the field view.
// See the LayoutString method on each field view :   ImageBox, FormattedTextBox, etc. 
//
export interface FieldViewProps extends DocumentViewSharedProps {
    // FieldView specific props that are not part of DocumentView props
    fieldKey: string;
    overflow?: boolean; // bcz: would like to think this can be avoided -- need to look at further

    active: (outsideReaction?: boolean) => boolean;
    select: (isCtrlPressed: boolean) => void;
    isSelected: (outsideReaction?: boolean) => boolean;

    // properties intended to be used from within layout strings (otherwise use the function equivalents that work more efficiently with React)
    pointerEvents?: string;
    fontSize?: number;
    height?: number;
    width?: number;
    background?: string;
    color?: string;
    xMargin?: number;
    yMargin?: number;
}

@observer
export class FieldView extends React.Component<FieldViewProps> {
    public static LayoutString(fieldType: { name: string }, fieldStr: string) {
        return `<${fieldType.name} {...props} fieldKey={'${fieldStr}'}/>`;  //e.g., "<ImageBox {...props} fieldKey={"data} />"
    }

    @computed
    get field(): FieldResult {
        const { Document, fieldKey } = this.props;
        return Document[fieldKey];
    }
    render() {
        const field = this.field;
        if (field === undefined) {
            return <p>{'<null>'}</p>;
        }
        // if (typeof field === "string") {
        //     return <p>{field}</p>;
        // }
        // else if (field instanceof RichTextField) {
        //     return <FormattedTextBox {...this.props} />;
        // }
        // else if (field instanceof ImageField) {
        //     return <ImageBox {...this.props} />;
        // }
        // else if (field instaceof PresBox) {
        //    return <PresBox {...this.props} />;
        // }
        else if (field instanceof VideoField) {
            return <VideoBox {...this.props} />;
        }
        // else if (field instanceof AudioField) {
        //     return <AudioBox {...this.props} />;
        //}
        else if (field instanceof DateField) {
            return <p>{field.date.toLocaleString()}</p>;
        }
        else if (field instanceof Doc) {
            return <p><b>{field.title && field.title.toString()}</b></p>;
            //return <p><b>{field.title + " : id= " + field[Id]}</b></p>;
            // let returnHundred = () => 100;
            // return (
            //     <DocumentContentsView Document={field}
            //         addDocument={undefined}
            //         addDocTab={this.props.addDocTab}
            //         removeDocument={undefined}
            //         ScreenToLocalTransform={Transform.Identity}
            //         ContentScaling={returnOne}
            //         PanelWidth={returnHundred}
            //         PanelHeight={returnHundred}
            //         renderDepth={0} //TODO Why is renderDepth reset?
            //         focus={emptyFunction}
            //         isSelected={this.props.isSelected}
            //         select={returnFalse}
            //         layoutKey={"layout"}
            //         ContainingCollectionView={this.props.ContainingCollectionView}
            //         parentActive={this.props.active}
            //         whenActiveChanged={this.props.whenActiveChanged}
            //         bringToFront={emptyFunction} />
            // );
        }
        else if (field instanceof List) {
            return <div> {field.length ? field.map(f => Field.toString(f)).join(", ") : ""}  </div>;
        }
        // bcz: this belongs here, but it doesn't render well so taking it out for now
        else if (field instanceof WebField) {
            return <p>{Field.toString(field.url.href)}</p>;
        }
        else if (!(field instanceof Promise)) {
            return <p>{Field.toString(field)}</p>;
        }
        else {
            return <p> {"Waiting for server..."} </p>;
        }
    }

}