Skip to content

Commit

Permalink
refactored html writer into rendering engine and create grid engine t…
Browse files Browse the repository at this point in the history
…o consolidate code
  • Loading branch information
Joe Harrison committed Feb 11, 2015
1 parent 805af74 commit af97a18
Show file tree
Hide file tree
Showing 10 changed files with 388 additions and 412 deletions.
162 changes: 162 additions & 0 deletions MVCGrid/Engine/GridEngine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
using MVCGrid.Interfaces;
using MVCGrid.Models;
using MVCGrid.Rendering;
using MVCGrid.Utility;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace MVCGrid.Engine
{
public class GridEngine
{
public void Run(IMVCGridRenderingEngine renderingEngine, GridContext gridContext, Stream outputStream)
{
if (!renderingEngine.AllowsPaging)
{
gridContext.QueryOptions.ItemsPerPage = null;
}

int? totalRecords;
var rows = ((GridDefinitionBase)gridContext.GridDefinition).GetData(gridContext, out totalRecords);

// if a page was requested higher than available pages, requery for first page
if (rows.Count == 0 && totalRecords.HasValue && totalRecords.Value > 0)
{
gridContext.QueryOptions.PageIndex = 0;
rows = ((GridDefinitionBase)gridContext.GridDefinition).GetData(gridContext, out totalRecords);
}

var model = PrepModel(totalRecords, rows, gridContext);

renderingEngine.Render(model, outputStream);
}

private RenderingModel PrepModel(int? totalRecords, List<Row> rows, Models.GridContext gridContext)
{
RenderingModel model = new RenderingModel();

model.HandlerPath = HtmlUtility.GetHandlerPath();
model.TableHtmlId = HtmlUtility.GetTableHtmlId(gridContext.GridName);

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

if (model.Rows.Count == 0)
{
model.NoResultsMessage = gridContext.GridDefinition.NoResultsMessage;
}

model.PagingModel = null;
if (gridContext.QueryOptions.ItemsPerPage.HasValue)
{
model.PagingModel = new PagingModel();

int currentPageIndex = gridContext.QueryOptions.PageIndex.Value;

model.PagingModel.TotalRecords = totalRecords.Value;

model.PagingModel.FirstRecord = (currentPageIndex * gridContext.QueryOptions.ItemsPerPage.Value) + 1;
model.PagingModel.LastRecord = (model.PagingModel.FirstRecord + gridContext.QueryOptions.ItemsPerPage.Value) - 1;
if (model.PagingModel.LastRecord > model.PagingModel.TotalRecords)
{
model.PagingModel.LastRecord = model.PagingModel.TotalRecords;
}
model.PagingModel.CurrentPage = currentPageIndex + 1;

var numberOfPagesD = (model.PagingModel.TotalRecords + 0.0) / (gridContext.QueryOptions.ItemsPerPage.Value + 0.0);
model.PagingModel.NumberOfPages = (int)Math.Ceiling(numberOfPagesD);

for (int i = 1; i <= model.PagingModel.NumberOfPages; i++)
{
model.PagingModel.PageLinks.Add(i, HtmlUtility.MakeGotoPageLink(gridContext.GridName, i));
}
}
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())
{
Column renderingColumn = new Column();
model.Columns.Add(renderingColumn);
renderingColumn.Name = col.ColumnName;
renderingColumn.HeaderText = col.HeaderText;

if (gridContext.GridDefinition.Sorting && col.EnableSorting)
{
SortDirection linkDirection = SortDirection.Asc;
SortDirection iconDirection = SortDirection.Unspecified;

if (gridContext.QueryOptions.SortColumn == col.ColumnName && gridContext.QueryOptions.SortDirection == SortDirection.Asc)
{
iconDirection = SortDirection.Asc;
linkDirection = SortDirection.Dsc;
}
else if (gridContext.QueryOptions.SortColumn == col.ColumnName && gridContext.QueryOptions.SortDirection == SortDirection.Dsc)
{
iconDirection = SortDirection.Dsc;
linkDirection = SortDirection.Asc;
}
else
{
iconDirection = SortDirection.Unspecified;
linkDirection = SortDirection.Dsc;
}

renderingColumn.Onclick = HtmlUtility.MakeSortLink(gridContext.GridName, col.ColumnName, linkDirection);
renderingColumn.SortIconDirection = iconDirection;
}
}
}
}
}
13 changes: 0 additions & 13 deletions MVCGrid/Interfaces/IMVCGridHtmlWriter.cs

This file was deleted.

