mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Some more experimentation with form-like shapes
--HG-- branch : mvc3p1
This commit is contained in:
@@ -55,14 +55,22 @@ namespace Orchard.DevTools.Controllers {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ActionResult FormShapes() {
|
public ActionResult FormShapes() {
|
||||||
var model = Shape.Form(Submit: Shape.FormSubmit(Text: T("Finish Setup")))
|
var model = Shape.Form()
|
||||||
.Fieldsets(Shape.Zone(typeof (Array), Name: "Fieldsets"));
|
.Fieldsets(Shape.Fieldsets(typeof(Array))
|
||||||
|
.Add(Shape.Fieldset(typeof(Array)).Name("site")
|
||||||
|
.Add(Shape.InputText().Name("SiteName").Text(T("Site Name")).Value(T("some default/pre-pop value...").Text))
|
||||||
|
)
|
||||||
|
.Add(Shape.Fieldset(typeof(Array)).Name("admin")
|
||||||
|
.Add(Shape.InputText().Name("AdminUsername").Text(T("Admin Username")))
|
||||||
|
.Add(Shape.InputPassword().Name("AdminPassword").Text(T("Admin Password")))
|
||||||
|
)
|
||||||
|
.Add(Shape.Fieldset(typeof(Array)).Name("actions")
|
||||||
|
.Add(Shape.FormSubmit().Text(T("Finish Setup")))
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
model.Fieldsets.Add(Shape.InputText(Name: "SiteName", Text: T("Site Name"), Value: T("some default/pre-pop value...")));
|
// get at the first input?
|
||||||
model.Fieldsets.Add(Shape.InputText(Name: "AdminUsername", Text: T("Admin Username")));
|
//model.Fieldsets[0].Attributes(new {autofocus = "autofocus"}); // <-- could be applied by some other behavior - need to be able to modify attributes instead of clobbering them like this
|
||||||
model.Fieldsets.Add(Shape.InputPassword(Name: "AdminPassword", Text: T("Admin Password")));
|
|
||||||
|
|
||||||
model.Fieldsets[0].Attributes(new {autofocus = "autofocus"}); // <-- could be applied by some other behavior - need to be able to modify attributes instead of clobbering them like this
|
|
||||||
|
|
||||||
return View(model);
|
return View(model);
|
||||||
}
|
}
|
||||||
|
@@ -1,45 +1,152 @@
|
|||||||
using System.Web;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Web;
|
||||||
using System.Web.Mvc;
|
using System.Web.Mvc;
|
||||||
using ClaySharp;
|
using ClaySharp;
|
||||||
using Orchard.DisplayManagement;
|
using Orchard.DisplayManagement;
|
||||||
|
using Orchard.Localization;
|
||||||
|
|
||||||
namespace Orchard.Mvc.Html {
|
namespace Orchard.Mvc.Html {
|
||||||
public class Shapes : IShapeDriver {
|
public class Shapes : IShapeDriver {
|
||||||
|
public Shapes(IShapeHelperFactory shapeHelperFactory) {
|
||||||
|
New = shapeHelperFactory.CreateHelper();
|
||||||
|
}
|
||||||
|
|
||||||
|
dynamic New { get; set; }
|
||||||
|
|
||||||
public IHtmlString Link(dynamic Display, object Content, string Url, INamedEnumerable<object> Attributes) {
|
public IHtmlString Link(dynamic Display, object Content, string Url, INamedEnumerable<object> Attributes) {
|
||||||
var tagBuilder = new TagBuilder("a") { InnerHtml = Display(Content).ToString() };
|
var a = new TagBuilder("a") {
|
||||||
tagBuilder.MergeAttributes(Attributes.Named);
|
InnerHtml = Display(Content).ToString()
|
||||||
tagBuilder.MergeAttribute("href", Url);
|
};
|
||||||
return Display(new HtmlString(tagBuilder.ToString(TagRenderMode.Normal)));
|
a.MergeAttributes(Attributes.Named);
|
||||||
|
a.MergeAttribute("href", Url);
|
||||||
|
return Display(new HtmlString(a.ToString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public IHtmlString Image(dynamic Display, string Url, INamedEnumerable<object> Attributes) {
|
public IHtmlString Image(dynamic Display, string Url, INamedEnumerable<object> Attributes) {
|
||||||
var tagBuilder = new TagBuilder("img");
|
var img = new TagBuilder("img");
|
||||||
tagBuilder.MergeAttributes(Attributes.Named);
|
img.MergeAttributes(Attributes.Named);
|
||||||
tagBuilder.MergeAttribute("src", Url);
|
img.MergeAttribute("src", Url);
|
||||||
return Display(new HtmlString(tagBuilder.ToString(TagRenderMode.SelfClosing)));
|
return Display(new HtmlString(img.ToString(TagRenderMode.SelfClosing)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public IHtmlString UnorderedList(dynamic Display, dynamic Shape, INamedEnumerable<object> Attributes) {
|
||||||
|
var ul = new TagBuilder("ul") {
|
||||||
|
InnerHtml = Combine(DisplayAll(Display, Shape, New.ListItem())).ToString()
|
||||||
|
};
|
||||||
|
ul.MergeAttributes(Attributes.Named);
|
||||||
|
return Display(new HtmlString(ul.ToString()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public IHtmlString ListItem(dynamic Display, dynamic Shape, INamedEnumerable<object> Attributes) {
|
||||||
|
var li = new TagBuilder("li") {
|
||||||
|
InnerHtml = Display(Shape.Content).ToString()
|
||||||
|
};
|
||||||
|
li.MergeAttributes(Attributes.Named);
|
||||||
|
if (!string.IsNullOrWhiteSpace(Shape.Content.Name as string))
|
||||||
|
li.MergeAttribute("class", Shape.Content.Name);
|
||||||
|
return Display(new HtmlString(li.ToString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
#region form and company
|
#region form and company
|
||||||
|
|
||||||
public IHtmlString Form(dynamic Display, dynamic Shape, object Submit) {
|
public IHtmlString Form(dynamic Display, dynamic Shape, object Submit, INamedEnumerable<object> Attributes) {
|
||||||
return Display(new HtmlString("<form>" + Display(Shape.Fieldsets, Submit).ToString() + "</form>"));
|
var form = new TagBuilder("form") {
|
||||||
|
InnerHtml = Display(Shape.Fieldsets).ToString()
|
||||||
|
};
|
||||||
|
form.MergeAttributes(Attributes.Named);
|
||||||
|
return Display(new HtmlString(form.ToString()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public IHtmlString Fieldsets(dynamic Display, dynamic Shape) {
|
||||||
|
return Display(new HtmlString(Combine(DisplayAll(Display, Shape)).ToString()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public IHtmlString Fieldset(dynamic Display, dynamic Shape, INamedEnumerable<object> Attributes) {
|
||||||
|
var fieldset = new TagBuilder("fieldset");
|
||||||
|
fieldset.MergeAttributes(Attributes.Named);
|
||||||
|
if (!string.IsNullOrWhiteSpace(Shape.Name as string))
|
||||||
|
fieldset.MergeAttribute("class", Shape.Name);
|
||||||
|
|
||||||
|
if (Shape.Count > 1) {
|
||||||
|
Shape.ShapeMetadata.Type = "UnorderedList";
|
||||||
|
fieldset.InnerHtml = Display(Shape).ToString();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
fieldset.InnerHtml = Combine(DisplayAll(Display, Shape)).ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return Display(new HtmlString(fieldset.ToString()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public IHtmlString FormButton(dynamic Display, dynamic Shape, INamedEnumerable<object> Attributes) {
|
||||||
|
var button = new TagBuilder("button") {
|
||||||
|
InnerHtml = Display(Shape.Text).ToString() //not caring about anything other than text at the moment
|
||||||
|
};
|
||||||
|
button.MergeAttributes(Attributes.Named);
|
||||||
|
return Display(new HtmlString(button.ToString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public IHtmlString FormSubmit(dynamic Display, dynamic Shape) {
|
public IHtmlString FormSubmit(dynamic Display, dynamic Shape) {
|
||||||
return Display(new HtmlString("<button type='submit'>" + Shape.Text + "</button>"));
|
Shape.ShapeMetadata.Type = "FormButton";
|
||||||
|
Shape.Attributes(new { type = "submit" });
|
||||||
|
return Display(Shape);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IHtmlString Input(dynamic Display, dynamic Shape, INamedEnumerable<object> Attributes) {
|
||||||
|
var input = new TagBuilder("input");
|
||||||
|
input.MergeAttributes(Attributes.Named);
|
||||||
|
input.MergeAttribute("name", Shape.Name);
|
||||||
|
if (!string.IsNullOrWhiteSpace(Shape.Value as string))
|
||||||
|
input.MergeAttribute("value", Shape.Value);
|
||||||
|
return Display(new HtmlString(input.ToString(TagRenderMode.SelfClosing)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public IHtmlString Label(dynamic Display, dynamic Shape, INamedEnumerable<object> Attributes) {
|
||||||
|
var label = new TagBuilder("label") {
|
||||||
|
InnerHtml = Display(Shape.Text).ToString()
|
||||||
|
};
|
||||||
|
label.MergeAttributes(Attributes.Named);
|
||||||
|
return Display(new HtmlString(label.ToString()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public IHtmlString Text(dynamic Shape) {
|
||||||
|
return new HtmlString(Shape.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public IHtmlString InputPassword(dynamic Display, dynamic Shape) {
|
public IHtmlString InputPassword(dynamic Display, dynamic Shape) {
|
||||||
return Display(new HtmlString("<label>" + Shape.Text + "</label><input type='password' name='" + Shape.Name + "' value='" + (Shape.Value == null ? "" : Shape.Value) + "' />"));
|
Shape.ShapeMetadata.Type = "Input";
|
||||||
|
Shape.Attributes(new { type = "password" });
|
||||||
|
return Display(
|
||||||
|
New.Label().Text(Shape.Text),
|
||||||
|
Shape); //need validation shape
|
||||||
}
|
}
|
||||||
|
|
||||||
public IHtmlString InputText(dynamic Display, dynamic Shape, INamedEnumerable<object> Attributes) {
|
public IHtmlString InputText(dynamic Display, dynamic Shape) {
|
||||||
// not optimal obviously, just testing the waters
|
Shape.ShapeMetadata.Type = "Input";
|
||||||
return Display(new HtmlString("<label>" + Shape.Text + "</label>"
|
Shape.Attributes(new { type = "text" });
|
||||||
+ "<input type='text'" + (Attributes.Named.ContainsKey("autofocus") ? " autofocus" : "" )+ " name='" + Shape.Name
|
return Display(
|
||||||
+ "' value='" + (Shape.Value == null ? "" : Shape.Value) + "' />")); // <- otherwise Shape.Value is "ClaySharp.Clay"
|
New.Label().Text(Shape.Text),
|
||||||
|
Shape); //need validation shape
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
static IHtmlString Combine(IEnumerable<IHtmlString> contents) {
|
||||||
|
return new HtmlString(contents.Aggregate("", (a, b) => a + b));
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerable<IHtmlString> DisplayAll(dynamic Display, dynamic Shape) {
|
||||||
|
foreach (var item in Shape) {
|
||||||
|
yield return Display(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerable<IHtmlString> DisplayAll(dynamic Display, dynamic Shape, dynamic Wrapper) {
|
||||||
|
foreach (var item in Shape) {
|
||||||
|
yield return Display(Wrapper.Content(item));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user