Skip to content

Commit

Permalink
Refactor to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
lauzadis committed Jul 2, 2024
1 parent fd4f885 commit ffc7158
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 131 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ allprojects {
],
"customAssets": [
"${rootProject.file("docs/dokka-presets/assets/logo-icon.svg")}",
"${rootProject.file("docs/dokka-presets/assets/aws_logo_white_59x35.png")}"
"${rootProject.file("docs/dokka-presets/assets/aws_logo_white_59x35.png")}",
"${rootProject.file("docs/dokka-presets/scripts/accessibility.js")}"
],
"footerMessage": "© $year, Amazon Web Services, Inc. or its affiliates. All rights reserved.",
"separateInheritedMembers" : true,
Expand Down
125 changes: 125 additions & 0 deletions docs/dokka-presets/scripts/accessibility.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Fix for accessibiliy violation: "Provide a mechanism for skipping past repetitive content"
document.addEventListener('DOMContentLoaded', function() {
function insertSkipLink(element) {
const skipLink = document.createElement('div');
// Create an anchor element with the href pointing to the main content
const anchor = document.createElement('a');
anchor.classList.add('skip-to-content');
anchor.href = '#content';
anchor.innerHTML = 'Skip to Main Content';
anchor.setAttribute("tabindex", "0");
skipLink.appendChild(anchor);
if (element.children.length > 1) {
element.insertBefore(skipLink, element.children[1]);
} else {
element.appendChild(skipLink);
}
}

function handleChanges(mutationsList) {
for (const mutation of mutationsList) {
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
// Check added nodes for elements with class 'sideMenuPart' and without class 'hidden'
mutation.addedNodes.forEach(function(node) {
if (node.nodeType === 1 && node.classList.contains('sideMenuPart') && !node.classList.contains('hidden')) {
insertSkipLink(node);
}
});
} else if (mutation.type === 'attributes' && mutation.target.classList.contains('sideMenuPart') && !mutation.target.classList.contains('hidden')) {
// Handle changes in the 'class' attribute of existing elements
// Check if the element is 'sideMenuPart' and not 'hidden'
insertSkipLink(mutation.target);
}
}
}

const observer = new MutationObserver(handleChanges);
const observerConfig = {
childList: true,
subtree: true,
attributes: true,
attributeFilter: ['class']
};
observer.observe(document.body, observerConfig);
});

// Fix for accessibilty violation: "Ensure all interactive functionality is operable with the keyboard"
window.onload = function() {
const navButtons = document.querySelectorAll('.navButton');

navButtons.forEach(function(navButton) {
// Make the navButton focusable, add accessibility information
navButton.setAttribute('tabindex', '0');
navButton.setAttribute('role', 'button');
navButton.setAttribute('aria-expanded', 'false');

// Grab the page ID, use it for aria-label and aria-controls
const sectionName = navButton.parentElement.parentElement.getAttribute('pageid')
// Remove the page ID suffix auto-generated by Dokka
const cleanedSectionName = sectionName.substring(0, sectionName.indexOf("////PointingToDeclaration"))
navButton.setAttribute('aria-label', cleanedSectionName);

const sectionID = navButton.parentElement.parentElement.id
navButton.setAttribute('aria-controls', sectionID);

// Add event listener for Enter and Space keys
navButton.addEventListener('keydown', function(event) {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault(); // Prevent the default action to avoid navigation
this.click(); // Trigger the onclick event
}
});

// Update aria-expanded attribute on click
navButton.addEventListener('click', function() {
const isExpanded = navButton.getAttribute('aria-expanded') === 'true';
navButton.setAttribute('aria-expanded', (!isExpanded).toString());
});
});
}

// Fix for accessibility violation: "Ensure pages reflow without requiring two-dimensional scrolling without loss of content or functionality"
document.addEventListener('DOMContentLoaded', function() {
const MIN_WINDOW_SIZE = 550

// Function to insert 'toggle content' button
function insertToggleContentButton(element) {
const toggleContent = document.createElement('button');
toggleContent.className = 'aws-toggle-content-btn';
toggleContent.textContent = '☰';

const initiallyVisible = window.innerWidth >= MIN_WINDOW_SIZE

toggleContent.setAttribute('aria-expanded', initiallyVisible.toString());
toggleContent.setAttribute('aria-label', 'Toggle code block for' + element.getAttribute("data-togglable"));
toggleContent.setAttribute('aria-controls', element.id);

// Set initial visibility based on window size
element.style.display = initiallyVisible ? 'block' : 'none'

// Toggle visibility onclick
toggleContent.onclick = function() {
const isExpanded = toggleContent.getAttribute('aria-expanded') === 'true';
toggleContent.setAttribute('aria-expanded', (!isExpanded).toString());
element.style.display = isExpanded ? 'none' : 'block'
};

element.parentNode.insertBefore(toggleContent, element);
}

document.querySelectorAll('.content[data-togglable]').forEach(insertToggleContentButton);

// Update content visibility on resize
window.addEventListener('resize', function() {
document.querySelectorAll('.content[data-togglable]').forEach(function(element) {
const toggleContent = element.previousSibling;
if (window.innerWidth < MIN_WINDOW_SIZE) {
element.style.display = 'none';
toggleContent.setAttribute('aria-expanded', 'false');
} else {
element.style.display = 'block';
toggleContent.setAttribute('aria-expanded', 'true');
}
});
});
});
130 changes: 0 additions & 130 deletions docs/dokka-presets/templates/base.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -27,136 +27,6 @@
<#-- Resources (scripts, stylesheets) are handled by Dokka.
Use customStyleSheets and customAssets to change them. -->
<@resources/>

