Нема описа

queuehandler.js 11KB

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