Update: Plugin Web Helpers
Web Helpers included in UI Extensions and WebHandlers
This commit is contained in:
@@ -178,6 +178,7 @@
|
||||
<Compile Include="Plugins\UnknownPluginException.cs" />
|
||||
<Compile Include="Plugins\WebHelper.cs" />
|
||||
<Compile Include="Plugins\PluginWebViewPage.cs" />
|
||||
<Compile Include="Plugins\WebPageHelper.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Tasks\ScheduledTask.cs" />
|
||||
<Compile Include="Tasks\ScheduledTasks.cs" />
|
||||
@@ -224,7 +225,7 @@
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<ProjectExtensions>
|
||||
<VisualStudio>
|
||||
<UserProperties BuildVersion_UseGlobalSettings="False" BuildVersion_DetectChanges="False" BuildVersion_BuildAction="Both" BuildVersion_StartDate="2011/7/1" BuildVersion_BuildVersioningStyle="None.DeltaBaseYear.MonthAndDayStamp.TimeStamp" BuildVersion_UpdateAssemblyVersion="True" BuildVersion_UpdateFileVersion="True" />
|
||||
<UserProperties BuildVersion_UpdateFileVersion="True" BuildVersion_UpdateAssemblyVersion="True" BuildVersion_BuildVersioningStyle="None.DeltaBaseYear.MonthAndDayStamp.TimeStamp" BuildVersion_StartDate="2011/7/1" BuildVersion_BuildAction="Both" BuildVersion_DetectChanges="False" BuildVersion_UseGlobalSettings="False" />
|
||||
</VisualStudio>
|
||||
</ProjectExtensions>
|
||||
<PropertyGroup>
|
||||
|
||||
@@ -13,26 +13,26 @@ namespace Disco.Services.Plugins.Features.UIExtension
|
||||
public abstract class UIExtensionFeature<UIModel> : PluginFeature where UIModel : BaseUIModel
|
||||
{
|
||||
public ControllerContext Context { get; set; }
|
||||
private Lazy<WebHelper> plugin;
|
||||
protected WebHelper Plugin
|
||||
{
|
||||
get
|
||||
{
|
||||
return plugin.Value;
|
||||
}
|
||||
}
|
||||
public UIExtensionFeature()
|
||||
{
|
||||
this.plugin = new Lazy<WebHelper>(new Func<WebHelper>(() => {
|
||||
if (this.Context == null)
|
||||
throw new InvalidOperationException("The Context property is not initialized");
|
||||
|
||||
return new WebHelper(this.Context.HttpContext, this.Manifest.PluginManifest);
|
||||
}));
|
||||
}
|
||||
|
||||
public abstract UIExtensionResult ExecuteAction(ControllerContext context, UIModel model);
|
||||
|
||||
#region Bundles
|
||||
public void IncludeStyleSheet(string Resource)
|
||||
{
|
||||
if (this.Context == null)
|
||||
throw new NullReferenceException("This method can only be called when a Context is available");
|
||||
|
||||
this.Context.HttpContext.IncludeStyleSheetResource(Resource, this.Manifest.PluginManifest);
|
||||
}
|
||||
public void IncludeScript(string Resource)
|
||||
{
|
||||
if (this.Context == null)
|
||||
throw new NullReferenceException("This method can only be called when a Context is available");
|
||||
|
||||
this.Context.HttpContext.IncludeScriptResource(Resource, this.Manifest.PluginManifest);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ActionResults
|
||||
|
||||
protected LiteralResult Literal(string Content)
|
||||
|
||||
@@ -457,6 +457,17 @@ namespace Disco.Services.Plugins
|
||||
return string.Format("/Plugin/{0}", HttpUtility.UrlEncode(this.Id));
|
||||
}
|
||||
}
|
||||
public string WebActionUrl(string Action)
|
||||
{
|
||||
if (!HasWebHandler)
|
||||
throw new NotSupportedException("This plugin doesn't have a web handler");
|
||||
|
||||
var url = UrlHelper.GenerateUrl("Plugin", null, null,
|
||||
new RouteValueDictionary(new Dictionary<string, object>() { { "PluginId", this.Id }, { "PluginAction", Action } }),
|
||||
RouteTable.Routes, HttpContext.Current.Request.RequestContext, false);
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
public Tuple<string, string> WebResourcePath(string Resource)
|
||||
{
|
||||
|
||||
@@ -17,6 +17,19 @@ namespace Disco.Services.Plugins
|
||||
public PluginManifest Manifest { get; set; }
|
||||
public Controller HostController { get; set; }
|
||||
protected DiscoDataContext Database;
|
||||
private Lazy<WebHelper> plugin;
|
||||
protected WebHelper Plugin
|
||||
{
|
||||
get
|
||||
{
|
||||
return plugin.Value;
|
||||
}
|
||||
}
|
||||
|
||||
public PluginWebHandler()
|
||||
{
|
||||
this.plugin = new Lazy<WebHelper>(new Func<WebHelper>(() => new WebHelper(this.HostController.HttpContext, this.Manifest)));
|
||||
}
|
||||
|
||||
public void OnActionExecuting()
|
||||
{
|
||||
@@ -70,19 +83,6 @@ namespace Disco.Services.Plugins
|
||||
|
||||
#endregion
|
||||
|
||||
#region Bundles
|
||||
|
||||
public void IncludeStyleSheet(string Resource)
|
||||
{
|
||||
this.HostController.HttpContext.IncludeStyleSheetResource(Resource, this.Manifest);
|
||||
}
|
||||
public void IncludeScript(string Resource)
|
||||
{
|
||||
this.HostController.HttpContext.IncludeScriptResource(Resource, this.Manifest);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Action Results
|
||||
|
||||
#region Compiled View
|
||||
@@ -124,7 +124,7 @@ namespace Disco.Services.Plugins
|
||||
{
|
||||
return this.CompiledView<ViewType>();
|
||||
}
|
||||
|
||||
|
||||
[Obsolete("Use Generic Methods")]
|
||||
public ActionResult CompiledView(Type CompiledViewType, object Model, bool UseDiscoLayout)
|
||||
{
|
||||
|
||||
@@ -8,14 +8,14 @@ namespace Disco.Services.Plugins
|
||||
{
|
||||
public abstract class PluginWebViewPage<T> : Disco.Services.Web.WebViewPage<T>
|
||||
{
|
||||
public WebHelper<T> Plugin { get; private set; }
|
||||
public WebPageHelper<T> Plugin { get; private set; }
|
||||
|
||||
public PluginWebViewPage()
|
||||
{
|
||||
var self = this.GetType();
|
||||
var manifest = Plugins.GetPlugin(self.Assembly);
|
||||
|
||||
this.Plugin = new WebHelper<T>(this, manifest);
|
||||
this.Plugin = new WebPageHelper<T>(this, manifest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,117 +8,30 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Mvc.Html;
|
||||
using System.Web.Routing;
|
||||
using System.Web.WebPages;
|
||||
|
||||
namespace Disco.Services.Plugins
|
||||
{
|
||||
public class WebHelper<T>
|
||||
public class WebHelper
|
||||
{
|
||||
private WebViewPage<T> ViewPage { get; set; }
|
||||
protected HttpContextBase Context { get; set; }
|
||||
public PluginManifest Manifest { get; private set; }
|
||||
|
||||
public WebHelper(WebViewPage<T> ViewPage, PluginManifest manifest)
|
||||
public WebHelper(HttpContextBase Context, PluginManifest Manifest)
|
||||
{
|
||||
this.ViewPage = ViewPage;
|
||||
this.Manifest = manifest;
|
||||
this.Context = Context;
|
||||
this.Manifest = Manifest;
|
||||
}
|
||||
|
||||
#region Html Helpers
|
||||
|
||||
#region Form Helpers
|
||||
public MvcForm BeginForm(string Action, FormMethod Method, bool MultipartEncoding, IDictionary<string, object> HtmlAttributes)
|
||||
{
|
||||
if (string.IsNullOrEmpty(Action))
|
||||
throw new ArgumentNullException("PluginAction");
|
||||
|
||||
var url = ActionUrl(Action);
|
||||
|
||||
return BeginForm_Helper(url.ToString(), Method, MultipartEncoding, HtmlAttributes);
|
||||
}
|
||||
public MvcForm BeginForm(string Action, FormMethod Method, bool MultipartEncoding)
|
||||
{
|
||||
return BeginForm(Action, Method, MultipartEncoding, null);
|
||||
}
|
||||
public MvcForm BeginForm(string Action, FormMethod Method, IDictionary<string, object> HtmlAttributes)
|
||||
{
|
||||
return BeginForm(Action, Method, false, HtmlAttributes);
|
||||
}
|
||||
public MvcForm BeginForm(string Action, FormMethod Method)
|
||||
{
|
||||
return BeginForm(Action, Method, false, null);
|
||||
}
|
||||
public MvcForm BeginForm(string Action)
|
||||
{
|
||||
return BeginForm(Action, FormMethod.Get, false, null);
|
||||
}
|
||||
private MvcForm BeginForm_Helper(string FormAction, FormMethod Method, bool MultipartEncoding, IDictionary<string, object> HtmlAttributes)
|
||||
{
|
||||
TagBuilder builder = new TagBuilder("form");
|
||||
builder.MergeAttributes(HtmlAttributes);
|
||||
if (MultipartEncoding)
|
||||
builder.MergeAttribute("enctype", "multipart/form-data");
|
||||
builder.MergeAttribute("action", FormAction);
|
||||
builder.MergeAttribute("method", HtmlHelper.GetFormMethodString(Method), true);
|
||||
|
||||
bool useClientValidation = ViewPage.ViewContext.ClientValidationEnabled && !ViewPage.ViewContext.UnobtrusiveJavaScriptEnabled;
|
||||
if (useClientValidation)
|
||||
{
|
||||
object lastFormNumber = ViewPage.ViewContext.HttpContext.Items["DiscoPluginLastFormNum"];
|
||||
int num = (lastFormNumber != null) ? (((int)lastFormNumber) + 1) : 1000;
|
||||
ViewPage.ViewContext.HttpContext.Items["DiscoPluginLastFormNum"] = num;
|
||||
|
||||
builder.GenerateId(string.Format(CultureInfo.InvariantCulture, "form{0}", new object[] { num }));
|
||||
}
|
||||
ViewPage.ViewContext.Writer.Write(builder.ToString(TagRenderMode.StartTag));
|
||||
MvcForm form = new MvcForm(ViewPage.ViewContext);
|
||||
if (useClientValidation)
|
||||
{
|
||||
ViewPage.ViewContext.FormContext.FormId = builder.Attributes["id"];
|
||||
}
|
||||
return form;
|
||||
}
|
||||
#endregion
|
||||
|
||||
public void IncludeStyleSheet(string Resource)
|
||||
{
|
||||
ViewPage.Context.IncludeStyleSheetResource(Resource, this.Manifest);
|
||||
Context.IncludeStyleSheetResource(Resource, this.Manifest);
|
||||
}
|
||||
public void IncludeJavaScript(string Resource)
|
||||
{
|
||||
ViewPage.Context.IncludeScriptResource(Resource, this.Manifest);
|
||||
Context.IncludeScriptResource(Resource, this.Manifest);
|
||||
}
|
||||
|
||||
public HtmlString PartialCompiled<ViewType>(object Model) where ViewType : WebViewPage
|
||||
{
|
||||
using (System.IO.StringWriter writer = new StringWriter(CultureInfo.CurrentCulture))
|
||||
{
|
||||
RenderPartialCompiled<ViewType>(writer, Model);
|
||||
|
||||
return new HtmlString(writer.ToString());
|
||||
}
|
||||
}
|
||||
public HtmlString PartialCompiled<ViewType>() where ViewType : WebViewPage
|
||||
{
|
||||
return PartialCompiled<ViewType>(null);
|
||||
}
|
||||
private void RenderPartialCompiled<ViewType>(TextWriter Writer, object Model)
|
||||
{
|
||||
if (Writer == null)
|
||||
throw new ArgumentNullException("Writer");
|
||||
WebViewPage page = Activator.CreateInstance(typeof(ViewType)) as WebViewPage;
|
||||
if (page == null)
|
||||
throw new InvalidOperationException("Invalid View Type");
|
||||
page.ViewContext = ViewPage.ViewContext;
|
||||
page.ViewData = new ViewDataDictionary(Model);
|
||||
page.InitHelpers();
|
||||
HttpContextBase httpContext = ViewPage.ViewContext.HttpContext;
|
||||
page.ExecutePageHierarchy(new WebPageContext(httpContext, null, Model), Writer, null);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Urls
|
||||
|
||||
public HtmlString ConfigurationUrl()
|
||||
@@ -130,7 +43,7 @@ namespace Disco.Services.Plugins
|
||||
|
||||
public HtmlString ActionUrl(string Action)
|
||||
{
|
||||
var url = GenerateUrl("Plugin", new Dictionary<string, object>() { { "PluginId", Manifest.Id }, { "PluginAction", Action } });
|
||||
var url = Manifest.WebActionUrl(Action);
|
||||
|
||||
return new HtmlString(url);
|
||||
}
|
||||
@@ -155,7 +68,7 @@ namespace Disco.Services.Plugins
|
||||
#region Helpers
|
||||
private string GenerateUrl(string RouteName, RouteValueDictionary RouteValues)
|
||||
{
|
||||
return UrlHelper.GenerateUrl(RouteName, null, null, RouteValues, RouteTable.Routes, ViewPage.Request.RequestContext, false);
|
||||
return UrlHelper.GenerateUrl(RouteName, null, null, RouteValues, RouteTable.Routes, Context.Request.RequestContext, false);
|
||||
}
|
||||
private string GenerateUrl(string RouteName, IDictionary<string, object> RouteValues)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Mvc.Html;
|
||||
using System.Web.WebPages;
|
||||
|
||||
namespace Disco.Services.Plugins
|
||||
{
|
||||
public class WebPageHelper<T> : WebHelper
|
||||
{
|
||||
protected WebViewPage<T> ViewPage { get; set; }
|
||||
|
||||
public WebPageHelper(WebViewPage<T> ViewPage, PluginManifest Manifest)
|
||||
: base(ViewPage.Context, Manifest)
|
||||
{
|
||||
this.ViewPage = ViewPage;
|
||||
}
|
||||
|
||||
#region Html Helpers
|
||||
|
||||
#region Form Helpers
|
||||
public MvcForm BeginForm(string Action, FormMethod Method, bool MultipartEncoding, IDictionary<string, object> HtmlAttributes)
|
||||
{
|
||||
if (string.IsNullOrEmpty(Action))
|
||||
throw new ArgumentNullException("PluginAction");
|
||||
|
||||
var url = ActionUrl(Action);
|
||||
|
||||
return BeginForm_Helper(url.ToString(), Method, MultipartEncoding, HtmlAttributes);
|
||||
}
|
||||
public MvcForm BeginForm(string Action, FormMethod Method, bool MultipartEncoding)
|
||||
{
|
||||
return BeginForm(Action, Method, MultipartEncoding, null);
|
||||
}
|
||||
public MvcForm BeginForm(string Action, FormMethod Method, IDictionary<string, object> HtmlAttributes)
|
||||
{
|
||||
return BeginForm(Action, Method, false, HtmlAttributes);
|
||||
}
|
||||
public MvcForm BeginForm(string Action, FormMethod Method)
|
||||
{
|
||||
return BeginForm(Action, Method, false, null);
|
||||
}
|
||||
public MvcForm BeginForm(string Action)
|
||||
{
|
||||
return BeginForm(Action, FormMethod.Get, false, null);
|
||||
}
|
||||
private MvcForm BeginForm_Helper(string FormAction, FormMethod Method, bool MultipartEncoding, IDictionary<string, object> HtmlAttributes)
|
||||
{
|
||||
TagBuilder builder = new TagBuilder("form");
|
||||
builder.MergeAttributes(HtmlAttributes);
|
||||
if (MultipartEncoding)
|
||||
builder.MergeAttribute("enctype", "multipart/form-data");
|
||||
builder.MergeAttribute("action", FormAction);
|
||||
builder.MergeAttribute("method", HtmlHelper.GetFormMethodString(Method), true);
|
||||
|
||||
bool useClientValidation = ViewPage.ViewContext.ClientValidationEnabled && !ViewPage.ViewContext.UnobtrusiveJavaScriptEnabled;
|
||||
if (useClientValidation)
|
||||
{
|
||||
object lastFormNumber = ViewPage.ViewContext.HttpContext.Items["DiscoPluginLastFormNum"];
|
||||
int num = (lastFormNumber != null) ? (((int)lastFormNumber) + 1) : 1000;
|
||||
ViewPage.ViewContext.HttpContext.Items["DiscoPluginLastFormNum"] = num;
|
||||
|
||||
builder.GenerateId(string.Format(CultureInfo.InvariantCulture, "form{0}", new object[] { num }));
|
||||
}
|
||||
ViewPage.ViewContext.Writer.Write(builder.ToString(TagRenderMode.StartTag));
|
||||
MvcForm form = new MvcForm(ViewPage.ViewContext);
|
||||
if (useClientValidation)
|
||||
{
|
||||
ViewPage.ViewContext.FormContext.FormId = builder.Attributes["id"];
|
||||
}
|
||||
return form;
|
||||
}
|
||||
#endregion
|
||||
|
||||
public HtmlString PartialCompiled<ViewType>(object Model) where ViewType : WebViewPage
|
||||
{
|
||||
using (System.IO.StringWriter writer = new StringWriter(CultureInfo.CurrentCulture))
|
||||
{
|
||||
RenderPartialCompiled<ViewType>(writer, Model);
|
||||
|
||||
return new HtmlString(writer.ToString());
|
||||
}
|
||||
}
|
||||
public HtmlString PartialCompiled<ViewType>() where ViewType : WebViewPage
|
||||
{
|
||||
return PartialCompiled<ViewType>(null);
|
||||
}
|
||||
private void RenderPartialCompiled<ViewType>(TextWriter Writer, object Model)
|
||||
{
|
||||
if (Writer == null)
|
||||
throw new ArgumentNullException("Writer");
|
||||
WebViewPage page = Activator.CreateInstance(typeof(ViewType)) as WebViewPage;
|
||||
if (page == null)
|
||||
throw new InvalidOperationException("Invalid View Type");
|
||||
page.ViewContext = ViewPage.ViewContext;
|
||||
page.ViewData = new ViewDataDictionary(Model);
|
||||
page.InitHelpers();
|
||||
HttpContextBase httpContext = ViewPage.ViewContext.HttpContext;
|
||||
page.ExecutePageHierarchy(new WebPageContext(httpContext, null, Model), Writer, null);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user