All pastes #231462 Raw Edit

Shin-Ra

public text v1 · immutable
#231462 ·published 2006-10-31 20:56 UTC
rendered paste body
Imports System
Imports System.Collections
Imports System.Configuration
Imports System.Web
Imports System.Web.UI
Imports BulletinX.Components

Namespace BulletinX.Controls
    <PersistChildren(True), ParseChildren(False)> _
    Public Class Head
        Inherits Control
        ''' <summary>
        ''' The value of the title in the web.config.
        ''' </summary>
        ''' <remarks>
        ''' <add key="SiteName" value="BulletinX Community Software" />
        ''' </remarks>
        Public Shared ReadOnly titleKey As String = ConfigurationManager.AppSettings("SiteTitle")
        ''' <summary>
        ''' The value of the location of the Favicon.
        ''' </summary>
        ''' <remarks>
        ''' <add key="Favicon" value="~/favicon.ico" />
        ''' </remarks>
        Public Shared ReadOnly faviconKey As String = ConfigurationManager.AppSettings("Favicon")
        ''' <summary>
        ''' The string format of the style to create each time a style is added to the head.
        ''' </summary>
        ''' <remarks>
        ''' The style format that is created each time. Href and Media are string inputs.
        ''' </remarks>
        Public Shared styleFormat As String = "<link rel=""stylesheet"" href=""{0}"" type=""text/css"" media=""{1}"" />"

        ''' <summary>
        ''' Overrides the default Render method.
        ''' </summary>
        ''' <param name="writer">
        ''' HtmlTextWriter to create the Rendering process.
        ''' </param>
        ''' <remarks>
        ''' Need to write the output to the browser.
        ''' </remarks>
        Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
            writer.Write("")
            writer.WriteLine("<head>")
            RenderTitle(writer)
            RenderFavicon(writer)
            RenderStyleSheets(writer)
            writer.WriteLine("</head>")
        End Sub

        ''' <summary>
        ''' Render the html title tags.
        ''' </summary>
        ''' <param name="writer">
        ''' HtmlTextWriter to write the title tags.
        ''' </param>
        ''' <remarks>
        ''' Creates the title tags by the control.
        ''' </remarks>
        Protected Overridable Sub RenderTitle(ByVal writer As HtmlTextWriter)
            Dim title As String = titleKey
            If title Is Nothing Then
                ''
                '' TODO: Change this
                ''
                Throw New ApplicationException("SiteTitle is empty, please enter a title in the web.config.")
            End If
            writer.WriteLine("<title>{0}</title>", title)
        End Sub

        ''' <summary>
        ''' Render the favicon tag.
        ''' </summary>
        ''' <param name="writer">
        ''' HtmlTextWriter to write by the favicon tags.
        ''' </param>
        ''' <remarks>
        ''' Creates the favicon tags by the control.
        ''' </remarks>
        Protected Overridable Sub RenderFavicon(ByVal writer As HtmlTextWriter)
            writer.WriteLine("<link rel=""shortcut icon"" type=""image/ico"" href=""{0}"" />", faviconKey)
        End Sub

        ''' <summary>
        ''' Render the stylesheets tags.
        ''' </summary>
        ''' <param name="writer">
        ''' HtmlTextWriter to write by the stylesheet tags.
        ''' </param>
        ''' <remarks>
        ''' Creates the stylesheet tags by the control.
        ''' </remarks>
        Protected Overridable Sub RenderStyleSheets(ByVal writer As HtmlTextWriter)
            RenderStyleSheets(writer, StyleFlags.First)
            RenderStyleSheets(writer, StyleFlags.Last)
            RenderStyleSheets(writer, StyleFlags.Unknown)
        End Sub

        ''' <summary>
        ''' Render the stylesheets tags.
        ''' </summary>
        ''' <param name="writer">
        ''' HtmlTextWriter to write by the stylesheet tags.
        ''' </param>
        ''' <param name="flags">
        ''' Position of the way the stylesheets are created.
        ''' </param>
        ''' <remarks>
        ''' Creates the stylesheet tags by the control.
        ''' </remarks>
        Protected Overridable Sub RenderStyleSheets(ByVal writer As HtmlTextWriter, ByVal flags As StyleFlags)
            Dim q As Queue = CType(Context.Items(styleFormat), Queue)
            If q IsNot Nothing AndAlso q.Count > 0 Then
                Dim ie As IEnumerator = q.GetEnumerator()
                While (ie.MoveNext())
                    Dim si As StyleQueue = CType(ie.Current, StyleQueue)
                    If si._position = flags Then
                        writer.WriteLine(si._styleTag)
                    End If
                End While
            End If
        End Sub

        ''' <summary>
        ''' Gets values from the child controls.
        ''' </summary>
        ''' <param name="url">
        ''' Url of the stylesheet location.
        ''' </param>
        ''' <param name="media">
        ''' Media type for the stylesheet.
        ''' </param>
        ''' <remarks>
        ''' Gets the values from the child control of Style.
        ''' </remarks>
        Public Shared Sub AddStyle(ByVal url As String, ByVal media As String)
            AddStyle(url, StyleFlags.Unknown)
        End Sub

        ''' <summary>
        ''' Gets values from the child controls.
        ''' </summary>
        ''' <param name="url">
        ''' Url of the stylesheet location.
        ''' </param>
        ''' <param name="media">
        ''' Media type for the stylesheet.
        ''' </param>
        ''' <param name="flags">
        ''' Position of the stylesheet.
        ''' </param>
        ''' <remarks>
        ''' Gets the values from the child control of Style.
        ''' </remarks>
        Public Shared Sub AddStyle(ByVal url As String, ByVal media As String, ByVal flags As StyleFlags)
            Dim context As HttpContext = HttpContext.Current
            Dim styleQueue As Queue = CType(context.Items(styleFormat), Queue)
            If styleQueue Is Nothing Then
                styleQueue = New Queue
                context.Items(styleFormat) = styleQueue
            End If
            styleQueue.Enqueue(New StyleQueue(String.Format(styleFormat, url, media), flags))
        End Sub
    End Class
End Namespace