drupal/core/modules/toolbar/toolbar.js

105 lines
2.4 KiB
JavaScript

(function ($) {
"use strict";
Drupal.toolbar = Drupal.toolbar || {};
/**
* Attach toggling behavior and notify the overlay of the toolbar.
*/
Drupal.behaviors.toolbar = {
attach: function(context) {
var $context = $(context);
// Set the initial state of the toolbar.
$context.find('#toolbar').once('toolbar', Drupal.toolbar.init);
// Toggling toolbar drawer.
$context.find('#toolbar a.toggle').once('toolbar-toggle').click(function(e) {
Drupal.toolbar.toggle();
// Allow resize event handlers to recalculate sizes/positions.
$(window).triggerHandler('resize');
return false;
});
}
};
/**
* Retrieve last saved cookie settings and set up the initial toolbar state.
*/
Drupal.toolbar.init = function() {
// Retrieve the collapsed status from a stored cookie.
var collapsed = $.cookie('Drupal.toolbar.collapsed');
// Expand or collapse the toolbar based on the cookie value.
if (collapsed === '1') {
Drupal.toolbar.collapse();
}
else {
Drupal.toolbar.expand();
}
};
/**
* Collapse the toolbar.
*/
Drupal.toolbar.collapse = function() {
var toggle_text = Drupal.t('Show shortcuts');
$('#toolbar div.toolbar-drawer').addClass('collapsed');
$('#toolbar a.toggle')
.removeClass('toggle-active')
.attr('title', toggle_text)
.html(toggle_text);
$('body').removeClass('toolbar-drawer').css('paddingTop', Drupal.toolbar.height());
$.cookie(
'Drupal.toolbar.collapsed',
1,
{
path: Drupal.settings.basePath,
// The cookie should "never" expire.
expires: 36500
}
);
};
/**
* Expand the toolbar.
*/
Drupal.toolbar.expand = function() {
var toggle_text = Drupal.t('Hide shortcuts');
$('#toolbar div.toolbar-drawer').removeClass('collapsed');
$('#toolbar a.toggle')
.addClass('toggle-active')
.attr('title', toggle_text)
.html(toggle_text);
$('body').addClass('toolbar-drawer').css('paddingTop', Drupal.toolbar.height());
$.cookie(
'Drupal.toolbar.collapsed',
0,
{
path: Drupal.settings.basePath,
// The cookie should "never" expire.
expires: 36500
}
);
};
/**
* Toggle the toolbar.
*/
Drupal.toolbar.toggle = function() {
if ($('#toolbar div.toolbar-drawer').hasClass('collapsed')) {
Drupal.toolbar.expand();
}
else {
Drupal.toolbar.collapse();
}
};
Drupal.toolbar.height = function() {
var $toolbar = $('#toolbar');
var height = $toolbar.outerHeight();
return height;
};
})(jQuery);