2013-11-28 10:37:08 +11:00
|
|
|
|
using System.Collections.Generic;
|
2014-03-07 10:40:27 +11:00
|
|
|
|
using System.Globalization;
|
2013-11-27 18:42:35 +11:00
|
|
|
|
using System.IO;
|
2013-08-12 15:06:12 +02:00
|
|
|
|
using System.Text.RegularExpressions;
|
2013-11-28 14:27:58 +11:00
|
|
|
|
using System.Web;
|
2013-11-27 18:42:35 +11:00
|
|
|
|
using ClientDependency.Core;
|
2014-03-07 10:40:27 +11:00
|
|
|
|
using ClientDependency.Core.Config;
|
2013-08-12 15:06:12 +02:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
2014-03-07 10:40:27 +11:00
|
|
|
|
using Umbraco.Core;
|
|
|
|
|
|
using Umbraco.Core.Configuration;
|
2013-08-12 15:06:12 +02:00
|
|
|
|
using Umbraco.Core.IO;
|
|
|
|
|
|
using Umbraco.Core.Manifest;
|
2013-11-27 18:42:35 +11:00
|
|
|
|
using System.Linq;
|
2013-08-12 15:06:12 +02:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.UI.JavaScript
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Reads from all defined manifests and ensures that any of their initialization is output with the
|
|
|
|
|
|
/// main Umbraco initialization output.
|
|
|
|
|
|
/// </summary>
|
2013-11-28 10:37:08 +11:00
|
|
|
|
internal class JsInitialization : AssetInitialization
|
2013-08-12 15:06:12 +02:00
|
|
|
|
{
|
|
|
|
|
|
private readonly ManifestParser _parser;
|
|
|
|
|
|
|
|
|
|
|
|
public JsInitialization(ManifestParser parser)
|
|
|
|
|
|
{
|
|
|
|
|
|
_parser = parser;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//used to strip comments
|
|
|
|
|
|
internal static readonly Regex Comments = new Regex("(/\\*.*\\*/)", RegexOptions.Compiled);
|
|
|
|
|
|
//used for dealing with js functions inside of json (which is not a supported json syntax)
|
|
|
|
|
|
private const string PrefixJavaScriptObject = "@@@@";
|
|
|
|
|
|
private static readonly Regex JsFunctionParser = new Regex(string.Format("(\"{0}(.*?)\")+", PrefixJavaScriptObject),
|
|
|
|
|
|
RegexOptions.Multiline
|
|
|
|
|
|
| RegexOptions.CultureInvariant
|
|
|
|
|
|
| RegexOptions.Compiled);
|
|
|
|
|
|
//used to replace the tokens in the js main
|
|
|
|
|
|
private static readonly Regex Token = new Regex("(\"##\\w+?##\")", RegexOptions.Compiled);
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Processes all found manifest files and outputs the main.js file containing all plugin manifests
|
|
|
|
|
|
/// </summary>
|
2013-11-28 14:27:58 +11:00
|
|
|
|
public string GetJavascriptInitialization(HttpContextBase httpContext, JArray umbracoInit, JArray additionalJsFiles = null)
|
2014-01-15 17:18:23 +11:00
|
|
|
|
{
|
|
|
|
|
|
var result = GetJavascriptInitializationArray(httpContext, umbracoInit, additionalJsFiles);
|
2014-02-19 00:15:22 +01:00
|
|
|
|
|
2014-05-05 12:59:34 +02:00
|
|
|
|
return ParseMain(
|
|
|
|
|
|
result.ToString(),
|
|
|
|
|
|
IOHelper.ResolveUrl(SystemDirectories.Umbraco));
|
2014-01-15 17:18:23 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public JArray GetJavascriptInitializationArray(HttpContextBase httpContext, JArray umbracoInit, JArray additionalJsFiles = null)
|
2013-08-12 15:06:12 +02:00
|
|
|
|
{
|
|
|
|
|
|
foreach (var m in _parser.GetManifests())
|
|
|
|
|
|
{
|
|
|
|
|
|
ManifestParser.MergeJArrays(umbracoInit, m.JavaScriptInitialize);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-10-15 16:18:55 +11:00
|
|
|
|
//merge in the additional ones specified if there are any
|
|
|
|
|
|
if (additionalJsFiles != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
ManifestParser.MergeJArrays(umbracoInit, additionalJsFiles);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-11-27 18:42:35 +11:00
|
|
|
|
//now we can optimize if in release mode
|
2015-03-18 19:06:41 +11:00
|
|
|
|
umbracoInit = OptimizeAssetCollection(umbracoInit, ClientDependencyType.Javascript, httpContext);
|
2013-11-28 14:27:58 +11:00
|
|
|
|
|
|
|
|
|
|
//now we need to merge in any found cdf declarations on property editors
|
|
|
|
|
|
ManifestParser.MergeJArrays(umbracoInit, ScanPropertyEditors(ClientDependencyType.Javascript, httpContext));
|
2013-11-27 18:42:35 +11:00
|
|
|
|
|
2014-01-15 17:18:23 +11:00
|
|
|
|
return umbracoInit;
|
2013-08-12 15:06:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns the default config as a JArray
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
internal static JArray GetDefaultInitialization()
|
|
|
|
|
|
{
|
|
|
|
|
|
var init = Resources.JsInitialize;
|
2013-11-27 18:42:35 +11:00
|
|
|
|
var deserialized = JsonConvert.DeserializeObject<JArray>(init);
|
|
|
|
|
|
var result = new JArray();
|
|
|
|
|
|
foreach (var j in deserialized.Where(j => j.Type == JTokenType.String))
|
|
|
|
|
|
{
|
|
|
|
|
|
result.Add(j);
|
|
|
|
|
|
}
|
|
|
|
|
|
return result;
|
2013-08-12 15:06:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Parses the JsResources.Main and replaces the replacement tokens accordingly.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="replacements"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
internal static string ParseMain(params string[] replacements)
|
|
|
|
|
|
{
|
|
|
|
|
|
var count = 0;
|
|
|
|
|
|
|
|
|
|
|
|
return Token.Replace(Resources.Main, match =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var replaced = replacements[count];
|
|
|
|
|
|
|
|
|
|
|
|
//we need to cater for the special syntax when we have js function() objects contained in the json
|
|
|
|
|
|
var jsFunctionParsed = JsFunctionParser.Replace(replaced, "$2");
|
|
|
|
|
|
|
|
|
|
|
|
count++;
|
|
|
|
|
|
|
|
|
|
|
|
return jsFunctionParsed;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|