using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; using Microsoft.JSInterop; namespace BlazorReorderList; public class ReorderService : IReorderService { public bool isDragging { get; set; } = false; public TItem? selected { get; set; } = default; public List? originItems { get; set; } public int elemIndex { get; set; } = -1; public point elemClickPosition { get; set; } = new point(0, 0); private Lazy>? _moduleTask; private bool _copy = false; public ReorderService(IJSRuntime jsRuntime) { _moduleTask = new(() => jsRuntime.InvokeAsync( "import", "./_content/BlazorReorderList/ReorderJsInterop.js").AsTask()); } public ReorderService() { } public ReorderService InitService(IJSRuntime jsRuntime) { _moduleTask = new(() => jsRuntime.InvokeAsync( "import", "./_content/BlazorReorderList/ReorderJsInterop.js").AsTask()); return this; } public void Set(List list, TItem item, int index, point clickPoint, bool copy) { isDragging = true; originItems = list; selected = item; elemIndex = index; elemClickPosition = clickPoint; _copy = copy; } // whe copy only the first time public bool isCopy() { var tempcopy = _copy; _copy = false; return tempcopy; } public void Reset() { isDragging = false; originItems = default; selected = default; elemClickPosition = new point(0, 0); _copy = false; } public async ValueTask initEvents(DotNetObjectReference> dotNetInstance) { if (_moduleTask == null) throw new Exception("Blazor Reorder: JS module not initializated"); var module = await _moduleTask.Value; await module.InvokeVoidAsync("initEvents", dotNetInstance); } public async ValueTask removeEvents(DotNetObjectReference> dotNetInstance) { if (_moduleTask == null) throw new Exception("Blazor Reorder: JS module not initializated"); var module = await _moduleTask.Value; await module.InvokeVoidAsync("removeEvents", dotNetInstance); } public async ValueTask getWidth(ElementReference el) { if (_moduleTask == null) throw new Exception("Blazor Reorder: JS module not initializated"); var module = await _moduleTask.Value; return await module.InvokeAsync("getWidth", el); } public async ValueTask getPosition(ElementReference el) { if (_moduleTask == null) throw new Exception("Blazor Reorder: JS module not initializated"); var module = await _moduleTask.Value; return await module.InvokeAsync("getPosition", el); } public async ValueTask getPoint(double pageX, double pageY, double clientX, double clientY) { if (_moduleTask == null) throw new Exception("Blazor Reorder: JS module not initializated"); var module = await _moduleTask.Value; return await module.InvokeAsync("getPoint", pageX, pageY, clientX, clientY); } public async ValueTask getClientRect(ElementReference el) { if (_moduleTask == null) throw new Exception("Blazor Reorder: JS module not initializated"); var module = await _moduleTask.Value; return await module.InvokeAsync("getClientRect", el); } public async ValueTask DisposeAsync() { if (_moduleTask == null) throw new Exception("Blazor Reorder: JS module not initializated"); if (_moduleTask.IsValueCreated) { var module = await _moduleTask.Value; await module.DisposeAsync(); GC.SuppressFinalize(this); } } }