(function() { "use strict"; angular.module('shared') .component('searchBar', { templateUrl: '/shared/js/angular/main-navigation/search-bar.html', controller: 'SearchBarController', bindings: { autocompleteSource: '@', searchPath: '@' } }) .controller('SearchBarController', ['windowService', 'SITE_ID', 'SiteHelper', 'KeyCodes', function SearchBarController(windowService, SITE_ID, SiteHelper, KeyCodes) { var ctrl = this; ctrl.$onInit = function() { ctrl.searchTerms = ''; }; ctrl.onKeydown = function(event) { event.stopPropagation(); if (event.keyCode === KeyCodes.enter) { ctrl.submitSearch(); } }; ctrl.submitSearch = function() { if (isBasicRazSearch()) { raz.search.doBasicSearch(ctrl.searchTerms); angular.element('#hsearchTerms').autocomplete('close'); } else { var searchUrl = getSearchUrl(); windowService.replaceRedirect(searchUrl); } }; ctrl.autocompleteSelect = function (event, ui) { ctrl.searchTerms = ui.item.value; }; function getSearchUrl() { var embeddedSpaces = new RegExp(' +', 'g'); var searchTerms = ctrl.searchTerms.trim().replace(embeddedSpaces, '+'); return ctrl.searchPath + encodeURIComponent(searchTerms); } function isBasicRazSearch() { return ( parseInt(SITE_ID) === SiteHelper.RAZ_SITE_ID || parseInt(SITE_ID) === SiteHelper.RAZ_PLUS_SITE_ID || parseInt(SITE_ID) === SiteHelper.RAZ_PLUS_SUBSCRIPTION_SITE_ID ) && (document.location.pathname === '/search/' || document.location.pathname === '/search/index.php') && window.raz.search; } } ]) })();