mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 11:44:58 +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() {
|
||||
var model = Shape.Form(Submit: Shape.FormSubmit(Text: T("Finish Setup")))
|
||||
.Fieldsets(Shape.Zone(typeof (Array), Name: "Fieldsets"));
|
||||
var model = Shape.Form()
|
||||
.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...")));
|
||||
model.Fieldsets.Add(Shape.InputText(Name: "AdminUsername", Text: T("Admin Username")));
|
||||
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
|
||||
// get at the first input?
|
||||
//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);
|
||||
}
|
||||
|
@@ -1,45 +1,152 @@
|
||||
using System.Web;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using ClaySharp;
|
||||
using Orchard.DisplayManagement;
|
||||
using Orchard.Localization;
|
||||
|
||||
namespace Orchard.Mvc.Html {
|
||||
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) {
|
||||
var tagBuilder = new TagBuilder("a") { InnerHtml = Display(Content).ToString() };
|
||||
tagBuilder.MergeAttributes(Attributes.Named);
|
||||
tagBuilder.MergeAttribute("href", Url);
|
||||
return Display(new HtmlString(tagBuilder.ToString(TagRenderMode.Normal)));
|
||||
var a = new TagBuilder("a") {
|
||||
InnerHtml = Display(Content).ToString()
|
||||
};
|
||||
a.MergeAttributes(Attributes.Named);
|
||||
a.MergeAttribute("href", Url);
|
||||
return Display(new HtmlString(a.ToString()));
|
||||
}
|
||||
|
||||
public IHtmlString Image(dynamic Display, string Url, INamedEnumerable<object> Attributes) {
|
||||
var tagBuilder = new TagBuilder("img");
|
||||
tagBuilder.MergeAttributes(Attributes.Named);
|
||||
tagBuilder.MergeAttribute("src", Url);
|
||||
return Display(new HtmlString(tagBuilder.ToString(TagRenderMode.SelfClosing)));
|
||||
var img = new TagBuilder("img");
|
||||
img.MergeAttributes(Attributes.Named);
|
||||
img.MergeAttribute("src", Url);
|
||||
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
|
||||
|
||||
public IHtmlString Form(dynamic Display, dynamic Shape, object Submit) {
|
||||
return Display(new HtmlString("<form>" + Display(Shape.Fieldsets, Submit).ToString() + "</form>"));
|
||||
public IHtmlString Form(dynamic Display, dynamic Shape, object Submit, INamedEnumerable<object> Attributes) {
|
||||
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) {
|
||||
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) {
|
||||
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) {
|
||||
// not optimal obviously, just testing the waters
|
||||
return Display(new HtmlString("<label>" + Shape.Text + "</label>"
|
||||
+ "<input type='text'" + (Attributes.Named.ContainsKey("autofocus") ? " autofocus" : "" )+ " name='" + Shape.Name
|
||||
+ "' value='" + (Shape.Value == null ? "" : Shape.Value) + "' />")); // <- otherwise Shape.Value is "ClaySharp.Clay"
|
||||
public IHtmlString InputText(dynamic Display, dynamic Shape) {
|
||||
Shape.ShapeMetadata.Type = "Input";
|
||||
Shape.Attributes(new { type = "text" });
|
||||
return Display(
|
||||
New.Label().Text(Shape.Text),
|
||||
Shape); //need validation shape
|
||||
}
|
||||
|
||||
#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