@@ -59,25 +59,48 @@ function shiftWindow() {
59
59
}
60
60
61
61
function initSearch ( ) {
62
- var elements = new Bloodhound ( {
63
- datumTokenizer : function ( d ) {
64
- return Bloodhound . tokenizers . whitespace ( d . name ) ;
62
+ var index ;
63
+
64
+ function findMatches ( q , cb ) {
65
+ var matches , substringRegex ;
66
+
67
+ // an array that will be populated with substring matches
68
+ matches = [ ] ;
69
+
70
+ // regex used to determine if a string contains the substring `q`
71
+ substrRegex = new RegExp ( q , 'i' ) ;
72
+
73
+ // iterate through the pool of strings and for any string that
74
+ // contains the substring `q`, add it to the `matches` array
75
+ $ . each ( index , function ( i , element ) {
76
+ if ( substrRegex . test ( element . name ) ) {
77
+ matches . push ( element . name ) ;
78
+ }
79
+ } ) ;
80
+
81
+ cb ( matches ) ;
82
+ } ;
83
+
84
+ function initTypeahead ( ) {
85
+ $ ( '#search-box.typeahead' ) . typeahead ( {
86
+ hint : true ,
87
+ highlight : true ,
88
+ minLength : 3
65
89
} ,
66
- queryTokenizer : Bloodhound . tokenizers . whitespace ,
67
- prefetch : 'index.json'
68
- } ) ;
90
+ {
91
+ name : 'elements' ,
92
+ source : findMatches
93
+ } ) ;
94
+ }
69
95
70
- $ ( '#search-box.typeahead' ) . typeahead ( {
71
- hint : true ,
72
- highlight : true ,
73
- minLength : 3
74
- } ,
75
- {
76
- name : 'elements' ,
77
- source : elements ,
78
- displayKey : 'name' ,
79
- limit : 10
96
+ var jsonReq = new XMLHttpRequest ( ) ;
97
+ jsonReq . open ( 'GET' , 'index.json' , true ) ;
98
+ jsonReq . addEventListener ( 'load' , function ( ) {
99
+ index = JSON . parse ( jsonReq . responseText ) ;
100
+ initTypeahead ( ) ;
80
101
} ) ;
102
+ jsonReq . send ( ) ;
103
+
81
104
}
82
105
83
106
document . addEventListener ( "DOMContentLoaded" , function ( ) {
0 commit comments