Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed inline js from breadcrumb_select.html. #8447

Merged
merged 5 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions openlibrary/plugins/openlibrary/js/breadcrumb_select/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Initialize the breadcrumb select elements.
*
* @param {NodeList<HTMLElement>} crumbs - NodeList of breadcrumb select elements.
*/
export function initBreadcrumbSelect(crumbs) {
const allowedKeys = new Set(['Tab', 'Enter', ' ']);
const preventedKeys = new Set(['ArrowUp', 'ArrowDown']);
// watch crumbs for changes,
// ensures it's a full value change, not a user exploring options via keyboard
function handleNavEvents(nav) {
let ignoreChange = false;

nav.addEventListener('change', () => {
if (ignoreChange) return;
window.location = nav.value;
});

nav.addEventListener('keydown', ({ key }) => {
if (preventedKeys.has(key)) {
ignoreChange = true;
} else if (allowedKeys.has(key)) {
ignoreChange = false;
}
});
}

crumbs.forEach(handleNavEvents);
}
7 changes: 7 additions & 0 deletions openlibrary/plugins/openlibrary/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,4 +540,11 @@ jQuery(function () {
import(/* webpackChunkName: "return-form" */ './return-form')
.then(module => module.initReturnForms(returnForms))
}

const crumbs = document.querySelectorAll('.crumb select');
if (crumbs.length) {
import(/* webpackChunkName: "breadcrumb-select" */ './breadcrumb_select')
.then(module => module.initBreadcrumbSelect(crumbs));
}

});
25 changes: 0 additions & 25 deletions openlibrary/templates/books/breadcrumb_select.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,3 @@ <h2 class="breadcrumb-title">$breadcrumb_title</h2>
</select>
</span>
</span>

<script>
const crumbs = document.querySelectorAll('.crumb select')
const allowedKeys = new Set(['Tab', 'Enter', ' '])
const preventedKeys = new Set(['ArrowUp', 'ArrowDown'])

// watch crumbs for changes,
// ensures it's a full value change, not a user exploring options via keyboard
crumbs.forEach(nav => {
let ignoreChange = false

nav.addEventListener('change', e => {
if (ignoreChange) return
// it's actually changed!
window.location = nav.value;
})

nav.addEventListener('keydown', ({ key }) => {
if (preventedKeys.has(key))
ignoreChange = true
else if (allowedKeys.has(key))
ignoreChange = false
})
})
</script>