2 changes: 1 addition & 1 deletion MVCGrid/Interfaces/IMVCGridRenderingEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public interface IMVCGridRenderingEngine
{
bool AllowsPaging { get; }
void PrepareResponse(HttpResponse response);
void Render(GridData data, GridContext gridContext, Stream outputStream);
void Render(RenderingModel model, Stream outputStream);
}
}
3 changes: 1 addition & 2 deletions MVCGrid/MVCGrid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Interfaces\IMVCGridHtmlWriter.cs" />
<Compile Include="Engine\GridEngine.cs" />
<Compile Include="Interfaces\IMVCGridRenderingEngine.cs" />
<Compile Include="Models\MVCGridBuilder.cs" />
<Compile Include="Models\GridColumnBuilder.cs" />
<Compile Include="Models\GridContext.cs" />
<Compile Include="Models\RenderingModel.cs" />
<Compile Include="Rendering\BootstrapHtmlWriter.cs" />
<Compile Include="Rendering\CsvRenderingEngine.cs" />
<Compile Include="Rendering\HtmlRenderingEngine.cs" />
<Compile Include="Utility\GridContextUtility.cs" />
Expand Down
49 changes: 27 additions & 22 deletions MVCGrid/Models/GridDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ namespace MVCGrid.Models
{
public abstract class GridDefinitionBase
{
internal abstract GridData GetData(GridContext context);
internal abstract List<Row> GetData(GridContext context, out int? totalRecords);
}

public class GridDefinition<T1> : GridDefinitionBase, IMVCGridDefinition
{
private Type _htmlWriterType;
private Type _defaultRenderingEngine;
const string DefaultNoResultsMessage = "No results.";

public GridDefinition() : this(null)
{

//_defaultRenderingEngine=
}

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

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

public GridConfiguration GridConfiguration { get; set; }
Expand Down Expand Up @@ -78,58 +78,63 @@ public void AddColumn(GridColumn<T1> column)
public Func<QueryOptions, QueryResult<T1>> RetrieveData { get; set; }
public Func<T1, GridContext, string> RowCssClassExpression { get; set; }

internal override GridData GetData(GridContext context)
internal override List<Row> GetData(GridContext context, out int? totalRecords)
{
GridData result = new GridData();
List<Row> resultRows = new List<Row>();

var queryResult = RetrieveData(context.QueryOptions);
result.TotalRecords = queryResult.TotalRecords;
totalRecords = queryResult.TotalRecords;

if (context.GridDefinition.Paging && !result.TotalRecords.HasValue)
if (context.GridDefinition.Paging && !totalRecords.HasValue)
{
throw new Exception("When paging is enabled, QueryResult must contain the TotalRecords");
}

foreach (var item in queryResult.Items)
{
GridRow thisRow = new GridRow();
Row thisRow = new Row();

if (RowCssClassExpression != null)
{
string rowCss = RowCssClassExpression(item, context);
if (!String.IsNullOrWhiteSpace(rowCss))
{
thisRow.RowCssClass = rowCss;
thisRow.CalculatedCssClass = rowCss;
}
}

foreach (var col in this.Columns)
{
string val = col.ValueExpression(item, context);
Cell thisCell = new Cell();
thisRow.Cells.Add(col.ColumnName, thisCell);

string plainVal = val;
if (col.PlainTextValueExpression != null)
thisCell.HtmlText = col.ValueExpression(item, context);

if (col.HtmlEncode)
{
plainVal = col.PlainTextValueExpression(item, context);
thisCell.HtmlText = System.Web.HttpUtility.HtmlEncode(thisCell.HtmlText);
}

thisRow.Values.Add(col.ColumnName, val);
thisRow.PlainTextValues.Add(col.ColumnName, plainVal);
thisCell.PlainText = thisCell.HtmlText;
if (col.PlainTextValueExpression != null)
{
thisCell.PlainText = col.PlainTextValueExpression(item, context);
}

if (col.CellCssClassExpression != null)
{
string cellCss = col.CellCssClassExpression(item, context);
if (!String.IsNullOrWhiteSpace(cellCss))
{
thisRow.CellCssClasses.Add(col.ColumnName, cellCss);
thisCell.CalculatedCssClass = cellCss;
}
}
}

result.Rows.Add(thisRow);
resultRows.Add(thisRow);
}

return result;
return resultRows;
}

/// <summary>
Expand All @@ -149,15 +154,15 @@ internal override GridData GetData(GridContext context)
public string ClientSideLoadingMessageFunctionName { get; set; }
public string ClientSideLoadingCompleteFunctionName { get; set; }

public Type HtmlWriterType
public Type DefaultRenderingEngine
{
get
{
return _htmlWriterType;
return _defaultRenderingEngine;
}
set
{
_htmlWriterType = value;
_defaultRenderingEngine = value;
}
}
}
Expand Down
Loading

0 comments on commit af97a18

Please sign in to comment.