diff options
Diffstat (limited to 'modules/toolbar/toolbar.module')
-rw-r--r-- | modules/toolbar/toolbar.module | 91 |
1 files changed, 90 insertions, 1 deletions
diff --git a/modules/toolbar/toolbar.module b/modules/toolbar/toolbar.module index 0c050c8a1..c137942e5 100644 --- a/modules/toolbar/toolbar.module +++ b/modules/toolbar/toolbar.module @@ -27,10 +27,76 @@ function toolbar_theme($existing, $type, $theme, $path) { 'template' => 'toolbar', 'path' => drupal_get_path('module', 'toolbar'), ); + $items['toolbar_toggle'] = array( + 'variables' => array( + 'collapsed' => NULL, + 'attributes' => array(), + ), + ); + return $items; +} + +/** + * Implement hook_menu(). + */ +function toolbar_menu() { + $items['toolbar/toggle'] = array( + 'title' => 'Toggle drawer visibility', + 'type' => MENU_CALLBACK, + 'page callback' => 'toolbar_toggle_page', + 'access arguments' => array('access toolbar'), + ); return $items; } /** + * Menu callback; toggles the visibility of the toolbar drawer. + */ +function toolbar_toggle_page() { + global $base_path; + // Toggle the value in the cookie. + setcookie('Drupal.admin.toolbar.collapsed', !_toolbar_is_collapsed(), NULL, $base_path); + // Redirect the user from where he used the toggle element. + drupal_goto(); +} + +/** + * Formats an element used to toggle the toolbar drawer's visibility. + * + * @param $variables + * An associative array containing: + * - collapsed: A boolean value representing the toolbar drawer's visibility. + * - attributes: An associative array of HTML attributes. + * @return + * An HTML string representing the element for toggling. + * + * @ingroup themable + */ +function theme_toolbar_toggle($variables) { + if ($variables['collapsed']) { + $toggle_text = t('Open the drawer'); + return '<a href="' . url('toolbar/toggle', array('query' => drupal_get_destination())) . '" title="' . $toggle_text . '"' . drupal_attributes($variables['attributes']) . '>' . $toggle_text . '</a>'; + } + else { + $toggle_text = t('Close the drawer'); + $variables['attributes']['class'][] = 'toggle-active'; + return '<a href="' . url('toolbar/toggle', array('query' => drupal_get_destination())) . '" title="' . $toggle_text . '"' . drupal_attributes($variables['attributes']) . '>' . $toggle_text . '</a>'; + } +} + +/** + * Determines the current state of the toolbar drawer's visibility. + * + * @return + * TRUE when drawer is collapsed, FALSE when it is expanded. + */ +function _toolbar_is_collapsed() { + // PHP converts dots into underscores in cookie names to avoid problems with + // its parser, so we use a converted cookie name. + return isset($_COOKIE['Drupal_admin_toolbar_collapsed']) ? $_COOKIE['Drupal_admin_toolbar_collapsed'] : 0; +} + +/** * Implement hook_page_build(). * * Add admin toolbar to the page_top region automatically. @@ -61,7 +127,10 @@ function toolbar_pre_render($toolbar) { */ function toolbar_preprocess_html(&$vars) { if (user_access('access toolbar')) { - $vars['classes_array'][] = 'toolbar toolbar-drawer'; + $vars['classes_array'][] = 'toolbar'; + if (!_toolbar_is_collapsed()) { + $vars['classes_array'][] = 'toolbar-drawer'; + } } } @@ -90,11 +159,13 @@ function toolbar_build() { ); // Retrieve the admin menu from the database. + $main_menu = menu_load('management'); $links = toolbar_menu_navigation_links(toolbar_get_menu_tree()); $build['toolbar_menu'] = array( '#theme' => 'links', '#links' => $links, '#attributes' => array('id' => 'toolbar-menu'), + '#heading' => array('text' => $main_menu['title'], 'level' => 'h2', 'class' => 'element-invisible'), ); // Add logout & user account links or login link @@ -124,6 +195,24 @@ function toolbar_build() { '#links' => $links, '#attributes' => array('id' => 'toolbar-user'), ); + + // Add an anchor to be able to toggle the visibility of the drawer. + $build['toolbar_toggle'] = array( + '#theme' => 'toolbar_toggle', + '#collapsed' => _toolbar_is_collapsed(), + '#attributes' => array('class' => array('toggle')), + ); + + // Prepare the drawer links CSS classes. + $toolbar_drawer_classes = array( + 'toolbar-drawer', + 'clearfix', + ); + if(_toolbar_is_collapsed()) { + $toolbar_drawer_classes[] = 'collapsed'; + } + $build['toolbar_drawer_classes'] = implode(' ', $toolbar_drawer_classes); + return $build; } |