Skip to content

Commit 2f59b45

Browse files
committed
display what the suggestion is and what it is enclosed by, selecting a suggestion takes the user there
1 parent 6a51aec commit 2f59b45

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

lib/resources/script.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function shiftWindow() {
5959
}
6060

6161
function initSearch() {
62-
var index;
62+
var index; // the JSON data
6363

6464
function findMatches(q, cb) {
6565
var matches, substringRegex;
@@ -74,22 +74,44 @@ function initSearch() {
7474
// contains the substring `q`, add it to the `matches` array
7575
$.each(index, function(i, element) {
7676
if (substrRegex.test(element.name)) {
77-
matches.push(element.name);
77+
matches.push(element);
7878
}
7979
});
8080

8181
cb(matches);
8282
};
8383

8484
function initTypeahead() {
85+
$('#search-box').prop('disabled', false);
86+
8587
$('#search-box.typeahead').typeahead({
8688
hint: true,
8789
highlight: true,
8890
minLength: 3
8991
},
9092
{
9193
name: 'elements',
92-
source: findMatches
94+
source: findMatches,
95+
display: function(element) { return element.name; },
96+
templates: {
97+
suggestion: function(match) {
98+
return [
99+
'<div>',
100+
match.name,
101+
' ',
102+
match.type.toLowerCase(),
103+
(match.enclosedBy ? [
104+
'<div class="search-from-lib">from ',
105+
match.enclosedBy.name,
106+
'</div>'].join('') : ''),
107+
'</div>'
108+
].join('');
109+
}
110+
}
111+
});
112+
113+
$('#search-box.typeahead').bind('typeahead:select', function(ev, suggestion) {
114+
window.location = suggestion.href;
93115
});
94116
}
95117

@@ -100,7 +122,6 @@ function initSearch() {
100122
initTypeahead();
101123
});
102124
jsonReq.send();
103-
104125
}
105126

106127
document.addEventListener("DOMContentLoaded", function() {

lib/resources/styles.css

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,12 @@ button {
610610

611611
/* find-as-you-type search box */
612612

613+
/* override bootstrap defaults */
614+
.form-control {
615+
border-radius: 0;
616+
border: 0;
617+
}
618+
613619
.typeahead,
614620
.tt-query,
615621
.tt-hint {
@@ -624,10 +630,6 @@ button {
624630
background-color: #fff;
625631
}
626632

627-
.typeahead:focus {
628-
border: 2px solid #0097cf;
629-
}
630-
631633
.tt-query {
632634
-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
633635
-moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
@@ -673,3 +675,8 @@ button {
673675
.tt-suggestion p {
674676
margin: 0;
675677
}
678+
679+
.search-from-lib {
680+
font-style: italic;
681+
color: gray;
682+
}

lib/templates/_footer.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
</div> <!-- row -->
12
</div> <!-- container -->
23

34
<footer>

lib/templates/_head.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<div class="self-name">{{self.name}}</div>
4040
<form class="navbar-form navbar-right" role="search">
4141
<div class="form-group">
42-
<input type="text" id="search-box" class="form-control typeahead" placeholder="Search">
42+
<input type="text" id="search-box" disabled class="form-control typeahead" placeholder="Search">
4343
</div>
4444
</form>
4545
</div>
@@ -70,3 +70,4 @@ <h1 class="title">
7070
</header>
7171

7272
<div class="container body">
73+
<div class="row">

0 commit comments

Comments
 (0)