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
2 changes: 1 addition & 1 deletion sphinx_scylladb_theme/static/js/main.bundle.js

Large diffs are not rendered by default.

51 changes: 51 additions & 0 deletions src/js/body.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
export const openExternalLinksNewBrowserTab = () => {
const isExternal = new RegExp("^(?:[a-z]+:)?//", "i");
$("a.reference").each(function () {
$(this).removeClass("internal external");

if (isExternal.test($(this).attr("href"))) {
$(this).addClass("external");
$(this).attr("target", "_blank");
} else {
$(this).addClass("internal");
}
});
};

export const createResponsiveTables = () => {
const tables = $("table.docutils");
tables.wrap("<div class='table-wrapper'></div>");

tables.each(function () {
if ($(this).find("thead tr").length > 1) {
$(this).addClass("thead-border");
}
});
};

export const createEnlargeImagesButtons = () => {
$(".content img[width], .content img[style]").each(function () {
// Update parent css
const revealID = (Math.random() + 1).toString(36).substring(7);
$(this).wrap(
`<span class="enlarge-image" data-open="` + revealID + `"></div>`
);
// Add image reveal
const imageReveal =
`
<div class="reveal enlarge-image-reveal" id="` +
revealID +
`" data-reveal>
<img src="` +
$(this).attr("src") +
`" data-close aria-label="Close modal">
</div>
`;
$(this).after(imageReveal);
});
$(".content a.image-reference").click(function (event) {
if ($(this).children(".enlarge-image").length > 0) {
event.preventDefault();
}
});
};
65 changes: 65 additions & 0 deletions src/js/collapse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* Collapse navigation */
const localStorageKey = "scylla-docs-collapse-side-nav";

const calculateCollapsibleNavigationButtonPosition = () => {
const collapsibleButton = $(".collapsible-button");
const footer = $(".footer");
const offset = 10;
const footerOffset = footer.offset();
const footerTop = footerOffset.top;
const footerBottom = footerTop + footer.outerHeight();
const screenBottom = $(window).scrollTop() + $(window).innerHeight();
const screenTop = $(window).scrollTop();
if (screenBottom > footerTop && screenTop < footerBottom) {
collapsibleButton.css("bottom", screenBottom - footerTop + offset);
} else {
collapsibleButton.css("bottom", offset);
}
};

const collapseNavigation = () => {
new Foundation.Tooltip($(".collapsible-button"), {
position: "top",
tipText: "Expand",
});
localStorage.setItem(localStorageKey, "1");
$("#side-nav").addClass("side-nav--collapsed");
$(".content").addClass("content--collapsed");
};

const expandNavigation = () => {
new Foundation.Tooltip($(".collapsible-button"), {
position: "top",
tipText: "Collapse",
});
localStorage.setItem(localStorageKey, "0");
$("#side-nav").removeClass("side-nav--collapsed");
$(".content").removeClass("content--collapsed");
};

export const loadCollapsibleNavigation = () => {
if (localStorage.getItem(localStorageKey) == "1") {
collapseNavigation();
} else {
expandNavigation();
}
calculateCollapsibleNavigationButtonPosition();
};

export const onClickCollapsibleNavigationButton = () => {
$(".collapsible-button").on("click", function () {
$(".collapsible-button").foundation("destroy");
if ($("#side-nav").hasClass("side-nav--collapsed")) {
expandNavigation();
} else {
collapseNavigation();
}
calculateCollapsibleNavigationButtonPosition();
});
};

export const onScrollCollapsibleNavigation = () => {
$(window).scroll(function () {
calculateCollapsibleNavigationButtonPosition();
});
};
186 changes: 15 additions & 171 deletions src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,182 +6,24 @@ window.$ = $;
require("../css/main.scss");
require("foundation-sites/dist/js/foundation");

const openExternalLinksNewBrowserTab = () => {
const isExternal = new RegExp("^(?:[a-z]+:)?//", "i");
$("a.reference").each(function () {
$(this).removeClass("internal external");

if (isExternal.test($(this).attr("href"))) {
$(this).addClass("external");
$(this).attr("target", "_blank");
} else {
$(this).addClass("internal");
}
});
};

