Skip to content

Commit f8c2700

Browse files
committed
Add JS to view glossary
1 parent 1aeebb3 commit f8c2700

File tree

6 files changed

+150
-0
lines changed

6 files changed

+150
-0
lines changed

assets/sass/reference.scss

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

60+
.tooltip {
61+
background: white;
62+
position: absolute;
63+
border: 1px solid #ccc;
64+
border-radius: 4px;
65+
padding: 10px;
66+
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
67+
max-width: 300px;
68+
font-size: 14px;
69+
line-height: 1.4;
70+
visibility: hidden;
71+
}
72+
73+
.tooltip.show {
74+
visibility: visible;
75+
}
76+
77+
.expand-icon {
78+
position: absolute;
79+
top: 8px;
80+
right: 8px;
81+
text-decoration: none;
82+
83+
img {
84+
width: 16px;
85+
height: 16px;
86+
opacity: 0.6;
87+
}
88+
89+
&:hover img {
90+
opacity: 1;
91+
}
92+
}
93+
94+
.hover-term {
95+
cursor: help;
96+
text-decoration: underline dotted;
97+
}
98+
6099
// § section sign anchor links
61100
#content {
62101
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">

script/update-docs.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ def extract_glossary_from_html(content)
116116
term_names = []
117117
if term_name == "tree-ish (also treeish)"
118118
term_names = ['tree-ish', 'treeish']
119+
elsif term_name == "arbre-esque (aussi arbresque)"
120+
term_names = [''arbre-esque, 'arbresque']
119121
else
120122
term_names = [term_name]
121123
end

static/images/external-link.svg

Lines changed: 7 additions & 0 deletions
Loading

static/js/glossary.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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>'
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: 8,
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+
this.tooltip.innerHTML = `
59+
<a href="#" class="expand-icon">
60+
<img src="${baseURLPrefix}images/external-link.svg" alt="expand" />
61+
</a>
62+
<strong>&lt;${term}&gt;</strong><br><br>
63+
${truncatedDefinition}
64+
`;
65+
this.show();
66+
}
67+
});
68+
69+
content.addEventListener('mouseout', (e) => {
70+
if (e.target.classList.contains('hover-term')) {
71+
this.hide();
72+
}
73+
});
74+
75+
// Keep popover open when hovering over it
76+
this.tooltip.addEventListener('mouseenter', () => {
77+
this.show();
78+
});
79+
80+
this.tooltip.addEventListener('mouseleave', () => {
81+
this.hide();
82+
});
83+
},
84+
85+
truncateWords: function(text, maxWords) {
86+
const words = text.split(/\s+/);
87+
if (words.length <= maxWords) {
88+
return text;
89+
}
90+
return words.slice(0, maxWords).join(' ') + '...';
91+
},
92+
};
93+
94+
// Initialize when document is ready
95+
$(document).ready(() => {
96+
GitGlossary.init();
97+
});

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)