| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988 |
- //
- // System.Web.UI.WebControls.Menu.cs
- //
- // Authors:
- // Lluis Sanchez Gual ([email protected])
- //
- // (C) 2004 Novell, Inc (http://www.novell.com)
- //
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- #if NET_2_0
- using System;
- using System.Collections;
- using System.Text;
- using System.ComponentModel;
- using System.Web.UI;
- using System.Web.Handlers;
- using System.Collections.Specialized;
- using System.IO;
- namespace System.Web.UI.WebControls
- {
- public class Menu : HierarchicalDataBoundControl, IPostBackEventHandler, INamingContainer
- {
- MenuItemStyle dynamicMenuItemStyle;
- MenuItemStyle dynamicMenuStyle;
- MenuItemStyle dynamicSelectedStyle;
- MenuItemStyle staticMenuItemStyle;
- MenuItemStyle staticMenuStyle;
- MenuItemStyle staticSelectedStyle;
- Style staticHoverStyle;
- Style dynamicHoverStyle;
- MenuItemStyleCollection levelMenuItemStyles;
- MenuItemStyleCollection levelSelectedStyles;
-
- MenuItemCollection items;
- MenuItemBindingCollection dataBindings;
- MenuItem selectedItem;
- Hashtable bindings;
-
- private static readonly object MenuItemClickEvent = new object();
-
- public event MenuEventHandler MenuItemClick {
- add { Events.AddHandler (MenuItemClickEvent, value); }
- remove { Events.RemoveHandler (MenuItemClickEvent, value); }
- }
-
- protected virtual void OnMenuItemClick (MenuEventArgs e)
- {
- if (Events != null) {
- MenuEventHandler eh = (MenuEventHandler) Events [MenuItemClickEvent];
- if (eh != null) eh (this, e);
- }
- }
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- [Editor ("System.Web.UI.Design.MenuItemBindingsEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
- public virtual MenuItemBindingCollection DataBindings {
- get {
- if (dataBindings == null) {
- dataBindings = new MenuItemBindingCollection ();
- if (IsTrackingViewState)
- ((IStateManager)dataBindings).TrackViewState();
- }
- return dataBindings;
- }
- }
- [DefaultValue (500)]
- public virtual int DisappearAfter {
- get {
- object o = ViewState ["DisappearAfter"];
- if (o != null) return (int)o;
- return 500;
- }
- set {
- ViewState["DisappearAfter"] = value;
- }
- }
- [DefaultValue ("")]
- [UrlProperty]
- [WebCategory ("Appearance")]
- [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
- public virtual string DynamicBottomSeparatorImageUrl {
- get {
- object o = ViewState ["dbsiu"];
- if (o != null) return (string)o;
- return "";
- }
- set {
- ViewState["dbsiu"] = value;
- }
- }
- [DefaultValue ("")]
- [UrlProperty]
- [WebCategory ("Appearance")]
- [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
- public virtual string DynamicTopSeparatorImageUrl {
- get {
- object o = ViewState ["dtsiu"];
- if (o != null) return (string)o;
- return "";
- }
- set {
- ViewState["dtsiu"] = value;
- }
- }
- [DefaultValue ("")]
- [UrlProperty]
- [WebCategory ("Appearance")]
- [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
- public virtual string StaticBottomSeparatorImageUrl {
- get {
- object o = ViewState ["sbsiu"];
- if (o != null) return (string)o;
- return "";
- }
- set {
- ViewState["sbsiu"] = value;
- }
- }
- [DefaultValue ("")]
- [UrlProperty]
- [WebCategory ("Appearance")]
- [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
- public virtual string StaticTopSeparatorImageUrl {
- get {
- object o = ViewState ["stsiu"];
- if (o != null) return (string)o;
- return "";
- }
- set {
- ViewState["stsiu"] = value;
- }
- }
- [DefaultValue (Orientation.Vertical)]
- public virtual Orientation Orientation {
- get {
- object o = ViewState ["Orientation"];
- if (o != null) return (Orientation) o;
- return Orientation.Vertical;
- }
- set {
- ViewState["Orientation"] = value;
- }
- }
- [DefaultValue (1)]
- public virtual int StaticDisplayLevels {
- get {
- object o = ViewState ["StaticDisplayLevels"];
- if (o != null) return (int)o;
- return 1;
- }
- set {
- if (value < 1) throw new ArgumentOutOfRangeException ();
- ViewState["StaticDisplayLevels"] = value;
- }
- }
- [DefaultValue ("16px")]
- public Unit StaticSubMenuIndent {
- get {
- object o = ViewState ["StaticSubMenuIndent"];
- if (o != null) return (Unit)o;
- return new Unit (16);
- }
- set {
- ViewState["StaticSubMenuIndent"] = value;
- }
- }
- [DefaultValue (3)]
- public virtual int MaximumDynamicDisplayLevels {
- get {
- object o = ViewState ["MaximumDynamicDisplayLevels"];
- if (o != null) return (int)o;
- return 3;
- }
- set {
- if (value < 0) throw new ArgumentOutOfRangeException ();
- ViewState["MaximumDynamicDisplayLevels"] = value;
- }
- }
- [DefaultValue (0)]
- public virtual int DynamicVerticalOffset {
- get {
- object o = ViewState ["DynamicVerticalOffset"];
- if (o != null) return (int)o;
- return 0;
- }
- set {
- ViewState["DynamicVerticalOffset"] = value;
- }
- }
- [DefaultValue (0)]
- public virtual int DynamicHorizontalOffset {
- get {
- object o = ViewState ["DynamicHorizontalOffset"];
- if (o != null) return (int)o;
- return 0;
- }
- set {
- ViewState["DynamicHorizontalOffset"] = value;
- }
- }
- [DefaultValue (true)]
- public virtual bool DynamicEnableDefaultPopOutImage {
- get {
- object o = ViewState ["dedpoi"];
- if (o != null) return (bool)o;
- return true;
- }
- set {
- ViewState["dedpoi"] = value;
- }
- }
- [DefaultValue (true)]
- public virtual bool StaticEnableDefaultPopOutImage {
- get {
- object o = ViewState ["sedpoi"];
- if (o != null) return (bool)o;
- return true;
- }
- set {
- ViewState["sedpoi"] = value;
- }
- }
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [Editor ("System.Web.UI.Design.MenuItemCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
- public virtual MenuItemCollection Items {
- get {
- if (items == null) {
- items = new MenuItemCollection (this);
- if (IsTrackingViewState)
- ((IStateManager)items).TrackViewState();
- }
- return items;
- }
- }
- [DefaultValue ('/')]
- public virtual char PathSeparator {
- get {
- object o = ViewState ["PathSeparator"];
- if(o != null) return (char)o;
- return '/';
- }
- set {
- ViewState ["PathSeparator"] = value;
- }
- }
- [DefaultValue (false)]
- public virtual bool ItemWrap {
- get {
- object o = ViewState ["ItemWrap"];
- if(o != null) return (bool)o;
- return false;
- }
- set {
- ViewState ["ItemWrap"] = value;
- }
- }
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [NotifyParentProperty (true)]
- [DefaultValue (null)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- public virtual MenuItemStyle DynamicMenuItemStyle {
- get {
- if (dynamicMenuItemStyle == null) {
- dynamicMenuItemStyle = new MenuItemStyle ();
- if (IsTrackingViewState)
- dynamicMenuItemStyle.TrackViewState();
- }
- return dynamicMenuItemStyle;
- }
- }
-
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [NotifyParentProperty (true)]
- [DefaultValue (null)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- public virtual MenuItemStyle DynamicSelectedStyle {
- get {
- if (dynamicSelectedStyle == null) {
- dynamicSelectedStyle = new MenuItemStyle ();
- if (IsTrackingViewState)
- dynamicSelectedStyle.TrackViewState();
- }
- return dynamicSelectedStyle;
- }
- }
-
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [NotifyParentProperty (true)]
- [DefaultValue (null)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- public virtual MenuItemStyle DynamicMenuStyle {
- get {
- if (dynamicMenuStyle == null) {
- dynamicMenuStyle = new MenuItemStyle ();
- if (IsTrackingViewState)
- dynamicMenuStyle.TrackViewState();
- }
- return dynamicMenuStyle;
- }
- }
-
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [NotifyParentProperty (true)]
- [DefaultValue (null)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- public virtual MenuItemStyle StaticMenuItemStyle {
- get {
- if (staticMenuItemStyle == null) {
- staticMenuItemStyle = new MenuItemStyle ();
- if (IsTrackingViewState)
- staticMenuItemStyle.TrackViewState();
- }
- return staticMenuItemStyle;
- }
- }
-
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [NotifyParentProperty (true)]
- [DefaultValue (null)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- public virtual MenuItemStyle StaticSelectedStyle {
- get {
- if (staticSelectedStyle == null) {
- staticSelectedStyle = new MenuItemStyle ();
- if (IsTrackingViewState)
- staticSelectedStyle.TrackViewState();
- }
- return staticSelectedStyle;
- }
- }
-
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [NotifyParentProperty (true)]
- [DefaultValue (null)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- public virtual MenuItemStyle StaticMenuStyle {
- get {
- if (staticMenuStyle == null) {
- staticMenuStyle = new MenuItemStyle ();
- if (IsTrackingViewState)
- staticMenuStyle.TrackViewState();
- }
- return staticMenuStyle;
- }
- }
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- public virtual MenuItemStyleCollection LevelMenuItemStyles {
- get {
- if (levelMenuItemStyles == null) {
- levelMenuItemStyles = new MenuItemStyleCollection ();
- if (IsTrackingViewState)
- ((IStateManager)levelMenuItemStyles).TrackViewState();
- }
- return levelMenuItemStyles;
- }
- }
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- public virtual MenuItemStyleCollection LevelSelectedStyles {
- get {
- if (levelSelectedStyles == null) {
- levelSelectedStyles = new MenuItemStyleCollection ();
- if (IsTrackingViewState)
- ((IStateManager)levelSelectedStyles).TrackViewState();
- }
- return levelSelectedStyles;
- }
- }
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [NotifyParentProperty (true)]
- [DefaultValue (null)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- public virtual Style DynamicHoverStyle {
- get {
- if (dynamicHoverStyle == null) {
- dynamicHoverStyle = new Style ();
- if (IsTrackingViewState)
- dynamicHoverStyle.TrackViewState();
- }
- return dynamicHoverStyle;
- }
- }
-
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [NotifyParentProperty (true)]
- [DefaultValue (null)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- public virtual Style StaticHoverStyle {
- get {
- if (staticHoverStyle == null) {
- staticHoverStyle = new Style ();
- if (IsTrackingViewState)
- staticHoverStyle.TrackViewState();
- }
- return staticHoverStyle;
- }
- }
-
- [Browsable (false)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- public MenuItem SelectedItem {
- get { return selectedItem; }
- }
- internal void SetSelectedItem (MenuItem item)
- {
- if (selectedItem == item) return;
- if (selectedItem != null)
- selectedItem.SelectedFlag = false;
- selectedItem = item;
- selectedItem.SelectedFlag = true;
- }
-
- public MenuItem FindItem (string valuePath)
- {
- if (valuePath == null) throw new ArgumentNullException ("valuePath");
- string[] path = valuePath.Split (PathSeparator);
- int n = 0;
- MenuItemCollection col = Items;
- bool foundBranch = true;
- while (col.Count > 0 && foundBranch) {
- foundBranch = false;
- foreach (MenuItem item in col) {
- if (item.Value == path [n]) {
- if (++n == path.Length) return item;
- col = item.ChildItems;
- foundBranch = true;
- break;
- }
- }
- }
- return null;
- }
-
- string GetBindingKey (string dataMember, int depth)
- {
- return dataMember + " " + depth;
- }
-
- internal MenuItemBinding FindBindingForItem (string type, int depth)
- {
- if (bindings == null) return null;
- MenuItemBinding bin = (MenuItemBinding) bindings [GetBindingKey (type, depth)];
- if (bin != null) return bin;
-
- bin = (MenuItemBinding) bindings [GetBindingKey (type, -1)];
- if (bin != null) return bin;
-
- bin = (MenuItemBinding) bindings [GetBindingKey ("", depth)];
- if (bin != null) return bin;
-
- bin = (MenuItemBinding) bindings [GetBindingKey ("", -1)];
- return bin;
- }
-
- protected internal override void PerformDataBinding ()
- {
- base.PerformDataBinding ();
- HierarchicalDataSourceView data = GetData ("");
- IHierarchicalEnumerable e = data.Select ();
- foreach (object obj in e) {
- IHierarchyData hdata = e.GetHierarchyData (obj);
- MenuItem item = new MenuItem ();
- item.Bind (hdata);
- Items.Add (item);
- }
- }
-
- void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
- {
- MenuItem item = FindItemByPos (eventArgument);
- if (item == null) return;
- item.Selected = true;
- OnMenuItemClick (new MenuEventArgs (item));
- }
-
- MenuItem FindItemByPos (string path)
- {
- string[] indexes = path.Split ('_');
- MenuItem item = null;
-
- foreach (string index in indexes) {
- int i = int.Parse (index);
- if (item == null) {
- if (i >= Items.Count) return null;
- item = Items [i];
- } else {
- if (i >= item.ChildItems.Count) return null;
- item = item.ChildItems [i];
- }
- }
- return item;
- }
-
- protected override void TrackViewState()
- {
- EnsureDataBound ();
-
- base.TrackViewState();
- if (dataBindings != null) {
- ((IStateManager)dataBindings).TrackViewState ();
- }
- if (items != null) {
- ((IStateManager)items).TrackViewState();
- }
- if (dynamicMenuItemStyle != null)
- dynamicMenuItemStyle.TrackViewState ();
- if (dynamicMenuStyle != null)
- dynamicMenuStyle.TrackViewState ();
- if (levelMenuItemStyles != null)
- ((IStateManager)levelMenuItemStyles).TrackViewState();
- if (levelSelectedStyles != null)
- ((IStateManager)levelSelectedStyles).TrackViewState();
- if (dynamicSelectedStyle != null)
- dynamicSelectedStyle.TrackViewState();
- if (staticMenuItemStyle != null)
- staticMenuItemStyle.TrackViewState ();
- if (staticMenuStyle != null)
- staticMenuStyle.TrackViewState ();
- if (staticSelectedStyle != null)
- staticSelectedStyle.TrackViewState();
- if (staticHoverStyle != null)
- staticHoverStyle.TrackViewState();
- if (dynamicHoverStyle != null)
- dynamicHoverStyle.TrackViewState();
- }
- protected override object SaveViewState()
- {
- object[] states = new object [13];
- states[0] = base.SaveViewState ();
- states[1] = dataBindings == null ? null : ((IStateManager)dataBindings).SaveViewState();
- states[2] = items == null ? null : ((IStateManager)items).SaveViewState();
- states[3] = dynamicMenuItemStyle == null ? null : dynamicMenuItemStyle.SaveViewState();
- states[4] = dynamicMenuStyle == null ? null : dynamicMenuStyle.SaveViewState();
- states[5] = levelMenuItemStyles == null ? null : ((IStateManager)levelMenuItemStyles).SaveViewState();
- states[6] = levelSelectedStyles == null ? null : ((IStateManager)levelSelectedStyles).SaveViewState();
- states[7] = dynamicSelectedStyle == null ? null : dynamicSelectedStyle.SaveViewState();
- states[8] = (staticMenuItemStyle == null ? null : staticMenuItemStyle.SaveViewState());
- states[9] = staticMenuStyle == null ? null : staticMenuStyle.SaveViewState();
- states[10] = staticSelectedStyle == null ? null : staticSelectedStyle.SaveViewState();
- states[11] = staticHoverStyle == null ? null : staticHoverStyle.SaveViewState();
- states[12] = dynamicHoverStyle == null ? null : dynamicHoverStyle.SaveViewState();
- for (int i = states.Length - 1; i >= 0; i--) {
- if (states [i] != null)
- return states;
- }
- return null;
- }
- protected override void LoadViewState (object savedState)
- {
- if (savedState == null)
- return;
- object [] states = (object []) savedState;
- base.LoadViewState (states[0]);
-
- if (states[1] != null)
- ((IStateManager)dataBindings).LoadViewState(states[1]);
- if (states[2] != null)
- ((IStateManager)Items).LoadViewState(states[2]);
- if (states[3] != null)
- dynamicMenuItemStyle.LoadViewState (states[3]);
- if (states[4] != null)
- dynamicMenuStyle.LoadViewState (states[4]);
- if (states[5] != null)
- ((IStateManager)levelMenuItemStyles).LoadViewState(states[5]);
- if (states[6] != null)
- ((IStateManager)levelSelectedStyles).LoadViewState(states[6]);
- if (states[7] != null)
- dynamicSelectedStyle.LoadViewState (states[7]);
- if (states[8] != null)
- staticMenuItemStyle.LoadViewState (states[8]);
- if (states[9] != null)
- staticMenuStyle.LoadViewState (states[9]);
- if (states[10] != null)
- staticSelectedStyle.LoadViewState (states[10]);
- if (states[11] != null)
- staticHoverStyle.LoadViewState (states[11]);
- if (states[12] != null)
- dynamicHoverStyle.LoadViewState (states[12]);
- }
-
- protected override void OnPreRender (EventArgs e)
- {
- base.OnPreRender (e);
-
- if (!Page.ClientScript.IsClientScriptIncludeRegistered (typeof(Menu), "Menu.js")) {
- string url = Page.GetWebResourceUrl (typeof(Menu), "Menu.js");
- Page.ClientScript.RegisterClientScriptInclude (typeof(Menu), "Menu.js", url);
- }
-
- string cmenu = ClientID + "_data";
- string script = string.Format ("var {0} = new Object ();\n", cmenu);
- script += string.Format ("{0}.disappearAfter = {1};\n", cmenu, ClientScriptManager.GetScriptLiteral (DisappearAfter));
- script += string.Format ("{0}.vertical = {1};\n", cmenu, ClientScriptManager.GetScriptLiteral (Orientation == Orientation.Vertical));
- if (DynamicHorizontalOffset != 0)
- script += string.Format ("{0}.dho = {1};\n", cmenu, ClientScriptManager.GetScriptLiteral (DynamicHorizontalOffset));
- if (DynamicVerticalOffset != 0)
- script += string.Format ("{0}.dvo = {1};\n", cmenu, ClientScriptManager.GetScriptLiteral (DynamicVerticalOffset));
-
- // The order in which styles are defined matters when more than one class
- // is assigned to an element
-
- if (dynamicMenuStyle != null)
- RegisterItemStyle (dynamicMenuStyle);
- if (staticMenuStyle != null)
- RegisterItemStyle (staticMenuStyle);
-
- if (staticMenuItemStyle != null)
- RegisterItemStyle (staticMenuItemStyle);
- if (staticSelectedStyle != null)
- RegisterItemStyle (staticSelectedStyle);
- if (dynamicMenuItemStyle != null)
- RegisterItemStyle (dynamicMenuItemStyle);
- if (dynamicSelectedStyle != null)
- RegisterItemStyle (dynamicSelectedStyle);
- if (levelMenuItemStyles != null)
- foreach (Style style in levelMenuItemStyles)
- RegisterItemStyle (style);
- if (levelSelectedStyles != null)
- foreach (Style style in levelSelectedStyles)
- RegisterItemStyle (style);
-
- if (dynamicHoverStyle != null)
- RegisterItemStyle (dynamicHoverStyle);
- if (staticHoverStyle != null)
- RegisterItemStyle (staticHoverStyle);
- if (staticHoverStyle != null)
- script += string.Format ("{0}.staticHover = {1};\n", cmenu, ClientScriptManager.GetScriptLiteral (staticHoverStyle.RegisteredCssClass));
- if (dynamicHoverStyle != null)
- script += string.Format ("{0}.dynamicHover = {1};\n", cmenu, ClientScriptManager.GetScriptLiteral (dynamicHoverStyle.RegisteredCssClass));
-
- Page.ClientScript.RegisterStartupScript (typeof(Menu), ClientID, script, true);
- if (dataBindings != null && dataBindings.Count > 0) {
- bindings = new Hashtable ();
- foreach (TreeNodeBinding bin in dataBindings) {
- string key = GetBindingKey (bin.DataMember, bin.Depth);
- bindings [key] = bin;
- }
- }
- else
- bindings = null;
- }
-
- void RegisterItemStyle (Style baseStyle)
- {
- Page.Header.StyleSheet.RegisterStyle (baseStyle, this);
- Style ts = new Style ();
- ts.CopyTextStylesFrom (baseStyle);
- Page.Header.StyleSheet.CreateStyleRule (ts, "." + baseStyle.RegisteredCssClass + " A", this);
- }
-
- protected override void RenderContents (HtmlTextWriter writer)
- {
- ArrayList dynamicMenus = new ArrayList ();
-
- RenderMenu (writer, Items, Orientation == Orientation.Vertical, dynamicMenus, false);
-
- for (int n=0; n<dynamicMenus.Count; n++) {
- MenuItem item = (MenuItem) dynamicMenus [n];
- writer.AddStyleAttribute ("visibility", "hidden");
- writer.AddStyleAttribute ("position", "absolute");
- writer.AddStyleAttribute ("left", "0px");
- writer.AddStyleAttribute ("top", "0px");
- writer.AddAttribute ("id", GetItemClientId (item, "s"));
- writer.RenderBeginTag (HtmlTextWriterTag.Div);
-
- RenderMenu (writer, item.ChildItems, true, dynamicMenus, true);
-
- writer.RenderEndTag (); // DIV
- }
- }
-
- void RenderMenu (HtmlTextWriter writer, MenuItemCollection items, bool vertical, ArrayList dynamicMenus, bool dynamic)
- {
- writer.AddAttribute ("cellpadding", "0");
- writer.AddAttribute ("cellspacing", "0");
- if (dynamic && dynamicMenuStyle != null)
- writer.AddAttribute ("class", dynamicMenuStyle.RegisteredCssClass);
- else if (!dynamic && staticMenuStyle != null)
- writer.AddAttribute ("class", staticMenuStyle.RegisteredCssClass);
-
- writer.RenderBeginTag (HtmlTextWriterTag.Table);
- if (!vertical) writer.RenderBeginTag (HtmlTextWriterTag.Tr);
-
- for (int n=0; n<items.Count; n++) {
- MenuItem item = items [n];
- if (n > 0) {
- int itemSpacing = GetItemSpacing (item, dynamic);
- if (itemSpacing != 0) {
- if (vertical) {
- writer.AddAttribute ("height", itemSpacing + "px");
- writer.RenderBeginTag (HtmlTextWriterTag.Tr);
- writer.RenderEndTag ();
- } else {
- writer.AddAttribute ("width", itemSpacing + "px");
- writer.RenderBeginTag (HtmlTextWriterTag.Td);
- writer.RenderEndTag ();
- }
- }
- }
- RenderMenuItem (writer, item, dynamicMenus);
- }
-
- if (!vertical) writer.RenderEndTag (); // TR
- writer.RenderEndTag (); // TABLE
-
- }
-
- void RenderMenuItem (HtmlTextWriter writer, MenuItem item, ArrayList dynamicMenus)
- {
- bool displayChildren = (item.Depth + 1 < StaticDisplayLevels + MaximumDynamicDisplayLevels);
- bool dynamicChildren = displayChildren && (item.Depth + 1 >= StaticDisplayLevels) && item.ChildItems.Count > 0;
- bool isDynamicItem = item.Depth + 1 > StaticDisplayLevels;
- bool vertical = (Orientation == Orientation.Vertical) || isDynamicItem;
- if (vertical)
- writer.RenderBeginTag (HtmlTextWriterTag.Tr);
-
- Style itemStyle = null;
- if (levelMenuItemStyles != null && item.Depth < levelMenuItemStyles.Count)
- itemStyle = levelMenuItemStyles [item.Depth];
- else if (isDynamicItem) {
- if (dynamicMenuItemStyle != null)
- itemStyle = dynamicMenuItemStyle;
- } else {
- if (staticMenuItemStyle != null)
- itemStyle = staticMenuItemStyle;
- }
-
- Style selectedStyle = null;
- if (item == SelectedItem) {
- if (levelSelectedStyles != null && item.Depth < levelSelectedStyles.Count)
- selectedStyle = levelSelectedStyles [item.Depth];
- else if (isDynamicItem) {
- if (dynamicSelectedStyle != null)
- selectedStyle = dynamicSelectedStyle;
- } else {
- if (staticSelectedStyle != null)
- selectedStyle = staticSelectedStyle;
- }
- }
-
- string cls = "";
- if (itemStyle != null) cls += itemStyle.RegisteredCssClass + " ";
- if (selectedStyle != null) cls += selectedStyle.RegisteredCssClass + " ";
- if (cls != "")
- writer.AddAttribute ("class", cls);
-
- string parentId = isDynamicItem ? "'" + item.Parent.Path + "'" : "null";
- if (dynamicChildren) {
- writer.AddAttribute ("onmouseover", string.Format ("javascript:Menu_OverItem ('{0}','{1}',{2})", ClientID, item.Path, parentId));
- writer.AddAttribute ("onmouseout", string.Format ("javascript:Menu_OutItem ('{0}','{1}')", ClientID, item.Path));
- } else if (isDynamicItem) {
- writer.AddAttribute ("onmouseover", string.Format ("javascript:Menu_OverDynamicLeafItem ('{0}','{1}',{2})", ClientID, item.Path, parentId));
- writer.AddAttribute ("onmouseout", string.Format ("javascript:Menu_OutItem ('{0}','{1}',{2})", ClientID, item.Path, parentId));
- } else {
- writer.AddAttribute ("onmouseover", string.Format ("javascript:Menu_OverStaticLeafItem ('{0}','{1}')", ClientID, item.Path));
- writer.AddAttribute ("onmouseout", string.Format ("javascript:Menu_OutItem ('{0}','{1}')", ClientID, item.Path));
- }
-
- writer.AddAttribute ("id", GetItemClientId (item, "i"));
-
- writer.RenderBeginTag (HtmlTextWriterTag.Td);
- // Bottom separator image
- if (isDynamicItem && DynamicTopSeparatorImageUrl != "") {
- writer.AddAttribute ("src", DynamicTopSeparatorImageUrl);
- writer.RenderBeginTag (HtmlTextWriterTag.Img);
- writer.RenderEndTag (); // IMG
- } else if (!isDynamicItem && StaticTopSeparatorImageUrl != "") {
- writer.AddAttribute ("src", StaticTopSeparatorImageUrl);
- writer.RenderBeginTag (HtmlTextWriterTag.Img);
- writer.RenderEndTag (); // IMG
- }
-
- // Menu item box
-
-
- writer.AddAttribute ("cellpadding", "0");
- writer.AddAttribute ("cellspacing", "0");
- writer.AddAttribute ("width", "100%");
- writer.RenderBeginTag (HtmlTextWriterTag.Table);
- writer.RenderBeginTag (HtmlTextWriterTag.Tr);
-
- if (item.Depth > 0 && !isDynamicItem) {
- writer.RenderBeginTag (HtmlTextWriterTag.Td);
- writer.AddStyleAttribute ("width", StaticSubMenuIndent.ToString ());
- writer.RenderBeginTag (HtmlTextWriterTag.Div);
- writer.RenderEndTag (); // DIV
- writer.RenderEndTag (); // TD
- }
-
- if (item.ImageUrl != "") {
- writer.RenderBeginTag (HtmlTextWriterTag.Td);
- RenderItemHref (writer, item);
- writer.RenderBeginTag (HtmlTextWriterTag.A);
- writer.AddAttribute ("src", item.ImageUrl);
- writer.AddAttribute ("border", "0");
- writer.RenderBeginTag (HtmlTextWriterTag.Img);
- writer.RenderEndTag (); // IMG
- writer.RenderEndTag (); // A
- writer.RenderEndTag (); // TD
- }
-
- // Menu item text
-
- writer.AddAttribute ("width", "100%");
- if (!ItemWrap)
- writer.AddAttribute ("nowrap", "nowrap");
- writer.RenderBeginTag (HtmlTextWriterTag.Td);
-
- RenderItemHref (writer, item);
- writer.AddStyleAttribute ("text-decoration", "none");
- writer.RenderBeginTag (HtmlTextWriterTag.A);
- writer.Write (item.Text);
- writer.RenderEndTag (); // A
-
- writer.RenderEndTag (); // TD
-
- // Popup image
-
- if (dynamicChildren && ((isDynamicItem && DynamicEnableDefaultPopOutImage) || (!isDynamicItem && StaticEnableDefaultPopOutImage) || item.PopOutImageUrl != ""))
- {
- writer.RenderBeginTag (HtmlTextWriterTag.Td);
- string src;
- if (item.PopOutImageUrl != "")
- src = item.PopOutImageUrl;
- else
- src = AssemblyResourceLoader.GetResourceUrl (typeof(Menu), "arrow_plus.gif");
- writer.AddAttribute ("src", src);
- writer.AddAttribute ("border", "0");
- writer.RenderBeginTag (HtmlTextWriterTag.Img);
- writer.RenderEndTag (); // IMG
-
- writer.RenderEndTag (); // TD
- }
-
- writer.RenderEndTag (); // TR
- writer.RenderEndTag (); // TABLE
-
- // Bottom separator image
-
- if (isDynamicItem && DynamicBottomSeparatorImageUrl != "") {
- writer.AddAttribute ("src", DynamicBottomSeparatorImageUrl);
- writer.RenderBeginTag (HtmlTextWriterTag.Img);
- writer.RenderEndTag (); // IMG
- } else if (!isDynamicItem && StaticBottomSeparatorImageUrl != "") {
- writer.AddAttribute ("src", StaticBottomSeparatorImageUrl);
- writer.RenderBeginTag (HtmlTextWriterTag.Img);
- writer.RenderEndTag (); // IMG
- }
-
- // Submenu
-
- if (vertical) {
- if (displayChildren) {
- if (dynamicChildren) dynamicMenus.Add (item);
- else {
- writer.AddAttribute ("width", "100%");
- RenderMenu (writer, item.ChildItems, true, dynamicMenus, false);
- }
- }
-
- writer.RenderEndTag (); // TD
- writer.RenderEndTag (); // TR
- } else {
- writer.RenderEndTag (); // TD
-
- writer.RenderBeginTag (HtmlTextWriterTag.Td);
- if (displayChildren) {
- if (dynamicChildren) dynamicMenus.Add (item);
- else RenderMenu (writer, item.ChildItems, false, dynamicMenus, false);
- }
- writer.RenderEndTag (); // TD
- }
- }
-
- int GetItemSpacing (MenuItem item, bool dynamic)
- {
- int itemSpacing;
-
- if (item.Selected) {
- if (levelSelectedStyles != null && item.Depth < levelSelectedStyles.Count) {
- itemSpacing = levelSelectedStyles [item.Depth].ItemSpacing;
- if (itemSpacing != 0) return itemSpacing;
- }
-
- if (dynamic) itemSpacing = DynamicSelectedStyle.ItemSpacing;
- else itemSpacing = StaticSelectedStyle.ItemSpacing;
- if (itemSpacing != 0) return itemSpacing;
- }
-
- if (levelMenuItemStyles != null && item.Depth < levelMenuItemStyles.Count) {
- itemSpacing = levelMenuItemStyles [item.Depth].ItemSpacing;
- if (itemSpacing != 0) return itemSpacing;
- }
-
- if (dynamic) return DynamicMenuItemStyle.ItemSpacing;
- else return StaticMenuItemStyle.ItemSpacing;
- }
-
- void RenderItemHref (HtmlTextWriter writer, MenuItem item)
- {
- if (item.NavigateUrl != "") {
- writer.AddAttribute ("href", item.NavigateUrl);
- if (item.Target != null)
- writer.AddAttribute ("target", item.Target);
- }
- else {
- writer.AddAttribute ("href", GetClientEvent (item));
- }
- }
-
- string GetItemClientId (MenuItem item, string sufix)
- {
- return ClientID + "_" + item.Path + sufix;
- }
-
- string GetClientEvent (MenuItem item)
- {
- return Page.GetPostBackClientHyperlink (this, item.Path);
- }
- }
- }
- #endif
|