Click here to Skip to main content
15,881,248 members
Articles / Web Development / HTML

MenuPilot 1.0 (Open-Source Context Menu for ASP.NET 2.0)

Rate me:
Please Sign up or sign in to vote.
4.94/5 (60 votes)
23 Dec 20063 min read 161.3K   1.2K   195  
Fine DHTML context menu with layout of Action Lists/Smart Tags known from Visual Studio .NET 2005
/*
Acknowledgements:
IE <select> bug - http://dotnetjunkies.com/WebLog/jking/archive/2003/10/30/2975.aspx
  
 */
/*
Copyright � 2006 Annpoint, s.r.o.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-------------------------------------------------------------------------

NOTE: Reuse requires the following acknowledgement (see also NOTICE):
This product includes DayPilot (http://www.menupilot.org) developed by Annpoint, s.r.o.
*/

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using System.Drawing;

namespace MenuPilot.Web.Ui
{
    [PersistChildren(false)]
    [ParseChildren(true, "MenuItems")]
    [DefaultProperty(null)]
    [Designer(typeof(ControlDesigner))]
    [ToolboxBitmap(typeof(HyperLink))]
    public class MenuPilotHyperlink : HyperLink
    {
        private MenuItemCollection items = new MenuItemCollection();

        [PersistenceMode(PersistenceMode.Attribute)]
        public override string Text
        {
            get
            {
                string text = ViewState["Text"] as string;

                if (text == null || text.Trim().Length == 0)
                {
                    if (this.DesignMode)
                    {
                        return "[" + this.ID + "]";
                    }
                    else
                    {
                        return String.Empty;
                    }
                }
                return text;
            }
            set
            {
                ViewState["Text"] = value;
            }

        }
        
        [Category("Appearance")]
        [Description("Title of the action menu.")]
        public string MenuTitle
        {
            get
            {
                if (ViewState["MenuTitle"] == null)
                    return "Tasks";
                return (string) ViewState["MenuTitle"];
            }
            set
            {
                ViewState["MenuTitle"] = value;
            }
        }



        [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public MenuItemCollection MenuItems
        {
            get { return items; }
        }


        [Category("Behavior"), DefaultValue(500), Description("The time (in milliseconds) after which the action menu disappears.")]
        public int DisappearAfter
        {
            get
            {
                if (ViewState["DisappearAfter"] == null)
                    return 500;
                return (int)ViewState["DisappearAfter"];
            }
            set
            {
                ViewState["DisappearAfter"] = value;
            }
        }

        [Category("Appearance")]
        [TypeConverter(typeof(WebColorConverter))]
        public Color MenuBorderColor
        {
            get
            {
                if (ViewState["MenuBorderColor"] == null)
                    return WebColor.ColorFromString("#ACA899"); ; //#ACA899
                return (Color)ViewState["MenuBorderColor"];
            }
            set
            {
                ViewState["MenuBorderColor"] = value;
            }
        }

        [Category("Appearance")]
        [TypeConverter(typeof(WebColorConverter))]
        public Color MenuBackColor
        {
            get 
            {
                if (ViewState["MenuBackColor"] == null)
                    return WebColor.ColorFromString("#F0EEE1"); ; 
                return (Color)ViewState["MenuBackColor"];
            }
            set
            {
                ViewState["MenuBackColor"] = value;
            }
        }

        [Category("Appearance")]
        [TypeConverter(typeof(WebColorConverter))]
        public Color MenuTitleBackColor
        {
            get
            {
                if (ViewState["MenuTitleBackColor"] == null)
                    return WebColor.ColorFromString("#C1D2EE"); ;
                return (Color)ViewState["MenuTitleBackColor"];

            }
            set
            {
                ViewState["MenuTitleBackColor"] = value;
            }
        }

        [Category("Appearance")]
        public string MenuFontSize
        {
            get
            {
                if (ViewState["MenuFontSize"] == null)
                    return "8pt";
                return (string)ViewState["MenuFontSize"];
            }
            set
            {
                ViewState["MenuFontSize"] = value;
            }
        }

        [Category("Appearance")]
        [TypeConverter(typeof(WebColorConverter))]
        public Color MenuActionColor
        {
            get
            {
                if (ViewState["MenuActionColor"] == null)
                    return WebColor.ColorFromString("#2859AB"); ;
                return (Color)ViewState["MenuActionColor"];

            }
            set
            {
                ViewState["MenuActionColor"] = value;
            }
        }

        [Category("Appearance")]
        public string HintIcon
        {
            get
            {
                if (ViewState["HintIcon"] == null)
                    return "action.gif";
                return (string)ViewState["HintIcon"];
            }
            set
            {
                ViewState["HintIcon"] = value;
            }
        }

        [Category("Appearance")]
        public int HintIconWidth
        {
            get
            {
                if (ViewState["HintIconWidth"] == null)
                    return 11;
                return (int)ViewState["HintIconWidth"];
            }
            set
            {
                ViewState["HintIconWidth"] = value;
            }
        }

        [Category("Appearance")]
        public int HintIconHeight
        {
            get
            {
                if (ViewState["HintIconHeight"] == null)
                    return 11;
                return (int)ViewState["HintIconHeight"];
            }
            set
            {
                ViewState["HintIconHeight"] = value;
            }
        }

        [Description("Value that will be used for menu items. Any '{0}' string in the menu item value will be replaced by this property.")]
        public string Value
        {
            get
            {
                if (ViewState["Value"] == null)
                    return null;
                return (string)ViewState["Value"];
            }
            set
            {
                ViewState["Value"] = value;
            }
        }

        [Category("Behavior")]
        [DefaultValue(100)]
        [Description("The time (in milliseconds) after which the hint icon appears.")]
        public int AppearAfter
        {
            get
            {
                if (ViewState["AppearAfter"] == null)
                    return 100;
                return (int)ViewState["AppearAfter"];
            }
            set
            {
                ViewState["AppearAfter"] = value;
            }
        }


        protected override void OnLoad(System.EventArgs e)
        {
            if (!Page.ClientScript.IsStartupScriptRegistered("MenuPilot"))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "MenuPilot", JavaScript.GetCode());
            }

