Taylohtio/AlertManager/SPSolutions.SharePoint.Aler.../SPSolutions.SharePoint/SPSolutions.SharePoint.WebC.../SPScriptManager.cs

90 lines
2.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web.UI;
namespace SPSolutions.SharePoint.WebControls
{
public class SPScriptManager : Control
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (!base.DesignMode)
{
if (SPScriptManager.GetCurrent(this.Page) != null)
{
throw new Exception("Only one script manager allowed");
}
this.Page.Items[typeof(SPScriptManager)] = this;
}
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
}
private IEnumerable<string> GetCssReferences(Control control)
{
List<string> list = new List<string>();
Type type = control.GetType();
Type typeFromHandle = typeof(SPClientCssResourceAttribute);
object[] customAttributes = type.GetCustomAttributes(typeFromHandle, true);
for (int i = 0; i < customAttributes.Length; i++)
{
SPClientCssResourceAttribute sPClientCssResourceAttribute = (SPClientCssResourceAttribute)customAttributes[i];
list.Add(sPClientCssResourceAttribute.ClientResourceResolver.BuildResourcePath(sPClientCssResourceAttribute.ResourceName));
}
return list;
}
private IEnumerable<string> GetScriptReferences(Control control)
{
List<string> list = new List<string>();
Type type = control.GetType();
Type typeFromHandle = typeof(SPClientScriptResourceAttribute);
object[] customAttributes = type.GetCustomAttributes(typeFromHandle, true);
for (int i = 0; i < customAttributes.Length; i++)
{
SPClientScriptResourceAttribute sPClientScriptResourceAttribute = (SPClientScriptResourceAttribute)customAttributes[i];
list.Add(sPClientScriptResourceAttribute.ClientResourceResolver.BuildResourcePath(sPClientScriptResourceAttribute.ResourceName));
}
return list;
}
public void RegisterClientCss(Control control, string styleSheet)
{
control.Page.ClientScript.RegisterClientScriptBlock(typeof(SPScriptManager), styleSheet, string.Format(CultureInfo.InvariantCulture, "<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" />", new object[]
{
styleSheet
}), false);
}
public void RegisterClientScript(Control control, string scriptPath)
{
control.Page.ClientScript.RegisterClientScriptInclude(typeof(SPScriptManager), scriptPath, scriptPath);
}
public void RegisterCssReference(Control control)
{
foreach (string current in this.GetCssReferences(control))
{
this.RegisterClientCss(this, current);
}
}
public void RegisterScriptReferences(Control control)
{
foreach (string current in this.GetScriptReferences(control))
{
this.RegisterClientScript(this, current);
}
}
public static SPScriptManager GetCurrent(Page page)
{
if (page == null)
{
throw new ArgumentNullException("page");
}
return page.Items[typeof(SPScriptManager)] as SPScriptManager;
}
}
}