Skip to content

Commit cc095e1

Browse files
committed
Hackity hack the new search in.
1 parent a112ade commit cc095e1

File tree

1 file changed

+92
-82
lines changed

1 file changed

+92
-82
lines changed

themes/cakephp/static/app.js

Lines changed: 92 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,8 @@
11
App = {};
22

33
App.Book = (function() {
4-
5-
var menu = [];
6-
var base;
7-
var delay = (function(){
8-
var timer = 0;
9-
return function(callback, ms){
10-
clearTimeout(timer);
11-
timer = setTimeout(callback, ms);
12-
};
13-
})();
14-
15-
var positionElement = function (to, element) {
16-
var position = to.offset();
17-
var height = to.outerHeight();
18-
var width = to.outerWidth();
19-
20-
element.css({
21-
position: 'absolute',
22-
top: position.top + height + 'px',
23-
left: position.left + 'px',
24-
width: width + 'px'
25-
});
26-
};
27-
28-
var searchResults;
294

305
function init() {
31-
base = location.href.replace(location.protocol + '//' + location.host, '').split('/').slice(0, 3).join('/') + '/';
32-
searchResults = $('#inline-search-results');
33-
34-
// SEARCH EVENT
35-
$('.search-input').keyup(function() {
36-
if ($(this).val() === '') {
37-
searchResults.fadeOut('fast');
38-
} else {
39-
var _this = $(this);
40-
delay(function() {
41-
positionElement(_this, searchResults);
42-
searchResults.show();
43-
search_menu(_this.val());
44-
}, 200);
45-
}
46-
});
47-
48-
49-
$(document).keyup(function(e) {
50-
// escape key
51-
if (e.keyCode == 27) {
52-
if ($('.search-input').val() === '') {
53-
searchResults.fadeOut('fast');
54-
}
55-
$('#page-contents').fadeOut('fast');
56-
}
57-
});
58-
59-
$.getJSON(base + '_static/menu.json', function(data) {
60-
menu = data;
61-
});
626

637
// Make top nav responsive.
648
$('#cakephp-global-navigation ul').responsiveMenu();
@@ -82,32 +26,15 @@ App.Book = (function() {
8226
e.stopPropagation();
8327
$('#page-contents').fadeIn('fast');
8428
});
85-
$(document).bind('click', function (e) {
86-
$('#page-contents').fadeOut('fast');
87-
});
88-
}
89-
90-
function wait() {
9129

92-
}
93-
94-
function search_menu(query) {
95-
searchResults.empty().append('<ul></ul>');
96-
var matches = [];
97-
$.each(menu, function(index, item) {
98-
var scored_node = {
99-
score : item.text.toLowerCase().score(query.toLowerCase()),
100-
node : item
101-
};
102-
matches.push(scored_node);
103-
});
104-
matches.sort(compare_scores);
105-
results = matches.slice(0, 20);
106-
$.each(results, function(index, item) {
107-
if (item.score > 0) {
108-
searchResults.find('ul').append(
109-
"<li><a href='" + base + item.node.link + "'>" +
110-
item.node.text + "</a></li>");
30+
var hideContents = function (e) {
31+
$('#page-contents').fadeOut('fast');
32+
};
33+
34+
$(document).bind('click', hideContents);
35+
$(document).bind('keyup', function (e) {
36+
if (e.keyCode == 27) {
37+
hideContents();
11138
}
11239
});
11340
}
@@ -121,6 +48,89 @@ App.Book = (function() {
12148
}
12249
})();
12350

51+
// Inline search form, and standalone search form.
52+
App.Book.Search = (function () {
53+
54+
var segments = location.pathname.split('/');
55+
var base = '/' + segments.slice(1, segments.length - 2).join('/') + '/';
56+
var searchResults;
57+
var searchInput;
58+
var searchUrl = 'http://localhost/docs_search/search';
59+
60+
var delay = (function(){
61+
var timer = 0;
62+
return function(callback, ms){
63+
clearTimeout(timer);
64+
timer = setTimeout(callback, ms);
65+
};
66+
})();
67+
68+
var positionElement = function (to, element) {
69+
var position = to.offset();
70+
var height = to.outerHeight();
71+
var width = to.outerWidth();
72+
73+
element.css({
74+
position: 'absolute',
75+
top: position.top + height + 'px',
76+
left: position.left + 'px',
77+
width: width + 'px'
78+
});
79+
};
80+
81+
var handleKeyEvent = function (event) {
82+
if ($(this).val() === '') {
83+
searchResults.fadeOut('fast');
84+
} else {
85+
var _this = $(this);
86+
delay(function() {
87+
positionElement(_this, searchResults);
88+
searchResults.show();
89+
doSearch(_this.val());
90+
}, 200);
91+
}
92+
};
93+
94+
// escape key
95+
var handleEscape = function (event) {
96+
if (event.keyCode == 27 && searchInput.val() === '') {
97+
searchResults.fadeOut('fast');
98+
}
99+
};
100+
101+
var doSearch = function (value) {
102+
var url = searchUrl + '?lang=en&q=' + encodeURIComponent(value);
103+
var xhr = $.ajax({
104+
url: url,
105+
dataType: 'json',
106+
type: 'GET'
107+
});
108+
109+
xhr.done(function (response) {
110+
searchResults.empty().append('<ul></ul>');
111+
var results = response.data.slice(0, 10);
112+
$.each(results, function(index, item) {
113+
searchResults.find('ul').append(
114+
"<li><a href='" + base + item.url + "'>" +
115+
item.contents.join("\n") + "</a></li>");
116+
});
117+
});
118+
119+
};
120+
121+
var init = function () {
122+
searchInput = $('.search-input');
123+
searchResults = $('#inline-search-results');
124+
125+
searchInput.keyup(handleKeyEvent);
126+
$(document).keyup(handleEscape);
127+
};
128+
129+
return {
130+
init: init
131+
};
132+
})();
133+
124134

125135
function levenshtein (s1, s2) {
126136
// http://kevin.vanzonneveld.net
@@ -339,7 +349,7 @@ jQuery.extend(jQuery.expr[':'], {
339349
});
340350
}
341351
})(jQuery);
342-
343352

344353

345354
$(App.Book.init);
355+
$(App.Book.Search.init);

0 commit comments

Comments
 (0)