SharePoint and HTML 5 Technical Implementation
HTML Panel
// Does two things:
// 1. Adds a html5 or a div tag around it contents depending on page mode, e.g. <section></section> or <div class="section"></div>
// 2. Does or does not show it's child webpartzones when the page is in HTML5 mode based on an attribute of the webpart zone.
public class HTMLSwitchPanel : Panel
{
public CapGemini.Microsoft.Wiersema.Constants.PublishingDefinitions.TagTypes CurrentTagType { get; set; }
public override void RenderBeginTag(HtmlTextWriter writer)
{
bool InEditMode = (MSSharePoint.SPContext.Current.FormContext.FormMode != SPControlMode.Display);
string aTemp = PublishingUtilities.GetHTMLOpenTag(CurrentTagType, InEditMode);
writer.Write(aTemp);
}
public override void RenderEndTag(HtmlTextWriter writer)
{
bool InEditMode = (MSSharePoint.SPContext.Current.FormContext.FormMode != SPControlMode.Display);
string aTemp = PublishingUtilities.GetHTMLCloseTag(CurrentTagType, InEditMode);
writer.Write(aTemp);
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
// iterate through all child controls of this panel
// Thanks for the code fragment to Cristain Librado , see http://stackoverflow.com/questions/277646/finding-all-controls-in-an-asp-net-panel
foreach (Control c in EnumerateControlsRecursive(this))
{
// if the control is a webpartzone
if (c is MSWPP.WebPartZone)
{
MSWPP.WebPartZone aTempZone = (MSWPP.WebPartZone)c;
// when not in display mode make all webpart zones visible
if (MSSharePoint.SPContext.Current.FormContext.FormMode != SPControlMode.Display)
{
aTempZone.Visible = true;
}
else
{
// when in display mode
// if the attribute exist on the webpartzone, continue
if (aTempZone.Attributes["IsHTML5"] != null)
{
// if the page is in html5 mode
if (((this.Page as ExtendedPublishingPage).IsHTML5Page))
{
// if the page is in html5 mode and the current part zone ISHTML5 that show it
if (aTempZone.Attributes["IsHTML5"] == "true")
{
aTempZone.Visible = true;
}
else
{
// if the page is in html5 mode but the currentwebpart zone is not, don't show it
aTempZone.Visible = false;
}
}
else
{
// if the page is not in html5 mode and the webpartzone isnt either, show the webpart
if (aTempZone.Attributes["IsHTML5"] == "false")
{
aTempZone.Visible = true;
}
else
{
// if the page is not in html5 mode but the webpartzone is, don't show it
aTempZone.Visible = false;
}
}
}
}
}
}
}
// enumerate through all first level children of the panel
// Thanks for the code fragment to Cristain Librado , see http://stackoverflow.com/questions/277646/finding-all-controls-in-an-asp-net-panel
IEnumerable<Control> EnumerateControlsRecursive(Control parent)
{
foreach (Control child in parent.Controls)
{
yield return child;
/* don't need to return grandchildren because we're only interested in child webpartzones
/*
foreach (Control descendant in EnumerateControlsRecursive(child))
yield return descendant;
* */
}
}
}
// USAGE in a page layout:
<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">
<Wiersema:HTMLSwitchPanel CurrentTagType="article" runat="server">
<Wiersema:HTMLSwitchPanel CurrentTagType="header" runat="server">
<h2><SharePointWebControls:FieldValue ID="ArticleTitle" FieldName="Title" runat="server"/></h2>
</Wiersema:HTMLSwitchPanel> <!-- end article header -->
<WebPartPages:WebPartZone runat="server" ID="MainArticle_html5" Title="MainArticle_html5" FrameType="None" tableless="true" IsHTML5="true">
<ZoneTemplate></ZoneTemplate>
</WebPartPages:WebPartZone>
<WebPartPages:WebPartZone runat="server" ID="MainArticle_xhtml" Title="MainArticle_xhtml" FrameType="None" tableless="true" IsHTML5="false">
<ZoneTemplate></ZoneTemplate>
</WebPartPages:WebPartZone>
</Wiersema:HTMLSwitchPanel> <!-- end article -->
</asp:Content>
No comments:
Post a Comment