2019-11-15 23:26:29 +01:00
/* global sharing_loading_icon */
2020-09-15 14:30:05 +02:00
( function ( $ ) {
$ ( document ) . ready ( function ( ) {
2019-11-15 23:26:29 +01:00
function enable _share _button ( ) {
$ ( '.preview a.sharing-anchor' )
. unbind ( 'mouseenter mouseenter' )
. hover (
2020-09-15 14:30:05 +02:00
function ( ) {
2019-11-15 23:26:29 +01:00
if ( $ ( this ) . data ( 'hasappeared' ) !== true ) {
var item = $ ( '.sharing-hidden .inner' ) ;
var original = $ ( this ) . parents ( 'li' ) ;
// Create a timer to make the area appear if the mouse hovers for a period
2020-09-15 14:30:05 +02:00
var timer = setTimeout ( function ( ) {
2019-11-15 23:26:29 +01:00
$ ( item )
. css ( {
left : $ ( original ) . position ( ) . left + 'px' ,
top : $ ( original ) . position ( ) . top + $ ( original ) . height ( ) + 3 + 'px' ,
} )
2020-09-15 14:30:05 +02:00
. slideDown ( 200 , function ( ) {
2019-11-15 23:26:29 +01:00
// Mark the item as have being appeared by the hover
$ ( original )
. data ( 'hasappeared' , true )
. data ( 'hasoriginal' , true )
. data ( 'hasitem' , false ) ;
// Remove all special handlers
2020-09-15 14:30:05 +02:00
$ ( item ) . mouseleave ( handler _item _leave ) . mouseenter ( handler _item _enter ) ;
2019-11-15 23:26:29 +01:00
$ ( original )
. mouseleave ( handler _original _leave )
. mouseenter ( handler _original _enter ) ;
// Add a special handler to quickly close the item
$ ( original ) . click ( close _it ) ;
} ) ;
// 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
2020-09-15 14:30:05 +02:00
var handler _item _leave = function ( ) {
2019-11-15 23:26:29 +01:00
$ ( original ) . data ( 'hasitem' , false ) ;
if ( $ ( original ) . data ( 'hasoriginal' ) === false ) {
var timer = setTimeout ( close _it , 800 ) ;
$ ( original ) . data ( 'timer2' , timer ) ;
}
} ;
2020-09-15 14:30:05 +02:00
var handler _item _enter = function ( ) {
2019-11-15 23:26:29 +01:00
$ ( original ) . data ( 'hasitem' , true ) ;
clearTimeout ( $ ( original ) . data ( 'timer2' ) ) ;
} ;
2020-09-15 14:30:05 +02:00
var handler _original _leave = function ( ) {
2019-11-15 23:26:29 +01:00
$ ( original ) . data ( 'hasoriginal' , false ) ;
if ( $ ( original ) . data ( 'hasitem' ) === false ) {
var timer = setTimeout ( close _it , 800 ) ;
$ ( original ) . data ( 'timer2' , timer ) ;
}
} ;
2020-09-15 14:30:05 +02:00
var handler _original _enter = function ( ) {
2019-11-15 23:26:29 +01:00
$ ( original ) . data ( 'hasoriginal' , true ) ;
clearTimeout ( $ ( original ) . data ( 'timer2' ) ) ;
} ;
2020-09-15 14:30:05 +02:00
var close _it = function ( ) {
2019-11-15 23:26:29 +01:00
item . slideUp ( 200 ) ;
// Clear all hooks
$ ( original )
. unbind ( 'mouseleave' , handler _original _leave )
. unbind ( 'mouseenter' , handler _original _enter ) ;
$ ( item )
. unbind ( 'mouseleave' , handler _item _leave )
. unbind ( 'mouseenter' , handler _item _leave ) ;
$ ( original ) . data ( 'hasappeared' , false ) ;
$ ( original ) . unbind ( 'click' , close _it ) ;
return false ;
} ;
} , 200 ) ;
// Remember the timer so we can detect it on the mouseout
$ ( this ) . data ( 'timer' , timer ) ;
}
} ,
2020-09-15 14:30:05 +02:00
function ( ) {
2019-11-15 23:26:29 +01:00
// Mouse out - remove any timer
clearTimeout ( $ ( this ) . data ( 'timer' ) ) ;
$ ( this ) . data ( 'timer' , false ) ;
}
) ;
}
function update _preview ( ) {
var button _style = $ ( '#button_style' ) . val ( ) ;
2023-04-26 17:39:43 +02:00
// Toggle .sd-social-official class
var sharedaddy = $ ( '.sharedaddy' ) ;
var oficialClass = 'sd-social-official' ;
if ( 'official' === button _style ) {
sharedaddy . addClass ( oficialClass ) ;
} else {
sharedaddy . removeClass ( oficialClass ) ;
}
2019-11-15 23:26:29 +01:00
// Clear the live preview
$ ( '#live-preview ul.preview li' ) . remove ( ) ;
// Add label
if (
$ ( '#save-enabled-shares input[name=visible]' ) . val ( ) ||
$ ( '#save-enabled-shares input[name=hidden]' ) . val ( )
) {
$ ( '#live-preview ul.preview' ) . append (
$ ( '#live-preview ul.archive .sharing-label' ) . clone ( )
) ;
}
// Re-insert all the enabled items
2020-09-15 14:30:05 +02:00
$ ( 'ul.services-enabled li' ) . each ( function ( ) {
2019-11-15 23:26:29 +01:00
if ( $ ( this ) . hasClass ( 'service' ) ) {
var service = $ ( this ) . attr ( 'id' ) ;
$ ( '#live-preview ul.preview' ) . append (
$ ( '#live-preview ul.archive li.preview-' + service ) . clone ( )
) ;
}
} ) ;
// Add any hidden items
if ( $ ( '#save-enabled-shares input[name=hidden]' ) . val ( ) ) {
// Add share button
$ ( '#live-preview ul.preview' ) . append (
2020-09-15 14:30:05 +02:00
$ ( '#live-preview ul.archive .share-more' ) . parent ( ) . clone ( )
2019-11-15 23:26:29 +01:00
) ;
$ ( '.sharing-hidden ul li' ) . remove ( ) ;
// Add hidden items into the inner panel
2020-09-15 14:30:05 +02:00
$ ( 'ul.services-hidden li' ) . each ( function ( /*pos, item*/ ) {
2019-11-15 23:26:29 +01:00
if ( $ ( this ) . hasClass ( 'service' ) ) {
var service = $ ( this ) . attr ( 'id' ) ;
$ ( '.sharing-hidden .inner ul' ) . append (
$ ( '#live-preview ul.archive .preview-' + service ) . clone ( )
) ;
}
} ) ;
enable _share _button ( ) ;
}
$ ( '#live-preview div.sharedaddy' ) . removeClass ( 'sd-social-icon' ) ;
$ ( '#live-preview li.advanced' ) . removeClass ( 'no-icon' ) ;
// Button style
if ( 'icon' === button _style ) {
$ ( '#live-preview ul.preview div span, .sharing-hidden .inner ul div span' )
. html ( ' ' )
. parent ( )
. addClass ( 'no-text' ) ;
$ ( '#live-preview div.sharedaddy' ) . addClass ( 'sd-social-icon' ) ;
} else if ( 'official' === button _style ) {
$ ( '#live-preview ul.preview .advanced, .sharing-hidden .inner ul .advanced' ) . each (
2020-09-15 14:30:05 +02:00
function ( /*i*/ ) {
2019-11-15 23:26:29 +01:00
if (
! $ ( this ) . hasClass ( 'preview-press-this' ) &&
! $ ( this ) . hasClass ( 'preview-email' ) &&
2023-04-26 17:39:43 +02:00
! $ ( this ) . hasClass ( 'preview-mastodon' ) &&
2019-11-15 23:26:29 +01:00
! $ ( this ) . hasClass ( 'preview-print' ) &&
! $ ( this ) . hasClass ( 'preview-telegram' ) &&
! $ ( this ) . hasClass ( 'preview-jetpack-whatsapp' ) &&
! $ ( this ) . hasClass ( 'share-custom' ) &&
! $ ( this ) . hasClass ( 'share-deprecated' )
) {
$ ( this )
. find ( '.option a span' )
. html ( '' )
. parent ( )
. removeClass ( 'sd-button' )
. parent ( )
. attr ( 'class' , 'option option-smart-on' ) ;
}
}
) ;
} else if ( 'text' === button _style ) {
$ ( '#live-preview li.advanced' ) . addClass ( 'no-icon' ) ;
}
}
2020-09-15 14:30:05 +02:00
window . sharing _option _changed = function ( ) {
2019-11-15 23:26:29 +01:00
var item = this ;
// Loading icon
$ ( this )
. parents ( 'li:first' )
. css ( 'backgroundImage' , 'url("' + sharing _loading _icon + '")' ) ;
// Save
$ ( this )
. parents ( 'form' )
2020-09-15 14:30:05 +02:00
. ajaxSubmit ( function ( response ) {
2019-11-15 23:26:29 +01:00
if ( response . indexOf ( '<!---' ) >= 0 ) {
var button = response . substring ( 0 , response . indexOf ( '<!--->' ) ) ;
var preview = response . substring ( response . indexOf ( '<!--->' ) + 6 ) ;
if ( $ ( item ) . is ( ':submit' ) === true ) {
// Update the DOM using a bit of cut/paste technology
2020-09-15 14:30:05 +02:00
$ ( item ) . parents ( 'li:first' ) . replaceWith ( button ) ;
2019-11-15 23:26:29 +01:00
}
$ (
'#live-preview ul.archive li.preview-' +
2020-09-15 14:30:05 +02:00
$ ( item ) . parents ( 'form' ) . find ( 'input[name=service]' ) . val ( )
2019-11-15 23:26:29 +01:00
) . replaceWith ( preview ) ;
}
// Update preview
update _preview ( ) ;
// Restore the icon
2020-09-15 14:30:05 +02:00
$ ( item ) . parents ( 'li:first' ) . removeAttr ( 'style' ) ;
2019-11-15 23:26:29 +01:00
} ) ;
if ( $ ( item ) . is ( ':submit' ) === true ) {
return false ;
}
return true ;
} ;
function showExtraOptions ( service ) {
jQuery ( '.' + service + '-extra-options' )
. css ( { backgroundColor : '#ffffcc' } )
. fadeIn ( ) ;
}
function hideExtraOptions ( service ) {
jQuery ( '.' + service + '-extra-options' ) . fadeOut ( 'slow' ) ;
}
function save _services ( ) {
$ ( '#enabled-services h3 img' ) . show ( ) ;
// Toggle various dividers/help texts
if ( $ ( '#enabled-services ul.services-enabled li.service' ) . length > 0 ) {
$ ( '#drag-instructions' ) . hide ( ) ;
} else {
$ ( '#drag-instructions' ) . show ( ) ;
}
if ( $ ( '#enabled-services li.service' ) . length > 0 ) {
$ ( '#live-preview .services h2' ) . hide ( ) ;
} else {
$ ( '#live-preview .services h2' ) . show ( ) ;
}
// Gather the modules
var visible = [ ] ,
hidden = [ ] ;
2020-09-15 14:30:05 +02:00
$ ( 'ul.services-enabled li' ) . each ( function ( ) {
2019-11-15 23:26:29 +01:00
if ( $ ( this ) . hasClass ( 'service' ) ) {
// Ready for saving
visible [ visible . length ] = $ ( this ) . attr ( 'id' ) ;
showExtraOptions ( $ ( this ) . attr ( 'id' ) ) ;
}
} ) ;
2020-09-15 14:30:05 +02:00
$ ( 'ul.services-available li' ) . each ( function ( ) {
2019-11-15 23:26:29 +01:00
if ( $ ( this ) . hasClass ( 'service' ) ) {
hideExtraOptions ( $ ( this ) . attr ( 'id' ) ) ;
}
} ) ;
2020-09-15 14:30:05 +02:00
$ ( 'ul.services-hidden li' ) . each ( function ( ) {
2019-11-15 23:26:29 +01:00
if ( $ ( this ) . hasClass ( 'service' ) ) {
// Ready for saving
hidden [ hidden . length ] = $ ( this ) . attr ( 'id' ) ;
showExtraOptions ( $ ( this ) . attr ( 'id' ) ) ;
}
} ) ;
// Set the hidden element values
$ ( '#save-enabled-shares input[name=visible]' ) . val ( visible . join ( ',' ) ) ;
$ ( '#save-enabled-shares input[name=hidden]' ) . val ( hidden . join ( ',' ) ) ;
update _preview ( ) ;
// Save it
2020-09-15 14:30:05 +02:00
$ ( '#save-enabled-shares' ) . ajaxSubmit ( function ( ) {
2019-11-15 23:26:29 +01:00
$ ( '#enabled-services h3 img' ) . hide ( ) ;
} ) ;
}
$ ( '#enabled-services .services ul' ) . sortable ( {
2020-09-15 14:30:05 +02:00
receive : function ( /*event, ui*/ ) {
2019-11-15 23:26:29 +01:00
save _services ( ) ;
} ,
2020-09-15 14:30:05 +02:00
stop : function ( ) {
2019-11-15 23:26:29 +01:00
save _services ( ) ;
$ ( 'li.service' ) . enableSelection ( ) ; // Fixes a problem with Chrome
} ,
2020-09-15 14:30:05 +02:00
over : function ( /*event, ui*/ ) {
$ ( this ) . find ( 'ul' ) . addClass ( 'dropping' ) ;
2019-11-15 23:26:29 +01:00
// Ensure the 'end-fix' is at the end
$ ( '#enabled-services li.end-fix' ) . remove ( ) ;
$ ( '#enabled-services ul' ) . append ( '<li class="end-fix"></li>' ) ;
} ,
2020-09-15 14:30:05 +02:00
out : function ( /*event, ui*/ ) {
$ ( this ) . find ( 'ul' ) . removeClass ( 'dropping' ) ;
2019-11-15 23:26:29 +01:00
// Ensure the 'end-fix' is at the end
$ ( '#enabled-services li.end-fix' ) . remove ( ) ;
$ ( '#enabled-services ul' ) . append ( '<li class="end-fix"></li>' ) ;
} ,
2020-09-15 14:30:05 +02:00
helper : function ( event , ui ) {
2019-11-15 23:26:29 +01:00
ui . find ( '.advanced-form' ) . hide ( ) ;
return ui . clone ( ) ;
} ,
2020-09-15 14:30:05 +02:00
start : function ( /*event, ui*/ ) {
2019-11-15 23:26:29 +01:00
// Make sure that the advanced section is closed
$ ( '.advanced-form' ) . hide ( ) ;
$ ( 'li.service' ) . disableSelection ( ) ; // Fixes a problem with Chrome
} ,
placeholder : 'dropzone' ,
opacity : 0.8 ,
delay : 150 ,
forcePlaceholderSize : true ,
items : 'li' ,
connectWith : '#available-services ul, #enabled-services .services ul' ,
cancel : '.advanced-form' ,
} ) ;
$ ( '#available-services ul' ) . sortable ( {
opacity : 0.8 ,
delay : 150 ,
cursor : 'move' ,
connectWith : '#enabled-services .services ul' ,
placeholder : 'dropzone' ,
forcePlaceholderSize : true ,
2020-09-15 14:30:05 +02:00
start : function ( ) {
2019-11-15 23:26:29 +01:00
$ ( '.advanced-form' ) . hide ( ) ;
} ,
} ) ;
// Accessibility keyboard shortcurts
2020-09-15 14:30:05 +02:00
$ ( '.service' ) . on ( 'keydown' , function ( e ) {
2019-11-15 23:26:29 +01:00
// Reposition if one of the directional keys is pressed
switch ( e . keyCode ) {
case 13 :
keyboardDragDrop ( $ ( this ) ) ;
break ; // Enter
case 32 :
keyboardDragDrop ( $ ( this ) ) ;
break ; // Space
case 37 :
keyboardChangeOrder ( $ ( this ) , 'left' ) ;
break ; // Left
case 39 :
keyboardChangeOrder ( $ ( this ) , 'right' ) ;
break ; // Right
default :
return true ; // Exit and bubble
}
e . preventDefault ( ) ;
} ) ;
function keyboardChangeOrder ( $this , dir ) {
var thisParent = $this . parent ( ) ,
thisParentsChildren = thisParent . find ( 'li' ) ,
thisPosition = thisParentsChildren . index ( $this ) + 1 ,
totalChildren = thisParentsChildren . length - 1 ,
thisService ;
// No need to be able to sort order for the "Available Services" section
if ( thisParent . hasClass ( 'services-available' ) ) {
return ;
}
if ( 'left' === dir ) {
if ( 1 === thisPosition ) {
return ;
}
// Find service to left
var prevSibling = $this . prev ( ) ;
// Detach this service from DOM
thisService = $this . detach ( ) ;
// Move it to the appropriate area and add focus back to service
prevSibling . before ( thisService ) ;
// Add focus
prevSibling . prev ( ) . focus ( ) ;
}
if ( 'right' === dir ) {
if ( thisPosition === totalChildren ) {
return ;
}
// Find service to left
var nextSibling = $this . next ( ) ;
// Detach this service from DOM
thisService = $this . detach ( ) ;
// Move it to the appropriate area and add focus back to service
nextSibling . after ( thisService ) ;
// Add focus
nextSibling . next ( ) . focus ( ) ;
}
//Save changes
save _services ( ) ;
}
function keyboardDragDrop ( $this ) {
var dropzone ,
thisParent = $this . parent ( ) ;
// Rotate through 3 available dropzones
if ( thisParent . hasClass ( 'services-available' ) ) {
dropzone = 'services-enabled' ;
} else if ( thisParent . hasClass ( 'services-enabled' ) ) {
dropzone = 'services-hidden' ;
} else {
dropzone = 'services-available' ;
}
// Detach this service from DOM
var thisService = $this . detach ( ) ;
// Move it to the appropriate area and add focus back to service
$ ( '.' + dropzone )
. prepend ( thisService )
. find ( 'li:first-child' )
. focus ( ) ;
//Save changes
save _services ( ) ;
}
// Live preview 'hidden' button
2020-09-15 14:30:05 +02:00
$ ( '.preview-hidden a' ) . click ( function ( ) {
$ ( this ) . parent ( ) . find ( '.preview' ) . toggle ( ) ;
2019-11-15 23:26:29 +01:00
return false ;
} ) ;
// Add service
$ ( '#new-service form' ) . ajaxForm ( {
2020-09-15 14:30:05 +02:00
beforeSubmit : function ( ) {
2019-11-15 23:26:29 +01:00
$ ( '#new-service-form .error' ) . hide ( ) ;
$ ( '#new-service-form img' ) . show ( ) ;
$ ( '#new-service-form input[type=submit]' ) . prop ( 'disabled' , true ) ;
} ,
2020-09-15 14:30:05 +02:00
success : function ( response ) {
2019-11-15 23:26:29 +01:00
$ ( '#new-service-form img' ) . hide ( ) ;
if ( '' + response === '1' ) {
2020-09-15 14:30:05 +02:00
$ ( '#new-service-form .inerror' ) . removeClass ( 'inerror' ) . addClass ( 'error' ) ;
2019-11-15 23:26:29 +01:00
$ ( '#new-service-form .error' ) . show ( ) ;
$ ( '#new-service-form input[type=submit]' ) . prop ( 'disabled' , false ) ;
} else {
document . location . reload ( ) ;
}
} ,
} ) ;
function init _handlers ( ) {
$ ( '#services-config a.remove' )
. unbind ( 'click' )
2020-09-15 14:30:05 +02:00
. click ( function ( ) {
var form = $ ( this ) . parent ( ) . next ( ) ;
2019-11-15 23:26:29 +01:00
// Loading icon
$ ( this )
. parents ( 'li:first' )
. css ( 'backgroundImage' , 'url("' + sharing _loading _icon + '")' ) ;
// Save
2020-09-15 14:30:05 +02:00
form . ajaxSubmit ( function ( /*response*/ ) {
2019-11-15 23:26:29 +01:00
// Remove the item
2020-09-15 14:30:05 +02:00
form . parents ( 'li:first' ) . fadeOut ( function ( ) {
2019-11-15 23:26:29 +01:00
$ ( this ) . remove ( ) ;
// Update preview
update _preview ( ) ;
} ) ;
} ) ;
return false ;
} ) ;
}
$ ( '#button_style' )
2020-09-15 14:30:05 +02:00
. change ( function ( ) {
2019-11-15 23:26:29 +01:00
update _preview ( ) ;
return true ;
} )
. change ( ) ;
2020-09-15 14:30:05 +02:00
$ ( 'input[name=sharing_label]' ) . blur ( function ( ) {
$ ( '#live-preview h3.sd-title' ) . text ( $ ( '<div/>' ) . text ( $ ( this ) . val ( ) ) . html ( ) ) ;
2019-11-15 23:26:29 +01:00
} ) ;
init _handlers ( ) ;
enable _share _button ( ) ;
} ) ;
} ) ( jQuery ) ;