PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/WebRoot/asset/js/ddsmoothmenu.js

https://gitlab.com/ergouge/tea
JavaScript | 137 lines | 112 code | 13 blank | 12 comment | 17 complexity | 98cf12a1206809e7b6b1f53cbca3aa38 MD5 | raw file
  1. //** Smooth Navigational Menu- By Dynamic Drive DHTML code library: http://www.dynamicdrive.com
  2. //** Script Download/ instructions page: http://www.dynamicdrive.com/dynamicindex1/ddlevelsmenu/
  3. //** Menu created: Nov 12, 2008
  4. //** Dec 12th, 08" (v1.01): Fixed Shadow issue when multiple LIs within the same UL (level) contain sub menus: http://www.dynamicdrive.com/forums/showthread.php?t=39177&highlight=smooth
  5. //** Feb 11th, 09" (v1.02): The currently active main menu item (LI A) now gets a CSS class of ".selected", including sub menu items.
  6. //** May 1st, 09" (v1.3):
  7. //** 1) Now supports vertical (side bar) menu mode- set "orientation" to 'v'
  8. //** 2) In IE6, shadows are now always disabled
  9. //** July 27th, 09" (v1.31): Fixed bug so shadows can be disabled if desired.
  10. //** Feb 2nd, 10" (v1.4): Adds ability to specify delay before sub menus appear and disappear, respectively. See showhidedelay variable below
  11. var ddsmoothmenu={
  12. //Specify full URL to down and right arrow images (23 is padding-right added to top level LIs with drop downs):
  13. arrowimages: {down:['downarrowclass', 'down.gif', 23], right:['rightarrowclass', 'right.gif']},
  14. transition: {overtime:300, outtime:300}, //duration of slide in/ out animation, in milliseconds
  15. shadow: {enable:true, offsetx:5, offsety:5}, //enable shadow?
  16. showhidedelay: {showdelay: 100, hidedelay: 200}, //set delay in milliseconds before sub menus appear and disappear, respectively
  17. ///////Stop configuring beyond here///////////////////////////
  18. detectwebkit: navigator.userAgent.toLowerCase().indexOf("applewebkit")!=-1, //detect WebKit browsers (Safari, Chrome etc)
  19. detectie6: document.all && !window.XMLHttpRequest,
  20. getajaxmenu:function($, setting){ //function to fetch external page containing the panel DIVs
  21. var $menucontainer=$('#'+setting.contentsource[0]) //reference empty div on page that will hold menu
  22. $menucontainer.html("Loading Menu...")
  23. $.ajax({
  24. url: setting.contentsource[1], //path to external menu file
  25. async: true,
  26. error:function(ajaxrequest){
  27. $menucontainer.html('Error fetching content. Server Response: '+ajaxrequest.responseText)
  28. },
  29. success:function(content){
  30. $menucontainer.html(content)
  31. ddsmoothmenu.buildmenu($, setting)
  32. }
  33. })
  34. },
  35. buildmenu:function($, setting){
  36. var smoothmenu=ddsmoothmenu
  37. var $mainmenu=$("#"+setting.mainmenuid+">ul") //reference main menu UL
  38. $mainmenu.parent().get(0).className=setting.classname || "ddsmoothmenu"
  39. var $headers=$mainmenu.find("ul").parent()
  40. $headers.hover(
  41. function(e){
  42. $(this).children('a:eq(0)').addClass('selected')
  43. },
  44. function(e){
  45. $(this).children('a:eq(0)').removeClass('selected')
  46. }
  47. )
  48. $headers.each(function(i){ //loop through each LI header
  49. var $curobj=$(this).css({zIndex: 100-i}) //reference current LI header
  50. var $subul=$(this).find('ul:eq(0)').css({display:'block'})
  51. $subul.data('timers', {})
  52. this._dimensions={w:this.offsetWidth, h:this.offsetHeight, subulw:$subul.outerWidth(), subulh:$subul.outerHeight()}
  53. this.istopheader=$curobj.parents("ul").length==1? true : false //is top level header?
  54. $subul.css({top:this.istopheader && setting.orientation!='v'? this._dimensions.h+"px" : 0})
  55. if (smoothmenu.shadow.enable){
  56. this._shadowoffset={x:(this.istopheader?$subul.offset().left+smoothmenu.shadow.offsetx : this._dimensions.w), y:(this.istopheader? $subul.offset().top+smoothmenu.shadow.offsety : $curobj.position().top)} //store this shadow's offsets
  57. if (this.istopheader)
  58. $parentshadow=$(document.body)
  59. else{
  60. var $parentLi=$curobj.parents("li:eq(0)")
  61. $parentshadow=$parentLi.get(0).$shadow
  62. }
  63. this.$shadow=$('<div class="ddshadow'+(this.istopheader? ' toplevelshadow' : '')+'"></div>').prependTo($parentshadow).css({left:this._shadowoffset.x+'px', top:this._shadowoffset.y+'px'}) //insert shadow DIV and set it to parent node for the next shadow div
  64. }
  65. $curobj.hover(
  66. function(e){
  67. var $targetul=$subul //reference UL to reveal
  68. var header=$curobj.get(0) //reference header LI as DOM object
  69. clearTimeout($targetul.data('timers').hidetimer)
  70. $targetul.data('timers').showtimer=setTimeout(function(){
  71. header._offsets={left:$curobj.offset().left, top:$curobj.offset().top}
  72. var menuleft=header.istopheader && setting.orientation!='v'? 0 : header._dimensions.w
  73. menuleft=(header._offsets.left+menuleft+header._dimensions.subulw>$(window).width())? (header.istopheader && setting.orientation!='v'? -header._dimensions.subulw+header._dimensions.w : -header._dimensions.w) : menuleft //calculate this sub menu's offsets from its parent
  74. if ($targetul.queue().length<=1){ //if 1 or less queued animations
  75. $targetul.css({left:menuleft+"px", width:header._dimensions.subulw+'px'}).animate({height:'show',opacity:'show'}, ddsmoothmenu.transition.overtime)
  76. if (smoothmenu.shadow.enable){
  77. var shadowleft=header.istopheader? $targetul.offset().left+ddsmoothmenu.shadow.offsetx : menuleft
  78. var shadowtop=header.istopheader?$targetul.offset().top+smoothmenu.shadow.offsety : header._shadowoffset.y
  79. if (!header.istopheader && ddsmoothmenu.detectwebkit){ //in WebKit browsers, restore shadow's opacity to full
  80. header.$shadow.css({opacity:1})
  81. }
  82. header.$shadow.css({overflow:'', width:header._dimensions.subulw+'px', left:shadowleft+'px', top:shadowtop+'px'}).animate({height:header._dimensions.subulh+'px'}, ddsmoothmenu.transition.overtime)
  83. }
  84. }
  85. }, ddsmoothmenu.showhidedelay.showdelay)
  86. },
  87. function(e){
  88. var $targetul=$subul
  89. var header=$curobj.get(0)
  90. clearTimeout($targetul.data('timers').showtimer)
  91. $targetul.data('timers').hidetimer=setTimeout(function(){
  92. $targetul.animate({height:'hide', opacity:'hide'}, ddsmoothmenu.transition.outtime)
  93. if (smoothmenu.shadow.enable){
  94. if (ddsmoothmenu.detectwebkit){ //in WebKit browsers, set first child shadow's opacity to 0, as "overflow:hidden" doesn't work in them
  95. header.$shadow.children('div:eq(0)').css({opacity:0})
  96. }
  97. header.$shadow.css({overflow:'hidden'}).animate({height:0}, ddsmoothmenu.transition.outtime)
  98. }
  99. }, ddsmoothmenu.showhidedelay.hidedelay)
  100. }
  101. ) //end hover
  102. }) //end $headers.each()
  103. $mainmenu.find("ul").css({display:'none', visibility:'visible'})
  104. },
  105. init:function(setting){
  106. if (typeof setting.customtheme=="object" && setting.customtheme.length==2){ //override default menu colors (default/hover) with custom set?
  107. var mainmenuid='#'+setting.mainmenuid
  108. var mainselector=(setting.orientation=="v")? mainmenuid : mainmenuid+', '+mainmenuid
  109. document.write('<style type="text/css">\n'
  110. +mainselector+' ul li a {background:'+setting.customtheme[0]+';}\n'
  111. +mainmenuid+' ul li a:hover {background:'+setting.customtheme[1]+';}\n'
  112. +'</style>')
  113. }
  114. this.shadow.enable=(document.all && !window.XMLHttpRequest)? false : this.shadow.enable //in IE6, always disable shadow
  115. jQuery(document).ready(function($){ //ajax menu?
  116. if (typeof setting.contentsource=="object"){ //if external ajax menu
  117. ddsmoothmenu.getajaxmenu($, setting)
  118. }
  119. else{ //else if markup menu
  120. ddsmoothmenu.buildmenu($, setting)
  121. }
  122. })
  123. }
  124. } //end ddsmoothmenu variable