Lucie Bedouret 2 years ago
commit 0c9b033c3d

@ -1,23 +1,33 @@
[
{
"itemName": "null",
"position": 1
"itemName": "Sand",
"position": 1,
"number": 2,
"stackSize": 64
},
{
"itemName": "null",
"position": 2
"itemName": "Grass Block",
"position": 2,
"number": 3,
"stackSize": 64
},
{
"itemName": "null",
"position": 3
"itemName": "Cobblestone",
"position": 3,
"number": 3,
"stackSize": 64
},
{
"itemName": "null",
"position": 4
"itemName": "Sapling",
"position": 4,
"number": 1,
"stackSize": 64
},
{
"itemName": "null",
"position": 5
"itemName": "Dirt",
"position": 5,
"number": 2,
"stackSize": 64
},
{
"itemName": "null",

@ -7,15 +7,30 @@
@ondragend="@OnDragEnd"
@ondragenter="@OnDragEnter"
@ondragleave="@OnDragLeave">
@if (Item != "null")
@if (NoDrop == true)
{
<label id="name">@Item</label>
<label id="number">@Number</label>
<label id="stackSize">@StackSize</label>
@if (!string.IsNullOrWhiteSpace(@Image))
{
<img src="data:image/png;base64, @(Image)" class="img-thumbnail" title="@Item" alt="@Item" style="width: 64px" />
}
else
{
<img src="images/default.png" class="img-thumbnail" title="@Item" alt="@Item" style="max-width: 150px" />
}
}
else
{
<p id="vide">@Localizer["emptyElement"]</p>
@if (Item != "null")
{
<label id="name">@Item</label>
<label id="number">@Number</label>
<label id="stackSize">@StackSize</label>
}
else
{
<p id="vide">@Localizer["emptyElement"]</p>
}
}
</div>

@ -24,6 +24,9 @@ public partial class InventoryItem
[Parameter]
public bool NoDrop { get; set; }
[Parameter]
public string Image { get; set; }
[Inject]
public IStringLocalizer<InventoryItem> Localizer { get; set; }
@ -50,19 +53,22 @@ public partial class InventoryItem
internal void OnDrop()
{
if (NoDrop)
{
return;
}
if (Parent.CurrentDragItem != "null")
{
if(this.Item == Parent.CurrentDragItem)
if(Parent.CurrentDragIndex == 0)
{
this.Number = this.Number + Parent.CurrentDragNumber;
this.Item = Parent.CurrentDragItem;
this.StackSize = Parent.CurrentDragStackSize;
Parent.ItemsInventory[this.Index - 1] = new InventoryListItem(this.Item, this.Index, this.Number, this.StackSize);
}
else if (this.Item == Parent.CurrentDragItem)
{
this.Number = this.Number + Parent.CurrentDragNumber;
Parent.CurrentInventoryItem.Number = 0;
Parent.CurrentInventoryItem.Item = "null";
Parent.CurrentInventoryItem.StackSize = 0;
Parent.ItemsInventory[Parent.CurrentInventoryItem.Index-1] = new InventoryListItem(Parent.CurrentInventoryItem.Item, Parent.CurrentInventoryItem.Index, Parent.CurrentInventoryItem.Number, Parent.CurrentInventoryItem.StackSize);
Parent.ItemsInventory[Parent.CurrentInventoryItem.Index - 1] = new InventoryListItem(Parent.CurrentInventoryItem.Item, Parent.CurrentInventoryItem.Index, Parent.CurrentInventoryItem.Number, Parent.CurrentInventoryItem.StackSize);
Parent.Actions.Add(new InventoryAction { Action = "End", ItemName = Parent.CurrentInventoryItem.Item, Index = Parent.CurrentInventoryItem.Index, Number = Parent.CurrentInventoryItem.Number, StackSize = Parent.CurrentInventoryItem.StackSize });
}
else
@ -70,7 +76,7 @@ public partial class InventoryItem
string tmpItem = this.Item;
int tmpNumber = this.Number;
int tmpStackSize = this.StackSize;
Parent.ItemsInventory[Parent.CurrentDragIndex-1] = new InventoryListItem(tmpItem, Parent.CurrentDragIndex, tmpNumber, tmpStackSize);
Parent.ItemsInventory[Parent.CurrentDragIndex - 1] = new InventoryListItem(tmpItem, Parent.CurrentDragIndex, tmpNumber, tmpStackSize);
this.Item = Parent.CurrentDragItem;
this.Number = Parent.CurrentDragNumber;
this.StackSize = Parent.CurrentDragStackSize;
@ -78,9 +84,10 @@ public partial class InventoryItem
Parent.CurrentInventoryItem.Number = tmpNumber;
Parent.CurrentInventoryItem.StackSize = tmpStackSize;
}
}
Parent.Actions.Add(new InventoryAction { Action = "Drop", ItemName = this.Item, Index = this.Index, Number = this.Number, StackSize = this.StackSize });
}
internal void OnDragEnd()

@ -1,12 +1,44 @@
@using myBlazorApp.Models
@using myBlazorApp.Components
<CascadingValue Value="@this">
<h3>@Localizer["Title"]</h3>
<h3>@Localizer["Inventory"]</h3>
<div class="body">
<div class="inventory">
<div class="inventory-items">
@foreach(InventoryListItem i in ItemsInventory)
{
<InventoryItem Index="@i.position" Item="@i.itemName" Number="@i.number" StackSize="@i.stackSize"/>
}
</div>
</div>
<div id="ItemList" class="align-end">
<h3>@Localizer["List"]</h3>
<input type="text" @bind-value="@SearchText"
@bind-value:event="oninput" placeholder="Search by Name"
@onchange="@inputValue" />
<button type="button" @onclick="@OnPress">@Localizer["Sort"]</button>
<DataGrid TItem="Item"
Data="@choix"
ReadData="@OnReadData"
TotalItems="@totalItem"
PageSize="10"
ShowPager
Responsive>
<div class="inventory-items">
@foreach (InventoryListItem i in ItemsInventory)
{
<InventoryItem Index="@i.position" Item="@i.itemName" Number="@i.number" StackSize="@i.stackSize" />
}
<DataGridColumn Field="@nameof(Item.Id)">
<DisplayTemplate>
<InventoryItem Item="@context.DisplayName" NoDrop="true" Number=1 StackSize="@context.StackSize" Image="@context.ImageBase64"/>
</DisplayTemplate>
</DataGridColumn>
<DataGridColumn TItem="Item" Field="@nameof(Item.DisplayName)" />
</DataGrid>
</div>
</div>
</CascadingValue>

@ -2,17 +2,21 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using Blazorise.DataGrid;
using Blazorise.Extensions;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;
using Microsoft.JSInterop;
using myBlazorApp.Models;
using myBlazorApp.Services;
using Syncfusion.Blazor.Grids;
namespace myBlazorApp.Components
{
public partial class MyInventory
{
[Parameter]
public List<InventoryListItem> ItemsInventory { get; set; }
public List<InventoryListItem> ItemsInventory { get; set; } = new List<InventoryListItem>();
public int index { get; set; } = 0;
@ -24,6 +28,36 @@ namespace myBlazorApp.Components
public int CurrentDragStackSize { get; set; }
public InventoryItem CurrentInventoryItem {get; set;} = new InventoryItem();
private List<Item> items = new List<Item>();
private List<Item> full = new List<Item>();
private List<Item> choix = new List<Item>();
public string SearchText = "";
private int totalItem;
private int currentPage;
private int pageSize;
List<Item> Filtered = new List<Item>();
List<Item> Sorted = new List<Item>();
[Inject]
public IDataService DataService { get; set; }
[Inject]
public IWebHostEnvironment WebHostEnvironment { get; set; }
[Inject]
public NavigationManager NavigationManager { get; set; }
private bool isSorted = false;
public MyInventory()
{
Actions = new ObservableCollection<InventoryAction>();
@ -31,17 +65,9 @@ namespace myBlazorApp.Components
}
[Inject]
public IDataService DataService { get; set; }
[Inject]
public IStringLocalizer<MyInventory> Localizer { get; set; }
[Inject]
public IWebHostEnvironment WebHostEnvironment { get; set; }
[Inject]
public NavigationManager NavigationManager { get; set; }
[Inject]
internal IJSRuntime JavaScriptRuntime { get; set; }
@ -55,6 +81,108 @@ namespace myBlazorApp.Components
await DataService.UpdateInventory(infos);
}
}
private async Task OnReadData(DataGridReadDataEventArgs<Item> e)
{
if (e.CancellationToken.IsCancellationRequested)
{
return;
}
if (!e.CancellationToken.IsCancellationRequested)
{
totalItem = await DataService.Count();
items = await DataService.List(e.Page, e.PageSize);
full = await DataService.List(e.Page, totalItem);
ItemsInventory = await DataService.GetInventoryItems();
currentPage = e.Page;
pageSize = e.PageSize;
if (isSorted == false)
{
choix = items;
}
else
{
choix = SortList();
return;
}
if (SearchText.IsNullOrEmpty())
{
choix = items;
}
else
{
choix = choseList();
return;
}
StateHasChanged();
}
}
private List<Item> choseList()
{
if (SearchText.IsNullOrEmpty())
{
choix = items;
totalItem = full.Count();
NavigationManager.NavigateTo("inventory", false);
}
else
{
if (Filtered.Count() < (currentPage - 1) * 10 + pageSize)
{
pageSize = Filtered.Count() - (currentPage - 1) * 10;
}
choix = Filtered.GetRange((currentPage - 1) * 10, pageSize);
totalItem = Filtered.Count();
}
StateHasChanged();
NavigationManager.NavigateTo("inventory", false);
return choix;
}
private void inputValue()
{
Filtered = full.Where(
itm => itm.DisplayName.ToLower().Contains(SearchText.ToLower())).ToList();
choseList();
}
private void OnPress()
{
if (isSorted == true)
{
isSorted = false;
}
else isSorted = true;
SortList();
}
private List<Item> SortList()
{
if (isSorted == false)
{
choix = items;
NavigationManager.NavigateTo("inventory", true);
}
else
{
if (Sorted.IsNullOrEmpty())
{
Sorted = full;
}
Sorted.Sort((x, y) => string.Compare(x.DisplayName, y.DisplayName));
if (Sorted.Count() < (currentPage - 1) * 10 + pageSize)
{
pageSize = Sorted.Count() - (currentPage - 1) * 10;
}
choix = Sorted.GetRange((currentPage - 1) * 10, pageSize);
}
return choix;
}
}
}

