From c3571474b92e3eb716d2646fa66357e4c61f2d81 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Sun, 22 Feb 2015 14:31:39 +0100 Subject: Search: work on spelling suggestion --- chimere/static/chimere/js/search-autocomplete.js | 39 ++++++++++++++++++++++-- chimere/templates/search/search.html | 28 +++++++++++------ chimere/views.py | 8 ++++- 3 files changed, 62 insertions(+), 13 deletions(-) diff --git a/chimere/static/chimere/js/search-autocomplete.js b/chimere/static/chimere/js/search-autocomplete.js index ea7531d..5e8a85e 100644 --- a/chimere/static/chimere/js/search-autocomplete.js +++ b/chimere/static/chimere/js/search-autocomplete.js @@ -1,4 +1,7 @@ +var do_you_mean = "Do you mean: "; +var end_do_you_mean = "?"; + var Autocomplete = function(options) { this.form_selector = options.form_selector; this.url = options.url || '/search/autocomplete/'; @@ -28,14 +31,23 @@ Autocomplete.prototype.setup = function() { } self.fetch(query); - }) + }); // on selecting a result, populate the search field. this.form_elem.on('click', '.ac-result', function(ev) { self.query_box.val($(this).text()); $('.ac-results').remove(); + $('#spelling').fadeOut(); return false; - }) + }); + + // on selecting a suggestion, populate the search field. + $('#search-box').on('click', '.spelling-item', function(ev) { + self.query_box.val($(this).text()); + $('.ac-results').remove(); + $('#spelling').fadeOut(); + return false; + }); } Autocomplete.prototype.fetch = function(query) { @@ -50,10 +62,33 @@ Autocomplete.prototype.fetch = function(query) { } else { $('.ac-results').remove(); } + if(data.spelling.length){ + self.show_spelling(data.spelling) + } else { + $("#spelling").fadeOut(); + } + return true; } }) } +Autocomplete.prototype.show_spelling = function(spelling) { + var text = do_you_mean; + var base_elem = '' + var end_base_elem = ''; + for(var offset in spelling) { + if (offset > 0){ + text += ", "; + } + text += base_elem; + text += spelling[offset]; + text += end_base_elem; + } + text += end_do_you_mean; + $("#spelling").html(text); + $("#spelling").fadeIn(); +} + Autocomplete.prototype.show_results = function(data) { // Remove any existing results. $('.ac-results').remove(); diff --git a/chimere/templates/search/search.html b/chimere/templates/search/search.html index d5a0579..b0c7045 100644 --- a/chimere/templates/search/search.html +++ b/chimere/templates/search/search.html @@ -1,17 +1,24 @@ {% load url from future %}{% load i18n %} + {% if query %}