Skip to content

Commit

Permalink
Markup control is recompiled when the RWHTML file changes
Browse files Browse the repository at this point in the history
Resolves #33
  • Loading branch information
tomasherceg authored and tomasherceg committed Apr 17, 2015
1 parent adf3fe3 commit 49c40ed
Show file tree
Hide file tree
Showing 20 changed files with 168 additions and 126 deletions.
9 changes: 6 additions & 3 deletions src/Redwood.Framework/Controls/DelegateTemplate.cs
@@ -1,3 +1,5 @@
using Redwood.Framework.Hosting;
using Redwood.Framework.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -7,12 +9,13 @@ namespace Redwood.Framework.Controls
public class DelegateTemplate : ITemplate
{

public Func<RedwoodControl> BuildContentBody { get; set; }
public Func<IControlBuilderFactory, RedwoodControl> BuildContentBody { get; set; }


public void BuildContent(RedwoodControl container)
public void BuildContent(RedwoodRequestContext context, RedwoodControl container)
{
var control = BuildContentBody();
var controlBuilderFactory = context.Configuration.ServiceLocator.GetService<IControlBuilderFactory>();
var control = BuildContentBody(controlBuilderFactory);
container.Children.Add(control);
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/Redwood.Framework/Controls/GridView.cs
Expand Up @@ -51,18 +51,18 @@ public Action<string> SortChanged

protected internal override void OnLoad(RedwoodRequestContext context)
{
DataBind();
DataBind(context);
base.OnLoad(context);
}

protected internal override void OnPreRender(RedwoodRequestContext context)
{
DataBind(); // TODO: support for observable collection
DataBind(context); // TODO: support for observable collection
base.OnPreRender(context);
}


private void DataBind()
private void DataBind(RedwoodRequestContext context)
{
Children.Clear();

Expand All @@ -88,7 +88,7 @@ private void DataBind()
if (dataSource != null)
{
// create header row
CreateHeaderRow(sortCommandPath);
CreateHeaderRow(context, sortCommandPath);

foreach (var item in GetIEnumerableFromDataSource(dataSource))
{
Expand All @@ -97,14 +97,14 @@ private void DataBind()
placeholder.SetBinding(DataContextProperty, new ValueBindingExpression(dataSourcePath + "[" + index + "]"));
Children.Add(placeholder);

CreateRow(placeholder);
CreateRow(context, placeholder);

index++;
}
}
}

private void CreateHeaderRow(string sortCommandPath)
private void CreateHeaderRow(RedwoodRequestContext context, string sortCommandPath)
{
var headerRow = new HtmlGenericControl("tr");
Children.Add(headerRow);
Expand All @@ -114,7 +114,7 @@ private void CreateHeaderRow(string sortCommandPath)
SetCellAttributes(column, cell, true);
headerRow.Children.Add(cell);

column.CreateHeaderControls(this, sortCommandPath, cell);
column.CreateHeaderControls(context, this, sortCommandPath, cell);
}
}

Expand All @@ -139,7 +139,7 @@ private static void SetCellAttributes(GridViewColumn column, HtmlGenericControl
}
}

private void CreateRow(DataItemContainer placeholder)
private void CreateRow(RedwoodRequestContext context, DataItemContainer placeholder)
{
var row = new HtmlGenericControl("tr");

Expand All @@ -158,7 +158,7 @@ private void CreateRow(DataItemContainer placeholder)
var cell = new HtmlGenericControl("td");
SetCellAttributes(column, cell, false);
row.Children.Add(cell);
column.CreateControls(cell);
column.CreateControls(context, cell);
}
}

Expand Down Expand Up @@ -197,7 +197,7 @@ protected override void RenderContents(IHtmlWriter writer, RenderContext context
var placeholder = new DataItemContainer { DataContext = null };
Children.Add(placeholder);

CreateRow(placeholder);
CreateRow(context.RequestContext, placeholder);

context.PathFragments.Push(dataSourceBinding.GetViewModelPathExpression(this, DataSourceProperty) + "[$index]");
placeholder.Render(writer, context);
Expand Down
5 changes: 3 additions & 2 deletions src/Redwood.Framework/Controls/GridViewColumn.cs
@@ -1,4 +1,5 @@
using Redwood.Framework.Binding;
using Redwood.Framework.Hosting;
using System;

namespace Redwood.Framework.Controls
Expand Down Expand Up @@ -68,7 +69,7 @@ public string Width



public abstract void CreateControls(RedwoodControl container);
public abstract void CreateControls(RedwoodRequestContext context, RedwoodControl container);



Expand All @@ -88,7 +89,7 @@ private void ThrowValueBindingNotSet()
}


