From 0c3ba4ea0b9eb471e2f2afb7dd235d279e350f18 Mon Sep 17 00:00:00 2001 From: elgransan Date: Mon, 11 Apr 2022 18:36:07 -0300 Subject: [PATCH] * created callback functionality (#3) * added example page * added example class and service * removed logs Co-authored-by: Santiago Cattaneo --- .../BlazorReorderExample.csproj | 6 ++ BlazorReorderExample/Pages/Callbacks.razor | 71 +++++++++++++++++++ BlazorReorderExample/Program.cs | 15 ++++ BlazorReorderExample/Shared/NavMenu.razor | 5 ++ BlazorReorderList/Reorder.razor | 29 ++------ 5 files changed, 104 insertions(+), 22 deletions(-) create mode 100644 BlazorReorderExample/Pages/Callbacks.razor diff --git a/BlazorReorderExample/BlazorReorderExample.csproj b/BlazorReorderExample/BlazorReorderExample.csproj index 23944bc..ed66c7a 100644 --- a/BlazorReorderExample/BlazorReorderExample.csproj +++ b/BlazorReorderExample/BlazorReorderExample.csproj @@ -15,4 +15,10 @@ + + + true + + + diff --git a/BlazorReorderExample/Pages/Callbacks.razor b/BlazorReorderExample/Pages/Callbacks.razor new file mode 100644 index 0000000..2b1ef5b --- /dev/null +++ b/BlazorReorderExample/Pages/Callbacks.razor @@ -0,0 +1,71 @@ +@page "/Callbacks" + +Blazor Reorder Example + +

Callbacks

+
@callback: @data
+ +
+
+
+ +
+
@context.title
+

@context.details

+
+
+
+
+
+
+ +
+
@context.title
+

@context.details

+
+
+
+
+
+
+ +
+
@context.title
+

@context.details

+
+
+
+
+
+ +@code { + private string callback = ""; + private string data = ""; + + public List list1 = new() + { + new ItemList("Google", "Again looking for a bug ..."), + new ItemList("StackOverflow", "Could be this the solution?"), + new ItemList("GitHub", "Let's get awesome code"), + new ItemList("Twitter", "I want to rest"), + new ItemList("Another", "The solution must be somewhere!!!") + }; + + public List list2 = new() + { + new ItemList("Facebook", "Meta"), + new ItemList("Instagram", "Meta"), + new ItemList("Whatsapp", "Meta"), + }; + + public List list3 = new() + { + new ItemList("Oculus", "Meta"), + }; +} diff --git a/BlazorReorderExample/Program.cs b/BlazorReorderExample/Program.cs index bff5390..0827570 100644 --- a/BlazorReorderExample/Program.cs +++ b/BlazorReorderExample/Program.cs @@ -8,7 +8,22 @@ builder.RootComponents.Add("head::after"); builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); builder.Services.AddScoped>(); +builder.Services.AddScoped>(); await builder.Build().RunAsync(); public record ListItem(string title, string url, string details); + +public class ItemList +{ + public string title { get; set; } + public string details { get; set; } + public string domClass { get; set; } + + public ItemList(string a, string c) + { + title = a; + domClass = ""; + details = c; + } +} \ No newline at end of file diff --git a/BlazorReorderExample/Shared/NavMenu.razor b/BlazorReorderExample/Shared/NavMenu.razor index f3c4b0c..939ecc8 100644 --- a/BlazorReorderExample/Shared/NavMenu.razor +++ b/BlazorReorderExample/Shared/NavMenu.razor @@ -24,6 +24,11 @@ BetweenLists + diff --git a/BlazorReorderList/Reorder.razor b/BlazorReorderList/Reorder.razor index 3f229f1..235cab4 100644 --- a/BlazorReorderList/Reorder.razor +++ b/BlazorReorderList/Reorder.razor @@ -5,12 +5,6 @@ @inject NavigationManager nav @implements IDisposable -@if (Debug) -{ -

@log

-

@log2

-} - @if(rs.isDragging && elemPosition.x != -1000) {
ChildContent { get; set; } = null!; [Parameter, EditorRequired] public List Items { get; set; } = null!; [Parameter] public Func Copy { get; set; } + [Parameter] public EventCallback OnStart { get; set; } + [Parameter] public EventCallback OnChange { get; set; } + [Parameter] public EventCallback OnFinish { get; set; } [Parameter] public bool WithShadow { get; set; } = true; - [Parameter] public bool Debug { get; set; } = false; private bool shouldRender = true; // cancel re-rendering private DotNetObjectReference>? dotNetHelper; //js-interop 2-ways @@ -50,8 +46,6 @@ int elemWidth = 0; int newElemIndex = -1; - string log = "", log2 = ""; - protected override void OnInitialized() { nav.LocationChanged += HandleLocationChanged; @@ -92,14 +86,16 @@ clickPosition = await rs.getPoint(m); TItem? clone = Copy != null ? Copy(item) : default(TItem); rs.Set(Items, clone != null ? clone : item, index, clickPosition, clone != null); + await OnStart.InvokeAsync(rs.selected); } [JSInvokable] - public void onRelease(MouseEventArgs m) + public async Task onRelease(MouseEventArgs m) { elemPosition = new point(-1000, 0); if (rs.isDragging && rs.originItems == Items) { + await OnFinish.InvokeAsync(rs.selected); rs.Reset(); // flip @@ -109,7 +105,6 @@ shouldRender = false; Items.RemoveAt(rs.elemIndex); Items.Insert(newElemIndex, item); - Log("mouseup"); } shouldRender = true; } @@ -124,7 +119,6 @@ { shouldRender = false; ghostTrans = new point(pos.x - rs.elemClickPosition.x, pos.y - rs.elemClickPosition.y); - Log($"onMove ({elemPosition.x}, {elemPosition.y})"); // check if current drag item is over another item and swap places for (var b = 0; b < itemElem.Count; ++b) @@ -145,6 +139,7 @@ Items.Insert(newElemIndex, rs.selected); rs.elemIndex = newElemIndex; if (rs.originItems != Items) rs.originItems = Items; + await OnChange.InvokeAsync(rs.selected); break; } } @@ -165,20 +160,10 @@ { var box = await rs.getClientRect(item); if (box.width < 0) return false; - Log($"\npos x: {pos.x}, y: {pos.y}\n item: left:{box.left}, width:{box.width}\nitem: top:{box.top}, height:{box.height}", true); var isx = (pos.x > box.left && pos.x < (box.left + box.width)); var isy = (pos.y > box.top && pos.y < (box.top + box.height)); var res = isx && isy; return res; } - - void Log(string info, bool sublog = false) - { - if (Debug) - { - if (sublog) log2 = info; - else log = info; - } - } }