Skip to content

Commit

Permalink
Merge pull request #8447 from imperial-chief/8380/refactor/remove-inl…
Browse files Browse the repository at this point in the history
…ine-js-breadcrumb

Removed inline js from breadcrumb_select.html.
  • Loading branch information
jimchamp committed Oct 24, 2023
2 parents 1628e56 + dc742af commit 08c5f93
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
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>

0 comments on commit 08c5f93

Please sign in to comment.