Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow dynamic drop action selection #107

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions Plk.Blazor.DragDrop.Demo/Pages/Accepts.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@page "/accepts"
@page "/accepts"
@using Plk.Blazor.DragDrop.Demo.Components
@using Plk.Blazor.DragDrop.Demo.Data
@using Plk.Blazor.DragDrop.DropActions

<div class="bg-success">You can accept or deny items.</div>

Expand All @@ -10,9 +11,14 @@


<Dropzone Items="MyFirstList">
<div style="border: 2px solid blue">
@context.Titel
</div>
<DropActions>
<MoveDropAction TItem="TodoItem" />
</DropActions>
<ChildContent>
<div style="border: 2px solid blue">
@context.Titel
</div>
</ChildContent>
</Dropzone>


Expand All @@ -22,9 +28,14 @@


<Dropzone Items="MySecondList" Accepts='(d, t)=>d.Titel.Contains("fun")' TItem="TodoItem" OnItemDropRejected="@((i)=>lastdropped = i)">
<div style="border: 2px solid black">
@context.Titel
</div>
<DropActions>
<MoveDropAction TItem="TodoItem" />
</DropActions>
<ChildContent>
<div style="border: 2px solid black">
@context.Titel
</div>
</ChildContent>
</Dropzone>

<hr />
Expand Down
17 changes: 14 additions & 3 deletions Plk.Blazor.DragDrop.Demo/Pages/AllowsDrag.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@page "/allowsdrag"
@page "/allowsdrag"
@using Plk.Blazor.DragDrop.Demo.Components
@using Plk.Blazor.DragDrop.Demo.Data
@using Plk.Blazor.DragDrop.DropActions

<div class="bg-success">You can drag a new item into and around a list, but not the other items.</div>

Expand All @@ -9,15 +10,25 @@
<div class="bg-light">First Dropzone:</div>

<Dropzone Items="MyFirstList" TItem="TodoItem" OnItemDrop="@((i)=>lastdropped = i)" AllowsDrag='(d)=>d.Titel == "Item 1"' InstantReplace="false">
<Todo Item="@context"></Todo>
<DropActions>
<MoveDropAction TItem="TodoItem" />
</DropActions>
<ChildContent>
<Todo Item="@context"></Todo>
</ChildContent>
</Dropzone>

<hr />

<div class="bg-light">Second Dropzone:</div>

<Dropzone Items="MySecondList" TItem="TodoItem" OnItemDrop="@((i)=>lastdropped = i)" AllowsDrag='(d)=>d.Titel == "Item 1"' InstantReplace="false">
<Todo Item="@context"></Todo>
<DropActions>
<MoveDropAction TItem="TodoItem" />
</DropActions>
<ChildContent>
<Todo Item="@context"></Todo>
</ChildContent>
</Dropzone>

<hr />
Expand Down
14 changes: 10 additions & 4 deletions Plk.Blazor.DragDrop.Demo/Pages/DisplayGrid.razor
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
@page "/displaygrid"
@page "/displaygrid"
@using Plk.Blazor.DragDrop.DropActions

<div class="bg-success">You can use grid display</div>

<hr />

<div class="grid-container">
<Dropzone Items="Items">
<div>
<div class="grid-item">@context</div>
</div>
<DropActions>
<MoveDropAction TItem="string" />
</DropActions>
<ChildContent>
<div>
<div class="grid-item">@context</div>
</div>
</ChildContent>
</Dropzone>
</div>

Expand Down
10 changes: 8 additions & 2 deletions Plk.Blazor.DragDrop.Demo/Pages/Flex.razor
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
@page "/flex"
@page "/flex"
@using Plk.Blazor.DragDrop.DropActions

<div class="bg-success">You can use flex display</div>

<hr />

<div class="plk-flex">
<Dropzone Class="flex-container" Items="Items" ItemWrapperClass="@(item => "flex-item")">
<div>@context</div>
<DropActions>
<MoveDropAction TItem="string" />
</DropActions>
<ChildContent>
<div>@context</div>
</ChildContent>
</Dropzone>

