// https://civicrm.org/licensing (function($, _) { "use strict"; var templates, initialized, ENTER_KEY = 13, SPACE_KEY = 32; CRM.menubar = _.extend({ data: null, settings: {collapsibleBehavior: 'accordion'}, position: 'over-cms-menu', toggleButton: true, attachTo: (CRM.menubar && CRM.menubar.position === 'above-crm-container') ? '#crm-container' : 'body', initialize: function() { var cache = CRM.cache.get('menubar'); if (cache && cache.code === CRM.menubar.cacheCode && cache.locale === CRM.config.locale && cache.cid === CRM.config.cid && localStorage.civiMenubar) { CRM.menubar.data = cache.data; insert(localStorage.civiMenubar); } else { $.getJSON(CRM.url('civicrm/ajax/navmenu', {code: CRM.menubar.cacheCode, locale: CRM.config.locale, cid: CRM.config.cid})) .done(function(data) { var markup = getTpl('tree')(data); CRM.cache.set('menubar', {code: CRM.menubar.cacheCode, locale: CRM.config.locale, cid: CRM.config.cid, data: data}); CRM.menubar.data = data; localStorage.setItem('civiMenubar', markup); insert(markup); }); } // Wait for crm-container present on the page as it's faster than document.ready function insert(markup) { if (document.getElementById('crm-container')) { render(markup); } else { new MutationObserver(function(mutations, observer) { if (document.getElementById('crm-container')) { observer.disconnect(); render(markup); } }).observe(document, {childList: true, subtree: true}); } } function render(markup) { var position = CRM.menubar.attachTo === 'body' ? 'beforeend' : 'afterbegin'; $(CRM.menubar.attachTo)[0].insertAdjacentHTML(position, markup); CRM.menubar.initializePosition(); $('#civicrm-menu').trigger('crmLoad'); $(document).ready(function() { handleResize(); $('#civicrm-menu') .on('click', 'a[href="#"]', function() { // For empty links - keep the menu open and don't jump the page anchor return false; }) .on('click', 'a:not([href^="#"])', function(e) { if (e.ctrlKey || e.altKey || e.shiftKey || e.metaKey) { // Prevent menu closing when link is clicked with a keyboard modifier. e.stopPropagation(); } }) .on('dragstart', function() { // Stop user from accidentally dragging menu links // This was added because a user noticed they could drag the civi icon into the quicksearch box. return false; }) .on('click', 'a[href="#hidemenu"]', function(e) { e.preventDefault(); CRM.menubar.hide(250, true); }) .on('keyup', 'a', function(e) { // Simulate a click when spacebar key is pressed if (e.which == SPACE_KEY) { $(e.currentTarget)[0].click(); } }) .on('show.smapi', function(e, menu) { // Focus menu when opened with an accesskey if ($(menu).parent().data('name') === 'Home') { $('#crm-menubar-drilldown').focus(); } else { $(menu).siblings('a[accesskey]').focus(); } }) .smartmenus(CRM.menubar.settings); initialized = true; CRM.menubar.initializeResponsive(); CRM.menubar.initializeSearch(); CRM.menubar.initializeDrill(); }); } }, destroy: function() { $.SmartMenus.destroy(); $('#civicrm-menu-nav').remove(); initialized = false; $('body[class]').attr('class', function(i, c) { return c.replace(/(^|\s)crm-menubar-\S+/g, ''); }); }, show: function(speed) { if (typeof speed === 'number') { $('#civicrm-menu').slideDown(speed, function() { $(this).css('display', ''); handleResize(); }); } $('body') .removeClass('crm-menubar-hidden') .addClass('crm-menubar-visible'); handleResize(); }, hide: function(speed, showMessage) { if (typeof speed === 'number') { $('#civicrm-menu').slideUp(speed, function() { $(this).css('display', ''); }); } $('body') .addClass('crm-menubar-hidden') .removeClass('crm-menubar-visible'); document.documentElement.style.setProperty('--crm-menubar-bottom', '0px'); if (showMessage === true && $('#crm-notification-container').length && initialized) { var alert = CRM.alert('' + _.escape(ts('Restore CiviCRM Menu')) + '', ts('Menu hidden'), 'none', {expires: 10000}); $('#crm-restore-menu') .click(function(e) { e.preventDefault(); alert.close(); CRM.menubar.show(speed); }); } }, open: function(itemName) { var $item = $('li[data-name="' + itemName + '"] > a', '#civicrm-menu'); if ($item.length) { $('#civicrm-menu').smartmenus('itemActivate', $item); $item[0].focus(); } }, close: $.SmartMenus.hideAll, isOpen: function(itemName) { if (itemName) { return !!$('li[data-name="' + itemName + '"] > ul[aria-expanded="true"]', '#civicrm-menu').length; } return !!$('ul[aria-expanded="true"]', '#civicrm-menu').length; }, spin: function(spin) { $('.crm-logo-sm', '#civicrm-menu').toggleClass('fa-spin', spin); }, getItem: function(itemName) { return traverse(CRM.menubar.data.menu, itemName, 'get'); }, findItems: function(searchTerm) { return findRecursive(CRM.menubar.data.menu, searchTerm.toLowerCase().replace(/ /g, '')); }, addItems: function(position, targetName, items) { var list, container, $ul; if (position === 'before' || position === 'after') { if (!targetName) { throw 'Cannot add sibling of main menu'; } list = traverse(CRM.menubar.data.menu, targetName, 'parent'); if (!list) { throw targetName + ' not found'; } var offset = position === 'before' ? 0 : 1; position = offset + _.findIndex(list, {name: targetName}); $ul = $('li[data-name="' + targetName + '"]', '#civicrm-menu').closest('ul'); } else if (targetName) { container = traverse(CRM.menubar.data.menu, targetName, 'get'); if (!container) { throw targetName + ' not found'; } container.child = container.child || []; list = container.child; var $target = $('li[data-name="' + targetName + '"]', '#civicrm-menu'); if (!$target.children('ul').length) { $target.append('