jquery.popupmenu.js 1.9 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.bgiframe();
  31. popupmenu.positionBy({ target: $(this),
  32. targetPos: 4,
  33. elementPos: 0,
  34. hideAfterPosition: true
  35. });
  36. // In links containing a hash, replace what's after the hash by the current hash
  37. // In order to navigate to the same tab as the current one when editing an object
  38. currentHash = '';
  39. aMatches = /#(.*)$/.exec(window.location.href);
  40. if (aMatches != null)
  41. {
  42. currentHash = aMatches[1];
  43. popupmenu.find('a').each( function() {
  44. if ( /#(.*)$/.test(this.href))
  45. {
  46. this.href = this.href.replace(/#(.*)$/, '#'+currentHash);
  47. }
  48. });
  49. }
  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. };