aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/ContextMenu.tsx
blob: 4f26a75d22f313595bd96cd2a57ebe1cacd0b93f (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
import React = require("react");
import { ContextMenuItem, ContextMenuProps } from "./ContextMenuItem";
import { observable } from "mobx";
import { observer } from "mobx-react";
import "./ContextMenu.scss"

@observer
export class ContextMenu extends React.Component {
    static Instance: ContextMenu

    @observable private _items: Array<ContextMenuProps> = [{ description: "test", event: (e: React.MouseEvent) => e.preventDefault() }];
    @observable private _pageX: number = 0;
    @observable private _pageY: number = 0;
    @observable private _display: string = "none";

    private ref: React.RefObject<HTMLDivElement>;

    constructor(props: Readonly<{}>) {
        super(props);

        this.ref = React.createRef()

        ContextMenu.Instance = this;
    }

    clearItems() {
        this._items = []
        this._display = "none"
    }

    addItem(item: ContextMenuProps) {
        if (this._items.indexOf(item) === -1) {
            this._items.push(item);
        }
    }

    getItems() {
        return this._items;
    }

    displayMenu(x: number, y: number) {
        this._pageX = x
        this._pageY = y

        this._display = "flex"
    }

    intersects = (x: number, y: number): boolean => {
        if (this.ref.current && this._display !== "none") {
            if (x >= this._pageX && x <= this._pageX + this.ref.current.getBoundingClientRect().width) {
                if (y >= this._pageY && y <= this._pageY + this.ref.current.getBoundingClientRect().height) {
                    return true;
                }
            }
        }
        return false;
    }

    render() {
        return (
            <div className="contextMenu-cont" style={{ left: this._pageX, top: this._pageY, display: this._display }} ref={this.ref}>
                {this._items.map(prop => {
                    return <ContextMenuItem {...prop} key={prop.description} />
                })}
            </div>
        )
    }
}