Skip to content

Commit 81c058c

Browse files
authored
Notifications: allow disable latest and/or non-stable (#414)
Closes #133 Requires readthedocs/readthedocs.org#11718
1 parent 6c86cb0 commit 81c058c

6 files changed

+96
-95
lines changed

dist/readthedocs-addons.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/readthedocs-addons.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/_/readthedocs-addons.json

+4-6
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,11 @@
112112
"enabled": true,
113113
"code": "UA-12345"
114114
},
115-
"external_version_warning": {
115+
"notifications": {
116116
"enabled": true,
117-
"query_selector": "[role=main]"
118-
},
119-
"non_latest_version_warning": {
120-
"enabled": true,
121-
"query_selector": "[role=main]"
117+
"show_on_latest": true,
118+
"show_on_non_stable": true,
119+
"show_on_external": true
122120
},
123121
"doc_diff": {
124122
"enabled": true,

src/data-validation.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -285,15 +285,9 @@ const addons_notifications = {
285285
properties: {
286286
addons: {
287287
type: "object",
288-
required: ["external_version_warning", "non_latest_version_warning"],
288+
required: ["notifications"],
289289
properties: {
290-
external_version_warning: {
291-
type: "object",
292-
properties: {
293-
enabled: { type: "boolean" },
294-
},
295-
},
296-
non_latest_version_warning: {
290+
enabled: {
297291
type: "object",
298292
properties: {
299293
enabled: { type: "boolean" },

src/notification.js

+80-71
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
faHourglassHalf,
88
} from "@fortawesome/free-solid-svg-icons";
99
import { html, nothing, render, LitElement } from "lit";
10+
import { default as objectPath } from "object-path";
1011

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

137138
if (
138-
this.config.addons.external_version_warning.enabled &&
139+
this.config.addons.notifications.enabled &&
139140
this.config.versions.current.type === "external"
140141
) {
141142
this.urls = {
@@ -150,7 +151,11 @@ export class NotificationElement extends LitElement {
150151
}
151152

152153
if (
153-
config.addons.non_latest_version_warning.enabled &&
154+
objectPath.get(
155+
this.config,
156+
"addons.notifications.show_on_latest",
157+
false,
158+
) &&
154159
config.projects.current.versioning_scheme !==
155160
"single_version_without_translations" &&
156161
config.versions.current.type !== "external"
@@ -189,16 +194,42 @@ export class NotificationElement extends LitElement {
189194
return nothing;
190195
}
191196

197+
if (!this.config.addons.notifications.enabled) {
198+
return nothing;
199+
}
200+
192201
if (this.config.versions.current.type === "external") {
193-
if (this.config.addons.external_version_warning.enabled) {
202+
if (
203+
objectPath.get(
204+
this.config,
205+
"addons.notifications.show_on_external",
206+
false,
207+
)
208+
) {
194209
return this.renderExternalVersionWarning();
195210
}
196-
} else if (
197-
this.config.addons.non_latest_version_warning.enabled &&
198-
(this.readingLatestVersion || this.stableVersionAvailable)
211+
}
212+
213+
if (
214+
this.readingLatestVersion &&
215+
this.stableVersionAvailable &&
216+
objectPath.get(this.config, "addons.notifications.show_on_latest", false)
199217
) {
200-
return this.renderStableLatestVersionWarning();
218+
return this.renderLatestVersionWarning();
201219
}
220+
221+
if (
222+
!this.readingStableVersion &&
223+
this.stableVersionAvailable &&
224+
objectPath.get(
225+
this.config,
226+
"addons.notifications.show_on_non_stable",
227+
false,
228+
)
229+
) {
230+
return this.renderStableVersionWarning();
231+
}
232+
202233
return nothing;
203234
}
204235

@@ -247,56 +278,51 @@ export class NotificationElement extends LitElement {
247278
}
248279
}
249280

250-
renderStableLatestVersionWarning() {
251-
library.add(faHourglassHalf);
281+
renderLatestVersionWarning() {
252282
library.add(faFlask);
253-
if (this.readingLatestVersion && this.stableVersionAvailable) {
254-
const iconFlask = icon(faFlask, {
255-
classes: ["header", "icon"],
256-
});
257-
258-
return html`
259-
<div>
260-
${iconFlask.node[0]}
261-
<div class="title">
262-
This is the <span>latest development version</span>
263-
${this.renderCloseButton()}
264-
</div>
265-
<div class="content">
266-
Some features may not yet be available in the published stable
267-
version. Read the
268-
<a href="${this.urls.stable}"
269-
>stable version of this documentation</a
270-
>.
271-
</div>
272-
</div>
273-
`;
274-
}
283+
const iconFlask = icon(faFlask, {
284+
classes: ["header", "icon"],
285+
});
275286

276-
if (!this.readingStableVersion && this.stableVersionAvailable) {
277-
const iconHourglassHalf = icon(faHourglassHalf, {
278-
classes: ["header", "icon"],
279-
});
280-
281-
return html`
282-
<div>
283-
${iconHourglassHalf.node[0]}
284-
<div class="title">
285-
This <em>may</em> be an
286-
<span>old version of this documentation</span>
287-
${this.renderCloseButton()}
288-
</div>
289-
<div class="content">
290-
You may be reading an old version of this documentation. Read the
291-
<a href="${this.urls.stable}"
292-
>latest stable version of this documentation</a
293-
>.
294-
</div>
287+
return html`
288+
<div>
289+
${iconFlask.node[0]}
290+
<div class="title">
291+
This is the <span>latest development version</span>
292+
${this.renderCloseButton()}
295293
</div>
296-
`;
297-
}
294+
<div class="content">
295+
Some features may not yet be available in the published stable
296+
version. Read the
297+
<a href="${this.urls.stable}">stable version of this documentation</a
298+
>.
299+
</div>
300+
</div>
301+
`;
302+
}
298303

299-
return nothing;
304+
renderStableVersionWarning() {
305+
library.add(faHourglassHalf);
306+
const iconHourglassHalf = icon(faHourglassHalf, {
307+
classes: ["header", "icon"],
308+
});
309+
310+
return html`
311+
<div>
312+
${iconHourglassHalf.node[0]}
313+
<div class="title">
314+
This <em>may</em> be an
315+
<span>old version of this documentation</span>
316+
${this.renderCloseButton()}
317+
</div>
318+
<div class="content">
319+
You may be reading an old version of this documentation. Read the
320+
<a href="${this.urls.stable}"
321+
>latest stable version of this documentation</a
322+
>.
323+
</div>
324+
</div>
325+
`;
300326
}
301327

302328
renderExternalVersionWarning() {
@@ -376,6 +402,7 @@ export class NotificationElement extends LitElement {
376402
export class NotificationAddon extends AddonBase {
377403
static jsonValidationURI =
378404
"http://v1.schemas.readthedocs.org/addons.notifications.json";
405+
static addonEnabledPath = "addons.notifications.enabled";
379406
static addonName = "Notification";
380407

381408
constructor(config) {
@@ -393,24 +420,6 @@ export class NotificationAddon extends AddonBase {
393420
elem.loadConfig(config);
394421
}
395422
}
396-
397-
/**
398-
* Test if addon is enabled in the configuration
399-
*
400-
* @param {Object} config - Addon configuration object
401-
*/
402-
static isEnabled(config) {
403-
if (!super.isConfigValid(config)) {
404-
return false;
405-
}
406-
407-
return (
408-
(config.addons.external_version_warning.enabled === true &&
409-
config.versions.current.type === "external") ||
410-
(config.addons.non_latest_version_warning.enabled === true &&
411-
config.versions.current.type !== "external")
412-
);
413-
}
414423
}
415424

416425
customElements.define("readthedocs-notification", NotificationElement);

tests/notification.test.html

+6-6
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232
setLocalStorageStub.reset();
3333
config = {
3434
addons: {
35-
external_version_warning: {
36-
enabled: true,
37-
},
38-
non_latest_version_warning: {
35+
notifications: {
3936
enabled: true,
37+
show_on_latest: true,
38+
show_on_non_stable: true,
39+
show_on_external: true,
4040
},
4141
},
4242
builds: {
@@ -144,7 +144,7 @@
144144
</div>
145145
<div class="content">
146146
Some features may not yet be available in the published stable
147-
version. Read the
147+
version. Read the
148148
<a href="https://project.readthedocs.io/en/stable/section/page.html">
149149
stable version of this documentation
150150
</a>
@@ -180,7 +180,7 @@
180180
</div>
181181
<div class="content">
182182
Some features may not yet be available in the published stable
183-
version. Read the
183+
version. Read the
184184
<a href="https://project.readthedocs.io/en/stable/section/page.html">
185185
stable version of this documentation
186186
</a>

0 commit comments

Comments
 (0)