You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
9.0 KiB

4 years ago
  1. /* Flex Level Popup Menu v1.0
  2. * Created: Dec 27th, 2009 by DynamicDrive.com. This notice must stay intact for usage
  3. * Author: Dynamic Drive at http://www.dynamicdrive.com/
  4. * Visit http://www.dynamicdrive.com/ for full source code
  5. */
  6. //Version 1.1 (March 5th, 2010): Each flex menu (UL) can now be associated with a link dynamically, and/or defined using JavaScript instead of as markup.
  7. //Version 1.2 (July 1st, 2011): Menu updated to work properly in popular mobile devices such as iPad/iPhone and Android tablets.
  8. //Usage: $(elementselector).addpopupmenu('menuid')
  9. //ie:
  10. //jQuery(document).ready(function($){
  11. //$('a.mylinks').addpopupmenu('popupmenu1') //apply popup menu with ID "popmenu1" to links with class="mylinks"
  12. //})
  13. jQuery.noConflict()
  14. var jquerypopupmenu = {
  15. arrowpath: 'arrow.gif', //full URL or path to arrow image
  16. popupmenuoffsets: [0, 0], //additional x and y offset from mouse cursor for popup menus
  17. animspeed: 200, //reveal animation speed (in milliseconds)
  18. showhidedelay: [150, 150], //delay before menu appears and disappears when mouse rolls over it, in milliseconds
  19. //***** NO NEED TO EDIT BEYOND HERE
  20. startzindex: 1000,
  21. ismobile: navigator.userAgent.match(/(iPad)|(iPhone)|(iPod)|(android)|(webOS)/i) != null, //boolean check for popular mobile browsers
  22. builtpopupmenuids: [], //ids of popup menus already built (to prevent repeated building of same popup menu)
  23. positionul: function ($, $ul, e) {
  24. var istoplevel = $ul.hasClass('jqpopupmenu') //Bool indicating whether $ul is top level popup menu DIV
  25. var docrightedge = $(document).scrollLeft() + $(window).width() - 40 //40 is to account for shadows in FF
  26. var docbottomedge = $(document).scrollTop() + $(window).height() - 40
  27. if (istoplevel) { //if main popup menu DIV
  28. var x = e.pageX + this.popupmenuoffsets[0] //x pos of main popup menu UL
  29. var y = e.pageY + this.popupmenuoffsets[1]
  30. x = (x + $ul.data('dimensions').w > docrightedge) ? docrightedge - $ul.data('dimensions').w : x //if not enough horizontal room to the ridge of the cursor
  31. y = (y + $ul.data('dimensions').h > docbottomedge) ? docbottomedge - $ul.data('dimensions').h : y
  32. }
  33. else { //if sub level popup menu UL
  34. var $parentli = $ul.data('$parentliref')
  35. var parentlioffset = $parentli.offset()
  36. var x = $ul.data('dimensions').parentliw //x pos of sub UL
  37. var y = 0
  38. x = (parentlioffset.left + x + $ul.data('dimensions').w > docrightedge) ? x - $ul.data('dimensions').parentliw - $ul.data('dimensions').w : x //if not enough horizontal room to the ridge parent LI
  39. y = (parentlioffset.top + $ul.data('dimensions').h > docbottomedge) ? y - $ul.data('dimensions').h + $ul.data('dimensions').parentlih : y
  40. }
  41. $ul.css({ left: x, top: y })
  42. },
  43. showbox: function ($, $popupmenu, e) {
  44. clearTimeout($popupmenu.data('timers').hidetimer)
  45. $popupmenu.data('timers').showtimer = setTimeout(function () { $popupmenu.show(jquerypopupmenu.animspeed) }, this.showhidedelay[0])
  46. },
  47. hidebox: function ($, $popupmenu) {
  48. clearTimeout($popupmenu.data('timers').showtimer)
  49. $popupmenu.data('timers').hidetimer = setTimeout(function () { $popupmenu.hide(100) }, this.showhidedelay[1]) //hide popup menu plus all of its sub ULs
  50. },
  51. buildpopupmenu: function ($, $menu, $target) {
  52. $menu.css({ display: 'block', visibility: 'hidden', zIndex: this.startzindex }).addClass('jqpopupmenu').appendTo(document.body)
  53. $menu.bind('mouseenter', function () {
  54. clearTimeout($menu.data('timers').hidetimer)
  55. })
  56. $menu.bind('mouseleave', function () { //hide menu when mouse moves out of it
  57. jquerypopupmenu.hidebox($, $menu)
  58. })
  59. $menu.data('dimensions', { w: $menu.outerWidth(), h: $menu.outerHeight() }) //remember main menu's dimensions
  60. $menu.data('timers', {})
  61. var $lis = $menu.find("ul").parent() //find all LIs within menu with a sub UL
  62. $lis.each(function (i) {
  63. var $li = $(this).css({ zIndex: 1000 + i })
  64. var $subul = $li.find('ul:eq(0)').css({ display: 'block' }) //set sub UL to "block" so we can get dimensions
  65. $subul.data('dimensions', { w: $subul.outerWidth(), h: $subul.outerHeight(), parentliw: this.offsetWidth, parentlih: this.offsetHeight })
  66. $subul.data('$parentliref', $li) //cache parent LI of each sub UL
  67. $subul.data('timers', {})
  68. $li.data('$subulref', $subul) //cache sub UL of each parent LI
  69. $li.children("a:eq(0)").append( //add arrow images
  70. '<img src="' + jquerypopupmenu.arrowpath + '" class="rightarrowclass" style="border:0;" />'
  71. )
  72. $li.bind(jquerypopupmenu.triggerevt, function (e) { //show sub UL when mouse moves over parent LI
  73. var $targetul = $(this).css('zIndex', ++jquerypopupmenu.startzindex).addClass("selected").data('$subulref')
  74. if ($targetul.queue().length <= 1) { //if 1 or less queued animations
  75. clearTimeout($targetul.data('timers').hidetimer)
  76. $targetul.data('timers').showtimer = setTimeout(function () {
  77. jquerypopupmenu.positionul($, $targetul, e)
  78. $targetul.show(jquerypopupmenu.animspeed)
  79. }, jquerypopupmenu.showhidedelay[0])
  80. if (jquerypopupmenu.triggerevt == "click" && $(e.target).next('ul').length == 1) //if LI being clicked on is a menu header
  81. return false
  82. }
  83. })
  84. $li.bind('mouseleave', function (e) { //hide sub UL when mouse moves out of parent LI
  85. var $targetul = $(this).data('$subulref')
  86. clearTimeout($targetul.data('timers').showtimer)
  87. $targetul.data('timers').hidetimer = setTimeout(function () { $targetul.hide(100).data('$parentliref').removeClass('selected') }, jquerypopupmenu.showhidedelay[1])
  88. })
  89. })
  90. $menu.find('ul').andSelf().css({ display: 'none', visibility: 'visible' }) //collapse all ULs again
  91. $menu.data('$targetref', $target)
  92. this.builtpopupmenuids.push($menu.get(0).id) //remember id of popup menu that was just built
  93. },
  94. init: function ($, $target, $popupmenu) {
  95. this.triggerevt = (this.ismobile) ? "click" : "mouseenter"
  96. this.showhidedelay[0] = (this.ismobile) ? 0 : this.showhidedelay[0]
  97. if (this.builtpopupmenuids.length == 0) { //only bind click event to document once
  98. $(document).bind("click", function (e) {
  99. if (e.button == 0) { //hide all popup menus (and their sub ULs) when left mouse button is clicked
  100. $('.jqpopupmenu').find('ul').andSelf().hide()
  101. }
  102. })
  103. }
  104. if (jQuery.inArray($popupmenu.get(0).id, this.builtpopupmenuids) == -1) //if this popup menu hasn't been built yet
  105. this.buildpopupmenu($, $popupmenu, $target)
  106. if ($target.parents().filter('ul.jqpopupmenu').length > 0) //if $target matches an element within the popup menu markup, don't bind onpopupmenu to that element
  107. return
  108. $target.bind(jquerypopupmenu.triggerevt, function (e) {
  109. $popupmenu.css('zIndex', ++jquerypopupmenu.startzindex)
  110. jquerypopupmenu.positionul($, $popupmenu, e)
  111. jquerypopupmenu.showbox($, $popupmenu, e)
  112. if (jquerypopupmenu.triggerevt == "click")
  113. e.preventDefault()
  114. })
  115. $target.bind("mouseleave", function (e) {
  116. jquerypopupmenu.hidebox($, $popupmenu)
  117. })
  118. }
  119. }
  120. jQuery.fn.addpopupmenu = function (popupmenuid) {
  121. var $ = jQuery
  122. return this.each(function () { //return jQuery obj
  123. var $target = $(this)
  124. jquerypopupmenu.init($, $target, $('#' + popupmenuid))
  125. })
  126. };
  127. //By default, add popup menu to anchor links with attribute "data-popupmenu"
  128. jQuery(document).ready(function ($) {
  129. var $anchors = $('*[data-popupmenu]')
  130. $anchors.each(function () {
  131. $(this).addpopupmenu(this.getAttribute('data-popupmenu'))
  132. document.getElementById("SelectedMyValue").innerHTML = this.getAttribute('MyValue');
  133. })
  134. })
  135. //ddlistmenu: Function to define a UL list menu dynamically
  136. function ddlistmenu(id, className) {
  137. var menu = document.createElement('ul')
  138. if (id)
  139. menu.id = id
  140. if (className)
  141. menu.className = className
  142. this.menu = menu
  143. }
  144. ddlistmenu.prototype = {
  145. addItem: function (url, text, target) {
  146. var li = document.createElement('li')
  147. li.innerHTML = '<a href="' + url + '" target="' + target + '">' + text + '</a>'
  148. this.menu.appendChild(li)
  149. this.li = li
  150. return this
  151. },
  152. addSubMenu: function () {
  153. var s = new ddlistmenu(null, null)
  154. this.li.appendChild(s.menu)
  155. return s
  156. }
  157. }