Skip to content

Commit 485aa42

Browse files
committed
Revert "Revert "Merge pull request #1499 from rtfd/cleanup-doc-embed-js""
This reverts commit 13a8162. Conflicts: readthedocs/core/static-src/core/js/readthedocs-doc-embed.js readthedocs/core/static/core/js/readthedocs-doc-embed.js
1 parent 4416479 commit 485aa42

File tree

14 files changed

+452
-334
lines changed

14 files changed

+452
-334
lines changed

readthedocs/api/base.py

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
from readthedocs.builds.models import Build, Version
1919
from readthedocs.core.utils import trigger_build
2020
from readthedocs.projects.models import Project, ImportedFile
21-
from readthedocs.projects.version_handling import highest_version
22-
from readthedocs.projects.version_handling import parse_version_failsafe
21+
from readthedocs.restapi.views.footer_views import get_version_compare_data
2322

2423
from .utils import SearchMixin, PostAuthentication
2524

@@ -144,34 +143,16 @@ def get_object_list(self, request):
144143
self._meta.queryset = Version.objects.api(user=request.user, only_active=False)
145144
return super(VersionResource, self).get_object_list(request)
146145

147-
def version_compare(self, request, **kwargs):
148-
project = get_object_or_404(Project, slug=kwargs['project_slug'])
149-
highest_version_obj, highest_version_comparable = highest_version(
150-
project.versions.filter(active=True))
151-
base = kwargs.get('base', None)
152-
ret_val = {
153-
'project': highest_version_obj,
154-
'version': highest_version_comparable,
155-
'is_highest': True,
156-
}
157-
if highest_version_obj:
158-
ret_val['url'] = highest_version_obj.get_absolute_url()
159-
ret_val['slug'] = highest_version_obj.slug,
146+
def version_compare(self, request, project_slug, base=None, **kwargs):
147+
project = get_object_or_404(Project, slug=project_slug)
160148
if base and base != LATEST:
161149
try:
162-
base_version_obj = project.versions.get(slug=base)
163-
base_version_comparable = parse_version_failsafe(
164-
base_version_obj.verbose_name)
165-
if base_version_comparable:
166-
# This is only place where is_highest can get set. All
167-
# error cases will be set to True, for non- standard
168-
# versions.
169-
ret_val['is_highest'] = (
170-
base_version_comparable >= highest_version_comparable)
171-
else:
172-
ret_val['is_highest'] = True
150+
base_version = project.versions.get(slug=base)
173151
except (Version.DoesNotExist, TypeError):
174-
ret_val['is_highest'] = True
152+
base_version = None
153+
else:
154+
base_version = None
155+
ret_val = get_version_compare_data(project, base_version)
175156
return self.create_response(request, ret_val)
176157

