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