This commit is contained in:
Sipke Schoorstra 2018-11-18 22:46:52 +01:00
parent 4e829ad27e
commit 430e62628c
7 changed files with 149 additions and 0 deletions

View file

@ -0,0 +1,20 @@
using Flowsharp.Models;
namespace Flowsharp.Web.Abstractions.Drivers
{
/// <summary>
/// Base class for activity drivers.
/// </summary>
public abstract class TypedActivityDisplayDriver<TActivity> : ActivityDisplayDriver where TActivity : class, IActivity
{
public override bool CanHandleModel(IActivity model) => model.Name == typeof(TActivity).Name;
}
/// <summary>
/// Base class for activity drivers using a strongly typed view model.
/// </summary>
public abstract class TypedActivityDisplayDriver<TActivity, TEditViewModel> : ActivityDisplayDriver<TEditViewModel> where TActivity : class, IActivity where TEditViewModel : class, new()
{
public override bool CanHandleModel(IActivity model) => model.Name == typeof(TActivity).Name;
}
}

View file

@ -0,0 +1,9 @@
using Flowsharp.Models;
using OrchardCore.DisplayManagement;
namespace Flowsharp.Web.Abstractions.Services
{
public interface IActivityDisplayManager : IDisplayManager<IActivity>
{
}
}

View file

@ -0,0 +1,21 @@
using Flowsharp.Models;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using OrchardCore.DisplayManagement.Views;
namespace Flowsharp.Web.Abstractions.ViewModels
{
public class ActivityViewModel : ShapeViewModel
{
public ActivityViewModel()
{
}
public ActivityViewModel(IActivity activity)
{
Activity = activity;
}
[BindNever]
public IActivity Activity { get; set; }
}
}

View file

@ -0,0 +1,40 @@
///<reference path='../../../node_modules/@types/jquery/index.d.ts' />
///<reference path='../../../node_modules/@types/bootstrap/index.d.ts' />
namespace Flowsharp {
export class ActivityEditor {
public show = () => this.containerElement.modal('show');
public hide = () => this.containerElement.modal('hide');
public display = (activityName: string) => $.ajax({
type: 'POST',
url: `/activity/display/${activityName}`,
data: {},
contentType: 'application/json',
dataType: 'json'
}).done(this.displayActivity);
private baseUrl: string;
private containerElement: JQuery<HTMLElement>;
private titleElement: JQuery<HTMLElement>;
private contentElement: JQuery<HTMLElement>;
private displayActivity = (data: any) => {
const html = <string>data;
this.contentElement.html(html);
};
constructor(container: HTMLElement) {
this.containerElement = $(container);
this.titleElement = this.containerElement.find('.modal-title');
this.contentElement = this.containerElement.find('.modal-body');
this.baseUrl = this.containerElement.data('base-url');
}
public get title(): string {
return this.titleElement.text();
}
public set title(value: string) {
this.titleElement.text(value);
}
}
}

View file

@ -0,0 +1,32 @@
using System.Threading;
using System.Threading.Tasks;
using Flowsharp.Models;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using OrchardCore.DisplayManagement;
using OrchardCore.DisplayManagement.ModelBinding;
namespace Flowsharp.Web.ViewComponents.Controllers
{
[Route("[controller]")]
[IgnoreAntiforgeryToken]
public class ActivityController : Controller, IUpdateModel
{
private readonly IDisplayManager<Flowsharp.Models.ActivityDescriptor> displayManager;
private readonly IActivityLibrary activityLibrary;
public ActivityController(IDisplayManager<Flowsharp.Models.ActivityDescriptor> displayManager, IActivityLibrary activityLibrary)
{
this.displayManager = displayManager;
this.activityLibrary = activityLibrary;
}
[HttpPost("display/{activityName}")]
public async Task<IActionResult> Display(string activityName, [FromBody] JToken json, CancellationToken cancellationToken)
{
var activity = await activityLibrary.GetActivityByNameAsync(activityName, cancellationToken);
var editorShape = await displayManager.BuildEditorAsync(activity, this, true);
return View(editorShape);
}
}
}

View file

@ -0,0 +1,25 @@
using System.Collections.Generic;
using Flowsharp.Models;
using Flowsharp.Web.Abstractions.Services;
using Microsoft.Extensions.Logging;
using OrchardCore.DisplayManagement;
using OrchardCore.DisplayManagement.Descriptors;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.DisplayManagement.Layout;
using OrchardCore.DisplayManagement.Theming;
namespace Flowsharp.Web.ViewComponents.Services
{
public class ActivityDisplayManager : DisplayManager<IActivity>, IActivityDisplayManager
{
public ActivityDisplayManager(
IEnumerable<IDisplayDriver<IActivity>> drivers,
IShapeTableManager shapeTableManager,
IShapeFactory shapeFactory,
IThemeManager themeManager,
ILogger<DisplayManager<IActivity>> logger,
ILayoutAccessor layoutAccessor) : base(drivers, shapeTableManager, shapeFactory, themeManager, logger, layoutAccessor)
{
}
}
}

View file

@ -0,0 +1,2 @@
@model dynamic
@await DisplayAsync(Model)