JavaScript clean-up and documenting
This commit is contained in:
parent
8bb0b69e5d
commit
277ee1c6d3
|
@ -29,7 +29,8 @@ var nodemcu = nodemcu || {};
|
|||
}
|
||||
|
||||
/**
|
||||
* Adds a language selector to the RTD fly out menu found bottom left. Example
|
||||
* Adds a language selector to the RTD fly-out menu found bottom left. Example:
|
||||
*
|
||||
* <dl>
|
||||
* <dt>Languages</dt>
|
||||
* <dd><a href="http://nodemcu.readthedocs.org/en/<branch>/de/">de</a></dd>
|
||||
|
@ -38,28 +39,37 @@ var nodemcu = nodemcu || {};
|
|||
* </strong>
|
||||
* </dl>
|
||||
*
|
||||
* UGLY. That menu is added by RTD with an AJAX call after page load. Hence, we need to react to
|
||||
* the subsequent DOM manipulation using a DOM4 MutationObserver.
|
||||
* UGLY! That fly-out menu is added by RTD with an AJAX call after page load. Hence, we need to
|
||||
* react to the subsequent DOM manipulation using a DOM4 MutationObserver. The provided structure
|
||||
* is as follows:
|
||||
*
|
||||
* <div class="rst-other-versions">
|
||||
* <!-- Inserted RTD Footer -->
|
||||
* <div class="injected">
|
||||
*/
|
||||
function addLanguageSelectorToRtdFlyOutMenu() {
|
||||
var observer = new MutationObserver(function (mutations) {
|
||||
// since mutation on the target node was triggered we can safely assume the inject RTD div has now been added
|
||||
var injectedDiv = $('.rst-other-versions .injected');
|
||||
var selectedLanguageCode = determineSelectedLanguageCode();
|
||||
var dl = document.createElement('dl');
|
||||
var dt = document.createElement('dt');
|
||||
dl.appendChild(dt);
|
||||
dt.appendChild(document.createTextNode('Languages'));
|
||||
for (var languageCode in languageCodeToNameMap) {
|
||||
dl.appendChild(createLanguageLinkFor(languageCode, selectedLanguageCode === languageCode));
|
||||
}
|
||||
injectedDiv.prepend(dl);
|
||||
// no need for that observer anymore
|
||||
observer.disconnect();
|
||||
});
|
||||
var flyOutWrapper = $('.rst-other-versions');
|
||||
// only relevant on RTD
|
||||
if (flyOutWrapper.size() > 0) {
|
||||
var observer = new MutationObserver(function (mutations) {
|
||||
// since mutation on the target node was triggered we can safely assume the injected RTD div has now been added
|
||||
var injectedDiv = $('.rst-other-versions .injected');
|
||||
var selectedLanguageCode = determineSelectedLanguageCode();
|
||||
var dl = document.createElement('dl');
|
||||
var dt = document.createElement('dt');
|
||||
dl.appendChild(dt);
|
||||
dt.appendChild(document.createTextNode('Languages'));
|
||||
for (var languageCode in languageCodeToNameMap) {
|
||||
dl.appendChild(createLanguageLinkFor(languageCode, selectedLanguageCode === languageCode));
|
||||
}
|
||||
injectedDiv.prepend(dl);
|
||||
// no need for that observer anymore
|
||||
observer.disconnect();
|
||||
});
|
||||
|
||||
// observed target node is the fly-out wrapper, the only event we care about is when children are modified
|
||||
observer.observe($('.rst-other-versions')[0], {childList: true});
|
||||
// observed target node is the fly-out wrapper, the only event we care about is when children are modified
|
||||
observer.observe(flyOutWrapper[0], {childList: true});
|
||||
}
|
||||
}
|
||||
function createLanguageLinkFor(languageCode, isCurrentlySelected) {
|
||||
var strong;
|
||||
|
@ -79,6 +89,14 @@ var nodemcu = nodemcu || {};
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Analyzes the URL of the current page to find out what the selected language is. It's usually
|
||||
* part of the location path. The code needs to distinguish between running MkDocs standalone
|
||||
* and docs served from RTD. If no valid language could be determined the default language is
|
||||
* returned.
|
||||
*
|
||||
* @returns 2-char language code
|
||||
*/
|
||||
function determineSelectedLanguageCode() {
|
||||
var selectedLanguageCode, path = window.location.pathname;
|
||||
if (window.location.origin.indexOf('readthedocs') > -1) {
|
||||
|
|
Loading…
Reference in New Issue