]>
nmode's Git Repositories - Rnaught/blob - pkgdown.js
1 /* http://gregfranko.com/blog/jquery-best-practices/ */
5 $('nav.navbar').headroom();
9 $scope: $("main h2, main h3, main h4, main h5, main h6")
12 if ($('#toc').length
) {
15 offset: $("nav.navbar").outerHeight() + 1
20 $('[data-bs-toggle="popover"]').popover({
28 $('[data-bs-toggle="tooltip"]').tooltip();
30 /* Clipboard --------------------------*/
32 function changeTooltipMessage(element
, msg
) {
33 var tooltipOriginalTitle
=element
.getAttribute('data-bs-original-title');
34 element
.setAttribute('data-bs-original-title', msg
);
35 $(element
).tooltip('show');
36 element
.setAttribute('data-bs-original-title', tooltipOriginalTitle
);
39 if(ClipboardJS
.isSupported()) {
40 $(document
).ready(function() {
41 var copyButton
= "<button type='button' class='btn btn-primary btn-copy-ex' title='Copy to clipboard' aria-label='Copy to clipboard' data-toggle='tooltip' data-placement='left' data-trigger='hover' data-clipboard-copy><i class='fa fa-copy'></i></button>";
43 $("div.sourceCode").addClass("hasCopyButton");
45 // Insert copy buttons:
46 $(copyButton
).prependTo(".hasCopyButton");
48 // Initialize tooltips:
49 $('.btn-copy-ex').tooltip({container: 'body'});
51 // Initialize clipboard:
52 var clipboard
= new ClipboardJS('[data-clipboard-copy]', {
53 text: function(trigger
) {
54 return trigger
.parentNode
.textContent
.replace(/\n#>[^\n]*/g, "");
58 clipboard
.on('success', function(e
) {
59 changeTooltipMessage(e
.trigger
, 'Copied!');
63 clipboard
.on('error', function(e
) {
64 changeTooltipMessage(e
.trigger
,'Press Ctrl+C or Command+C to copy');
70 /* Search marking --------------------------*/
71 var url
= new URL(window
.location
.href
);
72 var toMark
= url
.searchParams
.get("q");
73 var mark
= new Mark("main#main");
77 value: "complementary",
78 limiters: [",", ".", ":", "/"],
83 /* Search --------------------------*/
84 /* Adapted from https://github.com/rstudio/bookdown/blob/2d692ba4b61f1e466c92e78fd712b0ab08c11d31/inst/resources/bs4_book/bs4_book.js#L25 */
85 // Initialise search index on focus
87 $("#search-input").focus(async
function(e
) {
92 $(e
.target
).addClass("loading");
93 var response
= await
fetch($("#search-input").data("search-index"));
94 var data
= await response
.json();
97 keys: ["what", "text", "code"],
100 includeMatches: true,
103 fuse
= new Fuse(data
, options
);
105 $(e
.target
).removeClass("loading");
108 // Use algolia autocomplete
116 async
function searchFuse(query
, callback
) {
124 var results
= fuse
.search(query
, { limit: 20 });
126 .filter((x
) => x
.score
<= 0.75)
128 if (items
.length
=== 0) {
129 items
= [{dir:"Sorry 😿",previous_headings:"",title:"No results found.",what:"No results found.",path:window
.location
.href
}];
134 $("#search-input").autocomplete(options
, [
140 if (s
.title
== s
.what
) {
141 return `${s.dir} > <div class="search-details"> ${s.title}</div>`;
142 } else if (s
.previous_headings
== "") {
143 return `${s.dir} > <div class="search-details"> ${s.title}</div> > ${s.what}`;
145 return `${s.dir} > <div class="search-details"> ${s.title}</div> > ${s.previous_headings} > ${s.what}`;
150 ]).on('autocomplete:selected', function(event
, s
) {
151 window
.location
.href
= s
.path
+ "?q=" + q
+ "#" + s
.id
;
154 })(window
.jQuery
|| window
.$)
156 document
.addEventListener('keydown', function(event
) {
157 // Check if the pressed key is '/'
158 if (event
.key
=== '/') {
159 event
.preventDefault(); // Prevent any default action associated with the '/' key
160 document
.getElementById('search-input').focus(); // Set focus to the search input