Merge pull request #23932 from webmaster-cses-org-uk/patch-4
[civicrm-core.git] / js / crm.drupal8.js
index 74e64cb779290f261b403dddcce74bd3f54a3afa..91c7080fbe293a5f50f466857b2e50ed230a911e 100644 (file)
@@ -5,7 +5,7 @@ localStorage.setItem('Drupal.toolbar.activeTabID', JSON.stringify('toolbar-item-
 
 (function($) {
   function adjustToggle() {
-    if ($(window).width() < 768) {
+    if ($(window).width() < 768 && $('#toolbar-item-civicrm').length) {
       $('#civicrm-menu-nav .crm-menubar-toggle-btn').css({
         left: '' + $('#toolbar-item-civicrm').offset().left + 'px',
         width: '' + $('#toolbar-item-civicrm').innerWidth() + 'px'
@@ -17,13 +17,16 @@ localStorage.setItem('Drupal.toolbar.activeTabID', JSON.stringify('toolbar-item-
 
   // Wait for document.ready so Drupal's jQuery is available to this script
   $(function($) {
-    // Need Drupal's jQuery to listen to this event
-    jQuery(document).on('drupalToolbarTabChange', function(event, tab) {
-      if (CRM.menubar && CRM.menubar.position === 'below-cms-menu') {
-        var action = jQuery(tab).is('#toolbar-item-civicrm') ? 'show' : 'hide';
-        CRM.menubar[action]();
-      }
-    });
+    // If Drupal's jQuery isn't loaded (e.g. on a stripped-down front-end page), we don't need to worry about the toolbar
+    if (window.jQuery) {
+      // This event is only triggered by Drupal's copy of jQuery. CRM.$ won't pick it up.
+      jQuery(document).on('drupalToolbarTabChange', function (event, tab) {
+        if (CRM.menubar && CRM.menubar.position === 'below-cms-menu') {
+          var action = jQuery(tab).is('#toolbar-item-civicrm') ? 'show' : 'hide';
+          CRM.menubar[action]();
+        }
+      });
+    }
   });
 
 })(CRM.$);