const createResponsiveTables = () => {
const tables = $("table.docutils");
tables.wrap("<div class='table-wrapper'></div>");

tables.each(function () {
if ($(this).find("thead tr").length > 1) {
$(this).addClass("thead-border");
}
});
};

const createEnlargeImagesButtons = () => {
$(".content img[width], .content img[style]").each(function () {
// Update parent css
const reveal_id = (Math.random() + 1).toString(36).substring(7);
$(this).wrap(
`<span class="enlarge-image" data-open="` + reveal_id + `"></div>`
);
// Add image reveal
const image_reveal =
`
<div class="reveal enlarge-image-reveal" id="` +
reveal_id +
`" data-reveal>
<img src="` +
$(this).attr("src") +
`" data-close aria-label="Close modal">
</div>
`;
$(this).after(image_reveal);
});
$(".content a.image-reference").click(function (event) {
if ($(this).children(".enlarge-image").length > 0) {
event.preventDefault();
}
});
};

const onScrollHighlightSecondarySidebar = () => {
const sections = $(".content").find("h2").parent();
const headerHeight = 80;
const offset = 20;

$(window).scroll(function () {
const currentScroll = $(this).scrollTop();
sections.each(function () {
const sectionPosition = $(this).offset().top;
if (sectionPosition - headerHeight - offset < currentScroll) {
const id = $(this).attr("id");

$(".secondary-side-nav a").removeClass("current");
$('.secondary-side-nav a[href="#' + id + '"]').addClass("current");
}
});
});
};

const hideBanner = () => {
const promoBanner = $(".promo-banner");
const promoBannerHeight = promoBanner.outerHeight();
if (promoBanner.length && !localStorage.getItem("scylladocs-hide-banner")) {
promoBanner.show();
$("body").css("margin-top", promoBannerHeight);
$(".side-nav").css("margin-top", promoBannerHeight);
$(".secondary-side-nav").css("margin-top", promoBannerHeight);
$(".layout").addClass("layout--has-banner");
} else {
promoBanner.hide();
}
};

const onCloseBanner = () => {
$(".promo-banner__close").on("click", function () {
localStorage.setItem("scylladocs-hide-banner", "1");
$("body").css("margin-top", 0);
$(".side-nav").css("margin-top", 0);
$(".secondary-side-nav").css("margin-top", 0);
$(".promo-banner").hide();
$(".layout").removeClass("layout--has-banner");
});
};

const onResizeBanner = () => {
$(window).resize(function () {
const promoBanner = $(".promo-banner");
const promoBannerHeight = promoBanner.outerHeight();
if (promoBanner.is(":visible")) {
$("body").css("margin-top", promoBannerHeight);
$(".side-nav").css("margin-top", promoBannerHeight);
$(".secondary-side-nav").css("margin-top", promoBannerHeight);
}
});
};

const calculateCollapsibleNavigationButtonPosition = () => {
const collapsibleButton = $(".collapsible-button");
const footer = $(".footer");
const offset = 10;
const footerOffset = footer.offset();
const footerTop = footerOffset.top;
const footerBottom = footerTop + footer.outerHeight();
const screenBottom = $(window).scrollTop() + $(window).innerHeight();
const screenTop = $(window).scrollTop();
if (screenBottom > footerTop && screenTop < footerBottom) {
collapsibleButton.css("bottom", screenBottom - footerTop + offset);
} else {
collapsibleButton.css("bottom", offset);
}
};

const collapseNavigation = () => {
new Foundation.Tooltip($(".collapsible-button"), {
position: "top",
tipText: "Expand",
});
localStorage.setItem("scylladocs-collapse-side-nav", "1");
$("#side-nav").addClass("side-nav--collapsed");
$(".content").addClass("content--collapsed");
};

