-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API: use
<meta>
to define supported API version and trigger `Custom…
…Event` (#64) Let users to define `<meta name="readthedocs-api-version" content="1.0">` to tell Read the Docs client what is the API scheme version supported by them. When our client request the API data, if the `api_version` returned does not match with the one expected by the user, another request is done to force a particular API scheme version. Then, we dispatch a `readthedocsdataready` custom event ~~and expose `window.readthedocs`~~, to let our users know this data is ready to be consumed by their own integrations. Closes #60 Closes #61 Closes #17 Closes readthedocs/readthedocs.org#9957 Closes #250
- Loading branch information
Showing
8 changed files
with
186 additions
and
25 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,53 @@ | ||
import { getMetadataAddonsAPIVersion } from "./readthedocs-config"; | ||
import { ADDONS_API_VERSION } from "./utils"; | ||
|
||
export const EVENT_READTHEDOCS_SEARCH_SHOW = "readthedocs-search-show"; | ||
export const EVENT_READTHEDOCS_SEARCH_HIDE = "readthedocs-search-hide"; | ||
export const EVENT_READTHEDOCS_DOCDIFF_ADDED_REMOVED_SHOW = | ||
"readthedocs-docdiff-added-removed-show"; | ||
export const EVENT_READTHEDOCS_DOCDIFF_HIDE = "readthedocs-docdiff-hide"; | ||
export const EVENT_READTHEDOCS_FLYOUT_SHOW = "readthedocs-flyout-show"; | ||
export const EVENT_READTHEDOCS_FLYOUT_HIDE = "readthedocs-flyout-hide"; | ||
export const EVENT_READTHEDOCS_ADDONS_DATA_READY = | ||
"readthedocs-addons-data-ready"; | ||
|
||
/** | ||
* Object to pass to user subscribing to `EVENT_READTHEDOCS_ADDONS_DATA_READY`. | ||
* | ||
* This object allows us to have a better communication with the user. | ||
* Instead of passing the raw data, we pass this object and enforce them | ||
* to use it in an expected way: | ||
* | ||
* document.addEventListener( | ||
* "readthedocs-addons-data-ready", | ||
* function (event) { | ||
* const data = event.detail.data(); | ||
* } | ||
* ); | ||
* | ||
* Note that we perform some checks/validations when `.data()` is called, | ||
* to make sure the user is using the pattern in the expected way. | ||
* Otherwise, we throw an exception. | ||
*/ | ||
export class ReadTheDocsEventData { | ||
constructor(data) { | ||
this._initialized = false; | ||
this._data = data; | ||
} | ||
|
||
initialize() { | ||
const metadataAddonsAPIVersion = getMetadataAddonsAPIVersion(); | ||
if (metadataAddonsAPIVersion === undefined) { | ||
throw `Subscribing to '${EVENT_READTHEDOCS_ADDONS_DATA_READY}' requires defining the '<meta name="readthedocs-addons-api-version" content="${ADDONS_API_VERSION}" />' tag in the HTML.`; | ||
} | ||
|
||
this._initialized = true; | ||
} | ||
|
||
data() { | ||
if (!this._initialized) { | ||
this.initialize(); | ||
} | ||
return this._data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters