2004-12-10 Lluis Sanchez Gual <lluis@novell.com>
authorLluis Sanchez <lluis@novell.com>
Fri, 10 Dec 2004 17:52:06 +0000 (17:52 -0000)
committerLluis Sanchez <lluis@novell.com>
Fri, 10 Dec 2004 17:52:06 +0000 (17:52 -0000)
* Menu.cs: Implemented basic rendering. Added some missing properties.
* MenuItem.cs: Improved implementation of Depth.
* Unit.cs: Added serializable attribute.
* TreeView.cs: Moved GetScriptLiteral method to ClientScriptManager,
so it can be reused.
* Menu.js: New script to support he Menu control.

svn path=/trunk/mcs/; revision=37584

mcs/class/System.Web/System.Web.UI.WebControls/ChangeLog
mcs/class/System.Web/System.Web.UI.WebControls/Menu.cs
mcs/class/System.Web/System.Web.UI.WebControls/Menu.js [new file with mode: 0644]
mcs/class/System.Web/System.Web.UI.WebControls/MenuItem.cs
mcs/class/System.Web/System.Web.UI.WebControls/TreeView.cs
mcs/class/System.Web/System.Web.UI.WebControls/Unit.cs

index c21f3b1b58ba6636d7052e5f7c8e9ffd63b14d0c..33e7f121a26b424e62353821d09383e37cc4aa34 100644 (file)
@@ -1,3 +1,12 @@
+2004-12-10 Lluis Sanchez Gual <lluis@novell.com>
+
+       * Menu.cs: Implemented basic rendering. Added some missing properties.
+       * MenuItem.cs: Improved implementation of Depth.
+       * Unit.cs: Added serializable attribute.
+       * TreeView.cs: Moved GetScriptLiteral method to ClientScriptManager,
+       so it can be reused.
+       * Menu.js: New script to support he Menu control.
+
 2004-12-03 Lluis Sanchez Gual <lluis@novell.com>
 
        * MenuEventArgs.cs: Changed to sealed.
index 42f9ae629d48b4d4ea290e6c7fec93068110a9d6..f2c462cf9319b10b2d17b37f0ef77b9a8c6c092d 100644 (file)
@@ -89,6 +89,56 @@ namespace System.Web.UI.WebControls
                        }
                }
 
