contextual.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. * @file
  3. * Attaches behaviors for the Contextual module.
  4. */
  5. (function ($) {
  6. Drupal.contextualLinks = Drupal.contextualLinks || {};
  7. /**
  8. * Attaches outline behavior for regions associated with contextual links.
  9. */
  10. Drupal.behaviors.contextualLinks = {
  11. attach: function (context) {
  12. $('div.contextual-links-wrapper', context).once('contextual-links', function () {
  13. var $wrapper = $(this);
  14. var $region = $wrapper.closest('.contextual-links-region');
  15. var $links = $wrapper.find('ul.contextual-links');
  16. var $trigger = $('<a class="contextual-links-trigger" href="#" />').text(Drupal.t('Configure')).click(
  17. function () {
  18. $links.stop(true, true).slideToggle(100);
  19. $wrapper.toggleClass('contextual-links-active');
  20. return false;
  21. }
  22. );
  23. // Attach hover behavior to trigger and ul.contextual-links.
  24. $trigger.add($links).hover(
  25. function () { $region.addClass('contextual-links-region-active'); },
  26. function () { $region.removeClass('contextual-links-region-active'); }
  27. );
  28. // Hide the contextual links when user clicks a link or rolls out of the .contextual-links-region.
  29. $region.bind('mouseleave click', Drupal.contextualLinks.mouseleave);
  30. $region.hover(
  31. function() { $trigger.addClass('contextual-links-trigger-active'); },
  32. function() { $trigger.removeClass('contextual-links-trigger-active'); }
  33. );
  34. // Prepend the trigger.
  35. $wrapper.prepend($trigger);
  36. });
  37. }
  38. };
  39. /**
  40. * Disables outline for the region contextual links are associated with.
  41. */
  42. Drupal.contextualLinks.mouseleave = function () {
  43. $(this)
  44. .find('.contextual-links-active').removeClass('contextual-links-active')
  45. .find('ul.contextual-links').hide();
  46. };
  47. })(jQuery);