This repository was archived by the owner on Feb 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathuComponentsModule.cs
More file actions
163 lines (136 loc) · 5.36 KB
/
uComponentsModule.cs
File metadata and controls
163 lines (136 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using uComponents.Core;
using uComponents.DataTypes.Shared.Extensions;
using umbraco;
using umbraco.cms.businesslogic.datatype;
using Umbraco.Web;
using umbraco.editorControls;
using Umbraco.Core.IO;
namespace uComponents.UI
{
/// <summary>
/// This module adds the scripts required for the shared ui extensions
/// </summary>
public class uComponentsModule : IHttpModule
{
/// <summary>
/// Include resources from these IResourceSets
/// </summary>
private static List<IResourceSet> sets = new List<IResourceSet> { new UIExtensionResources() };
private static bool _trayPeek;
private static bool _dragAndDrop;
/// <summary>
/// Gets the sets.
/// </summary>
/// <value>The sets.</value>
public static IList<IResourceSet> Sets { get { return sets; } }
#region IHttpModule Members
/// <summary>
/// Disposes of the resources (other than memory) used by the module that implements <see cref="T:System.Web.IHttpModule"/>.
/// </summary>
public void Dispose()
{
}
/// <summary>
/// Initializes a module and prepares it to handle requests.
/// </summary>
/// <param name="context">An <see cref="T:System.Web.HttpApplication"/> that provides access to the methods, properties, and events common to all application objects within an ASP.NET application</param>
public void Init(HttpApplication context)
{
//Item info service
context.BeginRequest += (sender, e) =>
{
ItemInfoService.ProcessRequest((HttpApplication)sender);
};
#region Script injection (for drag & drop etc.)
string umbracoPath = GlobalSettings.Path + "/";
string enableDragAndDrop = ConfigurationManager.AppSettings[Constants.AppKey_DragAndDrop] ?? "true";
string enableTrayPeek = ConfigurationManager.AppSettings[Constants.AppKey_TrayPeek] ?? "true";
bool.TryParse(enableDragAndDrop, out _dragAndDrop);
bool.TryParse(enableTrayPeek, out _trayPeek);
context.PostMapRequestHandler += (sender, e) =>
{
if (context.Request.CurrentExecutionFilePath.StartsWith(umbracoPath, StringComparison.CurrentCultureIgnoreCase)) // Fix for #12951
{
if (context.Request.CurrentExecutionFilePath.Equals(umbracoPath + "default.aspx", StringComparison.InvariantCultureIgnoreCase))
{
// /umbraco/default.aspx forwards to umbraco.aspx with Server.Transfer. That's evil, because then the code below doesn't work because it doesn't have the right page, i.e. umbraco.aspx
// Until a better solution is found this redirect works well.
context.Response.Redirect("umbraco.aspx");
return;
}
var page = HttpContext.Current.CurrentHandler as Page;
if (page != null && !UmbracoContext.Current.InPreviewMode)
{
page.Load += (s2, e2) =>
IncludeScripts(page, (reg) => reg.Filters == null || reg.Filters.Any(x => x.IsMatch(context.Request.CurrentExecutionFilePath)));
}
}
};
#endregion
}
#endregion
/// <summary>
/// Includes the scripts.
/// </summary>
/// <param name="page">The page.</param>
/// <param name="filter">The filter.</param>
protected static void IncludeScripts(Page page, Func<ResourceRegistration, bool> filter)
{
//BUG Fix: Unfortunately, CD doesn't work with all IIS's in all versions
//var loader = ClientDependencyLoader.Instance;
//if (loader == null)
//{
// if (page.Header != null)
// {
// //If the page doesn't have a ClientDependecyLoader already, and doesn't have a head with runat="server" we don't bother
// page.Header.Controls.Add(loader = new ClientDependencyLoader());
// }
//}
//Item info service
page.ClientScript.RegisterClientScriptBlock(page.GetType(), "uComponentsItemInfoService",
"var UC_ITEM_INFO_SERVICE = \"" + ItemInfoService.ServicePath + "\";" +
//Used when inserting images in content editors
"var UC_IMAGE_SERVICE = \"" +
IOHelper.ResolveUrl(SystemDirectories.Umbraco) +
"/controls/Images/ImageViewerUpdater.asmx/UpdateImage\";", true);
var ucSettings = new StringBuilder();
ucSettings.Append("var UC_SETTINGS = { ENABLE_DRAG_AND_DROP: ").Append(_dragAndDrop ? "true" : "false")
.Append(", ENABLE_TRAY_PEEK: ").Append(_trayPeek ? "true" : "false").Append("};");
page.ClientScript.RegisterClientScriptBlock(page.GetType(), "uComponentsSettings",
ucSettings.ToString(), true);
//// int userPriority = 100; // Core umbraco stuff must be added first
var resourcesHtml = new StringBuilder();
resourcesHtml.Append("<!-- uComponents -->");
foreach (var set in sets)
{
foreach (var reg in set.Resources)
{
if (filter(reg))
{
page.RegisterEmbeddedClientResource(
set.GetType(),
reg.ResourceName,
reg.IsScript ? ClientDependencyType.Javascript : ClientDependencyType.Css);
//loader.RegisterDependency(userPriority++, resourceUrl,
// reg.IsScript ? ClientDependencyType.Javascript : ClientDependencyType.Css);
}
}
}
}
/// <summary>
/// Use this method to include drag and drop and shortcut scripts on the page specified
/// </summary>
public void IncludeScripts(Page page)
{
string[] purposes = { "*", "trees", "shortucts" };
IncludeScripts(page, (reg) => reg.Purposes == null || reg.Purposes.Any(x => purposes.Any(p => p == x)));
}
}
}