            base.OnLoad(e);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            string mainId = UniqueID + "AL";

            // <span> main
            writer.AddAttribute("onmouseover", JavaScript.Prefix + "activateLabel(this, " + AppearAfter + ");");
            writer.AddAttribute("onmouseout", JavaScript.Prefix + "clearAll(" + DisappearAfter + ");");
            writer.AddAttribute("id", mainId);
            writer.RenderBeginTag("span");

            base.Render(writer);

            // <span> relative
            writer.AddStyleAttribute("position", "relative");
            writer.RenderBeginTag("span");

            //<span> down
            writer.AddAttribute("id", mainId + "down");
            writer.AddAttribute("onclick", JavaScript.Prefix + "activateMenu(this);");
            writer.AddAttribute("onmouseout", JavaScript.Prefix + "clearAll(" + DisappearAfter + ");");
            writer.AddStyleAttribute("position", "absolute");
            writer.AddStyleAttribute("padding-left", "2px");
            writer.AddStyleAttribute("cursor", "hand");
            writer.AddStyleAttribute("cursor", "pointer");
            writer.AddStyleAttribute("display", "none");
            writer.RenderBeginTag("span");

            // <img/>
            writer.AddAttribute("src", ResolveUrl(HintIcon));
            writer.AddAttribute("alt", "");
            writer.AddAttribute("width", HintIconWidth.ToString());
            writer.AddAttribute("height", HintIconHeight.ToString());
            writer.AddStyleAttribute("vertical-align", "top");
            writer.RenderBeginTag("img");
            writer.RenderEndTag();

            // </span> down
            writer.RenderEndTag();


            if (!this.DesignMode)
            {

                // <span> menu
                writer.AddAttribute("id", mainId + "menu");
                writer.AddAttribute("onmouseover", JavaScript.Prefix + "hold();");
                writer.AddAttribute("onmouseout", JavaScript.Prefix + "clearAll(" + DisappearAfter + ");");
                writer.AddStyleAttribute("position", "absolute");
                writer.AddStyleAttribute("left", "15px");
                writer.AddStyleAttribute("border", "1px solid " + WebColor.StringFromColor(MenuBorderColor));
                writer.AddStyleAttribute("background-color", WebColor.StringFromColor(MenuBackColor));
                writer.AddStyleAttribute("font-size", MenuFontSize);
                writer.AddStyleAttribute("display", "none");
                writer.RenderBeginTag("span");

                // <span> title
                writer.AddStyleAttribute("font-weight", "bold");
                writer.AddStyleAttribute("display", "block");
                writer.AddStyleAttribute("background-color", WebColor.StringFromColor(MenuTitleBackColor));
                writer.AddStyleAttribute("padding", "2px 20px 2px 10px");
                writer.AddStyleAttribute("border-bottom", "1px solid " + WebColor.StringFromColor(MenuBorderColor));
                writer.RenderBeginTag("span");
                writer.Write(MenuTitle);
                writer.RenderEndTag();

                bool separatorBefore = false;
                
                foreach (MenuItem mi in MenuItems)
                {
                    if (mi.Text == "-")
                    {
                        separatorBefore = true;
                    }
                    else
                    {
                        writer.AddAttribute("href", String.Format(mi.NavigateUrl, Value));
                        
                        if (mi.Target != String.Empty)
                        {
                            writer.AddAttribute("target", mi.Target);
                        }
                        
                        if (mi.Title != String.Empty)
                        {
                            writer.AddAttribute("title", mi.Title);
                        }
                        
                        writer.AddStyleAttribute("padding", "2px 20px 2px 10px");
                        writer.AddStyleAttribute("display", "block");
                        writer.AddStyleAttribute("color", WebColor.StringFromColor(MenuActionColor));
                        writer.AddStyleAttribute("text-decoration", "none");
                        writer.AddStyleAttribute("white-space", "nowrap");

                        if (separatorBefore)
                        {
                            writer.AddStyleAttribute("border-top", "1px solid " + WebColor.StringFromColor(MenuBorderColor));
                        }
                        
                        writer.RenderBeginTag("a");
                        writer.Write(mi.Text);
                        writer.RenderEndTag();
                        
                        separatorBefore = false;
                    }
                }

                // </span> menu
                writer.RenderEndTag();
                
                writer.AddAttribute("id", mainId + "if");
                writer.AddAttribute("src", "javascript:false;");
                writer.AddAttribute("scrolling", "no");
                writer.AddAttribute("frameborder", "0");
                writer.AddStyleAttribute("position", "absolute");
                writer.AddStyleAttribute("top", "0px");
                writer.AddStyleAttribute("display", "none");
                writer.RenderBeginTag("iframe");
                writer.RenderEndTag();
            }

            // </span> relative
            writer.RenderEndTag();

            // </span> main
            writer.RenderEndTag();

        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Czech Republic Czech Republic
My open-source event calendar/scheduling web UI components:

DayPilot for JavaScript, Angular, React and Vue

Comments and Discussions