wpgroho.js
| 1 | /* global WPGroHo:true, Gravatar */ |
| 2 | ( function () { |
| 3 | var extend = function ( out ) { |
| 4 | out = out || {}; |
| 5 | |
| 6 | for ( var i = 1; i < arguments.length; i++ ) { |
| 7 | if ( ! arguments[ i ] ) { |
| 8 | continue; |
| 9 | } |
| 10 | |
| 11 | for ( var key in arguments[ i ] ) { |
| 12 | if ( Object.hasOwn( arguments[ i ], key ) ) { |
| 13 | out[ key ] = arguments[ i ][ key ]; |
| 14 | } |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | return out; |
| 19 | }; |
| 20 | |
| 21 | WPGroHo = extend( |
| 22 | { |
| 23 | my_hash: '', |
| 24 | data: {}, |
| 25 | renderers: {}, |
| 26 | syncProfileData: function ( hash, id ) { |
| 27 | var hashElements; |
| 28 | |
| 29 | if ( ! WPGroHo.data[ hash ] ) { |
| 30 | WPGroHo.data[ hash ] = {}; |
| 31 | hashElements = document.querySelectorAll( 'div.grofile-hash-map-' + hash + ' span' ); |
| 32 | for ( var i = 0; i < hashElements.length; i++ ) { |
| 33 | WPGroHo.data[ hash ][ hashElements[ i ].className ] = hashElements[ i ].innerText; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | WPGroHo.appendProfileData( WPGroHo.data[ hash ], hash, id ); |
| 38 | }, |
| 39 | appendProfileData: function ( data, hash, id ) { |
| 40 | for ( var key in data ) { |
| 41 | if ( 'function' === typeof WPGroHo.renderers[ key ] ) { |
| 42 | return WPGroHo.renderers[ key ]( data[ key ], hash, id, key ); |
| 43 | } |
| 44 | |
| 45 | var card = document.getElementById( id ); |
| 46 | if ( card ) { |
| 47 | var heading = card.querySelector( 'h4' ); |
| 48 | if ( heading ) { |
| 49 | var extra = document.createElement( 'p' ); |
| 50 | extra.className = 'grav-extra ' + key; |
| 51 | extra.innerHTML = data[ key ]; |
| 52 | |
| 53 | heading.insertAdjacentElement( 'afterend', extra ); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | }, |
| 58 | }, |
| 59 | WPGroHo || {} |
| 60 | ); |
| 61 | |
| 62 | var jetpackHovercardsInit = function () { |
| 63 | if ( 'undefined' === typeof Gravatar ) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | Gravatar.profile_cb = function ( h, d ) { |
| 68 | WPGroHo.syncProfileData( h, d ); |
| 69 | }; |
| 70 | |
| 71 | Gravatar.my_hash = WPGroHo.my_hash; |
| 72 | Gravatar.init( 'body', '#wpadminbar' ); |
| 73 | }; |
| 74 | |
| 75 | if ( document.readyState === 'interactive' || document.readyState === 'complete' ) { |
| 76 | jetpackHovercardsInit(); |
| 77 | } else { |
| 78 | document.addEventListener( 'DOMContentLoaded', jetpackHovercardsInit ); |
| 79 | } |
| 80 | } )(); |
| 81 |