public virtual void CreateHeaderControls(GridView gridView, string sortCommandPath, HtmlGenericControl cell)
public virtual void CreateHeaderControls(RedwoodRequestContext context, GridView gridView, string sortCommandPath, HtmlGenericControl cell)
{
if (AllowSorting)
{
Expand Down
5 changes: 3 additions & 2 deletions src/Redwood.Framework/Controls/GridViewTemplateColumn.cs
@@ -1,4 +1,5 @@
using Redwood.Framework.Binding;
using Redwood.Framework.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -21,9 +22,9 @@ public ITemplate ContentTemplate



public override void CreateControls(RedwoodControl container)
public override void CreateControls(RedwoodRequestContext context, RedwoodControl container)
{
ContentTemplate.BuildContent(container);
ContentTemplate.BuildContent(context, container);
}

private ValueBindingExpression GetValueBinding()
Expand Down
3 changes: 2 additions & 1 deletion src/Redwood.Framework/Controls/GridViewTextColumn.cs
@@ -1,4 +1,5 @@
using Redwood.Framework.Binding;
using Redwood.Framework.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -22,7 +23,7 @@ public string FormatString



public override void CreateControls(RedwoodControl container)
public override void CreateControls(RedwoodRequestContext context, RedwoodControl container)
{
var literal = new Literal();
literal.FormatString = FormatString;
Expand Down
4 changes: 3 additions & 1 deletion src/Redwood.Framework/Controls/ITemplate.cs
@@ -1,3 +1,5 @@
using Redwood.Framework.Hosting;

namespace Redwood.Framework.Controls
{
/// <summary>
Expand All @@ -9,7 +11,7 @@ public interface ITemplate
/// <summary>
/// Builds the content of the template into the specified container.
/// </summary>
void BuildContent(RedwoodControl container);
void BuildContent(RedwoodRequestContext context, RedwoodControl container);

}
}
10 changes: 5 additions & 5 deletions src/Redwood.Framework/Controls/Repeater.cs
Expand Up @@ -51,7 +51,7 @@ public Repeater()
/// </summary>
protected internal override void OnLoad(RedwoodRequestContext context)
{
DataBind();
DataBind(context);
base.OnLoad(context);
}

Expand All @@ -60,14 +60,14 @@ protected internal override void OnLoad(RedwoodRequestContext context)
/// </summary>
protected internal override void OnPreRender(RedwoodRequestContext context)
{
DataBind(); // TODO: we should handle observable collection operations to persist controlstate of controls inside the Repeater
DataBind(context); // TODO: we should handle observable collection operations to persist controlstate of controls inside the Repeater
base.OnPreRender(context);
}

/// <summary>
/// Performs the data-binding and builds the controls inside the <see cref="Repeater"/>.
/// </summary>
private void DataBind()
private void DataBind(RedwoodRequestContext context)
{
Children.Clear();

Expand All @@ -83,7 +83,7 @@ private void DataBind()
var placeholder = new DataItemContainer { DataItemIndex = index };
placeholder.SetBinding(DataContextProperty, new ValueBindingExpression(dataSourcePath + "[" + index + "]"));
Children.Add(placeholder);
ItemTemplate.BuildContent(placeholder);
ItemTemplate.BuildContent(context, placeholder);

index++;
}
Expand Down Expand Up @@ -130,7 +130,7 @@ protected override void RenderContents(IHtmlWriter writer, RenderContext context
// render on client
var placeholder = new DataItemContainer { DataContext = null };
Children.Add(placeholder);
ItemTemplate.BuildContent(placeholder);
ItemTemplate.BuildContent(context.RequestContext, placeholder);

context.PathFragments.Push(dataSourceBinding.GetViewModelPathExpression(this, DataSourceProperty) + "[$index]");
placeholder.Render(writer, context);
Expand Down
15 changes: 5 additions & 10 deletions src/Redwood.Framework/Hosting/DefaultMarkupFileLoader.cs
Expand Up @@ -11,9 +11,9 @@ public class DefaultMarkupFileLoader : IMarkupFileLoader


/// <summary>
/// Gets the markup file from the current request URL.
/// Gets the markup file virtual path from the current request URL.
/// </summary>
public MarkupFile GetMarkup(RedwoodRequestContext context)
public string GetMarkupFileVirtualPath(RedwoodRequestContext context)
{
// get file name
var fileName = context.Route != null ? context.Route.VirtualPath : context.OwinContext.Request.Uri.LocalPath;
Expand All @@ -22,29 +22,24 @@ public MarkupFile GetMarkup(RedwoodRequestContext context)
throw new Exception("The view must be a file with the .rwhtml extension!"); // TODO: exception handling
}

return GetMarkupCore(context.Configuration, fileName);
return fileName;
}

/// <summary>
/// Gets the markup file for the specified virtual path.
/// </summary>
public MarkupFile GetMarkup(RedwoodConfiguration configuration, string virtualPath)
{
return GetMarkupCore(configuration, virtualPath);
}

private static MarkupFile GetMarkupCore(RedwoodConfiguration configuration, string fileName)
{
// check that we are not outside application directory
var fullPath = Path.Combine(configuration.ApplicationPhysicalPath, fileName);
var fullPath = Path.Combine(configuration.ApplicationPhysicalPath, virtualPath);
fullPath = Path.GetFullPath(fullPath);
if (!fullPath.StartsWith(configuration.ApplicationPhysicalPath, StringComparison.CurrentCultureIgnoreCase))
{
throw new Exception("The view cannot be located outside the website directory!"); // TODO: exception handling
}

// load the file
return new MarkupFile(fileName, fullPath);
return new MarkupFile(virtualPath, fullPath);
}
}
}
2 changes: 1 addition & 1 deletion src/Redwood.Framework/Hosting/IMarkupFileLoader.cs
Expand Up @@ -11,7 +11,7 @@ public interface IMarkupFileLoader
/// <summary>
/// Gets the markup file from the current request URL.
/// </summary>
MarkupFile GetMarkup(RedwoodRequestContext context);
string GetMarkupFileVirtualPath(RedwoodRequestContext context);

