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