From 65ab3ac19e87cab143299aa4a843593b6e75a032 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=2E=20Rene=CC=81e=20Beach?= <splendidnoise@gmail.com>
Date: Mon, 18 Nov 2013 17:58:25 -0500
Subject: [PATCH] local tasks
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: J. Renée Beach <splendidnoise@gmail.com>
---
 js/navbar.js  |   9 ++++-
 navbar.module | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 112 insertions(+), 2 deletions(-)

diff --git a/js/navbar.js b/js/navbar.js
index 2ea6a84..781f5a8 100644
--- a/js/navbar.js
+++ b/js/navbar.js
@@ -50,10 +50,14 @@ Drupal.behaviors.navbar = {
         }
       );
 
+      var activeTab = $(context).find('.navbar-tab a.active').get(0);
+      if (!activeTab || !activeTab.hasAttribute('data-navbar-tray')) {
+        activeTab = document.getElementById(JSON.parse(localStorage.getItem('Drupal.navbar.activeTabID')));
+      }
       // Establish the navbar models and views.
       var model = Drupal.navbar.models.navbarModel = new Drupal.navbar.NavbarModel({
         locked: JSON.parse(localStorage.getItem('Drupal.navbar.trayVerticalLocked')) || false,
-        activeTab: document.getElementById(JSON.parse(localStorage.getItem('Drupal.navbar.activeTabID')))
+        activeTab: activeTab
       });
       Drupal.navbar.views.navbarVisualView = new Drupal.navbar.NavbarVisualView({
         el: this,
@@ -141,6 +145,9 @@ Drupal.behaviors.navbar = {
         .on('change:activeTray', function (model, tray) {
           $(document).trigger('drupalNavbarTrayChange', tray);
         });
+        
+        $(context).find('ul.tabs.primary').remove();
+        $(context).find('ul.tabs.secondary').remove();
     });
   }
 };
diff --git a/navbar.module b/navbar.module
index 76b2700..b37b572 100644
--- a/navbar.module
+++ b/navbar.module
@@ -594,7 +594,110 @@ function navbar_navbar() {
     }
   }
 
-  return $items;
+  $tasks = navbar_tasks_output(navbar_get_tasks());
+  if (!empty($tasks)) {
+    $weight = 101;
+    foreach ($tasks as $key => $task) {
+      $items['tasks_' . $key] = array(
+        '#type' => 'navbar_item',
+        'tab' => array(
+          '#type' => 'link',
+          '#title' => $task['#title'],
+          '#href' => $task['#href'],
+          '#options' => array(
+            'attributes' => array(
+              'title' => $task['#title'],
+              'class' => array('navbar-icon', 'navbar-icon-' . strtolower(str_replace(' ', '-', $task['#title']))),
+            ),
+          ),
+        ),
+        '#weight' => $weight++,
+      );
+      if (!empty($task['#below'])) {
+        $items['tasks_' . $key]['tray'] = array(
+          '#heading' => t('Secondary tasks'),
+          'navbar_tasks' => array(
+            '#type' => 'container',
+            'tasks_menu' => $task['#below'],
+            '#attributes' => array(
+              'class' => array('navbar-menu-tasks'),
+            ),
+          ),
+        );
+      }
+    }
+    return $items;
+  }
+}
+
+/**
+ * Gets the primary and secondary tasks tree.
+ *
+ * @return
+ *   An array containing a menu tree of the primary and secondary tasks.
+ */
+function navbar_get_tasks() {
+  $primary_tasks = menu_primary_local_tasks();
+  if (!empty($primary_tasks)) {
+    $secondary_tasks = menu_secondary_local_tasks();
+    if (!empty($secondary_tasks)) {
+      foreach ($primary_tasks as $key => $primary_task) {
+        if (!empty($primary_task['#active']) && $primary_task['#active']) {
+          $primary_tasks[$key]['#below'] = $secondary_tasks;
+          break;
+        }
+      }
+    }
+  }
+  return $primary_tasks;
+}
+
+function navbar_tasks_output($primary_tasks) {
+  $num_items = count($primary_tasks);
+  $i = 0;
+  foreach ($primary_tasks as $key => $primary_task) {
+    $class = array();
+    if ($i == 0) {
+      $class[] = 'first';
+    }
+    if ($i == $num_items - 1) {
+      $class[] = 'last';
+    }
+    $link = $primary_task['#link'];
+    $below_tasks = array();
+    if (!empty($primary_task['#active']) && $primary_task['#active'] === true) {
+      $class[] = 'active';
+      $class[] = 'active-trail';
+      if (!empty($primary_task['#below'])) {
+        $below_tasks = navbar_tasks_output($primary_task['#below']);
+      }
+      $class[] = 'expanded';
+      $class[] = 'open';
+    }
+    else {
+      $class[] = 'leaf';
+    }
+    $tasks[$key] = array(
+      '#title' => check_plain($link['title']),
+      '#href' => $link['href'],
+      '#localized_options' => array(
+        'attributes' => array(
+          'id' => 'navbar-link-' . str_replace(array('/', '<', '>'), array('-', '', ''), $link['path']),
+          'class' => array(
+            'navbar-icon',
+            'navbar-icon-' . strtolower(str_replace(' ', '-', check_plain($link['title']))),
+          ),
+          'title' => check_plain($link['description']))
+      ),
+      '#theme' => 'menu_link__management',
+      '#attributes' => array('class' => $class),
+      '#below' => $below_tasks,
+    );
+    $i++;
+  }
+  $tasks['#sorted'] = 1;
+  $tasks['#theme_wrappers'] = array('menu_tree__management');
+  return $tasks;
 }
 
 /**
-- 
1.8.2