/// <summary>
/// Gets the markup file for the specified virtual path.
Expand Down
10 changes: 5 additions & 5 deletions src/Redwood.Framework/Runtime/Compilation/DefaultViewCompiler.cs
Expand Up @@ -57,10 +57,10 @@ public IControlBuilder CompileView(IReader reader, string fileName, string assem
// determine wrapper type
string wrapperClassName;
var wrapperType = ResolveWrapperType(node, className, out wrapperClassName);
var metadata = controlResolver.ResolveControl(wrapperType);
var metadata = controlResolver.ResolveControl(new ControlType(wrapperType, virtualPath: fileName));

// build the statements
emitter.PushNewMethod("BuildControl");
emitter.PushNewMethod(DefaultViewCompilerCodeEmitter.BuildControlFunctionName);
var pageName = wrapperClassName == null ? emitter.EmitCreateObject(wrapperType) : emitter.EmitCreateObject(wrapperClassName);
emitter.EmitSetAttachedProperty(pageName, typeof(Internal).FullName, Internal.UniqueIDProperty.Name, pageName);
foreach (var child in node.Content)
Expand Down Expand Up @@ -310,7 +310,7 @@ private string ProcessTemplate(RwHtmlElementNode element)
/// </summary>
private string CompileTemplate(RwHtmlElementNode element)
{
var methodName = "BuildTemplate" + currentTemplateIndex;
var methodName = DefaultViewCompilerCodeEmitter.BuildTemplateFunctionName + currentTemplateIndex;
currentTemplateIndex++;
emitter.PushNewMethod(methodName);

Expand Down Expand Up @@ -343,8 +343,8 @@ private string ProcessObjectElement(RwHtmlElementNode element)
}
else
{
// markup control
currentObjectName = emitter.EmitInvokeControlBuilder(controlMetadata.Type, controlMetadata.ControlBuilderType);
// markup control
currentObjectName = emitter.EmitInvokeControlBuilder(controlMetadata.Type, controlMetadata.VirtualPath);
}
emitter.EmitSetAttachedProperty(currentObjectName, typeof(Internal).FullName, Internal.UniqueIDProperty.Name, currentObjectName);

Expand Down

0 comments on commit 49c40ed

Please sign in to comment.