Dynamic search key examples

This commit is contained in:
Michael Abashian 2017-02-15 17:04:30 -05:00
parent 3ff4bb0d14
commit 1bb0207bc5
3 changed files with 27 additions and 7 deletions

View File

@ -16,6 +16,7 @@ class DjangoSearchModel {
return relatedSearchField.replace(/\__search$/, "");
}
this.name = name;
this.searchExamples = [];
this.related = _.map(relatedSearchFields, trimRelated);
// Remove "object" type fields from this list
for (var key in baseFields) {
@ -27,5 +28,23 @@ class DjangoSearchModel {
}
delete baseFields.url;
this.base = baseFields;
if(baseFields.id) {
this.searchExamples.push("id:>10");
}
// Loop across the base fields and try find one of type = string and one of type = datetime
let stringFound = false,
dateTimeFound = false;
_.forEach(baseFields, (value, key) => {
if(!stringFound && value.type === 'string') {
this.searchExamples.push(key + ":foobar");
stringFound = true;
}
if(!dateTimeFound && value.type === 'datetime') {
this.searchExamples.push(key + ":>=\"2000-01-01T00:00:00Z\"");
this.searchExamples.push(key + ":<2000-01-01");
dateTimeFound = true;
}
});
}
}

View File

@ -65,7 +65,7 @@ export default ['$q', 'Rest', 'ProcessErrors', '$rootScope', 'Wait', 'DjangoSear
let concated = '';
angular.forEach(value, function(item){
if(item && typeof item === 'string') {
item = item.replace(/"|'/g, "");
item = decodeURIComponent(item).replace(/"|'/g, "");
}
concated += `${key}=${item}&`;
});
@ -73,7 +73,7 @@ export default ['$q', 'Rest', 'ProcessErrors', '$rootScope', 'Wait', 'DjangoSear
}
else {
if(value && typeof value === 'string') {
value = value.replace(/"|'/g, "");
value = decodeURIComponent(value).replace(/"|'/g, "");
}
return `${key}=${value}&`;
}
@ -128,7 +128,7 @@ export default ['$q', 'Rest', 'ProcessErrors', '$rootScope', 'Wait', 'DjangoSear
}
}
return {[paramString] : valueString};
return {[paramString] : encodeURIComponent(valueString)};
},
// decodes a django queryset param into a ui smart-search tag or set of tags
decodeParam(value, key){
@ -164,7 +164,10 @@ export default ['$q', 'Rest', 'ProcessErrors', '$rootScope', 'Wait', 'DjangoSear
decodedParam = '<=' + decodedParam;
split = split.splice(0, split.length-1);
}
return exclude ? `-${split.join('.')}:${decodedParam}` : `${split.join('.')}:${decodedParam}`;
let uriDecodedParam = decodeURIComponent(decodedParam);
return exclude ? `-${split.join('.')}:${uriDecodedParam}` : `${split.join('.')}:${uriDecodedParam}`;
}
};

View File

@ -39,9 +39,7 @@
<div class="SmartSearch-examples--title">
<b translate>EXAMPLES:</b>
</div>
<div class="SmartSearch-examples--search">name:foo</div>
<div class="SmartSearch-examples--search">organization.name:Default</div>
<div class="SmartSearch-examples--search">id:>10</div>
<div class="SmartSearch-examples--search" ng-repeat="searchExample in model.searchExamples">{{searchExample}}</div>
</div>
</div>
<div class="SmartSearch-keyRow">