Skip to content

Instantly share code, notes, and snippets.

@tomfulton
Created June 18, 2013 15:33
Show Gist options
  • Save tomfulton/5806382 to your computer and use it in GitHub Desktop.
Save tomfulton/5806382 to your computer and use it in GitHub Desktop.
Add a toolbar icon to an Umbraco page
using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using umbraco.BusinessLogic;
using umbraco.presentation.LiveEditing;
using umbraco.presentation.masterpages;
using umbraco.uicontrols;
namespace YourApp
{
public class PageEvents : ApplicationBase
{
public PageEvents()
{
umbracoPage.Load += umbracoPage_Init;
}
void umbracoPage_Init(object sender, EventArgs e)
{
var umbPage = sender as umbracoPage;
if (umbPage == null)
return;
var path = umbPage.Page.Request.Path.ToLower();
if (!path.Contains("someumbracopage.aspx"))
return;
AddToolbarButton(umbPage);
}
private void AddToolbarButton(umbracoPage page)
{
int pageId;
int.TryParse(HttpContext.Current.Request.QueryString["id"], out pageId);
var menu = (ScrollingMenu)Utility.FindControl<Control>((Control c) => c.ClientID.EndsWith("_menu"), page.Page);
if (menu == null)
{
var tabView = (TabView)Utility.FindControl<Control>((Control c) => c.ID == "TabView1", page.Page);
foreach (TabPage page3 in tabView.GetPanels())
{
AddSomeMenuIcon(page3.Menu, page, pageId);
}
}
else
{
AddSomeMenuIcon(menu, page, pageId);
}
// Optional: Register some dirty JS/CSS to fix styling issues
string s = "<script type='text/javascript'>";
s += "$(document).ready(function() {";
s += @"$('.editorIcon[alt]').each(
function() {
if ($(this).attr('alt').indexOf('Icon Title') != -1) {
$(this).css('padding-bottom', '4px').css('cursor', 'pointer'); } });";
s += "});";
s += "</script>";
page.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "jsfixpadding", s);
string strCss = "<style type='text/css'>.mceToolbarExternal{padding-left: 15px;}</style>";
page.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "cssfixtoolbar", strCss);
}
private void AddSomeMenuIcon(ScrollingMenu menu, umbracoPage page, int pageId)
{
MenuIconI ni = menu.NewIcon();
ni.AltText = "Icon Title";
ni.OnClickCommand = "UmbClientMgr.openModalWindow('some-page.aspx', 'Modal Title', true, 600, 500, 0, 0); return false;";
ni.ImageURL = "/umbraco/images/umbraco/some-icon.png");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment