jquery.popupmenu.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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('display', 'block');
  50. }
  51. else
  52. {
  53. // The user clicked in the opened menu, it is closed now
  54. popupmenu = null;
  55. }
  56. evt.stopPropagation();
  57. });
  58. $(document).bind('click.popup_menu', function(evt)
  59. {
  60. if (popupmenu)
  61. {
  62. // The user clicked in the document's body, let's close the menu
  63. popupmenu.css('display', 'none');
  64. popupmenu = null;
  65. }
  66. });
  67. });
  68. };