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

Reload collapsed PHeT Iframes (fix #541) #551

Merged
merged 1 commit into from
May 28, 2019
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
33 changes: 33 additions & 0 deletions assets/src/scripts/collapse-sections.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,32 @@
*/

document.addEventListener( 'DOMContentLoaded', function () {

// ### Helper functions:

/**
* @param {string} url
* @returns {string}
*/
function url_domain( url ) {
const matches = url.match( /^https?:\/\/([^/?#]+)(?:[/?#]|$)/i );
const domain = matches && matches[ 1 ]; // domain will be null if no match is found
if ( typeof domain === 'string' || domain instanceof String ) {
return domain;
} else {
return '';
}
}

// ### Show/hide code:

// Get all the headings
const sectionHeadingEl = 'h1';
const headings = document.querySelectorAll(
`#content section ${sectionHeadingEl}:not(.entry-title)`
);
// Collapsed Iframes
let collapsedIframesHaveBeenUnfurled = false;

Array.prototype.forEach.call( headings, heading => {
// Give each <h1> a toggle button child
Expand Down Expand Up @@ -82,6 +103,18 @@ document.addEventListener( 'DOMContentLoaded', function () {
wrapper.hidden = expanded;
// Trigger H5P resize
window.dispatchEvent( new Event( 'resize' ) );
// Unfurl collapsed iframes
if ( ! expanded && ! collapsedIframesHaveBeenUnfurled ) {
const collapsedIframes = wrapper.querySelectorAll( 'iframe' );
Array.prototype.forEach.call( collapsedIframes, iframe => {
// Hack: Reload broken PHeT Iframes
// @see https://github.com/phetsims/tasks/issues/1002
if ( url_domain( iframe.src ).includes( 'phet.colorado.edu' ) ) {
iframe.src = iframe.src; // eslint-disable-line
}
} );
collapsedIframesHaveBeenUnfurled = true;
}
};
} );
} );
2 changes: 1 addition & 1 deletion dist/mix-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"/scripts/book.js": "/scripts/book.js?id=94aa48eec15c7a80f990",
"/styles/book.css": "/styles/book.css?id=fc62678c134b29a2980f",
"/styles/web-house-style.css": "/styles/web-house-style.css?id=d3c1aedc7525b0f30ccd",
"/scripts/collapse-sections.js": "/scripts/collapse-sections.js?id=0ed7575ee2f1f7fa9ed5",
"/scripts/collapse-sections.js": "/scripts/collapse-sections.js?id=5d222dcac387af7b6868",
"/scripts/lightbox.js": "/scripts/lightbox.js?id=91b6b3fe96be81915d2e",
"/scripts/pane.js": "/scripts/pane.js?id=4173f00e886097d83339",
"/scripts/sharer.js": "/scripts/sharer.js?id=cb1d75f017e08db3a704",
Expand Down
2 changes: 1 addition & 1 deletion dist/scripts/collapse-sections.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 21 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.