Skip to content

Commit

Permalink
dynamic instantiation of rendering engine
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Harrison committed Feb 11, 2015
1 parent af97a18 commit db7c439
Show file tree
Hide file tree
Showing 15 changed files with 50 additions and 125 deletions.
66 changes: 20 additions & 46 deletions MVCGrid/Engine/GridEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@ namespace MVCGrid.Engine
{
public class GridEngine
{
public IMVCGridRenderingEngine GetRenderingEngine(GridContext gridContext)
{
IMVCGridRenderingEngine renderingEngine = null;

if (!String.IsNullOrWhiteSpace(gridContext.QueryOptions.RenderingEngineName))
{
if (String.Compare(gridContext.QueryOptions.RenderingEngineName, "export", true) == 0)
{
renderingEngine = new CsvRenderingEngine();
}
}

if (renderingEngine == null)
{
renderingEngine = (IMVCGridRenderingEngine)Activator.CreateInstance(gridContext.GridDefinition.DefaultRenderingEngine, true);
}

return renderingEngine;
}

public void Run(IMVCGridRenderingEngine renderingEngine, GridContext gridContext, Stream outputStream)
{
if (!renderingEngine.AllowsPaging)
Expand Down Expand Up @@ -43,7 +63,6 @@ private RenderingModel PrepModel(int? totalRecords, List<Row> rows, Models.GridC
model.TableHtmlId = HtmlUtility.GetTableHtmlId(gridContext.GridName);

PrepColumns(gridContext, model);
//PrepRows(data, gridContext, model);
model.Rows = rows;

if (model.Rows.Count == 0)
Expand Down Expand Up @@ -78,51 +97,6 @@ private RenderingModel PrepModel(int? totalRecords, List<Row> rows, Models.GridC
}
return model;
}

//private void PrepRows(Models.GridData data, Models.GridContext gridContext, RenderingModel model)
//{
// foreach (var item in data.Rows)
// {
// Row renderingRow = new Row();
// model.Rows.Add(renderingRow);

// if (!String.IsNullOrWhiteSpace(item.RowCssClass))
// {
// renderingRow.CalculatedCssClass = item.RowCssClass;
// }

// foreach (var col in gridContext.GetVisibleColumns())
// {
// string val = "";

// if (item.Values.ContainsKey(col.ColumnName))
// {
// val = item.Values[col.ColumnName];
// }

// Cell renderingCell = new Cell();
// renderingRow.Cells.Add(col.ColumnName, renderingCell);
// if (item.CellCssClasses.ContainsKey(col.ColumnName))
// {
// string cellCss = item.CellCssClasses[col.ColumnName];
// if (!String.IsNullOrWhiteSpace(cellCss))
// {
// renderingCell.CalculatedCssClass = cellCss;
// }
// }

// if (col.HtmlEncode)
// {
// renderingCell.HtmlText = HttpUtility.HtmlEncode(val);
// }
// else
// {
// renderingCell.HtmlText = val;
// }
// }
// }
//}

private void PrepColumns(Models.GridContext gridContext, RenderingModel model)
{
foreach (var col in gridContext.GetVisibleColumns())
Expand Down
2 changes: 1 addition & 1 deletion MVCGrid/Interfaces/IMVCGridDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ internal interface IMVCGridDefinition
string ClientSideLoadingCompleteFunctionName { get; set; }
bool Filtering { get; set; }

Type HtmlWriterType { get; set; }
Type DefaultRenderingEngine { get; set; }
}
}
4 changes: 1 addition & 3 deletions MVCGrid/MVCGrid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,14 @@
<Compile Include="Models\GridContext.cs" />
<Compile Include="Models\RenderingModel.cs" />
<Compile Include="Rendering\CsvRenderingEngine.cs" />
<Compile Include="Rendering\HtmlRenderingEngine.cs" />
<Compile Include="Rendering\BootstrapRenderingEngine.cs" />
<Compile Include="Utility\GridContextUtility.cs" />
<Compile Include="Utility\HtmlUtility.cs" />
<Compile Include="Web\HtmlExtensions.cs" />
<Compile Include="Interfaces\IMVCGridColumn.cs" />
<Compile Include="Interfaces\IMVCGridDefinition.cs" />
<Compile Include="Models\GridColumn.cs" />
<Compile Include="Models\GridConfiguration.cs" />
<Compile Include="Models\GridData.cs" />
<Compile Include="Models\GridRow.cs" />
<Compile Include="Models\QueryResult.cs" />
<Compile Include="Models\GridDefinition.cs" />
<Compile Include="Models\SortDirection.cs" />
Expand Down
18 changes: 0 additions & 18 deletions MVCGrid/Models/GridData.cs

This file was deleted.

3 changes: 1 addition & 2 deletions MVCGrid/Models/GridDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class GridDefinition<T1> : GridDefinitionBase, IMVCGridDefinition

public GridDefinition() : this(null)
{
//_defaultRenderingEngine=
}

public GridDefinition(GridConfiguration copyFromConfig):base()
Expand All @@ -32,7 +31,7 @@ public GridDefinition(GridConfiguration copyFromConfig):base()

Columns = new List<GridColumn<T1>>();
NoResultsMessage = DefaultNoResultsMessage;
//_htmlWriterType = typeof(MVCGrid.Rendering.BootstrapHtmlWriter);
_defaultRenderingEngine = typeof(MVCGrid.Rendering.BootstrapRenderingEngine);
}

public GridConfiguration GridConfiguration { get; set; }
Expand Down
23 changes: 0 additions & 23 deletions MVCGrid/Models/GridRow.cs

This file was deleted.

2 changes: 2 additions & 0 deletions MVCGrid/Models/QueryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public QueryOptions()
Filters = new Dictionary<string, string>();
}

