ScriptElementCollectionRenderer.cs :  » Bloggers » SubText » Subtext » Framework » UI » Skinning » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Bloggers » SubText 
SubText » Subtext » Framework » UI » Skinning » ScriptElementCollectionRenderer.cs
#region Disclaimer/Info

///////////////////////////////////////////////////////////////////////////////////////////////////
// Subtext WebLog
// 
// Subtext is an open source weblog system that is a fork of the .TEXT
// weblog system.
//
// For updated news and information please visit http://subtextproject.com/
// Subtext is hosted at Google Code at http://code.google.com/p/subtext/
// The development mailing list is at subtext@googlegroups.com 
//
// This project is licensed under the BSD license.  See the License.txt file for more information.
///////////////////////////////////////////////////////////////////////////////////////////////////

#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using Subtext.Framework.Web;

namespace Subtext.Framework.UI.Skinning{
    /// <summary>
    /// Provides rendering facilities for script elements in the head element of the page
    /// </summary>
    public class ScriptElementCollectionRenderer
    {
        private readonly SkinEngine _skinEngine;

        public ScriptElementCollectionRenderer(SkinEngine skinEngine)
        {
            _skinEngine = skinEngine;
        }

        private IDictionary<string, SkinTemplate> Templates
        {
            get
            {
                var templates = _templates;
                if(templates == null)
                {
                    templates = _skinEngine.GetSkinTemplates(false /* mobile */);
                    _templates = templates;
                }
                return templates;
            }
        }
        IDictionary<string, SkinTemplate> _templates;

        private static string RenderScriptAttribute(string attributeName, string attributeValue)
        {
            return attributeValue != null ? string.Format(" {0}=\"{1}\"", attributeName, attributeValue) : String.Empty;
        }

        public static string RenderScriptElement(string skinPath, Script script)
        {
            return string.Format("<script{0}{1}{2}></script>{3}", RenderScriptAttribute("type", script.Type), RenderScriptAttribute("src", GetScriptSourcePath(skinPath, script)), RenderScriptAttribute("defer", script.Defer ? "defer" : null), Environment.NewLine);
        }

        public static string RenderScriptElement(string scriptPath)
        {
            return string.Format("<script{0}{1}></script>{2}", RenderScriptAttribute("type", "text/javascript"), RenderScriptAttribute("src", scriptPath), Environment.NewLine);
        }

        private static string GetScriptSourcePath(string skinPath, Script script)
        {
            if(script.Src.StartsWith("~"))
            {
                return HttpHelper.ExpandTildePath(script.Src);
            }
            if(script.Src.StartsWith("/") || script.Src.StartsWith("http://") || script.Src.StartsWith("https://"))
            {
                return script.Src;
            }
            return skinPath + script.Src;
        }

        /// <summary>
        /// Gets the skin path.
        /// </summary>
        /// <param name="skinTemplateFolder">Name of the skin.</param>
        /// <returns></returns>
        private static string GetSkinPath(string skinTemplateFolder)
        {
            string applicationPath = HttpContext.Current.Request.ApplicationPath;
            return string.Format("{0}/Skins/{1}/", (applicationPath == "/" ? String.Empty : applicationPath), skinTemplateFolder);
        }

        /// <summary>
        /// Renders the script element collection for thes kin key.
        /// </summary>
        /// <param name="skinKey">The skin key.</param>
        /// <returns></returns>
        public string RenderScriptElementCollection(string skinKey)
        {
            var result = new StringBuilder();

            SkinTemplate skinTemplate = Templates.ItemOrNull(skinKey);
            if(skinTemplate != null && skinTemplate.Scripts != null)
            {
                string skinPath = GetSkinPath(skinTemplate.TemplateFolder);
                if(CanScriptsBeMerged(skinTemplate))
                {
                    result.Append(RenderScriptElement(string.Format("{0}js.axd?name={1}", skinPath, skinKey)));
                }
                else
                {
                    foreach(Script script in skinTemplate.Scripts)
                    {
                        result.Append(RenderScriptElement(skinPath, script));
                    }
                }
            }
            return result.ToString();
        }

        public ScriptMergeMode GetScriptMergeMode(string skinName)
        {
            SkinTemplate skinTemplate = Templates.ItemOrNull(skinName);
            return skinTemplate.ScriptMergeMode;
        }

        public ICollection<string> GetScriptsToBeMerged(string skinName)
        {
            var scripts = new List<string>();

            SkinTemplate skinTemplate = Templates.ItemOrNull(skinName);

            if(skinTemplate != null && skinTemplate.Scripts != null)
            {
                if(CanScriptsBeMerged(skinTemplate))
                {
                    string skinPath = CreateStylePath(skinTemplate.TemplateFolder);
                    foreach(Script script in skinTemplate.Scripts)
                    {
                        if(script.Src.StartsWith("~"))
                        {
                            scripts.Add(HttpHelper.ExpandTildePath(script.Src));
                        }
                        else
                        {
                            scripts.Add(skinPath + script.Src);
                        }
                    }
                }
            }
            return scripts;
        }

        private static string CreateStylePath(string skinTemplateFolder)
        {
            string applicationPath = HttpContext.Current.Request.ApplicationPath;
            string path = string.Format("{0}/Skins/{1}/", (applicationPath == "/" ? String.Empty : applicationPath), skinTemplateFolder);
            return path;
        }

        public static bool CanScriptsBeMerged(SkinTemplate template)
        {
            if(!template.MergeScripts)
            {
                return false;
            }
            if(template.Scripts == null)
            {
                return false;
            }
            foreach(Script script in template.Scripts)
            {
                if(script.Src.Contains("?"))
                {
                    return false;
                }
                if(IsScriptRemote(script))
                {
                    return false;
                }
            }
            return true;
        }

        private static bool IsScriptRemote(Script script)
        {
            if(script.Src.StartsWith("http://") || script.Src.StartsWith("https://"))
            {
                return true;
            }
            return false;
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.