2010-01-26 08:57:11 +01:00
|
|
|
MenuSystem = {
|
|
|
|
_layers: [],
|
|
|
|
_overlay: null
|
|
|
|
};
|
|
|
|
|
2010-02-06 00:24:52 +01:00
|
|
|
MenuSystem.showMenu = function(elmt, onDismiss) {
|
2010-04-08 22:16:08 +02:00
|
|
|
if (!MenuSystem._overlay) {
|
2010-01-26 08:57:11 +01:00
|
|
|
MenuSystem._overlay = $('<div> </div>')
|
|
|
|
.addClass("menu-overlay")
|
|
|
|
.appendTo(document.body)
|
|
|
|
.click(MenuSystem.dismissAll);
|
|
|
|
}
|
|
|
|
|
2010-02-08 22:41:49 +01:00
|
|
|
elmt.css("z-index", 1010 + MenuSystem._layers.length).appendTo(document.body);
|
2010-01-26 08:57:11 +01:00
|
|
|
|
|
|
|
var layer = {
|
2010-02-06 00:24:52 +01:00
|
|
|
elmt: elmt,
|
|
|
|
onDismiss: onDismiss
|
2010-01-26 08:57:11 +01:00
|
|
|
};
|
|
|
|
MenuSystem._layers.push(layer);
|
|
|
|
|
|
|
|
var level = MenuSystem._layers.length;
|
|
|
|
|
2010-01-26 20:54:14 +01:00
|
|
|
return level;
|
2010-01-26 08:57:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
MenuSystem.dismissAll = function() {
|
2010-01-26 20:54:14 +01:00
|
|
|
MenuSystem.dismissUntil(0);
|
2010-04-09 03:00:44 +02:00
|
|
|
if (MenuSystem._overlay !== null) {
|
2010-01-26 08:57:11 +01:00
|
|
|
MenuSystem._overlay.remove();
|
|
|
|
MenuSystem._overlay = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-01-26 20:54:14 +01:00
|
|
|
MenuSystem.dismissUntil = function(level) {
|
2010-01-26 08:57:11 +01:00
|
|
|
for (var i = MenuSystem._layers.length - 1; i >= level; i--) {
|
2010-02-06 00:24:52 +01:00
|
|
|
var layer = MenuSystem._layers[i];
|
|
|
|
layer.elmt.remove();
|
2010-04-09 03:14:33 +02:00
|
|
|
layer.onDismiss();
|
2010-01-26 08:57:11 +01:00
|
|
|
}
|
|
|
|
MenuSystem._layers = MenuSystem._layers.slice(0, level);
|
|
|
|
};
|
|
|
|
|
|
|
|
MenuSystem.createMenu = function() {
|
|
|
|
return $('<div></div>').addClass("menu-container");
|
|
|
|
};
|
|
|
|
|
|
|
|
MenuSystem.createMenuItem = function() {
|
|
|
|
return $('<a href="javascript:{}"></a>').addClass("menu-item");
|
|
|
|
};
|
|
|
|
|
|
|
|
MenuSystem.positionMenuAboveBelow = function(menu, elmt) {
|
|
|
|
var offset = elmt.offset();
|
2010-03-22 22:48:36 +01:00
|
|
|
var windowWidth = $(window).width();
|
|
|
|
var windowHeight = $(window).height();
|
2010-03-05 09:11:48 +01:00
|
|
|
|
|
|
|
if (offset.top + elmt.outerHeight() - document.body.scrollTop + menu.outerHeight() > windowHeight - 10) {
|
|
|
|
menu.css("top", (offset.top - menu.outerHeight()) + "px");
|
|
|
|
} else {
|
|
|
|
menu.css("top", (offset.top + elmt.outerHeight()) + "px");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset.left - document.body.scrollLeft + menu.outerWidth() > windowWidth - 10) {
|
|
|
|
menu.css("left", (offset.left + elmt.outerWidth() - menu.outerWidth()) + "px");
|
|
|
|
} else {
|
|
|
|
menu.css("left", offset.left + "px");
|
|
|
|
}
|
2010-01-26 08:57:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
MenuSystem.positionMenuLeftRight = function(menu, elmt) {
|
|
|
|
var offset = elmt.offset();
|
2010-03-22 22:48:36 +01:00
|
|
|
var windowWidth = $(window).width();
|
|
|
|
var windowHeight = $(window).height();
|
2010-03-05 09:11:48 +01:00
|
|
|
|
|
|
|
if (offset.top - document.body.scrollTop + menu.outerHeight() > windowHeight - 10) {
|
|
|
|
menu.css("top", (offset.top + elmt.outerHeight() - menu.outerHeight()) + "px");
|
|
|
|
} else {
|
|
|
|
menu.css("top", offset.top + "px");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset.left + elmt.outerWidth() - document.body.scrollLeft + menu.outerWidth() > windowWidth - 10) {
|
2010-03-10 09:32:20 +01:00
|
|
|
menu.css("left", Math.max(10, offset.left - menu.outerWidth()) + "px");
|
2010-03-05 09:11:48 +01:00
|
|
|
} else {
|
|
|
|
menu.css("left", (offset.left + elmt.outerWidth()) + "px");
|
|
|
|
}
|
2010-01-26 08:57:11 +01:00
|
|
|
};
|
2010-01-26 20:54:14 +01:00
|
|
|
|
|
|
|
MenuSystem.createAndShowStandardMenu = function(items, elmt, options) {
|
|
|
|
options = options || {
|
|
|
|
horizontal: false
|
|
|
|
};
|
|
|
|
|
|
|
|
var menu = MenuSystem.createMenu();
|
2010-02-05 07:29:27 +01:00
|
|
|
if ("width" in options) {
|
|
|
|
menu.width(options.width);
|
|
|
|
}
|
|
|
|
|
2010-01-26 20:54:14 +01:00
|
|
|
var createMenuItem = function(item) {
|
|
|
|
if ("label" in item) {
|
|
|
|
var menuItem = MenuSystem.createMenuItem().appendTo(menu);
|
|
|
|
if ("submenu" in item) {
|
2010-02-07 04:36:40 +01:00
|
|
|
menuItem.html(
|
|
|
|
'<table width="100%" cellspacing="0" cellpadding="0" class="menu-item-layout"><tr>' +
|
|
|
|
'<td>' + item.label + '</td>' +
|
|
|
|
'<td width="1%"><img src="/images/right-arrow.png" /></td>' +
|
|
|
|
'</tr></table>'
|
|
|
|
);
|
|
|
|
menuItem.mouseenter(function() {
|
2010-01-26 20:54:14 +01:00
|
|
|
MenuSystem.dismissUntil(level);
|
2010-02-06 00:24:52 +01:00
|
|
|
|
|
|
|
menuItem.addClass("menu-expanded");
|
|
|
|
|
|
|
|
MenuSystem.createAndShowStandardMenu(
|
|
|
|
item.submenu,
|
|
|
|
this,
|
|
|
|
{
|
|
|
|
horizontal: true,
|
|
|
|
onDismiss: function() {
|
|
|
|
menuItem.removeClass("menu-expanded");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2010-01-26 20:54:14 +01:00
|
|
|
});
|
|
|
|
} else {
|
2010-03-05 09:11:48 +01:00
|
|
|
menuItem.html(item.label).click(function(evt) {
|
|
|
|
item.click.call(this, evt);
|
|
|
|
MenuSystem.dismissAll();
|
|
|
|
});
|
2010-02-02 21:50:35 +01:00
|
|
|
if ("tooltip" in item) {
|
|
|
|
menuItem.attr("title", item.tooltip);
|
|
|
|
}
|
2010-02-07 04:36:40 +01:00
|
|
|
menuItem.mouseenter(function() {
|
2010-02-05 00:38:40 +01:00
|
|
|
MenuSystem.dismissUntil(level);
|
|
|
|
});
|
2010-01-26 20:54:14 +01:00
|
|
|
}
|
2010-02-02 21:50:35 +01:00
|
|
|
} else if ("heading" in item) {
|
|
|
|
$('<div></div>').addClass("menu-section").text(item.heading).appendTo(menu);
|
2010-01-26 20:54:14 +01:00
|
|
|
} else {
|
|
|
|
$('<hr />').appendTo(menu);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (var i = 0; i < items.length; i++) {
|
|
|
|
createMenuItem(items[i]);
|
|
|
|
}
|
|
|
|
|
2010-02-06 00:24:52 +01:00
|
|
|
var level = MenuSystem.showMenu(menu, "onDismiss" in options ? options.onDismiss : function(){});
|
2010-01-26 20:54:14 +01:00
|
|
|
if (options.horizontal) {
|
|
|
|
MenuSystem.positionMenuLeftRight(menu, $(elmt));
|
|
|
|
} else {
|
|
|
|
MenuSystem.positionMenuAboveBelow(menu, $(elmt));
|
|
|
|
}
|
2010-02-08 22:41:49 +01:00
|
|
|
|
|
|
|
return level;
|
2010-01-26 20:54:14 +01:00
|
|
|
};
|