Merge pull request #1661 from mabashian/679-searchable-fields

Consume searchable fields from api options response
This commit is contained in:
Michael Abashian 2018-05-03 11:21:12 -04:00 committed by GitHub
commit da5781bfc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 9 deletions

View File

@ -364,7 +364,7 @@ function QuerysetService ($q, Rest, ProcessErrors, $rootScope, Wait, DjangoSearc
return merged;
},
getSearchInputQueryset (searchInput, isRelatedField = null, isAnsibleFactField = null, singleSearchParam = null) {
getSearchInputQueryset (searchInput, isFilterableBaseField = null, isRelatedField = null, isAnsibleFactField = null, singleSearchParam = null) {
// XXX Should find a better approach than passing in the two 'is...Field' callbacks XXX
const space = '%20and%20';
let params = {};
@ -406,12 +406,12 @@ function QuerysetService ($q, Rest, ProcessErrors, $rootScope, Wait, DjangoSearc
if (termParts.length === 1) {
termParams = searchWithoutKey(term, singleSearchParam);
} else if (isAnsibleFactField && isAnsibleFactField(termParts)) {
} else if ((isAnsibleFactField && isAnsibleFactField(termParts)) || (isFilterableBaseField && isFilterableBaseField(termParts))) {
termParams = this.encodeParam({ term, singleSearchParam });
} else if (isRelatedField && isRelatedField(termParts)) {
termParams = this.encodeParam({ term, singleSearchParam, related: true });
} else {
termParams = this.encodeParam({ term, singleSearchParam });
termParams = searchWithoutKey(term, singleSearchParam);
}
params = _.merge(params, termParams, combineSameSearches);
@ -419,7 +419,7 @@ function QuerysetService ($q, Rest, ProcessErrors, $rootScope, Wait, DjangoSearc
return params;
},
removeTermsFromQueryset(queryset, term, isRelatedField = null, singleSearchParam = null) {
removeTermsFromQueryset(queryset, term, isFilterableBaseField, isRelatedField = null, isAnsibleFactField = null, singleSearchParam = null) {
const modifiedQueryset = _.cloneDeep(queryset);
const removeSingleTermFromQueryset = (value, key) => {
@ -457,10 +457,12 @@ function QuerysetService ($q, Rest, ProcessErrors, $rootScope, Wait, DjangoSearc
if (termParts.length === 1) {
removed = searchWithoutKey(term, singleSearchParam);
} else if ((isAnsibleFactField && isAnsibleFactField(termParts)) || (isFilterableBaseField && isFilterableBaseField(termParts))) {
removed = this.encodeParam({ term, singleSearchParam });
} else if (isRelatedField && isRelatedField(termParts)) {
removed = this.encodeParam({ term, singleSearchParam, related: true });
} else {
removed = this.encodeParam({ term, singleSearchParam });
removed = searchWithoutKey(term, singleSearchParam);
}
if (!removed) {

View File

@ -52,6 +52,12 @@ function SmartSearchController (
.then((data) => {
$scope.models = data.models;
$scope.options = data.options.data;
$scope.keyFields = _.reduce(data.models[$scope.djangoModel].base, function(result, value, key) {
if (!(typeof value.filterable === "boolean" && value.filterable === false)) {
result.push(key);
}
return result;
}, []);
if ($scope.list) {
$scope.$emit(optionsKey, data.options);
}
@ -139,6 +145,17 @@ function SmartSearchController (
return rootField === 'ansible_facts';
}
function isFilterableBaseField (termParts) {
const rootField = termParts[0].split('.')[0].replace(/^-/, '');
const listName = $scope.list.name;
const baseFieldPath = `models.${listName}.base.${rootField}`;
const isBaseField = _.has($scope, `${baseFieldPath}`);
const filterable = _.get($scope, `${baseFieldPath}.filterable`);
const isNotFilterable = typeof filterable === "boolean" && filterable === false;
const isBaseModelRelatedSearchTermField = (_.get($scope, `${baseFieldPath}.type`) === 'field');
return isBaseField && !isBaseModelRelatedSearchTermField && !isNotFilterable;
}
function isRelatedField (termParts) {
const rootField = termParts[0].split('.')[0].replace(/^-/, '');
const listName = $scope.list.name;
@ -154,7 +171,7 @@ function SmartSearchController (
const { singleSearchParam } = $scope;
const unmodifiedQueryset = _.clone(queryset);
const searchInputQueryset = qs.getSearchInputQueryset(terms, isRelatedField, isAnsibleFactField, singleSearchParam);
const searchInputQueryset = qs.getSearchInputQueryset(terms, isFilterableBaseField, isRelatedField, isAnsibleFactField, singleSearchParam);
queryset = qs.mergeQueryset(queryset, searchInputQueryset, singleSearchParam);
// Go back to the first page after a new search
@ -191,7 +208,7 @@ function SmartSearchController (
const { singleSearchParam } = $scope;
const [term] = $scope.searchTags.splice(index, 1);
queryset = qs.removeTermsFromQueryset(queryset, term, isRelatedField, singleSearchParam);
queryset = qs.removeTermsFromQueryset(queryset, term, isFilterableBaseField, isRelatedField, isAnsibleFactField, singleSearchParam);
if (!$scope.querySet) {
$state.go('.', { [searchKey]: queryset })
@ -199,7 +216,7 @@ function SmartSearchController (
// for some reason deleting a tag from a list in a modal does not
// remove the param from $stateParams. Here we'll manually check to make sure
// that that happened and remove it if it didn't.
const clearedParams = qs.removeTermsFromQueryset($stateParams[searchKey], term, isRelatedField, singleSearchParam);
const clearedParams = qs.removeTermsFromQueryset($stateParams[searchKey], term, isFilterableBaseField, isRelatedField, isAnsibleFactField, singleSearchParam);
$stateParams[searchKey] = clearedParams;
});
}

View File

@ -42,7 +42,7 @@
</div>
</div>
<div class="SmartSearch-keyRow">
<b translate>FIELDS:</b> <span ng-repeat="(key,value) in model.base">{{ key }}<span ng-if="!$last">, </span></span>
<b translate>FIELDS:</b> <span ng-repeat="field in keyFields">{{ field }}<span ng-if="!$last">, </span></span>
</div>
<div class="SmartSearch-keyRow" ng-show="model.related && model.related.length > 0">
<b translate>RELATED FIELDS:</b> <span ng-repeat="relation in model.related">{{ relation }}<span ng-if="!$last">, </span></span>