+               [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 (true)]
                public virtual bool DynamicEnableDefaultPopOutImage {
                        get {
@@ -218,7 +268,7 @@ namespace System.Web.UI.WebControls
 
                protected override object SaveViewState()
                {
-                       object[] states = new object [2];
+                       object[] states = new object [3];
                        states[0] = base.SaveViewState();
                        states[1] = (dataBindings == null ? null : ((IStateManager)dataBindings).SaveViewState());
                        states[2] = (items == null ? null : ((IStateManager)items).SaveViewState());
@@ -245,11 +295,150 @@ namespace System.Web.UI.WebControls
                                ((IStateManager)Items).LoadViewState(states[9]);
                }
                
+               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));
+                               
+                               Page.ClientScript.RegisterStartupScript (typeof(Menu), "", 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;
+               }
+               
                protected override void RenderContents (HtmlTextWriter writer)
                {
-                       writer.WriteLine ("This is a menu");
+                       ArrayList dynamicMenus = new ArrayList ();
+                       
+                       if (Orientation == Orientation.Horizontal) {
+                               writer.AddAttribute ("cellpadding", "0");
+                               writer.AddAttribute ("cellspacing", "0");
+                               writer.AddStyleAttribute ("border-width", "0");
+                               writer.RenderBeginTag (HtmlTextWriterTag.Table);
+                               writer.RenderBeginTag (HtmlTextWriterTag.Tr);
+                       }
+                       
+                       foreach (MenuItem item in Items) {
+                               RenderMenuItem (writer, item, dynamicMenus);
+                       }
+                       
+                       if (Orientation == Orientation.Horizontal) {
+                               writer.RenderEndTag (); // TR
+                               writer.RenderEndTag (); // TABLE
+                       }
+                       
+                       for (int n=0; n<dynamicMenus.Count; n++) {
+                               MenuItem item = (MenuItem) dynamicMenus [n];
+                               writer.AddStyleAttribute ("display", "none");
+                               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);
+                               
+                               foreach (MenuItem mi in item.ChildItems) {
+                                       RenderMenuItem (writer, mi, dynamicMenus);
+                               }
+                               
+                               writer.RenderEndTag (); // DIV
+                       }
+               }
+               
+               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;
+
+                       if (Orientation == Orientation.Vertical) {
+                               writer.AddAttribute ("cellpadding", "0");
+                               writer.AddAttribute ("cellspacing", "0");
+                               writer.AddStyleAttribute ("border-width", "0");
+                               writer.RenderBeginTag (HtmlTextWriterTag.Table);
+                               writer.RenderBeginTag (HtmlTextWriterTag.Tr);
+                       }
+                       
+                       if (item.Depth > 0 && !isDynamicItem) {
+                               for (int n=0; n<item.Depth; n++) {
+                                       writer.RenderBeginTag (HtmlTextWriterTag.Td);
+                                       writer.AddStyleAttribute ("width", StaticSubMenuIndent.ToString ());
+                                       writer.RenderBeginTag (HtmlTextWriterTag.Div);
+                                       writer.RenderEndTag (); // DIV
+                                       writer.RenderEndTag (); // TD
+                               }
+                       }
+                       
+                       writer.RenderBeginTag (HtmlTextWriterTag.Td);
+                       
+                       if (item.NavigateUrl != "") {
+                               writer.AddAttribute ("href", item.NavigateUrl);
+                               if (item.Target != null)
+                                       writer.AddAttribute ("target", item.Target);
+                               writer.AddStyleAttribute ("text-decoration", "none");
+                       }
+                       else {
+                               writer.AddAttribute ("href", GetClientEvent (item, "sel"));
+                               writer.AddStyleAttribute ("text-decoration", "none");
+                       }
+                       
+                       string parentId = item.Parent != null ? "'" + item.Parent.Path + "'" : "null";
+                       if (dynamicChildren) {
+                               writer.AddAttribute ("onmouseover", "javascript:Menu_OverItem ('" + ClientID + "', '" + item.Path + "', " + parentId + ")");
+                               writer.AddAttribute ("onmouseout", "javascript:Menu_OutItem ('" + ClientID + "', '" + item.Path + "')");
+                       } else if (isDynamicItem) {
+                               writer.AddAttribute ("onmouseover", "javascript:Menu_OverLeafItem ('" + ClientID + "', " + parentId + ")");
+                               writer.AddAttribute ("onmouseout", "javascript:Menu_OutItem ('" + ClientID + "', " + parentId + ")");
+                       }
+                       
+                       writer.AddAttribute ("id", GetItemClientId (item, "i"));
+                       writer.RenderBeginTag (HtmlTextWriterTag.A);
+                       writer.Write (item.Text);
+                       writer.RenderEndTag (); // A
+                       
+                       writer.RenderEndTag (); // TD
+                       
+                       if (Orientation == Orientation.Vertical) {
+                               writer.RenderEndTag (); // TR
+                               writer.RenderEndTag (); // TABLE
+                       }
+                       
+                       if (displayChildren) {
+                               if (dynamicChildren) {
+                                       dynamicMenus.Add (item);
+                               } else {
+                                       foreach (MenuItem mi in item.ChildItems) {
+                                               RenderMenuItem (writer, mi, dynamicMenus);
+                                       }
+                               }
+                       }
                }
                
+               string GetItemClientId (MenuItem item, string sufix)
+               {
+                       return ClientID + "_" + item.Path + sufix;
+               }
+                                                       
+               string GetClientEvent (MenuItem item, string ev)
+               {
+                       return Page.GetPostBackClientHyperlink (this, ev + "|" + item.Path);
+               }
        }
 }
 