public string RenderingEngineName { get; set; }

public SortDirection SortDirection { get; set; }
public string SortColumn { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@

namespace MVCGrid.Rendering
{
public class HtmlRenderingEngine : IMVCGridRenderingEngine
public class BootstrapRenderingEngine : IMVCGridRenderingEngine
{
private string CssTable;
private string HtmlImageSortAsc;
private string HtmlImageSortDsc;
private string HtmlImageSort;

public HtmlRenderingEngine()
public BootstrapRenderingEngine()
{
CssTable = "table table-striped table-bordered";
}
Expand Down
4 changes: 2 additions & 2 deletions MVCGrid/Web/HtmlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ internal static IHtmlString MVCGrid(this HtmlHelper helper, string name, IMVCGri

var gridContext = GridContextUtility.Create(HttpContext.Current, gridName, grid, options);

IMVCGridRenderingEngine renderingEngine = new HtmlRenderingEngine();

GridEngine engine = new GridEngine();

IMVCGridRenderingEngine renderingEngine = engine.GetRenderingEngine(gridContext);

using (MemoryStream ms = new MemoryStream())
{
Expand Down
29 changes: 4 additions & 25 deletions MVCGrid/Web/MVCGridHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private void HandelPngImage(HttpContext context, string imageName)
context.Response.Flush();
}



private void HandleTable(HttpContext context)
{
Expand All @@ -173,34 +173,13 @@ private void HandleTable(HttpContext context)

var gridContext = GridContextUtility.Create(context, gridName, grid, options);

IMVCGridRenderingEngine renderingEngine = DetermineRenderingEngine(context);

GridEngine engine = new GridEngine();
renderingEngine.PrepareResponse(context.Response);
engine.Run(renderingEngine, gridContext, context.Response.OutputStream);
}

private IMVCGridRenderingEngine DetermineRenderingEngine(HttpContext context)
{
IMVCGridRenderingEngine engine = null;

if (context.Request.QueryString["engine"] != null)
{
string re = context.Request.QueryString["engine"];
if (String.Compare(re, "export", true) == 0)
{
engine = new CsvRenderingEngine();
}
}

if (engine == null)
{
engine = new HtmlRenderingEngine();
}
IMVCGridRenderingEngine renderingEngine = engine.GetRenderingEngine(gridContext);

return engine;
renderingEngine.PrepareResponse(context.Response);
engine.Run(renderingEngine, gridContext, context.Response.OutputStream);
}


private void HandleScript(HttpContext context)
{
Expand Down
6 changes: 6 additions & 0 deletions MVCGrid/Web/QueryStringParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public static QueryOptions ParseOptions(IMVCGridDefinition grid, HttpRequest htt

var options = new QueryOptions();

if (httpRequest.QueryString["engine"] != null)
{
string re = httpRequest.QueryString["engine"];
options.RenderingEngineName = re;
}

if (!grid.Paging)
{
options.ItemsPerPage = null;
Expand Down
3 changes: 2 additions & 1 deletion MVCGridExample/App_Start/MVCGridConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public static void RegisterGrids()
{
return String.Format("<a href='{0}'>{1}</a>",
c.UrlHelper.Action("detail", "demo", new { id = p.Id }), p.Id);
});
})
.WithPlainTextValueExpression((p,c) => p.Id.ToString());
cols.Add().WithColumnName("FirstName")
.WithHeaderText("First Name")
.WithValueExpression((p, c) => p.FirstName);
Expand Down
3 changes: 2 additions & 1 deletion MVCGridExample/Content/MVCGridConfig.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ namespace MVCGridExample
{
return String.Format("<a href='{0}'>{1}</a>",
c.UrlHelper.Action("detail", "demo", new { id = p.Id }), p.Id);
});
})
.WithPlainTextValueExpression((p,c) => p.Id.ToString());
cols.Add().WithColumnName("FirstName")
.WithHeaderText("First Name")
.WithValueExpression((p, c) => p.FirstName);
Expand Down
1 change: 0 additions & 1 deletion MVCGridExample/Views/Demo/Export.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
</button>
</div>
</div>
<p>&nbsp;</p>

@Html.MVCGrid("ExportGrid")

Expand Down
7 changes: 7 additions & 0 deletions MVCGridExample/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
</ul>

<h2>Example</h2>
<div class="container">
<div class="row">
<button class="btn btn-default pull-right" id="exportButton" onclick="location.href = MVCGrid.getExportUrl('TestGrid');">
<span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span> Export
</button>
</div>
</div>
@(Html.MVCGrid("TestGrid"))

@*<input type="button" value="Refresh" onclick="MVCGrid.reloadGrid('TestMapping');" />
Expand Down

0 comments on commit db7c439

Please sign in to comment.