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

Expand report <details> on print. Fixes #1240 #1468

Merged
merged 2 commits into from
Jan 14, 2017
Merged
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
27 changes: 27 additions & 0 deletions lighthouse-core/report/scripts/lighthouse-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,34 @@ function sendJSONReport() {
const popup = window.open(VIEWER_URL, '_blank');
}

/**
* Sets up listeners to expand audit `<details>` when the user prints the page.
Copy link
Member

Choose a reason for hiding this comment

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

this is kind of dumb, but maybe add a clause here that expanding <details> isn't possible via a print stylesheet? Whenever I think about this issue my first thought is always, "wait, why can't we just solve this in the print stylesheet?" and it probably will be again when I come across this function in the future :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

* Ideally, a print stylesheet could take care of this, but CSS has no way to
* open a `<details>` element. When the user closes the print dialog, all
* `<details>` are collapsed.
*/
function expandDetailsWhenPrinting() {
const details = Array.from(document.querySelectorAll('details'));

// FF and IE implement these old events.
if ('onbeforeprint' in window) {
window.addEventListener('beforeprint', _ => {
details.map(detail => detail.open = true);
});
window.addEventListener('afterprint', _ => {
details.map(detail => detail.open = false);
});
} else {
// Note: while FF has media listeners, it doesn't fire when matching 'print'.
window.matchMedia('print').addListener(mql => {
details.map(detail => detail.open = mql.matches);
});
}
}

window.addEventListener('DOMContentLoaded', _ => {
expandDetailsWhenPrinting();

const printButton = document.querySelector('.js-print');
printButton.addEventListener('click', _ => {
window.print();
Expand Down