|
| 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><${term}></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 | +}); |
0 commit comments