mirror of
https://github.com/ansible/awx.git
synced 2026-03-21 02:47:35 -02:30
Fixed issue where search actions were sending two requests. Cleaned up and organized smart search controller
This commit is contained in:
@@ -16,71 +16,26 @@ function SmartSearchController (
|
|||||||
let queryset;
|
let queryset;
|
||||||
let transitionSuccessListener;
|
let transitionSuccessListener;
|
||||||
|
|
||||||
configService.getConfig()
|
const compareParams = (a, b) => {
|
||||||
.then(config => init(config));
|
for (let key in a) {
|
||||||
|
if (!(key in b) || a[key].toString() !== b[key].toString()) {
|
||||||
function init (config) {
|
return false;
|
||||||
let version;
|
|
||||||
|
|
||||||
try {
|
|
||||||
[version] = config.version.split('-');
|
|
||||||
} catch (err) {
|
|
||||||
version = 'latest';
|
|
||||||
}
|
|
||||||
|
|
||||||
$scope.documentationLink = `http://docs.ansible.com/ansible-tower/${version}/html/userguide/search_sort.html`;
|
|
||||||
$scope.searchPlaceholder = i18n._('Search');
|
|
||||||
|
|
||||||
if ($scope.defaultParams) {
|
|
||||||
defaults = $scope.defaultParams;
|
|
||||||
} else {
|
|
||||||
// steps through the current tree of $state configurations, grabs default search params
|
|
||||||
const stateConfig = _.find($state.$current.path, step => _.has(step, `params.${searchKey}`));
|
|
||||||
defaults = stateConfig.params[searchKey].config.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($scope.querySet) {
|
|
||||||
queryset = $scope.querySet;
|
|
||||||
} else {
|
|
||||||
queryset = $state.params[searchKey];
|
|
||||||
}
|
|
||||||
|
|
||||||
path = GetBasePath($scope.basePath) || $scope.basePath;
|
|
||||||
generateSearchTags();
|
|
||||||
|
|
||||||
qs.initFieldset(path, $scope.djangoModel)
|
|
||||||
.then((data) => {
|
|
||||||
$scope.models = data.models;
|
|
||||||
$scope.options = data.options.data;
|
|
||||||
$scope.keyFields = _.reduce(data.models[$scope.djangoModel].base, function(result, value, key) {
|
|
||||||
if (value.filterable) {
|
|
||||||
result.push(key);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}, []);
|
|
||||||
if ($scope.list) {
|
|
||||||
$scope.$emit(optionsKey, data.options);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
function compareParams (a, b) {
|
|
||||||
for (let key in a) {
|
|
||||||
if (!(key in b) || a[key].toString() !== b[key].toString()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
for (let key in b) {
|
}
|
||||||
if (!(key in a)) {
|
for (let key in b) {
|
||||||
return false;
|
if (!(key in a)) {
|
||||||
}
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
if (transitionSuccessListener) {
|
const generateSearchTags = () => {
|
||||||
transitionSuccessListener();
|
const { singleSearchParam } = $scope;
|
||||||
}
|
$scope.searchTags = qs.createSearchTagsFromQueryset(queryset, defaults, singleSearchParam);
|
||||||
|
};
|
||||||
|
|
||||||
|
const listenForTransitionSuccess = () => {
|
||||||
transitionSuccessListener = $transitions.onSuccess({}, trans => {
|
transitionSuccessListener = $transitions.onSuccess({}, trans => {
|
||||||
// State has changed - check to see if this is a param change
|
// State has changed - check to see if this is a param change
|
||||||
if (trans.from().name === trans.to().name) {
|
if (trans.from().name === trans.to().name) {
|
||||||
@@ -99,29 +54,22 @@ function SmartSearchController (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
|
||||||
$scope.$on('$destroy', transitionSuccessListener);
|
const isAnsibleFactField = (termParts) => {
|
||||||
$scope.$watch('disableSearch', disableSearch => {
|
const rootField = termParts[0].split('.')[0].replace(/^-/, '');
|
||||||
if (disableSearch) {
|
return rootField === 'ansible_facts';
|
||||||
$scope.searchPlaceholder = i18n._('Cannot search running job');
|
};
|
||||||
} else {
|
|
||||||
$scope.searchPlaceholder = i18n._('Search');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function generateSearchTags () {
|
const revertSearch = (queryToBeRestored) => {
|
||||||
const { singleSearchParam } = $scope;
|
|
||||||
$scope.searchTags = qs.createSearchTagsFromQueryset(queryset, defaults, singleSearchParam);
|
|
||||||
}
|
|
||||||
|
|
||||||
function revertSearch (queryToBeRestored) {
|
|
||||||
queryset = queryToBeRestored;
|
queryset = queryToBeRestored;
|
||||||
// https://ui-router.github.io/docs/latest/interfaces/params.paramdeclaration.html#dynamic
|
// https://ui-router.github.io/docs/latest/interfaces/params.paramdeclaration.html#dynamic
|
||||||
// This transition will not reload controllers/resolves/views
|
// This transition will not reload controllers/resolves/views
|
||||||
// but will register new $stateParams[$scope.iterator + '_search'] terms
|
// but will register new $stateParams[$scope.iterator + '_search'] terms
|
||||||
if (!$scope.querySet) {
|
if (!$scope.querySet) {
|
||||||
$state.go('.', { [searchKey]: queryset });
|
transitionSuccessListener();
|
||||||
|
$state.go('.', { [searchKey]: queryset })
|
||||||
|
.then(() => listenForTransitionSuccess());
|
||||||
}
|
}
|
||||||
qs.search(path, queryset).then((res) => {
|
qs.search(path, queryset).then((res) => {
|
||||||
if ($scope.querySet) {
|
if ($scope.querySet) {
|
||||||
@@ -134,18 +82,9 @@ function SmartSearchController (
|
|||||||
$scope.searchTerm = null;
|
$scope.searchTerm = null;
|
||||||
|
|
||||||
generateSearchTags();
|
generateSearchTags();
|
||||||
}
|
|
||||||
|
|
||||||
$scope.toggleKeyPane = () => {
|
|
||||||
$scope.showKeyPane = !$scope.showKeyPane;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function isAnsibleFactField (termParts) {
|
const isFilterableBaseField = (termParts) => {
|
||||||
const rootField = termParts[0].split('.')[0].replace(/^-/, '');
|
|
||||||
return rootField === 'ansible_facts';
|
|
||||||
}
|
|
||||||
|
|
||||||
function isFilterableBaseField (termParts) {
|
|
||||||
const rootField = termParts[0].split('.')[0].replace(/^-/, '');
|
const rootField = termParts[0].split('.')[0].replace(/^-/, '');
|
||||||
const listName = $scope.list.name;
|
const listName = $scope.list.name;
|
||||||
const baseFieldPath = `models.${listName}.base.${rootField}`;
|
const baseFieldPath = `models.${listName}.base.${rootField}`;
|
||||||
@@ -155,9 +94,9 @@ function SmartSearchController (
|
|||||||
const isBaseModelRelatedSearchTermField = (_.get($scope, `${baseFieldPath}.type`) === 'field');
|
const isBaseModelRelatedSearchTermField = (_.get($scope, `${baseFieldPath}.type`) === 'field');
|
||||||
|
|
||||||
return isBaseField && !isBaseModelRelatedSearchTermField && isFilterable;
|
return isBaseField && !isBaseModelRelatedSearchTermField && isFilterable;
|
||||||
}
|
};
|
||||||
|
|
||||||
function isRelatedField (termParts) {
|
const isRelatedField = (termParts) => {
|
||||||
const rootField = termParts[0].split('.')[0].replace(/^-/, '');
|
const rootField = termParts[0].split('.')[0].replace(/^-/, '');
|
||||||
const listName = $scope.list.name;
|
const listName = $scope.list.name;
|
||||||
const baseRelatedTypePath = `models.${listName}.base.${rootField}.type`;
|
const baseRelatedTypePath = `models.${listName}.base.${rootField}.type`;
|
||||||
@@ -166,7 +105,69 @@ function SmartSearchController (
|
|||||||
const isBaseModelRelatedSearchTermField = (_.get($scope, baseRelatedTypePath) === 'field');
|
const isBaseModelRelatedSearchTermField = (_.get($scope, baseRelatedTypePath) === 'field');
|
||||||
|
|
||||||
return (isRelatedSearchTermField || isBaseModelRelatedSearchTermField);
|
return (isRelatedSearchTermField || isBaseModelRelatedSearchTermField);
|
||||||
}
|
};
|
||||||
|
|
||||||
|
configService.getConfig()
|
||||||
|
.then(config => {
|
||||||
|
let version;
|
||||||
|
|
||||||
|
try {
|
||||||
|
[version] = config.version.split('-');
|
||||||
|
} catch (err) {
|
||||||
|
version = 'latest';
|
||||||
|
}
|
||||||
|
|
||||||
|
$scope.documentationLink = `http://docs.ansible.com/ansible-tower/${version}/html/userguide/search_sort.html`;
|
||||||
|
$scope.searchPlaceholder = i18n._('Search');
|
||||||
|
|
||||||
|
if ($scope.defaultParams) {
|
||||||
|
defaults = $scope.defaultParams;
|
||||||
|
} else {
|
||||||
|
// steps through the current tree of $state configurations, grabs default search params
|
||||||
|
const stateConfig = _.find($state.$current.path, step => _.has(step, `params.${searchKey}`));
|
||||||
|
defaults = stateConfig.params[searchKey].config.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($scope.querySet) {
|
||||||
|
queryset = $scope.querySet;
|
||||||
|
} else {
|
||||||
|
queryset = $state.params[searchKey];
|
||||||
|
}
|
||||||
|
|
||||||
|
path = GetBasePath($scope.basePath) || $scope.basePath;
|
||||||
|
generateSearchTags();
|
||||||
|
|
||||||
|
qs.initFieldset(path, $scope.djangoModel)
|
||||||
|
.then((data) => {
|
||||||
|
$scope.models = data.models;
|
||||||
|
$scope.options = data.options.data;
|
||||||
|
$scope.keyFields = _.reduce(data.models[$scope.djangoModel].base, (result, value, key) => {
|
||||||
|
if (value.filterable) {
|
||||||
|
result.push(key);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}, []);
|
||||||
|
if ($scope.list) {
|
||||||
|
$scope.$emit(optionsKey, data.options);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$scope.$on('$destroy', transitionSuccessListener);
|
||||||
|
$scope.$watch('disableSearch', disableSearch => {
|
||||||
|
if (disableSearch) {
|
||||||
|
$scope.searchPlaceholder = i18n._('Cannot search running job');
|
||||||
|
} else {
|
||||||
|
$scope.searchPlaceholder = i18n._('Search');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
listenForTransitionSuccess();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
$scope.toggleKeyPane = () => {
|
||||||
|
$scope.showKeyPane = !$scope.showKeyPane;
|
||||||
|
};
|
||||||
|
|
||||||
$scope.addTerms = terms => {
|
$scope.addTerms = terms => {
|
||||||
const { singleSearchParam } = $scope;
|
const { singleSearchParam } = $scope;
|
||||||
@@ -182,11 +183,13 @@ function SmartSearchController (
|
|||||||
// This transition will not reload controllers/resolves/views but will register new
|
// This transition will not reload controllers/resolves/views but will register new
|
||||||
// $stateParams[searchKey] terms.
|
// $stateParams[searchKey] terms.
|
||||||
if (!$scope.querySet) {
|
if (!$scope.querySet) {
|
||||||
|
transitionSuccessListener();
|
||||||
$state.go('.', { [searchKey]: queryset })
|
$state.go('.', { [searchKey]: queryset })
|
||||||
.then(() => {
|
.then(() => {
|
||||||
// same as above in $scope.remove. For some reason deleting the page
|
// same as above in $scope.remove. For some reason deleting the page
|
||||||
// from the queryset works for all lists except lists in modals.
|
// from the queryset works for all lists except lists in modals.
|
||||||
delete $stateParams[searchKey].page;
|
delete $stateParams[searchKey].page;
|
||||||
|
listenForTransitionSuccess();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,6 +215,7 @@ function SmartSearchController (
|
|||||||
queryset = qs.removeTermsFromQueryset(queryset, term, isFilterableBaseField, isRelatedField, isAnsibleFactField, singleSearchParam);
|
queryset = qs.removeTermsFromQueryset(queryset, term, isFilterableBaseField, isRelatedField, isAnsibleFactField, singleSearchParam);
|
||||||
|
|
||||||
if (!$scope.querySet) {
|
if (!$scope.querySet) {
|
||||||
|
transitionSuccessListener();
|
||||||
$state.go('.', { [searchKey]: queryset })
|
$state.go('.', { [searchKey]: queryset })
|
||||||
.then(() => {
|
.then(() => {
|
||||||
// for some reason deleting a tag from a list in a modal does not
|
// for some reason deleting a tag from a list in a modal does not
|
||||||
@@ -219,6 +223,7 @@ function SmartSearchController (
|
|||||||
// that that happened and remove it if it didn't.
|
// that that happened and remove it if it didn't.
|
||||||
const clearedParams = qs.removeTermsFromQueryset($stateParams[searchKey], term, isFilterableBaseField, isRelatedField, isAnsibleFactField, singleSearchParam);
|
const clearedParams = qs.removeTermsFromQueryset($stateParams[searchKey], term, isFilterableBaseField, isRelatedField, isAnsibleFactField, singleSearchParam);
|
||||||
$stateParams[searchKey] = clearedParams;
|
$stateParams[searchKey] = clearedParams;
|
||||||
|
listenForTransitionSuccess();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -246,7 +251,9 @@ function SmartSearchController (
|
|||||||
queryset = cleared;
|
queryset = cleared;
|
||||||
|
|
||||||
if (!$scope.querySet) {
|
if (!$scope.querySet) {
|
||||||
$state.go('.', { [searchKey]: queryset });
|
transitionSuccessListener();
|
||||||
|
$state.go('.', { [searchKey]: queryset })
|
||||||
|
.then(() => listenForTransitionSuccess());
|
||||||
}
|
}
|
||||||
|
|
||||||
qs.search(path, queryset)
|
qs.search(path, queryset)
|
||||||
|
|||||||
Reference in New Issue
Block a user