1 // $Id$
  2 
  3 /**
  4  * Interacts with Solr's SpellCheckComponent.
  5  *
  6  * <p>Requires <tt>String#strtr</tt> defined in <tt>ajaxsolr.support.js</tt>.
  7  *
  8  * @see http://wiki.apache.org/solr/SpellCheckComponent
  9  *
 10  * @class AbstractSpellcheckWidget
 11  * @augments AjaxSolr.AbstractWidget
 12  */
 13 AjaxSolr.AbstractSpellcheckWidget = AjaxSolr.AbstractWidget.extend(
 14   /** @lends AjaxSolr.AbstractSpellcheckWidget.prototype */
 15   {
 16   /**
 17    * The suggestions.
 18    *
 19    * @field
 20    * @private
 21    * @type Object
 22    * @default {}
 23    */
 24   suggestions: {},
 25 
 26   /**
 27    * Uses the top suggestion for each word to return a suggested query.
 28    *
 29    * @returns {String} A suggested query.
 30    */
 31   suggestion: function () {
 32     var replacePairs = {};
 33     for (var word in this.suggestions) {
 34       replacePairs[word] = this.suggestions[word][0];
 35     }
 36     return this.manager.response.responseHeader.params['spellcheck.q'].strtr(replacePairs);
 37   },
 38 
 39   beforeRequest: function () {
 40     if (this.manager.store.get('spellcheck').val() && this.manager.store.get('q').val()) {
 41       this.manager.store.get('spellcheck.q').val(this.manager.store.get('q').val());
 42     }
 43     else {
 44       this.manager.store.remove('spellcheck.q');
 45     }
 46   },
 47 
 48   afterRequest: function () {
 49     this.suggestions = {};
 50 
 51     if (this.manager.response.spellcheck && this.manager.response.spellcheck.suggestions) {
 52       var suggestions = this.manager.response.spellcheck.suggestions;
 53 
 54       for (var word in suggestions) {
 55         if (word == 'collation' || word == 'correctlySpelled') continue;
 56 
 57         this.suggestions[word] = [];
 58         for (var i = 0, l = suggestions[word].suggestion.length; i < l; i++) {
 59           if (this.manager.response.responseHeader.params['spellcheck.extendedResults']) {
 60             this.suggestions[word].push(suggestions[word].suggestion[i].word);
 61           }
 62           else {
 63             this.suggestions[word].push(suggestions[word].suggestion[i]);
 64           }
 65         }
 66       }
 67 
 68       if (AjaxSolr.size(this.suggestions)) {
 69         this.handleSuggestions(this.manager.response);
 70       }
 71     }
 72   },
 73 
 74   /**
 75    * An abstract hook for child implementations.
 76    *
 77    * <p>Allow the child to handle the suggestions without parsing the response.</p>
 78    */
 79   handleSuggestions: function () {}
 80 });
 81