Socialify

Folder ..

Viewing fulltext-search.js
35 lines (28 loc) • 980.0 B

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
window.Searcher = (function() {
    function Searcher() {
        this._index = lunr(function () {
            this.field('title', {boost: 10})
            this.field('body')
            this.ref('id')
          }) ;

        this._indexContent = undefined;
    }

    Searcher.prototype.init = function() {
        var self = this;

        $("script[type='text/x-docstrap-searchdb']").each(function(idx, item)  {
            self._indexContent = JSON.parse(item.innerHTML);

            for (var entryId in self._indexContent) {
                self._index.add(self._indexContent[entryId]);
            }
        });
    };

    Searcher.prototype.search = function(searchTerm) {
        var results = [],
                searchResults = this._index.search(searchTerm);

        for (var idx = 0; idx < searchResults.length; idx++) {
            results.push(this._indexContent[searchResults[idx].ref])
        }

        return results;
    };

    return new Searcher();
})();