File: /home/investoo.co.uk/public_html/js/Menu.js
/*
Menu
-------------------------------
*/
function Menu(instance){
this.instance = instance;
this.items = new Array();
this.id = null;
this.interval = null;
this.timeout = 500;
this.isOpen = false;
this.operation = 'onmouseover';
this.classes = new Array();
Interface.addListener(this);
}
Menu.prototype.init = function(){
this.build();
}
Menu.prototype.addClass = function(id, name, direction){
this.classes[this.classes.length] = new MenuClass(id, name, direction);
}
Menu.prototype.getClass = function(id){
for(var i=0; i < this.classes.length; i++){
if(id == this.classes[i].id) return this.classes[i];
}
}
Menu.prototype.add = function(id, parent, label, url, icon, classId){
if(parent == null){
this.items[this.items.length] = new MenuItem(id, this, label, url, icon, classId, this.instance);
return true;
} else {
for(var i=0; i < this.items.length; i++){
var node = this.items[i];
if(node.add(id, parent, label, url, icon, classId, this.instance)){
node.hasChildren = true;
return true;
}
}
}
return false;
}
Menu.prototype.onRelease = function(id){
this.isOpen = !this.isOpen;
if(this.isOpen){
this.open(id);
} else {
this.close();
}
}
Menu.prototype.onRightClick = function(id){
this.isOpen = true;
this.open(id);
}
Menu.prototype.onRollOver = function(id){
clearTimeout(this.interval);
if(this.isOpen || (this.operation == 'onmouseover' && !this.isOpen)) this.open(id);
}
Menu.prototype.onRollOut = function(id){
var ref = this;
this.interval = setTimeout(function(){ref.close();}, this.timeout);
}
Menu.prototype.open = function(id){
for(var i=0; i < this.items.length; i++){
if(this.items[i].open(id)) return;
}
}
Menu.prototype.close = function(){
for(var i=0; i < this.items.length; i++){
this.items[i].close();
}
this.isOpen = false;
}
Menu.prototype.closeChildrenExcept = function(id){
for(var i=0; i < this.items.length; i++){
if(this.items[i].id != id) this.items[i].close();
}
}
Menu.prototype.build = function(){
//this.buildChildren(null);
for(var i=0; i < this.items.length; i++){
this.items[i].build();
}
// now we need to build the root
// see buildchildren
}
Menu.prototype.buildChildren = function(id){
if(id == null){
var container = this.instance + '_Root_Container';
} else {
var item = this.getItem(id);
var container = item.container;
}
var div = document.createElement('div');
div.id = container;
div.className = 'menuContainer';
document.body.appendChild(div);
var html = '<ul>';
for(var i=0; i < this.items.length; i++){
if(this.items[i].parentId == id){
html += this.items[i].getHtml();
if(this.items[i].hasChildren) this.buildChildren(this.items[i].id);
}
}
html += '</ul>';
div.innerHTML = html;
}
/*
Menu Class
-----------------------
*/
function MenuClass(id, className, direction){
this.id = id;
this.name = className;
this.direction = direction;
}
/*
Menu Items
-----------------------
*/
function MenuItem(id, parent, label, url, icon, classId, instance){
this.id = id;
this.parent = parent;
this.instance = instance;
this.menuClass = this.getClass(classId);
this.icon = icon;
this.label = label;
this.url = url;
this.container = this.instance + '_' + this.id + '_Container';
this.isOpen = false;
this.items = new Array();
this.hasChildren = false;
this.interval = null;
this.isDisabled = false;
}
MenuItem.prototype.getClass = function(classId){
var menu = eval(this.instance);
var classObject = menu.getClass(classId);
return classObject;
}
MenuItem.prototype.add = function(id, parent, label, url, icon, classId, instance){
if(parent == this.id){
this.items[this.items.length] = new MenuItem(id, this, label, url, icon, classId, instance);
return true;
} else {
for(var i=0; i < this.items.length; i++){
var node = this.items[i];
if(node.add(id, parent, label, url, icon, classId, instance)){
node.hasChildren = true;
return true;
}
}
return false
}
return false;
}
MenuItem.prototype.build = function(){
if(this.hasChildren){
var div = null;
if(!document.getElementById(this.container)){
div = document.createElement('div');
div.id = this.container;
div.className = this.menuClass.name;
document.body.appendChild(div);
} else {
div = document.getElementById(this.container);
}
var html = '<ul>';
for(var i=0; i < this.items.length; i++){
html += this.items[i].getHtml();
this.items[i].build();
}
html += '</ul>';
div.innerHTML = html;
}
}
MenuItem.prototype.closeChildrenExcept = function(id){
for(var i=0; i < this.items.length; i++){
if(this.items[i].id != id) this.items[i].close();
}
}
MenuItem.prototype.open = function(id){
if(id == this.id){
this.rollOver();
this.display(id);
return true;
} else {
for(var i = 0; i < this.items.length; i++){
if(this.items[i].open(id)) return true;
}
return false;
}
}
MenuItem.prototype.rollOver = function(){
var object = document.getElementById(this.id);
object.className = 'over';
if(this.parent.rollOver) this.parent.rollOver();
object = null;
}
MenuItem.prototype.display = function(){
// close all submenus of the parent.
this.parent.closeChildrenExcept(this.id);
if(this.hasChildren){
var object = document.getElementById(this.id);
//object.className = 'open';
var position = this.getPosition(object);
var container = document.getElementById(this.container);
container.style.display = 'block';
container.style.position = 'absolute';
if(this.menuClass.direction == 'down'){
container.style.left = position.x + 'px';
container.style.top = position.y + object.offsetHeight + 'px';
} else if(this.menuClass.direction == 'left'){
container.style.left = position.x + object.offsetWidth + 'px';
container.style.top = position.y + 'px';
} else if(this.menuClass.direction == 'context'){
container.style.left = Mouse._x -3 + 'px';
container.style.top = Mouse._y -3 + 'px';
}
container = null;
object = null;
}
}
MenuItem.prototype.getPosition = function(element){
var top = 0;
var left = 0;
var e = element;
while(e.offsetParent){
top += e.offsetTop;
left += e.offsetLeft;
e = e.offsetParent;
}
top += e.offsetTop;
left += e.offsetLeft;
return {x:left, y:top};
}
MenuItem.prototype.close = function(){
var object = document.getElementById(this.id);
object.className = 'out';
object = null;
if(this.hasChildren){
var container = document.getElementById(this.container);
container.style.display = 'none';
container = null;
}
// close its children
if(this.hasChildren){
for(var i=0; i < this.items.length; i++){
this.items[i].close();
}
}
}
MenuItem.prototype.getHtml = function(){
var html = '<li>';
html += '<a href="'+this.url+'" onmouseover="'+this.instance+'.onRollOver(\''+this.id+'\');" onmouseout="'+this.instance+'.onRollOut(\''+this.id+'\');" id="'+this.id+'">';
html += '<span class="capLeft"></span>';
html += '<span class="label">'+this.label+'</span>';
if(this.hasChildren) html += '<span class="subMenuIcon"></span>';
html += '<span class="capRight"></span>';
html += '</a>';
html += '</li>';
return html;
}