Skip to content

Fix/homepage preview button#12036

Merged
mekarpeles merged 6 commits into
internetarchive:masterfrom
lokesh:fix/homepage-preview-button
Mar 11, 2026
Merged

Fix/homepage preview button#12036
mekarpeles merged 6 commits into
internetarchive:masterfrom
lokesh:fix/homepage-preview-button

Conversation

@lokesh
Copy link
Copy Markdown
Collaborator

@lokesh lokesh commented Mar 7, 2026

Builds on #11903Closes #11899

This PR takes the fix from #11903 and improves the JavaScript approach to be less brittle and more maintainable.

Technical

Changes carried over from #11903:

  • index.html: Added $:macros.BookPreview('') to render the #bookPreview modal HTML on the homepage so it's available when lazy-loaded carousel buttons are clicked
  • BookPreview.html: Added $if ocaid: guard to prevent rendering a broken preview button when no ocaid is provided
  • test_home.py: Updated test to account for the new BookPreview macro call on the homepage

Improvements over #11903:

  1. Event delegation instead of re-initialization — The original fix imported initPreviewDialogs into lazy-carousel.js and called it after each AJAX load. This creates tight coupling: every future dynamic content loader would need to know about and manually call initPreviewDialogs. Instead, this PR uses a single delegated click handler on document ($(document).on('click.bookPreview', '[data-book-preview]', ...)), so any preview button works automatically regardless of when it's added to the DOM. This eliminates the import and call from lazy-carousel.js entirely.

  2. data-book-preview attribute as JS hook — The original fix targeted the CSS class .cta-btn--preview for JS behavior. This PR adds a data-book-preview attribute and targets that instead, separating styling concerns from behavioral hooks. The CSS class remains for styling; the data attribute is the JS contract. This prevents accidental breakage if someone renames or removes the CSS class.

  3. Idempotency guard — Uses jQuery event namespacing (.off('click.bookPreview').on('click.bookPreview', ...)) so the handler is safe if initPreviewDialogs() is ever called more than once, preventing duplicate handler stacking.

  4. Fixed module loading guard — Changed the dialog module guard selector in index.js from .cta-btn--preview to #bookPreview. The guard runs at page load to decide whether to lazy-import the dialog module. With event delegation, preview buttons don't exist at page load (they come from lazy carousels), but #bookPreview (the modal target) does — so the module loads correctly.

Testing

  1. Go to the homepage
  2. Scroll to a carousel with a "Preview" button (e.g. Trending, Classic Books)
  3. Click the "Preview" button
  4. The Internet Archive book reader should open in a modal on the same page
  5. Also verify Preview buttons still work on book pages and search results

Stakeholders

@bhardwajparth51 cc: @mekarpeles

@lokesh lokesh marked this pull request as ready for review March 11, 2026 20:00
@mekarpeles mekarpeles self-assigned this Mar 11, 2026
Copy link
Copy Markdown
Member

@mekarpeles mekarpeles left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! LGTM, tested

@mekarpeles mekarpeles merged commit 47b9d59 into internetarchive:master Mar 11, 2026
5 checks passed
@RayBB RayBB removed the On Testing label Mar 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Preview Button on Homepage Does Nothing When Clicked

4 participants