2 lines
11 KiB
JavaScript
2 lines
11 KiB
JavaScript
(function(e){var t=[],n=[],r=activeItem=null,i=e('<div class="menu-div outerbox" style="position:absolute;top:0;left:0;display:none;"><div class="shadowbox1"></div><div class="shadowbox2"></div><div class="shadowbox3"></div></div>')[0],s=e('<ul class="menu-ul innerbox"></ul>')[0],o=e('<li style="position:relative;"><div class="menu-item"></div></li>')[0],u=e('<img class="menu-item-arrow" />')[0],a=e('<div id="root-menu-div" style="position:absolute;top:0;left:0;"></div>'),f={showDelay:200,hideDelay:200,hoverOpenDelay:0,offsetTop:0,offsetLeft:0,minWidth:0,onOpen:null,onClose:null,onClick:null,arrowSrc:null,addExpando:false,copyClassAttr:false};e(function(){a.appendTo("body")});e.extend({MenuCollection:function(e){this.menus=[];this.init(e)}});e.extend(e.MenuCollection,{prototype:{init:function(e){if(e&&e.length){for(var t=0;t<e.length;t++){this.addMenu(e[t]);e[t].menuCollection=this}}},addMenu:function(t){if(t instanceof e.Menu)this.menus.push(t);t.menuCollection=this;var n=this;e(t.target).hover(function(){if(t.visible)return;for(var e=0;e<n.menus.length;e++){if(n.menus[e].visible){n.menus[e].hide();t.show();return}}},function(){})}}});e.extend({Menu:function(t,n,r){this.menuItems=[];this.subMenus=[];this.visible=false;this.active=false;this.parentMenuItem=null;this.settings=e.extend({},f,r);this.target=t;this.$eDIV=null;this.$eUL=null;this.timer=null;this.menuCollection=null;this.openTimer=null;this.init();if(n&&n.constructor==Array)this.addItems(n)}});e.extend(e.Menu,{checkMouse:function(t){var r=t.target;if(n.length&&r==n[0].target)return;while(r.parentNode&&r.parentNode!=a[0])r=r.parentNode;if(!e(n).filter(function(){return this.$eDIV[0]==r}).length){e.Menu.closeAll()}},checkKey:function(t){switch(t.keyCode){case 13:if(activeItem)activeItem.click(t,activeItem.$eLI[0]);break;case 27:e.Menu.closeAll();break;case 37:if(!r)r=n[0];var i=r;if(i&&i.parentMenuItem){var s=i.parentMenuItem;s.$eLI.unbind("mouseout").unbind("mouseover");i.hide();s.hoverIn(true);setTimeout(function(){s.bindHover()})}else if(i&&i.menuCollection){var o,u=i.menuCollection.menus;if((o=e.inArray(i,u))>-1){if(--o<0)o=u.length-1;e.Menu.closeAll();u[o].show();u[o].setActive();if(u[o].menuItems.length)u[o].menuItems[0].hoverIn(true)}}break;case 38:if(r)r.selectNextItem(-1);break;case 39:if(!r)r=n[0];var a,i=r,f=activeItem?activeItem.subMenu:null;if(i){if(f&&f.menuItems.length){f.show();f.menuItems[0].hoverIn()}else if(i=i.inMenuCollection()){var o,u=i.menuCollection.menus;if((o=e.inArray(i,u))>-1){if(++o>=u.length)o=0;e.Menu.closeAll();u[o].show();u[o].setActive();if(u[o].menuItems.length)u[o].menuItems[0].hoverIn(true)}}}break;case 40:if(!r){if(n.length&&n[0].menuItems.length)n[0].menuItems[0].hoverIn()}else r.selectNextItem();break}if(t.keyCode>36&&t.keyCode<41)return false},closeAll:function(){while(n.length)n[0].hide()},setDefaults:function(t){e.extend(f,t)},prototype:{init:function(){var n=this;if(!this.target)return;else if(this.target instanceof e.MenuItem){this.parentMenuItem=this.target;this.target.addSubMenu(this);this.target=this.target.$eLI}t.push(this);this.$eDIV=e(i.cloneNode(1));this.$eUL=e(s.cloneNode(1));this.$eDIV[0].appendChild(this.$eUL[0]);a[0].appendChild(this.$eDIV[0]);if(!this.parentMenuItem){e(this.target).click(function(e){n.onClick(e)}).hover(function(e){n.setActive();if(n.settings.hoverOpenDelay){n.openTimer=setTimeout(function(){if(!n.visible)n.onClick(e)},n.settings.hoverOpenDelay)}},function(){if(!n.visible)e(this).removeClass("activetarget");if(n.openTimer)clearTimeout(n.openTimer)})}else{this.$eDIV.hover(function(){n.setActive()},function(){})}},setActive:function(){if(!this.parentMenuItem)e(this.target).addClass("activetarget");else this.active=true},addItem:function(t){if(t instanceof e.MenuItem){if(e.inArray(t,this.menuItems)==-1){this.$eUL.append(t.$eLI);this.menuItems.push(t);t.parentMenu=this;if(t.subMenu)this.subMenus.push(t.subMenu)}}else{this.addItem(new e.MenuItem(t,this.settings))}},addItems:function(e){for(var t=0;t<e.length;t++){this.addItem(e[t])}},removeItem:function(t){var n=e.inArray(t,this.menuItems);if(n>-1)this.menuItems.splice(n,1);t.parentMenu=null},hide:function(){if(!this.visible)return;var t,i=e.inArray(this,n);this.$eDIV.hide();if(i>=0)n.splice(i,1);this.visible=this.active=false;e(this.target).removeClass("activetarget");for(t=0;t<this.subMenus.length;t++){this.subMenus[t].hide()}for(t=0;t<this.menuItems.length;t++){if(this.menuItems[t].active)this.menuItems[t].setInactive()}if(!n.length)e(document).unbind("mousedown",e.Menu.checkMouse).unbind("keydown",e.Menu.checkKey);if(r==this)r=null;if(this.settings.onClose)this.settings.onClose.call(this)},show:function(t){if(this.visible)return;var r,i=this.parentMenuItem;if(this.menuItems.length){if(i){r=parseInt(i.parentMenu.$eDIV.css("z-index"));this.$eDIV.css("z-index",isNaN(r)?1:r+1)}this.$eDIV.css({visibility:"hidden",display:"block"});if(this.settings.minWidth){if(this.$eDIV.width()<this.settings.minWidth)this.$eDIV.css("width",this.settings.minWidth)}this.setPosition();this.$eDIV.css({display:"none",visibility:""}).show();if(this.settings.onOpen)this.settings.onOpen.call(this)}if(n.length==0)e(document).bind("mousedown",e.Menu.checkMouse).bind("keydown",e.Menu.checkKey);this.visible=true;n.push(this)},setPosition:function(){var t,n,r,i,s,o,u,a=e(window).width(),f=e(window).height(),l=this.parentMenuItem,c=this.$eDIV[0].clientHeight,h=this.$eDIV[0].clientWidth,p;if(l){n=l.$eLI.offset();r=n.left+l.$eLI.width();i=n.top}else{t=e(this.target);n=t.offset();r=n.left+this.settings.offsetLeft;i=n.top+t.height()+this.settings.offsetTop}if(e.fn.scrollTop){o=e(window).scrollTop();if(f<c){i=o}else if(f+o<i+c){if(l){s=l.parentMenu.$eDIV.offset();p=l.parentMenu.$eDIV[0].clientHeight;if(c<=p){i=s.top+p-c}else{i=s.top}if(f+o<i+c){i-=i+c-(f+o)}}else{i-=i+c-(f+o)}}}if(e.fn.scrollLeft){u=e(window).scrollLeft();if(a+u<r+h){if(l){r-=l.$eLI.width()+h;if(r<u)r=u}else{r-=r+h-(a+u)}}}this.$eDIV.css({left:r,top:i})},onClick:function(t){if(this.visible){this.hide();this.setActive()}else{e.Menu.closeAll();this.show(t)}},addTimer:function(e,t){var n=this;this.timer=setTimeout(function(){e.call(n);n.timer=null},t)},removeTimer:function(){if(this.timer){clearTimeout(this.timer);this.timer=null}},selectNextItem:function(e){var t,n=0,r=this.menuItems.length,i=e||1;for(t=0;t<r;t++){if(this.menuItems[t].active){n=t;break}}this.menuItems[n].hoverOut();do{n+=i;if(n>=r)n=0;else if(n<0)n=r-1}while(this.menuItems[n].separator);this.menuItems[n].hoverIn(true)},inMenuCollection:function(){var e=this;while(e.parentMenuItem)e=e.parentMenuItem.parentMenu;return e.menuCollection?e:null},destroy:function(){var n,r;this.hide();if(!this.parentMenuItem)e(this.target).unbind("click").unbind("mouseover").unbind("mouseout");else this.$eDIV.unbind("mouseover").unbind("mouseout");while(this.menuItems.length){r=this.menuItems[0];r.destroy();delete r}if((n=e.inArray(this,t))>-1)t.splice(n,1);if(this.menuCollection){if((n=e.inArray(this,this.menuCollection.menus))>-1)this.menuCollection.menus.splice(n,1)}this.$eDIV.remove()}}});e.extend({MenuItem:function(t,n){if(typeof t=="string")t={src:t};this.src=t.src||"";this.url=t.url||null;this.urlTarget=t.target||null;this.addClass=t.addClass||null;this.data=t.data||null;this.$eLI=null;this.parentMenu=null;this.subMenu=null;this.settings=e.extend({},f,n);this.active=false;this.enabled=true;this.separator=false;this.init();if(t.subMenu)new e.Menu(this,t.subMenu,n)}});e.extend(e.MenuItem,{prototype:{init:function(){var t,n,r=this.src,i=this;this.$eLI=e(o.cloneNode(1));if(this.addClass)this.$eLI[0].setAttribute("class",this.addClass);if(this.settings.addExpando&&this.data)this.$eLI[0].menuData=this.data;if(r==""){this.$eLI.addClass("menu-separator");this.separator=true}else{n=typeof r=="string";if(n&&this.url)r=e('<a href="'+this.url+'"'+(this.urlTarget?'target="'+this.urlTarget+'"':"")+">"+r+"</a>");else if(n||!r.length)r=[r];for(t=0;t<r.length;t++){if(typeof r[t]=="string"){elem=document.createElement("span");elem.innerHTML=r[t];this.$eLI[0].firstChild.appendChild(elem)}else this.$eLI[0].firstChild.appendChild(r[t].cloneNode(1))}}this.$eLI.click(function(e){i.click(e,this)});this.bindHover()},click:function(e,t){if(this.enabled&&this.settings.onClick)this.settings.onClick.call(t,e,this)},bindHover:function(){var e=this;this.$eLI.hover(function(){e.hoverIn()},function(){e.hoverOut()})},hoverIn:function(e){this.removeTimer();var t,n=this.parentMenu.subMenus,i=this.parentMenu.menuItems,s=this;if(this.parentMenu.timer)this.parentMenu.removeTimer();if(!this.enabled)return;for(t=0;t<i.length;t++){if(i[t].active)i[t].setInactive()}this.setActive();r=this.parentMenu;for(t=0;t<n.length;t++){if(n[t].visible&&n[t]!=this.subMenu&&!n[t].timer)n[t].addTimer(function(){this.hide()},n[t].settings.hideDelay)}if(this.subMenu&&!e){this.subMenu.addTimer(function(){this.show()},this.subMenu.settings.showDelay)}},hoverOut:function(){this.removeTimer();if(!this.enabled)return;if(!this.subMenu||!this.subMenu.visible)this.setInactive()},removeTimer:function(){if(this.subMenu){this.subMenu.removeTimer()}},setActive:function(){this.active=true;this.$eLI.addClass("active");var e=this.parentMenu.parentMenuItem;if(e&&!e.active)e.setActive();activeItem=this},setInactive:function(){this.active=false;this.$eLI.removeClass("active");if(this==activeItem)activeItem=null},enable:function(){this.$eLI.removeClass("disabled");this.enabled=true},disable:function(){this.$eLI.addClass("disabled");this.enabled=false},destroy:function(){this.removeTimer();this.$eLI.remove();this.$eLI.unbind("mouseover").unbind("mouseout").unbind("click");if(this.subMenu){this.subMenu.destroy();delete this.subMenu}this.parentMenu.removeItem(this)},addSubMenu:function(t){if(this.subMenu)return;this.subMenu=t;if(this.parentMenu&&e.inArray(t,this.parentMenu.subMenus)==-1)this.parentMenu.subMenus.push(t);if(this.settings.arrowSrc){var n=u.cloneNode(0);n.setAttribute("src",this.settings.arrowSrc);this.$eLI[0].firstChild.appendChild(n)}}}});e.extend(e.fn,{menuFromElement:function(t,n,r){var i=function(n){var r=[],s,o,u,a,f,h,p,d,v=null;u=c(n,"LI");for(f=0;f<u.length;f++){s=[];if(!u[f].childNodes.length){r.push(new e.MenuItem("",t));continue}if(h=l(u[f],"UL")){s=i(h);e(h).remove()}a=e(u[f]);if(a[0].childNodes.length==1&&a[0].childNodes[0].nodeType==3)d=a[0].childNodes[0].nodeValue;else d=a[0].childNodes;if(t&&t.copyClassAttr)v=a.attr("class");o=new e.MenuItem({src:d,addClass:v},t);r.push(o);if(s.length)new e.Menu(o,s,t)}return r};return this.each(function(){var s,o;if(n||(s=l(this,"UL"))){s=n?e(n).clone(true)[0]:s;menuItems=i(s);if(menuItems.length){o=new e.Menu(this,menuItems,t);if(r)r.addMenu(o)}e(s).hide()}})},menuBarFromUL:function(t){return this.each(function(){var n,r=c(this,"LI");if(r.length){bar=new e.MenuCollection;for(n=0;n<r.length;n++)e(r[n]).menuFromElement(t,null,bar)}})},menuBar:function(t,n){return this.each(function(){if(n&&n.constructor==Array)new e.Menu(this,n,t);else{if(this.nodeName.toUpperCase()=="UL")e(this).menuBarFromUL(t);else e(this).menuFromElement(t,n)}})}});var l=function(e,t){if(!e)return null;var n=e.firstChild;for(;n;n=n.nextSibling){if(n.nodeType==1&&n.nodeName.toUpperCase()==t)return n}return null};var c=function(e,t){if(!e)return[];var n=[],r=e.firstChild;for(;r;r=r.nextSibling){if(r.nodeType==1&&r.nodeName.toUpperCase()==t)n[n.length]=r}return n}})(jQuery)
|