</div>
Expand Down
10 changes: 8 additions & 2 deletions Plk.Blazor.DragDrop.Demo/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@page "/"
@page "/"
@using Plk.Blazor.DragDrop.Demo.Components
@using Plk.Blazor.DragDrop.Demo.Data
@using Plk.Blazor.DragDrop.DropActions

<div id="main" class="container-fluid">

Expand All @@ -10,7 +11,12 @@
<hr />

<Dropzone Items="MyToDoList">
<Todo OnRemoveClick="(item)=>MyToDoList.Remove(item)" Item="@context"></Todo>
<DropActions>
<MoveDropAction TItem="TodoItem" />
</DropActions>
<ChildContent>
<Todo OnRemoveClick="(item)=>MyToDoList.Remove(item)" Item="@context"></Todo>
</ChildContent>
</Dropzone>

</div>
Expand Down
25 changes: 18 additions & 7 deletions Plk.Blazor.DragDrop.Demo/Pages/MaxItems.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@page "/maxitems"
@page "/maxitems"
@using Plk.Blazor.DragDrop.Demo.Components
@using Plk.Blazor.DragDrop.Demo.Data
@using Plk.Blazor.DragDrop.DropActions

<div class="bg-success">You can set an item limit on a dropzone.</div>

Expand All @@ -9,9 +10,14 @@
<div class="bg-light">First Dropzone:</div>

<Dropzone Items="MyFirstList">
<div class="mt-2" style="border: 2px solid blue">
@context.Titel
</div>
<DropActions>
<MoveDropAction TItem="TodoItem" />
</DropActions>
<ChildContent>
<div class="mt-2" style="border: 2px solid blue">
@context.Titel
</div>
</ChildContent>
</Dropzone>

<hr />
Expand All @@ -20,9 +26,14 @@


<Dropzone Items="MySecondList" MaxItems="2" TItem="TodoItem" OnItemDropRejectedByMaxItemLimit="@((i)=>lastRejected = i)">
<div style="border: 2px solid black">
@context.Titel
</div>
<DropActions>
<MoveDropAction TItem="TodoItem" />
</DropActions>
<ChildContent>
<div style="border: 2px solid black">
@context.Titel
</div>
</ChildContent>
</Dropzone>


Expand Down
2 changes: 1 addition & 1 deletion Plk.Blazor.DragDrop.Demo/Pages/ReplacedItem.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@page "/replacedItem"
@page "/replacedItem"

<div class="row">
<b class="col-12">
Expand Down
43 changes: 29 additions & 14 deletions Plk.Blazor.DragDrop.Demo/Pages/TwoZones.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@page "/twozones"
@page "/twozones"
@using Plk.Blazor.DragDrop.Demo.Components
@using Plk.Blazor.DragDrop.Demo.Data
@using Plk.Blazor.DragDrop.DropActions

<div class="bg-success">You can drag items from one dropzone to another dropzone.</div>

Expand All @@ -9,38 +10,53 @@
<div class="bg-light">First Dropzone:</div>

<Dropzone Items="MyFirstList" TItem="TodoItem" OnItemDrop="@((i)=>lastdropped = i)" AllowsDrag='(d)=>d.Titel == "Item 1"'>
<div style="border: 2px solid blue">
@context.Titel
</div>
<DropActions>
<MoveDropAction TItem="TodoItem" />
</DropActions>
<ChildContent>
<div style="border: 2px solid blue">
@context.Titel
</div>
</ChildContent>
</Dropzone>

<hr />

<div class="bg-light">Second Dropzone:</div>

<Dropzone Items="MySecondList" TItem="TodoItem" OnItemDrop="@((i)=>lastdropped = i)" AllowsDrag='(d)=>d.Titel == "Item 1"'>
<div style="border: 2px solid black">
@context.Titel
</div>
<DropActions>
<MoveDropAction TItem="TodoItem" />
</DropActions>
<ChildContent>
<div style="border: 2px solid black">
@context.Titel
</div>
</ChildContent>
</Dropzone>

<hr />

<div class="bg-light">Third Dropzone (copy items):</div>

