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

adding kind and path attributes to suggest response object and using it in autocomplete #464

Merged
merged 1 commit into from
Apr 7, 2021
Merged
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
3 changes: 3 additions & 0 deletions src/server/internalServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,8 @@ std::unique_ptr<Response> InternalServer::handle_suggest(const RequestContext& r
MustacheData result;
result.set("label", suggestion[0]);
result.set("value", suggestion[0]);
result.set("kind", "path");
result.set("path", suggestion[1]);
result.set("first", first);
first = false;
results.push_back(result);
Expand All @@ -428,6 +430,7 @@ std::unique_ptr<Response> InternalServer::handle_suggest(const RequestContext& r
MustacheData result;
result.set("label", "containing '" + term + "'...");
result.set("value", term + " ");
result.set("kind", "pattern");
result.set("first", first);
results.push_back(result);
}
Expand Down
14 changes: 9 additions & 5 deletions static/skin/taskbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,30 @@ function htmlDecode(input) {

(function ($) {
const root = $( `link[type='root']` ).attr("href");
const bookName = window.location.pathname.split(`${root}/`)[1].split('/')[0];

$( "#kiwixsearchbox" ).autocomplete({

source: `${root}/suggest?content=${window.location.pathname.split(`${root}/`)[1].split('/')[0]}`,
source: `${root}/suggest?content=${bookName}`,
dataType: "json",
cache: false,

response: function( event, ui ) {

for(const item of ui.content) {
item.label = htmlDecode(item.label);
item.value = htmlDecode(item.value);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since path is HTML-encoded in suggestion.json (that is important for producing a valid JSON in case if path contains a double quote symbol), it must be htmlDecode-ed here, too.

if (item.path) item.path = htmlDecode(item.path);
}
},

select: function(event, ui) {
$( "#kiwixsearchbox" ).val(ui.item.value);
$( "#kiwixsearchform" ).submit();
if (ui.item.kind === 'path') {
window.location.href = `${root}/${bookName}/${encodeURI(ui.item.path)}`;
} else {
$( "#kiwixsearchbox" ).val(ui.item.value);
$( "#kiwixsearchform" ).submit();
}
},

});

/* cybook hack */
Expand Down
6 changes: 5 additions & 1 deletion static/templates/suggestion.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
{{#suggestions}}{{^first}},{{/first}}
{
"value" : "{{value}}",
"label" : "{{label}}"
"label" : "{{label}}",
"kind" : "{{kind}}"
{{#path}}
, "path" : "{{path}}"
{{/path}}
}{{/suggestions}}
]