toolbar.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. (function ($) {
  2. Drupal.toolbar = Drupal.toolbar || {};
  3. /**
  4. * Attach toggling behavior and notify the overlay of the toolbar.
  5. */
  6. Drupal.behaviors.toolbar = {
  7. attach: function(context) {
  8. // Set the initial state of the toolbar.
  9. $('#toolbar', context).once('toolbar', Drupal.toolbar.init);
  10. // Toggling toolbar drawer.
  11. $('#toolbar a.toggle', context).once('toolbar-toggle').click(function(e) {
  12. Drupal.toolbar.toggle();
  13. // Allow resize event handlers to recalculate sizes/positions.
  14. $(window).triggerHandler('resize');
  15. return false;
  16. });
  17. }
  18. };
  19. /**
  20. * Retrieve last saved cookie settings and set up the initial toolbar state.
  21. */
  22. Drupal.toolbar.init = function() {
  23. // Retrieve the collapsed status from a stored cookie.
  24. var collapsed = $.cookie('Drupal.toolbar.collapsed');
  25. // Expand or collapse the toolbar based on the cookie value.
  26. if (collapsed == 1) {
  27. Drupal.toolbar.collapse();
  28. }
  29. else {
  30. Drupal.toolbar.expand();
  31. }
  32. };
  33. /**
  34. * Collapse the toolbar.
  35. */
  36. Drupal.toolbar.collapse = function() {
  37. var toggle_text = Drupal.t('Show shortcuts');
  38. $('#toolbar div.toolbar-drawer').addClass('collapsed');
  39. $('#toolbar a.toggle')
  40. .removeClass('toggle-active')
  41. .attr('title', toggle_text)
  42. .html(toggle_text);
  43. $('body').removeClass('toolbar-drawer').css('paddingTop', Drupal.toolbar.height());
  44. $.cookie(
  45. 'Drupal.toolbar.collapsed',
  46. 1,
  47. {
  48. // Workaround lack of support for the SameSite attribute in jQuery Cookie.
  49. path: Drupal.settings.basePath + '; SameSite=Lax',
  50. // The cookie should "never" expire.
  51. expires: 36500
  52. }
  53. );
  54. };
  55. /**
  56. * Expand the toolbar.
  57. */
  58. Drupal.toolbar.expand = function() {
  59. var toggle_text = Drupal.t('Hide shortcuts');
  60. $('#toolbar div.toolbar-drawer').removeClass('collapsed');
  61. $('#toolbar a.toggle')
  62. .addClass('toggle-active')
  63. .attr('title', toggle_text)
  64. .html(toggle_text);
  65. $('body').addClass('toolbar-drawer').css('paddingTop', Drupal.toolbar.height());
  66. $.cookie(
  67. 'Drupal.toolbar.collapsed',
  68. 0,
  69. {
  70. // Workaround lack of support for the SameSite attribute in jQuery Cookie.
  71. path: Drupal.settings.basePath + '; SameSite=Lax',
  72. // The cookie should "never" expire.
  73. expires: 36500
  74. }
  75. );
  76. };
  77. /**
  78. * Toggle the toolbar.
  79. */
  80. Drupal.toolbar.toggle = function() {
  81. if ($('#toolbar div.toolbar-drawer').hasClass('collapsed')) {
  82. Drupal.toolbar.expand();
  83. }
  84. else {
  85. Drupal.toolbar.collapse();
  86. }
  87. };
  88. Drupal.toolbar.height = function() {
  89. var $toolbar = $('#toolbar');
  90. var height = $toolbar.outerHeight();
  91. // In modern browsers (including IE9), when box-shadow is defined, use the
  92. // normal height.
  93. var cssBoxShadowValue = $toolbar.css('box-shadow');
  94. var boxShadow = (typeof cssBoxShadowValue !== 'undefined' && cssBoxShadowValue !== 'none');
  95. // In IE8 and below, we use the shadow filter to apply box-shadow styles to
  96. // the toolbar. It adds some extra height that we need to remove.
  97. if (!boxShadow && /DXImageTransform\.Microsoft\.Shadow/.test($toolbar.css('filter'))) {
  98. height -= $toolbar[0].filters.item("DXImageTransform.Microsoft.Shadow").strength;
  99. }
  100. return height;
  101. };
  102. })(jQuery);