Skip to content

Commit

Permalink
adding kind and path attributes to suggest response object and using …
Browse files Browse the repository at this point in the history
…it in autocomplete
  • Loading branch information
MananJethwani committed Apr 7, 2021
1 parent e4be97a commit 5cb276a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
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);
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}}
]

0 comments on commit 5cb276a

Please sign in to comment.