diff --git a/mcs/class/System.Web/System.Web.UI.WebControls/Menu.js b/mcs/class/System.Web/System.Web.UI.WebControls/Menu.js
new file mode 100644 (file)
index 0000000..b6bbad8
--- /dev/null
@@ -0,0 +1,77 @@
+
+function Menu_OverItem (menuId, itemId, parentId) {
+       var menu = getMenu (menuId);
+       var subm = getSubMenu (menuId, itemId);
+       if (subm.parentMenu == null && parentId != null)
+               subm.parentMenu = getSubMenu (menuId, parentId);
+       var item = getMenuItem (menuId, itemId);
+       
+       var offx; var offy;
+       if (subm.parentMenu != null) {
+               offx = parseInt (subm.parentMenu.style.left);
+               offy = parseInt (subm.parentMenu.style.top);
+       } else {
+               offx = offy = 0;
+       }
+       
+       if (menu.vertical)
+               Menu_Reposition (item, subm, item.offsetWidth + offx, offy);
+       else
+               Menu_Reposition (item, subm, offx, item.offsetHeight + offy);
+       Menu_ShowMenu (subm);
+}
+
+function Menu_OverLeafItem (menuId, parentId) {
+       Menu_ShowMenu (getSubMenu (menuId, parentId));
+}
+
+function Menu_OutItem (menuId, itemId) {
+       Menu_HideMenu (menuId, getSubMenu (menuId, itemId));
+}
+
+function Menu_HideMenu (menuId, subm)
+{
+       var menu = getMenu (menuId);
+       if (subm.timer != null) clearTimeout (subm.timer);
+       subm.timer = setTimeout ("Menu_HideMenuCallback ('" + subm.id + "')", menu.disappearAfter);
+       
+       if (subm.parentMenu != null)
+               Menu_HideMenu (menuId, subm.parentMenu);
+}
+
+function Menu_HideMenuCallback (spanId)
+{
+       var subm = document.getElementById (spanId);
+       subm.style.display = "none";
+       subm.style.visibility = "hidden";
+}
+
+function Menu_ShowMenu (subm)
+{
+       if (subm.timer != null)
+               clearTimeout (subm.timer);
+               
+       subm.style.display = "block";
+       subm.style.visibility = "visible";
+
+       if (subm.parentMenu != null)
+               Menu_ShowMenu (subm.parentMenu);
+}
+
+function Menu_Reposition (it, elem, offx, offy)
+{
+       var le = 0;
+       var to = 0;
+       while (it != null && it.style.position != "absolute") {
+               le += it.offsetLeft;
+               to += it.offsetTop;
+               it = it.offsetParent;
+       }
+       elem.style.left = (le + offx) + "px";
+       elem.style.top = (to + offy) + "px";
+}
+
+function getMenu (menuId) { return eval (menuId + "_data"); }
+function getSubMenu (menuId, itemId) { return document.getElementById (menuId + "_" + itemId + "s"); }
+function getMenuItem (menuId, itemId) { return document.getElementById (menuId + "_" + itemId + "i"); }
+
index cff024bef841086250c27a8d43ac5f290a30995d..20b4ede1dbc7f0a6e2e0932b61c15c972339ccfd 100644 (file)
@@ -91,12 +91,8 @@ namespace System.Web.UI.WebControls
                public int Depth {
                        get {
                                if (depth != -1) return depth;
-                               depth = 0;
-                               MenuItem nod = parent;
-                               while (nod != null) {
-                                       depth++;
-                                       nod = nod.parent;
-                               }
+                               if (Parent == null) depth = 0;
+                               else depth = Parent.Depth + 1;
                                return depth;
                        }
                }
