-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.js
50 lines (49 loc) · 1.84 KB
/
search.js
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
define(
["core/paths", "core/events", "core/ui/content", "core/css", "libraries/mustache", "text!templates/search.html"],
function (paths, events, content, css, mustache, searchTemplate) {
css.load("modules/search");
document.querySelector("#menubar").insertAdjacentHTML("afterEnd", mustache.to_html(searchTemplate));
document.querySelector("#search").addEventListener("submit", function (event) {
event.preventDefault();
paths.publish("search/" + this.querySelector("input[type='search']").value);
}, false);
var resultsTimeout = null;
var resultsElement = null;
var resultsListener = function (payload) {
clearPending();
require(["text!templates/searchResults.html"], function (searchResultsTemplate) {
var html = mustache.to_html(searchResultsTemplate, payload);
if (resultsElement) {
resultsElement.insertAdjacentHTML("beforeEnd", html);
}
});
return true;
};
var clearPending = function () {
var pendingElement = document.querySelector("#search-results-waiting");
if (pendingElement) {
pendingElement.parentNode.removeChild(pendingElement);
}
};
new content({
path: /^search/,
open: function (path, element) {
css.load("modules/searchPage");
require(["text!templates/searchPage.html"], function (searchPageTemplate) {
var queryString = path.substring(7);
element.insertAdjacentHTML("afterBegin", mustache.to_html(searchPageTemplate, {query: queryString}));
resultsElement = element.querySelector("#search-results-list");
events.subscribe("search.results", resultsListener);
resultsTimeout = setTimeout(clearPending, 2000);
events.publish("search.query", queryString);
});
},
close: function (path, element) {
css.unload("modules/searchPage");
resultsElement = null;
events.unsubscribe("search.results", resultsListener);
clearTimeout(resultsTimeout);
}
});
return {};
});