ホテル管理システム
ogi
yesterday 1a1c8e71fcd14858f595029f089b2d4a00202b32
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
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System.Reflection;
 
namespace HotelPms.Client.Blazor.Util
{
    public static class ElementReferenceEx
    {
        private static readonly PropertyInfo jsRuntimeProperty = typeof(WebElementReferenceContext).GetProperty("JSRuntime", BindingFlags.Instance | BindingFlags.NonPublic);
 
        internal static IJSRuntime GetJSRuntime(this ElementReference elementReference)
        {
            if (elementReference.Context is not WebElementReferenceContext context)
            {
                return null;
            }
 
            return (IJSRuntime)jsRuntimeProperty.GetValue(context);
        }
 
        /// <summary>
        /// input[text] or textarea
        /// </summary>
        /// <param name="elementReference"></param>
        /// <returns></returns>
        public static ValueTask<string> GetInputValue(this ElementReference elementReference)
        {
            return elementReference.GetJSRuntime()?.InvokeAsync<string>("NetCallJs.getInputValue", elementReference) ?? ValueTask.FromResult(string.Empty);
        }
 
        /// <summary>
        /// input[text] or textarea
        /// </summary>
        /// <param name="elementReference"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static ValueTask SetInputValue(this ElementReference elementReference, string value) => elementReference.GetJSRuntime()?.InvokeVoidAsync("NetCallJs.setInputValue", elementReference, value) ?? ValueTask.CompletedTask;
 
        public static ValueTask<BoundingClientRect> GetClientRect(this ElementReference elementReference)
        {
            return elementReference.GetJSRuntime()?.InvokeAsync<BoundingClientRect>("NetCallJs.getClientRect", elementReference) ?? ValueTask.FromResult(new BoundingClientRect());
        }
 
        public static ValueTask<ScrollPosition> GetScroll(this ElementReference elementReference)
        {
            return elementReference.GetJSRuntime()?.InvokeAsync<ScrollPosition>("NetCallJs.getScroll", elementReference) ?? ValueTask.FromResult(new ScrollPosition());
        }
    }
}