@ -1,5 +1,23 @@
.inventory-items {
.body {
display: flex;
width: 100%;
}
.inventory {
align-items: flex-start;
justify-content: start;
width: 45%;
margin-right: 5%;
}
.ItemList {
align-content: flex-end;
justify-content: start;
width: 45%;
}
.inventory-items {
grid-template-columns: repeat(6,minmax(0,1fr));
gap: 5px;
display: grid;
}
}

@ -4,44 +4,6 @@
@using myBlazorApp.Models;
@using Syncfusion.Blazor.Grids;
<div class="body">
<div class="inventory">
<MyInventory ItemsInventory="@itemsInv"/>
</div>
<div id="ItemList">
<div id="ItemList" class="align-end">
<h3>List of Items</h3>
<input type="text" @bind-value="@SearchText"
@bind-value:event="oninput" placeholder="Search by Name"
@onchange="@inputValue"/>
<button type="button" @onclick="@OnPress">Sort</button>
<DataGrid TItem="Item"
Data="@choix"
ReadData="@OnReadData"
TotalItems="@totalItem"
PageSize="10"
ShowPager
Responsive>
<DataGridColumn Field="@nameof(Item.Id)">
<DisplayTemplate>
@if (!string.IsNullOrWhiteSpace(context.ImageBase64))
{
<img src="data:image/png;base64, @(context.ImageBase64)" class="img-thumbnail" title="@context.DisplayName" alt="@context.DisplayName" style="width: 50px" />
}
else
{
<img src="images/default.png" class="img-thumbnail" title="@context.DisplayName" alt="@context.DisplayName" style="max-width: 150px" />
}
</DisplayTemplate>
</DataGridColumn>
<DataGridColumn TItem="Item" Field="@nameof(Item.DisplayName)" />
</DataGrid>
</div>
</div>
</div>
<MyInventory/>
</div>

