aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/linking/LinkMenuGroup.tsx
blob: cd735318e33defc0383d502aeba632bda800e60d (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
/* eslint-disable jsx-a11y/no-static-element-interactions */
/* eslint-disable jsx-a11y/click-events-have-key-events */
/* eslint-disable react/require-default-props */
import { action, observable } from 'mobx';
import { observer } from 'mobx-react';
import * as React from 'react';
import { Doc, StrListCast } from '../../../fields/Doc';
import { Id } from '../../../fields/FieldSymbols';
import { Cast, DocCast } from '../../../fields/Types';
import { DocumentType } from '../../documents/DocumentTypes';
import { DocumentView } from '../nodes/DocumentView';
import './LinkMenu.scss';
import { LinkMenuItem } from './LinkMenuItem';

interface LinkMenuGroupProps {
    sourceDoc: Doc;
    group: Doc[];
    groupType: string;
    clearLinkEditor?: () => void;
    docView: DocumentView;
    itemHandler?: (doc: Doc) => void;
}

@observer
export class LinkMenuGroup extends React.Component<LinkMenuGroupProps> {
    private _menuRef = React.createRef<HTMLDivElement>();
    @observable _collapsed = false;

    getBackgroundColor = (): string | undefined => {
        const linkRelationshipList = StrListCast(Doc.UserDoc().link_relationshipList);
        const linkColorList = StrListCast(Doc.UserDoc().link_ColorList);
        let color: string | undefined;
        // if this link's relationship property is not default "link", set its color
        if (linkRelationshipList) {
            const relationshipIndex = linkRelationshipList.indexOf(this.props.groupType);
            const RGBcolor: string = linkColorList[relationshipIndex];
            if (RGBcolor) {
                // set opacity to 0.25 by modifiying the rgb string
                color = RGBcolor.slice(0, RGBcolor.length - 1) + ', 0.25)';
            }
        }
        return color;
    };

    render() {
        const set = new Set<Doc>(this.props.group);
        const groupItems = Array.from(set.keys()).map(linkDoc => {
            const sourceDoc =
                this.props.docView.anchorViewDoc ??
                (this.props.docView.Document.type === DocumentType.LINK //
                    ? this.props.docView._props.LayoutTemplateString?.includes('link_anchor_1')
                        ? DocCast(linkDoc.link_anchor_1)
                        : DocCast(linkDoc.link_anchor_2)
                    : this.props.sourceDoc);
            const destDoc = !sourceDoc
                ? undefined
                : this.props.docView.Document.type === DocumentType.LINK
                  ? this.props.docView._props.LayoutTemplateString?.includes('link_anchor_1')
                      ? DocCast(linkDoc.link_anchor_2)
                      : DocCast(linkDoc.link_anchor_1)
                  : Doc.getOppositeAnchor(linkDoc, sourceDoc) || Doc.getOppositeAnchor(linkDoc, Cast(linkDoc.link_anchor_2, Doc, null)?.annotationOn === sourceDoc ? Cast(linkDoc.link_anchor_2, Doc, null) : Cast(linkDoc.link_anchor_1, Doc, null));
            return !destDoc || !sourceDoc ? null : (
                <LinkMenuItem
                    key={linkDoc[Id]}
                    itemHandler={this.props.itemHandler}
                    groupType={this.props.groupType}
                    docView={this.props.docView}
                    linkDoc={linkDoc}
                    sourceDoc={sourceDoc}
                    destinationDoc={destDoc}
                    clearLinkEditor={this.props.clearLinkEditor}
                    menuRef={this._menuRef}
                />
            );
        });

        return (
            <div className="linkMenu-group" ref={this._menuRef}>
                <div
                    className="linkMenu-group-name"
                    onClick={action(() => {
                        this._collapsed = !this._collapsed;
                    })}
                    style={{ background: this.getBackgroundColor() }}>
                    <p className={this.props.groupType === '*' || this.props.groupType === '' ? '' : 'expand-one'}> {this.props.groupType}:</p>
                </div>
                {this._collapsed ? null : <div className="linkMenu-group-wrapper">{groupItems}</div>}
            </div>
        );
    }
}