Debian Codegraph @

kapidox/data/htmlresource/js/main.js

raw ◀ ▶
1 Debian patch touch this file
•1 // Imported from https://invent.kde.org/documentation/develop-kde-org/-/blob/cafdb9dfe7b37b8060e6a4fcc0709bcc80a42d15/assets/js/base.js
2 /*
3 * Copyright 2018 Google LLC
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 https://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 */
17
18 (function($) {
19
20 'use strict';
21
22 $(function() {
23 $('[data-toggle="tooltip"]').tooltip();
24 $('[data-toggle="popover"]').popover();
25
26 $('.popover-dismiss').popover({
27 trigger: 'focus'
28 })
29 });
30
31
32 function bottomPos(element) {
33 return element.offset().top + element.outerHeight();
34 }
35
36 // Bootstrap Fixed Header
37 $(function() {
38 var promo = $(".js-td-cover");
39 if (!promo.length) {
40 return
41 }
42
43 var promoOffset = bottomPos(promo);
44 var navbarOffset = $('.js-navbar-scroll').offset().top;
45
46 var threshold = Math.ceil($('.js-navbar-scroll').outerHeight());
47 if ((promoOffset - navbarOffset) < threshold) {
48 $('.js-navbar-scroll').addClass('navbar-bg-onscroll');
49 }
50
51
52 $(window).on('scroll', function() {
53 var navtop = $('.js-navbar-scroll').offset().top - $(window).scrollTop();
54 var promoOffset = bottomPos($('.js-td-cover'));
55 var navbarOffset = $('.js-navbar-scroll').offset().top;
56 if ((promoOffset - navbarOffset) < threshold) {
57 $('.js-navbar-scroll').addClass('navbar-bg-onscroll');
58 } else {
59 $('.js-navbar-scroll').removeClass('navbar-bg-onscroll');
60 $('.js-navbar-scroll').addClass('navbar-bg-onscroll--fade');
61 }
62 });
63 });
64
65
66 }(jQuery));
67
68 function getOffsetSum(elem) {
69 var top=0, left=0
70 while (elem) {
71 top = top + parseInt(elem.offsetTop)
72 left = left + parseInt(elem.offsetLeft)
73 elem = elem.offsetParent
74 }
75 return { top: top, left: left }
76 }
77
78 function setActiveMenuItem(li) {
79 var isMenuItem = li.matches('#TableOfContents > ul > li')
80 var isSubMenuItem = li.matches('#TableOfContents > ul > li > ul > li')
81 var wasActive = li.classList.contains('active')
82
83 if (isMenuItem || !wasActive) {
84 // Unset current active item
85 var menuItem = document.querySelector('#TableOfContents > ul > li.active')
86 if (menuItem) {
87 menuItem.classList.remove('active')
88 }
89 var menuItem = document.querySelector('#TableOfContents > ul > li > ul > li.active')
90 if (menuItem) {
91 menuItem.classList.remove('active')
92 }
93 }
94 li.classList.add('active')
95
96 if (isMenuItem) {
97 var firstSubMenuItem = li.querySelector('ul > li')
98 if (firstSubMenuItem) {
99 firstSubMenuItem.classList.add('active')
100 }
101 } else if (isSubMenuItem) {
102 var menuItem = li.parentNode.parentNode // TODO: parentUntil('li')
103 menuItem.classList.add('active')
104 }
105
106 // Scroll to item
107 // li.scrollIntoView()
108 }
109 function getMenuItemElement(li) {
110 var id = li.querySelector('a').href.split('#', 2)[1]
111 return document.getElementById(id)
112 }
113 function updateTOC() {
114 var viewTop = window.scrollY
115 var tocList = document.querySelectorAll('#TableOfContents li')
116 // Binary Search maybe (since elements are in order)?
117 for (var i = 0; i < tocList.length; i++) {
118 var curMenuItem = tocList[i]
119 var curElement = getMenuItemElement(curMenuItem)
120 var offset = getOffsetSum(curElement)
121 var offsetDiff = viewTop - offset.top
122 if (offsetDiff < 0) {
123 if (i <= 1) {
124 // At top, select the first item
125 setActiveMenuItem(curMenuItem)
126 } else {
127 var prevMenuItem = tocList[i - 1]
128 var prevElement = getMenuItemElement(prevMenuItem)
129 var prevOffset = getOffsetSum(prevElement)
130 var prevOffsetDiff = viewTop - prevOffset.top
131 var sectionReadRatio = (prevOffsetDiff)/prevMenuItem.offsetHeight
132 if (sectionReadRatio >= 0.7) {
133 // Current item is being read
134 setActiveMenuItem(curMenuItem)
135 } else {
136 // Previous item is still being read
137 setActiveMenuItem(prevMenuItem)
138 }
139 }
140 break
141 } else if (i == tocList.length - 1) {
142 // At bottom, select last item
143 setActiveMenuItem(curMenuItem)
144 }
145 }
146 }
147
148 var updating = false
149 function queueUpdateTOC() {
150 if (!updating) {
151 updating = true
152 requestAnimationFrame(function() {
153 updateTOC()
154 updating = false
155 })
156 }
157 }
158
159 queueUpdateTOC()
•160 window.addEventListener('scroll', queueUpdateTOC)
161
162 // Imported from https://invent.kde.org/documentation/develop-kde-org/-/blob/cafdb9dfe7b37b8060e6a4fcc0709bcc80a42d15/assets/js/anchor.js
163 /*
164 * Copyright 2018 Google LLC
165
166 Licensed under the Apache License, Version 2.0 (the "License");
167 you may not use this file except in compliance with the License.
168 You may obtain a copy of the License at
169
170 https://www.apache.org/licenses/LICENSE-2.0
171
172 Unless required by applicable law or agreed to in writing, software
173 distributed under the License is distributed on an "AS IS" BASIS,
174 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
175 See the License for the specific language governing permissions and
176 limitations under the License.
177 */
178
179 (function ($) {
180 'use strict';
181
182 // Headers' anchor link that shows on hover
183 $(function () {
184 // append anchor links to headings in markdown.
185 var article = document.getElementsByTagName('main')[0];
186 if (!article) {
187 return;
188 }
189 var headings = article.querySelectorAll('h1, h2, h3, h4, h5, h6');
190 headings.forEach(function (heading) {
191 if (heading.id) {
192 var a = document.createElement('a');
193 // set visibility: hidden, not display: none to avoid layout change
194 a.style.visibility = 'hidden';
195 // [a11y] hide this from screen readers, etc..
196 a.setAttribute('aria-hidden', 'true');
197 // material insert_link icon in svg format
198 a.innerHTML = ' <svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" width="24" height="24" viewBox="0 0 24 24"><path d="M0 0h24v24H0z" fill="none"/><path d="M3.9 12c0-1.71 1.39-3.1 3.1-3.1h4V7H7c-2.76 0-5 2.24-5 5s2.24 5 5 5h4v-1.9H7c-1.71 0-3.1-1.39-3.1-3.1zM8 13h8v-2H8v2zm9-6h-4v1.9h4c1.71 0 3.1 1.39 3.1 3.1s-1.39 3.1-3.1 3.1h-4V17h4c2.76 0 5-2.24 5-5s-2.24-5-5-5z"/></svg>';
199 a.href = '#' + heading.id;
200 heading.insertAdjacentElement('beforeend', a);
201 heading.addEventListener('mouseenter', function () {
202 a.style.visibility = 'initial';
203 });
204 heading.addEventListener('mouseleave', function () {
205 a.style.visibility = 'hidden';
206 });
207 }
208 });
209 });
210
211 }(jQuery));
212
213 // Imported from https://invent.kde.org/documentation/develop-kde-org/-/blob/cafdb9dfe7b37b8060e6a4fcc0709bcc80a42d15/assets/js/offline-search.js
214 // Adapted from code by Matt Walters https://www.mattwalters.net/posts/hugo-and-lunr/
215
216 (function ($) {
217 'use strict';
218
219 $(document).ready(function () {
220 const $searchInput = $('.td-search-input');
221
222 //
223 // Options for popover
224 //
225
226 $searchInput.data('html', true);
227 $searchInput.data('placement', 'bottom');
228 $searchInput.data(
229 'template',
230 '<div class="popover offline-search-result" role="tooltip"><div class="arrow"></div><h3 class="popover-header"></h3><div class="popover-body"></div></div>'
231 );
232
233 //
234 // Register handler
235 //
236
237 $searchInput.on('change', (event) => {
238 render($(event.target));
239
240 // Hide keyboard on mobile browser
241 $searchInput.blur();
242 });
243
244 // Prevent reloading page by enter key on sidebar search.
245 $searchInput.closest('form').on('submit', () => {
246 return false;
247 });
248
249 //
250 // Lunr
251 //
252
253 let idx = null; // Lunr index
254 const resultDetails = new Map(); // Will hold the data for the search results (titles and summaries)
255
256 // Set up for an Ajax call to request the JSON data file that is created by Hugo's build process
257 $.ajax($searchInput.data('offline-search-index-json-src')).then(
258 (data) => {
259 idx = lunr(function () {
260 this.ref('ref');
261 this.field('title', { boost: 2 });
262 this.field('body');
263
264 data.forEach((doc) => {
265 this.add(doc);
266
267 resultDetails.set(doc.ref, {
268 title: doc.title,
269 excerpt: doc.excerpt,
270 });
271 });
272 });
273
274 $searchInput.trigger('change');
275 }
276 );
277
278 const render = ($targetSearchInput) => {
279 // Dispose the previous result
280 $targetSearchInput.popover('dispose');
281
282 //
283 // Search
284 //
285
286 if (idx === null) {
287 return;
288 }
289
290 const searchQuery = $targetSearchInput.val();
291 if (searchQuery === '') {
292 return;
293 }
294
295 const results = idx
296 .query((q) => {
297 const tokens = lunr.tokenizer(searchQuery.toLowerCase());
298 tokens.forEach((token) => {
299 const queryString = token.toString();
300 q.term(queryString, {
301 boost: 100,
302 });
303 q.term(queryString, {
304 wildcard:
305 lunr.Query.wildcard.LEADING |
306 lunr.Query.wildcard.TRAILING,
307 boost: 10,
308 });
309 q.term(queryString, {
310 editDistance: 2,
311 });
312 });
313 })
314 .slice(0, 10);
315
316 //
317 // Make result html
318 //
319
320 const $html = $('<div>');
321
322 $html.append(
323 $('<div>')
324 .css({
325 display: 'flex',
326 justifyContent: 'space-between',
327 marginBottom: '1em',
328 })
329 .append(
330 $('<span>')
331 .text('Search results')
332 .css({ fontWeight: 'bold' })
333 )
334 .append(
335 $('<i>')
336 .addClass('fas fa-times search-result-close-button')
337 .css({
338 cursor: 'pointer',
339 })
340 )
341 );
342
343 const $searchResultBody = $('<div>').css({
344 maxHeight: `calc(100vh - ${
345 $targetSearchInput.offset().top -
346 $(window).scrollTop() +
347 180
348 }px)`,
349 overflowY: 'auto',
350 });
351 $html.append($searchResultBody);
352
353 if (results.length === 0) {
354 $searchResultBody.append(
355 $('<p>').text(`No results found for query "${searchQuery}"`)
356 );
357 } else {
358 results.forEach((r) => {
359 const $cardHeader = $('<div>').addClass('card-header');
360 const doc = resultDetails.get(r.ref);
361 const href =
362 $searchInput.data('offline-search-base-href') +
363 r.ref.replace(/^\//, '');
364
365 $cardHeader.append(
366 $('<a>').attr('href', href).text(doc.title)
367 );
368
369 const $cardBody = $('<div>').addClass('card-body');
370 $cardBody.append(
371 $('<p>')
372 .addClass('card-text text-muted')
373 .text(doc.excerpt)
374 );
375
376 const $card = $('<div>').addClass('card');
377 $card.append($cardHeader).append($cardBody);
378
379 $searchResultBody.append($card);
380 });
381 }
382
383 $targetSearchInput.on('shown.bs.popover', () => {
384 $('.search-result-close-button').on('click', () => {
385 $targetSearchInput.val('');
386 $targetSearchInput.trigger('change');
387 });
388 });
389
390 $targetSearchInput
391 .data('content', $html[0].outerHTML)
392 .popover('show');
393 };
394 });
395 })(jQuery);