<script>
// Fix for accessibiliy violation: "Provide a mechanism for skipping past repetitive content"
document.addEventListener('DOMContentLoaded', function() {
function insertSkipLink(element) {
const skipLink = document.createElement('div');
// Create an anchor element with the href pointing to the main content
const anchor = document.createElement('a');
anchor.classList.add('skip-to-content');
anchor.href = '#content';
anchor.innerHTML = 'Skip to Main Content';
anchor.setAttribute("tabindex", "0");
skipLink.appendChild(anchor);
if (element.children.length > 1) {
element.insertBefore(skipLink, element.children[1]);
} else {
element.appendChild(skipLink);
}
}
function handleChanges(mutationsList) {
for (const mutation of mutationsList) {
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
// Check added nodes for elements with class 'sideMenuPart' and without class 'hidden'
mutation.addedNodes.forEach(function(node) {
if (node.nodeType === 1 && node.classList.contains('sideMenuPart') && !node.classList.contains('hidden')) {
insertSkipLink(node);
}
});
} else if (mutation.type === 'attributes' && mutation.target.classList.contains('sideMenuPart') && !mutation.target.classList.contains('hidden')) {
// Handle changes in the 'class' attribute of existing elements
// Check if the element is 'sideMenuPart' and not 'hidden'
insertSkipLink(mutation.target);
}
}
}
const observer = new MutationObserver(handleChanges);
const observerConfig = {
childList: true,
subtree: true,
attributes: true,
attributeFilter: ['class']
};
observer.observe(document.body, observerConfig);
});
// Fix for accessibilty violation: "Ensure all interactive functionality is operable with the keyboard"
window.onload = function() {
const navButtons = document.querySelectorAll('.navButton');
navButtons.forEach(function(navButton) {
// Make the navButton focusable, add accessibility information
navButton.setAttribute('tabindex', '0');
navButton.setAttribute('role', 'button');
navButton.setAttribute('aria-expanded', 'false');
// Grab the page ID, use it for aria-label and aria-controls
const sectionName = navButton.parentElement.parentElement.getAttribute('pageid')
// Remove the page ID suffix auto-generated by Dokka
const cleanedSectionName = sectionName.substring(0, sectionName.indexOf("////PointingToDeclaration"))
navButton.setAttribute('aria-label', cleanedSectionName);
const sectionID = navButton.parentElement.parentElement.id
navButton.setAttribute('aria-controls', sectionID);
// Add event listener for Enter and Space keys
navButton.addEventListener('keydown', function(event) {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault(); // Prevent the default action to avoid navigation
this.click(); // Trigger the onclick event
}
});
// Update aria-expanded attribute on click
navButton.addEventListener('click', function() {
const isExpanded = navButton.getAttribute('aria-expanded') === 'true';
navButton.setAttribute('aria-expanded', (!isExpanded).toString());
});
});
}
</script>

<script>
// Fix for accessibility violation: "Ensure pages reflow without requiring two-dimensional scrolling without loss of content or functionality"
document.addEventListener('DOMContentLoaded', function() {
const MIN_WINDOW_SIZE = 550
// Function to insert 'toggle content' button
function insertToggleContentButton(element) {
const toggleContent = document.createElement('button');
toggleContent.className = 'aws-toggle-content-btn';
toggleContent.textContent = '';
const initiallyVisible = window.innerWidth >= MIN_WINDOW_SIZE
toggleContent.setAttribute('aria-expanded', initiallyVisible.toString());
toggleContent.setAttribute('aria-label', 'Toggle code block for' + element.getAttribute("data-togglable"));
toggleContent.setAttribute('aria-controls', element.id);
// Set initial visibility based on window size
element.style.display = initiallyVisible ? 'block' : 'none'
// Toggle visibility onclick
toggleContent.onclick = function() {
const isExpanded = toggleContent.getAttribute('aria-expanded') === 'true';
toggleContent.setAttribute('aria-expanded', (!isExpanded).toString());
element.style.display = isExpanded ? 'none' : 'block'
};
element.parentNode.insertBefore(toggleContent, element);
}
document.querySelectorAll('.content[data-togglable]').forEach(insertToggleContentButton);
// Update content visibility on resize
window.addEventListener('resize', function() {
document.querySelectorAll('.content[data-togglable]').forEach(function(element) {
const toggleContent = element.previousSibling;
if (window.innerWidth < MIN_WINDOW_SIZE) {
element.style.display = 'none';
toggleContent.setAttribute('aria-expanded', 'false');
} else {
element.style.display = 'block';
toggleContent.setAttribute('aria-expanded', 'true');
}
});
});
});
</script>
</head>
<body>
<div class="root">
Expand Down

0 comments on commit ffc7158

Please sign in to comment.