1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,92 @@ |
1 |
+$(function() { |
|
2 |
+ var SearchView = Backbone.View.extend({ |
|
3 |
+ events: { |
|
4 |
+ "click #search-clear": "clear" |
|
5 |
+ }, |
|
6 |
+ |
|
7 |
+ initialize: function() { |
|
8 |
+ this.algolia = algoliasearch("M19DXW5X0Q", "c79b2e61519372a99fa5890db070064c"); |
|
9 |
+ this.algoliaHelper = algoliasearchHelper(this.algolia, "font_awesome"); |
|
10 |
+ this.template = _.template($("#results-template").html()); |
|
11 |
+ |
|
12 |
+ this.$searchInput = this.$("#search-input"); |
|
13 |
+ this.$searchResultsSection = this.$("#search-results"); |
|
14 |
+ this.$searchInputClear = this.$("#search-clear"); |
|
15 |
+ this.$iconsSection = this.$("#icons"); |
|
16 |
+ |
|
17 |
+ this.$searchInput.on("keyup", _.debounce(_.bind(this.search, this), 200)); |
|
18 |
+ this.algoliaHelper.on("result", _.bind(this.showResults, this)); |
|
19 |
+ }, |
|
20 |
+ |
|
21 |
+ search: function(event) { |
|
22 |
+ var query = this.$searchInput.val(); |
|
23 |
+ |
|
24 |
+ if (query !== "") { |
|
25 |
+ this. algoliaHelper.setQuery(query).search(); |
|
26 |
+ } else { |
|
27 |
+ this.clearResults(); |
|
28 |
+ } |
|
29 |
+ }, |
|
30 |
+ |
|
31 |
+ clear: function(event) { |
|
32 |
+ event.preventDefault(); |
|
33 |
+ |
|
34 |
+ this.clearResults(); |
|
35 |
+ }, |
|
36 |
+ |
|
37 |
+ showResults: function(content, state) { |
|
38 |
+ this.$searchResultsSection.html(""); |
|
39 |
+ this.$searchResultsSection.removeClass("hide"); |
|
40 |
+ this.$searchInputClear.removeClass("hide"); |
|
41 |
+ this.$iconsSection.addClass("hide"); |
|
42 |
+ |
|
43 |
+ var results = []; |
|
44 |
+ |
|
45 |
+ _.each(content.hits, function(result) { |
|
46 |
+ results.push(new SearchResultView({ result: result }).render()) |
|
47 |
+ }); |
|
48 |
+ |
|
49 |
+ this.$searchResultsSection.html(this.template({ content: content, results: results.join("") })); |
|
50 |
+ }, |
|
51 |
+ |
|
52 |
+ clearResults: function() { |
|
53 |
+ this.$searchInput.val("").focus(); |
|
54 |
+ this.$searchResultsSection.addClass("hide"); |
|
55 |
+ this.$searchResultsSection.html(""); |
|
56 |
+ this.$searchInputClear.addClass("hide"); |
|
57 |
+ this.$iconsSection.removeClass("hide"); |
|
58 |
+ } |
|
59 |
+ }); |
|
60 |
+ |
|
61 |
+ var SearchResultView = Backbone.View.extend({ |
|
62 |
+ initialize: function(options) { |
|
63 |
+ this.template = _.template($("#result-template").html()); |
|
64 |
+ this.result = options.result |
|
65 |
+ }, |
|
66 |
+ |
|
67 |
+ render: function() { |
|
68 |
+ var matches = []; |
|
69 |
+ |
|
70 |
+ this.pullMatches(matches, this.result._highlightResult.aliases); |
|
71 |
+ this.pullMatches(matches, this.result._highlightResult.synonyms); |
|
72 |
+ |
|
73 |
+ return this.template({ result: this.result, matches: matches }); |
|
74 |
+ }, |
|
75 |
+ |
|
76 |
+ pullMatches: function(matches, list) { |
|
77 |
+ if (list !== undefined) { |
|
78 |
+ _.each(list, function(highlight) { |
|
79 |
+ if (highlight.matchLevel !== "none") { |
|
80 |
+ matches.push(highlight.value) |
|
81 |
+ } |
|
82 |
+ }) |
|
83 |
+ } |
|
84 |
+ } |
|
85 |
+ }); |
|
86 |
+ |
|
87 |
+ var $searchViewElement = $("[data-view=search]"); |
|
88 |
+ |
|
89 |
+ if ($searchViewElement.length > 0) { |
|
90 |
+ new SearchView({ el: $searchViewElement }); |
|
91 |
+ } |
|
92 |
+}); |