From b7386f8be297a4a5439bfad4bc76705de049e4af Mon Sep 17 00:00:00 2001 From: Lucie Bedouret Date: Fri, 2 Dec 2022 12:05:23 +0100 Subject: [PATCH] ADD : drag working --- .../Components/InventoryItem.razor | 12 +++++++++- .../Components/InventoryItem.razor.cs | 22 +++++++++++++---- .../myBlazorApp/Components/MyInventory.razor | 24 ++++++++++--------- .../Components/MyInventory.razor.cs | 21 ++++------------ 4 files changed, 46 insertions(+), 33 deletions(-) diff --git a/myBlazorApp/myBlazorApp/Components/InventoryItem.razor b/myBlazorApp/myBlazorApp/Components/InventoryItem.razor index 8a168d5..3755db3 100644 --- a/myBlazorApp/myBlazorApp/Components/InventoryItem.razor +++ b/myBlazorApp/myBlazorApp/Components/InventoryItem.razor @@ -1,4 +1,14 @@ -
+ +
@if (Item != null) { @Item.DisplayName diff --git a/myBlazorApp/myBlazorApp/Components/InventoryItem.razor.cs b/myBlazorApp/myBlazorApp/Components/InventoryItem.razor.cs index 1fafa8d..3efe7a0 100644 --- a/myBlazorApp/myBlazorApp/Components/InventoryItem.razor.cs +++ b/myBlazorApp/myBlazorApp/Components/InventoryItem.razor.cs @@ -1,6 +1,5 @@ using System; namespace myBlazorApp.Components; - using Blazorise; using Microsoft.AspNetCore.Components; using myBlazorApp.Models; @@ -9,27 +8,41 @@ public partial class InventoryItem { [Parameter] public Item Item { get; set; } + [Parameter] public int Index { get; set; } + + [Parameter] + public bool NoDrop { get; set; } + [CascadingParameter] public MyInventory Parent { get; set; } internal void OnDragEnter() { - + if (NoDrop) + { + return; + } Parent.Actions.Add(new InventoryAction { Action = "Drag Enter", Item = this.Item, Index = this.Index }); } internal void OnDragLeave() { - + if (NoDrop) + { + return; + } Parent.Actions.Add(new InventoryAction { Action = "Drag Leave", Item = this.Item, Index = this.Index }); } internal void OnDrop() { + if (NoDrop) + { + return; + } this.Item = Parent.CurrentDragItem; - Parent.Actions.Add(new InventoryAction { Action = "Drop", Item = this.Item, Index = this.Index }); } @@ -37,7 +50,6 @@ public partial class InventoryItem private void OnDragStart() { Parent.CurrentDragItem = this.Item; - Parent.Actions.Add(new InventoryAction { Action = "Drag Start", Item = this.Item, Index = this.Index }); } } diff --git a/myBlazorApp/myBlazorApp/Components/MyInventory.razor b/myBlazorApp/myBlazorApp/Components/MyInventory.razor index 6f4ccf3..f4718ae 100644 --- a/myBlazorApp/myBlazorApp/Components/MyInventory.razor +++ b/myBlazorApp/myBlazorApp/Components/MyInventory.razor @@ -1,16 +1,18 @@ -

My Inventory

+ +

My Inventory

-
+
- @foreach (var item in ItemsInventory) - { - + @foreach (var item in ItemsInventory) + { + index++; - } - @for (int i = @index; i<18; i++) - { - - } -
+ } + @for (int i = @index; i < 18; i++) + { + + } +
+
diff --git a/myBlazorApp/myBlazorApp/Components/MyInventory.razor.cs b/myBlazorApp/myBlazorApp/Components/MyInventory.razor.cs index 0d94bf8..ae5b809 100644 --- a/myBlazorApp/myBlazorApp/Components/MyInventory.razor.cs +++ b/myBlazorApp/myBlazorApp/Components/MyInventory.razor.cs @@ -11,11 +11,14 @@ namespace myBlazorApp.Components { [Parameter] public List ItemsInventory { get; set; } = new List(); - public ObservableCollection Actions { get; set; } - public Item CurrentDragItem { get; set; } + [Parameter] public int index { get; set; } = 0; + public ObservableCollection Actions { get; set; } + + public Item CurrentDragItem { get; set; } = new Item(); + public MyInventory() { Actions = new ObservableCollection(); @@ -34,20 +37,6 @@ namespace myBlazorApp.Components [Inject] internal IJSRuntime JavaScriptRuntime { get; set; } - /* - protected override async Task OnAfterRenderAsync(bool firstRender) - { - base.OnAfterRenderAsync(firstRender); - - if (!firstRender) - { - return; - } - itemsInventory = await DataService.List(0, await DataService.Count()); - StateHasChanged(); - } - */ - private void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) { JavaScriptRuntime.InvokeVoidAsync("MyInventory.AddActions", e.NewItems);