Completes: U4-3712 Allow c# property editors to have custom js injected into the js initialization block so they can load in angular controllers

This commit is contained in:
Shannon
2013-11-28 14:27:58 +11:00
parent 478b7cde42
commit 50e10155e7
10 changed files with 162 additions and 70 deletions

View File

@@ -1,25 +1,109 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using ClientDependency.Core;
using ClientDependency.Core.Config;
using ClientDependency.Core.FileRegistration.Providers;
using Newtonsoft.Json.Linq;
using Umbraco.Core.PropertyEditors;
using Umbraco.Web.PropertyEditors;
namespace Umbraco.Web.UI.JavaScript
{
/// <summary>
/// A custom renderer that only outputs a dependency path instead of script tags - for use with the js loader with yepnope
/// </summary>
public class DependencyPathRenderer : StandardRenderer
{
public override string Name
{
get { return "Umbraco.DependencyPathRenderer"; }
}
/// <summary>
/// Used to delimit each dependency so we can split later
/// </summary>
public const string Delimiter = "||||";
/// <summary>
/// Override because the StandardRenderer replaces & with &amp; but we don't want that so we'll reverse it
/// </summary>
/// <param name="allDependencies"></param>
/// <param name="paths"></param>
/// <param name="jsOutput"></param>
/// <param name="cssOutput"></param>
/// <param name="http"></param>
public override void RegisterDependencies(List<IClientDependencyFile> allDependencies, HashSet<IClientDependencyPath> paths, out string jsOutput, out string cssOutput, HttpContextBase http)
{
base.RegisterDependencies(allDependencies, paths, out jsOutput, out cssOutput, http);
jsOutput = jsOutput.Replace("&amp;", "&");
cssOutput = cssOutput.Replace("&amp;", "&");
}
protected override string RenderSingleJsFile(string js, IDictionary<string, string> htmlAttributes)
{
return js + Delimiter;
}
protected override string RenderSingleCssFile(string css, IDictionary<string, string> htmlAttributes)
{
return css + Delimiter;
}
}
internal abstract class AssetInitialization
{
/// <summary>
/// Get all dependencies declared on property editors
/// </summary>
/// <param name="cdfType"></param>
/// <param name="httpContext"></param>
/// <returns></returns>
protected JArray ScanPropertyEditors(ClientDependencyType cdfType, HttpContextBase httpContext)
{
if (httpContext == null) throw new ArgumentNullException("httpContext");
var cdfAttributes =
PropertyEditorResolver.Current.PropertyEditors
.SelectMany(x => x.GetType().GetCustomAttributes<PropertyEditorAssetAttribute>(false))
.Where(x => x.AssetType == cdfType)
.Select(x => x.DependencyFile)
.ToList();
string jsOut;
string cssOut;
var renderer = ClientDependencySettings.Instance.MvcRendererCollection["Umbraco.DependencyPathRenderer"];
renderer.RegisterDependencies(cdfAttributes, new HashSet<IClientDependencyPath>(), out jsOut, out cssOut, httpContext);
var toParse = cdfType == ClientDependencyType.Javascript ? jsOut : cssOut;
var result = new JArray();
//split the result by the delimiter and add to the array
foreach (var u in toParse.Split(new[] { DependencyPathRenderer.Delimiter }, StringSplitOptions.RemoveEmptyEntries))
{
result.Add(u);
}
return result;
}
/// <summary>
/// This will check if we're in release mode, if so it will create a CDF URL to load them all in at once
/// </summary>
/// <param name="fileRefs"></param>
/// <param name="cdfType"></param>
/// <param name="httpContext"></param>
/// <returns></returns>
protected JArray CheckIfReleaseAndOptimized(JArray fileRefs, ClientDependencyType cdfType)
protected JArray CheckIfReleaseAndOptimized(JArray fileRefs, ClientDependencyType cdfType, HttpContextBase httpContext)
{
if (HttpContext.Current != null && HttpContext.Current.IsDebuggingEnabled == false)
if (httpContext == null) throw new ArgumentNullException("httpContext");
if (httpContext.IsDebuggingEnabled == false)
{
return GetOptimized(fileRefs, cdfType);
return GetOptimized(fileRefs, cdfType, httpContext);
}
return fileRefs;
}
@@ -29,8 +113,9 @@ namespace Umbraco.Web.UI.JavaScript
/// </summary>
/// <param name="fileRefs"></param>
/// <param name="cdfType"></param>
/// <param name="httpContext"></param>
/// <returns></returns>
protected JArray GetOptimized(JArray fileRefs, ClientDependencyType cdfType)
protected JArray GetOptimized(JArray fileRefs, ClientDependencyType cdfType, HttpContextBase httpContext)
{
var depenencies = fileRefs.Select(x =>
{
@@ -39,7 +124,7 @@ namespace Umbraco.Web.UI.JavaScript
{
if (Uri.IsWellFormedUriString(asString, UriKind.Relative))
{
var absolute = new Uri(HttpContext.Current.Request.Url, asString);
var absolute = new Uri(httpContext.Request.Url, asString);
return new BasicFile(cdfType) { FilePath = absolute.AbsolutePath };
}
return null;
@@ -49,8 +134,8 @@ namespace Umbraco.Web.UI.JavaScript
var urls = ClientDependencySettings.Instance.DefaultCompositeFileProcessingProvider.ProcessCompositeList(
depenencies,
cdfType,
new HttpContextWrapper(HttpContext.Current));
cdfType,
httpContext);
var result = new JArray();
foreach (var u in urls)

View File

@@ -1,4 +1,5 @@
using ClientDependency.Core;
using System.Web;
using ClientDependency.Core;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
@@ -22,7 +23,7 @@ namespace Umbraco.Web.UI.JavaScript
/// <summary>
/// Processes all found manifest files and outputs yepnope.injectcss calls for all css files found in all manifests
/// </summary>
public string GetStylesheetInitialization()
public string GetStylesheetInitialization(HttpContextBase httpContext)
{
var merged = new JArray();
foreach (var m in _parser.GetManifests())
@@ -31,7 +32,10 @@ namespace Umbraco.Web.UI.JavaScript
}
//now we can optimize if in release mode
merged = CheckIfReleaseAndOptimized(merged, ClientDependencyType.Css);
merged = CheckIfReleaseAndOptimized(merged, ClientDependencyType.Css, httpContext);
//now we need to merge in any found cdf declarations on property editors
ManifestParser.MergeJArrays(merged, ScanPropertyEditors(ClientDependencyType.Css, httpContext));
return ParseMain(merged);
}

View File

@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;
using ClientDependency.Core;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@@ -37,7 +38,7 @@ namespace Umbraco.Web.UI.JavaScript
/// <summary>
/// Processes all found manifest files and outputs the main.js file containing all plugin manifests
/// </summary>
public string GetJavascriptInitialization(JArray umbracoInit, JArray additionalJsFiles = null)
public string GetJavascriptInitialization(HttpContextBase httpContext, JArray umbracoInit, JArray additionalJsFiles = null)
{
foreach (var m in _parser.GetManifests())
{
@@ -51,7 +52,10 @@ namespace Umbraco.Web.UI.JavaScript
}
//now we can optimize if in release mode
umbracoInit = CheckIfReleaseAndOptimized(umbracoInit, ClientDependencyType.Javascript);
umbracoInit = CheckIfReleaseAndOptimized(umbracoInit, ClientDependencyType.Javascript, httpContext);
//now we need to merge in any found cdf declarations on property editors
ManifestParser.MergeJArrays(umbracoInit, ScanPropertyEditors(ClientDependencyType.Javascript, httpContext));
return ParseMain(
umbracoInit.ToString(),