public override void PreRender (Page page, HtmlHead head, ClientScriptManager csm, string cmenu, StringBuilder script) { Menu owner = Owner; MenuItemStyle staticMenuItemStyle = owner.StaticMenuItemStyleInternal; SubMenuStyle staticMenuStyle = owner.StaticMenuStyleInternal; MenuItemStyle dynamicMenuItemStyle = owner.DynamicMenuItemStyleInternal; SubMenuStyle dynamicMenuStyle = owner.DynamicMenuStyleInternal; MenuItemStyleCollection levelMenuItemStyles = owner.LevelMenuItemStyles; List<Style> levelMenuItemLinkStyles = owner.LevelMenuItemLinkStyles; SubMenuStyleCollection levelSubMenuStyles = owner.LevelSubMenuStylesInternal; MenuItemStyle staticSelectedStyle = owner.StaticSelectedStyleInternal; MenuItemStyle dynamicSelectedStyle = owner.DynamicSelectedStyleInternal; MenuItemStyleCollection levelSelectedStyles = owner.LevelSelectedStylesInternal; List<Style> levelSelectedLinkStyles = owner.LevelSelectedLinkStyles; Style staticHoverStyle = owner.StaticHoverStyleInternal; Style dynamicHoverStyle = owner.DynamicHoverStyleInternal; if (!csm.IsClientScriptIncludeRegistered (typeof (Menu), "Menu.js")) { string url = csm.GetWebResourceUrl (typeof (Menu), "Menu.js"); csm.RegisterClientScriptInclude (typeof (Menu), "Menu.js", url); } script.AppendFormat (onPreRenderScript, cmenu, page.IsMultiForm ? page.theForm : "window", ClientScriptManager.GetScriptLiteral (owner.DisappearAfter), ClientScriptManager.GetScriptLiteral (owner.Orientation == Orientation.Vertical)); if (owner.DynamicHorizontalOffset != 0) script.Append (String.Concat (cmenu, ".dho = ", ClientScriptManager.GetScriptLiteral (owner.DynamicHorizontalOffset), ";\n")); if (owner.DynamicVerticalOffset != 0) script.Append (String.Concat (cmenu, ".dvo = ", ClientScriptManager.GetScriptLiteral (owner.DynamicVerticalOffset), ";\n")); // The order in which styles are defined matters when more than one class // is assigned to an element RegisterStyle (owner.PopOutBoxStyle, head); RegisterStyle (owner.ControlStyle, owner.ControlLinkStyle, head); if (staticMenuItemStyle != null) RegisterStyle (owner.StaticMenuItemStyle, owner.StaticMenuItemLinkStyle, head); if (staticMenuStyle != null) RegisterStyle (owner.StaticMenuStyle, head); if (dynamicMenuItemStyle != null) RegisterStyle (owner.DynamicMenuItemStyle, owner.DynamicMenuItemLinkStyle, head); if (dynamicMenuStyle != null) RegisterStyle (owner.DynamicMenuStyle, head); if (levelMenuItemStyles != null && levelMenuItemStyles.Count > 0) { levelMenuItemLinkStyles = new List<Style> (levelMenuItemStyles.Count); foreach (Style style in levelMenuItemStyles) { Style linkStyle = new Style (); levelMenuItemLinkStyles.Add (linkStyle); RegisterStyle (style, linkStyle, head); } } if (levelSubMenuStyles != null) foreach (Style style in levelSubMenuStyles) RegisterStyle (style, head); if (staticSelectedStyle != null) RegisterStyle (staticSelectedStyle, owner.StaticSelectedLinkStyle, head); if (dynamicSelectedStyle != null) RegisterStyle (dynamicSelectedStyle, owner.DynamicSelectedLinkStyle, head); if (levelSelectedStyles != null && levelSelectedStyles.Count > 0) { levelSelectedLinkStyles = new List<Style> (levelSelectedStyles.Count); foreach (Style style in levelSelectedStyles) { Style linkStyle = new Style (); levelSelectedLinkStyles.Add (linkStyle); RegisterStyle (style, linkStyle, head); } } if (staticHoverStyle != null) { if (head == null) throw new InvalidOperationException ("Using Menu.StaticHoverStyle requires Page.Header to be non-null (e.g. <head runat=\"server\" />)."); RegisterStyle (staticHoverStyle, owner.StaticHoverLinkStyle, head); script.Append (String.Concat (cmenu, ".staticHover = ", ClientScriptManager.GetScriptLiteral (staticHoverStyle.RegisteredCssClass), ";\n")); script.Append (String.Concat (cmenu, ".staticLinkHover = ", ClientScriptManager.GetScriptLiteral (owner.StaticHoverLinkStyle.RegisteredCssClass), ";\n")); } if (dynamicHoverStyle != null) { if (head == null) throw new InvalidOperationException ("Using Menu.DynamicHoverStyle requires Page.Header to be non-null (e.g. <head runat=\"server\" />)."); RegisterStyle (dynamicHoverStyle, owner.DynamicHoverLinkStyle, head); script.Append (String.Concat (cmenu, ".dynamicHover = ", ClientScriptManager.GetScriptLiteral (dynamicHoverStyle.RegisteredCssClass), ";\n")); script.Append (String.Concat (cmenu, ".dynamicLinkHover = ", ClientScriptManager.GetScriptLiteral (owner.DynamicHoverLinkStyle.RegisteredCssClass), ";\n")); } }
/// <summary> /// 添加样式脚本。 /// </summary> /// <param name="scriptManger">ClientScriptManager。</param> protected virtual void AddScript(ClientScriptManager scriptManger) { string[] scripts = this.ModulePage.WebScriptPaths; if (scripts != null && scripts.Length > 0 && scriptManger!= null) { foreach (string path in scripts) { if (!string.IsNullOrEmpty(path)) { string strKey = HashCrypto.Hash(path, "md5"); if(!scriptManger.IsClientScriptIncludeRegistered(strKey)) scriptManger.RegisterClientScriptInclude(strKey, this.Page.ResolveUrl(path)); } } } }
public override void PreRender (Page page, HtmlHead head, ClientScriptManager csm, string cmenu, StringBuilder script) { Menu owner = Owner; script.AppendFormat ("new Sys.WebForms.Menu ({{ element: '{0}', disappearAfter: {1}, orientation: '{2}', tabIndex: {3}, disabled: {4} }});", owner.ClientID, ClientScriptManager.GetScriptLiteral (owner.DisappearAfter), owner.Orientation.ToString ().ToLowerInvariant (), ClientScriptManager.GetScriptLiteral (owner.TabIndex), (!owner.Enabled).ToString ().ToLowerInvariant ()); Type mt = typeof (Menu); if (!csm.IsClientScriptIncludeRegistered (mt, "MenuModern.js")) { string url = csm.GetWebResourceUrl (mt, "MenuModern.js"); csm.RegisterClientScriptInclude (mt, "MenuModern.js", url); } if (!owner.IncludeStyleBlock) return; if (head == null) throw new InvalidOperationException ("Using Menu.IncludeStyleBlock requires Page.Header to be non-null (e.g. <head runat=\"server\" />)."); StyleBlock block = new StyleBlock (owner.ClientID); Style style = owner.ControlStyle; bool horizontal = owner.Orientation == Orientation.Horizontal; if (style != null) block.RegisterStyle (style); // #MenuId img.icon { border-style:none;vertical-align:middle; } block.RegisterStyle (HtmlTextWriterStyle.BorderStyle, "none", "img.icon") .Add (HtmlTextWriterStyle.VerticalAlign, "middle"); // #MenuId img.separator { border-style:none;display:block; } block.RegisterStyle (HtmlTextWriterStyle.BorderStyle, "none", "img.separator") .Add (HtmlTextWriterStyle.Display, "block"); // #MenuId img.horizontal-separator { border-style:none;vertical-align:middle; } if (horizontal) block.RegisterStyle (HtmlTextWriterStyle.BorderStyle, "none", "img.horizontal-separator") .Add (HtmlTextWriterStyle.VerticalAlign, "middle"); // #MenuId ul { list-style:none;margin:0;padding:0;width:auto; } block.RegisterStyle (HtmlTextWriterStyle.ListStyleType, "none", "ul") .Add (HtmlTextWriterStyle.Margin, "0") .Add (HtmlTextWriterStyle.Padding, "0") .Add (HtmlTextWriterStyle.Width, "auto"); SubMenuStyle sms = owner.StaticMenuStyleInternal; if (sms != null) { // #MenuId ul.static { ... } block.RegisterStyle (sms, "ul.static"); } // #MenuId ul.dynamic { ...; z-index:1; ... } NamedCssStyleCollection css = block.RegisterStyle ("ul.dynamic"); sms = owner.DynamicMenuStyleInternal; if (sms != null) { sms.ForeColor = Color.Empty; css.Add (sms); } css.Add (HtmlTextWriterStyle.ZIndex, "1"); int num = owner.DynamicHorizontalOffset; if (num != 0) css.Add (HtmlTextWriterStyle.MarginLeft, num + "px"); num = owner.DynamicVerticalOffset; if (num != 0) css.Add (HtmlTextWriterStyle.MarginTop, num + "px"); // BUG: rendering of LevelSubMenuStyles throws InvalidCastException on .NET // but I suspect the code it is supposed to generate is as follows: // // #MenuId ul.levelX { ... } // // so we will just ignore the bug and go with the above code. RenderLevelStyles (block, num, owner.LevelSubMenuStyles, "ul.level"); // #MenuId a { text-decoration:none;white-space:nowrap;display:block; } block.RegisterStyle (HtmlTextWriterStyle.TextDecoration, "none", "a") .Add (HtmlTextWriterStyle.WhiteSpace, "nowrap") .Add (HtmlTextWriterStyle.Display, "block"); // #MenuId a.static { ... } RenderAnchorStyle (block, owner.StaticMenuItemStyleInternal, "a.static"); // #MenuId a.popout { background-image:url("...");background-repeat:no-repeat;background-position:right center;padding-right:14px; } bool needDynamicPopOut = false; string str = owner.StaticPopOutImageUrl; css = null; string urlFormat = "url(\"{0}\")"; if (String.IsNullOrEmpty (str)) { if (owner.StaticEnableDefaultPopOutImage) css = block.RegisterStyle (HtmlTextWriterStyle.BackgroundImage, String.Format (urlFormat, GetArrowResourceUrl (owner)), "a.popout"); else needDynamicPopOut = true; } else { css = block.RegisterStyle (HtmlTextWriterStyle.BackgroundImage, String.Format (urlFormat, str), "a.popout"); needDynamicPopOut = true; } if (css != null) css.Add ("background-repeat", "no-repeat") .Add ("background-position", "right center") .Add (HtmlTextWriterStyle.PaddingRight, "14px"); // #MenuId a.popout-dynamic { background:url("...") no-repeat right center;padding-right:14px; } str = owner.DynamicPopOutImageUrl; bool haveDynamicUrl = !String.IsNullOrEmpty (str); css = null; if (needDynamicPopOut || haveDynamicUrl) { urlFormat = "url(\"{0}\") no-repeat right center"; if (!haveDynamicUrl) { if (owner.DynamicEnableDefaultPopOutImage) css = block.RegisterStyle (HtmlTextWriterStyle.BackgroundImage, String.Format (urlFormat, GetArrowResourceUrl (owner)), "a.popout-dynamic"); } else css = block.RegisterStyle (HtmlTextWriterStyle.BackgroundImage, String.Format (urlFormat, str), "a.popout-dynamic"); } if (css != null) { haveDynamicPopOut = true; css.Add (HtmlTextWriterStyle.PaddingRight, "14px"); } // #MenuId a.dynamic { ... } RenderAnchorStyle (block, owner.DynamicMenuItemStyleInternal, "a.dynamic"); num = owner.StaticDisplayLevels; Unit ssmi = owner.StaticSubMenuIndent; string unitName; double indent; if (ssmi == Unit.Empty) { unitName = "em"; indent = 1; } else { unitName = Unit.GetExtension (ssmi.Type); indent = ssmi.Value; } // #MenuId a.levelX { ... } RenderLevelStyles (block, num, owner.LevelMenuItemStyles, "a.level", unitName, indent); // #MenuId a.selected.levelX { ... } RenderLevelStyles (block, num, owner.LevelSelectedStyles, "a.selected.level"); // #MenuId a.static.selected { ...;text-decoration:none; } RenderAnchorStyle (block, owner.StaticSelectedStyleInternal, "a.static.selected"); // #MenuId a.dynamic.selected { ...;text-decoration:none;border-style:none; } RenderAnchorStyle (block, owner.DynamicSelectedStyleInternal, "a.dynamic.selected"); // #MenuId a.static.highlighted { ... } style = owner.StaticHoverStyleInternal; if (style != null) block.RegisterStyle (style, "a.static.highlighted"); // #MenuId a.dynamic.highlighted { ... } style = owner.DynamicHoverStyleInternal; if (style != null) block.RegisterStyle (style, "a.dynamic.highlighted"); head.Controls.Add (block); }