Skip to content

Commit 08813a7

Browse files
committed
add functional elements of search
1 parent 930e8fd commit 08813a7

File tree

5 files changed

+118
-5
lines changed

5 files changed

+118
-5
lines changed

_includes/tutorial_navigation.html

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,17 @@
1111
{% endfor %}
1212
</ul>
1313

14-
<div class="menu" data-toggle="#menu-list">
15-
<div class="hamburger-icon"> &#9776</div>
16-
<div class="menu-title">Chapter {{page.number}} -- {{ page.title }}</div>
14+
<div class="menu-container">
15+
<div class="menu" data-toggle="#menu-list">
16+
<div class="hamburger-icon"> &#9776</div>
17+
<div class="menu-title">Chapter {{page.number}} -- {{ page.title }}</div>
18+
</div>
19+
20+
<div class="search">
21+
<form action="/rails_tutorial/search" method="get" target="_self">
22+
<input type="text" id="search-box" name="query">
23+
</form>
24+
</div>
1725
</div>
26+
1827
</div>

_scss/tutorial/tutorial_nav.scss

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@
1313
text-decoration: none;
1414
}
1515

16+
.menu-container {
17+
display: table;
18+
width: 100%;
19+
}
20+
1621
.menu {
1722
font-size: 1.4em;
1823
cursor: pointer;
19-
display: table;
20-
width: 100%;
24+
display: table-cell;
25+
width: 75%;
2126
height: $nav-bar-height;
2227
}
2328

@@ -31,6 +36,7 @@
3136
.menu-title {
3237
display: table-cell;
3338
vertical-align: middle;
39+
width: 75%;
3440
}
3541

3642
ul {
@@ -52,6 +58,17 @@
5258
height: 100%;
5359
width: 0%;
5460
}
61+
62+
.search {
63+
display: table-cell;
64+
vertical-align: middle;
65+
width: 25%;
66+
67+
input {
68+
height: 25px;
69+
width: 200px;
70+
}
71+
}
5572
}
5673

5774
@media (min-width: 480px) {

js/lunr.min.js

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/search.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
(function() {
2+
function displaySearchResults(results, store) {
3+
var searchResults = document.getElementById('search-results');
4+
5+
if (results.length) { // Are there any results?
6+
var appendString = '';
7+
8+
for (var i = 0; i < results.length; i++) { // Iterate over the results
9+
var item = store[results[i].ref];
10+
appendString += '<li><a href="' + item.url + '"><h3>' + item.title + '</h3></a>';
11+
}
12+
13+
searchResults.innerHTML = appendString;
14+
} else {
15+
searchResults.innerHTML = '<li>No results found</li>';
16+
}
17+
}
18+
19+
function getQueryVariable(variable) {
20+
var query = window.location.search.substring(1);
21+
var vars = query.split('&');
22+
23+
for (var i = 0; i < vars.length; i++) {
24+
var pair = vars[i].split('=');
25+
26+
if (pair[0] === variable) {
27+
return decodeURIComponent(pair[1].replace(/\+/g, '%20'));
28+
}
29+
}
30+
}
31+
32+
var searchTerm = getQueryVariable('query');
33+
34+
if (searchTerm) {
35+
document.getElementById('search-box').setAttribute("value", searchTerm);
36+
37+
// Initalize lunr with the fields it will be searching on. I've given title
38+
// a boost of 10 to indicate matches on this field are more important.
39+
var idx = lunr(function () {
40+
this.field('id');
41+
this.field('title');
42+
this.field('content');
43+
});
44+
45+
for (var key in window.store) { // Add the data to lunr
46+
idx.add({
47+
'id': key,
48+
'title': window.store[key].title,
49+
'author': window.store[key].author,
50+
'category': window.store[key].category,
51+
'content': window.store[key].content
52+
});
53+
54+
var results = idx.search(searchTerm); // Get lunr to perform a search
55+
displaySearchResults(results, window.store); // We'll write this in the next section
56+
}
57+
}
58+
})();

search.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
layout: rails_tutorial
3+
title: Search
4+
permalink: /search/
5+
---
6+
7+
8+
<ul id="search-results"></ul>
9+
10+
<script>
11+
window.store = {
12+
{% for chapter in site.chapters %}
13+
"{{ chapter.url | slugify }}": {
14+
"title": "{{ chapter.title | xml_escape }}",
15+
"content": {{ chapter.content | strip_html | strip_newlines | jsonify }},
16+
"url": "{{ site.baseurl | append: chapter.url | xml_escape }}"
17+
}
18+
{% unless forloop.last %},{% endunless %}
19+
{% endfor %}
20+
};
21+
</script>
22+
<script src="{{ site.baseurl }}/js/lunr.min.js"></script>
23+
<script src="{{ site.baseurl }}/js/search.js"></script>

0 commit comments

Comments
 (0)