177158
def build_version(self, request, **kwargs):
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
var rtd = require('./rtd-data');
2+
var versionCompare = require('./version-compare');
3+
4+
5+
function init(build) {
6+
var get_data = {
7+
project: rtd['project'],
8+
version: rtd['version'],
9+
page: rtd['page'],
10+
theme: rtd['theme'],
11+
format: "jsonp",
12+
};
13+
14+
// Crappy heuristic, but people change the theme name on us.
15+
// So we have to do some duck typing.
16+
if ("docroot" in rtd) {
17+
get_data['docroot'] = rtd['docroot'];
18+
}
19+
20+
if ("source_suffix" in rtd) {
21+
get_data['source_suffix'] = rtd['source_suffix'];
22+
}
23+
24+
if (window.location.pathname.indexOf('/projects/') === 0) {
25+
get_data['subproject'] = true;
26+
}
27+
28+
// Get footer HTML from API and inject it into the page.
29+
$.ajax({
30+
url: rtd.api_host + "/api/v2/footer_html/",
31+
crossDomain: true,
32+
xhrFields: {
33+
withCredentials: true,
34+
},
35+
dataType: "jsonp",
36+
data: get_data,
37+
success: function (data) {
38+
versionCompare.init(data.version_compare);
39+
injectFooter(data);
40+
setupBookmarkCSRFToken();
41+
},
42+
error: function () {
43+
console.error('Error loading Read the Docs footer');
44+
}
45+
});
46+
}
47+
48+
49+
function injectFooter(data) {
50+
// If the theme looks like ours, update the existing badge
51+
// otherwise throw a a full one into the page.
52+
if (build.is_rtd_theme()) {
53+
$("div.rst-other-versions").html(data['html']);
54+
} else {
55+
$("body").append(data['html']);
56+
}
57+
58+
if (!data['version_active']) {
59+
$('.rst-current-version').addClass('rst-out-of-date');
60+
} else if (!data['version_supported']) {
61+
//$('.rst-current-version').addClass('rst-active-old-version')
62+
}
63+
64+
// Show promo selectively
65+
if (data.promo && build.show_promo()) {
66+
var promo = new sponsorship.Promo(
67+
data.promo_data.id,
68+
data.promo_data.text,
69+
data.promo_data.link,
70+
data.promo_data.image
71+
)
72+
if (promo) {
73+
promo.display();
74+
}
75+
}
76+
}
77+
78+
79+
function setupBookmarkCSRFToken() {
80+
function csrfSafeMethod(method) {
81+
// these HTTP methods do not require CSRF protection
82+
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
83+
}
84+
85+
$.ajaxSetup({
86+
beforeSend: function(xhr, settings) {
87+
if (!csrfSafeMethod(settings.type)) {
88+
xhr.setRequestHeader("X-CSRFToken", $('a.bookmark[token]').attr('token'));
89+
}
90+
}
91+
});
92+
}
93+
94+
95+
module.exports = {
96+
init: init
97+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function init() {
2+
// Add Grok the Docs Client
3+
$.ajax({
4+
url: "https://api.grokthedocs.com/static/javascript/bundle-client.js",
5+
crossDomain: true,
6+
dataType: "script",
7+
});
8+
}
9+
10+
11+
module.exports = {
12+
init: init
13+
};
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Mkdocs specific JS code.
3+
*/
4+
5+
6+
var rtd = require('./rtd-data');
7+
8+
9+
function init() {
10+
11+
// Override MkDocs styles
12+
if ("builder" in rtd && rtd["builder"] == "mkdocs") {
13+
$('<input>').attr({
14+
type: 'hidden',
15+
name: 'project',
16+
value: rtd["project"]
17+
}).appendTo('#rtd-search-form');
18+
$('<input>').attr({
19+
type: 'hidden',
20+
name: 'version',
21+
value: rtd["version"]
22+
}).appendTo('#rtd-search-form');
23+
$('<input>').attr({
24+
type: 'hidden',
25+
name: 'type',
26+
value: 'file'
27+
}).appendTo('#rtd-search-form');
28+
29+
$("#rtd-search-form").prop("action", rtd.api_host + "/elasticsearch/");
30+
31+
// Apply stickynav to mkdocs builds
32+
var nav_bar = $('nav.wy-nav-side:first'),
33+
win = $(window),
34+
sticky_nav_class = 'stickynav',
35+
apply_stickynav = function () {
36+
if (nav_bar.height() <= win.height()) {
37+
nav_bar.addClass(sticky_nav_class);
38+
} else {
39+
nav_bar.removeClass(sticky_nav_class);
40+
}
41+
};
42+
win.on('resize', apply_stickynav);
43+
apply_stickynav();
44+
}
45+
46+
}
47+
48+
49+
module.exports = {
50+
init: init
51+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* This exposes data injected during the RTD build into the template. It's
3+
* provided via the global READTHEDOCS_DATA variable and is exposed here as a
4+
* module for cleaner usage.
5+
*/
6+
7+
8+
var data = READTHEDOCS_DATA;
9+
10+
11+
if (data.api_host === undefined) {
12+
data.api_host = 'https://readthedocs.org';
13+
}
14+
15+
16+
module.exports = data;
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Sphinx builder specific JS code.
3+
*/
4+
5+
6+
var rtd = require('./rtd-data');
7+
8+
9+
function init() {
10+
11+
/// Read the Docs Sphinx theme code
12+
if (!("builder" in rtd) || "builder" in rtd && rtd["builder"] != "mkdocs") {
13+
function toggleCurrent (elem) {
14+
var parent_li = elem.closest('li');
15+
parent_li.siblings('li.current').removeClass('current');
16+
parent_li.siblings().find('li.current').removeClass('current');
17+
parent_li.find('> ul li.current').removeClass('current');
18+
parent_li.toggleClass('current');
19+
}
20+
21+
// Shift nav in mobile when clicking the menu.
22+
$(document).on('click', "[data-toggle='wy-nav-top']", function() {
23+
$("[data-toggle='wy-nav-shift']").toggleClass("shift");
24+
$("[data-toggle='rst-versions']").toggleClass("shift");
25+
});
26+
// Nav menu link click operations
27+
$(document).on('click', ".wy-menu-vertical .current ul li a", function() {
28+
var target = $(this);
29+
// Close menu when you click a link.
30+
$("[data-toggle='wy-nav-shift']").removeClass("shift");
31+
$("[data-toggle='rst-versions']").toggleClass("shift");
32+
// Handle dynamic display of l3 and l4 nav lists
33+
toggleCurrent(target);
34+
if (typeof(window.SphinxRtdTheme) != 'undefined') {
35+
window.SphinxRtdTheme.StickyNav.hashChange();
36+
}
37+
});
38+
$(document).on('click', "[data-toggle='rst-current-version']", function() {
39+
$("[data-toggle='rst-versions']").toggleClass("shift-up");
40+
});
41+
// Make tables responsive
42+
$("table.docutils:not(.field-list)").wrap("<div class='wy-table-responsive'></div>");
43+
44+
// Add expand links to all parents of nested ul
45+
$('.wy-menu-vertical ul').siblings('a').each(function () {
46+
var link = $(this);
47+
expand = $('<span class="toctree-expand"></span>');
48+
expand.on('click', function (ev) {
49+
toggleCurrent(link);
50+
ev.stopPropagation();
51+
return false;
52+
});
53+
link.prepend(expand);
54+
});
55+
56+
// Sphinx theme state
57+
window.SphinxRtdTheme = (function (jquery) {
58+
var stickyNav = (function () {
59+
var navBar,
60+
win,
61+
winScroll = false,
62+
linkScroll = false,
63+
winPosition = 0,
64+
enable = function () {
65+
init();
66+
reset();
67+
win.on('hashchange', reset);
68+
69+
// Set scrolling
70+
win.on('scroll', function () {
71+
if (!linkScroll) {
72+
winScroll = true;
73+
}
74+
});
75+
setInterval(function () {
76+
if (winScroll) {
77+
winScroll = false;
78+
var newWinPosition = win.scrollTop(),
79+
navPosition = navBar.scrollTop(),
80+
newNavPosition = navPosition + (newWinPosition - winPosition);
81+
navBar.scrollTop(newNavPosition);
82+
winPosition = newWinPosition;
83+
}
84+
}, 25);
85+
},
86+
init = function () {
87+
navBar = jquery('nav.wy-nav-side:first');
88+
win = jquery(window);
89+
},
90+
reset = function () {
91+
// Get anchor from URL and open up nested nav
92+
var anchor = encodeURI(window.location.hash);
93+
if (anchor) {
94+
try {
95+
var link = $('.wy-menu-vertical')
96+
.find('[href="' + anchor + '"]');
97+
$('.wy-menu-vertical li.toctree-l1 li.current')
98+
.removeClass('current');
99+
link.closest('li.toctree-l2').addClass('current');
100+
link.closest('li.toctree-l3').addClass('current');
101+
link.closest('li.toctree-l4').addClass('current');
102+
}
103+
catch (err) {
104+
console.log("Error expanding nav for anchor", err);
105+
}
106+
}
107+
},
108+
hashChange = function () {
109+
linkScroll = true;
110+
win.one('hashchange', function () {
111+
linkScroll = false;
112+
});
113+
};
114+
jquery(init);
115+
return {
116+
enable: enable,
117+
hashChange: hashChange
118+
};
119+
}());
120+
return {
121+
StickyNav: stickyNav
122+
};
123+
}($));
124+
}
125+
126+
}
127+
128+
129+
module.exports = {
130+
init: init
131+
};

0 commit comments

Comments
 (0)