1(() => { 2 let resizeMechanismStarted = false; 3 4 // userParams {object} should be passed by WordPress 5 const userParams = window.h5pResizePulseParameters || {}; 6 7 // Simple sanitizing 8 userParams.timeout = parseInt(userParams.timeout); 9 10 if ( 11 isNaN(userParams.timeout) || 12 userParams.timeout < 500 13 ) { 14 userParams.timeout = 500; 15 } 16 17 /** 18 * Trigger a resize event on the window that H5P core will recognize. 19 */ 20 const triggerResize = () => { 21 window.dispatchEvent(new Event('resize')); 22 }; 23 24 /** 25 * Regularly trigger H5P resizing. 26 * @param {number} timeout Timeout. 27 */ 28 const scheduleResizePulse = (timeout) => { 29 setTimeout(() => { 30 triggerResize(); 31 scheduleResizePulse(timeout); 32 }, timeout); 33 }; 34 35 /** 36 * Add resize pulse. 37 * @returns {boolean} True, if resize mechanism could be started, else false. 38 */ 39 const addResizePulse = () => { 40 if (!window.H5P || !window.H5P.externalDispatcher) { 41 return false; // H5P not present, but should be by now 42 } 43 44 // Resize once an H5P instance is initialized and resize might be needed 45 window.H5P.externalDispatcher.once('initialized', () => { 46 scheduleResizePulse(userParams.timeout); 47 }); 48 49 return true; 50 }; 51 52 /** 53 * Add resize triggers. 54 * 55 * @param {string} selector CSS selector for elements. 56 * @returns {boolean} True, if resize mechanism could be started, else false. 57 */ 58 const addClickListeners = (selector) => { 59 if (typeof userParams.selector !== 'string') { 60 return false; // No valid selector 61 } 62 63 let triggerElements; 64 try { 65 triggerElements = Array.from(document.querySelectorAll(selector)); 66 } catch (error) { 67 triggerElements = []; 68 } 69 70 if (!triggerElements.length) { 71 return false; // No elements found 72 } 73 74 triggerElements.forEach((element) => { 75 element.addEventListener('click', () => { 76 // DOM will have to have re-rendered, so height can be determined 77 window.requestAnimationFrame(() => { 78 triggerResize(); 79 }); 80 }); 81 }); 82 83 return true; 84 }; 85 86 /** 87 * Add resize observer to each H5P iframe parent node. 88 * @returns {boolean} True, if resize mechanism could be started, else false. 89 */ 90 const addResizeObservers = () => { 91 const h5pIframes = Array.from( 92 document.body.querySelectorAll('iframe.h5p-iframe') 93 ); 94 95 if (!h5pIframes.length) { 96 return false; 97 } 98 99 h5pIframes.forEach((iframe) => { 100 (new ResizeObserver(() => { 101 triggerResize(); 102 })).observe(iframe.parentNode); 103 }); 104 105 return true; 106 }; 107 108 /** 109 * Try to start. 110 */ 111 const tryToStart = () => { 112 if (userParams.mode === 'observer') { 113 resizeMechanismStarted = addResizeObservers(); 114 } else if (userParams.mode === 'selector') { 115 resizeMechanismStarted = addClickListeners(userParams.selector); 116 } else if (userParams.mode === 'interval') { 117 resizeMechanismStarted = addResizePulse(); 118 } 119 }; 120 121 document.addEventListener('readystatechange', () => { 122 if ('interactive' === document.readyState) { 123 tryToStart(); 124 } else if ('complete' === document.readyState) { 125 // Plugin might not have rendered elements on 'interactive' 126 if (!resizeMechanismStarted) { 127 tryToStart(); 128 } 129 } 130 }); 131})();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.