Skip to content

Embedded js: remove more dependency on jquery #9515

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 7 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions readthedocs/core/static-src/core/js/doc-embed/footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,23 @@ function injectFooter(data) {
// * All other pages just get it appended to the <body>

var config = rtddata.get();
var placement = $(EXPLICIT_FLYOUT_PLACEMENT_SELECTOR);
if (placement.length > 0) {
placement.html(data['html']);
let placement = document.querySelector(EXPLICIT_FLYOUT_PLACEMENT_SELECTOR);
if (placement !== null) {
placement.innerHTML = data['html'];
}
else if (config.is_sphinx_builder() && config.is_rtd_like_theme()) {
$("div.rst-other-versions").html(data['html']);
let placement = document.querySelector('div.rst-other-versions');
if (placement !== null) {
placement.innerHTML = data['html'];
}
} else {
$("body").append(data['html']);
document.body.insertAdjacentHTML('beforeend', data['html']);
}

if (!data['version_active']) {
$('.rst-current-version').addClass('rst-out-of-date');
for (let element of document.getElementsByClassName('rst-current-version')) {
element.classList.add('rst-out-of-date');
}
} else if (!data['version_supported']) {
//$('.rst-current-version').addClass('rst-active-old-version')
}
Expand Down
26 changes: 5 additions & 21 deletions readthedocs/core/static-src/core/js/doc-embed/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,11 @@
* Sphinx and Mkdocs search overrides
*/

var rtddata = require('./rtd-data');
var xss = require('xss/lib/index');
var MAX_RESULT_PER_SECTION = 3;
var MAX_SUBSTRING_LIMIT = 100;

/**
* Create and return DOM nodes with given attributes.
*
* @param {String} nodeName name of the node
* @param {Object} attributes obj of attributes to be assigned to the node
* @return {Object} DOM node
*/
const createDomNode = (nodeName, attributes) => {
let node = document.createElement(nodeName);
if (attributes) {
for (let attr of Object.keys(attributes)) {
node.setAttribute(attr, attributes[attr]);
}
}
return node;
};
const rtddata = require('./rtd-data');
const xss = require('xss/lib/index');
const { createDomNode } = require("./utils");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to specify that we're using this function from utils, or is the compiler smart enough to know what is happening if we add additional utils functions?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The compile step will resolve the functions in utils.js for use here, yeah. This syntax will only import createDomNode from utils.js. If more functions are added to utils.js, they won't automatically be imported here, but could be with const { createDomNode, someOtherUtil } = ..

const MAX_RESULT_PER_SECTION = 3;
const MAX_SUBSTRING_LIMIT = 100;


/*
Expand Down
18 changes: 18 additions & 0 deletions readthedocs/core/static-src/core/js/doc-embed/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Create and return DOM nodes with given attributes.
*
* @param {String} nodeName name of the node
* @param {Object} attributes obj of attributes to be assigned to the node
* @return {Object} DOM node
*/
const createDomNode = (nodeName, attributes) => {
let node = document.createElement(nodeName);
if (attributes) {
for (let attr of Object.keys(attributes)) {
node.setAttribute(attr, attributes[attr]);
}
}
return node;
};

module.exports = { createDomNode };
37 changes: 17 additions & 20 deletions readthedocs/core/static-src/core/js/doc-embed/version-compare.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
var rtddata = require('./rtd-data');
const rtddata = require('./rtd-data');
const { createDomNode } = require('./utils');


function init(data) {
var rtd = rtddata.get();
let rtd = rtddata.get();

/// Out of date message

if (data.is_highest) {
return;
}

var currentURL = window.location.pathname.replace(rtd['version'], data.slug);
var warning = $(
'<div class="admonition warning"> ' +
'<p class="first admonition-title">Note</p> ' +
let currentURL = window.location.pathname.replace(rtd['version'], data.slug);
let warning = createDomNode('div', {class: 'admonition warning'});
let link = createDomNode('a', {href: currentURL});
link.innerText = data.slug;
warning.innerHTML = '<p class="first admonition-title">Note</p> ' +
'<p class="last"> ' +
'You are not reading the most recent version of this documentation. ' +
'<a href="#"></a> is the latest version available.' +
'</p>' +
'</div>');

warning
.find('a')
.attr('href', currentURL)
.text(data.slug);

var selectors = ['[role=main]', 'main', 'div.body', 'div.document'];
for (var i = 0; i < selectors.length; i += 1) {
var body = $(selectors[i]);
if (body.length) {
body.prepend(warning);
link.outerHTML +
' is the latest version available.' +
'</p>';

let selectors = ['[role=main]', 'main', 'div.body', 'div.document'];
for (let selector of selectors) {
let element = document.querySelector(selector);
if (element !== null) {
element.prepend(warning);
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion readthedocs/core/static/core/js/readthedocs-doc-embed.js

Large diffs are not rendered by default.