Skip to content

Commit 18dec05

Browse files
committed
Add JS to view glossary
1 parent da73dde commit 18dec05

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed

assets/sass/reference.scss

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,33 @@ h3.plumbing {
5757
@include background-image-2x($baseurl + "images/icons/plumbing-sm", 17px, 14px, 2px 50%);
5858
}
5959

60+
.tooltip {
61+
position: absolute;
62+
/* invisible padding to make it easier to enter with the mouse */
63+
padding: 15px;
64+
visibility: hidden;
65+
66+
.tooltip-content {
67+
background: white;
68+
border: 1px solid #ccc;
69+
border-radius: 4px;
70+
padding: 10px;
71+
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
72+
max-width: 300px;
73+
font-size: 14px;
74+
line-height: 1.4;
75+
}
76+
}
77+
78+
.tooltip.show {
79+
visibility: visible;
80+
}
81+
82+
.hover-term {
83+
cursor: help;
84+
text-decoration: underline dotted;
85+
}
86+
6087
// § section sign anchor links
6188
#content {
6289
h1>a.anchor,

layouts/_default/baseof.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ <h3 hidden="true" data-pagefind-weight="{{ $weight }}">{{ $command_name }}</h3>
147147
</div>
148148
</div>
149149
{{ partialCached "footer.html" . }}
150+
<script src="{{ relURL "js/nanopop.umd.js" }}"></script>
151+
<script src="{{ relURL "js/glossary.js" }}"></script>
150152
</div>
151153
{{ else }}
152154
<div class="inner">

static/js/glossary.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
var GitGlossary = {
2+
data: null,
3+
term: null,
4+
tooltip: null,
5+
6+
init: function() {
7+
const language = document.querySelector("html")?.getAttribute("lang") || 'en';
8+
$.getJSON(baseURLPrefix + 'js/glossary/' + language + '.json')
9+
.done((data) => this.onDataLoaded(data));
10+
window.addEventListener('resize', () => this.reposition())
11+
},
12+
13+
onDataLoaded: function(data) {
14+
this.data = data;
15+
const content = document.querySelector('#content');
16+
17+
// Create the popover element
18+
document.body.insertAdjacentHTML('beforeend',
19+
'<div class="tooltip"><div class="tooltip-content"></div></div>'
20+
);
21+
this.tooltip = document.body.lastElementChild;
22+
this.attachHoverEvents(content);
23+
},
24+
25+
show: function() {
26+
this.tooltip.classList.add('show');
27+
this.reposition();
28+
},
29+
30+
hide: function() {
31+
this.tooltip.classList.remove('show');
32+
},
33+
34+
reposition: function() {
35+
const result = NanoPop.reposition(this.term, this.tooltip, {
36+
position: 'bottom',
37+
margin: -7,
38+
container: {
39+
top: 0,
40+
left: 0,
41+
bottom: window.innerHeight,
42+
right: window.innerWidth
43+
}
44+
});
45+
},
46+
47+
attachHoverEvents: function(content) {
48+
let timeout = undefined;
49+
50+
content.addEventListener('mouseover', (e) => {
51+
if (e.target.classList.contains('hover-term')) {
52+
console.log(this.term);
53+
this.term = e.target;
54+
const term = e.target.dataset.term;
55+
const definition = this.data[term] || '';
56+
const truncatedDefinition = this.truncateWords(definition, 60);
57+
58+
const language = document.querySelector("html")?.getAttribute("lang") || 'en';
59+
const glossaryUrl = language === 'en' ? '/docs/gitglossary' : `/docs/gitglossary/${language}`;
60+
61+
this.tooltip.querySelector('.tooltip-content').innerHTML = `
62+
<a href="${glossaryUrl}#def_${term}" target="_blank">
63+
<strong>&lt;${term}&gt;</strong>
64+
</a>
65+
<br><br>
66+
${truncatedDefinition}
67+
`;
68+
this.show();
69+
}
70+
});
71+
72+
content.addEventListener('mouseout', (e) => {
73+
if (e.target.classList.contains('hover-term')) {
74+
this.hide();
75+
}
76+
});
77+
78+
// Keep popover open when hovering over it
79+
this.tooltip.addEventListener('mouseenter', () => {
80+
this.show();
81+
});
82+
83+
this.tooltip.addEventListener('mouseleave', () => {
84+
this.hide();
85+
});
86+
},
87+
88+
truncateWords: function(text, maxWords) {
89+
const words = text.split(/\s+/);
90+
if (words.length <= maxWords) {
91+
return text;
92+
}
93+
return words.slice(0, maxWords).join(' ') + '...';
94+
},
95+
};
96+
97+
// Initialize when document is ready
98+
$(document).ready(() => {
99+
GitGlossary.init();
100+
});

static/js/nanopop.umd.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)