-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Cleanup doc embed js #1507
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
Cleanup doc embed js #1507
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
485aa42
Revert "Revert "Merge pull request #1499 from rtfd/cleanup-doc-embed-…
ericholscher a675748
Add click tracking as diff commit to this branch
ericholscher b11b242
Fix fox
ericholscher 8580b54
Fix other test imports
ericholscher 48224cc
Prefix imports with readthedocs
gregmuellegger 0d8d794
Fix READTHEDOCS_DATA loading in embed js
gregmuellegger e907314
Move instantiation of Build class into 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
File renamed without changes.
102 changes: 102 additions & 0 deletions
102
readthedocs/core/static-src/core/js/doc-embed/footer.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,102 @@ | ||
var Build = require('./build').Build; | ||
var rtddata = require('./rtd-data'); | ||
var versionCompare = require('./version-compare'); | ||
|
||
|
||
function init() { | ||
var rtd = rtddata.get(); | ||
|
||
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) { | ||
var build = new Build(rtddata.get()); | ||
|
||
// 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,52 @@ | ||
/* | ||
* Mkdocs specific JS code. | ||
*/ | ||
|
||
|
||
var rtddata = require('./rtd-data'); | ||
|
||
|
||
function init() { | ||
var rtd = rtddata.get(); | ||
|
||
// 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,21 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
|
||
/* | ||
* Access READTHEDOCS_DATA on call, not on module load. The reason is that the | ||
* READTHEDOCS_DATA might not be available during script load time. | ||
*/ | ||
function get() { | ||
return $.extend({ | ||
api_host: 'https://readthedocs.org' | ||
}, window.READTHEDOCS_DATA); | ||
} | ||
|
||
|
||
module.exports = { | ||
get: get | ||
}; |
143 changes: 143 additions & 0 deletions
143
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,143 @@ | ||
/* | ||
* Sphinx builder specific JS code. | ||
*/ | ||
|
||
|
||
var rtddata = require('./rtd-data'); | ||
|
||
|
||
function init() { | ||
var rtd = rtddata.get(); | ||
|
||
/// Click tracking on flyout | ||
$(document).on('click', "[data-toggle='rst-current-version']", function() { | ||
var flyout_state = $("[data-toggle='rst-versions']").hasClass('shift-up') ? 'was_open' : 'was_closed' | ||
if (_gaq) { | ||
_gaq.push( | ||
['rtfd._setAccount', 'UA-17997319-1'], | ||
['rtfd._trackEvent', 'Flyout', 'Click', flyout_state] | ||
); | ||
} | ||
}); | ||
|
||
/// 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.
build
doesn't exist here.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.
Argh :( that's addressed now.