dropdown.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * @file
  3. * Implement a simple, clickable dropdown menu.
  4. *
  5. * See dropdown.theme.inc for primary documentation.
  6. *
  7. * The javascript relies on four classes:
  8. * - The dropdown must be fully contained in a div with the class
  9. * ctools-dropdown. It must also contain the class ctools-dropdown-no-js
  10. * which will be immediately removed by the javascript; this allows for
  11. * graceful degradation.
  12. * - The trigger that opens the dropdown must be an a tag wit hthe class
  13. * ctools-dropdown-link. The href should just be '#' as this will never
  14. * be allowed to complete.
  15. * - The part of the dropdown that will appear when the link is clicked must
  16. * be a div with class ctools-dropdown-container.
  17. * - Finally, ctools-dropdown-hover will be placed on any link that is being
  18. * hovered over, so that the browser can restyle the links.
  19. *
  20. * This tool isn't meant to replace click-tips or anything, it is specifically
  21. * meant to work well presenting menus.
  22. */
  23. (function ($) {
  24. Drupal.behaviors.CToolsDropdown = {
  25. attach: function() {
  26. $('div.ctools-dropdown').once('ctools-dropdown', function() {
  27. var $dropdown = $(this);
  28. var open = false;
  29. var hovering = false;
  30. var timerID = 0;
  31. $dropdown.removeClass('ctools-dropdown-no-js');
  32. var toggle = function(close) {
  33. // if it's open or we're told to close it, close it.
  34. if (open || close) {
  35. // If we're just toggling it, close it immediately.
  36. if (!close) {
  37. open = false;
  38. $("div.ctools-dropdown-container", $dropdown).slideUp(100);
  39. }
  40. else {
  41. // If we were told to close it, wait half a second to make
  42. // sure that's what the user wanted.
  43. // Clear any previous timer we were using.
  44. if (timerID) {
  45. clearTimeout(timerID);
  46. }
  47. timerID = setTimeout(function() {
  48. if (!hovering) {
  49. open = false;
  50. $("div.ctools-dropdown-container", $dropdown).slideUp(100);
  51. }
  52. }, 500);
  53. }
  54. }
  55. else {
  56. // open it.
  57. open = true;
  58. $("div.ctools-dropdown-container", $dropdown)
  59. .animate({height: "show", opacity: "show"}, 100);
  60. }
  61. }
  62. $("a.ctools-dropdown-link", $dropdown).click(function() {
  63. toggle();
  64. return false;
  65. });
  66. $dropdown.hover(
  67. function() {
  68. hovering = true;
  69. }, // hover in
  70. function() { // hover out
  71. hovering = false;
  72. toggle(true);
  73. return false;
  74. });
  75. // @todo -- just use CSS for this noise.
  76. $("div.ctools-dropdown-container a").hover(
  77. function() { $(this).addClass('ctools-dropdown-hover'); },
  78. function() { $(this).removeClass('ctools-dropdown-hover'); }
  79. );
  80. });
  81. }
  82. }
  83. })(jQuery);