/// <summary> /// 引入js文件 /// </summary> /// <param name="htmlHelper"></param> /// <param name="scriptUrl">脚本文件地址(比如:~/Scripts/Site.js)</param> /// <returns>空字符串,只有在调用RenderScripts方法时,才会统一输出</returns> public static MvcHtmlString Script(this HtmlHelper htmlHelper, string scriptUrl) { IPageResourceManager pageResourceManager = DIContainer.ResolvePerHttpRequest <IPageResourceManager>(); pageResourceManager.IncludeScript(scriptUrl); return(MvcHtmlString.Empty); }
/// <summary> /// 引入js代码块 /// </summary> /// <param name="htmlHelper"></param> /// <param name="template">代码块</param> /// <returns></returns> public static MvcHtmlString ScriptBlock(this HtmlHelper htmlHelper, Func <object, HelperResult> template) { IPageResourceManager pageResourceManager = DIContainer.ResolvePerHttpRequest <IPageResourceManager>(); pageResourceManager.RegisterScriptBlock(template(null).ToHtmlString()); return(MvcHtmlString.Empty); }
/// <summary> /// 批量输出js代码块 /// </summary> /// <param name="htmlHelper"></param> /// <returns></returns> public static MvcHtmlString RenderScriptBlocks(this HtmlHelper htmlHelper) { IPageResourceManager pageResourceManager = DIContainer.ResolvePerHttpRequest <IPageResourceManager>(); IList <string> scriptBlocks = pageResourceManager.GetRegisteredScriptBlocks(); pageResourceManager.ClearRegisteredScriptBlocks(); return(MvcHtmlString.Create(string.Join(Environment.NewLine, scriptBlocks))); }
/// <summary> /// 批量输出css文件引用 /// </summary> /// <param name="htmlHelper"></param> /// <returns></returns> public static MvcHtmlString RenderStyles(this HtmlHelper htmlHelper) { IPageResourceManager pageResourceManager = DIContainer.ResolvePerHttpRequest <IPageResourceManager>(); IList <string> styles = pageResourceManager.GetIncludedStyleHtmls(); pageResourceManager.ClearIncludedStyles(); return(MvcHtmlString.Create(string.Join(Environment.NewLine, styles))); }
/// <summary> /// 在页面底部呈现引入的js /// </summary> /// <remarks> /// </body>结束以前 /// </remarks> /// <param name="html"></param> public static void RenderIncludedScriptsAtFoot(this HtmlHelper html) { TextWriter _writer = html.ViewContext.Writer; IPageResourceManager pageResourceManager = DIContainer.ResolvePerHttpRequest <IPageResourceManager>(); IList <ResourceEntry> includeScripts = pageResourceManager.GetIncludedScripts(ResourceRenderLocation.Foot); foreach (var includeScript in includeScripts) { _writer.WriteLine(pageResourceManager.GetRenderingHtml(includeScript)); } }
public void OnResultExecuted(ResultExecutedContext filterContext) { if (filterContext.IsChildAction) { return; } if (filterContext.HttpContext.Request.AcceptTypes != null) { if (filterContext.HttpContext.Request.AcceptTypes.Contains("application/json")) { return; } } string token = filterContext.HttpContext.Request.Form.Get <string>("CurrentUserIdToken", string.Empty); if (!string.IsNullOrEmpty(token)) { return; } IPageResourceManager pageResourceManager = DIContainer.ResolvePerHttpRequest <IPageResourceManager>(); //输出Script IList <string> scriptHtmls = pageResourceManager.GetIncludedScriptHtmls(); if (scriptHtmls.Count > 0) { var scripts = MvcHtmlString.Create(string.Join(System.Environment.NewLine, scriptHtmls)); filterContext.RequestContext.HttpContext.Response.Write(scripts); pageResourceManager.ClearIncludedScripts(); } //输出Style IList <string> styleHtmls = pageResourceManager.GetIncludedStyleHtmls(); if (styleHtmls.Count > 0) { var styles = MvcHtmlString.Create(string.Join(System.Environment.NewLine, styleHtmls)); filterContext.RequestContext.HttpContext.Response.Write(styles); pageResourceManager.ClearIncludedStyles(); } //输出ScriptBlock IList <string> scriptBlocks = pageResourceManager.GetRegisteredScriptBlocks(); if (scriptBlocks.Count > 0) { var scripts = MvcHtmlString.Create(string.Join(System.Environment.NewLine, scriptBlocks)); filterContext.RequestContext.HttpContext.Response.Write(scripts); pageResourceManager.ClearRegisteredScriptBlocks(); } }
/// <summary> /// 构造函数 /// </summary> /// <param name="viewContext"></param> /// <param name="title"></param> /// <param name="disableClientCache"></param> /// <param name="charset"></param> public MvcHead(ViewContext viewContext, string title = null, bool disableClientCache = false, string charset = "UTF-8") { if (viewContext == null) { throw new ArgumentNullException("viewContext"); } _viewContext = viewContext; _writer = viewContext.Writer; pageResourceManager = DIContainer.ResolvePerHttpRequest <IPageResourceManager>(); TagBuilder tagBuilder = new TagBuilder("head"); _writer.WriteLine(tagBuilder.ToString(TagRenderMode.StartTag)); _writer.WriteLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset={0}\" />", charset); if (string.IsNullOrEmpty(title)) { if (pageResourceManager.IsAppendSiteName) { ISiteSettingsManager siteSettingsManager = DIContainer.Resolve <ISiteSettingsManager>(); pageResourceManager.AppendTitleParts(siteSettingsManager.Get().SiteName); } title = pageResourceManager.GenerateTitle(); } if (!string.IsNullOrEmpty(title)) { _writer.WriteLine("<title>{0}</title>", title); } _writer.WriteLine("<link rel=\"shortcut icon\" type=\"image/ico\" href=\"{0}\" />", pageResourceManager.ShortcutIcon); if (disableClientCache) { _writer.WriteLine("<meta http-equiv=\"Pragma\" content=\"no-cache\" />\n"); _writer.WriteLine("<meta http-equiv=\"no-cache\" />\n"); _writer.WriteLine("<meta http-equiv=\"Expires\" content=\"-1\" />\n"); _writer.WriteLine("<meta http-equiv=\"Cache-Control\" content=\"no-cache\" />\n"); } IList <MetaEntry> metas = pageResourceManager.GetRegisteredMetas(); foreach (var meta in metas) { _writer.WriteLine(meta.GetRenderingTag()); } }
public void IncludeCss(RequestContext controllerContext) { ThemeAppearance themeAppearance = GetRequestTheme(controllerContext); if (themeAppearance == null) { return; } string themeCssPath = "~/Themes/Channel/Default/theme.css"; string appearanceCssPath = "~/Themes/Channel/Default/Appearances/Default/appearance.css"; IPageResourceManager resourceManager = DIContainer.ResolvePerHttpRequest <IPageResourceManager>(); resourceManager.IncludeCss(themeCssPath, renderPriority: ResourceRenderPriority.Last); resourceManager.IncludeCss(appearanceCssPath, renderPriority: ResourceRenderPriority.Last); }
/// <summary> /// 在页面底部呈现注册的css代码块 /// </summary> /// <remarks> /// </body>结束以前 /// </remarks> /// <param name="html"></param> public static void RenderRegisteredCssBlocksAtFoot(this HtmlHelper html) { TextWriter _writer = html.ViewContext.Writer; IPageResourceManager pageResourceManager = DIContainer.ResolvePerHttpRequest <IPageResourceManager>(); IList <string> registeredCssBlocks = pageResourceManager.GetRegisteredCssBlocks(ResourceRenderLocation.Foot); if (registeredCssBlocks != null && registeredCssBlocks.Count > 0) { _writer.WriteLine("<style type=\"text/css\">"); foreach (var registeredCssBlock in registeredCssBlocks) { _writer.WriteLine(registeredCssBlock); } _writer.WriteLine("</style>"); } }
/// <summary> /// 在页面链接Javascript文件,并于代码引入位置呈现 /// </summary> /// <param name="html">要扩展的HtmlHelper</param> /// <param name="scriptUrl">需要引入的Script资源</param> /// <returns></returns> public static MvcHtmlString LinkScript(this HtmlHelper html, string scriptUrl) { if (string.IsNullOrEmpty(scriptUrl)) { throw new ArgumentNullException("resouceSetName"); } IPageResourceManager pageResourceManager = DIContainer.ResolvePerHttpRequest <IPageResourceManager>(); TagBuilder tagBuilder; tagBuilder = new TagBuilder("script"); tagBuilder.MergeAttribute("src", pageResourceManager.ResolveUrlWithResourceSite(scriptUrl)); tagBuilder.MergeAttribute("type", "text/javascript"); return(new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal))); }
/// <summary> /// 在页面链接css文件,并于代码引入位置呈现 /// </summary> /// <param name="html">要扩展的HtmlHelper</param> /// <param name="cssUrl">需要引入的css资源</param> /// <returns></returns> public static MvcHtmlString LinkCss(this HtmlHelper html, string cssUrl) { if (string.IsNullOrEmpty(cssUrl)) { throw new ArgumentNullException("cssUrl"); } IPageResourceManager pageResourceManager = DIContainer.ResolvePerHttpRequest <IPageResourceManager>(); TagBuilder tagBuilder; tagBuilder = new TagBuilder("link"); tagBuilder.MergeAttribute("href", pageResourceManager.ResolveUrlWithResourceSite(cssUrl)); tagBuilder.MergeAttribute("type", "text/css"); tagBuilder.MergeAttribute("rel", "stylesheet"); return(new MvcHtmlString(tagBuilder.ToString(TagRenderMode.SelfClosing))); }
/// <summary> /// 重写OnResultExecuting /// </summary> /// <param name="filterContext">ResultExecutingContext</param> public override void OnResultExecuting(ResultExecutingContext filterContext) { if (filterContext.IsChildAction) { return; } IPageResourceManager pageResourceManager = DIContainer.ResolvePerHttpRequest <IPageResourceManager>(); pageResourceManager.IsAppendSiteName = IsAppendSiteName; if (string.IsNullOrEmpty(TitlePart) && !string.IsNullOrEmpty(TitlePartResourceName)) { TitlePart = Tunynet.Globalization.ResourceAccessor.GetString(TitlePartResourceName); } if (!string.IsNullOrEmpty(TitlePart)) { pageResourceManager.AppendTitleParts(TitlePart); } }
/// <summary> /// 构造函数 /// </summary> /// <param name="viewContext"></param> /// <param name="title"></param> /// <param name="disableClientCache"></param> /// <param name="charset"></param> public MvcHead(ViewContext viewContext, string title = null, bool disableClientCache = false, string charset = "UTF-8") { if (viewContext == null) throw new ArgumentNullException("viewContext"); _viewContext = viewContext; _writer = viewContext.Writer; pageResourceManager = DIContainer.ResolvePerHttpRequest<IPageResourceManager>(); TagBuilder tagBuilder = new TagBuilder("head"); _writer.WriteLine(tagBuilder.ToString(TagRenderMode.StartTag)); _writer.WriteLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset={0}\" />", charset); if (string.IsNullOrEmpty(title)) { if (pageResourceManager.IsAppendSiteName) { pageResourceManager.AppendTitleParts(DIContainer.Resolve<ISettingsManager<SiteSettings>>().Get().SiteName); } title = pageResourceManager.GenerateTitle(); } if (!string.IsNullOrEmpty(title)) _writer.WriteLine("<title>{0}</title>", title); _writer.WriteLine("<link rel=\"shortcut icon\" type=\"image/ico\" href=\"{0}\" />", pageResourceManager.ShortcutIcon); if (disableClientCache) { _writer.WriteLine("<meta http-equiv=\"Pragma\" content=\"no-cache\" />\n"); _writer.WriteLine("<meta http-equiv=\"no-cache\" />\n"); _writer.WriteLine("<meta http-equiv=\"Expires\" content=\"-1\" />\n"); _writer.WriteLine("<meta http-equiv=\"Cache-Control\" content=\"no-cache\" />\n"); } IList<MetaEntry> metas = pageResourceManager.GetRegisteredMetas(); foreach (var meta in metas) { _writer.WriteLine(meta.GetRenderingTag()); } }
/// <summary> /// 加载皮肤的css /// </summary> /// <param name="controllerContext"><see cref="RequestContext" /></param> /// <author version=""></author> /// <modifier version=""></modifier> public void IncludeStyle(RequestContext controllerContext) { ThemeAppearance themeAppearance = GetRequestTheme(controllerContext); if (themeAppearance == null) { return; } PresentArea presentArea = new PresentAreaService().Get(themeAppearance.PresentAreaKey); if (presentArea == null) { return; } string themeCssPath = string.Format("{0}/{1}/theme.css", presentArea.ThemeLocation, themeAppearance.ThemeKey); //string appearanceCssPath = string.Format("{0}/{1}/Appearances/{2}/appearance.css", presentArea.ThemeLocation, themeAppearance.ThemeKey, themeAppearance.AppearanceKey); IPageResourceManager resourceManager = DIContainer.ResolvePerHttpRequest <IPageResourceManager>(); // resourceManager.IncludeStyle(themeCssPath); //resourceManager.IncludeStyle(appearanceCssPath); }
void IThemeResolver.IncludeStyle(RequestContext controllerContext) { string spaceKey = controllerContext.GetParameterFromRouteDataOrQueryString("SpaceKey"); GroupEntity group = new GroupService().Get(spaceKey); if (group == null) { return; } PresentArea presentArea = new PresentAreaService().Get(PresentAreaKeysOfBuiltIn.GroupSpace); if (presentArea == null) { return; } string themeKey = null; string appearanceKey = null; IPageResourceManager resourceManager = DIContainer.ResolvePerHttpRequest <IPageResourceManager>(); if (!presentArea.EnableThemes) { string themeCssPath = string.Format("{0}/{1}/theme.css", presentArea.ThemeLocation, presentArea.DefaultThemeKey); string appearanceCssPath = string.Format("{0}/{1}/Appearances/{2}/appearance.css", presentArea.ThemeLocation, presentArea.DefaultThemeKey, presentArea.DefaultAppearanceKey); resourceManager.IncludeStyle(themeCssPath); resourceManager.IncludeStyle(appearanceCssPath); return; } if (group.IsUseCustomStyle) { var customStyleEntity = new CustomStyleService().Get(presentArea.PresentAreaKey, group.GroupId); if (customStyleEntity == null) { return; } CustomStyle customStyle = customStyleEntity.CustomStyle; if (customStyle == null) { return; } string themeCssPath = string.Format("{0}/Custom/theme{1}.css", presentArea.ThemeLocation, customStyle.IsDark ? "-deep" : ""); string appearanceCssPath = SiteUrls.Instance().CustomStyle(presentArea.PresentAreaKey, group.GroupId); resourceManager.IncludeStyle(themeCssPath); resourceManager.IncludeStyle(appearanceCssPath); StringBuilder builder = new StringBuilder(); builder.AppendLine(".tn-page-bg{"); if (customStyle.IsUseBackgroundImage && !string.IsNullOrEmpty(customStyle.BackgroundImageStyle.Url)) { builder.AppendLine("background-image:url('" + customStyle.BackgroundImageStyle.Url + @"');"); builder.AppendFormat("background-repeat:{0};\n", customStyle.BackgroundImageStyle.IsRepeat ? "repeat" : "no-repeat"); builder.AppendFormat("background-attachment:{0};\n", customStyle.BackgroundImageStyle.IsFix ? "fixed" : "scroll"); string position = "center"; switch (customStyle.BackgroundImageStyle.BackgroundPosition) { case BackgroundPosition.Left: position = "left"; break; case BackgroundPosition.Center: position = "center"; break; case BackgroundPosition.Right: position = "right"; break; default: position = "center"; break; } builder.AppendFormat("background-position:{0} top;\n", position); } builder.AppendLine("}"); resourceManager.RegisterStyleBlock(builder.ToString()); } else { string[] themeAppearanceArray = group.ThemeAppearance.Split(','); var appearance = new ThemeService().GetThemeAppearance(PresentAreaKeysOfBuiltIn.GroupSpace, group.ThemeAppearance); if (appearance != null && themeAppearanceArray.Count() == 2) { themeKey = themeAppearanceArray[0]; appearanceKey = themeAppearanceArray[1]; } else { themeKey = presentArea.DefaultThemeKey; appearanceKey = presentArea.DefaultAppearanceKey; } string themeCssPath = string.Format("{0}/{1}/theme.css", presentArea.ThemeLocation, themeKey); string appearanceCssPath = string.Format("{0}/{1}/Appearances/{2}/appearance.css", presentArea.ThemeLocation, themeKey, appearanceKey); resourceManager.IncludeStyle(themeCssPath); resourceManager.IncludeStyle(appearanceCssPath); } }
/// <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); }
/// <summary> /// 在页面引用Script /// </summary> /// <param name="html"></param> /// <param name="basePath">基路径</param> /// <param name="fileName">文件名称</param> /// <param name="debugFileName">用于调试的文件名称</param> /// <param name="renderPriority"><see cref="ResourceRenderPriority"/></param> /// <param name="renderLocation"><see cref="ResourceRenderLocation"/></param> /// <param name="htmlAttributes">html属性</param> public static void IncludeScript(this HtmlHelper html, string basePath, string fileName, string debugFileName = null, ResourceRenderPriority renderPriority = ResourceRenderPriority.Unspecified, ResourceRenderLocation renderLocation = ResourceRenderLocation.Head, IDictionary <string, string> htmlAttributes = null) { IPageResourceManager pageResourceManager = DIContainer.ResolvePerHttpRequest <IPageResourceManager>(); pageResourceManager.IncludeScript(basePath, fileName, debugFileName, renderPriority, renderLocation, htmlAttributes); }
/// <summary> /// 加载皮肤的css /// </summary> /// <param name="controllerContext"><see cref="RequestContext"/></param> void IThemeResolver.IncludeCss(RequestContext controllerContext) { string spaceKey = controllerContext.GetParameterFromRouteDataOrQueryString("SpaceKey"); IUserService userService = DIContainer.Resolve <IUserService>(); User user = userService.GetFullUser(spaceKey); if (user == null) { throw new ExceptionFacade(new ResourceExceptionDescriptor().WithUserNotFound(spaceKey, 0)); } PresentArea presentArea = new PresentAreaService().Get(PresentAreaKeysOfBuiltIn.UserSpace); if (presentArea == null) { return; } string themeKey = null; string appearanceKey = null; IPageResourceManager resourceManager = DIContainer.ResolvePerHttpRequest <IPageResourceManager>(); if (!presentArea.EnableThemes) { string themeCssPath = string.Format("{0}/{1}/theme.css", presentArea.ThemeLocation, presentArea.DefaultThemeKey); string appearanceCssPath = string.Format("{0}/{1}/Appearances/{2}/appearance.css", presentArea.ThemeLocation, presentArea.DefaultThemeKey, presentArea.DefaultAppearanceKey); resourceManager.IncludeCss(themeCssPath, renderPriority: ResourceRenderPriority.Last); resourceManager.IncludeCss(appearanceCssPath, renderPriority: ResourceRenderPriority.Last); return; } if (user.IsUseCustomStyle) { var customStyleEntity = new CustomStyleService().Get(presentArea.PresentAreaKey, user.UserId); if (customStyleEntity == null) { return; } CustomStyle customStyle = customStyleEntity.CustomStyle; if (customStyle == null) { return; } string themeCssPath = string.Format("{0}/Custom/theme{1}.css", presentArea.ThemeLocation, customStyle.IsDark ? "-deep" : ""); string appearanceCssPath = SiteUrls.Instance().CustomStyle(presentArea.PresentAreaKey, user.UserId); resourceManager.IncludeCss(themeCssPath, renderPriority: ResourceRenderPriority.Last); resourceManager.IncludeCss(appearanceCssPath, renderPriority: ResourceRenderPriority.Last); StringBuilder builder = new StringBuilder(); builder.AppendLine(".tn-page-bg{"); if (customStyle.IsUseBackgroundImage) { builder.AppendLine("background-image:url('" + customStyle.BackgroundImageStyle.Url + @"');"); builder.AppendFormat("background-repeat:{0};\n", customStyle.BackgroundImageStyle.IsRepeat ? "repeat" : "no-repeat"); builder.AppendFormat("background-attachment:{0};\n", customStyle.BackgroundImageStyle.IsFix ? "fixed" : "scroll"); string position = "center"; switch (customStyle.BackgroundImageStyle.BackgroundPosition) { case BackgroundPosition.Left: position = "left"; break; case BackgroundPosition.Center: position = "center"; break; case BackgroundPosition.Right: position = "right"; break; default: position = "center"; break; } builder.AppendFormat("background-position:{0} top;\n", position); } builder.AppendLine("}"); builder.AppendLine("#tn-content{"); builder.AppendLine("margin-top:" + customStyle.HeaderHeight.ToString() + "px;"); builder.AppendLine("}"); resourceManager.RegisterCssBlockAtHead(builder.ToString()); } else { string[] themeAppearanceArray = user.ThemeAppearance.Split(','); var appearance = new ThemeService().GetThemeAppearance(PresentAreaKeysOfBuiltIn.UserSpace, user.ThemeAppearance); if (appearance != null && themeAppearanceArray.Count() == 2) { themeKey = themeAppearanceArray[0]; appearanceKey = themeAppearanceArray[1]; } else { themeKey = presentArea.DefaultThemeKey; appearanceKey = presentArea.DefaultAppearanceKey; } string themeCssPath = string.Format("{0}/{1}/theme.css", presentArea.ThemeLocation, themeKey); string appearanceCssPath = string.Format("{0}/{1}/Appearances/{2}/appearance.css", presentArea.ThemeLocation, themeKey, appearanceKey); resourceManager.IncludeCss(themeCssPath, renderPriority: ResourceRenderPriority.Last); resourceManager.IncludeCss(appearanceCssPath, renderPriority: ResourceRenderPriority.Last); } }