PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 9.2
Jetpack – WP Security, Backup, Speed, & Growth v9.2
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / likes / queuehandler.js

queuehandler.js in Jetpack – WP Security, Backup, Speed, & Growth 9.2, at modules/likes/queuehandler.js

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