UNPKG

4.29 kBJavaScriptView Raw
1$(document).ready(function() {
2
3 vis.createBreadcrumbs($(".container.full").first());
4 vis.initSiteSearch();
5 vis.initKeywords();
6
7 $("#tipue_search_input").keyup(checkInput)
8 vis.typingTimeout = 0;
9
10});
11
12function checkInput() {
13 if (document.getElementById("tipue_search_input").value.length > 3) {
14 clearTimeout(vis.typingTimeout);
15 vis.typingTimeout = setTimeout(function () {vis.initSiteSearch(true)},200);
16 }
17 else {
18 var title = document.title.replace(/(\(.+\) )/g,"");
19 document.title = title;
20 document.getElementById("search-results-wrapper").style.display = "none";
21 }
22}
23
24// namespace
25var vis = {};
26
27/**
28 * Adds a breadcrumb as first child to the specified container.
29 *
30 * @author felixhayashi
31 */
32vis.createBreadcrumbs = function(container) {
33
34 // use the url to infer the path
35 var crumbs = location.pathname.split('/');
36
37 // number of ancestor directories
38 var stepbackIndex = crumbs.length-1;
39 var breadcrumbs = $.map(crumbs, function(crumb, i) {
40
41 // first and last element of the split
42 if(!crumb) return;
43
44 stepbackIndex--;
45
46 if(/\.html$/.test(crumb)) {
47
48 // strip the .html to make it look prettier
49 return "<span>" + crumb.replace(/\.html$/, "") + "</span>";
50
51 } else {
52
53 // calculate the relative url
54 for(var ref=crumb+"/", j=0; j<stepbackIndex; j++, ref="../"+ref);
55
56 return "<a href='" + ref + "'>" + crumb + "</a>";
57 }
58 }).join("") || "Home";
59
60 // insert into the container at the beginning.
61 $(container).prepend("<div id=\"breadcrumbs\">" + breadcrumbs + "</div>");
62
63};
64
65/**
66 * Will load tipue search field.
67 * If the search has already begun, we also display the results.
68 *
69 * For information how it works:
70 * @see https://github.com/almende/vis/issues/909#issuecomment-120119414
71 * @see https://github.com/almende/vis/issues/909#issuecomment-120397562
72 *
73 * @author felixhayashi
74 */
75vis.initSiteSearch = function(dynamic) { // Added dynamic flag for live update ~ Alex
76 $("#tipue_search_input").tipuesearch({
77 "mode": "live",
78 "show": 3,
79 },dynamic);
80
81
82 var hasSearchMessage = $("#tipue_search_content").children().length > 0;
83 if(hasSearchMessage) {
84 // show result panel
85 if ($("#search-results-wrapper").css("display") === 'none') {
86 $("#search-results-wrapper").css("display", "block");
87 }
88 // encode the keywords that were entered by the user
89 var keywords = $("#tipue_search_input").val().replace(/\s/g, ",");
90 // add keywords to result-urls
91 $(".tipue_search_content_url a, .tipue_search_content_title a").each(function() {
92 $(this).attr("href", $(this).attr("href") + "?keywords=" + keywords);
93 });
94 } else {
95 $("#search-results-wrapper").css("display", "none");
96 }
97
98};
99
100/**
101 * Will highlight the keywords that are passed as url get-parameters.
102 * All keywords are higlighted and a panel is displayed to jump to the
103 * first keyword found.
104 *
105 * For information how it works:
106 * @see https://github.com/almende/vis/issues/909#issuecomment-120119414
107 * @see https://github.com/almende/vis/issues/909#issuecomment-120397562
108 *
109 * @author felixhayashi
110 */
111vis.initKeywords = function() {
112
113 // extract keywords from get-variable
114 var keywords = url("?keywords");
115
116 if(keywords) {
117
118 // highlighting all keywords
119 keywords = keywords.split(",");
120 for(var i = 0; i < keywords.length; i++) {
121 $("body").highlight(keywords[i]);
122 }
123
124 // nasty hack: programmatically open full options tab
125 // because no browser allows scrolling to hidden elements!
126 $("[role=presentation][targetnode=fullOptions]").click();
127 $("tr.toggle:not(collapsible)").click();
128
129 // init keyword info panel
130 $("#keyword-info").css("display", "block");
131 $("#keyword-count").text($(".highlight").length);
132 $("#keyword-jumper-button").on('click', function(event) {
133 event.preventDefault();
134 // do not cache hits outside the handler; creates problems with prettyfy lib
135 // we use the first visible(!) hit at the time the button is clicked
136 var firstHit = $(".highlight:visible").first();
137 if(firstHit.length) {
138 $("html, body").animate({ scrollTop: $(firstHit).offset().top }, 2000);
139 }
140 });
141
142 }
143
144};
\No newline at end of file