-
Notifications
You must be signed in to change notification settings - Fork 40
/
hoverxref.js_t
217 lines (191 loc) · 8.48 KB
/
hoverxref.js_t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
var mathjax = {{ 'true' if hoverxref_mathjax else 'false' }};
var sphinxtabs = {{ 'true' if hoverxref_sphinxtabs else 'false' }};
{% set sphinx_rtd_theme = 'true' if html_theme == 'sphinx_rtd_theme' else 'false' %}
function reRenderTooltip (instance, helper) {
// Check if the content is the same as the default content until
// it's different. Once it's different, re renders its content
// to show it properly (this may involve calling external JS
// libraries like MathJax) and finally call tooltip.reposition().
if (helper.tooltip.textContent !== '{{ hoverxref_tooltip_content }}') {
// https://stackoverflow.com/questions/5200545/how-to-recall-or-restart-mathjax
if (mathjax) {
if (typeof MathJax !== 'undefined') {
reLoadMathJax(helper.tooltip.id);
} else {
console.debug('Not triggering MathJax because it is not defined');
};
};
instance.reposition();
} else {
setTimeout(reRenderTooltip, 100, instance, helper);
};
}
function reLoadMathJax(elementId) {
console.debug('Triggering MathJax.Hub.Typeset()');
MathJax.Hub.Queue((["Typeset", MathJax.Hub, elementId]));
}
function reLoadSphinxTabs() {
if (sphinxtabs) {
// https://github.com/djungelorm/sphinx-tabs
console.debug('Triggering Sphinx Tabs rendering');
(function(d, script) {
// HACK: please, improve this code to call the content of "tab.js" without creating a script element
// Get the URL from the current generated page since it's not always the same
var src = $('script[src$="sphinx_tabs/tabs.js"]')[0].src;
script = d.createElement('script');
script.type = 'text/javascript';
script.onload = function(){
// remote script has loaded
};
script.src = src;
d.getElementsByTagName('head')[0].appendChild(script);
// Once the script has been executed, we remove it from the DOM
script.parentNode.removeChild(script);
}(document));
};
};
function getEmbedURL(project, version, doc, docpath, section) {
var params = {
'project': project,
'version': version,
'doc': doc,
'path': docpath,
'section': section,
}
console.debug('Data: ' + JSON.stringify(params));
var url = '{{ hoverxref_api_host }}' + '/api/v2/embed/?' + $.param(params);
console.debug('URL: ' + url);
return url
}
$(document).ready(function() {
$('.hoverxref.tooltip').tooltipster({
theme: {{ hoverxref_tooltip_theme }},
interactive: {{ 'true' if hoverxref_tooltip_interactive else 'false' }},
maxWidth: {{ hoverxref_tooltip_maxwidth }},
animation: '{{ hoverxref_tooltip_animation }}',
animationDuration: {{ hoverxref_tooltip_animation_duration }},
side: '{{ hoverxref_tooltip_side }}',
content: '{{ hoverxref_tooltip_content }}',
functionBefore: function(instance, helper) {
var $origin = $(helper.origin);
var project = $origin.data('project');
var version = $origin.data('version');
var doc = $origin.data('doc');
var docpath = $origin.data('docpath');
var section = $origin.data('section');
// we set a variable so the data is only loaded once via Ajax, not every time the tooltip opens
if ($origin.data('loaded') !== true) {
var url = getEmbedURL(project, version, doc, docpath, section);
$.get(url, function(data) {
// call the 'content' method to update the content of our tooltip with the returned data.
// note: this content update will trigger an update animation (see the updateAnimation option)
instance.content(data['content']);
// to remember that the data has been loaded
$origin.data('loaded', true);
});
}
},
functionReady: function(instance, helper) {
// most of Read the Docs Sphinx theme bases its style on "rst-content".
// We add that class to the tooltipser HTML tag here by default or a user-defined one.
helper.tooltip.classList.add('{{ hoverxref_tooltip_class }}');
reLoadSphinxTabs();
setTimeout(
reRenderTooltip,
50,
instance,
helper
);
}
})
var modalHtml = `
<div class="modal micromodal-slide {{ hoverxref_modal_class }}" id="micromodal" aria-hidden="true">
<div class="modal__overlay" tabindex="-1" data-micromodal-close>
<div class="modal__container" role="dialog" aria-modal="true" aria-labelledby="micromodal-title">
<header class="modal__header">
<h1 class="modal__title" id="micromodal-title"></h1>
<button class="modal__close" aria-label="Close modal" data-micromodal-close></button>
</header>
<hr/>
<main class="modal__content" id="micromodal-content"></main>
<footer class="modal__footer">
<button class="modal__btn" data-micromodal-close aria-label="Close this dialog window">Close</button>
</footer>
</div>
</div>
</div>
`
$('body').append(modalHtml);
{% if sphinx_rtd_theme %}
function onShow(modal, element) {
// This is a HACK to get some "smart" left position of the
// modal depending its size.
var container = $('#micromodal .modal__container')
var maxWidth = $('.wy-nav-content').innerWidth() - 150;
var contentLeft = $('.wy-nav-content').position().left;
if (container.width() >= maxWidth) {
var left = contentLeft - 150;
}
else {
var left = contentLeft + 150;
}
console.debug('Container left position: ' + left);
container.css('left', left);
}
{% endif %}
function showModal(element) {
var project = element.data('project');
var version = element.data('version');
var doc = element.data('doc');
var docpath = element.data('docpath');
var section = element.data('section');
var url = getEmbedURL(project, version, doc, docpath, section);
$.get(url, function(data) {
var content = $('<div></div>');
content.html(data['content'][0]);
var h1 = $('h1:first', content);
var title = h1.text()
if (title) {
var link = $('a', h1).attr('href') || '#';
var a = $('<a></a>').attr('href', link).text('{{ hoverxref_modal_prefix_title }}' + title.replace('¶', ''));
}
else {
var a = '{{ hoverxref_modal_prefix_title }}{{ hoverxref_modal_default_title }}';
}
h1.replaceWith('');
$('#micromodal-title').html(a);
$('#micromodal-content').html(content);
MicroModal.show('micromodal', {
{% if sphinx_rtd_theme or hoverxref_modal_onshow_function %}
onShow: {{ hoverxref_modal_onshow_function|default('onShow', true) }},
{% endif %}
openClass: '{{ hoverxref_modal_openclass }}',
disableScroll: {{ 'true' if hoverxref_modal_disable_scroll else 'false' }},
disableFocus: {{ 'true' if hoverxref_modal_disable_focus else 'false' }},
awaitOpenAnimation: {{ 'true' if hoverxref_modal_awaitopenanimation else 'false' }},
awaitCloseAnimation: {{ 'true' if hoverxref_modal_awaitcloseanimation else 'false' }},
debugMode: {{ 'true' if hoverxref_modal_debugmode else 'false' }}
});
$('#micromodal .modal__container').scrollTop(0);
reLoadSphinxTabs();
if (mathjax) {
if (typeof MathJax !== 'undefined') {
reLoadMathJax('micromodal');
} else {
console.debug('Not triggering MathJax because it is not defined');
};
};
});
};
var delay = {{ hoverxref_modal_hover_delay }}, setTimeoutConst;
$('.hoverxref.modal').hover(function(event) {
var element = $(this);
console.debug('Event: ' + event + ' Element: ' + element);
event.preventDefault();
setTimeoutConst = setTimeout(function(){
showModal(element);
}, delay);
}, function(){
clearTimeout(setTimeoutConst);
});
});