11$ ( function ( ) {
22 var searchIndex ,
3- searchHits ;
3+ searchHits ,
4+ searchWorker = new Worker ( "/assets/javascripts/search_worker.js" ) ;
45
56 $ ( '.help-search .search-box' ) . focus ( function ( ) {
67 $ ( this ) . css ( 'background-position' , '0px -25px' )
@@ -17,15 +18,22 @@ $(function() {
1718 if ( localStorage [ 'searchIndex' ] ) {
1819 searchIndex = JSON . parse ( localStorage [ 'searchIndex' ] ) ;
1920
20- if ( localStorageHasExpired ( ) )
21+ if ( localStorageHasExpired ( ) ) {
2122 loadSearchIndex ( ) ;
23+ }
24+ else {
25+ searchIndex . type = "index" ;
26+ searchWorker . postMessage ( searchIndex ) ;
27+ }
2228 } else {
2329 loadSearchIndex ( ) ;
2430 }
2531
2632 function loadSearchIndex ( ) {
27- $ . getJSON ( '/assets/search-index.json' , function ( data ) {
28- searchIndex = data [ "pages" ] ;
33+ $ . getJSON ( '/search/search-index.json' , function ( data ) {
34+ data . type = { index : true } ;
35+ searchWorker . postMessage ( data ) ;
36+ searchIndex = data ;
2937 localStorage [ 'searchIndex' ] = JSON . stringify ( searchIndex ) ;
3038 localStorage [ 'updated' ] = new Date ( ) . getTime ( ) ;
3139 } ) ;
@@ -118,22 +126,17 @@ $(function() {
118126 function searchForString ( searchString ) {
119127 searchHits = [ ] ;
120128 searchString = searchString . toLowerCase ( ) ;
129+ searchWorker . postMessage ( { query : searchString , type : "search" } )
130+ }
121131
122- // Search for string in all pages
123- for ( var i = 0 ; i < searchIndex . length ; i ++ ) {
124- var page = searchIndex [ i ] ;
125-
126- // Add the page to the array of hits if there's a match
127- if ( page . title . toLowerCase ( ) . indexOf ( searchString ) !== - 1 ) {
128- searchHits . push ( page ) ;
129- }
132+ searchWorker . addEventListener ( "message" , function ( e ) {
133+ if ( e . data . type . search ) {
134+ renderResultsForSearch ( e . data . query , e . data . results ) ;
130135 }
131-
132- renderResultsForSearch ( searchString ) ;
133- }
136+ } ) ;
134137
135138 // Update the UI representation of the search hits
136- function renderResultsForSearch ( searchString ) {
139+ function renderResultsForSearch ( searchString , searchHits ) {
137140 $ ( "#search-results" ) . empty ( ) ;
138141
139142 // Check if there are any results. If not, show placeholder and exit
@@ -146,7 +149,7 @@ $(function() {
146149 for ( var i = 0 ; i < Math . min ( searchHits . length , 8 ) ; i ++ ) {
147150 var page = searchHits [ i ] ;
148151
149- $ ( '<li class="result"><a href="' + page . url + '"><em>' + page . title + '</em><small>' + page . section + '</small>< /a></li>') . appendTo ( "#search-results" ) ;
152+ $ ( '<li class="result"><a href="' + page . url + '"><em>' + page . title + '</em></a></li>' ) . appendTo ( "#search-results" ) ;
150153 }
151154
152155 // Select the first alternative
0 commit comments