@ -145,6 +145,10 @@ namespace myBlazorApp.Pages
Sorted = full;
}
Sorted.Sort((x, y) => string.Compare(x.DisplayName, y.DisplayName));
if (Sorted.Count() < (currentPage - 1) * 10 + pageSize)
{
pageSize = Sorted.Count() - (currentPage - 1) * 10;
}
choix = Sorted.GetRange((currentPage - 1) * 10, pageSize);
}
return choix;

@ -1,17 +1 @@
.body {
display: flex;
width: 100%;
}
.inventory {
align-items: flex-start;
justify-content: start;
width: 45%;
margin-right: 5%;
}
.ItemList {
align-content: flex-end;
justify-content: end;
width: 45%;
}


@ -60,9 +60,27 @@ namespace myBlazorApp.Resources {
/// <summary>
/// Looks up a localized string similar to My inventory.
/// </summary>
internal static string Title {
internal static string Inventory {
get {
return ResourceManager.GetString("Title", resourceCulture);
return ResourceManager.GetString("Inventory", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Available items.
/// </summary>
internal static string List {
get {
return ResourceManager.GetString("List", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to SORT.
/// </summary>
internal static string Sort {
get {
return ResourceManager.GetString("Sort", resourceCulture);
}
}
}

@ -117,8 +117,16 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Title" xml:space="preserve">
<data name="Inventory" xml:space="preserve">
<value>Mon inventaire</value>
<comment/>
</data>
<data name="List" xml:space="preserve">
<value>Objets Disponibles</value>
<comment/>
</data>
<data name="Sort" xml:space="preserve">
<value>TRIER</value>
<comment/>
</data>
</root>

@ -117,8 +117,16 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Title" xml:space="preserve">
<data name="Inventory" xml:space="preserve">
<value>My inventory</value>
<comment/>
</data>
<data name="List" xml:space="preserve">
<value>Available items</value>
<comment/>
</data>
<data name="Sort" xml:space="preserve">
<value>SORT</value>
<comment/>
</data>
</root>
Loading…
Cancel
Save