PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.3.4
Jetpack – WP Security, Backup, Speed, & Growth v7.3.4
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 / sharedaddy / admin-sharing.js

admin-sharing.js in Jetpack – WP Security, Backup, Speed, & Growth 7.3.4, at modules/sharedaddy/admin-sharing.js

533 lines 14.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /* jshint onevar: false, smarttabs: true */
2 /* global sharing_loading_icon */
3
4 ( function( $ ) {
5 $( document ).ready( function() {
6 function enable_share_button() {
7 $( '.preview a.sharing-anchor' )
8 .unbind( 'mouseenter mouseenter' )
9 .hover(
10 function() {
11 if ( $( this ).data( 'hasappeared' ) !== true ) {
12 var item = $( '.sharing-hidden .inner' );
13 var original = $( this ).parents( 'li' );
14
15 // Create a timer to make the area appear if the mouse hovers for a period
16 var timer = setTimeout( function() {
17 $( item )
18 .css( {
19 left: $( original ).position().left + 'px',
20 top: $( original ).position().top + $( original ).height() + 3 + 'px',
21 } )
22 .slideDown( 200, function() {
23 // Mark the item as have being appeared by the hover
24 $( original )
25 .data( 'hasappeared', true )
26 .data( 'hasoriginal', true )
27 .data( 'hasitem', false );
28
29 // Remove all special handlers
30 $( item )
31 .mouseleave( handler_item_leave )
32 .mouseenter( handler_item_enter );
33 $( original )
34 .mouseleave( handler_original_leave )
35 .mouseenter( handler_original_enter );
36
37 // Add a special handler to quickly close the item
38 $( original ).click( close_it );
39 } );
40
41 // The following handlers take care of the mouseenter/mouseleave for the share button and the share area - if both are left then we close the share area
42 var handler_item_leave = function() {
43 $( original ).data( 'hasitem', false );
44
45 if ( $( original ).data( 'hasoriginal' ) === false ) {
46 var timer = setTimeout( close_it, 800 );
47 $( original ).data( 'timer2', timer );
48 }
49 };
50
51 var handler_item_enter = function() {
52 $( original ).data( 'hasitem', true );
53 clearTimeout( $( original ).data( 'timer2' ) );
54 };
55
56 var handler_original_leave = function() {
57 $( original ).data( 'hasoriginal', false );
58
59 if ( $( original ).data( 'hasitem' ) === false ) {
60 var timer = setTimeout( close_it, 800 );
61 $( original ).data( 'timer2', timer );
62 }
63 };
64
65 var handler_original_enter = function() {
66 $( original ).data( 'hasoriginal', true );
67 clearTimeout( $( original ).data( 'timer2' ) );
68 };
69
70 var close_it = function() {
71 item.slideUp( 200 );
72
73 // Clear all hooks
74 $( original )
75 .unbind( 'mouseleave', handler_original_leave )
76 .unbind( 'mouseenter', handler_original_enter );
77 $( item )
78 .unbind( 'mouseleave', handler_item_leave )
79 .unbind( 'mouseenter', handler_item_leave );
80 $( original ).data( 'hasappeared', false );
81 $( original ).unbind( 'click', close_it );
82 return false;
83 };
84 }, 200 );
85
86 // Remember the timer so we can detect it on the mouseout
87 $( this ).data( 'timer', timer );
88 }
89 },
90 function() {
91 // Mouse out - remove any timer
92 clearTimeout( $( this ).data( 'timer' ) );
93 $( this ).data( 'timer', false );
94 }
95 );
96 }
97
98 function update_preview() {
99 var button_style = $( '#button_style' ).val();
100
101 // Clear the live preview
102 $( '#live-preview ul.preview li' ).remove();
103
104 // Add label
105 if (
106 $( '#save-enabled-shares input[name=visible]' ).val() ||
107 $( '#save-enabled-shares input[name=hidden]' ).val()
108 ) {
109 $( '#live-preview ul.preview' ).append(
110 $( '#live-preview ul.archive .sharing-label' ).clone()
111 );
112 }
113
114 // Re-insert all the enabled items
115 $( 'ul.services-enabled li' ).each( function() {
116 if ( $( this ).hasClass( 'service' ) ) {
117 var service = $( this ).attr( 'id' );
118 $( '#live-preview ul.preview' ).append(
119 $( '#live-preview ul.archive li.preview-' + service ).clone()
120 );
121 }
122 } );
123
124 // Add any hidden items
125 if ( $( '#save-enabled-shares input[name=hidden]' ).val() ) {
126 // Add share button
127 $( '#live-preview ul.preview' ).append(
128 $( '#live-preview ul.archive .share-more' )
129 .parent()
130 .clone()
131 );
132
133 $( '.sharing-hidden ul li' ).remove();
134
135 // Add hidden items into the inner panel
136 $( 'ul.services-hidden li' ).each( function(/*pos, item*/) {
137 if ( $( this ).hasClass( 'service' ) ) {
138 var service = $( this ).attr( 'id' );
139 $( '.sharing-hidden .inner ul' ).append(
140 $( '#live-preview ul.archive .preview-' + service ).clone()
141 );
142 }
143 } );
144
145 enable_share_button();
146 }
147
148 $( '#live-preview div.sharedaddy' ).removeClass( 'sd-social-icon' );
149 $( '#live-preview li.advanced' ).removeClass( 'no-icon' );
150
151 // Button style
152 if ( 'icon' === button_style ) {
153 $( '#live-preview ul.preview div span, .sharing-hidden .inner ul div span' )
154 .html( ' ' )
155 .parent()
156 .addClass( 'no-text' );
157 $( '#live-preview div.sharedaddy' ).addClass( 'sd-social-icon' );
158 } else if ( 'official' === button_style ) {
159 $( '#live-preview ul.preview .advanced, .sharing-hidden .inner ul .advanced' ).each(
160 function(/*i*/) {
161 if (
162 ! $( this ).hasClass( 'preview-press-this' ) &&
163 ! $( this ).hasClass( 'preview-email' ) &&
164 ! $( this ).hasClass( 'preview-print' ) &&
165 ! $( this ).hasClass( 'preview-telegram' ) &&
166 ! $( this ).hasClass( 'preview-jetpack-whatsapp' ) &&
167 ! $( this ).hasClass( 'share-custom' ) &&
168 ! $( this ).hasClass( 'share-deprecated' )
169 ) {
170 $( this )
171 .find( '.option a span' )
172 .html( '' )
173 .parent()
174 .removeClass( 'sd-button' )
175 .parent()
176 .attr( 'class', 'option option-smart-on' );
177 }
178 }
179 );
180 } else if ( 'text' === button_style ) {
181 $( '#live-preview li.advanced' ).addClass( 'no-icon' );
182 }
183 }
184
185 window.sharing_option_changed = function() {
186 var item = this;
187
188 // Loading icon
189 $( this )
190 .parents( 'li:first' )
191 .css( 'backgroundImage', 'url("' + sharing_loading_icon + '")' );
192
193 // Save
194 $( this )
195 .parents( 'form' )
196 .ajaxSubmit( function( response ) {
197 if ( response.indexOf( '<!---' ) >= 0 ) {
198 var button = response.substring( 0, response.indexOf( '<!--->' ) );
199 var preview = response.substring( response.indexOf( '<!--->' ) + 6 );
200
201 if ( $( item ).is( ':submit' ) === true ) {
202 // Update the DOM using a bit of cut/paste technology
203
204 $( item )
205 .parents( 'li:first' )
206 .replaceWith( button );
207 }
208
209 $(
210 '#live-preview ul.archive li.preview-' +
211 $( item )
212 .parents( 'form' )
213 .find( 'input[name=service]' )
214 .val()
215 ).replaceWith( preview );
216 }
217
218 // Update preview
219 update_preview();
220
221 // Restore the icon
222 $( item )
223 .parents( 'li:first' )
224 .removeAttr( 'style' );
225 } );
226
227 if ( $( item ).is( ':submit' ) === true ) {
228 return false;
229 }
230 return true;
231 };
232
233 function showExtraOptions( service ) {
234 jQuery( '.' + service + '-extra-options' )
235 .css( { backgroundColor: '#ffffcc' } )
236 .fadeIn();
237 }
238
239 function hideExtraOptions( service ) {
240 jQuery( '.' + service + '-extra-options' ).fadeOut( 'slow' );
241 }
242
243 function save_services() {
244 $( '#enabled-services h3 img' ).show();
245
246 // Toggle various dividers/help texts
247 if ( $( '#enabled-services ul.services-enabled li.service' ).length > 0 ) {
248 $( '#drag-instructions' ).hide();
249 } else {
250 $( '#drag-instructions' ).show();
251 }
252
253 if ( $( '#enabled-services li.service' ).length > 0 ) {
254 $( '#live-preview .services h2' ).hide();
255 } else {
256 $( '#live-preview .services h2' ).show();
257 }
258
259 // Gather the modules
260 var visible = [],
261 hidden = [];
262
263 $( 'ul.services-enabled li' ).each( function() {
264 if ( $( this ).hasClass( 'service' ) ) {
265 // Ready for saving
266 visible[ visible.length ] = $( this ).attr( 'id' );
267 showExtraOptions( $( this ).attr( 'id' ) );
268 }
269 } );
270
271 $( 'ul.services-available li' ).each( function() {
272 if ( $( this ).hasClass( 'service' ) ) {
273 hideExtraOptions( $( this ).attr( 'id' ) );
274 }
275 } );
276
277 $( 'ul.services-hidden li' ).each( function() {
278 if ( $( this ).hasClass( 'service' ) ) {
279 // Ready for saving
280 hidden[ hidden.length ] = $( this ).attr( 'id' );
281 showExtraOptions( $( this ).attr( 'id' ) );
282 }
283 } );
284
285 // Set the hidden element values
286 $( '#save-enabled-shares input[name=visible]' ).val( visible.join( ',' ) );
287 $( '#save-enabled-shares input[name=hidden]' ).val( hidden.join( ',' ) );
288
289 update_preview();
290
291 // Save it
292 $( '#save-enabled-shares' ).ajaxSubmit( function() {
293 $( '#enabled-services h3 img' ).hide();
294 } );
295 }
296
297 $( '#enabled-services .services ul' ).sortable( {
298 receive: function(/*event, ui*/) {
299 save_services();
300 },
301 stop: function() {
302 save_services();
303 $( 'li.service' ).enableSelection(); // Fixes a problem with Chrome
304 },
305 over: function(/*event, ui*/) {
306 $( this )
307 .find( 'ul' )
308 .addClass( 'dropping' );
309
310 // Ensure the 'end-fix' is at the end
311 $( '#enabled-services li.end-fix' ).remove();
312 $( '#enabled-services ul' ).append( '<li class="end-fix"></li>' );
313 },
314 out: function(/*event, ui*/) {
315 $( this )
316 .find( 'ul' )
317 .removeClass( 'dropping' );
318
319 // Ensure the 'end-fix' is at the end
320 $( '#enabled-services li.end-fix' ).remove();
321 $( '#enabled-services ul' ).append( '<li class="end-fix"></li>' );
322 },
323 helper: function( event, ui ) {
324 ui.find( '.advanced-form' ).hide();
325
326 return ui.clone();
327 },
328 start: function(/*event, ui*/) {
329 // Make sure that the advanced section is closed
330 $( '.advanced-form' ).hide();
331 $( 'li.service' ).disableSelection(); // Fixes a problem with Chrome
332 },
333 placeholder: 'dropzone',
334 opacity: 0.8,
335 delay: 150,
336 forcePlaceholderSize: true,
337 items: 'li',
338 connectWith: '#available-services ul, #enabled-services .services ul',
339 cancel: '.advanced-form',
340 } );
341
342 $( '#available-services ul' ).sortable( {
343 opacity: 0.8,
344 delay: 150,
345 cursor: 'move',
346 connectWith: '#enabled-services .services ul',
347 placeholder: 'dropzone',
348 forcePlaceholderSize: true,
349 start: function() {
350 $( '.advanced-form' ).hide();
351 },
352 } );
353
354 // Accessibility keyboard shortcurts
355 $( '.service' ).on( 'keydown', function( e ) {
356 // Reposition if one of the directional keys is pressed
357 switch ( e.keyCode ) {
358 case 13:
359 keyboardDragDrop( $( this ) );
360 break; // Enter
361 case 32:
362 keyboardDragDrop( $( this ) );
363 break; // Space
364 case 37:
365 keyboardChangeOrder( $( this ), 'left' );
366 break; // Left
367 case 39:
368 keyboardChangeOrder( $( this ), 'right' );
369 break; // Right
370 default:
371 return true; // Exit and bubble
372 }
373
374 e.preventDefault();
375 } );
376
377 function keyboardChangeOrder( $this, dir ) {
378 var thisParent = $this.parent(),
379 thisParentsChildren = thisParent.find( 'li' ),
380 thisPosition = thisParentsChildren.index( $this ) + 1,
381 totalChildren = thisParentsChildren.length - 1,
382 thisService;
383
384 // No need to be able to sort order for the "Available Services" section
385 if ( thisParent.hasClass( 'services-available' ) ) {
386 return;
387 }
388
389 if ( 'left' === dir ) {
390 if ( 1 === thisPosition ) {
391 return;
392 }
393
394 // Find service to left
395 var prevSibling = $this.prev();
396
397 // Detach this service from DOM
398 thisService = $this.detach();
399
400 // Move it to the appropriate area and add focus back to service
401 prevSibling.before( thisService );
402
403 // Add focus
404 prevSibling.prev().focus();
405 }
406
407 if ( 'right' === dir ) {
408 if ( thisPosition === totalChildren ) {
409 return;
410 }
411
412 // Find service to left
413 var nextSibling = $this.next();
414
415 // Detach this service from DOM
416 thisService = $this.detach();
417
418 // Move it to the appropriate area and add focus back to service
419 nextSibling.after( thisService );
420
421 // Add focus
422 nextSibling.next().focus();
423 }
424
425 //Save changes
426 save_services();
427 }
428
429 function keyboardDragDrop( $this ) {
430 var dropzone,
431 thisParent = $this.parent();
432
433 // Rotate through 3 available dropzones
434 if ( thisParent.hasClass( 'services-available' ) ) {
435 dropzone = 'services-enabled';
436 } else if ( thisParent.hasClass( 'services-enabled' ) ) {
437 dropzone = 'services-hidden';
438 } else {
439 dropzone = 'services-available';
440 }
441
442 // Detach this service from DOM
443 var thisService = $this.detach();
444
445 // Move it to the appropriate area and add focus back to service
446 $( '.' + dropzone )
447 .prepend( thisService )
448 .find( 'li:first-child' )
449 .focus();
450
451 //Save changes
452 save_services();
453 }
454
455 // Live preview 'hidden' button
456 $( '.preview-hidden a' ).click( function() {
457 $( this )
458 .parent()
459 .find( '.preview' )
460 .toggle();
461 return false;
462 } );
463
464 // Add service
465 $( '#new-service form' ).ajaxForm( {
466 beforeSubmit: function() {
467 $( '#new-service-form .error' ).hide();
468 $( '#new-service-form img' ).show();
469 $( '#new-service-form input[type=submit]' ).prop( 'disabled', true );
470 },
471 success: function( response ) {
472 $( '#new-service-form img' ).hide();
473
474 if ( '' + response === '1' ) {
475 $( '#new-service-form .inerror' )
476 .removeClass( 'inerror' )
477 .addClass( 'error' );
478 $( '#new-service-form .error' ).show();
479 $( '#new-service-form input[type=submit]' ).prop( 'disabled', false );
480 } else {
481 document.location.reload();
482 }
483 },
484 } );
485
486 function init_handlers() {
487 $( '#services-config a.remove' )
488 .unbind( 'click' )
489 .click( function() {
490 var form = $( this )
491 .parent()
492 .next();
493
494 // Loading icon
495 $( this )
496 .parents( 'li:first' )
497 .css( 'backgroundImage', 'url("' + sharing_loading_icon + '")' );
498
499 // Save
500 form.ajaxSubmit( function(/*response*/) {
501 // Remove the item
502 form.parents( 'li:first' ).fadeOut( function() {
503 $( this ).remove();
504
505 // Update preview
506 update_preview();
507 } );
508 } );
509
510 return false;
511 } );
512 }
513
514 $( '#button_style' )
515 .change( function() {
516 update_preview();
517 return true;
518 } )
519 .change();
520
521 $( 'input[name=sharing_label]' ).blur( function() {
522 $( '#live-preview h3.sd-title' ).text(
523 $( '<div/>' )
524 .text( $( this ).val() )
525 .html()
526 );
527 } );
528
529 init_handlers();
530 enable_share_button();
531 } );
532 } )( jQuery );
533