2012-09-05 09:35:24 +07:00
using System ;
using System.Collections.Generic ;
2016-01-05 14:54:02 +01:00
using System.ComponentModel ;
2012-09-26 13:42:03 +07:00
using System.Linq ;
2012-09-05 09:35:24 +07:00
using System.Text ;
2012-10-31 11:36:22 +06:00
using System.Web ;
2012-09-05 09:35:24 +07:00
using System.Web.Mvc ;
using System.Web.Mvc.Html ;
2013-02-19 06:11:58 +06:00
using System.Web.Routing ;
2012-09-05 09:35:24 +07:00
using Umbraco.Core ;
2013-09-16 12:36:12 +10:00
using Umbraco.Core.Configuration ;
2012-09-27 08:30:35 +07:00
using Umbraco.Core.Dynamics ;
2013-03-06 03:12:04 +06:00
using Umbraco.Core.IO ;
2015-10-22 17:52:58 +02:00
using Umbraco.Core.Models ;
2013-05-13 20:10:28 -10:00
using Umbraco.Core.Profiling ;
2015-10-22 17:52:58 +02:00
using Umbraco.Web.Models ;
2012-09-26 13:42:03 +07:00
using Umbraco.Web.Mvc ;
2015-10-22 17:52:58 +02:00
using Constants = Umbraco . Core . Constants ;
using Member = umbraco . cms . businesslogic . member . Member ;
2012-09-05 09:35:24 +07:00
namespace Umbraco.Web
{
2013-05-13 20:10:28 -10:00
/// <summary>
2012-09-05 09:35:24 +07:00
/// HtmlHelper extensions for use in templates
/// </summary>
public static class HtmlHelperRenderExtensions
{
2013-05-13 20:10:28 -10:00
/// <summary>
/// Renders the markup for the profiler
/// </summary>
/// <param name="helper"></param>
/// <returns></returns>
public static IHtmlString RenderProfiler ( this HtmlHelper helper )
{
return new HtmlString ( ProfilerResolver . Current . Profiler . Render ( ) ) ;
}
2013-03-06 03:12:04 +06:00
/// <summary>
2013-04-29 19:48:11 -10:00
/// Renders a partial view that is found in the specified area
/// </summary>
/// <param name="helper"></param>
/// <param name="partial"></param>
/// <param name="area"></param>
/// <param name="model"></param>
/// <param name="viewData"></param>
/// <returns></returns>
public static MvcHtmlString AreaPartial ( this HtmlHelper helper , string partial , string area , object model = null , ViewDataDictionary viewData = null )
{
var originalArea = helper . ViewContext . RouteData . DataTokens [ "area" ] ;
helper . ViewContext . RouteData . DataTokens [ "area" ] = area ;
var result = helper . Partial ( partial , model , viewData ) ;
helper . ViewContext . RouteData . DataTokens [ "area" ] = originalArea ;
return result ;
}
/// <summary>
2013-03-06 03:12:04 +06:00
/// Will render the preview badge when in preview mode which is not required ever unless the MVC page you are
/// using does not inherit from UmbracoTemplatePage
/// </summary>
/// <param name="helper"></param>
/// <returns></returns>
/// <remarks>
/// See: http://issues.umbraco.org/issue/U4-1614
/// </remarks>
public static MvcHtmlString PreviewBadge ( this HtmlHelper helper )
{
if ( UmbracoContext . Current . InPreviewMode )
{
var htmlBadge =
2013-09-25 19:23:41 +10:00
String . Format ( UmbracoConfig . For . UmbracoSettings ( ) . Content . PreviewBadge ,
2013-03-06 03:12:04 +06:00
IOHelper . ResolveUrl ( SystemDirectories . Umbraco ) ,
IOHelper . ResolveUrl ( SystemDirectories . UmbracoClient ) ,
UmbracoContext . Current . HttpContext . Server . UrlEncode ( UmbracoContext . Current . HttpContext . Request . Path ) ) ;
return new MvcHtmlString ( htmlBadge ) ;
}
return new MvcHtmlString ( "" ) ;
}
2012-10-31 11:36:22 +06:00
public static IHtmlString CachedPartial (
this HtmlHelper htmlHelper ,
string partialViewName ,
object model ,
2012-10-31 11:46:02 +06:00
int cachedSeconds ,
2012-10-31 11:36:22 +06:00
bool cacheByPage = false ,
bool cacheByMember = false ,
2013-09-30 18:23:56 -05:00
ViewDataDictionary viewData = null ,
Func < object , ViewDataDictionary , string > contextualKeyBuilder = null )
2012-10-31 11:36:22 +06:00
{
var cacheKey = new StringBuilder ( partialViewName ) ;
if ( cacheByPage )
{
if ( UmbracoContext . Current = = null )
{
throw new InvalidOperationException ( "Cannot cache by page if the UmbracoContext has not been initialized, this parameter can only be used in the context of an Umbraco request" ) ;
}
cacheKey . AppendFormat ( "{0}-" , UmbracoContext . Current . PageId ) ;
}
if ( cacheByMember )
{
var currentMember = Member . GetCurrentMember ( ) ;
cacheKey . AppendFormat ( "m{0}-" , currentMember = = null ? 0 : currentMember . Id ) ;
2013-09-30 18:23:56 -05:00
}
if ( contextualKeyBuilder ! = null )
{
var contextualKey = contextualKeyBuilder ( model , viewData ) ;
2013-12-19 12:40:41 +11:00
cacheKey . AppendFormat ( "c{0}-" , contextualKey ) ;
2013-09-30 18:23:56 -05:00
}
2012-10-31 11:46:02 +06:00
return ApplicationContext . Current . ApplicationCache . CachedPartialView ( htmlHelper , partialViewName , model , cachedSeconds , cacheKey . ToString ( ) , viewData ) ;
2012-10-31 11:36:22 +06:00
}
2012-09-27 08:30:35 +07:00
public static MvcHtmlString EditorFor < T > ( this HtmlHelper htmlHelper , string templateName = "" , string htmlFieldName = "" , object additionalViewData = null )
where T : new ( )
{
var model = new T ( ) ;
var typedHelper = new HtmlHelper < T > (
htmlHelper . ViewContext . CopyWithModel ( model ) ,
htmlHelper . ViewDataContainer . CopyWithModel ( model ) ) ;
return typedHelper . EditorFor ( x = > model , templateName , htmlFieldName , additionalViewData ) ;
}
/// <summary>
/// A validation summary that lets you pass in a prefix so that the summary only displays for elements
/// containing the prefix. This allows you to have more than on validation summary on a page.
/// </summary>
/// <param name="htmlHelper"></param>
/// <param name="prefix"></param>
/// <param name="excludePropertyErrors"></param>
/// <param name="message"></param>
/// <param name="htmlAttributes"></param>
/// <returns></returns>
public static MvcHtmlString ValidationSummary ( this HtmlHelper htmlHelper ,
string prefix = "" ,
bool excludePropertyErrors = false ,
string message = "" ,
IDictionary < string , object > htmlAttributes = null )
{
if ( prefix . IsNullOrWhiteSpace ( ) )
{
return htmlHelper . ValidationSummary ( excludePropertyErrors , message , htmlAttributes ) ;
}
//if there's a prefix applied, we need to create a new html helper with a filtered ModelState collection so that it only looks for
//specific model state with the prefix.
var filteredHtmlHelper = new HtmlHelper ( htmlHelper . ViewContext , htmlHelper . ViewDataContainer . FilterContainer ( prefix ) ) ;
return filteredHtmlHelper . ValidationSummary ( excludePropertyErrors , message , htmlAttributes ) ;
}
2013-02-19 06:11:58 +06:00
/// <summary>
/// Returns the result of a child action of a strongly typed SurfaceController
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="htmlHelper"></param>
/// <param name="actionName"></param>
/// <returns></returns>
public static IHtmlString Action < T > ( this HtmlHelper htmlHelper , string actionName )
where T : SurfaceController
{
return htmlHelper . Action ( actionName , typeof ( T ) ) ;
}
/// <summary>
/// Returns the result of a child action of a SurfaceController
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="htmlHelper"></param>
/// <param name="actionName"></param>
/// <param name="surfaceType"></param>
/// <returns></returns>
public static IHtmlString Action ( this HtmlHelper htmlHelper , string actionName , Type surfaceType )
{
Mandate . ParameterNotNull ( surfaceType , "surfaceType" ) ;
Mandate . ParameterNotNullOrEmpty ( actionName , "actionName" ) ;
var routeVals = new RouteValueDictionary ( new { area = "" } ) ;
var surfaceController = SurfaceControllerResolver . Current . RegisteredSurfaceControllers
. SingleOrDefault ( x = > x = = surfaceType ) ;
if ( surfaceController = = null )
throw new InvalidOperationException ( "Could not find the surface controller of type " + surfaceType . FullName ) ;
var metaData = PluginController . GetMetadata ( surfaceController ) ;
if ( ! metaData . AreaName . IsNullOrWhiteSpace ( ) )
{
//set the area to the plugin area
2014-01-22 11:02:24 +11:00
if ( routeVals . ContainsKey ( "area" ) )
{
routeVals [ "area" ] = metaData . AreaName ;
}
else
{
routeVals . Add ( "area" , metaData . AreaName ) ;
}
2013-02-19 06:11:58 +06:00
}
return htmlHelper . Action ( actionName , metaData . ControllerName , routeVals ) ;
}
2015-10-22 17:52:58 +02:00
#region GetCropUrl
2012-09-27 08:30:35 +07:00
2016-01-05 14:54:02 +01:00
[Obsolete("Use the UrlHelper.GetCropUrl extension instead")]
[EditorBrowsable(EditorBrowsableState.Never)]
2015-10-22 17:52:58 +02:00
public static IHtmlString GetCropUrl ( this HtmlHelper htmlHelper , IPublishedContent mediaItem , string cropAlias )
{
return new HtmlString ( mediaItem . GetCropUrl ( cropAlias : cropAlias , useCropDimensions : true ) ) ;
}
2016-01-05 14:54:02 +01:00
[Obsolete("Use the UrlHelper.GetCropUrl extension instead")]
[EditorBrowsable(EditorBrowsableState.Never)]
2015-10-22 17:52:58 +02:00
public static IHtmlString GetCropUrl ( this HtmlHelper htmlHelper , IPublishedContent mediaItem , string propertyAlias , string cropAlias )
{
return new HtmlString ( mediaItem . GetCropUrl ( propertyAlias : propertyAlias , cropAlias : cropAlias , useCropDimensions : true ) ) ;
}
2016-01-05 14:54:02 +01:00
[Obsolete("Use the UrlHelper.GetCropUrl extension instead")]
[EditorBrowsable(EditorBrowsableState.Never)]
2015-10-22 17:52:58 +02:00
public static IHtmlString GetCropUrl ( this HtmlHelper htmlHelper ,
IPublishedContent mediaItem ,
int? width = null ,
int? height = null ,
string propertyAlias = Constants . Conventions . Media . File ,
string cropAlias = null ,
int? quality = null ,
ImageCropMode ? imageCropMode = null ,
ImageCropAnchor ? imageCropAnchor = null ,
bool preferFocalPoint = false ,
bool useCropDimensions = false ,
bool cacheBuster = true ,
string furtherOptions = null ,
ImageCropRatioMode ? ratioMode = null ,
bool upScale = true )
{
return
new HtmlString ( mediaItem . GetCropUrl ( width , height , propertyAlias , cropAlias , quality , imageCropMode ,
imageCropAnchor , preferFocalPoint , useCropDimensions , cacheBuster , furtherOptions , ratioMode ,
upScale ) ) ;
}
2016-01-05 14:54:02 +01:00
[Obsolete("Use the UrlHelper.GetCropUrl extension instead")]
[EditorBrowsable(EditorBrowsableState.Never)]
2015-10-22 17:52:58 +02:00
public static IHtmlString GetCropUrl ( this HtmlHelper htmlHelper ,
string imageUrl ,
int? width = null ,
int? height = null ,
string imageCropperValue = null ,
string cropAlias = null ,
int? quality = null ,
ImageCropMode ? imageCropMode = null ,
ImageCropAnchor ? imageCropAnchor = null ,
bool preferFocalPoint = false ,
bool useCropDimensions = false ,
string cacheBusterValue = null ,
string furtherOptions = null ,
ImageCropRatioMode ? ratioMode = null ,
bool upScale = true )
{
return
new HtmlString ( imageUrl . GetCropUrl ( width , height , imageCropperValue , cropAlias , quality , imageCropMode ,
imageCropAnchor , preferFocalPoint , useCropDimensions , cacheBusterValue , furtherOptions , ratioMode ,
upScale ) ) ;
}
#endregion
#region BeginUmbracoForm
/// <summary>
/// Used for rendering out the Form for BeginUmbracoForm
/// </summary>
internal class UmbracoForm : MvcForm
2012-09-05 09:35:24 +07:00
{
2013-09-05 14:01:28 +10:00
/// <summary>
/// Creates an UmbracoForm
/// </summary>
/// <param name="viewContext"></param>
2013-09-06 11:40:37 +10:00
/// <param name="controllerName"></param>
/// <param name="controllerAction"></param>
2013-09-05 14:01:28 +10:00
/// <param name="area"></param>
/// <param name="method"></param>
/// <param name="additionalRouteVals"></param>
public UmbracoForm (
2012-09-05 09:35:24 +07:00
ViewContext viewContext ,
2013-09-06 11:40:37 +10:00
string controllerName ,
string controllerAction ,
2012-09-27 08:30:35 +07:00
string area ,
2013-09-05 14:01:28 +10:00
FormMethod method ,
2012-09-05 09:35:24 +07:00
object additionalRouteVals = null )
: base ( viewContext )
{
2013-09-05 14:01:28 +10:00
_viewContext = viewContext ;
_method = method ;
2013-09-06 11:40:37 +10:00
_encryptedString = UmbracoHelper . CreateEncryptedRouteString ( controllerName , controllerAction , area , additionalRouteVals ) ;
2012-09-05 09:35:24 +07:00
}
2013-09-05 14:01:28 +10:00
private readonly ViewContext _viewContext ;
private readonly FormMethod _method ;
2012-09-05 09:35:24 +07:00
private bool _disposed ;
2013-01-15 20:07:07 +00:00
private readonly string _encryptedString ;
2012-09-05 09:35:24 +07:00
protected override void Dispose ( bool disposing )
{
if ( this . _disposed )
return ;
this . _disposed = true ;
2013-09-06 11:40:37 +10:00
//write out the hidden surface form routes
_viewContext . Writer . Write ( "<input name='ufprt' type='hidden' value='" + _encryptedString + "' />" ) ;
2012-09-05 09:35:24 +07:00
base . Dispose ( disposing ) ;
}
}
2013-09-05 14:01:28 +10:00
/// <summary>
/// Helper method to create a new form to execute in the Umbraco request pipeline against a locally declared controller
/// </summary>
/// <param name="html"></param>
/// <param name="action"></param>
/// <param name="controllerName"></param>
/// <param name="method"></param>
/// <returns></returns>
public static MvcForm BeginUmbracoForm ( this HtmlHelper html , string action , string controllerName , FormMethod method )
{
return html . BeginUmbracoForm ( action , controllerName , null , new Dictionary < string , object > ( ) , method ) ;
}
2012-09-05 09:35:24 +07:00
/// <summary>
/// Helper method to create a new form to execute in the Umbraco request pipeline against a locally declared controller
/// </summary>
/// <param name="html"></param>
/// <param name="action"></param>
/// <param name="controllerName"></param>
/// <returns></returns>
public static MvcForm BeginUmbracoForm ( this HtmlHelper html , string action , string controllerName )
{
return html . BeginUmbracoForm ( action , controllerName , null , new Dictionary < string , object > ( ) ) ;
}
2013-09-05 14:01:28 +10:00
/// <summary>
/// Helper method to create a new form to execute in the Umbraco request pipeline against a locally declared controller
/// </summary>
/// <param name="html"></param>
/// <param name="action"></param>
/// <param name="controllerName"></param>
/// <param name="additionalRouteVals"></param>
/// <param name="method"></param>
/// <returns></returns>
public static MvcForm BeginUmbracoForm ( this HtmlHelper html , string action , string controllerName , object additionalRouteVals , FormMethod method )
{
return html . BeginUmbracoForm ( action , controllerName , additionalRouteVals , new Dictionary < string , object > ( ) , method ) ;
}
2012-09-05 09:35:24 +07:00
/// <summary>
/// Helper method to create a new form to execute in the Umbraco request pipeline against a locally declared controller
/// </summary>
/// <param name="html"></param>
/// <param name="action"></param>
/// <param name="controllerName"></param>
/// <param name="additionalRouteVals"></param>
/// <returns></returns>
2012-09-26 13:42:03 +07:00
public static MvcForm BeginUmbracoForm ( this HtmlHelper html , string action , string controllerName , object additionalRouteVals )
2012-09-05 09:35:24 +07:00
{
return html . BeginUmbracoForm ( action , controllerName , additionalRouteVals , new Dictionary < string , object > ( ) ) ;
}
2013-09-05 14:01:28 +10:00
/// <summary>
/// Helper method to create a new form to execute in the Umbraco request pipeline against a locally declared controller
/// </summary>
/// <param name="html"></param>
/// <param name="action"></param>
/// <param name="controllerName"></param>
/// <param name="additionalRouteVals"></param>
/// <param name="htmlAttributes"></param>
/// <param name="method"></param>
/// <returns></returns>
public static MvcForm BeginUmbracoForm ( this HtmlHelper html , string action , string controllerName ,
object additionalRouteVals ,
object htmlAttributes ,
FormMethod method )
{
2013-11-07 12:50:26 +00:00
return html . BeginUmbracoForm ( action , controllerName , additionalRouteVals , HtmlHelper . AnonymousObjectToHtmlAttributes ( htmlAttributes ) , method ) ;
2013-09-05 14:01:28 +10:00
}
/// <summary>
2012-09-05 09:35:24 +07:00
/// Helper method to create a new form to execute in the Umbraco request pipeline against a locally declared controller
/// </summary>
/// <param name="html"></param>
/// <param name="action"></param>
/// <param name="controllerName"></param>
/// <param name="additionalRouteVals"></param>
/// <param name="htmlAttributes"></param>
/// <returns></returns>
public static MvcForm BeginUmbracoForm ( this HtmlHelper html , string action , string controllerName ,
2012-09-27 08:30:35 +07:00
object additionalRouteVals ,
object htmlAttributes )
2012-09-05 09:35:24 +07:00
{
2013-11-07 12:50:26 +00:00
return html . BeginUmbracoForm ( action , controllerName , additionalRouteVals , HtmlHelper . AnonymousObjectToHtmlAttributes ( htmlAttributes ) ) ;
2012-09-05 09:35:24 +07:00
}
2013-09-05 14:01:28 +10:00
/// <summary>
/// Helper method to create a new form to execute in the Umbraco request pipeline against a locally declared controller
/// </summary>
/// <param name="html"></param>
/// <param name="action"></param>
/// <param name="controllerName"></param>
/// <param name="additionalRouteVals"></param>
/// <param name="htmlAttributes"></param>
/// <param name="method"></param>
/// <returns></returns>
public static MvcForm BeginUmbracoForm ( this HtmlHelper html , string action , string controllerName ,
object additionalRouteVals ,
IDictionary < string , object > htmlAttributes ,
FormMethod method )
{
Mandate . ParameterNotNullOrEmpty ( action , "action" ) ;
Mandate . ParameterNotNullOrEmpty ( controllerName , "controllerName" ) ;
return html . BeginUmbracoForm ( action , controllerName , "" , additionalRouteVals , htmlAttributes , method ) ;
}
/// <summary>
2012-09-05 09:35:24 +07:00
/// Helper method to create a new form to execute in the Umbraco request pipeline against a locally declared controller
/// </summary>
/// <param name="html"></param>
/// <param name="action"></param>
/// <param name="controllerName"></param>
/// <param name="additionalRouteVals"></param>
/// <param name="htmlAttributes"></param>
/// <returns></returns>
public static MvcForm BeginUmbracoForm ( this HtmlHelper html , string action , string controllerName ,
2012-09-27 08:30:35 +07:00
object additionalRouteVals ,
IDictionary < string , object > htmlAttributes )
2012-09-05 09:35:24 +07:00
{
2012-09-26 13:42:03 +07:00
Mandate . ParameterNotNullOrEmpty ( action , "action" ) ;
2012-09-27 08:30:35 +07:00
Mandate . ParameterNotNullOrEmpty ( controllerName , "controllerName" ) ;
2012-09-26 13:42:03 +07:00
2013-02-25 20:17:04 +06:00
return html . BeginUmbracoForm ( action , controllerName , "" , additionalRouteVals , htmlAttributes ) ;
2012-09-26 13:42:03 +07:00
}
2012-09-05 09:35:24 +07:00
2013-09-05 14:01:28 +10:00
/// <summary>
/// Helper method to create a new form to execute in the Umbraco request pipeline to a surface controller plugin
/// </summary>
/// <param name="html"></param>
/// <param name="action"></param>
/// <param name="surfaceType">The surface controller to route to</param>
/// <param name="method"></param>
/// <returns></returns>
public static MvcForm BeginUmbracoForm ( this HtmlHelper html , string action , Type surfaceType , FormMethod method )
{
return html . BeginUmbracoForm ( action , surfaceType , null , new Dictionary < string , object > ( ) , method ) ;
}
2012-09-26 13:42:03 +07:00
/// <summary>
/// Helper method to create a new form to execute in the Umbraco request pipeline to a surface controller plugin
/// </summary>
/// <param name="html"></param>
/// <param name="action"></param>
/// <param name="surfaceType">The surface controller to route to</param>
/// <returns></returns>
public static MvcForm BeginUmbracoForm ( this HtmlHelper html , string action , Type surfaceType )
{
return html . BeginUmbracoForm ( action , surfaceType , null , new Dictionary < string , object > ( ) ) ;
2012-09-05 09:35:24 +07:00
}
2013-09-05 14:01:28 +10:00
/// <summary>
/// Helper method to create a new form to execute in the Umbraco request pipeline to a surface controller plugin
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="html"></param>
/// <param name="action"></param>
/// <param name="method"></param>
/// <returns></returns>
public static MvcForm BeginUmbracoForm < T > ( this HtmlHelper html , string action , FormMethod method )
where T : SurfaceController
{
return html . BeginUmbracoForm ( action , typeof ( T ) , method ) ;
}
2012-09-05 09:35:24 +07:00
/// <summary>
/// Helper method to create a new form to execute in the Umbraco request pipeline to a surface controller plugin
/// </summary>
2012-09-26 13:42:03 +07:00
/// <typeparam name="T"></typeparam>
2012-09-05 09:35:24 +07:00
/// <param name="html"></param>
/// <param name="action"></param>
/// <returns></returns>
2012-09-26 13:42:03 +07:00
public static MvcForm BeginUmbracoForm < T > ( this HtmlHelper html , string action )
2012-09-27 08:30:35 +07:00
where T : SurfaceController
2012-09-05 09:35:24 +07:00
{
2012-09-27 08:30:35 +07:00
return html . BeginUmbracoForm ( action , typeof ( T ) ) ;
2012-09-05 09:35:24 +07:00
}
2013-09-05 14:01:28 +10:00
/// <summary>
/// Helper method to create a new form to execute in the Umbraco request pipeline to a surface controller plugin
/// </summary>
/// <param name="html"></param>
/// <param name="action"></param>
/// <param name="surfaceType">The surface controller to route to</param>
/// <param name="additionalRouteVals"></param>
/// <param name="method"></param>
/// <returns></returns>
public static MvcForm BeginUmbracoForm ( this HtmlHelper html , string action , Type surfaceType ,
object additionalRouteVals , FormMethod method )
{
return html . BeginUmbracoForm ( action , surfaceType , additionalRouteVals , new Dictionary < string , object > ( ) , method ) ;
}
2012-09-05 09:35:24 +07:00
/// <summary>
/// Helper method to create a new form to execute in the Umbraco request pipeline to a surface controller plugin
/// </summary>
/// <param name="html"></param>
/// <param name="action"></param>
2012-09-26 13:42:03 +07:00
/// <param name="surfaceType">The surface controller to route to</param>
2012-09-05 09:35:24 +07:00
/// <param name="additionalRouteVals"></param>
/// <returns></returns>
2012-09-26 13:42:03 +07:00
public static MvcForm BeginUmbracoForm ( this HtmlHelper html , string action , Type surfaceType ,
2012-09-27 08:30:35 +07:00
object additionalRouteVals )
2012-09-05 09:35:24 +07:00
{
2012-09-26 13:42:03 +07:00
return html . BeginUmbracoForm ( action , surfaceType , additionalRouteVals , new Dictionary < string , object > ( ) ) ;
2012-09-05 09:35:24 +07:00
}
2013-09-05 14:01:28 +10:00
/// <summary>
/// Helper method to create a new form to execute in the Umbraco request pipeline to a surface controller plugin
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="html"></param>
/// <param name="action"></param>
/// <param name="additionalRouteVals"></param>
/// <param name="method"></param>
/// <returns></returns>
public static MvcForm BeginUmbracoForm < T > ( this HtmlHelper html , string action , object additionalRouteVals , FormMethod method )
where T : SurfaceController
{
return html . BeginUmbracoForm ( action , typeof ( T ) , additionalRouteVals , method ) ;
}
2012-09-05 09:35:24 +07:00
/// <summary>
/// Helper method to create a new form to execute in the Umbraco request pipeline to a surface controller plugin
/// </summary>
2012-09-26 13:42:03 +07:00
/// <typeparam name="T"></typeparam>
2012-09-05 09:35:24 +07:00
/// <param name="html"></param>
/// <param name="action"></param>
2012-09-26 13:42:03 +07:00
/// <param name="additionalRouteVals"></param>
/// <returns></returns>
public static MvcForm BeginUmbracoForm < T > ( this HtmlHelper html , string action , object additionalRouteVals )
where T : SurfaceController
{
2012-09-27 08:30:35 +07:00
return html . BeginUmbracoForm ( action , typeof ( T ) , additionalRouteVals ) ;
2012-09-26 13:42:03 +07:00
}
2013-09-05 14:01:28 +10:00
/// <summary>
/// Helper method to create a new form to execute in the Umbraco request pipeline to a surface controller plugin
/// </summary>
/// <param name="html"></param>
/// <param name="action"></param>
/// <param name="surfaceType">The surface controller to route to</param>
/// <param name="additionalRouteVals"></param>
/// <param name="htmlAttributes"></param>
/// <param name="method"></param>
/// <returns></returns>
public static MvcForm BeginUmbracoForm ( this HtmlHelper html , string action , Type surfaceType ,
object additionalRouteVals ,
object htmlAttributes ,
FormMethod method )
{
2013-11-07 12:50:26 +00:00
return html . BeginUmbracoForm ( action , surfaceType , additionalRouteVals , HtmlHelper . AnonymousObjectToHtmlAttributes ( htmlAttributes ) , method ) ;
2013-09-05 14:01:28 +10:00
}
/// <summary>
2012-09-26 13:42:03 +07:00
/// Helper method to create a new form to execute in the Umbraco request pipeline to a surface controller plugin
/// </summary>
/// <param name="html"></param>
/// <param name="action"></param>
/// <param name="surfaceType">The surface controller to route to</param>
2012-09-05 09:35:24 +07:00
/// <param name="additionalRouteVals"></param>
/// <param name="htmlAttributes"></param>
/// <returns></returns>
2012-09-26 13:42:03 +07:00
public static MvcForm BeginUmbracoForm ( this HtmlHelper html , string action , Type surfaceType ,
2012-09-27 08:30:35 +07:00
object additionalRouteVals ,
object htmlAttributes )
2012-09-05 09:35:24 +07:00
{
2013-11-07 12:50:26 +00:00
return html . BeginUmbracoForm ( action , surfaceType , additionalRouteVals , HtmlHelper . AnonymousObjectToHtmlAttributes ( htmlAttributes ) ) ;
2012-09-05 09:35:24 +07:00
}
2013-09-05 14:01:28 +10:00
/// <summary>
/// Helper method to create a new form to execute in the Umbraco request pipeline to a surface controller plugin
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="html"></param>
/// <param name="action"></param>
/// <param name="additionalRouteVals"></param>
/// <param name="htmlAttributes"></param>
2013-09-06 11:40:37 +10:00
/// <param name="method"></param>
2013-09-05 14:01:28 +10:00
/// <returns></returns>
public static MvcForm BeginUmbracoForm < T > ( this HtmlHelper html , string action ,
object additionalRouteVals ,
object htmlAttributes ,
FormMethod method )
where T : SurfaceController
{
return html . BeginUmbracoForm ( action , typeof ( T ) , additionalRouteVals , htmlAttributes , method ) ;
}
/// <summary>
2012-09-05 09:35:24 +07:00
/// Helper method to create a new form to execute in the Umbraco request pipeline to a surface controller plugin
/// </summary>
2012-09-26 13:42:03 +07:00
/// <typeparam name="T"></typeparam>
2012-09-05 09:35:24 +07:00
/// <param name="html"></param>
/// <param name="action"></param>
/// <param name="additionalRouteVals"></param>
/// <param name="htmlAttributes"></param>
/// <returns></returns>
2012-09-27 08:30:35 +07:00
public static MvcForm BeginUmbracoForm < T > ( this HtmlHelper html , string action ,
object additionalRouteVals ,
object htmlAttributes )
where T : SurfaceController
2012-09-26 13:42:03 +07:00
{
2012-09-27 08:30:35 +07:00
return html . BeginUmbracoForm ( action , typeof ( T ) , additionalRouteVals , htmlAttributes ) ;
2012-09-26 13:42:03 +07:00
}
2013-09-05 14:01:28 +10:00
/// <summary>
/// Helper method to create a new form to execute in the Umbraco request pipeline to a surface controller plugin
/// </summary>
/// <param name="html"></param>
/// <param name="action"></param>
/// <param name="surfaceType">The surface controller to route to</param>
/// <param name="additionalRouteVals"></param>
/// <param name="htmlAttributes"></param>
/// <param name="method"></param>
/// <returns></returns>
public static MvcForm BeginUmbracoForm ( this HtmlHelper html , string action , Type surfaceType ,
object additionalRouteVals ,
IDictionary < string , object > htmlAttributes ,
FormMethod method )
{
Mandate . ParameterNotNullOrEmpty ( action , "action" ) ;
Mandate . ParameterNotNull ( surfaceType , "surfaceType" ) ;
var area = "" ;
var surfaceController = SurfaceControllerResolver . Current . RegisteredSurfaceControllers
. SingleOrDefault ( x = > x = = surfaceType ) ;
if ( surfaceController = = null )
throw new InvalidOperationException ( "Could not find the surface controller of type " + surfaceType . FullName ) ;
var metaData = PluginController . GetMetadata ( surfaceController ) ;
if ( metaData . AreaName . IsNullOrWhiteSpace ( ) = = false )
{
//set the area to the plugin area
area = metaData . AreaName ;
}
return html . BeginUmbracoForm ( action , metaData . ControllerName , area , additionalRouteVals , htmlAttributes , method ) ;
}
/// <summary>
2012-09-26 13:42:03 +07:00
/// Helper method to create a new form to execute in the Umbraco request pipeline to a surface controller plugin
/// </summary>
/// <param name="html"></param>
/// <param name="action"></param>
/// <param name="surfaceType">The surface controller to route to</param>
/// <param name="additionalRouteVals"></param>
/// <param name="htmlAttributes"></param>
/// <returns></returns>
public static MvcForm BeginUmbracoForm ( this HtmlHelper html , string action , Type surfaceType ,
2012-09-27 08:30:35 +07:00
object additionalRouteVals ,
IDictionary < string , object > htmlAttributes )
2013-09-05 14:01:28 +10:00
{
return html . BeginUmbracoForm ( action , surfaceType , additionalRouteVals , htmlAttributes , FormMethod . Post ) ;
}
2012-09-26 13:42:03 +07:00
2013-09-05 14:01:28 +10:00
/// <summary>
/// Helper method to create a new form to execute in the Umbraco request pipeline to a surface controller plugin
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="html"></param>
/// <param name="action"></param>
/// <param name="additionalRouteVals"></param>
/// <param name="htmlAttributes"></param>
/// <param name="method"></param>
/// <returns></returns>
public static MvcForm BeginUmbracoForm < T > ( this HtmlHelper html , string action ,
object additionalRouteVals ,
IDictionary < string , object > htmlAttributes ,
FormMethod method )
where T : SurfaceController
{
return html . BeginUmbracoForm ( action , typeof ( T ) , additionalRouteVals , htmlAttributes , method ) ;
}
2012-09-26 13:42:03 +07:00
2013-09-05 14:01:28 +10:00
/// <summary>
2012-09-26 13:42:03 +07:00
/// Helper method to create a new form to execute in the Umbraco request pipeline to a surface controller plugin
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="html"></param>
/// <param name="action"></param>
/// <param name="additionalRouteVals"></param>
/// <param name="htmlAttributes"></param>
/// <returns></returns>
2012-09-27 08:30:35 +07:00
public static MvcForm BeginUmbracoForm < T > ( this HtmlHelper html , string action ,
object additionalRouteVals ,
IDictionary < string , object > htmlAttributes )
where T : SurfaceController
2012-09-26 13:42:03 +07:00
{
2012-09-27 08:30:35 +07:00
return html . BeginUmbracoForm ( action , typeof ( T ) , additionalRouteVals , htmlAttributes ) ;
2012-09-26 13:42:03 +07:00
}
2013-09-05 14:01:28 +10:00
/// <summary>
/// Helper method to create a new form to execute in the Umbraco request pipeline to a surface controller plugin
/// </summary>
/// <param name="html"></param>
/// <param name="action"></param>
/// <param name="controllerName"></param>
/// <param name="area"></param>
/// <param name="method"></param>
/// <returns></returns>
public static MvcForm BeginUmbracoForm ( this HtmlHelper html , string action , string controllerName , string area , FormMethod method )
{
return html . BeginUmbracoForm ( action , controllerName , area , null , new Dictionary < string , object > ( ) , method ) ;
}
2012-09-28 08:08:36 +07:00
/// <summary>
/// Helper method to create a new form to execute in the Umbraco request pipeline to a surface controller plugin
/// </summary>
/// <param name="html"></param>
/// <param name="action"></param>
/// <param name="controllerName"></param>
/// <param name="area"></param>
/// <returns></returns>
public static MvcForm BeginUmbracoForm ( this HtmlHelper html , string action , string controllerName , string area )
{
return html . BeginUmbracoForm ( action , controllerName , area , null , new Dictionary < string , object > ( ) ) ;
}
2013-09-05 14:01:28 +10:00
/// <summary>
/// Helper method to create a new form to execute in the Umbraco request pipeline to a surface controller plugin
/// </summary>
/// <param name="html"></param>
/// <param name="action"></param>
/// <param name="controllerName"></param>
/// <param name="area"></param>
/// <param name="additionalRouteVals"></param>
/// <param name="htmlAttributes"></param>
/// <param name="method"></param>
/// <returns></returns>
public static MvcForm BeginUmbracoForm ( this HtmlHelper html , string action , string controllerName , string area ,
object additionalRouteVals ,
IDictionary < string , object > htmlAttributes ,
FormMethod method )
{
Mandate . ParameterNotNullOrEmpty ( action , "action" ) ;
Mandate . ParameterNotNullOrEmpty ( controllerName , "controllerName" ) ;
var formAction = UmbracoContext . Current . OriginalRequestUrl . PathAndQuery ;
return html . RenderForm ( formAction , method , htmlAttributes , controllerName , action , area , additionalRouteVals ) ;
}
/// <summary>
2012-09-26 13:42:03 +07:00
/// Helper method to create a new form to execute in the Umbraco request pipeline to a surface controller plugin
/// </summary>
/// <param name="html"></param>
/// <param name="action"></param>
/// <param name="controllerName"></param>
/// <param name="area"></param>
/// <param name="additionalRouteVals"></param>
/// <param name="htmlAttributes"></param>
/// <returns></returns>
public static MvcForm BeginUmbracoForm ( this HtmlHelper html , string action , string controllerName , string area ,
object additionalRouteVals ,
IDictionary < string , object > htmlAttributes )
2013-09-05 14:01:28 +10:00
{
return html . BeginUmbracoForm ( action , controllerName , area , additionalRouteVals , htmlAttributes , FormMethod . Post ) ;
}
2012-09-05 09:35:24 +07:00
/// <summary>
/// This renders out the form for us
/// </summary>
/// <param name="htmlHelper"></param>
/// <param name="formAction"></param>
/// <param name="method"></param>
/// <param name="htmlAttributes"></param>
/// <param name="surfaceController"></param>
/// <param name="surfaceAction"></param>
2012-09-26 13:42:03 +07:00
/// <param name="area"></param>
2012-09-05 09:35:24 +07:00
/// <param name="additionalRouteVals"></param>
/// <returns></returns>
/// <remarks>
/// This code is pretty much the same as the underlying MVC code that writes out the form
/// </remarks>
private static MvcForm RenderForm ( this HtmlHelper htmlHelper ,
2012-09-27 08:30:35 +07:00
string formAction ,
FormMethod method ,
IDictionary < string , object > htmlAttributes ,
string surfaceController ,
string surfaceAction ,
string area ,
object additionalRouteVals = null )
2012-09-05 09:35:24 +07:00
{
2012-10-02 21:19:21 +05:00
//ensure that the multipart/form-data is added to the html attributes
2013-09-05 14:01:28 +10:00
if ( htmlAttributes . ContainsKey ( "enctype" ) = = false )
2012-10-02 21:19:21 +05:00
{
htmlAttributes . Add ( "enctype" , "multipart/form-data" ) ;
}
2012-09-05 09:35:24 +07:00
var tagBuilder = new TagBuilder ( "form" ) ;
tagBuilder . MergeAttributes ( htmlAttributes ) ;
// action is implicitly generated, so htmlAttributes take precedence.
tagBuilder . MergeAttribute ( "action" , formAction ) ;
// method is an explicit parameter, so it takes precedence over the htmlAttributes.
tagBuilder . MergeAttribute ( "method" , HtmlHelper . GetFormMethodString ( method ) , true ) ;
2013-09-05 14:01:28 +10:00
var traditionalJavascriptEnabled = htmlHelper . ViewContext . ClientValidationEnabled & & htmlHelper . ViewContext . UnobtrusiveJavaScriptEnabled = = false ;
2012-09-05 09:35:24 +07:00
if ( traditionalJavascriptEnabled )
{
// forms must have an ID for client validation
tagBuilder . GenerateId ( "form" + Guid . NewGuid ( ) . ToString ( "N" ) ) ;
}
htmlHelper . ViewContext . Writer . Write ( tagBuilder . ToString ( TagRenderMode . StartTag ) ) ;
//new UmbracoForm:
2013-09-05 14:01:28 +10:00
var theForm = new UmbracoForm ( htmlHelper . ViewContext , surfaceController , surfaceAction , area , method , additionalRouteVals ) ;
2012-09-05 09:35:24 +07:00
if ( traditionalJavascriptEnabled )
{
htmlHelper . ViewContext . FormContext . FormId = tagBuilder . Attributes [ "id" ] ;
}
return theForm ;
}
2012-09-27 08:30:35 +07:00
#endregion
2015-10-16 13:36:10 +02:00
2012-09-27 08:30:35 +07:00
#region Wrap
public static HtmlTagWrapper Wrap ( this HtmlHelper html , string tag , string innerText , params IHtmlTagWrapper [ ] children )
{
var item = html . Wrap ( tag , innerText , ( object ) null ) ;
foreach ( var child in children )
{
item . AddChild ( child ) ;
}
return item ;
}
public static HtmlTagWrapper Wrap ( this HtmlHelper html , string tag , object inner , object anonymousAttributes , params IHtmlTagWrapper [ ] children )
{
string innerText = null ;
if ( inner ! = null & & inner . GetType ( ) ! = typeof ( DynamicNull ) )
{
innerText = string . Format ( "{0}" , inner ) ;
}
var item = html . Wrap ( tag , innerText , anonymousAttributes ) ;
foreach ( var child in children )
{
item . AddChild ( child ) ;
}
return item ;
}
public static HtmlTagWrapper Wrap ( this HtmlHelper html , string tag , object inner )
{
string innerText = null ;
if ( inner ! = null & & inner . GetType ( ) ! = typeof ( DynamicNull ) )
{
innerText = string . Format ( "{0}" , inner ) ;
}
return html . Wrap ( tag , innerText , ( object ) null ) ;
}
public static HtmlTagWrapper Wrap ( this HtmlHelper html , string tag , string innerText , object anonymousAttributes , params IHtmlTagWrapper [ ] children )
{
var wrap = new HtmlTagWrapper ( tag ) ;
if ( anonymousAttributes ! = null )
{
wrap . ReflectAttributesFromAnonymousType ( anonymousAttributes ) ;
}
if ( ! string . IsNullOrWhiteSpace ( innerText ) )
{
wrap . AddChild ( new HtmlTagWrapperTextNode ( innerText ) ) ;
}
foreach ( var child in children )
{
wrap . AddChild ( child ) ;
}
return wrap ;
}
public static HtmlTagWrapper Wrap ( this HtmlHelper html , bool visible , string tag , string innerText , object anonymousAttributes , params IHtmlTagWrapper [ ] children )
{
var item = html . Wrap ( tag , innerText , anonymousAttributes , children ) ;
item . Visible = visible ;
foreach ( var child in children )
{
item . AddChild ( child ) ;
}
return item ;
}
2015-10-16 13:36:10 +02:00
#endregion
#region canvasdesigner
public static IHtmlString EnableCanvasDesigner ( this HtmlHelper html ,
UrlHelper url ,
UmbracoContext umbCtx )
{
return html . EnableCanvasDesigner ( url , umbCtx , string . Empty , string . Empty ) ;
}
public static IHtmlString EnableCanvasDesigner ( this HtmlHelper html ,
UrlHelper url ,
UmbracoContext umbCtx , string canvasdesignerConfigPath )
{
return html . EnableCanvasDesigner ( url , umbCtx , canvasdesignerConfigPath , string . Empty ) ;
}
public static IHtmlString EnableCanvasDesigner ( this HtmlHelper html ,
UrlHelper url ,
UmbracoContext umbCtx , string canvasdesignerConfigPath , string canvasdesignerPalettesPath )
{
var umbracoPath = url . Content ( SystemDirectories . Umbraco ) ;
string previewLink = @"<script src=""{0}/lib/jquery/jquery.min.js"" type=""text/javascript""></script>" +
@"<script src=""{1}"" type=""text/javascript""></script>" +
@"<script src=""{2}"" type=""text/javascript""></script>" +
@"<script type=""text/javascript"">var pageId = '{3}'</script>" +
@"<script src=""{0}/js/canvasdesigner.front.js"" type=""text/javascript""></script>" ;
string noPreviewLinks = @"<link href=""{1}"" type=""text/css"" rel=""stylesheet"" data-title=""canvasdesignerCss"" />" ;
// Get page value
int pageId = umbCtx . PublishedContentRequest . UmbracoPage . PageID ;
string [ ] path = umbCtx . PublishedContentRequest . UmbracoPage . SplitPath ;
string result = string . Empty ;
string cssPath = CanvasDesignerUtility . GetStylesheetPath ( path , false ) ;
if ( umbCtx . InPreviewMode )
{
canvasdesignerConfigPath = string . IsNullOrEmpty ( canvasdesignerConfigPath ) = = false
? canvasdesignerConfigPath
: string . Format ( "{0}/js/canvasdesigner.config.js" , umbracoPath ) ;
canvasdesignerPalettesPath = string . IsNullOrEmpty ( canvasdesignerPalettesPath ) = = false
? canvasdesignerPalettesPath
: string . Format ( "{0}/js/canvasdesigner.palettes.js" , umbracoPath ) ;
if ( string . IsNullOrEmpty ( cssPath ) = = false )
result = string . Format ( noPreviewLinks , cssPath ) + Environment . NewLine ;
result = result + string . Format ( previewLink , umbracoPath , canvasdesignerConfigPath , canvasdesignerPalettesPath , pageId ) ;
}
else
{
// Get css path for current page
if ( string . IsNullOrEmpty ( cssPath ) = = false )
result = string . Format ( noPreviewLinks , cssPath ) ;
}
return new HtmlString ( result ) ;
}
#endregion
2012-09-05 09:35:24 +07:00
2015-10-16 13:36:10 +02:00
}
2013-09-30 18:23:56 -05:00
}