November Happy Hour will be moved to Thursday December 5th.

Edit/admin menu - how does it work?

Vote:
 

I'd like to add a button on a page (for logged in users with correct access rights) to activate on-page edit, the same functionality that exists today in the right click edit/admin menu. I can't quite make it work and thus I have some questions:

  1. What is it that triggers the rendering of the edit/admin menu on right click? And "who" renders it?
  2. Can I have it appear for any user/role? What would I have to do?
  3. Can I control what's on that menu, i.e. add my own items or remove existing?
  4. I tried calling EPi.ope.init() when the user clicks the button, but I get an error. It seems $("body").prepend has not been defined. I suppose I must call something else first to have that work. Any idea what I need to do?

Looking forward to hear from you!

/Kristoffer

#57921
Apr 03, 2012 13:50
Vote:
 

1. The menu is added as a page extension by the constructor of the TemplatePage class (http://sdk.episerver.com/library/cms6/html/T_EPiServer_Web_PageExtensions_ContextMenu.htm). This will add a web control that adds the items and does the rendering. You can access the plug-in using the property ContextMenu on any class that inherits from PageBase.

2. The menu appears for users that have either publish access rights to the current page or access to the backing UI (edit, admin etc).

3. I don't think that this is publicly documented but I guess you could add items to the RightClickMenu class that can be accessed by the Menu property on ContextMenu.

4. No idea here...

Regards
Linus Ekström

#57926
Apr 03, 2012 17:27
Vote:
 

You can add or remove context menu items. Please look at this: http://sdk.episerver.com/library/cms5/Developers%20Guide/How%20To/Add%20and%20Remove%20Right-click%20Menu%20Items.htm

Here is sample code to add one custom item to context menu. You can easily extend it to add/remove/disable few items. This plug-in can be  enabled/disabled in CMS Admin mode / Confug / Plug-in Manager.

/// <summary>
/// Sample plug-in to add cutom item to context menu
/// </summary>
[PagePlugIn]
public class CustomContextMenuPlugIn
{
    /// <summary>
    /// Initializes plug-in with the specified option flag.
    /// </summary>
    /// <param name="optionFlag">The option flag.</param>
    public static void Initialize(int optionFlag)
    {
        PageBase.PageSetup += new PageSetupEventHandler(OnPageSetup);
    }

    /// <summary>
    /// Gets a value indicating whether this <see cref="CustomContextMenuPlugIn"/> is enabled.
    /// </summary>
    /// <value>
    ///   <c>true</c> if enabled; otherwise, <c>false</c>.
    /// </value>
    private static bool Enabled
    {
        get
        {
            PlugInDescriptor descriptor = PlugInDescriptor.Load(typeof(CustomContextMenuPlugIn));
            return descriptor != null && descriptor.Enabled;
        }
    }

    /// <summary>
    /// Called on page setup event.
    /// </summary>
    /// <param name="page">The page.</param>
    /// <param name="e">The <see cref="EPiServer.PageSetupEventArgs"/> instance containing the event data.</param>
    public static void OnPageSetup(PageBase page, PageSetupEventArgs e)
    {
        if (!Enabled)
        {
            return;
        }
        // TODO: check security here...
        page.Init += new EventHandler(OnPageInit);
    }

    /// <summary>
    /// Called on page init event.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    static void OnPageInit(object sender, EventArgs e)
    {
        var menuItemName = "CustomMenuItem";
        PageBase page = sender as PageBase;
        if (page == null || page.ContextMenu == null || !page.ContextMenu.IsMenuEnabled)
        {
            return;
        }
        if (page.ContextMenu.Menu.Items == null || page.ContextMenu.Menu.Items.Contains(menuItemName))
        {
            return;
        }

        var menuItem = page.ContextMenu.CreateMenuItem(
            "Menu item title",
            UriSupport.ResolveUrlBySettings("~/your/path"),
            "alert('Hello menu item!')",
            "true",
            UriSupport.ResolveUrlBySettings("~/Resources/CustomMenuIcon.gif"),
            RightClickMode.All);
        page.ContextMenu.Menu.Add(menuItemName, menuItem);
    }
}

    

#57940
Apr 04, 2012 11:02
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.