const expandNavigation = () => {
new Foundation.Tooltip($(".collapsible-button"), {
position: "top",
tipText: "Collapse",
});
localStorage.setItem("scylladocs-collapse-side-nav", "0");
$("#side-nav").removeClass("side-nav--collapsed");
$(".content").removeClass("content--collapsed");
};

const loadCollapsibleNavigation = () => {
if (localStorage.getItem("scylladocs-collapse-side-nav") == "1") {
collapseNavigation();
} else {
expandNavigation();
}
calculateCollapsibleNavigationButtonPosition();
};

const onClickCollapsibleNavigationButton = () => {
$(".collapsible-button").on("click", function () {
$(".collapsible-button").foundation("destroy");
if ($("#side-nav").hasClass("side-nav--collapsed")) {
expandNavigation();
} else {
collapseNavigation();
}
calculateCollapsibleNavigationButtonPosition();
});
};

const onScrollCollapsibleNavigation = () => {
$(window).scroll(function () {
calculateCollapsibleNavigationButtonPosition();
});
};
import {
createEnlargeImagesButtons,
createResponsiveTables,
openExternalLinksNewBrowserTab,
} from "./body";
import { onScrollHighlightSecondarySidebar } from "./sidebar";
import { hideBanner, onCloseBanner, onResizeBanner } from "./promo-banner";
import {
loadCollapsibleNavigation,
onClickCollapsibleNavigationButton,
onScrollCollapsibleNavigation,
} from "./collapse";

$(document).ready(function () {
/* Body */
createEnlargeImagesButtons();
$(document).foundation();
createResponsiveTables();
onScrollHighlightSecondarySidebar();
openExternalLinksNewBrowserTab();
/* Banner */
hideBanner();
Expand All @@ -191,4 +33,6 @@ $(document).ready(function () {
loadCollapsibleNavigation();
onClickCollapsibleNavigationButton();
onScrollCollapsibleNavigation();
/* Sidebar */
onScrollHighlightSecondarySidebar();
});
63 changes: 63 additions & 0 deletions src/js/promo-banner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const localStorageKey = "scylladb-docs-hide-banner";

const setItemWithExpiry = (key, value, days) => {
const now = new Date();
const ttl = days * 24 * 60 * 60 * 1000;
const item = {
value: value,
expiry: now.getTime() + ttl,
};
localStorage.setItem(key, JSON.stringify(item));
};

function getItemWithExpiry(key) {
const itemStr = localStorage.getItem(key);
if (!itemStr) {
return null;
}
const item = JSON.parse(itemStr);
const now = new Date();

if (now.getTime() > item.expiry) {
localStorage.removeItem(key);
return null;
}
return item.value;
}

export const hideBanner = () => {
const promoBanner = $(".promo-banner");
const promoBannerHeight = promoBanner.outerHeight();
if (promoBanner.length && !getItemWithExpiry(localStorageKey)) {
promoBanner.show();
$("body").css("margin-top", promoBannerHeight);
$(".side-nav").css("margin-top", promoBannerHeight);
$(".secondary-side-nav").css("margin-top", promoBannerHeight);
$(".layout").addClass("layout--has-banner");
} else {
promoBanner.hide();
}
};

export const onCloseBanner = () => {
$(".promo-banner__close").on("click", function () {
setItemWithExpiry(localStorageKey, "1", 30);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

can we move this value to config.py?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It is possible. If necessary, we can work on it as part of a separate issue.

$("body").css("margin-top", 0);
$(".side-nav").css("margin-top", 0);
$(".secondary-side-nav").css("margin-top", 0);
$(".promo-banner").hide();
$(".layout").removeClass("layout--has-banner");
});
};

export const onResizeBanner = () => {
$(window).resize(function () {
const promoBanner = $(".promo-banner");
const promoBannerHeight = promoBanner.outerHeight();
if (promoBanner.is(":visible")) {
$("body").css("margin-top", promoBannerHeight);
$(".side-nav").css("margin-top", promoBannerHeight);
$(".secondary-side-nav").css("margin-top", promoBannerHeight);
}
});
};
Loading