-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Integrate version compare API info into footer_html endpoint #1499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
97b9bfd
Separating grokthedocs JS code into its own file
gregmuellegger b8df150
Encapsulate global READTHEDOCS_DATA into js module
gregmuellegger ed71caf
Separating footer embed JS code into its own file
gregmuellegger d169dcb
Separating mkdocs specific embed code into its own file
gregmuellegger 9f378e4
Separating out-of-date message code into its own file
gregmuellegger 4b5dbc6
Separating sphinx JS specific code into its own file
gregmuellegger aa8590d
Provide version compare info in footer API endpoint
gregmuellegger 7b762a0
Use version compare info from footer in embeded JS instead of making …
gregmuellegger 88a4ddb
Refactoring embed footer js
gregmuellegger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
var rtd = require('./rtd-data'); | ||
var versionCompare = require('./version-compare'); | ||
|
||
|
||
function init(build) { | ||
var get_data = { | ||
project: rtd['project'], | ||
version: rtd['version'], | ||
page: rtd['page'], | ||
theme: rtd['theme'], | ||
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) { | ||
// If the theme looks like ours, update the existing badge | ||
// otherwise throw a a full one into the page. | ||
if (build.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') | ||
} | ||
|
||
// Show promo selectively | ||
if (data.promo && build.show_promo()) { | ||
var promo = new sponsorship.Promo( | ||
data.promo_data.id, | ||
data.promo_data.text, | ||
data.promo_data.link, | ||
data.promo_data.image | ||
) | ||
if (promo) { | ||
promo.display(); | ||
} | ||
} | ||
} | ||
|
||
|
||
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 | ||
}; |
13 changes: 13 additions & 0 deletions
13
readthedocs/core/static-src/core/js/doc-embed/grokthedocs-client.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
function init() { | ||
// Add Grok the Docs Client | ||
$.ajax({ | ||
url: "https://api.grokthedocs.com/static/javascript/bundle-client.js", | ||
crossDomain: true, | ||
dataType: "script", | ||
}); | ||
} | ||
|
||
|
||
module.exports = { | ||
init: init | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Mkdocs specific JS code. | ||
*/ | ||
|
||
|
||
var rtd = require('./rtd-data'); | ||
|
||
|
||
function init() { | ||
|
||
// Override MkDocs styles | ||
if ("builder" in rtd && rtd["builder"] == "mkdocs") { | ||
$('<input>').attr({ | ||
type: 'hidden', | ||
name: 'project', | ||
value: rtd["project"] | ||
}).appendTo('#rtd-search-form'); | ||
$('<input>').attr({ | ||
type: 'hidden', | ||
name: 'version', | ||
value: rtd["version"] | ||
}).appendTo('#rtd-search-form'); | ||
$('<input>').attr({ | ||
type: 'hidden', | ||
name: 'type', | ||
value: 'file' | ||
}).appendTo('#rtd-search-form'); | ||
|
||
$("#rtd-search-form").prop("action", rtd.api_host + "/elasticsearch/"); | ||
|
||
// Apply stickynav to mkdocs builds | ||
var nav_bar = $('nav.wy-nav-side:first'), | ||
win = $(window), | ||
sticky_nav_class = 'stickynav', | ||
apply_stickynav = function () { | ||
if (nav_bar.height() <= win.height()) { | ||
nav_bar.addClass(sticky_nav_class); | ||
} else { | ||
nav_bar.removeClass(sticky_nav_class); | ||
} | ||
}; | ||
win.on('resize', apply_stickynav); | ||
apply_stickynav(); | ||
} | ||
|
||
} | ||
|
||
|
||
module.exports = { | ||
init: init | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* This exposes data injected during the RTD build into the template. It's | ||
* provided via the global READTHEDOCS_DATA variable and is exposed here as a | ||
* module for cleaner usage. | ||
*/ | ||
|
||
|
||
var data = READTHEDOCS_DATA; | ||
|
||
|
||
if (data.api_host === undefined) { | ||
data.api_host = 'https://readthedocs.org'; | ||
} | ||
|
||
|
||
module.exports = data; |
131 changes: 131 additions & 0 deletions
131
readthedocs/core/static-src/core/js/doc-embed/sphinx.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* Sphinx builder specific JS code. | ||
*/ | ||
|
||
|
||
var rtd = require('./rtd-data'); | ||
|
||
|
||
function init() { | ||
|
||
/// Read the Docs Sphinx theme code | ||
if (!("builder" in rtd) || "builder" in rtd && rtd["builder"] != "mkdocs") { | ||
function toggleCurrent (elem) { | ||
var parent_li = elem.closest('li'); | ||
parent_li.siblings('li.current').removeClass('current'); | ||
parent_li.siblings().find('li.current').removeClass('current'); | ||
parent_li.find('> ul li.current').removeClass('current'); | ||
parent_li.toggleClass('current'); | ||
} | ||
|
||
// Shift nav in mobile when clicking the menu. | ||
$(document).on('click', "[data-toggle='wy-nav-top']", function() { | ||
$("[data-toggle='wy-nav-shift']").toggleClass("shift"); | ||
$("[data-toggle='rst-versions']").toggleClass("shift"); | ||
}); | ||
// Nav menu link click operations | ||
$(document).on('click', ".wy-menu-vertical .current ul li a", function() { | ||
var target = $(this); | ||
// Close menu when you click a link. | ||
$("[data-toggle='wy-nav-shift']").removeClass("shift"); | ||
$("[data-toggle='rst-versions']").toggleClass("shift"); | ||
// Handle dynamic display of l3 and l4 nav lists | ||
toggleCurrent(target); | ||
if (typeof(window.SphinxRtdTheme) != 'undefined') { | ||
window.SphinxRtdTheme.StickyNav.hashChange(); | ||
} | ||
}); | ||
$(document).on('click', "[data-toggle='rst-current-version']", function() { | ||
$("[data-toggle='rst-versions']").toggleClass("shift-up"); | ||
}); | ||
// Make tables responsive | ||
$("table.docutils:not(.field-list)").wrap("<div class='wy-table-responsive'></div>"); | ||
|
||
// Add expand links to all parents of nested ul | ||
$('.wy-menu-vertical ul').siblings('a').each(function () { | ||
var link = $(this); | ||
expand = $('<span class="toctree-expand"></span>'); | ||
expand.on('click', function (ev) { | ||
toggleCurrent(link); | ||
ev.stopPropagation(); | ||
return false; | ||
}); | ||
link.prepend(expand); | ||
}); | ||
|
||
// Sphinx theme state | ||
window.SphinxRtdTheme = (function (jquery) { | ||
var stickyNav = (function () { | ||
var navBar, | ||
win, | ||
winScroll = false, | ||
linkScroll = false, | ||
winPosition = 0, | ||
enable = function () { | ||
init(); | ||
reset(); | ||
win.on('hashchange', reset); | ||
|
||
// Set scrolling | ||
win.on('scroll', function () { | ||
if (!linkScroll) { | ||
winScroll = true; | ||
} | ||
}); | ||
setInterval(function () { | ||
if (winScroll) { | ||
winScroll = false; | ||
var newWinPosition = win.scrollTop(), | ||
navPosition = navBar.scrollTop(), | ||
newNavPosition = navPosition + (newWinPosition - winPosition); | ||
navBar.scrollTop(newNavPosition); | ||
winPosition = newWinPosition; | ||
} | ||
}, 25); | ||
}, | ||
init = function () { | ||
navBar = jquery('nav.wy-nav-side:first'); | ||
win = jquery(window); | ||
}, | ||
reset = function () { | ||
// Get anchor from URL and open up nested nav | ||
var anchor = encodeURI(window.location.hash); | ||
if (anchor) { | ||
try { | ||
var link = $('.wy-menu-vertical') | ||
.find('[href="' + anchor + '"]'); | ||
$('.wy-menu-vertical li.toctree-l1 li.current') | ||
.removeClass('current'); | ||
link.closest('li.toctree-l2').addClass('current'); | ||
link.closest('li.toctree-l3').addClass('current'); | ||
link.closest('li.toctree-l4').addClass('current'); | ||
} | ||
catch (err) { | ||
console.log("Error expanding nav for anchor", err); | ||
} | ||
} | ||
}, | ||
hashChange = function () { | ||
linkScroll = true; | ||
win.one('hashchange', function () { | ||
linkScroll = false; | ||
}); | ||
}; | ||
jquery(init); | ||
return { | ||
enable: enable, | ||
hashChange: hashChange | ||
}; | ||
}()); | ||
return { | ||
StickyNav: stickyNav | ||
}; | ||
}($)); | ||
} | ||
|
||
} | ||
|
||
|
||
module.exports = { | ||
init: init | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can probably kill this for now -- we have killed the Bookmarking feature (it was half done, and probably not the most reasonable from a product perspective). However, this code does do some interesting things that we'll likely need to do when we actually look seriously at doing CNAME-supported javascript clients.