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