<Dropzone CopyItem="(item)=> { return new TodoItem() {Titel = item.Titel}; }" Items="MyThirdList" TItem="TodoItem" OnItemDrop="@((i)=>lastdropped = i)">
<div style="border: 2px solid black">
@context.Titel
</div>
<Dropzone Items="MyThirdList" TItem="TodoItem" OnItemDrop="@((i)=>lastdropped = i)">
<DropActions>
<CopyDropAction CopyItem="(TodoItem item) => { return new TodoItem() { Titel = item.Titel }; }" />
</DropActions>
<ChildContent>
<div style="border: 2px solid black">
@context.Titel
</div>
</ChildContent>
</Dropzone>

<hr />

<div>Last Dropped Item: @lastdropped?.Titel</div>


@code {

@code {
private TodoItem lastdropped { get; set; }

public List<TodoItem> MyFirstList = new List<TodoItem>()
Expand All @@ -64,5 +80,4 @@
public List<TodoItem> MyThirdList = new List<TodoItem>()
{
};

}
2 changes: 1 addition & 1 deletion Plk.Blazor.DragDrop.Demo/Plk.Blazor.DragDrop.Demo.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<UserSecretsId>f648770a-26a2-462b-87a0-17835ea631cb</UserSecretsId>
</PropertyGroup>

Expand Down
16 changes: 11 additions & 5 deletions Plk.Blazor.DragDrop.Test/Dropzone_Copy.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@inherits TestComponentBase
@using Plk.Blazor.DragDrop.DropActions
@inherits TestComponentBase

@code{
void RegisterService(Fixture fixture)
Expand Down Expand Up @@ -68,15 +69,20 @@
@context.Name
</Dropzone>
<hr />
<Dropzone TItem="MyItem" CopyItem="(item)=> { return new MyItem() { Name = item.Name }; }" Id="SecondDropzone" Items="Items2">
@context.Name
<Dropzone Id="SecondDropzone" Items="Items2">
<DropActions>
<CopyDropAction CopyItem="(MyItem item)=> { return new MyItem() { Name = item.Name }; }" />
</DropActions>
<ChildContent>
@context.Name
</ChildContent>
</Dropzone>
</ComponentUnderTest>


@code
{
void DropzoneCopyTest(Fixture fixture)
{
void DropzoneCopyTest(Fixture fixture)
{
var cut = fixture.GetComponentUnderTest();

Expand Down
2 changes: 1 addition & 1 deletion Plk.Blazor.DragDrop.Test/Plk.Blazor.DragDrop.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<RazorLangVersion>3.0</RazorLangVersion>
</PropertyGroup>

Expand Down
26 changes: 26 additions & 0 deletions Plk.Blazor.DragDrop/DropActions/CopyDropAction.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@typeparam TItem

@inherits DropActionBase<TItem>

@code
{
[Parameter]
public Func<TItem, TItem> CopyItem { get; set; }

public override void Execute(DropActionContext<TItem> context)
{
TItem copiedItem = CopyItem != null ? CopyItem(context.ActiveItem) : context.ActiveItem;

if (copiedItem == null)
return;

if (context.TargetItemIndex.HasValue)
{
context.TargetItems.Insert(context.TargetItemIndex.Value, copiedItem);
}
else
{
context.TargetItems.Add(copiedItem);
}
}
}
25 changes: 25 additions & 0 deletions Plk.Blazor.DragDrop/DropActions/DropActionBase.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@typeparam TItem

@implements IDisposable

@code
{
[CascadingParameter]
public Dropzone<TItem> Dropzone { get; set; }

protected override void OnInitialized()
{
if (Dropzone == null)
throw new ArgumentNullException(nameof(Dropzone), "DropAction must exist within a Dropzone");

Dropzone.RegisterDropAction(this);

base.OnInitialized();
}

public void Dispose()
{
if (Dropzone != null)
Dropzone.DeregisterDropAction(this);
}
}
13 changes: 13 additions & 0 deletions Plk.Blazor.DragDrop/DropActions/DropActionBase.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using Microsoft.AspNetCore.Components;

namespace Plk.Blazor.DragDrop.DropActions
{
public abstract partial class DropActionBase<TItem> : IDropAction<TItem>
{
[Parameter]
public Func<DropActionContext<TItem>, bool> IsSuitable { get; set; } = (i) => true;

public abstract void Execute(DropActionContext<TItem> context);
}
}
Loading