forked from readthedocs/readthedocs.org
/
footer.js
88 lines (72 loc) · 2.25 KB
/
footer.js
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
var rtddata = require('./rtd-data'),
versionCompare = require('./version-compare');
function init() {
var rtd = rtddata.get();
var get_data = {
project: rtd['project'],
version: rtd['version'],
page: rtd['page'],
theme: rtd.get_theme_name(),
format: "jsonp",
};
// Crappy heuristic, but people change the theme name on us.
// So we have to do some duck typing.
if ("docroot" in rtd) {
get_data['docroot'] = rtd['docroot'];
}
if ("source_suffix" in rtd) {
get_data['source_suffix'] = rtd['source_suffix'];
}
if (window.location.pathname.indexOf('/projects/') === 0) {
get_data['subproject'] = true;
}
// Get footer HTML from API and inject it into the page.
$.ajax({
url: rtd.api_host + "/api/v2/footer_html/",
crossDomain: true,
xhrFields: {
withCredentials: true,
},
dataType: "jsonp",
data: get_data,
success: function (data) {
versionCompare.init(data.version_compare);
injectFooter(data);
setupBookmarkCSRFToken();
},
error: function () {
console.error('Error loading Read the Docs footer');
}
});
}
function injectFooter(data) {
var config = rtddata.get();
// If the theme looks like ours, update the existing badge
// otherwise throw a a full one into the page.
if (config.is_rtd_theme()) {
$("div.rst-other-versions").html(data['html']);
} else {
$("body").append(data['html']);
}
if (!data['version_active']) {
$('.rst-current-version').addClass('rst-out-of-date');
} else if (!data['version_supported']) {
//$('.rst-current-version').addClass('rst-active-old-version')
}
}
function setupBookmarkCSRFToken() {
function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type)) {
xhr.setRequestHeader("X-CSRFToken", $('a.bookmark[token]').attr('token'));
}
}
});
}
module.exports = {
init: init
};