121 lines
2.6 KiB
C#
121 lines
2.6 KiB
C#
using System.Xml;
|
|
using MBS.Web.UI;
|
|
|
|
namespace MBS.Web;
|
|
|
|
public abstract class Control : IWebHandler
|
|
{
|
|
|
|
public class ControlCollection
|
|
: System.Collections.ObjectModel.Collection<Control>
|
|
{
|
|
|
|
}
|
|
|
|
public Dictionary<string, string> PathVariables { get; } = new Dictionary<string, string>();
|
|
public bool Visible { get; set; } = true;
|
|
|
|
public Control(Dictionary<string, string>? pathVariables = null)
|
|
{
|
|
if (pathVariables == null)
|
|
pathVariables = new Dictionary<string, string>();
|
|
|
|
PathVariables = pathVariables;
|
|
}
|
|
|
|
protected virtual void OnInit(RenderEventArgs e)
|
|
{
|
|
}
|
|
protected virtual void RenderContents(XmlWriter writer)
|
|
{
|
|
}
|
|
|
|
protected virtual string TagName { get; } = "div";
|
|
public Dictionary<string, string> Attributes { get; } = new Dictionary<string, string>();
|
|
public CssClassList ClassList { get; } = new CssClassList();
|
|
|
|
protected virtual IEnumerable<string> GetStyleClasses()
|
|
{
|
|
return new string[0];
|
|
}
|
|
|
|
private bool _initted = false;
|
|
protected void EnsureInitialized()
|
|
{
|
|
if (!_initted)
|
|
{
|
|
InitializeInternal();
|
|
_initted = true;
|
|
}
|
|
PersistentInitializeInternal();
|
|
}
|
|
|
|
protected virtual void InitializeInternal()
|
|
{
|
|
}
|
|
|
|
protected virtual void PersistentInitializeInternal()
|
|
{
|
|
}
|
|
|
|
protected virtual void RenderBeginTag(XmlWriter writer)
|
|
{
|
|
EnsureInitialized();
|
|
|
|
writer.WriteStartElement(TagName);
|
|
|
|
bool classListWritten = false;
|
|
IDictionary<string, string> attrs = GetControlAttributes();
|
|
foreach (KeyValuePair<string, string> kvp in attrs)
|
|
{
|
|
string value = kvp.Value;
|
|
if (kvp.Key == "class")
|
|
{
|
|
IEnumerable<string> classList = ClassList.Union(kvp.Value.Split(new char[] { ' ' }));
|
|
|
|
value = String.Join(' ', classList);
|
|
classListWritten = true;
|
|
}
|
|
writer.WriteAttributeString(kvp.Key, value);
|
|
}
|
|
|
|
IEnumerable<string> actualClassList = ClassList.Union(GetStyleClasses());
|
|
if (!classListWritten && actualClassList.Count() > 0)
|
|
{
|
|
writer.WriteAttributeString("class", String.Join(' ', actualClassList));
|
|
}
|
|
}
|
|
protected virtual void RenderEndTag(XmlWriter writer)
|
|
{
|
|
writer.WriteEndElement();
|
|
}
|
|
|
|
public void Render(XmlWriter writer)
|
|
{
|
|
if (!Visible) return;
|
|
|
|
RenderBeginTag(writer);
|
|
RenderContents(writer);
|
|
RenderEndTag(writer);
|
|
}
|
|
|
|
public void ProcessRequest(WebContext context)
|
|
{
|
|
context.Response.ContentType = "application/xhtml+xml";
|
|
|
|
EnsureInitialized();
|
|
OnInit(new RenderEventArgs(context));
|
|
|
|
XmlWriter writer = XmlWriter.Create(context.Response.Stream);
|
|
Render(writer);
|
|
|
|
writer.Flush();
|
|
writer.Close();
|
|
}
|
|
|
|
protected virtual IDictionary<string, string> GetControlAttributes()
|
|
{
|
|
return Attributes;
|
|
}
|
|
}
|