/** * Search Navbar */ 'use strict'; // ! Removed following code if you do't wish to use jQuery. Remember that navbar search functionality will stop working on removal. if (typeof $ !== 'undefined') { $(function () { // ! TODO: Required to load after DOM is ready, did this now with jQuery ready. window.Helpers.initSidebarToggle(); // Toggle Universal Sidebar // Navbar Search with autosuggest (typeahead) // ? You can remove the following JS if you don't want to use search functionality. //---------------------------------------------------------------------------------- var searchToggler = $('.search-toggler'), searchInputWrapper = $('.search-input-wrapper'), searchInput = $('.search-input'), contentBackdrop = $('.content-backdrop'); // Open search input on click of search icon if (searchToggler.length) { searchToggler.on('click', function () { if (searchInputWrapper.length) { searchInputWrapper.toggleClass('d-none'); searchInput.focus(); } }); } // Open search on 'CTRL+/' $(document).on('keydown', function (event) { let ctrlKey = event.ctrlKey, slashKey = event.which === 191; if (ctrlKey && slashKey) { if (searchInputWrapper.length) { searchInputWrapper.toggleClass('d-none'); searchInput.focus(); } } }); // Note: Following code is required to update container class of typeahead dropdown width on focus of search input. setTimeout is required to allow time to initiate Typeahead UI. setTimeout(function () { var twitterTypeahead = $('.twitter-typeahead'); searchInput.on('focus', function () { if (searchInputWrapper.hasClass('container-xxl')) { searchInputWrapper.find(twitterTypeahead).addClass('container-xxl'); twitterTypeahead.removeClass('container-fluid'); } else if (searchInputWrapper.hasClass('container-fluid')) { searchInputWrapper.find(twitterTypeahead).addClass('container-fluid'); twitterTypeahead.removeClass('container-xxl'); } }); }, 10); if (searchInput.length) { // Filter config var filterConfig = function (data) { return function findMatches(q, cb) { let matches; matches = []; data.filter(function (i) { if (i.name.toLowerCase().startsWith(q.toLowerCase())) { matches.push(i); } else if ( !i.name.toLowerCase().startsWith(q.toLowerCase()) && i.name.toLowerCase().includes(q.toLowerCase()) ) { matches.push(i); matches.sort(function (a, b) { return b.name < a.name ? 1 : -1; }); } else { return []; } }); cb(matches); }; }; // Search JSON var searchJson = 'search-navbar'; // For vertical layout if ($('#layout-menu').hasClass('menu-horizontal')) { var searchJson = 'search-navbar'; // For vertical layout } // Search API AJAX call var searchData = $.ajax({ url: assetsPath + '../../admin/navbar/' + searchJson, //? Use your own search api instead dataType: 'json', async: false }).responseJSON; // Init typeahead on searchInput searchInput.each(function () { var $this = $(this); searchInput .typeahead( { hint: false, classNames: { menu: 'tt-menu navbar-search-suggestion', cursor: 'active', suggestion: 'suggestion d-flex justify-content-between px-4 py-2 w-100' } }, // ? Add/Update blocks as per need // Pages { name: 'pages', display: 'name', limit: 6, source: filterConfig(searchData.pages), templates: { header: '
Pages
', suggestion: function ({ url, icon, name, path }) { return ( '' + '
' + '' + '' + path + ' / ' + name + '' + '
' + '
' ); }, notFound: '
' + '
Pages
' + '

No se encontro resultados

' + '
' } }, // Categories { name: 'categories', display: 'name', limit: 6, source: filterConfig(searchData.categories), templates: { header: '
Categorías
', suggestion: function ({ slug, icon, name, path }) { return ( '' + '
' + '' + '' + path + (path? ' / ': '') + name + '' + '
' + '
' ); }, notFound: '
' + '
Pages
' + '

No se encontro resultados

' + '
' } }, // Files /* { name: 'files', display: 'name', limit: 4, source: filterConfig(searchData.files), templates: { header: '
Files
', suggestion: function ({ src, name, subtitle, meta }) { return ( '' + '
' + '' +
                                        name +
                                        '' + '
' + '
' + name + '
' + '' + subtitle + '' + '
' + '
' + '' + meta + '' + '
' ); }, notFound: '
' + '
Files
' + '

No se encontro resultados

' + '
' } }, // Members { name: 'members', display: 'name', limit: 4, source: filterConfig(searchData.members), templates: { header: '
Members
', suggestion: function ({ name, src, subtitle }) { return ( '' + '
' + '' +
                                        name +
                                        '' + '' + '
' + '
' ); }, notFound: '
' + '
Members
' + '

No se encontro resultados

' + '
' } } */ ) //On typeahead result render. .bind('typeahead:render', function () { // Show content backdrop, contentBackdrop.addClass('show').removeClass('fade'); }) // On typeahead select .bind('typeahead:select', function (ev, suggestion) { // Open selected page if (suggestion.url !== 'javascript:;') { window.location = baseUrl + suggestion.url; } }) // On typeahead close .bind('typeahead:close', function () { // Clear search searchInput.val(''); $this.typeahead('val', ''); // Hide search input wrapper searchInputWrapper.addClass('d-none'); // Fade content backdrop contentBackdrop.addClass('fade').removeClass('show'); }); // On searchInput keyup, Fade content backdrop if search input is blank searchInput.on('keyup', function () { if (searchInput.val() == '') { contentBackdrop.addClass('fade').removeClass('show'); } }); }); // Init PerfectScrollbar in search result var psSearch; $('.navbar-search-suggestion').each(function () { psSearch = new PerfectScrollbar($(this)[0], { wheelPropagation: false, suppressScrollX: true }); }); searchInput.on('keyup', function () { psSearch.update(); }); } }); }