2014-05-13 12:36:38 +02:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
2015-01-30 15:48:55 +11:00
using System.Web.Mvc.Html ;
2015-01-30 15:42:12 +11:00
using Umbraco.Core ;
2014-05-13 12:36:38 +02:00
using Umbraco.Core.Models ;
using System.Web.Mvc ;
using Umbraco.Web.Templates ;
using System.IO ;
using System.Web.Routing ;
using Umbraco.Web.Mvc ;
namespace Umbraco.Web
{
2015-01-30 15:48:55 +11:00
2014-10-07 13:39:18 +02:00
public static class GridTemplateExtensions
2014-05-13 12:36:38 +02:00
{
2015-01-30 15:48:55 +11:00
public static MvcHtmlString GetGridHtml ( this HtmlHelper html , IPublishedProperty property , string framework = "bootstrap3" )
{
var asString = property . Value as string ;
2015-06-16 15:04:31 +02:00
if ( asString ! = null & & string . IsNullOrEmpty ( asString ) ) return new MvcHtmlString ( string . Empty ) ;
2015-01-30 15:48:55 +11:00
var view = "Grid/" + framework ;
return html . Partial ( view , property . Value ) ;
}
public static MvcHtmlString GetGridHtml ( this HtmlHelper html , IPublishedContent contentItem )
{
return html . GetGridHtml ( contentItem , "bodyText" , "bootstrap3" ) ;
}
public static MvcHtmlString GetGridHtml ( this HtmlHelper html , IPublishedContent contentItem , string propertyAlias )
{
Mandate . ParameterNotNullOrEmpty ( propertyAlias , "propertyAlias" ) ;
return html . GetGridHtml ( contentItem , propertyAlias , "bootstrap3" ) ;
}
public static MvcHtmlString GetGridHtml ( this HtmlHelper html , IPublishedContent contentItem , string propertyAlias , string framework )
{
Mandate . ParameterNotNullOrEmpty ( propertyAlias , "propertyAlias" ) ;
var view = "Grid/" + framework ;
var prop = contentItem . GetProperty ( propertyAlias ) ;
if ( prop = = null ) throw new NullReferenceException ( "No property type found with alias " + propertyAlias ) ;
var model = prop . Value ;
var asString = model as string ;
2015-01-30 19:04:39 +11:00
if ( asString ! = null & & string . IsNullOrEmpty ( asString ) ) return new MvcHtmlString ( string . Empty ) ;
return html . Partial ( view , model ) ;
}
public static MvcHtmlString GetGridHtml ( this IPublishedProperty property , HtmlHelper html , string framework = "bootstrap3" )
{
var asString = property . Value as string ;
2015-06-16 15:04:31 +02:00
if ( asString ! = null & & string . IsNullOrEmpty ( asString ) ) return new MvcHtmlString ( string . Empty ) ;
2015-01-30 15:48:55 +11:00
2015-01-30 19:04:39 +11:00
var view = "Grid/" + framework ;
return html . Partial ( view , property . Value ) ;
}
public static MvcHtmlString GetGridHtml ( this IPublishedContent contentItem , HtmlHelper html )
{
return GetGridHtml ( contentItem , html , "bodyText" , "bootstrap3" ) ;
}
public static MvcHtmlString GetGridHtml ( this IPublishedContent contentItem , HtmlHelper html , string propertyAlias )
{
Mandate . ParameterNotNullOrEmpty ( propertyAlias , "propertyAlias" ) ;
return GetGridHtml ( contentItem , html , propertyAlias , "bootstrap3" ) ;
}
public static MvcHtmlString GetGridHtml ( this IPublishedContent contentItem , HtmlHelper html , string propertyAlias , string framework )
{
Mandate . ParameterNotNullOrEmpty ( propertyAlias , "propertyAlias" ) ;
var view = "Grid/" + framework ;
var prop = contentItem . GetProperty ( propertyAlias ) ;
if ( prop = = null ) throw new NullReferenceException ( "No property type found with alias " + propertyAlias ) ;
var model = prop . Value ;
var asString = model as string ;
if ( asString ! = null & & string . IsNullOrEmpty ( asString ) ) return new MvcHtmlString ( string . Empty ) ;
2015-01-30 15:48:55 +11:00
return html . Partial ( view , model ) ;
}
2015-10-16 13:36:10 +02:00
[Obsolete("This should not be used, GetGridHtml methods accepting HtmlHelper as a parameter or GetGridHtml extensions on HtmlHelper should be used instead")]
2014-10-07 13:39:18 +02:00
public static MvcHtmlString GetGridHtml ( this IPublishedProperty property , string framework = "bootstrap3" )
2014-05-13 12:36:38 +02:00
{
2015-01-30 15:48:55 +11:00
var asString = property . Value as string ;
2015-06-16 15:04:31 +02:00
if ( asString ! = null & & string . IsNullOrEmpty ( asString ) ) return new MvcHtmlString ( string . Empty ) ;
2015-01-30 19:04:39 +11:00
var htmlHelper = CreateHtmlHelper ( property . Value ) ;
return htmlHelper . GetGridHtml ( property , framework ) ;
2014-05-13 12:36:38 +02:00
}
2015-10-16 13:36:10 +02:00
[Obsolete("This should not be used, GetGridHtml methods accepting HtmlHelper as a parameter or GetGridHtml extensions on HtmlHelper should be used instead")]
2014-10-07 13:39:18 +02:00
public static MvcHtmlString GetGridHtml ( this IPublishedContent contentItem )
2014-05-13 12:36:38 +02:00
{
2014-10-07 13:39:18 +02:00
return GetGridHtml ( contentItem , "bodyText" , "bootstrap3" ) ;
2014-05-13 12:36:38 +02:00
}
2015-10-16 13:36:10 +02:00
[Obsolete("This should not be used, GetGridHtml methods accepting HtmlHelper as a parameter or GetGridHtml extensions on HtmlHelper should be used instead")]
2014-10-07 13:39:18 +02:00
public static MvcHtmlString GetGridHtml ( this IPublishedContent contentItem , string propertyAlias )
2014-05-13 12:36:38 +02:00
{
2015-01-30 15:42:12 +11:00
Mandate . ParameterNotNullOrEmpty ( propertyAlias , "propertyAlias" ) ;
2014-10-07 13:39:18 +02:00
return GetGridHtml ( contentItem , propertyAlias , "bootstrap3" ) ;
2014-05-13 12:36:38 +02:00
}
2015-10-16 13:36:10 +02:00
[Obsolete("This should not be used, GetGridHtml methods accepting HtmlHelper as a parameter or GetGridHtml extensions on HtmlHelper should be used instead")]
2014-10-07 13:39:18 +02:00
public static MvcHtmlString GetGridHtml ( this IPublishedContent contentItem , string propertyAlias , string framework )
2014-05-13 12:36:38 +02:00
{
2015-01-30 15:42:12 +11:00
Mandate . ParameterNotNullOrEmpty ( propertyAlias , "propertyAlias" ) ;
var prop = contentItem . GetProperty ( propertyAlias ) ;
if ( prop = = null ) throw new NullReferenceException ( "No property type found with alias " + propertyAlias ) ;
var model = prop . Value ;
var asString = model as string ;
2015-01-30 19:04:39 +11:00
if ( asString ! = null & & string . IsNullOrEmpty ( asString ) ) return new MvcHtmlString ( string . Empty ) ;
2014-05-13 12:36:38 +02:00
2015-01-30 19:04:39 +11:00
var htmlHelper = CreateHtmlHelper ( model ) ;
return htmlHelper . GetGridHtml ( contentItem , propertyAlias , framework ) ;
2014-05-13 12:36:38 +02:00
}
2015-10-16 13:36:10 +02:00
[Obsolete("This shouldn't need to be used but because the obsolete extension methods above don't have access to the current HtmlHelper, we need to create a fake one, unfortunately however this will not pertain the current views viewdata, tempdata or model state so should not be used")]
2015-01-30 19:04:39 +11:00
private static HtmlHelper CreateHtmlHelper ( object model )
2014-05-13 12:36:38 +02:00
{
2015-01-30 19:04:39 +11:00
var cc = new ControllerContext
2014-05-13 12:36:38 +02:00
{
2015-01-30 19:04:39 +11:00
RequestContext = UmbracoContext . Current . HttpContext . Request . RequestContext
} ;
var viewContext = new ViewContext ( cc , new FakeView ( ) , new ViewDataDictionary ( model ) , new TempDataDictionary ( ) , new StringWriter ( ) ) ;
var htmlHelper = new HtmlHelper ( viewContext , new ViewPage ( ) ) ;
return htmlHelper ;
}
private class FakeView : IView
{
public void Render ( ViewContext viewContext , TextWriter writer )
{
2014-05-13 12:36:38 +02:00
}
}
}
}