Skip to content
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
58 changes: 51 additions & 7 deletions src/ethicalads.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ export class EthicalAdsAddon extends AddonBase {

createAdPlacement() {
let placement;
let selectors;

const placementIdSuffix = docTool.getDocumentationTool() || "nodoctool";

// TODO: fix this on testing. It works fine on production/regular browser.
// TypeError: Failed to execute 'indexed value' on 'ObservableArray<CSSStyleSheet>': Failed to convert value to 'CSSStyleSheet'.
Expand All @@ -67,7 +70,6 @@ export class EthicalAdsAddon extends AddonBase {
} else {
// Inject our own floating element
placement = document.createElement("div");
placement.setAttribute("id", "readthedocs-ea");
placement.classList.add("raised");

// Define where to inject the Ad based on the theme and if it's above the fold or not.
Expand All @@ -84,6 +86,12 @@ export class EthicalAdsAddon extends AddonBase {
placement.classList.add("ethical-rtd");
placement.classList.add("ethical-dark-theme");
knownPlacementFound = true;
} else {
// We know it's RTD theme and the ad in the navbar is not above the fold at this point.
// Then, we render the ad as fixed footer.
selectors = ["section", "nav"];
this.setFixedFooterAdProperties(selectors, placement);
knownPlacementFound = true;
}
} else if (docTool.isSphinxFuroLikeTheme()) {
// NOTE: The code to handle furo theme shouldn't be required,
Expand All @@ -97,7 +105,6 @@ export class EthicalAdsAddon extends AddonBase {
if (this.elementAboveTheFold(element)) {
placement.classList.add("ethical-alabaster");
placement.setAttribute("data-ea-type", "readthedocs-sidebar");
placement.setAttribute("id", "furo-sidebar-ad-placement");
knownPlacementFound = true;
}
} else if (docTool.isSphinxBookThemeLikeTheme()) {
Expand Down Expand Up @@ -201,11 +208,6 @@ export class EthicalAdsAddon extends AddonBase {
} else {
// Default to a text ad appended to the root selector when no known placement found
placement.setAttribute("data-ea-type", "text");
// TODO: Check this placement on the dashboard,
// and see how this is performing.
const docToolName = docTool.getDocumentationTool();
const idSuffix = docToolName ? `-${docToolName}` : "";
placement.setAttribute("id", `readthedocs-ea-text-footer${idSuffix}`);

const rootSelector = docTool.getRootSelector();
const rootElement = document.querySelector(rootSelector);
Expand Down Expand Up @@ -240,6 +242,43 @@ export class EthicalAdsAddon extends AddonBase {
campaign_types.join("|"),
);
}

const placementStyle =
placement.getAttribute("data-ea-style") || "nostyle";
const placementType = placement.getAttribute("data-ea-type") || "notype";
const placementIdPrefix = `${placementType}-${placementStyle}`;
if (!placement.getAttribute("id")) {
// Set a standardized id attribute
placement.setAttribute(
"id",
`readthedocs-ea-${placementIdPrefix}-${placementIdSuffix}`,
);
}

if (placementStyle == "fixedfooter") {
// Use a ``MutationObserver`` to listen to style changes in the fixed footer ad.
// Grab the height of it and use to add some ``padding-bottom`` to the required elements.
const config = { attributes: true, childList: false, subtree: false };
Copy link
Member

Choose a reason for hiding this comment

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

Is this needed? Did we get this logic from somewhere else? It feels pretty magical to me.

Copy link
Member Author

Choose a reason for hiding this comment

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

const callback = (mutationList, observer) => {
for (const mutation of mutationList) {
if (mutation.type === "attributes") {
const fixedFooterAdHeight = window.getComputedStyle(
mutation.target,
).height;
console.debug("fixedFooterAdHeight", fixedFooterAdHeight);
for (const selector of selectors) {
const element = document.querySelector(selector);
element.style.setProperty(
"padding-bottom",
fixedFooterAdHeight,
);
}
}
}
};
const observer = new MutationObserver(callback);
observer.observe(placement, config);
}
}

return placement;
Expand Down Expand Up @@ -271,6 +310,11 @@ export class EthicalAdsAddon extends AddonBase {
return true;
}

setFixedFooterAdProperties(selectors, placement) {
placement.setAttribute("data-ea-type", "text");
placement.setAttribute("data-ea-style", "fixedfooter");
}

addEaPlacementToElement(element) {
// Add `ea-placement-bottom` to the element only if the flyout is enabled.
const flyoutEnabled = objectPath.get(
Expand Down
5 changes: 2 additions & 3 deletions tests/__snapshots__/ethicalads.test.snap.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@ snapshots["EthicalAd addon ad placement defined by the user"] = `<div
</div>
`;
/* end snapshot EthicalAd addon ad placement defined by the user */
snapshots["EthicalAd addon ad placement injected"] =
snapshots["EthicalAd addon ad placement injected"] =
`<div
class="raised"
data-ea-campaign-types="community|paid"
data-ea-keywords="docs|data-science"
data-ea-manual="true"
data-ea-publisher="readthedocs"
data-ea-type="text"
id="readthedocs-ea-text-footer"
id="readthedocs-ea-text-nostyle-nodoctool"
>
</div>
`;
/* end snapshot EthicalAd addon ad placement injected */

4 changes: 1 addition & 3 deletions tests/ethicalads.test.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@

it("ad placement injected", async () => {
const addon = new ethicalads.EthicalAdsAddon(config);
const element = document.querySelector(
"#readthedocs-ea-text-footer",
);
const element = document.querySelector("[id^=readthedocs-ea");

expect(element).to.exist;
expect(element).to.have.attribute("data-ea-type", "text");
Expand Down