Exemplo n.º 1
0
        /// <summary>
        /// 基于Combres的ResourceName引入js/css
        /// </summary>
        /// <param name="resourceType"><see cref="PageResourceType"/></param>
        /// <param name="resouceSetName">Combres中设置的ResourceSet</param>
        /// <param name="debugResouceSetName">debug时使用的ResouceSetName</param>
        /// <param name="renderPriority">呈现优先级</param>
        /// <param name="renderLocation">在页面中的呈现位置</param>
        /// <param name="htmlAttributes">设置的html属性</param>
        public void IncludeCombresResouceSet(PageResourceType resourceType, string resouceSetName, string debugResouceSetName = null, ResourceRenderPriority renderPriority = ResourceRenderPriority.Unspecified, ResourceRenderLocation renderLocation = ResourceRenderLocation.Head, IDictionary <string, string> htmlAttributes = null)
        {
            if (string.IsNullOrEmpty(resouceSetName))
            {
                throw new ArgumentNullException("resouceSetName");
            }

            string url      = WebExtensions.CombresUrl(resouceSetName);
            string debugUrl = null;

            if (debugResouceSetName != null)
            {
                debugUrl = WebExtensions.CombresUrl(debugResouceSetName);
            }

            ResourceEntry resource = new ResourceEntry(resourceType, null, url, debugUrl, renderPriority, renderLocation, htmlAttributes);

            switch (resourceType)
            {
            case PageResourceType.JS:
                _includedScripts.Add(resource);
                break;

            case PageResourceType.CSS:
                _includedCsss.Add(resource);
                break;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Renders a resource (Javascript, CSS, ...)
        /// </summary>
        /// <param name="HtmlHelper"></param>
        /// <param name="resourceType"></param>
        /// <returns></returns>
        public static HtmlString RenderResources(this IHtmlHelper HtmlHelper, PageResourceType resourceType)
        {
            if (HtmlHelper.ViewContext.HttpContext.Items[resourceType] != null)
            {
                var resources = (Dictionary <int, Func <object, HelperResult> >)HtmlHelper.ViewContext.HttpContext.Items[resourceType];
                foreach (var resource in resources?.Values)
                {
                    HtmlHelper.ViewContext.Writer.Write(resource(null));
                }
            }

            return(new HtmlString(string.Empty));
        }
Exemplo n.º 3
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="resourceType">资源类型</param>
        /// <param name="basePath">基路径</param>
        /// <param name="url"></param>
        /// <param name="debugUrl"></param>
        /// <param name="renderPriority"></param>
        /// <param name="renderLocation"></param>
        /// <param name="attributes"></param>
        public ResourceEntry(PageResourceType resourceType, string basePath, string url, string debugUrl,
                             ResourceRenderPriority renderPriority, ResourceRenderLocation renderLocation, IDictionary <string, string> attributes)
        {
            this.ResourceType   = resourceType;
            this.BasePath       = basePath;
            this.Url            = url;
            this.DebugUrl       = debugUrl;
            this.RenderPriority = renderPriority;
            this.RenderLocation = renderLocation;

            if (attributes != null)
            {
                this.Attributes = attributes;
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Adds a resource (Javascript, CSS, ...)
        /// </summary>
        /// <param name="HtmlHelper"></param>
        /// <param name="resourceType"></param>
        /// <param name="Template"></param>
        /// <returns></returns>
        public static HtmlString AddResource(this IHtmlHelper HtmlHelper, PageResourceType resourceType, Func <object, HelperResult> Template)
        {
            if (!(HtmlHelper.ViewContext.HttpContext.Items[resourceType] is Dictionary <int, Func <object, HelperResult> > allItems))
            {
                allItems = new Dictionary <int, Func <object, HelperResult> >();
                HtmlHelper.ViewContext.HttpContext.Items[resourceType] = allItems;
            }

            int hash = -1;

            using (var writer = new StringWriter(CultureInfo.InvariantCulture))
            {
                Template(null).WriteTo(writer, HtmlEncoder.Default);
                hash = writer.ToString().GetHashCode();
            }

            if (!allItems.ContainsKey(hash))
            {
                allItems.Add(hash, Template);
            }

            return(new HtmlString(string.Empty));
        }
Exemplo n.º 5
0
 public static IList <T> Resources <T>(this Page page, PageResourceType type) where T : PageResource
 {
     return(page.Resources.Where(x => x.ResourceType == type).OrderBy(x => x.Ordinal).Cast <T>().ToList());
 }
Exemplo n.º 6
0
 /// <summary>
 /// 基于Combres的ResourceName引入js/css
 /// </summary>
 /// <param name="resourceType"><see cref="PageResourceType"/></param>
 /// <param name="resouceSetName">Combres中设置的ResourceSet</param>
 public void IncludeCombresResouceSet(PageResourceType resourceType, string resouceSetName)
 {
     IncludeCombresResouceSet(resourceType, resouceSetName, null);
 }
Exemplo n.º 7
0
        /// <summary>
        /// 在页面引用Combres Script
        /// </summary>
        /// <param name="html"></param>
        /// <param name="resourceType"><see cref="PageResourceType"/></param>
        /// <param name="resourceSetName">在Combres设置的ResourceSet名称</param>
        /// <param name="renderPriority">资源呈现优先级<see cref="ResourceRenderPriority"/></param>
        public static void IncludeCombresScript(this HtmlHelper html, PageResourceType resourceType, string resourceSetName, ResourceRenderPriority renderPriority = ResourceRenderPriority.Unspecified)
        {
            IPageResourceManager pageResourceManager = DIContainer.ResolvePerHttpRequest <IPageResourceManager>();

            pageResourceManager.IncludeCombresResouceSet(resourceType, resourceSetName, renderPriority: renderPriority);
        }