Files: 084c01b393044ead2958af330fb5de1515de5682 / javascripts / ta.js
2242 bytesRaw
1 | var substringMatcher = function(strs) { |
2 | return function findMatches(q, cb) { |
3 | var matches, substrRegex; |
4 | |
5 | // an array that will be populated with substring matches |
6 | matches = []; |
7 | |
8 | // regex used to determine if a string contains the substring `q` |
9 | substrRegex = new RegExp(q, 'i'); |
10 | |
11 | // iterate through the pool of strings and for any string that |
12 | // contains the substring `q`, add it to the `matches` array |
13 | $.each(strs, function(i, str) { |
14 | if (substrRegex.test(str)) { |
15 | // the typeahead jQuery plugin expects suggestions to a |
16 | // JavaScript object, refer to typeahead docs for more info |
17 | matches.push({ value: str }); |
18 | } |
19 | }); |
20 | |
21 | cb(matches); |
22 | }; |
23 | }; |
24 | |
25 | var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', |
26 | 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', |
27 | 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', |
28 | 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', |
29 | 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', |
30 | 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', |
31 | 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', |
32 | 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', |
33 | 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming' |
34 | ]; |
35 | |
36 | $('#the-basics .typeahead').typeahead({ |
37 | hint: true, |
38 | highlight: true, |
39 | minLength: 1 |
40 | }, |
41 | { |
42 | name: 'states', |
43 | displayKey: 'value', |
44 | source: substringMatcher(states) |
45 | }); |
46 | |
47 | // constructs the suggestion engine |
48 | var states = new Bloodhound({ |
49 | datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'), |
50 | queryTokenizer: Bloodhound.tokenizers.whitespace, |
51 | // `states` is an array of state names defined in "The Basics" |
52 | local: $.map(states, function(state) { return { value: state }; }) |
53 | }); |
54 | |
55 | // kicks off the loading/processing of `local` and `prefetch` |
56 | states.initialize(); |
57 | |
58 | $('#bloodhound .typeahead').typeahead({ |
59 | hint: true, |
60 | highlight: true, |
61 | minLength: 1 |
62 | }, |
63 | { |
64 | name: 'states', |
65 | displayKey: 'value', |
66 | // `ttAdapter` wraps the suggestion engine in an adapter that |
67 | // is compatible with the typeahead jQuery plugin |
68 | source: states.ttAdapter() |
69 | }); |
70 |
Built with git-ssb-web