Skip to content

Commit

Permalink
Notifications: allow disable latest and/or non-stable (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
humitos authored Nov 4, 2024
1 parent 6c86cb0 commit 81c058c
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 95 deletions.
6 changes: 3 additions & 3 deletions dist/readthedocs-addons.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/readthedocs-addons.js.map

Large diffs are not rendered by default.

10 changes: 4 additions & 6 deletions public/_/readthedocs-addons.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,11 @@
"enabled": true,
"code": "UA-12345"
},
"external_version_warning": {
"notifications": {
"enabled": true,
"query_selector": "[role=main]"
},
"non_latest_version_warning": {
"enabled": true,
"query_selector": "[role=main]"
"show_on_latest": true,
"show_on_non_stable": true,
"show_on_external": true
},
"doc_diff": {
"enabled": true,
Expand Down
10 changes: 2 additions & 8 deletions src/data-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,15 +285,9 @@ const addons_notifications = {
properties: {
addons: {
type: "object",
required: ["external_version_warning", "non_latest_version_warning"],
required: ["notifications"],
properties: {
external_version_warning: {
type: "object",
properties: {
enabled: { type: "boolean" },
},
},
non_latest_version_warning: {
enabled: {
type: "object",
properties: {
enabled: { type: "boolean" },
Expand Down
151 changes: 80 additions & 71 deletions src/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
faHourglassHalf,
} from "@fortawesome/free-solid-svg-icons";
import { html, nothing, render, LitElement } from "lit";
import { default as objectPath } from "object-path";

import styleSheet from "./notification.css";
import { AddonBase, addUtmParameters, getLinkWithFilename } from "./utils";
Expand Down Expand Up @@ -135,7 +136,7 @@ export class NotificationElement extends LitElement {
this.config = config;

if (
this.config.addons.external_version_warning.enabled &&
this.config.addons.notifications.enabled &&
this.config.versions.current.type === "external"
) {
this.urls = {
Expand All @@ -150,7 +151,11 @@ export class NotificationElement extends LitElement {
}

if (
config.addons.non_latest_version_warning.enabled &&
objectPath.get(
this.config,
"addons.notifications.show_on_latest",
false,
) &&
config.projects.current.versioning_scheme !==
"single_version_without_translations" &&
config.versions.current.type !== "external"
Expand Down Expand Up @@ -189,16 +194,42 @@ export class NotificationElement extends LitElement {
return nothing;
}

if (!this.config.addons.notifications.enabled) {
return nothing;
}

if (this.config.versions.current.type === "external") {
if (this.config.addons.external_version_warning.enabled) {
if (
objectPath.get(
this.config,
"addons.notifications.show_on_external",
false,
)
) {
return this.renderExternalVersionWarning();
}
} else if (
this.config.addons.non_latest_version_warning.enabled &&
(this.readingLatestVersion || this.stableVersionAvailable)
}

if (
this.readingLatestVersion &&
this.stableVersionAvailable &&
objectPath.get(this.config, "addons.notifications.show_on_latest", false)
) {
return this.renderStableLatestVersionWarning();
return this.renderLatestVersionWarning();
}

if (
!this.readingStableVersion &&
this.stableVersionAvailable &&
objectPath.get(
this.config,
"addons.notifications.show_on_non_stable",
false,
)
) {
return this.renderStableVersionWarning();
}

return nothing;
}

Expand Down Expand Up @@ -247,56 +278,51 @@ export class NotificationElement extends LitElement {
}
}

renderStableLatestVersionWarning() {
library.add(faHourglassHalf);
renderLatestVersionWarning() {
library.add(faFlask);
if (this.readingLatestVersion && this.stableVersionAvailable) {
const iconFlask = icon(faFlask, {
classes: ["header", "icon"],
});

return html`
<div>
${iconFlask.node[0]}
<div class="title">
This is the <span>latest development version</span>
${this.renderCloseButton()}
</div>
<div class="content">
Some features may not yet be available in the published stable
version. Read the
<a href="${this.urls.stable}"
>stable version of this documentation</a
>.
</div>
</div>
`;
}
const iconFlask = icon(faFlask, {
classes: ["header", "icon"],
});

if (!this.readingStableVersion && this.stableVersionAvailable) {
const iconHourglassHalf = icon(faHourglassHalf, {
classes: ["header", "icon"],
});

return html`
<div>
${iconHourglassHalf.node[0]}
<div class="title">
This <em>may</em> be an
<span>old version of this documentation</span>
${this.renderCloseButton()}
</div>
<div class="content">
You may be reading an old version of this documentation. Read the
<a href="${this.urls.stable}"
>latest stable version of this documentation</a
>.
</div>
return html`
<div>
${iconFlask.node[0]}
<div class="title">
This is the <span>latest development version</span>
${this.renderCloseButton()}
</div>
`;
}
<div class="content">
Some features may not yet be available in the published stable
version. Read the
<a href="${this.urls.stable}">stable version of this documentation</a
>.
</div>
</div>
`;
}

return nothing;
renderStableVersionWarning() {
library.add(faHourglassHalf);
const iconHourglassHalf = icon(faHourglassHalf, {
classes: ["header", "icon"],
});

return html`
<div>
${iconHourglassHalf.node[0]}
<div class="title">
This <em>may</em> be an
<span>old version of this documentation</span>
${this.renderCloseButton()}
</div>
<div class="content">
You may be reading an old version of this documentation. Read the
<a href="${this.urls.stable}"
>latest stable version of this documentation</a
>.
</div>
</div>
`;
}

renderExternalVersionWarning() {
Expand Down Expand Up @@ -376,6 +402,7 @@ export class NotificationElement extends LitElement {
export class NotificationAddon extends AddonBase {
static jsonValidationURI =
"http://v1.schemas.readthedocs.org/addons.notifications.json";
static addonEnabledPath = "addons.notifications.enabled";
static addonName = "Notification";

constructor(config) {
Expand All @@ -393,24 +420,6 @@ export class NotificationAddon extends AddonBase {
elem.loadConfig(config);
}
}

/**
* Test if addon is enabled in the configuration
*
* @param {Object} config - Addon configuration object
*/
static isEnabled(config) {
if (!super.isConfigValid(config)) {
return false;
}

return (
(config.addons.external_version_warning.enabled === true &&
config.versions.current.type === "external") ||
(config.addons.non_latest_version_warning.enabled === true &&
config.versions.current.type !== "external")
);
}
}

customElements.define("readthedocs-notification", NotificationElement);
12 changes: 6 additions & 6 deletions tests/notification.test.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
setLocalStorageStub.reset();
config = {
addons: {
external_version_warning: {
enabled: true,
},
non_latest_version_warning: {
notifications: {
enabled: true,
show_on_latest: true,
show_on_non_stable: true,
show_on_external: true,
},
},
builds: {
Expand Down Expand Up @@ -144,7 +144,7 @@
</div>
<div class="content">
Some features may not yet be available in the published stable
version. Read the
version. Read the
<a href="https://project.readthedocs.io/en/stable/section/page.html">
stable version of this documentation
</a>
Expand Down Expand Up @@ -180,7 +180,7 @@
</div>
<div class="content">
Some features may not yet be available in the published stable
version. Read the
version. Read the
<a href="https://project.readthedocs.io/en/stable/section/page.html">
stable version of this documentation
</a>
Expand Down

0 comments on commit 81c058c

Please sign in to comment.