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

194 lines
7.5 KiB
C#

using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using SPSolutions.Web;
using System;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SPSolutions.SharePoint.WebControls
{
public abstract class SelectorEntityEditorAdapter<T> : ContextSelector<T>, IPostBackEventHandler
{
private static readonly string SelectedCommand = "selected";
private HiddenField HiddenSelectedId;
public event EventHandler Selected;
//{
// [MethodImpl(MethodImplOptions.Synchronized)]
// add
// {
// this.Selected += value;// (EventHandler)Delegate.Combine(this.Selected, value);
// //this.Selected += SelectorEntityEditorAdapter_Selected;
// }
// [MethodImpl(MethodImplOptions.Synchronized)]
// remove
// {
// this.Selected -= value;
// //this.Selected += (EventHandler)Delegate.Remove(this.Selected, value);
// }
//}
void SelectorEntityEditorAdapter_Selected(object sender, EventArgs e)
{
throw new NotImplementedException();
}
public abstract Type PickerDialogType
{
get;
}
public string SelectedId
{
get
{
return this.HiddenSelectedId.Value;
}
}
public bool UseCookieInitialization
{
get
{
return this.ViewState["Microsoft.SharePoint.SPAlertTemplate"] == null || (bool)this.ViewState["Microsoft.SharePoint.SPAlertTemplate"];
}
set
{
this.ViewState["Microsoft.SharePoint.SPAlertTemplate"] = value;
}
}
internal static string CookieCurrentId
{
get
{
PropertyInfo property = typeof(ContextSelector<T>).GetProperty("CookieCurrentId", BindingFlags.Static | BindingFlags.NonPublic);
return (string)property.GetValue(null, null);
}
set
{
PropertyInfo property = typeof(ContextSelector<T>).GetProperty("CookieCurrentId", BindingFlags.Static | BindingFlags.NonPublic);
property.SetValue(null, value, null);
}
}
protected override string NavigateUrl
{
get
{
return string.Format("/_layouts/15/picker.aspx?PickerDialogType={0}", HttpUtility.UrlEncode(this.PickerDialogType.AssemblyQualifiedName));
}
}
protected override void CreateChildControls()
{
this.HiddenSelectedId = new HiddenField();
this.HiddenSelectedId.ID = "HiddenSelectedId";
this.Controls.Add(this.HiddenSelectedId);
base.CreateChildControls();
}
protected override void RenderContents(HtmlTextWriter output)
{
this.HiddenSelectedId.RenderControl(output);
base.RenderContents(output);
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (base.AllowChange)
{
var text1 = "SelectContext_" + this.ID;
string text = "CallbackWrapper_" + this.ID;
string postBackEventReference = this.Page.ClientScript.GetPostBackEventReference(this, SelectorEntityEditorAdapter<T>.SelectedCommand);
string text2;
if (base.CurrentItem == null)
{
text2 = this.NavigateUrl;
}
else
{
text2 = HttpUtil.AppendUrlQueryString(this.NavigateUrl, string.Format("{0}={1}", this.Key, base.CurrentId));
}
string script = string.Concat(new string[]
{
"<script language='javascript'>\r\nfunction ",
text,
"(xml)\r\n{\r\nvar xmlDoc;\r\nif(xml==null)\r\nreturn;\r\nif(document.implementation && document.implementation.createDocument)\r\n{\r\nxmlDoc=(new DOMParser()).parseFromString(xml, \"text/xml\");\r\n}\r\nelse\r\n{\r\nxmlDoc=new ActiveXObject(\"Microsoft.XMLDOM\");\r\nxmlDoc.async=false;\r\nxmlDoc.loadXML(xml);\r\n}\r\nvar entities=xmlDoc.documentElement;\r\nif(entities.childNodes.length == 0)\r\nreturn;\r\nvar entity = entities.childNodes[0];\r\nvar hiddenSelectedId = document.getElementById('",
this.HiddenSelectedId.ClientID,
"');\r\nhiddenSelectedId.value = entity.getAttribute(\"Key\");\r\n",
postBackEventReference,
";\r\n}\r\n</script>"
});
this.Page.ClientScript.RegisterClientScriptBlock(typeof(ContextSelectorEntityEditorAdapter<T>), text, script);
string text3 = string.Format(CultureInfo.InvariantCulture, "commonShowModalDialog('{0}','resizable: yes; status: no; scroll: no; help: no; center: yes; dialogWidth : {1}pt; dialogHeight : {2}pt;',{3});", new object[]
{
SPHttpUtility.EcmaScriptStringLiteralEncode(text2),
base.DialogWidth,
base.DialogHeight,
SPHttpUtility.EcmaScriptStringLiteralEncode(text)
});
this.ChangeItem.ClientOnClickScript = (SPHttpUtility.NoEncode(text3));
PickerDialog.RegisterPickerReturnValueHiddenFields(this.Page);
}
}
protected override void OnLoad(EventArgs e)
{
if (!this.Page.IsPostBack)
{
if (base.CurrentItem != null)
{
T currentItem = base.CurrentItem;
if (!currentItem.Equals(default(T)))
{
return;
}
}
string idFromQueryString = this.IdFromQueryString;
if (idFromQueryString == null)
{
if (string.IsNullOrEmpty(base.ClearSelectionText))
{
if (this.UseCookieInitialization)
{
base.CurrentId = (SelectorEntityEditorAdapter<T>.CookieCurrentId);
return;
}
if (base.CurrentItem != null)
{
T currentItem2 = base.CurrentItem;
if (!currentItem2.Equals(default(T)))
{
return;
}
}
base.CurrentId = (this.DefaultSelectionId());
return;
}
}
else
{
base.CurrentId = idFromQueryString;
}
}
}
public void RaisePostBackEvent(string eventArgument)
{
if (eventArgument == null)
{
throw new ArgumentNullException("eventArgument");
}
string a = eventArgument.ToLower(CultureInfo.InvariantCulture);
if (a == SelectorEntityEditorAdapter<T>.SelectedCommand)
{
this.OnSelected(new EventArgs());
}
}
protected virtual void OnSelected(EventArgs e)
{
base.CurrentId = (this.SelectedId);
if (this.Selected != null)
{
this.Selected(this, e);
}
}
}
}