Changeset 11217
- Timestamp:
- 09/08/2021 01:07:57 AM (3 years ago)
- Location:
- sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-openverse
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-openverse/functions.php
r11060 r11217 86 86 wp_add_inline_script( 87 87 /* handle */ 'openverse-message', 88 /* JS */ 'const openverseUrl = ' . json_encode( get_theme_mod( 'ov_src_url', OPENVERSE_URL ) ) . ";\n" . 89 /* JS */ 'const openverseSubpath = ' . json_encode( OPENVERSE_SUBPATH ) . ";\n", 88 /* JS */ 'const openverseUrl = ' . wp_json_encode( get_theme_mod( 'ov_src_url', OPENVERSE_URL ) ) . ";\n" . 89 /* JS */ 'const openverseSubpath = ' . wp_json_encode( OPENVERSE_SUBPATH ) . ";\n" . 90 /* JS */ 'const currentLocale = ' . wp_json_encode( get_locale() ) . ";\n", 90 91 /* position */ 'before' 91 92 ); -
sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-openverse/index.php
r11060 r11217 20 20 <main id="main" class="site-main" role="main"> 21 21 <iframe id="openverse_embed"> 22 22 😢 Your browser does not support inline frames. 23 23 </iframe> 24 24 </main><!-- #main --> -
sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-openverse/js/message.js
r11060 r11217 7 7 */ 8 8 function updateHeight(value) { 9 10 let heightProp 11 12 13 14 15 16 17 18 19 9 const height = value.height; 10 let heightProp; 11 if (height) { 12 heightProp = `${height}px`; 13 } else { 14 heightProp = '100vh'; 15 } 16 document 17 .documentElement 18 .style 19 .setProperty('--openverse-embed-height', heightProp); 20 20 } 21 21 … … 32 32 */ 33 33 function updatePath(value) { 34 35 34 const path = value.path; 35 const url = `${openverseSubpath}${path}`; // openverseSubpath defined in `index.php` 36 36 37 38 39 40 41 42 37 console.log(`Replacing state URL: ${url}`); 38 history.replaceState( 39 value.state, 40 value.title ?? 'Openverse', 41 url, 42 ); 43 43 44 if (value.title) { 45 console.log(`Setting document title: ${value.title}`); 46 document.title = value.title; 47 } 44 if (value.title) { 45 console.log(`Setting document title: ${value.title}`); 46 document.title = value.title; 47 } 48 } 49 50 /** 51 * Emit a message to the `iframe` containing information about the current 52 * locale. This function combines the locale from WordPress with attributes 53 * from the top-level HTML document. 54 */ 55 function emitLocale() { 56 const currentLang = document.documentElement.lang; 57 const currentDir = document.documentElement.dir; 58 59 const iframe = document.getElementById('openverse_embed'); 60 iframe.contentWindow.postMessage( 61 { 62 type: 'localeSet', 63 value: { 64 dir: currentDir, 65 lang: currentLang, 66 locale: currentLocale, // set in `header.php` 67 }, 68 }, 69 '*', // Bad practice, but we are not sending sensitive info 70 ); 48 71 } 49 72 … … 53 76 */ 54 77 function logUnhandled() { 55 78 console.error('No handler configured for event received'); 56 79 } 57 80 … … 65 88 * }>} message - the message object sent to this document 66 89 */ 67 function handleIframeMessages 68 69 70 71 90 function handleIframeMessages({ origin, data }) { 91 if (data.debug) { 92 console.log(`Received message from origin ${origin}:`); 93 console.log(data); 94 } 72 95 73 let handler 74 switch(data.type) { 75 case 'resize': 76 handler = updateHeight; 77 break; 78 case 'urlChange': 79 handler = updatePath; 80 break; 81 default: 82 handler = logUnhandled; 83 } 84 handler(data.value); 96 let handler; 97 switch (data.type) { 98 case 'resize': 99 handler = updateHeight; 100 break; 101 case 'urlChange': 102 handler = updatePath; 103 break; 104 case 'localeGet': 105 handler = emitLocale; 106 break; 107 default: 108 handler = logUnhandled; 109 } 110 handler(data.value); 85 111 } 86 112 -
sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-openverse/js/message_test.html
r11060 r11217 8 8 margin-top: 1em; 9 9 } 10 10 11 label { 11 12 display: inline-block; … … 20 21 * @param {any} value - the data to send along with the message 21 22 */ 22 function sendMessage (type, value) {23 function sendMessage(type, value = {}) { 23 24 if (window.parent !== window) { 24 25 const message = { debug: true, type, value }; … … 64 65 } 65 66 sendMessage('urlChange', message); 67 }); 68 69 document.forms.localeGet.addEventListener('submit', (event) => { 70 event.preventDefault(); 71 72 const listener = ({ origin, data }) => { 73 if (data.type !== 'localeSet') { 74 return; 75 } 76 77 console.log(`Received message from origin ${origin}:`); 78 document.getElementById('gotLocale').innerText = JSON.stringify(data, null, 2); 79 80 console.log('Removing listener'); 81 window.removeEventListener('message', listener); 82 }; 83 window.addEventListener('message', listener); 84 sendMessage('localeGet'); 66 85 }); 67 86 }); … … 114 133 </div> 115 134 </form> 135 136 <h2>Test locale get</h2> 137 <form name="localeGet"> 138 <div class="row"> 139 <button type="submit"> 140 Get locale 141 </button> 142 <pre><code id="gotLocale"></code></pre> 143 </div> 144 </form> 116 145 </body> 117 146 </html>
Note: See TracChangeset
for help on using the changeset viewer.