summaryrefslogtreecommitdiff
path: root/Pages/Component/Dialog.razor
blob: ac0551586ae73873c21ed7565bc6134bb623a373 (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
@inject IJSRuntime jsRuntime
@implements IDialog

<div
    class="dialog"
    onmousedown="dialogMouseDown(event)"
    style="opacity:0;visibility:hidden;@(sizeStyle)"
    @ref=dialogDiv>
    @if(Title is not null) {
        <div class="titlebar" onmousedown="dialogTitleMouseDown(event)">
            <p class="title">@Title</p>
            <hr/>
        </div>
    }
    <div class="content">
        @ChildContent
    </div>
</div>

@code {
    [Parameter]
    public string? Title { get; set; }

    [Parameter]
    public RenderFragment ChildContent { get; set; }

    [Parameter]
    public int WidthPixels { set => width = $"{value}px"; }
    [Parameter]
    public int HeightPixels  { set => height = $"{value}px"; }
    [Parameter]
    public int HeightPercent { set => height = $"{value}%"; }
    [Parameter]
    public int MinHeightPixels  { set => minHeight = $"{value}px"; }
    [Parameter]
    public int MinHeightPercent { set => minHeight = $"{value}%"; }
    [Parameter]
    public int MaxHeightPixels  { set => maxHeight = $"{value}px"; }
    [Parameter]
    public int MaxHeightPercent { set => maxHeight = $"{value}%"; }

    public bool Visible {
        get => visible;
        set {
            visible = value;
            jsRuntime.InvokeVoidAsync(
                "setDialogVisibility",
                new object?[] { dialogDiv, value });
        }
    }

    private bool visible = false;

    private string? width;
    private string? height;
    private string? minHeight;
    private string? maxHeight;

    private ElementReference dialogDiv;

    public void Show() => Visible = true;
    public void Hide() => Visible = false;

    protected override async void OnAfterRender(bool firstRender) {
        if(firstRender) {
            await jsRuntime.InvokeVoidAsync("dialogAddObjectReference", new object[] {
                dialogDiv,
                DotNetObjectReference.Create(this)
    });
        }
    }

    [JSInvokable("KeyHandler")]
    public void KeyHandler(string key) {
        if(key == "Escape") {
            Hide();
            return;
        }
    }

    private string sizeStyle => string.Join("", new[] {
        widthStyle, heightStyle, minHeightStyle, maxHeightStyle
    });

    private string widthStyle =>
        $"{(width is null ? "" : $"width:{width};")}";

    private string heightStyle =>
        $"{(height is null ? "" : $"height:{height};")}";

    private string minHeightStyle =>
        $"{(minHeight is null ? "" : $"min-height:{minHeight};")}";

    private string maxHeightStyle =>
        $"{(maxHeight is null ? "" : $"max-height:{maxHeight};")}";
}