Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix autocomplete displayed value #63

Open
wants to merge 2 commits into
base: 14.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/HISTORY.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

14.9 (unreleased)
-----------------
* Fix displayed value for autocomplete widget
[mpeeters]

14.8 - (2022-09-15)
---------------------------
* Change: Add support for AJAX filtering with multiselect widget
Expand Down
2 changes: 1 addition & 1 deletion eea/facetednavigation/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
14.8
14.9.dev0
19 changes: 15 additions & 4 deletions eea/facetednavigation/widgets/autocomplete/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,15 @@ Faceted.AutocompleteWidget.prototype = {
},

do_query: function(element){
var value = this.select.select2('val');
if (value && !Array.isArray(value)) {
var raw_value = this.select.select2('data');
var value = null;
if (raw_value && !Array.isArray(raw_value)) {
value = [value];
} else if (raw_value && Array.isArray(raw_value)) {
value = [];
jQuery.each(raw_value, function(idx, val){
value.push(JSON.stringify(val))
});
}

if(!element){
Expand Down Expand Up @@ -138,8 +144,13 @@ Faceted.AutocompleteWidget.prototype = {
}

var data = [];
jQuery.each(value, function(idx, val){
var item = {id: val, text: val};
jQuery.each(value, function(idx, raw_val){
try {
var val = JSON.parse(raw_val);
} catch {
val = {"id": raw_val, "text": raw_val}
}
var item = {id: val.id, text: val.text};
if(self.multiple) {
data.push(item);
} else {
Expand Down
17 changes: 16 additions & 1 deletion eea/facetednavigation/widgets/autocomplete/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,22 @@ def quote_bad_chars(self, string):
string = string.replace(char, self.quotestring(char))
return string

def parse_json(self, value):
if isinstance(value, (tuple, list)):
try:
return [json.loads(v) for v in value]
except ValueError: # This can happen with saved urls
return value
try:
return json.loads(value)
except ValueError:
return value

def normalize_string(self, value):
""" Process string values to be used in catalog query
"""
if isinstance(value, dict):
value = value["id"]
# Ensure words are string instances as ZCatalog requires strings
if isinstance(value, six.binary_type):
value = value.decode('utf-8')
Expand All @@ -62,9 +75,11 @@ def normalize_list(self, value):
def normalize(self, value):
""" Process value to be used in catalog query
"""
# select2 send us selected values separated by a comma
value = self.parse_json(value)
if ',' in value:
value = value.split(',')
if isinstance(value, dict):
value = value["id"]
if isinstance(value, (tuple, list)):
value = self.normalize_list(value)
elif isinstance(value, (six.binary_type, six.text_type)):
Expand Down