DMenu.prototype.drawMenu = function (oWhere) {
var oThis = this;
if (this.parentMenu) {
this.level = this.parentMenu.level + 1;
this.root = this.parentMenu.root;
}
var oTable = document.createElement("table");
oTable.className = "DMenu";
oTable.style.zIndex = 9999 + this.level;
oTable.style.visibility = "hidden";
oTable.cellSpacing = oTable.cellPadding = oTable.border = 0;
oTable.appendChild(document.createElement("tbody"));
var oBody = oTable.tBodies[0];
for (var i=0; i<this.childNodes.length; i++) {
var o = this.childNodes[i];
if (o instanceof DMenuItem) {
var oRow = document.createElement("tr");
oRow.className = "DMenuItem";
oRow.style.MozUserSelect = "none";
oRow.onselectstart = oRow.oncontextmenu = function(){ return false; };
oRow.onmouseover = function(x){ return (function(){ oThis.MenuOver(x); }) }(i);
oRow.onmouseout = function(x){ return (function(){ oThis.MenuOut(x); }) }(i);
oRow.onclick = function(x, e) { return (function(e){ oThis.MenuClick(x, e); }) }(i);
var oIcon = document.createElement("td");
oIcon.className = "DMenuItem-icon-cell";
if (o.icon) {
var m = document.createElement("img");
m.src = o.icon;
oIcon.appendChild(m);
}
var oLabel = document.createElement("td");
oLabel.className = "DMenuItem-label-cell";
oLabel.innerHTML = o.text;
var oMore = document.createElement("td");
oMore.className = "DMenuItem-more-cell";
if (o.hasMenu()) {
var m = document.createElement("img");
m.src = DMenuConfig.imagePath + DMenuConfig.moreIcon;
oMore.appendChild(m);
}
oRow.appendChild(oIcon);
oRow.appendChild(oLabel);
oRow.appendChild(oMore);
o.element = oRow;
oBody.appendChild(oRow);
if (o.hasMenu()) {
o.subMenu.drawMenu(document.body);
}
} else {
var oRow = document.createElement("tr");
var oSeparator = document.createElement("td");
oSeparator.colSpan = 3;
oSeparator.className = "DMenuItem-separator";
oSeparator.appendChild(document.createElement("div"));
oRow.appendChild(oSeparator);
oBody.appendChild(oRow);
}
}
oWhere.appendChild(oTable);
this.element = oTable;
this.body = oBody;
};
DMenu.prototype.add = function (oItem) {
this.childNodes[this.childNodes.length] = oItem;
oItem.thisMenu = this;
if (oItem instanceof DMenuItem) {
if (oItem.hasMenu()) {
oItem.subMenu.parentMenu = this;
}
}
return oItem;
};
DMenu.prototype.MenuOver = function (x) {
//clearTimeout(this.root._hideTimeout);
var c = this.childNodes[x];
var o = this.body.childNodes[x];
for (var i=0; i<this.childNodes.length; i++) {
var cc = this.childNodes[i];
if(cc instanceof Separator) continue;
if (i != x) {
if (cc.hasMenu()) {
if (cc.subMenu.visible)
cc.subMenu.hide();
}
}
}
if (c.hasMenu()) {
var ref = function(){ c.subMenu.show(); };
this.root._showTimeout = setTimeout(ref, DMenuConfig.showDelay);
}
o.className = "DMenuItem DMenuItem-hover";
};
DMenu.prototype.MenuOut = function (x) {
clearTimeout(this.root._showTimeout);
var c = this.childNodes[x];
var o = this.body.childNodes[x];
var oThis = this;
//var ref = function(){ oThis._hideAll(); }
//this.root._hideTimeout = setTimeout(ref, DMenuConfig.hideDelay);
if (c.hasMenu()) {
if (!c.subMenu.visible) o.className = "DMenuItem";
} else {
o.className = "DMenuItem";
}
};
DMenu.prototype.MenuClick = function (x, e) {
e = e || window.event;
var o = this.childNodes[x];
if (typeof o.action == "function") {
o.action();
} else {
eval(o.action);
}
this.hideAll();
if (ie) e.cancelBubble = true;
else e.stopPropagation();
};
DMenu.prototype.setVisible = function (v) {
this.visible = v;
var o = this.element;
if (ie55up) {
if (o.filters[0]) {
o.filters[0].Stop();
o.filters[0].Apply();
o.style.visibility = v ? "visible" : "hidden";
o.filters[0].Play();
} else {
o.style.visibility = v ? "visible" : "hidden";
}
} else {
o.style.visibility = v ? "visible" : "hidden";
}
};
DMenu.prototype.show = function () {
var s = this.element.style;
var p = this.parentMenu;
var o = this.parentNode;
var b = p.getBoundary();
var t = this.getBoundary();
var y = b.top + o.element.offsetTop;
var x = b.left + b.width;
var docwidth = ie ? document.body.clientWidth : window.innerWidth;
var docheight = ie ? document.body.clientHeight : window.innerHeight;
var scrollX = ie ? document.body.scrollLeft : window.pageXOffset;
var scrollY = ie ? document.body.scrollTop : window.pageYOffset;
if (t.width + x - docwidth - scrollX> 0)
x = b.left - t.width;
if (x < scrollX) x = scrollX;
if (t.height + y - docheight - scrollY > 0)
y = docheight + scrollY - t.height;
if (y < scrollY) y = scrollY;
s.left = x + "px";
s.top = y + "px";
this.setVisible(true);
this.parentNode.element.className = "DMenuItem DMenuItem-hover";
this.hideSubs();
if (typeof this.onshow == 'function')
this.onshow();
else
eval(this.onshow);
};
DMenu.prototype.hideAll = function () {
var o = this;
while (o.parentMenu)
o = o.parentMenu;
o.hideSubs();
};
DMenu.prototype.hide = function () {
var o = this.parentNode;
o.element.className = "DMenuItem";
this.setVisible(false);
this.hideSubs();
if (typeof this.onhide == 'function')
this.onhide();
else
eval(this.onhide);
};
DMenu.prototype.hideSubs = function () {
for (var i=0; i<this.childNodes.length; i++) {
var c = this.childNodes[i]
if( c instanceof Separator) continue;
c.element.className = "DMenuItem";
if (c.hasMenu())
if (c.subMenu.visible) c.subMenu.hide();
}
};
DMenu.prototype.getBoundary = function () {
var o = el = this.element;
var x = o.offsetLeft, y = o.offsetTop;
while (o = o.offsetParent) {
x += o.offsetLeft;
y += o.offsetTop;
}
return {
"left": x,
"top": y,
"width": el.offsetWidth,
"height": el.offsetHeight
};
};
DMenuItem.prototype.hasMenu = function () {
var s = this.subMenu;
if (s) {
if (s.childNodes.length == 0) s.add(new DMenuItem(DMenuConfig.defaultEmptyText));
return true;
}
return false;
};