2019-11-15 23:26:29 +01:00
/* global WPCOM_sharing_counts, grecaptcha */
var sharing _js _options ;
if ( sharing _js _options && sharing _js _options . counts ) {
var WPCOMSharing = {
done _urls : [ ] ,
2020-09-15 14:30:05 +02:00
get _counts : function ( ) {
2019-11-15 23:26:29 +01:00
var url , requests , id , service , service _request ;
if ( 'undefined' === typeof WPCOM _sharing _counts ) {
return ;
}
for ( url in WPCOM _sharing _counts ) {
id = WPCOM _sharing _counts [ url ] ;
if ( 'undefined' !== typeof WPCOMSharing . done _urls [ id ] ) {
continue ;
}
requests = {
// Pinterest handles share counts for both http and https
pinterest : [
window . location . protocol +
'//api.pinterest.com/v1/urls/count.json?callback=WPCOMSharing.update_pinterest_count&url=' +
encodeURIComponent ( url ) ,
] ,
// Facebook protocol summing has been shown to falsely double counts, so we only request the current URL
facebook : [
window . location . protocol +
'//graph.facebook.com/?callback=WPCOMSharing.update_facebook_count&ids=' +
encodeURIComponent ( url ) ,
] ,
} ;
for ( service in requests ) {
if ( ! jQuery ( 'a[data-shared=sharing-' + service + '-' + id + ']' ) . length ) {
continue ;
}
while ( ( service _request = requests [ service ] . pop ( ) ) ) {
jQuery . getScript ( service _request ) ;
}
if ( sharing _js _options . is _stats _active ) {
WPCOMSharing . bump _sharing _count _stat ( service ) ;
}
}
WPCOMSharing . done _urls [ id ] = true ;
}
} ,
// get the version of the url that was stored in the dom (sharing-$service-URL)
2020-09-15 14:30:05 +02:00
get _permalink : function ( url ) {
2019-11-15 23:26:29 +01:00
if ( 'https:' === window . location . protocol ) {
url = url . replace ( /^http:\/\//i , 'https://' ) ;
} else {
url = url . replace ( /^https:\/\//i , 'http://' ) ;
}
return url ;
} ,
2020-09-15 14:30:05 +02:00
update _facebook _count : function ( data ) {
2019-11-15 23:26:29 +01:00
var url , permalink ;
if ( ! data ) {
return ;
}
for ( url in data ) {
if (
! data . hasOwnProperty ( url ) ||
! data [ url ] . share ||
! data [ url ] . share . share _count
) {
continue ;
}
permalink = WPCOMSharing . get _permalink ( url ) ;
if ( ! ( permalink in WPCOM _sharing _counts ) ) {
continue ;
}
WPCOMSharing . inject _share _count (
'sharing-facebook-' + WPCOM _sharing _counts [ permalink ] ,
data [ url ] . share . share _count
) ;
}
} ,
2020-09-15 14:30:05 +02:00
update _pinterest _count : function ( data ) {
2019-11-15 23:26:29 +01:00
if ( 'undefined' !== typeof data . count && data . count * 1 > 0 ) {
WPCOMSharing . inject _share _count (
'sharing-pinterest-' + WPCOM _sharing _counts [ data . url ] ,
data . count
) ;
}
} ,
2020-09-15 14:30:05 +02:00
inject _share _count : function ( id , count ) {
2019-11-15 23:26:29 +01:00
var $share = jQuery ( 'a[data-shared=' + id + '] > span' ) ;
$share . find ( '.share-count' ) . remove ( ) ;
$share . append (
'<span class="share-count">' + WPCOMSharing . format _count ( count ) + '</span>'
) ;
} ,
2020-09-15 14:30:05 +02:00
format _count : function ( count ) {
2019-11-15 23:26:29 +01:00
if ( count < 1000 ) {
return count ;
}
if ( count >= 1000 && count < 10000 ) {
return String ( count ) . substring ( 0 , 1 ) + 'K+' ;
}
return '10K+' ;
} ,
2020-09-15 14:30:05 +02:00
bump _sharing _count _stat : function ( service ) {
2019-11-15 23:26:29 +01:00
new Image ( ) . src =
document . location . protocol +
'//pixel.wp.com/g.gif?v=wpcom-no-pv&x_sharing-count-request=' +
service +
'&r=' +
Math . random ( ) ;
} ,
} ;
}
2020-09-15 14:30:05 +02:00
( function ( $ ) {
2019-11-15 23:26:29 +01:00
var $body , $sharing _email ;
$ . fn . extend ( {
2020-09-15 14:30:05 +02:00
share _is _email : function ( ) {
2019-11-15 23:26:29 +01:00
return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i . test (
this . val ( )
) ;
} ,
} ) ;
$body = $ ( document . body ) . on ( 'post-load' , WPCOMSharing _do ) ;
2020-09-15 14:30:05 +02:00
$ ( document ) . ready ( function ( ) {
2019-11-15 23:26:29 +01:00
$sharing _email = $ ( '#sharing_email' ) ;
$body . append ( $sharing _email ) ;
WPCOMSharing _do ( ) ;
} ) ;
function WPCOMSharing _do ( ) {
var $more _sharing _buttons ;
if ( 'undefined' !== typeof WPCOMSharing ) {
WPCOMSharing . get _counts ( ) ;
}
$more _sharing _buttons = $ ( '.sharedaddy a.sharing-anchor' ) ;
2020-09-15 14:30:05 +02:00
$more _sharing _buttons . click ( function ( ) {
2019-11-15 23:26:29 +01:00
return false ;
} ) ;
2020-09-15 14:30:05 +02:00
$ ( '.sharedaddy a' ) . each ( function ( ) {
if ( $ ( this ) . attr ( 'href' ) && $ ( this ) . attr ( 'href' ) . indexOf ( 'share=' ) !== - 1 ) {
2019-11-15 23:26:29 +01:00
$ ( this ) . attr ( 'href' , $ ( this ) . attr ( 'href' ) + '&nb=1' ) ;
}
} ) ;
// Show hidden buttons
// Touchscreen device: use click.
// Non-touchscreen device: use click if not already appearing due to a hover event
2020-09-15 14:30:05 +02:00
$more _sharing _buttons . on ( 'click' , function ( ) {
2019-11-15 23:26:29 +01:00
var $more _sharing _button = $ ( this ) ,
$more _sharing _pane = $more _sharing _button . parents ( 'div:first' ) . find ( '.inner' ) ;
if ( $more _sharing _pane . is ( ':animated' ) ) {
// We're in the middle of some other event's animation
return ;
}
if ( true === $more _sharing _pane . data ( 'justSlid' ) ) {
// We just finished some other event's animation - don't process click event so that slow-to-react-clickers don't get confused
return ;
}
$sharing _email . slideUp ( 200 ) ;
$more _sharing _pane
. css ( {
left : $more _sharing _button . position ( ) . left + 'px' ,
top : $more _sharing _button . position ( ) . top + $more _sharing _button . height ( ) + 3 + 'px' ,
} )
. slideToggle ( 200 ) ;
} ) ;
if ( document . ontouchstart === undefined ) {
// Non-touchscreen device: use hover/mouseout with delay
$more _sharing _buttons . hover (
2020-09-15 14:30:05 +02:00
function ( ) {
2019-11-15 23:26:29 +01:00
var $more _sharing _button = $ ( this ) ,
$more _sharing _pane = $more _sharing _button . parents ( 'div:first' ) . find ( '.inner' ) ,
timer ;
if ( ! $more _sharing _pane . is ( ':animated' ) ) {
// Create a timer to make the area appear if the mouse hovers for a period
2020-09-15 14:30:05 +02:00
timer = setTimeout ( function ( ) {
2019-11-15 23:26:29 +01:00
var handler _item _leave ,
handler _item _enter ,
handler _original _leave ,
handler _original _enter ,
close _it ;
$sharing _email . slideUp ( 200 ) ;
$more _sharing _pane . data ( 'justSlid' , true ) ;
$more _sharing _pane
. css ( {
left : $more _sharing _button . position ( ) . left + 'px' ,
top :
$more _sharing _button . position ( ) . top + $more _sharing _button . 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
$more _sharing _button . data ( 'hasoriginal' , true ) . data ( 'hasitem' , false ) ;
2020-09-15 14:30:05 +02:00
setTimeout ( function ( ) {
2019-11-15 23:26:29 +01:00
$more _sharing _pane . data ( 'justSlid' , false ) ;
} , 300 ) ;
$more _sharing _pane
. mouseleave ( handler _item _leave )
. mouseenter ( handler _item _enter ) ;
$more _sharing _button
. mouseleave ( handler _original _leave )
. mouseenter ( handler _original _enter ) ;
} ) ;
// 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
handler _item _leave = function ( ) {
2019-11-15 23:26:29 +01:00
$more _sharing _button . data ( 'hasitem' , false ) ;
if ( $more _sharing _button . data ( 'hasoriginal' ) === false ) {
var timer = setTimeout ( close _it , 800 ) ;
$more _sharing _button . data ( 'timer2' , timer ) ;
}
} ;
2020-09-15 14:30:05 +02:00
handler _item _enter = function ( ) {
2019-11-15 23:26:29 +01:00
$more _sharing _button . data ( 'hasitem' , true ) ;
clearTimeout ( $more _sharing _button . data ( 'timer2' ) ) ;
} ;
2020-09-15 14:30:05 +02:00
handler _original _leave = function ( ) {
2019-11-15 23:26:29 +01:00
$more _sharing _button . data ( 'hasoriginal' , false ) ;
if ( $more _sharing _button . data ( 'hasitem' ) === false ) {
var timer = setTimeout ( close _it , 800 ) ;
$more _sharing _button . data ( 'timer2' , timer ) ;
}
} ;
2020-09-15 14:30:05 +02:00
handler _original _enter = function ( ) {
2019-11-15 23:26:29 +01:00
$more _sharing _button . data ( 'hasoriginal' , true ) ;
clearTimeout ( $more _sharing _button . data ( 'timer2' ) ) ;
} ;
2020-09-15 14:30:05 +02:00
close _it = function ( ) {
2019-11-15 23:26:29 +01:00
$more _sharing _pane . data ( 'justSlid' , true ) ;
2020-09-15 14:30:05 +02:00
$more _sharing _pane . slideUp ( 200 , function ( ) {
setTimeout ( function ( ) {
2019-11-15 23:26:29 +01:00
$more _sharing _pane . data ( 'justSlid' , false ) ;
} , 300 ) ;
} ) ;
// Clear all hooks
$more _sharing _button
. unbind ( 'mouseleave' , handler _original _leave )
. unbind ( 'mouseenter' , handler _original _enter ) ;
$more _sharing _pane
. unbind ( 'mouseleave' , handler _item _leave )
. unbind ( 'mouseenter' , handler _item _leave ) ;
return false ;
} ;
} , 200 ) ;
// Remember the timer so we can detect it on the mouseout
$more _sharing _button . 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
2020-09-15 14:30:05 +02:00
$more _sharing _buttons . each ( function ( ) {
2019-11-15 23:26:29 +01:00
clearTimeout ( $ ( this ) . data ( 'timer' ) ) ;
} ) ;
$more _sharing _buttons . data ( 'timer' , false ) ;
}
) ;
} else {
$ ( document . body ) . addClass ( 'jp-sharing-input-touch' ) ;
}
2020-09-15 14:30:05 +02:00
$ ( document ) . click ( function ( ) {
2019-11-15 23:26:29 +01:00
// Click outside
// remove any timer
2020-09-15 14:30:05 +02:00
$more _sharing _buttons . each ( function ( ) {
2019-11-15 23:26:29 +01:00
clearTimeout ( $ ( this ) . data ( 'timer' ) ) ;
} ) ;
$more _sharing _buttons . data ( 'timer' , false ) ;
// slide down forcibly
$ ( '.sharedaddy .inner' ) . slideUp ( ) ;
} ) ;
// Add click functionality
2020-09-15 14:30:05 +02:00
$ ( '.sharedaddy ul' ) . each ( function ( ) {
2019-11-15 23:26:29 +01:00
if ( 'yep' === $ ( this ) . data ( 'has-click-events' ) ) {
return ;
}
$ ( this ) . data ( 'has-click-events' , 'yep' ) ;
2020-09-15 14:30:05 +02:00
var printUrl = function ( uniqueId , urlToPrint ) {
2019-11-15 23:26:29 +01:00
$ ( 'body:first' ) . append (
'<iframe style="position:fixed;top:100;left:100;height:1px;width:1px;border:none;" id="printFrame-' +
uniqueId +
'" name="printFrame-' +
uniqueId +
'" src="' +
urlToPrint +
'" onload="frames[\'printFrame-' +
uniqueId +
"'].focus();frames['printFrame-" +
uniqueId +
'\'].print();"></iframe>'
) ;
} ;
// Print button
$ ( this )
. find ( 'a.share-print' )
2020-09-15 14:30:05 +02:00
. click ( function ( ) {
2019-11-15 23:26:29 +01:00
var ref = $ ( this ) . attr ( 'href' ) ,
2020-09-15 14:30:05 +02:00
do _print = function ( ) {
2019-11-15 23:26:29 +01:00
if ( ref . indexOf ( '#print' ) === - 1 ) {
var uid = new Date ( ) . getTime ( ) ;
printUrl ( uid , ref ) ;
} else {
print ( ) ;
}
} ;
// Is the button in a dropdown?
if ( $ ( this ) . parents ( '.sharing-hidden' ) . length > 0 ) {
$ ( this )
. parents ( '.inner' )
2020-09-15 14:30:05 +02:00
. slideUp ( 0 , function ( ) {
2019-11-15 23:26:29 +01:00
do _print ( ) ;
} ) ;
} else {
do _print ( ) ;
}
return false ;
} ) ;
// Press This button
$ ( this )
. find ( 'a.share-press-this' )
2020-09-15 14:30:05 +02:00
. click ( function ( ) {
2019-11-15 23:26:29 +01:00
var s = '' ;
if ( window . getSelection ) {
s = window . getSelection ( ) ;
} else if ( document . getSelection ) {
s = document . getSelection ( ) ;
} else if ( document . selection ) {
s = document . selection . createRange ( ) . text ;
}
if ( s ) {
$ ( this ) . attr ( 'href' , $ ( this ) . attr ( 'href' ) + '&sel=' + encodeURI ( s ) ) ;
}
if (
! window . open (
$ ( this ) . attr ( 'href' ) ,
't' ,
'toolbar=0,resizable=1,scrollbars=1,status=1,width=720,height=570'
)
) {
document . location . href = $ ( this ) . attr ( 'href' ) ;
}
return false ;
} ) ;
// Email button
2020-09-15 14:30:05 +02:00
$ ( 'a.share-email' , this ) . on ( 'click' , function ( ) {
2019-11-15 23:26:29 +01:00
var url = $ ( this ) . attr ( 'href' ) ;
var currentDomain = window . location . protocol + '//' + window . location . hostname + '/' ;
if ( url . indexOf ( currentDomain ) !== 0 ) {
return true ;
}
if ( $sharing _email . is ( ':visible' ) ) {
$sharing _email . slideUp ( 200 ) ;
} else {
$ ( '.sharedaddy .inner' ) . slideUp ( ) ;
$ ( '#sharing_email .response' ) . remove ( ) ;
$ ( '#sharing_email form' ) . show ( ) ;
$ ( '#sharing_email form input[type=submit]' ) . removeAttr ( 'disabled' ) ;
$ ( '#sharing_email form a.sharing_cancel' ) . show ( ) ;
// Reset reCATPCHA if exists.
if (
'object' === typeof grecaptcha &&
'function' === typeof grecaptcha . reset &&
window . _ _ _grecaptcha _cfg . count
) {
grecaptcha . reset ( ) ;
}
// Show dialog
$sharing _email
. css ( {
left : $ ( this ) . offset ( ) . left + 'px' ,
top : $ ( this ) . offset ( ) . top + $ ( this ) . height ( ) + 'px' ,
} )
. slideDown ( 200 ) ;
// Hook up other buttons
$ ( '#sharing_email a.sharing_cancel' )
. unbind ( 'click' )
2020-09-15 14:30:05 +02:00
. click ( function ( ) {
2019-11-15 23:26:29 +01:00
$ ( '#sharing_email .errors' ) . hide ( ) ;
$sharing _email . slideUp ( 200 ) ;
$ ( '#sharing_background' ) . fadeOut ( ) ;
return false ;
} ) ;
// Submit validation
$ ( '#sharing_email input[type=submit]' )
. unbind ( 'click' )
2020-09-15 14:30:05 +02:00
. click ( function ( ) {
2019-11-15 23:26:29 +01:00
var form = $ ( this ) . parents ( 'form' ) ;
var source _email _input = form . find ( 'input[name=source_email]' ) ;
var target _email _input = form . find ( 'input[name=target_email]' ) ;
// Disable buttons + enable loading icon
$ ( this ) . prop ( 'disabled' , true ) ;
form . find ( 'a.sharing_cancel' ) . hide ( ) ;
form . find ( 'img.loading' ) . show ( ) ;
$ ( '#sharing_email .errors' ) . hide ( ) ;
$ ( '#sharing_email .error' ) . removeClass ( 'error' ) ;
if ( ! source _email _input . share _is _email ( ) ) {
source _email _input . addClass ( 'error' ) ;
}
if ( ! target _email _input . share _is _email ( ) ) {
target _email _input . addClass ( 'error' ) ;
}
if ( $ ( '#sharing_email .error' ) . length === 0 ) {
// AJAX send the form
$ . ajax ( {
url : url ,
type : 'POST' ,
data : form . serialize ( ) ,
2020-09-15 14:30:05 +02:00
success : function ( response ) {
2019-11-15 23:26:29 +01:00
form . find ( 'img.loading' ) . hide ( ) ;
if ( response === '1' || response === '2' || response === '3' ) {
$ ( '#sharing_email .errors-' + response ) . show ( ) ;
form . find ( 'input[type=submit]' ) . removeAttr ( 'disabled' ) ;
form . find ( 'a.sharing_cancel' ) . show ( ) ;
if (
'object' === typeof grecaptcha &&
'function' === typeof grecaptcha . reset
) {
grecaptcha . reset ( ) ;
}
} else {
$ ( '#sharing_email form' ) . hide ( ) ;
$sharing _email . append ( response ) ;
2020-09-15 14:30:05 +02:00
$ ( '#sharing_email a.sharing_cancel' ) . click ( function ( ) {
2019-11-15 23:26:29 +01:00
$sharing _email . slideUp ( 200 ) ;
$ ( '#sharing_background' ) . fadeOut ( ) ;
return false ;
} ) ;
}
} ,
} ) ;
return false ;
}
form . find ( 'img.loading' ) . hide ( ) ;
form . find ( 'input[type=submit]' ) . removeAttr ( 'disabled' ) ;
form . find ( 'a.sharing_cancel' ) . show ( ) ;
$ ( '#sharing_email .errors-1' ) . show ( ) ;
return false ;
} ) ;
}
return false ;
} ) ;
} ) ;
$ ( 'li.share-email, li.share-custom a.sharing-anchor' ) . addClass ( 'share-service-visible' ) ;
}
} ) ( jQuery ) ;