Put samples on the web site

This commit is contained in:
Frank A. Krueger 2017-11-09 22:35:47 -08:00
parent 2e8a605d13
commit de9102f38a
10 changed files with 99 additions and 26 deletions

View File

@ -18,5 +18,6 @@
<ItemGroup>
<ProjectReference Include="..\..\Ooui.AspNetCore\Ooui.AspNetCore.csproj" />
<ProjectReference Include="..\..\Ooui\Ooui.csproj" />
<ProjectReference Include="..\..\Samples\Samples.csproj" />
</ItemGroup>
</Project>

View File

@ -29,5 +29,29 @@ namespace AspNetCoreMvc.Controllers
div.AppendChild (btn);
return new ElementResult (div);
}
static readonly Lazy<List<Samples.ISample>> lazySamples =
new Lazy<List<Samples.ISample>> ((() => {
var sampleType = typeof (Samples.ISample);
var asm = sampleType.Assembly;
var sampleTypes = asm.GetTypes ().Where (x => x.Name.EndsWith ("Sample", StringComparison.Ordinal) && x != sampleType);
var samples = from t in sampleTypes let s = Activator.CreateInstance (t) as Samples.ISample where s != null select s;
return samples.ToList ();
}), true);
public static List<Samples.ISample> Samples => lazySamples.Value;
[Route("/Samples/Run/{name}")]
public IActionResult Run (string name)
{
if (string.IsNullOrWhiteSpace (name) || name.Length > 32)
return BadRequest ();
var s = Samples.FirstOrDefault (x => x.Title == name);
if (s == null)
return NotFound ();
return new ElementResult (s.CreateElement ());
}
}
}

View File

@ -16,8 +16,9 @@
<div class="col-md-3">
<h3>Samples</h3>
<ul>
<li><a asp-area="" asp-controller="Samples" asp-action="Clicker">HTML Clicker</a></li>
<li><a asp-area="" asp-controller="Samples" asp-action="XamarinFormsClicker">Xamarin Forms Clicker</a></li>
@foreach (var s in SamplesController.Samples) {
<li><a asp-area="" asp-controller="Samples" asp-action="Run" asp-route-name="@s.Title">@s.Title</a></li>
}
</ul>
</div>
<div class="col-md-3">

View File

@ -1,3 +1,4 @@
@using AspNetCoreMvc
@using AspNetCoreMvc.Models
@using AspNetCoreMvc.Controllers
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

View File

@ -3,8 +3,10 @@ using Ooui;
namespace Samples
{
public class ButtonSample
public class ButtonSample : ISample
{
public string Title => "Button that count clicks";
Button MakeButton ()
{
var button = new Button ("Click me!");
@ -23,6 +25,11 @@ namespace Samples
UI.Publish ("/shared-button", b);
UI.Publish ("/button", MakeButton);
}
public Element CreateElement ()
{
return MakeButton ();
}
}
}

View File

@ -5,9 +5,16 @@ using Ooui;
namespace Samples
{
public class DrawSample
public class DrawSample : ISample
{
public string Title => "Collaborative Drawing";
public void Publish ()
{
UI.Publish ("/draw", CreateElement ());
}
public Element CreateElement ()
{
var heading = new Heading ("Draw");
var subtitle = new Paragraph ("Click to draw a collaborative masterpiece");
@ -41,9 +48,7 @@ namespace Samples
app.AppendChild (subtitle);
app.AppendChild (canvas);
app.AppendChild (clearbtn);
UI.Publish ("/draw", app);
return app;
}
}
}

View File

@ -6,24 +6,13 @@ using Ooui;
namespace Samples
{
public class FilesSample
public class FilesSample : ISample
{
public string Title => "Upload files";
public void Publish ()
{
var heading = new Heading ("Upload Files");
var subtitle = new Paragraph ("Upload files to the app");
var uploadForm = new Form ();
uploadForm.Action = "/files/upload";
uploadForm.Method = "POST";
uploadForm.EncodingType = "multipart/form-data";
uploadForm.AppendChild (new Input (InputType.File) { Name = "file" });
uploadForm.AppendChild (new Input (InputType.Submit) { Value = "Upload" });
var app = new Div ();
app.AppendChild (heading);
app.AppendChild (subtitle);
app.AppendChild (uploadForm);
var app = CreateElement ();
UI.Publish ("/files", app);
@ -133,5 +122,25 @@ namespace Samples
}
return -1;
}
}
public Element CreateElement ()
{
var heading = new Heading ("Upload Files");
var subtitle = new Paragraph ("Upload files to the app");
var uploadForm = new Form ();
uploadForm.Action = "/files/upload";
uploadForm.Method = "POST";
uploadForm.EncodingType = "multipart/form-data";
uploadForm.AppendChild (new Input (InputType.File) { Name = "file" });
uploadForm.AppendChild (new Input (InputType.Submit) { Value = "Upload" });
var app = new Div ();
app.AppendChild (heading);
app.AppendChild (subtitle);
app.AppendChild (uploadForm);
return app;
}
}
}

11
Samples/ISample.cs Normal file
View File

@ -0,0 +1,11 @@
using System;
using Ooui;
namespace Samples
{
public interface ISample
{
string Title { get; }
Element CreateElement ();
}
}

View File

@ -5,8 +5,10 @@ using Ooui;
namespace Samples
{
public class TodoSample
public class TodoSample : ISample
{
public string Title => "Global TODO list";
List items = new List () {
ClassName = "list-group",
};
@ -106,6 +108,11 @@ namespace Samples
UI.Publish ("/todo", MakeTodo);
}
public Element CreateElement ()
{
return MakeTodo ();
}
}
}

View File

@ -4,8 +4,10 @@ using Ooui.Forms;
namespace Samples
{
public class XamarinFormsSample
public class XamarinFormsSample : ISample
{
public string Title => "Xamarin.Forms Button Counter";
Page MakePage ()
{
Forms.Init ();
@ -46,5 +48,10 @@ namespace Samples
Ooui.UI.Publish ("/xamarin-forms", () => MakePage ().CreateElement ());
}
}
public Ooui.Element CreateElement ()
{
return MakePage ().CreateElement ();
}
}
}