jquery.popupmenu.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Simple popup menu 1.0 (2010-05-15)
  3. *
  4. * Copyright (c) 2010 Combodo SARL (www.combodo.com)
  5. * Licenced under the GPL licence.
  6. *
  7. * http://www.combodo.com/
  8. *
  9. * Built upon jQuery jQuery 1.2.3a (http://jquery.com)
  10. * Requires the (modified) jQuery positionBy plugin by Jonathan Sharp (http://jdsharp.us)
  11. */
  12. jQuery.fn.popupmenu = function ()
  13. {
  14. var popupmenu = null;
  15. return this.each(function()
  16. {
  17. $(this).bind('click.popup_menu', function (evt)
  18. {
  19. var previous_popup = popupmenu;
  20. var bMenuClosed = false;
  21. popupmenu = $(this).find('ul');
  22. if ( previous_popup != null)
  23. {
  24. // The user clicked while a menu is open, close the currently opened menu
  25. previous_popup.css('display', 'none');
  26. }
  27. if ( (previous_popup == null) || (previous_popup.get(0) != popupmenu.get(0))) // Comparing the DOM objects
  28. {
  29. // The user clicked in a different menu, let's open it
  30. popupmenu.positionBy({ target: $(this),
  31. targetPos: 4,
  32. elementPos: 0,
  33. hideAfterPosition: true
  34. });
  35. // In links containing a hash, replace what's after the hash by the current hash
  36. // In order to navigate to the same tab as the current one when editing an object
  37. currentHash = '';
  38. aMatches = /#(.*)$/.exec(window.location.href);
  39. if (aMatches != null)
  40. {
  41. currentHash = aMatches[1];
  42. popupmenu.find('a').each( function() {
  43. if ( /#(.*)$/.test(this.href))
  44. {
  45. this.href = this.href.replace(/#(.*)$/, '#'+currentHash);
  46. }
  47. });
  48. }
  49. popupmenu.css('top', ''); // let the "position: absolute;" do its job, for better support of scrolling...
  50. popupmenu.css('display', 'block');
  51. }
  52. else
  53. {
  54. // The user clicked in the opened menu, it is closed now
  55. popupmenu = null;
  56. }
  57. evt.stopPropagation();
  58. });
  59. $(document).bind('click.popup_menu', function(evt)
  60. {
  61. if (popupmenu)
  62. {
  63. // The user clicked in the document's body, let's close the menu
  64. popupmenu.css('display', 'none');
  65. popupmenu = null;
  66. }
  67. });
  68. });
  69. };