index 9a6f440d5a320817a094f4749fef48988af305df..9afd6f4e58a5eafd8a2ca1fe37c67230ae0a45c8 100644 (file)
@@ -888,27 +888,27 @@ namespace System.Web.UI.WebControls
                        
                        if (EnableClientScript && !Page.ClientScript.IsClientScriptIncludeRegistered (typeof(TreeView), "TreeView.js")) {
                                string url = Page.GetWebResourceUrl (typeof(TreeView), "TreeView.js");
-                               Page.ClientScript.RegisterClientScriptInclude (GetType(), "TreeView.js", url);
+                               Page.ClientScript.RegisterClientScriptInclude (typeof(TreeView), "TreeView.js", url);
                                
                                string ctree = ClientID + "_data";
                                string script = string.Format ("var {0} = new Object ();\n", ctree);
-                               script += string.Format ("{0}.showImage = {1};\n", ctree, GetScriptLiteral (ShowExpandCollapse));
+                               script += string.Format ("{0}.showImage = {1};\n", ctree, ClientScriptManager.GetScriptLiteral (ShowExpandCollapse));
                                
                                if (ShowExpandCollapse) {
                                        bool defaultImages = ShowLines || ImageSet != TreeViewImageSet.Custom || (ExpandImageUrl == "" && CollapseImageUrl == "");
-                                       script += string.Format ("{0}.defaultImages = {1};\n", ctree, GetScriptLiteral (defaultImages));
+                                       script += string.Format ("{0}.defaultImages = {1};\n", ctree, ClientScriptManager.GetScriptLiteral (defaultImages));
                                        ImageStyle imageStyle = GetImageStyle ();
                                        if (!defaultImages) {
-                                               script += string.Format ("{0}.expandImage = {1};\n", ctree, GetScriptLiteral (GetNodeImageUrl ("plus", imageStyle)));
-                                               script += string.Format ("{0}.collapseImage = {1};\n", ctree, GetScriptLiteral (GetNodeImageUrl ("minus", imageStyle)));
+                                               script += string.Format ("{0}.expandImage = {1};\n", ctree, ClientScriptManager.GetScriptLiteral (GetNodeImageUrl ("plus", imageStyle)));
+                                               script += string.Format ("{0}.collapseImage = {1};\n", ctree, ClientScriptManager.GetScriptLiteral (GetNodeImageUrl ("minus", imageStyle)));
                                        }
                                        if (PopulateNodesFromClient)
-                                               script += string.Format ("{0}.noExpandImage = {1};\n", ctree, GetScriptLiteral (GetNodeImageUrl ("noexpand", imageStyle)));
+                                               script += string.Format ("{0}.noExpandImage = {1};\n", ctree, ClientScriptManager.GetScriptLiteral (GetNodeImageUrl ("noexpand", imageStyle)));
                                }
-                               script += string.Format ("{0}.populateFromClient = {1};\n", ctree, GetScriptLiteral (PopulateNodesFromClient));
-                               script += string.Format ("{0}.expandAlt = {1};\n", ctree, GetScriptLiteral (GetNodeImageToolTip (true, null)));
-                               script += string.Format ("{0}.collapseAlt = {1};\n", ctree, GetScriptLiteral (GetNodeImageToolTip (false, null)));
-                               Page.ClientScript.RegisterStartupScript (GetType(), "", script, true);
+                               script += string.Format ("{0}.populateFromClient = {1};\n", ctree, ClientScriptManager.GetScriptLiteral (PopulateNodesFromClient));
+                               script += string.Format ("{0}.expandAlt = {1};\n", ctree, ClientScriptManager.GetScriptLiteral (GetNodeImageToolTip (true, null)));
+                               script += string.Format ("{0}.collapseAlt = {1};\n", ctree, ClientScriptManager.GetScriptLiteral (GetNodeImageToolTip (false, null)));
+                               Page.ClientScript.RegisterStartupScript (typeof(TreeView), "", script, true);
                        }
 
                        if (EnableClientScript) {
@@ -930,19 +930,6 @@ namespace System.Web.UI.WebControls
                                bindings = null;
                }
                
-               string GetScriptLiteral (object ob)
-               {
-                       if (ob is string) {
-                               string s = (string)ob;
-                               s = s.Replace ("\"", "\\\"");
-                               return "\"" + s + "\"";
-                       } else if (ob is bool) {
-                               return ob.ToString().ToLower();
-                       } else {
-                               return ob.ToString ();
-                       }
-               }
-               
                string GetBindingKey (string dataMember, int depth)
                {
                        return dataMember + " " + depth;
index a6c5a3c732da8bd3798a4094e5b4ba7e8e387ce5..6649c0fb08f8712ba103a9d70504b7dba9dcd14b 100644 (file)
@@ -41,6 +41,7 @@ using System.Web.UI;
 namespace System.Web.UI.WebControls\r
 {\r
        [TypeConverter(typeof(UnitConverter))]\r
+       [Serializable]\r
        public struct Unit\r
        {\r
                public static readonly Unit Empty = new Unit();\r