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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
(function($) {
'use strict';
/**
* Search modal with Algolia
* @constructor
*/
var SearchModal = function() {
this.$openButton = $('.open-algolia-search');
this.$searchModal = $('#algolia-search-modal');
this.$closeButton = this.$searchModal.find('.close-button');
this.$searchForm = $('#algolia-search-form');
this.$searchInput = $('#algolia-search-input');
this.$results = this.$searchModal.find('.results');
this.$noResults = this.$searchModal.find('.no-result');
this.$resultsCount = this.$searchModal.find('.results-count');
this.algolia = algoliaIndex;
};
SearchModal.prototype = {
/**
* Run feature
* @returns {void}
*/
run: function() {
var self = this;
// open modal when open button is clicked
self.$openButton.click(function() {
self.open();
});
// open modal when `s` button is pressed
$(document).keyup(function(event) {
var target = event.target || event.srcElement;
// exit if user is focusing an input
if (target.tagName.toUpperCase() === 'INPUT') {
return;
}
if (event.keyCode === 83 && !self.$searchModal.is(':visible')) {
self.open();
}
});
// close button when overlay is clicked
self.$searchModal.click(function(e) {
if (e.target === this) {
self.close();
}
});
// close modal when close button is clicked
self.$closeButton.click(function() {
self.close();
});
// close modal when `ESC` button is pressed
$(document).keyup(function(e) {
if (e.keyCode === 27 && self.$searchModal.is(':visible')) {
self.close();
}
});
// send search when form is submitted
self.$searchForm.submit(function(event) {
event.preventDefault();
self.search(self.$searchInput.val());
});
},
/**
* Open search modal and display overlay
* @returns {void}
*/
open: function() {
this.showSearchModal();
this.showOverlay();
this.$searchInput.focus();
},
/**
* Close search modal and overlay
* @returns {void}
*/
close: function() {
this.hideSearchModal();
this.hideOverlay();
this.$searchInput.blur();
},
/**
* Search with Algolia API and display results
* @param {String} search
* @returns {void}
*/
search: function(search) {
var self = this;
this.algolia.search(search, function(err, content) {
if (!err) {
self.showResults(content.hits);
self.showResultsCount(content.nbHits);
}
});
},
/**
* Display results
* @param {Array} posts
* @returns {void}
*/
showResults: function(posts) {
var html = '';
posts.forEach(function(post) {
var lang = window.navigator.userLanguage || window.navigator.language || post.lang;
html += '<div class="media">';
if (post.thumbnailImageUrl) {
html += '<div class="media-left">';
html += '<a class="link-unstyled" href="' + (post.link || post.permalink) + '">';
html += '<img class="media-image" ' +
'src="' + post.thumbnailImageUrl + '" ' +
'width="90" height="90"/>';
html += '</a>';
html += '</div>';
}
html += '<div class="media-body">';
html += '<a class="link-unstyled" href="' + (post.link || post.permalink) + '">';
html += '<h3 class="media-heading">' + post.title + '</h3>';
html += '</a>';
html += '<span class="media-meta">';
html += '<span class="media-date text-small">';
html += moment(post.date).locale(lang).format('ll');
html += '</span>';
html += '</span>';
html += '<div class="media-content hide-xs font-merryweather">' + post.excerpt + '</div>';
html += '</div>';
html += '<div style="clear:both;"></div>';
html += '<hr>';
html += '</div>';
});
this.$results.html(html);
},
/**
* Show search modal
* @returns {void}
*/
showSearchModal: function() {
this.$searchModal.fadeIn();
},
/**
* Hide search modal
* @returns {void}
*/
hideSearchModal: function() {
this.$searchModal.fadeOut();
},
/**
* Display messages and counts of results
* @param {Number} count
* @returns {void}
*/
showResultsCount: function(count) {
var string = '';
if (count < 1) {
string = this.$resultsCount.data('message-zero');
this.$noResults.show();
}
else if (count === 1) {
string = this.$resultsCount.data('message-one');
this.$noResults.hide();
}
else if (count > 1) {
string = this.$resultsCount.data('message-other').replace(/\{n\}/, count);
this.$noResults.hide();
}
this.$resultsCount.html(string);
},
/**
* Show overlay
* @returns {void}
*/
showOverlay: function() {
$('body').append('<div class="overlay"></div>');
$('.overlay').fadeIn();
$('body').css('overflow', 'hidden');
},
/**
* Hide overlay
* @returns {void}
*/
hideOverlay: function() {
$('.overlay').fadeOut(function() {
$(this).remove();
$('body').css('overflow', 'auto');
});
}
};
$(document).ready(function() {
// launch feature only if there is an Algolia index available
if (typeof algoliaIndex !== 'undefined') {
var searchModal = new SearchModal();
searchModal.run();
}
});
})(jQuery);
|