queuehandler.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /* global pm, wpcom_reblog, JSON */
  2. var jetpackLikesWidgetBatch = [];
  3. var jetpackLikesMasterReady = false;
  4. // Due to performance problems on pages with a large number of widget iframes that need to be loaded,
  5. // we are limiting the processing at any instant to unloaded widgets that are currently in viewport,
  6. // plus this constant that will allow processing of widgets above and bellow the current fold.
  7. // This aim of it is to improve the UX and hide the transition from unloaded to loaded state from users.
  8. var jetpackLikesLookAhead = 2000; // pixels
  9. // Keeps track of loaded comment likes widget so we can unload them when they are scrolled out of view.
  10. var jetpackCommentLikesLoadedWidgets = [];
  11. function JetpackLikesPostMessage(message, target ) {
  12. if ( 'string' === typeof message ){
  13. try {
  14. message = JSON.parse( message );
  15. } catch(e) {
  16. return;
  17. }
  18. }
  19. pm( {
  20. target: target,
  21. type: 'likesMessage',
  22. data: message,
  23. origin: '*'
  24. } );
  25. }
  26. function JetpackLikesBatchHandler() {
  27. var requests = [];
  28. jQuery( 'div.jetpack-likes-widget-unloaded' ).each( function() {
  29. if ( jetpackLikesWidgetBatch.indexOf( this.id ) > -1 ) {
  30. return;
  31. }
  32. if ( ! jetpackIsScrolledIntoView( this ) ) {
  33. return;
  34. }
  35. jetpackLikesWidgetBatch.push( this.id );
  36. var regex = /like-(post|comment)-wrapper-(\d+)-(\d+)-(\w+)/,
  37. match = regex.exec( this.id ),
  38. info;
  39. if ( ! match || match.length !== 5 ) {
  40. return;
  41. }
  42. info = {
  43. blog_id: match[2],
  44. width: this.width
  45. };
  46. if ( 'post' === match[1] ) {
  47. info.post_id = match[3];
  48. } else if ( 'comment' === match[1] ) {
  49. info.comment_id = match[3];
  50. }
  51. info.obj_id = match[4];
  52. requests.push( info );
  53. });
  54. if ( requests.length > 0 ) {
  55. JetpackLikesPostMessage( { event: 'initialBatch', requests: requests }, window.frames['likes-master'] );
  56. }
  57. }
  58. function JetpackLikesMessageListener( event, message ) {
  59. var allowedOrigin, $container, $list, offset, rowLength, height, scrollbarWidth;
  60. if ( 'undefined' === typeof event.event ) {
  61. return;
  62. }
  63. // We only allow messages from one origin
  64. allowedOrigin = 'https://widgets.wp.com';
  65. if ( allowedOrigin !== message.origin ) {
  66. return;
  67. }
  68. switch ( event.event ) {
  69. case 'masterReady':
  70. jQuery( document ).ready( function() {
  71. jetpackLikesMasterReady = true;
  72. var stylesData = {
  73. event: 'injectStyles'
  74. },
  75. $sdTextColor = jQuery( '.sd-text-color' ),
  76. $sdLinkColor = jQuery( '.sd-link-color' );
  77. if ( jQuery( 'iframe.admin-bar-likes-widget' ).length > 0 ) {
  78. JetpackLikesPostMessage( { event: 'adminBarEnabled' }, window.frames[ 'likes-master' ] );
  79. stylesData.adminBarStyles = {
  80. background: jQuery( '#wpadminbar .quicklinks li#wp-admin-bar-wpl-like > a' ).css( 'background' ),
  81. isRtl: ( 'rtl' === jQuery( '#wpadminbar' ).css( 'direction' ) )
  82. };
  83. }
  84. if ( ! window.addEventListener ) {
  85. jQuery( '#wp-admin-bar-admin-bar-likes-widget' ).hide();
  86. }
  87. stylesData.textStyles = {
  88. color: $sdTextColor.css( 'color' ),
  89. fontFamily: $sdTextColor.css( 'font-family' ),
  90. fontSize: $sdTextColor.css( 'font-size' ),
  91. direction: $sdTextColor.css( 'direction' ),
  92. fontWeight: $sdTextColor.css( 'font-weight' ),
  93. fontStyle: $sdTextColor.css( 'font-style' ),
  94. textDecoration: $sdTextColor.css( 'text-decoration' )
  95. };
  96. stylesData.linkStyles = {
  97. color: $sdLinkColor.css( 'color' ),
  98. fontFamily: $sdLinkColor.css( 'font-family' ),
  99. fontSize: $sdLinkColor.css( 'font-size' ),
  100. textDecoration: $sdLinkColor.css( 'text-decoration' ),
  101. fontWeight: $sdLinkColor.css( 'font-weight' ),
  102. fontStyle: $sdLinkColor.css( 'font-style' )
  103. };
  104. JetpackLikesPostMessage( stylesData, window.frames[ 'likes-master' ] );
  105. JetpackLikesBatchHandler();
  106. } );
  107. break;
  108. case 'showLikeWidget':
  109. jQuery( '#' + event.id + ' .likes-widget-placeholder' ).fadeOut( 'fast' );
  110. break;
  111. case 'showCommentLikeWidget':
  112. jQuery( '#' + event.id + ' .likes-widget-placeholder' ).fadeOut( 'fast' );
  113. break;
  114. case 'killCommentLikes':
  115. // If kill switch for comment likes is enabled remove all widgets wrappers and `Loading...` placeholders.
  116. jQuery( '.jetpack-comment-likes-widget-wrapper' ).remove();
  117. break;
  118. case 'clickReblogFlair':
  119. wpcom_reblog.toggle_reblog_box_flair( event.obj_id );
  120. break;
  121. case 'showOtherGravatars':
  122. $container = jQuery( '#likes-other-gravatars' );
  123. $list = $container.find( 'ul' );
  124. $container.hide();
  125. $list.html( '' );
  126. $container.find( '.likes-text span' ).text( event.total );
  127. jQuery.each( event.likers, function( i, liker ) {
  128. var element;
  129. if ( 'http' !== liker.profile_URL.substr( 0, 4 ) ) {
  130. // We only display gravatars with http or https schema
  131. return;
  132. }
  133. element = jQuery( '<li><a><img /></a></li>' );
  134. element.addClass( liker.css_class );
  135. element.find( 'a' ).
  136. attr( {
  137. href: liker.profile_URL,
  138. rel: 'nofollow',
  139. target: '_parent'
  140. } ).
  141. addClass( 'wpl-liker' );
  142. element.find( 'img' ).
  143. attr( {
  144. src: liker.avatar_URL,
  145. alt: liker.name
  146. } ).
  147. css( {
  148. width: '30px',
  149. height: '30px',
  150. paddingRight: '3px'
  151. } );
  152. $list.append( element );
  153. } );
  154. offset = jQuery( '[name=\'' + event.parent + '\']' ).offset();
  155. $container.css( 'left', offset.left + event.position.left - 10 + 'px' );
  156. $container.css( 'top', offset.top + event.position.top - 33 + 'px' );
  157. rowLength = Math.floor( event.width / 37 );
  158. height = ( Math.ceil( event.likers.length / rowLength ) * 37 ) + 13;
  159. if ( height > 204 ) {
  160. height = 204;
  161. }
  162. $container.css( 'height', height + 'px' );
  163. $container.css( 'width', rowLength * 37 - 7 + 'px' );
  164. $list.css( 'width', rowLength * 37 + 'px' );
  165. $container.fadeIn( 'slow' );
  166. scrollbarWidth = $list[0].offsetWidth - $list[0].clientWidth;
  167. if ( scrollbarWidth > 0 ) {
  168. $container.width( $container.width() + scrollbarWidth );
  169. $list.width( $list.width() + scrollbarWidth );
  170. }
  171. }
  172. }
  173. pm.bind( 'likesMessage', JetpackLikesMessageListener );
  174. jQuery( document ).click( function( e ) {
  175. var $container = jQuery( '#likes-other-gravatars' );
  176. if ( $container.has( e.target ).length === 0 ) {
  177. $container.fadeOut( 'slow' );
  178. }
  179. });
  180. function JetpackLikesWidgetQueueHandler() {
  181. var wrapperID;
  182. if ( ! jetpackLikesMasterReady ) {
  183. setTimeout( JetpackLikesWidgetQueueHandler, 500 );
  184. return;
  185. }
  186. // Restore widgets to initial unloaded state when they are scrolled out of view.
  187. jetpackUnloadScrolledOutWidgets();
  188. var unloadedWidgetsInView = jetpackGetUnloadedWidgetsInView();
  189. if ( unloadedWidgetsInView.length > 0 ) {
  190. // Grab any unloaded widgets for a batch request
  191. JetpackLikesBatchHandler();
  192. }
  193. for ( var i=0, length = unloadedWidgetsInView.length; i <= length - 1; i++ ) {
  194. wrapperID = unloadedWidgetsInView[i].id;
  195. if ( ! wrapperID ){
  196. continue;
  197. }
  198. jetpackLoadLikeWidgetIframe( wrapperID );
  199. }
  200. }
  201. function jetpackLoadLikeWidgetIframe( wrapperID ) {
  202. var $wrapper;
  203. if ( 'undefined' === typeof wrapperID ) {
  204. return;
  205. }
  206. $wrapper = jQuery( '#' + wrapperID );
  207. $wrapper.find( 'iframe' ).remove();
  208. var placeholder = $wrapper.find( '.likes-widget-placeholder' );
  209. // Post like iframe
  210. if ( placeholder.hasClass( 'post-likes-widget-placeholder' ) ) {
  211. var postLikesFrame = document.createElement( 'iframe' );
  212. postLikesFrame['class'] = 'post-likes-widget jetpack-likes-widget';
  213. postLikesFrame.name = $wrapper.data( 'name' );
  214. postLikesFrame.src = $wrapper.data( 'src' );
  215. postLikesFrame.height = '18px';
  216. postLikesFrame.width = '200px';
  217. postLikesFrame.frameBorder = '0';
  218. postLikesFrame.scrolling = 'no';
  219. if ( $wrapper.hasClass( 'slim-likes-widget' ) ) {
  220. postLikesFrame.height = '22px';
  221. postLikesFrame.width = '68px';
  222. postLikesFrame.scrolling = 'no';
  223. } else {
  224. postLikesFrame.height = '55px';
  225. postLikesFrame.width = '100%';
  226. }
  227. placeholder.after( postLikesFrame );
  228. }
  229. // Comment like iframe
  230. if ( placeholder.hasClass( 'comment-likes-widget-placeholder' ) ) {
  231. var commentLikesFrame = document.createElement( 'iframe' );
  232. commentLikesFrame['class'] = 'comment-likes-widget-frame jetpack-likes-widget-frame';
  233. commentLikesFrame.name = $wrapper.data( 'name' );
  234. commentLikesFrame.src = $wrapper.data( 'src' );
  235. commentLikesFrame.height = '18px';
  236. commentLikesFrame.width = '100%';
  237. commentLikesFrame.frameBorder = '0';
  238. commentLikesFrame.scrolling = 'no';
  239. $wrapper.find( '.comment-like-feedback' ).after( commentLikesFrame );
  240. jetpackCommentLikesLoadedWidgets.push( commentLikesFrame );
  241. }
  242. $wrapper.removeClass( 'jetpack-likes-widget-unloaded' ).addClass( 'jetpack-likes-widget-loading' );
  243. $wrapper.find( 'iframe' ).load( function( e ) {
  244. var $iframe = jQuery( e.target );
  245. JetpackLikesPostMessage( { event: 'loadLikeWidget', name: $iframe.attr( 'name' ), width: $iframe.width() }, window.frames[ 'likes-master' ] );
  246. $wrapper.removeClass( 'jetpack-likes-widget-loading' ).addClass( 'jetpack-likes-widget-loaded' );
  247. if ( $wrapper.hasClass( 'slim-likes-widget' ) ) {
  248. $wrapper.find( 'iframe' ).Jetpack( 'resizeable' );
  249. }
  250. });
  251. }
  252. function jetpackGetUnloadedWidgetsInView() {
  253. var $unloadedWidgets = jQuery( 'div.jetpack-likes-widget-unloaded' );
  254. return $unloadedWidgets.filter( function() {
  255. return jetpackIsScrolledIntoView( this );
  256. } );
  257. }
  258. function jetpackIsScrolledIntoView( element ) {
  259. var top = element.getBoundingClientRect().top;
  260. var bottom = element.getBoundingClientRect().bottom;
  261. // Allow some slack above and bellow the fold with jetpackLikesLookAhead,
  262. // with the aim of hiding the transition from unloaded to loaded widget from users.
  263. return ( top + jetpackLikesLookAhead >= 0 ) && ( bottom <= window.innerHeight + jetpackLikesLookAhead );
  264. }
  265. function jetpackUnloadScrolledOutWidgets() {
  266. for ( var i = jetpackCommentLikesLoadedWidgets.length - 1; i >= 0; i-- ) {
  267. var currentWidgetIframe = jetpackCommentLikesLoadedWidgets[ i ];
  268. if ( ! jetpackIsScrolledIntoView( currentWidgetIframe ) ) {
  269. var $widgetWrapper = jQuery( currentWidgetIframe ).parent().parent();
  270. // Restore parent class to 'unloaded' so this widget can be picked up by queue manager again if needed.
  271. $widgetWrapper
  272. .removeClass( 'jetpack-likes-widget-loaded jetpack-likes-widget-loading' )
  273. .addClass( 'jetpack-likes-widget-unloaded' );
  274. // Bring back the loading placeholder into view.
  275. $widgetWrapper.children( '.comment-likes-widget-placeholder' ).fadeIn();
  276. // Remove it from the list of loaded widgets.
  277. jetpackCommentLikesLoadedWidgets.splice( i, 1 );
  278. // Remove comment like widget iFrame.
  279. jQuery( currentWidgetIframe ).remove();
  280. }
  281. }
  282. }
  283. var jetpackWidgetsDelayedExec = function( after, fn ) {
  284. var timer;
  285. return function() {
  286. timer && clearTimeout( timer );
  287. timer = setTimeout( fn, after );
  288. };
  289. };
  290. var jetpackOnScrollStopped = jetpackWidgetsDelayedExec( 250, JetpackLikesWidgetQueueHandler );
  291. // Load initial batch of widgets, prior to any scrolling events.
  292. JetpackLikesWidgetQueueHandler();
  293. // Add event listener to execute queue handler after scroll.
  294. window.addEventListener( 'scroll', jetpackOnScrollStopped, true );