This commit is contained in:
emanuele-f 2017-04-12 20:15:25 +02:00
parent 5a7e4c3b99
commit ea5db96bb0
2 changed files with 45 additions and 6 deletions

View file

@ -15,6 +15,25 @@
</div>
<script type='text/javascript'>
var _typeahead_prototype = $.fn.typeahead.Constructor.prototype;
var _typeahead_overridden_callbacks = {
"click": null,
"select": null,
}
/* Temporary override the typeahead click callback */
for (var callback_name in _typeahead_overridden_callbacks) {
/* Dump original callback */
_typeahead_overridden_callbacks[callback_name] = _typeahead_prototype[callback_name];
_typeahead_prototype[callback_name] = function() {
if (this.$menu.find("> li[data-no-results]").length == 0) {
/* invoke the original callback */
_typeahead_overridden_callbacks[callback_name].apply(this, arguments);
}
};
}
$('#{{typeahead.base_id}}_search_field').typeahead({
source: function (query, process) {
var _get = {};
@ -24,12 +43,28 @@
_get["query"] = query;
return $.get("{*typeahead.query_url*}", _get, function (data) {
if (data.results)
return process(data.results);
var res = process(data.results);
var menu = res.$menu;
menu.click(function (e) { e.preventDefault(); });
if (data.results && data.results.length > 0) {
$("li:last", menu).addClass("hidden");
} else {
$("li:last", menu)
.attr("data-no-results", "")
.html("<span style='padding-left:1em; cursor:default;'>No results found</span>");
}
return res;
});
}, afterSelect: function(item) {
$("#{{typeahead.base_id}}_query").val(item["{{typeahead.json_key}}"]);
}
if (! item.no_results) {
$("#{{typeahead.base_id}}_query")
.val(item["{{typeahead.json_key}}"])
.closest("form").submit();
}
}, addItem: {name:"", no_results:true},
});
$('#{{typeahead.base_id}}_submit').click(function(e) {
@ -44,5 +79,9 @@
}
}
});
/* Restore the original callbacks for new objects */
for (var callback_name in _typeahead_overridden_callbacks)
_typeahead_prototype[callback_name] = _typeahead_overridden_callbacks[callback_name];
</script>
</form>

View file

@ -51,9 +51,9 @@ for _, tab in pairs(menu_subpages) do
-- Decorate with tab label
addResult(entry.title, tab, tab.label .. ": ")
break
elseif matchesQuery(entry.description, query) then
--[[elseif matchesQuery(entry.description, query) then
-- Decorate with entry title and tab label
addResult(entry.description, tab, tab.label .. " [" .. entry.title .. "]: ")
addResult(entry.description, tab, tab.label .. " [" .. entry.title .. "]: ")]]--
end
end
end