Code cleanup (#8)
* * style names * bug touch * bug last release * * More cleanup Co-authored-by: Santiago Cattaneo <santiago@rd-its.com>
This commit is contained in:
parent
32528b461b
commit
3d7ba328c0
4 changed files with 27 additions and 41 deletions
|
@ -1,4 +1,4 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
|
||||
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
|
@ -15,10 +15,4 @@
|
|||
<ProjectReference Include="..\BlazorReorderList\BlazorReorderList.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Update="Pages\Callbacks.razor">
|
||||
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -4,9 +4,16 @@
|
|||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<PackageId>Reorder</PackageId>
|
||||
<Version>1.0.0</Version>
|
||||
<Authors>Santiago Cattaneo</Authors>
|
||||
<projectUrl>https://github.com/elgransan/BlazorReorder</projectUrl>
|
||||
<description>Drag and Drop Reorder List for Blazor, with the less JS interop possible</description>
|
||||
<releaseNotes>First version</releaseNotes>
|
||||
<copyright>2022</copyright>
|
||||
<tags>Blazor RCL</tags>
|
||||
</PropertyGroup>
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<SupportedPlatform Include="browser" />
|
||||
</ItemGroup>
|
||||
|
|
|
@ -38,7 +38,6 @@
|
|||
[Parameter] public bool Disabled { get; set; } = false;
|
||||
[Parameter] public bool DisableDrop { get; set; } = false;
|
||||
[Parameter] public bool DisableDrag { get; set; } = false;
|
||||
[Parameter] public bool WithShadow { get; set; } = true;
|
||||
[Parameter] public ReorderService<TItem>? ReorderService { get; set; }
|
||||
|
||||
private bool shouldRender = true; // cancel re-rendering
|
||||
|
@ -49,7 +48,6 @@
|
|||
point ghostTrans = new point(0, 0);
|
||||
point clickPosition = new point(0, 0);
|
||||
int elemWidth = 0;
|
||||
int newElemIndex = -1;
|
||||
private IReorderService<TItem>? rs;
|
||||
|
||||
protected override void OnInitialized()
|
||||
|
@ -81,7 +79,7 @@
|
|||
if (provider != null)
|
||||
rs = (IReorderService<TItem>)provider;
|
||||
if (rs == null && ReorderService != null) rs = ReorderService.InitService(JS);
|
||||
if (rs == null) return;
|
||||
if (rs == null) throw new Exception("Blazor Reorder: Service not initialized");
|
||||
dotNetHelper = DotNetObjectReference.Create(this);
|
||||
await rs.initEvents(dotNetHelper);
|
||||
StateHasChanged();
|
||||
|
@ -124,15 +122,6 @@
|
|||
{
|
||||
await OnFinish.InvokeAsync(rs.selected);
|
||||
rs.Reset();
|
||||
|
||||
// flip
|
||||
if (!WithShadow)
|
||||
{
|
||||
var item = Items[rs.elemIndex];
|
||||
shouldRender = false;
|
||||
Items.RemoveAt(rs.elemIndex);
|
||||
Items.Insert(newElemIndex, item);
|
||||
}
|
||||
shouldRender = true;
|
||||
}
|
||||
StateHasChanged();
|
||||
|
@ -148,30 +137,26 @@
|
|||
ghostTrans = new point(pos.x - rs.elemClickPosition.x, pos.y - rs.elemClickPosition.y);
|
||||
|
||||
// check if current drag item is over another item and swap places
|
||||
for (var b = 0; b < itemElem.Count; ++b)
|
||||
for (var i = 0; i < itemElem.Count; i++)
|
||||
{
|
||||
if (Disabled || DisableDrop) break;
|
||||
var subItem = itemElem[b];
|
||||
if (rs.originItems == Items && b == rs.elemIndex) continue;
|
||||
var subItem = itemElem[i];
|
||||
if (rs.originItems == Items && i == rs.elemIndex) continue;
|
||||
|
||||
// If is on top of an element or if top top of a empty list -> confirm drag
|
||||
if (await isOnTop(subItem, pos) ||
|
||||
Items.Count == 0 && await isOnTop((ElementReference) reorder, pos))
|
||||
{
|
||||
// reorder
|
||||
newElemIndex = b;
|
||||
if (WithShadow)
|
||||
{
|
||||
if (!rs.isCopy())
|
||||
rs.originItems.RemoveAt(rs.elemIndex);
|
||||
Items.Insert(newElemIndex, rs.selected);
|
||||
rs.elemIndex = newElemIndex;
|
||||
Items.Insert(i, rs.selected);
|
||||
rs.elemIndex = i;
|
||||
if (rs.originItems != Items) rs.originItems = Items;
|
||||
await OnChange.InvokeAsync(rs.selected);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
shouldRender = true;
|
||||
StateHasChanged();
|
||||
|
|
|
@ -60,49 +60,49 @@ public class ReorderService<TItem> : IReorderService<TItem>
|
|||
|
||||
public async ValueTask initEvents(DotNetObjectReference<Reorder<TItem>> dotNetInstance)
|
||||
{
|
||||
if (_moduleTask == null) throw new Exception("Reorder Component: JS module not initializated");
|
||||
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<Reorder<TItem>> dotNetInstance)
|
||||
{
|
||||
if (_moduleTask == null) throw new Exception("Reorder Component: JS module not initializated");
|
||||
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<int> getWidth(ElementReference el)
|
||||
{
|
||||
if (_moduleTask == null) throw new Exception("Reorder Component: JS module not initializated");
|
||||
if (_moduleTask == null) throw new Exception("Blazor Reorder: JS module not initializated");
|
||||
var module = await _moduleTask.Value;
|
||||
return await module.InvokeAsync<int>("getWidth", el);
|
||||
}
|
||||
|
||||
public async ValueTask<point> getPosition(ElementReference el)
|
||||
{
|
||||
if (_moduleTask == null) throw new Exception("Reorder Component: JS module not initializated");
|
||||
if (_moduleTask == null) throw new Exception("Blazor Reorder: JS module not initializated");
|
||||
var module = await _moduleTask.Value;
|
||||
return await module.InvokeAsync<point>("getPosition", el);
|
||||
}
|
||||
|
||||
public async ValueTask<point> getPoint(double pageX, double pageY, double clientX, double clientY)
|
||||
{
|
||||
if (_moduleTask == null) throw new Exception("Reorder Component: JS module not initializated");
|
||||
if (_moduleTask == null) throw new Exception("Blazor Reorder: JS module not initializated");
|
||||
var module = await _moduleTask.Value;
|
||||
return await module.InvokeAsync<point>("getPoint", pageX, pageY, clientX, clientY);
|
||||
}
|
||||
|
||||
public async ValueTask<clientRect> getClientRect(ElementReference el)
|
||||
{
|
||||
if (_moduleTask == null) throw new Exception("Reorder Component: JS module not initializated");
|
||||
if (_moduleTask == null) throw new Exception("Blazor Reorder: JS module not initializated");
|
||||
var module = await _moduleTask.Value;
|
||||
return await module.InvokeAsync<clientRect>("getClientRect", el);
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_moduleTask == null) throw new Exception("Reorder Component: JS module not initializated");
|
||||
if (_moduleTask == null) throw new Exception("Blazor Reorder: JS module not initializated");
|
||||
if (_moduleTask.IsValueCreated)
|
||||
{
|
||||
var module = await _moduleTask.Value;
|
||||
|
|
Loading…
Reference in a new issue