Skip to content

Commit

Permalink
Move plugin version menu dependencies to script (vagrant-libvirt#1545)
Browse files Browse the repository at this point in the history
Move to manage the entire plugin version menu and dependencies from the
same script. This will allow the entire site to roll forward to newer
releases and bugfix the plugin versions menu as issues are found, mostly
without needing to touch any of the individual releases.
  • Loading branch information
electrofelix authored Aug 15, 2022
1 parent 4fc3c84 commit 77e53a2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 11 deletions.
6 changes: 0 additions & 6 deletions docs/_includes/head_custom.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.27.2/axios.min.js"></script>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/index.bundle.js"
integrity="sha256-yJbSlTxKmgU+sjlMx48OSjoiUsboy18gXTxUBniEEO0="
crossorigin="anonymous"
></script>
<link rel="stylesheet" href="{{ '/assets/css/just-the-docs-light.css' | relative_url }}" media="(prefers-color-scheme: light)">
<link rel="stylesheet" href="{{ '/assets/css/just-the-docs-dark.css' | relative_url }}" media="(prefers-color-scheme: dark)">
63 changes: 58 additions & 5 deletions docs/assets/js/plugin_versions_menu.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,61 @@
const { buildWebStorage, setupCache } = window.AxiosCacheInterceptor;
const storage = buildWebStorage(sessionStorage, 'axios-cache:');
const axiosCached = setupCache(axios.create(), { storage });
// Look at webpack to replace the dependency loading
// also would allow use yarn + jest to support unit testing code below

const dependencies = [
{
src: 'https://cdnjs.cloudflare.com/ajax/libs/axios/0.27.2/axios.min.js',
integrity: 'sha512-odNmoc1XJy5x1TMVMdC7EMs3IVdItLPlCeL5vSUPN2llYKMJ2eByTTAIiiuqLg+GdNr9hF6z81p27DArRFKT7A==',
crossOrigin: 'anonymous',
},
{
src: 'https://cdn.jsdelivr.net/npm/[email protected]/dist/index.bundle.js',
integrity: 'sha256-yJbSlTxKmgU+sjlMx48OSjoiUsboy18gXTxUBniEEO0=',
crossOrigin: 'anonymous',
},
]

const loaded = [];

dependencies.forEach( (dep, i) => {
loaded[i] = false;
loadScript(dep, function() {
loaded[i] = true;
if (loaded.every(value => value === true)) {
// arguments come from a site constants file
handleVersionedDocs(repository_nwo, basePath);
}
});
})

// helper taken from https://stackoverflow.com/a/950146 under CC BY-SA 4.0, added support
// for additional attributes to be set on the script tag.
function loadScript(url, callback)
{
// Adding the script tag to the head as suggested before
var head = document.head;
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url.src;
delete url.src;
for (var attribute in url) {
script[attribute] = url[attribute];
}

// Then bind the event to the callback function.
// There are several events for cross browser compatibility.
script.onreadystatechange = callback;
script.onload = callback;

// Fire the loading
head.appendChild(script);
}

// main function, must wait until dependencies are loaded before calling
function handleVersionedDocs(repository_nwo, basePath) {
const { buildWebStorage, setupCache } = window.AxiosCacheInterceptor;
const storage = buildWebStorage(sessionStorage, 'axios-cache:');
const axiosCached = setupCache(axios.create(), { storage });

changeVersion = function handleVersionedDocs(repository_nwo, basePath) {
menuBackgroundImageClosed = "url(\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='15 6 9 12 15 18'%3E%3C/polyline%3E%3C/svg%3E\")";
menuBackgroundImageOpen = "url(\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E\")";

Expand Down Expand Up @@ -173,4 +226,4 @@ changeVersion = function handleVersionedDocs(repository_nwo, basePath) {
hideMenu();
}
});
}(repository_nwo, basePath);
}

0 comments on commit 77e53a2

Please sign in to comment.