aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/RecommendationsBox.tsx
blob: 8ca81c070c7ca1b31895de40c4faf94e064923a0 (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
import { observer } from "mobx-react";
import React = require("react");
import { observable, action, computed, runInAction } from "mobx";
import Measure from "react-measure";
import "./RecommendationsBox.scss";
import { Doc, DocListCast, WidthSym, HeightSym } from "../../fields/Doc";
import { DocumentIcon } from "./nodes/DocumentIcon";
import { StrCast, NumCast } from "../../fields/Types";
import { returnFalse, emptyFunction, returnEmptyString, returnOne, emptyPath, returnZero } from "../../Utils";
import { Transform } from "../util/Transform";
import { ObjectField } from "../../fields/ObjectField";
import { DocumentView } from "./nodes/DocumentView";
import { DocumentType } from '../documents/DocumentTypes';
import { ClientRecommender } from "../ClientRecommender";
import { DocServer } from "../DocServer";
import { Id } from "../../fields/FieldSymbols";
import { FieldView, FieldViewProps } from "./nodes/FieldView";
import { DocumentManager } from "../util/DocumentManager";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { library } from "@fortawesome/fontawesome-svg-core";
import { faBullseye, faLink } from "@fortawesome/free-solid-svg-icons";
import { DocUtils } from "../documents/Documents";

export interface RecProps {
    documents: { preview: Doc, similarity: number }[];
    node: Doc;
}

library.add(faBullseye, faLink);

@observer
export class RecommendationsBox extends React.Component<FieldViewProps> {

    public static LayoutString(fieldKey: string) { return FieldView.LayoutString(RecommendationsBox, fieldKey); }

    // @observable private _display: boolean = false;
    @observable private _pageX: number = 0;
    @observable private _pageY: number = 0;
    @observable private _width: number = 0;
    @observable private _height: number = 0;
    @observable.shallow private _docViews: JSX.Element[] = [];
    // @observable private _documents: { preview: Doc, score: number }[] = [];
    private previewDocs: Doc[] = [];

    constructor(props: FieldViewProps) {
        super(props);
    }

    @action
    private DocumentIcon(doc: Doc) {
        const layoutresult = StrCast(doc.type);
        let renderDoc = doc;
        //let box: number[] = [];
        if (layoutresult.indexOf(DocumentType.COL) !== -1) {
            renderDoc = Doc.MakeDelegate(renderDoc);
        }
        const returnXDimension = () => 150;
        const returnYDimension = () => 150;
        const scale = () => returnXDimension() / NumCast(renderDoc.nativeWidth, returnXDimension());
        //let scale = () => 1;
        const newRenderDoc = Doc.MakeAlias(renderDoc); ///   newRenderDoc -> renderDoc -> render"data"Doc -> TextProt
        newRenderDoc.height = NumCast(this.props.Document.documentIconHeight);
        newRenderDoc.autoHeight = false;
        const docview = <div>
            <DocumentView
                fitToBox={StrCast(doc.type).indexOf(DocumentType.COL) !== -1}
                Document={newRenderDoc}
                addDocument={returnFalse}
                LibraryPath={emptyPath}
                removeDocument={returnFalse}
                rootSelected={returnFalse}
                ScreenToLocalTransform={Transform.Identity}
                addDocTab={returnFalse}
                pinToPres={returnFalse}
                renderDepth={1}
                NativeHeight={returnZero}
                NativeWidth={returnZero}
                PanelWidth={returnXDimension}
                PanelHeight={returnYDimension}
                focus={emptyFunction}
                backgroundColor={returnEmptyString}
                parentActive={returnFalse}
                whenActiveChanged={returnFalse}
                bringToFront={emptyFunction}
                ContainingCollectionView={undefined}
                ContainingCollectionDoc={undefined}
                ContentScaling={scale}
            />
        </div>;
        return docview;

    }

    // @action
    // closeMenu = () => {
    //     this._display = false;
    //     this.previewDocs.forEach(doc => DocServer.DeleteDocument(doc[Id]));
    //     this.previewDocs = [];
    // }

    // @action
    // resetDocuments = () => {
    //     this._documents = [];
    // }

    // @action
    // displayRecommendations(x: number, y: number) {
    //     this._pageX = x;
    //     this._pageY = y;
    //     this._display = true;
    // }

    static readonly buffer = 20;

    // get pageX() {
    //     const x = this._pageX;
    //     if (x < 0) {
    //         return 0;
    //     }
    //     const width = this._width;
    //     if (x + width > window.innerWidth - RecommendationsBox.buffer) {
    //         return window.innerWidth - RecommendationsBox.buffer - width;
    //     }
    //     return x;
    // }

    // get pageY() {
    //     const y = this._pageY;
    //     if (y < 0) {
    //         return 0;
    //     }
    //     const height = this._height;
    //     if (y + height > window.innerHeight - RecommendationsBox.buffer) {
    //         return window.innerHeight - RecommendationsBox.buffer - height;
    //     }
    //     return y;
    // }

    // get createDocViews() {
    //     return DocListCast(this.props.Document.data).map(doc => {
    //         return (
    //             <div className="content">
    //                 <span style={{ height: NumCast(this.props.Document.documentIconHeight) }} className="image-background">
    //                     {this.DocumentIcon(doc)}
    //                 </span>
    //                 <span className="score">{NumCast(doc.score).toFixed(4)}</span>
    //                 <div style={{ marginRight: 50 }} onClick={() => DocumentManager.Instance.jumpToDocument(doc, false)}>
    //                     <FontAwesomeIcon className="documentdecorations-icon" icon={"bullseye"} size="sm" />
    //                 </div>
    //                 <div style={{ marginRight: 50 }} onClick={() => DocUtils.MakeLink({ doc: this.props.Document.sourceDoc as Doc }, { doc: doc }, "User Selected Link", "Generated from Recommender", undefined)}>
    //                     <FontAwesomeIcon className="documentdecorations-icon" icon={"link"} size="sm" />
    //                 </div>
    //             </div>
    //         );
    //     });
    // }

    componentDidMount() { //TODO: invoking a computedFn from outside an reactive context won't be memoized, unless keepAlive is set
        runInAction(() => {
            if (this._docViews.length === 0) {
                this._docViews = DocListCast(this.props.Document.data).map(doc => {
                    return (
                        <div className="content">
                            <span style={{ height: NumCast(this.props.Document.documentIconHeight) }} className="image-background">
                                {this.DocumentIcon(doc)}
                            </span>
                            <span className="score">{NumCast(doc.score).toFixed(4)}</span>
                            <div style={{ marginRight: 50 }} onClick={() => DocumentManager.Instance.jumpToDocument(doc, false)}>
                                <FontAwesomeIcon className="documentdecorations-icon" icon={"bullseye"} size="sm" />
                            </div>
                            <div style={{ marginRight: 50 }} onClick={() => DocUtils.MakeLink({ doc: this.props.Document.sourceDoc as Doc }, { doc: doc }, "Recommender", undefined)}>
                                <FontAwesomeIcon className="documentdecorations-icon" icon={"link"} size="sm" />
                            </div>
                        </div>
                    );
                });
            }
        });
    }

    render() { //TODO: Invariant violation: max depth exceeded error. Occurs when images are rendered. 
        // if (!this._display) {
        //     return null;
        // }
        // let style = { left: this.pageX, top: this.pageY };
        //const transform = "translate(" + (NumCast(this.props.node.x) + 350) + "px, " + NumCast(this.props.node.y) + "px"
        let title = StrCast((this.props.Document.sourceDoc as Doc).title);
        if (title.length > 15) {
            title = title.substring(0, 15) + "...";
        }
        return (
            <div className="rec-scroll">
                <p>Recommendations for "{title}"</p>
                {this._docViews}
            </div>
        );
    }
    // 
    // 
}