aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/collections/CollectionStaffView.tsx
blob: c5c3f96e8ec5bce9adac0b8251d890de13018117 (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
import { CollectionSubView } from "./CollectionSubView";
import React = require("react");
import { computed, action, IReactionDisposer, reaction, runInAction, observable } from "mobx";
import { NumCast } from "../../../fields/Types";
import "./CollectionStaffView.scss";
import { observer } from "mobx-react";

@observer
export class CollectionStaffView extends CollectionSubView(doc => doc) {
    private _reactionDisposer: IReactionDisposer | undefined;
    @observable private _staves = NumCast(this.props.Document.staves);

    componentWillUnmount() {
        this._reactionDisposer?.();
    }
    componentDidMount = () => {
        this._reactionDisposer = reaction(() => NumCast(this.props.Document.staves),
            (staves) => runInAction(() => this._staves = staves)
        );

        this.props.Document.staves = 5;
    }

    @computed get addStaffButton() {
        return <div onPointerDown={this.addStaff}>+</div>;
    }

    @computed get staves() {
        const staves = [];
        for (let i = 0; i < this._staves; i++) {
            const rows = [];
            for (let j = 0; j < 5; j++) {
                rows.push(<div key={`staff-${i}-${j}`} className="collectionStaffView-line"></div>);
            }
            staves.push(<div key={`staff-${i}`} className="collectionStaffView-staff">
                {rows}
            </div>);
        }
        return staves;
    }

    @action
    addStaff = (e: React.PointerEvent) => {
        this.props.Document.staves = this._staves + 1;
    }

    render() {
        return <div className="collectionStaffView">
            {this.staves}
            {this.addStaffButton}
        </div>;
    }
}