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

Fix Magic Select binding for Editions DataTable #9888

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
35 changes: 32 additions & 3 deletions openlibrary/plugins/openlibrary/js/editions-table/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export function initEditionsTable() {
var rowCount;
let currentLength;

// Prevent reinitialization of the editions datatable
if ($.fn.DataTable.isDataTable($('#editions'))) {
return;
}
$('#editions th.title').on('mouseover', function(){
if ($(this).hasClass('sorting_asc')) {
$(this).attr('title','Sort latest to earliest');
Expand Down Expand Up @@ -54,7 +58,6 @@ export function initEditionsTable() {
});
} else {
currentLength = Number(localStorage.getItem(LS_RESULTS_LENGTH_KEY));

$('#editions').DataTable({
aoColumns: [{sType: 'html'},null],
order: [ [1,'asc'] ],
Expand All @@ -65,7 +68,33 @@ export function initEditionsTable() {
bFilter: true,
bStateSave: false,
bAutoWidth: false,
pageLength: currentLength ? currentLength : DEFAULT_LENGTH
});
pageLength: currentLength ? currentLength : DEFAULT_LENGTH,
drawCallback: function() {
if ($('#ile-toolbar')) {
const editionStorage = JSON.parse(sessionStorage.getItem('ile-items'))['edition']
const matchEdition = (string) => {
return string.match(/OL[0-9]+[a-zA-Z]/)
}
for (const el of $('.ile-selected')) {
const anchor = el.getElementsByTagName('a');
if (anchor.length) {
const edIdentifier = matchEdition(anchor[0].getAttribute('href'))
if (!editionStorage.includes(edIdentifier[0])) {
el.classList.remove('ile-selected');
}
}
}
for (const el of $('.ile-selectable')) {
const anchor = el.getElementsByTagName('a');
if (anchor.length) {
const edIdentifier = matchEdition(anchor[0].getAttribute('href'));
if (editionStorage.includes(edIdentifier[0])) {
el.classList.add('ile-selected');
}
}
}
}
}
})
}
}
17 changes: 10 additions & 7 deletions openlibrary/plugins/openlibrary/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,6 @@ jQuery(function () {

init($);

// conditionally load functionality based on what's in the page
if (document.getElementsByClassName('editions-table--progressively-enhanced').length) {
import(/* webpackChunkName: "editions-table" */ './editions-table')
.then(module => module.initEditionsTable());
}

const edition = document.getElementById('addWork');
const autocompleteAuthor = document.querySelector('.multi-input-autocomplete--author');
const autocompleteLanguage = document.querySelector('.multi-input-autocomplete--language');
Expand Down Expand Up @@ -339,8 +333,17 @@ jQuery(function () {
if (document.getElementsByClassName('show-librarian-tools').length) {
import(/* webpackChunkName: "ile" */ './ile')
.then((module) => module.init());
// Import ile then the datatable to apply clickable classes to all listed editions
if (document.getElementsByClassName('editions-table--progressively-enhanced').length) {
import(/* webpackChunkName: "editions-table" */ './editions-table')
.then(module => module.initEditionsTable())
}
}
// conditionally load functionality based on what's in the page
if (document.getElementsByClassName('editions-table--progressively-enhanced').length) {
import(/* webpackChunkName: "editions-table" */ './editions-table')
.then(module => module.initEditionsTable());
}

if ($('#cboxPrevious').length) {
$('#cboxPrevious').attr({'aria-label': 'Previous button', 'aria-hidden': 'true'});
}
Expand Down
11 changes: 10 additions & 1 deletion static/css/components/editions.less
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ table#editions,

td.book {
vertical-align: top;
display: inline-flex;
display: table-cell;
}

div.cover {
Expand Down Expand Up @@ -159,6 +159,15 @@ table#editions,
}
}

@media only screen and (max-width: @width-breakpoint-desktop) {
table#editions {
td.book {
width: 100%;
display: inline-flex;
}
}
}

@media only screen and (max-width: @width-breakpoint-tablet) {
table#editions {
width: 100%;
Expand Down