| 1 | window.Searcher = (function() { |
| 2 | function Searcher() { |
| 3 | this._index = lunr(function () { |
| 4 | this.field('title', {boost: 10}) |
| 5 | this.field('body') |
| 6 | this.ref('id') |
| 7 | }) ; |
| 8 | |
| 9 | this._indexContent = undefined; |
| 10 | } |
| 11 | |
| 12 | Searcher.prototype.init = function() { |
| 13 | var self = this; |
| 14 | |
| 15 | $("script[type='text/x-docstrap-searchdb']").each(function(idx, item) { |
| 16 | self._indexContent = JSON.parse(item.innerHTML); |
| 17 | |
| 18 | for (var entryId in self._indexContent) { |
| 19 | self._index.add(self._indexContent[entryId]); |
| 20 | } |
| 21 | }); |
| 22 | }; |
| 23 | |
| 24 | Searcher.prototype.search = function(searchTerm) { |
| 25 | var results = [], |
| 26 | searchResults = this._index.search(searchTerm); |
| 27 | |
| 28 | for (var idx = 0; idx < searchResults.length; idx++) { |
| 29 | results.push(this._indexContent[searchResults[idx].ref]) |
| 30 | } |
| 31 | |
| 32 | return results; |
| 33 | }; |
| 34 | |
| 35 | return new Searcher(); |
| 36 | })(); |