110 lines
5.4 KiB
JavaScript
110 lines
5.4 KiB
JavaScript
"use strict";
|
|
(function () {
|
|
'use strict';
|
|
angular.module('searchModule')
|
|
.controller('SearchBarController', ['$scope', 'searchModel', '$state', '$timeout', '$location', 'metadataModel', 'configurationModel', 'searchService',
|
|
function ($scope, searchModel, $state, $timeout, $location, metadataModel, configurationModel, searchService) {
|
|
$scope.isSearchBarActive = false;
|
|
var searchMetadata = configurationModel.get('search.targetAreas');
|
|
$scope.searchMetadata = { targetAreas: searchMetadata };
|
|
// FF and Chrome behaves differently. In FF, the state is 'search.task'.
|
|
// To make it work on all browsers, we will just check $state.parames.searchText since it is unique to search
|
|
$scope.searchText = $state.params.searchText || '';
|
|
if ($state.params.searchCriteria) {
|
|
$scope.selectedTargetArea = _.findWhere(searchMetadata, { types: $state.params.searchCriteria });
|
|
}
|
|
else {
|
|
$scope.selectedTargetArea = _.findWhere(searchMetadata, { selected: true });
|
|
}
|
|
$scope.changedTargetArea = searchService.targetArea;
|
|
$scope.$watch('changedTargetArea', function (newTargetArea) {
|
|
if (newTargetArea.selectedTargetArea) {
|
|
$scope.selectedTargetArea = newTargetArea.selectedTargetArea;
|
|
}
|
|
}, true);
|
|
$scope.skipWildcardInGlobalSearch = 'true';
|
|
$scope.wildCardEntered = false;
|
|
var globalMetadataPromise = metadataModel.getMetadataByType(EntityVO.TYPE_GLOBAL);
|
|
globalMetadataPromise.then(function (metadata) {
|
|
if (metadata.configurationParameters && metadata.configurationParameters.skipWildcardInGlobalSearch === 'true') {
|
|
$scope.skipWildcardInGlobalSearch = metadata.configurationParameters.skipWildcardInGlobalSearch;
|
|
}
|
|
else {
|
|
$scope.skipWildcardInGlobalSearch = 'false';
|
|
}
|
|
if (metadata.configurationParameters && metadata.configurationParameters.disableTypeaheadInGlobalSearch === 'true') {
|
|
$scope.disableTypeaheadInGlobalSearch = true;
|
|
}
|
|
else {
|
|
$scope.disableTypeaheadInGlobalSearch = false;
|
|
}
|
|
});
|
|
$scope.wildCardValidation = function () {
|
|
if ($scope.skipWildcardInGlobalSearch === 'true') {
|
|
$scope.wildCardEntered = false;
|
|
var countOfWildChar = 0;
|
|
for (var i = 0; i < $scope.searchText.length; i++) {
|
|
var searchedChar = $scope.searchText.charAt(i);
|
|
if (searchedChar === '%') {
|
|
countOfWildChar = countOfWildChar + 1;
|
|
$scope.wildCardEntered = true;
|
|
}
|
|
else {
|
|
$scope.wildCardEntered = false;
|
|
break;
|
|
}
|
|
}
|
|
$scope.searchText = $scope.searchText.substr(countOfWildChar);
|
|
$timeout.cancel($scope.timerPromise);
|
|
$scope.timerPromise = $timeout(function () {
|
|
angular.element('#globalSearchBox').siblings('.tooltip').fadeOut('slow');
|
|
}, 4000);
|
|
}
|
|
};
|
|
$scope.changeSearchCriteria = function (item) {
|
|
$scope.targetArea = item;
|
|
var urlLocation = $location.path();
|
|
if ($scope.searchText && urlLocation.startsWith('/search')) {
|
|
$scope.moveToSearchState();
|
|
}
|
|
};
|
|
$scope.activateSearchBar = function () {
|
|
$scope.isSearchBarActive = true;
|
|
};
|
|
$scope.deactivateSearchBar = function () {
|
|
$timeout(function () {
|
|
$scope.isSearchBarActive = false;
|
|
}, 500);
|
|
};
|
|
$scope.moveToSearchState = function (text) {
|
|
var searchedText, targetArea;
|
|
if (text) {
|
|
$scope.searchText = text;
|
|
searchedText = trimSearchedText(text);
|
|
}
|
|
else {
|
|
searchedText = trimSearchedText($scope.searchText);
|
|
}
|
|
targetArea = ($scope.selectedTargetArea) ? $scope.selectedTargetArea.types : null;
|
|
if (searchedText && targetArea) {
|
|
$scope.isSearchBarActive = false;
|
|
$state.go('search', { searchText: searchedText, searchCriteria: targetArea });
|
|
$scope.deactivateSearchBar();
|
|
}
|
|
};
|
|
$scope.selectSuggestedResult = function (result) {
|
|
$scope.moveToSearchState(result);
|
|
};
|
|
$scope.getSuggestedSearchResults = function (text) {
|
|
if (!$scope.disableTypeaheadInGlobalSearch && !text.startsWith('%')) {
|
|
$scope.isSearchBarActive = true;
|
|
return searchModel.getSuggestedSearchResults(text);
|
|
}
|
|
};
|
|
// private functions
|
|
function trimSearchedText(text) {
|
|
return text ? text.trim() : '';
|
|
}
|
|
}]);
|
|
})();
|