Skip to content

Commit

Permalink
built-in support for a:member-template
Browse files Browse the repository at this point in the history
  • Loading branch information
maxtoroq committed Dec 10, 2015
1 parent 95ba5c1 commit 09827ff
Show file tree
Hide file tree
Showing 12 changed files with 283 additions and 233 deletions.
52 changes: 26 additions & 26 deletions src/Xcst.Web.Mvc/Compilation/xcst-app.xsl
Expand Up @@ -170,7 +170,7 @@
-->

<template match="a:text-box" mode="src:extension-instruction">
<param name="output" tunnel="yes"/>
<param name="context-param" tunnel="yes"/>

<variable name="expr">
<value-of select="a:fully-qualified-helper-html('InputExtensions')"/>
Expand All @@ -179,7 +179,7 @@
<text>(</text>
<call-template name="a:html-helper"/>
<text>, </text>
<value-of select="$output"/>
<value-of select="$context-param"/>
<text>, </text>
<choose>
<when test="@for">
Expand Down Expand Up @@ -209,7 +209,7 @@
</template>

<template match="a:password" mode="src:extension-instruction">
<param name="output" tunnel="yes"/>
<param name="context-param" tunnel="yes"/>

<variable name="expr">
<value-of select="a:fully-qualified-helper-html('InputExtensions')"/>
Expand All @@ -218,7 +218,7 @@
<text>(</text>
<call-template name="a:html-helper"/>
<text>, </text>
<value-of select="$output"/>
<value-of select="$context-param"/>
<text>, </text>
<choose>
<when test="@for">
Expand All @@ -244,7 +244,7 @@
</template>

<template match="a:hidden" mode="src:extension-instruction">
<param name="output" tunnel="yes"/>
<param name="context-param" tunnel="yes"/>

<variable name="expr">
<value-of select="a:fully-qualified-helper-html('InputExtensions')"/>
Expand All @@ -253,7 +253,7 @@
<text>(</text>
<call-template name="a:html-helper"/>
<text>, </text>
<value-of select="$output"/>
<value-of select="$context-param"/>
<text>, </text>
<choose>
<when test="@for">
Expand All @@ -279,7 +279,7 @@
</template>

<template match="a:text-area" mode="src:extension-instruction">
<param name="output" tunnel="yes"/>
<param name="context-param" tunnel="yes"/>

<variable name="expr">
<value-of select="a:fully-qualified-helper-html('TextAreaExtensions')"/>
Expand All @@ -288,7 +288,7 @@
<text>(</text>
<call-template name="a:html-helper"/>
<text>, </text>
<value-of select="$output"/>
<value-of select="$context-param"/>
<text>, </text>
<choose>
<when test="@for">
Expand Down Expand Up @@ -320,7 +320,7 @@
</template>

<template match="a:check-box" mode="src:extension-instruction">
<param name="output" tunnel="yes"/>
<param name="context-param" tunnel="yes"/>

<variable name="expr">
<value-of select="a:fully-qualified-helper-html('InputExtensions')"/>
Expand All @@ -329,7 +329,7 @@
<text>(</text>
<call-template name="a:html-helper"/>
<text>, </text>
<value-of select="$output"/>
<value-of select="$context-param"/>
<text>, </text>
<choose>
<when test="@for">
Expand Down Expand Up @@ -357,7 +357,7 @@
</template>

<template match="a:radio-button" mode="src:extension-instruction">
<param name="output" tunnel="yes"/>
<param name="context-param" tunnel="yes"/>

<variable name="expr">
<value-of select="a:fully-qualified-helper-html('InputExtensions')"/>
Expand All @@ -366,7 +366,7 @@
<text>(</text>
<call-template name="a:html-helper"/>
<text>, </text>
<value-of select="$output"/>
<value-of select="$context-param"/>
<text>, </text>
<choose>
<when test="@for">
Expand Down Expand Up @@ -404,14 +404,14 @@
</template>

<template match="a:http-method-override" mode="src:extension-instruction">
<param name="output" tunnel="yes"/>
<param name="context-param" tunnel="yes"/>

<variable name="expr">
<value-of select="a:fully-qualified-helper-html('InputExtensions')"/>
<text>.HttpMethodOverride(</text>
<call-template name="a:html-helper"/>
<text>, </text>
<value-of select="$output"/>
<value-of select="$context-param"/>
<text>, </text>
<value-of select="src:expand-attribute(@method)"/>
<text>)</text>
Expand All @@ -420,7 +420,7 @@
</template>

<template match="a:drop-down-list | a:list-box" mode="src:extension-instruction">
<param name="output" tunnel="yes"/>
<param name="context-param" tunnel="yes"/>

<variable name="ddl" select="self::a:drop-down-list"/>
<variable name="expr">
Expand All @@ -431,7 +431,7 @@
<text>(</text>
<call-template name="a:html-helper"/>
<text>, </text>
<value-of select="$output"/>
<value-of select="$context-param"/>
<text>, </text>
<choose>
<when test="@for">
Expand Down Expand Up @@ -499,7 +499,7 @@
</template>

<template match="a:label" mode="src:extension-instruction">
<param name="output" tunnel="yes"/>
<param name="context-param" tunnel="yes"/>
<param name="a:model-metadata" as="xs:string?" tunnel="yes"/>

<variable name="for-model" select="empty((@for, @name, $a:model-metadata))"/>
Expand All @@ -514,7 +514,7 @@
<text>(</text>
<call-template name="a:html-helper"/>
<text>, </text>
<value-of select="$output"/>
<value-of select="$context-param"/>
<if test="$a:model-metadata">
<text>, </text>
<value-of select="$a:model-metadata"/>
Expand Down Expand Up @@ -551,14 +551,14 @@
</template>

<template match="a:validation-summary" mode="src:extension-instruction">
<param name="output" tunnel="yes"/>
<param name="context-param" tunnel="yes"/>

<variable name="expr">
<value-of select="a:fully-qualified-helper-html('ValidationExtensions')"/>
<text>.ValidationSummary(</text>
<call-template name="a:html-helper"/>
<text>, </text>
<value-of select="$output"/>
<value-of select="$context-param"/>
<if test="@exclude-member-errors">
<text>, excludePropertyErrors: </text>
<value-of select="@exclude-member-errors"/>
Expand All @@ -576,7 +576,7 @@
</template>

<template match="a:validation-message" mode="src:extension-instruction">
<param name="output" tunnel="yes"/>
<param name="context-param" tunnel="yes"/>
<param name="a:model-metadata" as="xs:string?" tunnel="yes"/>

<variable name="expr">
Expand All @@ -586,7 +586,7 @@
<text>(</text>
<call-template name="a:html-helper"/>
<text>, </text>
<value-of select="$output"/>
<value-of select="$context-param"/>
<text>, </text>
<choose>
<when test="@for">
Expand Down Expand Up @@ -731,7 +731,7 @@
-->

<template match="a:editor | a:display" mode="src:extension-instruction">
<param name="output" tunnel="yes"/>
<param name="context-param" tunnel="yes"/>
<param name="a:model-metadata" as="xs:string?" tunnel="yes"/>

<variable name="editor" select="self::a:editor"/>
Expand All @@ -745,7 +745,7 @@
<text>(</text>
<call-template name="a:html-helper"/>
<text>, </text>
<value-of select="$output"/>
<value-of select="$context-param"/>
<if test="not($for-model)">
<text>, </text>
<choose>
Expand Down Expand Up @@ -889,7 +889,7 @@
</template>

<template match="a:display-text" mode="src:extension-instruction">
<param name="output" tunnel="yes"/>
<param name="context-param" tunnel="yes"/>
<param name="src:current-mode" as="xs:QName" required="yes" tunnel="yes"/>
<param name="a:model-metadata" as="xs:string?" tunnel="yes"/>

Expand All @@ -904,7 +904,7 @@
<call-template name="a:html-helper"/>
<if test="$statement">
<text>, </text>
<value-of select="$output"/>
<value-of select="$context-param"/>
</if>
<text>, </text>
<choose>
Expand Down
56 changes: 37 additions & 19 deletions src/Xcst.Web.Mvc/Html/DefaultDisplayTemplates.cs
Expand Up @@ -24,12 +24,15 @@
using System.Linq;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using Xcst.Runtime;

namespace Xcst.Web.Mvc.Html {

static class DefaultDisplayTemplates {

public static void BooleanTemplate(HtmlHelper html, XcstWriter output) {
public static void BooleanTemplate(HtmlHelper html, DynamicContext context) {

XcstWriter output = context.Output;

bool? value = null;

Expand Down Expand Up @@ -76,11 +79,11 @@ static class DefaultDisplayTemplates {
}
}

public static void CollectionTemplate(HtmlHelper html, XcstWriter output) {
CollectionTemplate(html, output, TemplateHelpers.TemplateHelper);
public static void CollectionTemplate(HtmlHelper html, DynamicContext context) {
CollectionTemplate(html, context, TemplateHelpers.TemplateHelper);
}

internal static void CollectionTemplate(HtmlHelper html, XcstWriter output, TemplateHelpers.TemplateHelperDelegate templateHelper) {
internal static void CollectionTemplate(HtmlHelper html, DynamicContext context, TemplateHelpers.TemplateHelperDelegate templateHelper) {

object model = html.ViewContext.ViewData.ModelMetadata.Model;

Expand Down Expand Up @@ -122,50 +125,56 @@ static class DefaultDisplayTemplates {

ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForType(() => item, itemType);
string fieldName = String.Format(CultureInfo.InvariantCulture, "{0}[{1}]", fieldNameBase, index++);
templateHelper(html, output, metadata, fieldName, null /* templateName */, DataBoundControlMode.ReadOnly, null /* additionalViewData */);
templateHelper(html, context, metadata, fieldName, null /* templateName */, DataBoundControlMode.ReadOnly, null /* additionalViewData */);
}

} finally {
html.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = oldPrefix;
}
}

public static void DecimalTemplate(HtmlHelper html, XcstWriter output) {
public static void DecimalTemplate(HtmlHelper html, DynamicContext context) {

if (html.ViewContext.ViewData.TemplateInfo.FormattedModelValue == html.ViewContext.ViewData.ModelMetadata.Model) {
html.ViewContext.ViewData.TemplateInfo.FormattedModelValue = output.SimpleContent.Format("{0:0.00}", html.ViewContext.ViewData.ModelMetadata.Model);
html.ViewContext.ViewData.TemplateInfo.FormattedModelValue = context.Output.SimpleContent.Format("{0:0.00}", html.ViewContext.ViewData.ModelMetadata.Model);
}

StringTemplate(html, output);
StringTemplate(html, context);
}

public static void EmailAddressTemplate(HtmlHelper html, XcstWriter output) {
public static void EmailAddressTemplate(HtmlHelper html, DynamicContext context) {

XcstWriter output = context.Output;

output.WriteStartElement("a");
output.WriteAttributeString("href", "mailto:" + Convert.ToString(html.ViewContext.ViewData.Model, CultureInfo.InvariantCulture));
output.WriteString(html.ViewContext.ViewData.TemplateInfo.FormattedModelValue);
output.WriteEndElement();
}

public static void HiddenInputTemplate(HtmlHelper html, XcstWriter output) {
public static void HiddenInputTemplate(HtmlHelper html, DynamicContext context) {

if (html.ViewContext.ViewData.ModelMetadata.HideSurroundingHtml) {
return;
}

StringTemplate(html, output);
StringTemplate(html, context);
}

public static void HtmlTemplate(HtmlHelper html, XcstWriter output) {
public static void HtmlTemplate(HtmlHelper html, DynamicContext context) {

XcstWriter output = context.Output;

output.WriteRaw(output.SimpleContent.Convert(html.ViewContext.ViewData.TemplateInfo.FormattedModelValue));
}

public static void ObjectTemplate(HtmlHelper html, XcstWriter output) {
ObjectTemplate(html, output, TemplateHelpers.TemplateHelper);
public static void ObjectTemplate(HtmlHelper html, DynamicContext context) {
ObjectTemplate(html, context, TemplateHelpers.TemplateHelper);
}

internal static void ObjectTemplate(HtmlHelper html, XcstWriter output, TemplateHelpers.TemplateHelperDelegate templateHelper) {
internal static void ObjectTemplate(HtmlHelper html, DynamicContext context, TemplateHelpers.TemplateHelperDelegate templateHelper) {

XcstWriter output = context.Output;
ViewDataDictionary viewData = html.ViewContext.ViewData;
TemplateInfo templateInfo = viewData.TemplateInfo;
ModelMetadata modelMetadata = viewData.ModelMetadata;
Expand All @@ -190,10 +199,17 @@ static class DefaultDisplayTemplates {
return;
}

Action<DynamicContext> memberTemplate = viewData[DefaultEditorTemplates.MemberTemplateKey] as Action<DynamicContext>;

foreach (ModelMetadata propertyMetadata in modelMetadata.Properties.Where(pm => ShouldShow(pm, templateInfo))) {

if (!propertyMetadata.HideSurroundingHtml) {

if (memberTemplate != null) {
memberTemplate(new DynamicContext(context).WithParam("member", propertyMetadata));
continue;
}

output.WriteStartElement("div");
output.WriteAttributeString("class", "display-label");
output.WriteString(propertyMetadata.GetDisplayName() ?? "");
Expand All @@ -203,7 +219,7 @@ static class DefaultDisplayTemplates {
output.WriteAttributeString("class", "display-field");
}

templateHelper(html, output, propertyMetadata, propertyMetadata.PropertyName, null /* templateName */, DataBoundControlMode.ReadOnly, null /* additionalViewData */);
templateHelper(html, context, propertyMetadata, propertyMetadata.PropertyName, null /* templateName */, DataBoundControlMode.ReadOnly, null /* additionalViewData */);

if (!propertyMetadata.HideSurroundingHtml) {
output.WriteEndElement(); // </div>
Expand All @@ -219,11 +235,13 @@ static class DefaultDisplayTemplates {
&& !templateInfo.Visited(metadata);
}

public static void StringTemplate(HtmlHelper html, XcstWriter output) {
output.WriteString(html.ViewContext.ViewData.TemplateInfo.FormattedModelValue);
public static void StringTemplate(HtmlHelper html, DynamicContext context) {
context.Output.WriteString(html.ViewContext.ViewData.TemplateInfo.FormattedModelValue);
}

public static void UrlTemplate(HtmlHelper html, XcstWriter output) {
public static void UrlTemplate(HtmlHelper html, DynamicContext context) {

XcstWriter output = context.Output;

output.WriteStartElement("a");
output.WriteAttributeString("href", Convert.ToString(html.ViewContext.ViewData.Model, CultureInfo.InvariantCulture));
Expand Down

0 comments on commit 09827ff

Please sign in to comment.