-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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 #4265] Porting frontend docsearch to work with new API #4340
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,48 +23,48 @@ function attach_elastic_search_query(data) { | |
|
||
search_url.href = api_host; | ||
search_url.pathname = '/api/v2/docsearch/'; | ||
search_url.search = '?q=' + $.urlencode(query) + '&project=' + project + | ||
'&version=' + version + '&language=' + language; | ||
search_url.search = '?query=' + $.urlencode(query) + '&project=' + project + | ||
'&version=' + version + '&language=' + language; | ||
|
||
search_def | ||
.then(function (results) { | ||
var hits = results.hits || {}; | ||
var hit_list = hits.hits || []; | ||
.then(function (data) { | ||
var hit_list = data.results || []; | ||
var total_count = data.count || 0; | ||
|
||
if (hit_list.length) { | ||
for (var n in hit_list) { | ||
var hit = hit_list[n]; | ||
var fields = hit.fields || {}; | ||
var list_item = $('<li style="display: none;"></li>'); | ||
var item_url = document.createElement('a'); | ||
var highlight = hit.highlight; | ||
|
||
item_url.href += fields.link + | ||
DOCUMENTATION_OPTIONS.FILE_SUFFIX; | ||
item_url.search = '?highlight=' + $.urlencode(query); | ||
|
||
// Result list elements | ||
list_item.append( | ||
$('<a />') | ||
.attr('href', item_url) | ||
.html(fields.title) | ||
); | ||
// fields.project is returned as an array | ||
if (fields.project.indexOf(project) === -1) { | ||
list_item.append( | ||
$('<span>') | ||
.text(" (from project " + fields.project + ")") | ||
); | ||
for (var i = 0; i < hit_list.length; i += 1) { | ||
var doc = hit_list[i]; | ||
var highlight = doc.highlight; | ||
var $list_item = $('<li style="display: none;"></li>'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see what you're doing here, but we don't follow this convention anywhere else in the code, so we shouldn't do it here to be consistent. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand that we do not follow the convention. But it actually makes me bit much of confused while writing JS code. without knowing if its a jquery object, its hard to keep track. I am ok to have it like before, but what do you think if we should start following the convention? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for consistency, we don't prefix vars with |
||
|
||
// Creating the result from elements | ||
var link = doc.link + DOCUMENTATION_OPTIONS.FILE_SUFFIX + | ||
'?highlight=' + $.urlencode(query); | ||
|
||
var $item = $('<a>', {'href': link}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
$item.html(doc.title); | ||
$list_item.append($item); | ||
|
||
// If the document is from subproject, add extra information | ||
if (doc.project !== project) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Be careful that this doesn't revert to buggy behavior, if the return attribute There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed over slack, the new API always return |
||
var text = " (from project " + doc.project + ")"; | ||
var $extra = $('<span>', {'text': text}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. more |
||
|
||
$list_item.append($extra); | ||
} | ||
if (highlight.content.length) { | ||
var content = $('<div class="context">') | ||
.html(xss(highlight.content[0])); | ||
content.find('em').addClass('highlighted'); | ||
list_item.append(content); | ||
|
||
// Show highlighted texts | ||
if (highlight.content) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we not need to check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the previous API, the |
||
var content_text = xss(highlight.content[0]); | ||
var $contents = $('<div class="context">'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More |
||
|
||
$contents.html(content_text); | ||
$contents.find('em').addClass('highlighted'); | ||
$list_item.append($contents); | ||
} | ||
|
||
Search.output.append(list_item); | ||
list_item.slideDown(5); | ||
Search.output.append($list_item); | ||
$list_item.slideDown(5); | ||
} | ||
} | ||
|
||
|
@@ -74,7 +74,7 @@ function attach_elastic_search_query(data) { | |
} | ||
else { | ||
Search.status.text( | ||
_('Search finished, found %s page(s) matching the search query.').replace('%s', hit_list.length) | ||
_('Search finished, found %s page(s) matching the search query.').replace('%s', total_count) | ||
); | ||
} | ||
}) | ||
|
@@ -96,11 +96,11 @@ function attach_elastic_search_query(data) { | |
withCredentials: true, | ||
}, | ||
complete: function (resp, status_code) { | ||
if (typeof (resp.responseJSON) === 'undefined' || | ||
typeof (resp.responseJSON.results) === 'undefined') { | ||
console.log(status_code); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Debug logging can be removed |
||
if (status_code !== 'success' || resp.responseJSON.count === 0) { | ||
return search_def.reject(); | ||
} | ||
return search_def.resolve(resp.responseJSON.results); | ||
return search_def.resolve(resp.responseJSON); | ||
} | ||
}) | ||
.error(function (resp, status_code, error) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for (var n in hit_list)
is a more readable iteration format and is support down to ie6