-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmain.js
225 lines (184 loc) · 6.68 KB
/
main.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
---
---
function slugify(str) {
return String(str)
.normalize("NFKD") // split accented characters into their base characters and diacritical marks
.replace(/[\u0300-\u036f]/g, "") // remove all the accents, which happen to be all in the \u03xx UNICODE block.
.trim() // trim leading or trailing whitespace
.toLowerCase() // convert to lowercase
.replace(/[^a-z0-9 -]/g, "") // remove non-alphanumeric characters
.replace(/\s+/g, "-") // replace spaces with hyphens
.replace(/-+/g, "-"); // remove consecutive hyphens
}
function scrollToElement(id) {
const slugID = slugify(decodeURIComponent(id));
const element = document.getElementById(slugID);
if (element) {
const innerContainer = document.querySelector(".doc-inner"); // Adjust the selector as needed
innerContainer.scrollTop = element.offsetTop;
}
}
$(document).ready(function () {
/* ===== Affix Sidebar ===== */
/* Ref: http://getbootstrap.com/javascript/#affix-examples */
if (window.location.hash) {
const hash = window.location.hash.substring(1); // Remove the '#' character
scrollToElement(hash);
}
$("#doc-menu").affix({
offset: {
top: $("#header").outerHeight(true) + $("#doc-header").outerHeight(true) + 45,
bottom: $("#footer").outerHeight(true) + $("#promo-block").outerHeight(true) + 75,
},
});
/* Hack related to: https://github.com/twbs/bootstrap/issues/10236 */
$(window).on("load resize", function () {
$(window).trigger("scroll");
});
/* Activate scrollspy menu */
/* Smooth scrolling */
// $("a.scrollto").on("click", function (e) {
// //store hash
// var target = this.hash;
// e.preventDefault();
// $(".doc-inner").scrollTo(target, 800, { offset: 0, axis: "y" });
// // display anchor
// document.location.hash = target;
// });
/* ======= jQuery Responsive equal heights plugin ======= */
/* Ref: https://github.com/liabru/jquery-match-height */
$("#cards-wrapper .item-inner").matchHeight();
$("#showcase .card").matchHeight();
/* Bootstrap lightbox */
/* Ref: http://ashleydw.github.io/lightbox/ */
$(document).delegate('*[data-toggle="lightbox"]', "click", function (e) {
e.preventDefault();
$(this).ekkoLightbox();
});
window.addEventListener(
"hashchange",
function (e) {
e.preventDefault(); // Prevent default scroll behavior
// Optional: Add your custom scrolling or other logic here
// For example, manually scroll to an element with the ID from the hash
const hash = window.location.hash.replace("#", "");
const targetElement = document.getElementById(hash);
if (targetElement) {
// Perform a scroll to the target element or other custom actions
// For instance, scroll an inner div instead of the window
// innerDiv.scrollTop = targetElement.offsetTop;
}
},
false
);
const headings = document.querySelectorAll("h2, h3");
const scrollContainer = document.querySelector(".doc-inner");
const navContainer = document.querySelector(".doc-menu");
if (!navContainer){
return;
}
var h2List;
const offsetMap = [];
headings.forEach((heading, index) => {
if (heading.tagName == "H2") {
const navItem = document.createElement("li");
navItem.innerHTML = `<a class="scrollto" href="#${heading.textContent}">${heading.textContent}</a>`;
navContainer.appendChild(navItem);
h2List = undefined;
offsetMap[index] = { nav: navItem, offset: heading.getBoundingClientRect().top + scrollContainer.scrollTop };
} else if (heading.tagName == "H3") {
if (!h2List) {
h2List = document.createElement("ul");
h2List.classList = "nav doc-sub-menu";
navContainer.appendChild(h2List);
}
const navItem = document.createElement("li");
navItem.innerHTML = `<a class="scrollto" href="#${heading.textContent}">${heading.textContent}</a>`;
h2List.appendChild(navItem);
offsetMap[index] = { nav: navItem, offset: heading.getBoundingClientRect().top + scrollContainer.scrollTop };
}
});
const navLinks = document.querySelectorAll('a[href^="#"]');
navLinks.forEach((anchor) => {
anchor.addEventListener("click", function (e) {
e.preventDefault(); // Prevent default anchor behavior
const hash = this.getAttribute("href");
// Manually update the hash
history.pushState(null, null, hash);
// Optional: Add custom logic here for what should happen when a hash link is clicked
scrollToElement(hash.substring(1).toLowerCase());
});
});
var selected;
scrollContainer.addEventListener("scroll", function () {
selected?.classList.remove("active");
selected = offsetMap[0].nav;
console.log("top: " + scrollContainer.scrollTop);
for (var i = 1; i < offsetMap.length; i++) {
const section = offsetMap[i];
if (section.offset < scrollContainer.scrollTop + 220) {
const section2 = offsetMap[i + 1];
if (!section2) {
selected = section.nav;
break;
}
if (!(section2.offset < scrollContainer.scrollTop + 220)) {
selected = section.nav;
break;
}
}
}
if (selected) {
selected.classList.add("active");
}
});
});
// Instanciating InstantSearch.js with Algolia credentials
const search = instantsearch({
appId: "A2BO3OY80G",
indexName: "wurstscript",
apiKey: "dab955d0d2f46d0fa72e845eb9ea0ff2",
searchFunction(helper) {
const container = document.querySelector("#search-hits");
container.style.display = helper.state.query === "" ? "none" : "";
helper.search();
},
});
const hitTemplate = function (hit) {
let url = `${hit.url}#${hit.anchor}`;
const title = hit._highlightResult.title?.value;
let breadcrumbs = "";
if (hit._highlightResult.headings) {
breadcrumbs = hit._highlightResult.headings
.map((match) => {
return `<span class="post-breadcrumb">${match.value}</span>`;
})
.join(" > ");
}
const content = hit._highlightResult.html?.value;
return `
<div class="post-item">
<h2><a class="post-link" href="${url}">${title}</a></h2>
{{#breadcrumbs}}<a href="${url}" class="post-breadcrumbs">${breadcrumbs}</a>{{/breadcrumbs}}
<div class="post-snippet">${content}</div>
</div>
`;
};
// Adding searchbar and results widgets
search.addWidget(
instantsearch.widgets.searchBox({
container: "#search-searchbar",
placeholder: "Search Documentation",
poweredBy: true, // This is required if you're on the free Community plan
})
);
search.addWidget(
instantsearch.widgets.hits({
container: "#search-hits",
templates: {
item: hitTemplate,
},
})
);
// Starting the search
search.start();