Example #1
0
        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);
            }
        }
Example #2
0
        //使用RazorEngine解析 \Themes\[PresentAreaKey]\CustomStyle\css-dark.xml 或 css-light.xml
        public override void ProcessRequest(HttpContext context)
        {
            string presentAreaKey = context.Request.QueryString.GetString("PresentAreaKey", string.Empty);
            long   ownerId        = context.Request.QueryString.Get <long>("OwnerId", 0);

            if (string.IsNullOrEmpty(presentAreaKey))
            {
                WebUtility.Return404(context);
            }
            CustomStyle customStyle = null;

            if (ownerId > 0)
            {
                CustomStyleEntity entity = new CustomStyleService().Get(presentAreaKey, ownerId);
                if (entity == null)
                {
                    WebUtility.Return404(context);
                }
                customStyle = entity.CustomStyle;
                //通过QueryString 传递PresentAreaKey、OwnerId 获取css (使用客户端缓存)
                DateTime lastModified = customStyle.LastModified.ToUniversalTime();
                if (IsCacheOK(context, lastModified))
                {
                    WebUtility.Return304(context);
                    return;
                }
                context.Response.Cache.SetCacheability(HttpCacheability.Private);
                context.Response.Cache.SetLastModified(lastModified);
                context.Response.Cache.SetETag(lastModified.Ticks.ToString());
                context.Response.Cache.SetAllowResponseInBrowserHistory(true);
                context.Response.Cache.SetValidUntilExpires(true);
            }
            else
            {
                string customStyleJson = context.Request.QueryString.GetString("CustomStyle", string.Empty);
                bool   isDark          = context.Request.QueryString.GetBool("isDark", false);
                Dictionary <string, string> dictionary = Json.Decode <Dictionary <string, string> >(customStyleJson) ?? new Dictionary <string, string>();
                customStyle                = CustomStyle.New();
                customStyle.IsDark         = isDark;
                customStyle.DefinedColours = dictionary;

                context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                //通过QueryString 传递PresentAreaKey、CustomStyle的json数据 获取css(自定义风格时使用,禁用客户端缓存)
            }

            dynamic model = new ExpandoObject();

            if (customStyle.DefinedColours.ContainsKey(ColorLabel.PageBackground.ToString()))
            {
                model.PageBackground = customStyle.DefinedColours[ColorLabel.PageBackground.ToString()];
            }
            else
            {
                model.PageBackground = "#f2e3bf";
            }
            if (customStyle.DefinedColours.ContainsKey(ColorLabel.ContentBackground.ToString()))
            {
                model.ContentBackground = customStyle.DefinedColours[ColorLabel.ContentBackground.ToString()];
            }
            else
            {
                model.ContentBackground = "#f8f0e6";
            }
            if (customStyle.DefinedColours.ContainsKey(ColorLabel.BorderBackground.ToString()))
            {
                model.BorderBackground = customStyle.DefinedColours[ColorLabel.BorderBackground.ToString()];
            }
            else
            {
                model.BorderBackground = "#ebe6d9";
            }
            if (customStyle.DefinedColours.ContainsKey(ColorLabel.MainTextColor.ToString()))
            {
                model.MainTextColor = customStyle.DefinedColours[ColorLabel.MainTextColor.ToString()];
            }
            else
            {
                model.MainTextColor = "#666";
            }
            if (customStyle.DefinedColours.ContainsKey(ColorLabel.SubTextColor.ToString()))
            {
                model.SubTextColor = customStyle.DefinedColours[ColorLabel.SubTextColor.ToString()];
            }
            else
            {
                model.SubTextColor = "#ccc";
            }
            if (customStyle.DefinedColours.ContainsKey(ColorLabel.MainLinkColor.ToString()))
            {
                model.MainLinkColor = customStyle.DefinedColours[ColorLabel.MainLinkColor.ToString()];
            }
            else
            {
                model.MainLinkColor = "#cc6673";
            }
            if (customStyle.DefinedColours.ContainsKey(ColorLabel.SubLinkColor.ToString()))
            {
                model.SubLinkColor = customStyle.DefinedColours[ColorLabel.SubLinkColor.ToString()];
            }
            else
            {
                model.SubLinkColor = "#efc0ca";
            }

            string templateName = "css-dark";

            if (customStyle.IsDark)
            {
                templateName = "css-light";
            }



            XElement rootElement = XElement.Load(WebUtility.GetPhysicalFilePath(string.Format("~/Themes/{0}/Custom/css-{1}.xml", presentAreaKey, customStyle.IsDark ? "dark" : "light")));

            try
            {
                //编译模板
                string result = Razor.Parse(rootElement.Value, model, templateName);

                context.Response.Clear();
                context.Response.ContentType = "text/css";

                StreamWriter sw = new StreamWriter(context.Response.OutputStream, System.Text.Encoding.Default);
                sw.Write(result);
                sw.Flush();
            }
            catch (Exception ex)
            {
                context.Response.Write(ex.ToString());
            }
        }