| 1 |
/* global WPCOM_sharing_counts, grecaptcha */ |
| 2 |
|
| 3 |
// NOTE: This file intentionally does not make use of polyfills or libraries, |
| 4 |
// including jQuery. Please keep all code as IE11-compatible vanilla ES5, and |
| 5 |
// ensure everything is inside an IIFE to avoid global namespace pollution. |
| 6 |
// Code follows WordPress browser support guidelines. For an up to date list, |
| 7 |
// see https://make.wordpress.org/core/handbook/best-practices/browser-support/ |
| 8 |
|
| 9 |
( function () { |
| 10 |
var currentScript = document.currentScript; |
| 11 |
var recaptchaScriptAdded = false; |
| 12 |
|
| 13 |
// -------------------------- UTILITY FUNCTIONS -------------------------- // |
| 14 |
|
| 15 |
// Helper function to load an external script. |
| 16 |
function loadScript( url ) { |
| 17 |
var script = document.createElement( 'script' ); |
| 18 |
var prev = currentScript || document.getElementsByTagName( 'script' )[ 0 ]; |
| 19 |
script.setAttribute( 'async', true ); |
| 20 |
script.setAttribute( 'src', url ); |
| 21 |
prev.parentNode.insertBefore( script, prev ); |
| 22 |
} |
| 23 |
|
| 24 |
// Helper matches function (not a polyfill), compatible with IE 11. |
| 25 |
function matches( el, sel ) { |
| 26 |
if ( Element.prototype.matches ) { |
| 27 |
return el.matches( sel ); |
| 28 |
} |
| 29 |
|
| 30 |
if ( Element.prototype.msMatchesSelector ) { |
| 31 |
return el.msMatchesSelector( sel ); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
// Helper closest parent node function (not a polyfill) based on |
| 36 |
// https://developer.mozilla.org/en-US/docs/Web/API/Element/closest#Polyfill |
| 37 |
function closest( el, sel ) { |
| 38 |
if ( el.closest ) { |
| 39 |
return el.closest( sel ); |
| 40 |
} |
| 41 |
|
| 42 |
var current = el; |
| 43 |
|
| 44 |
do { |
| 45 |
if ( matches( current, sel ) ) { |
| 46 |
return current; |
| 47 |
} |
| 48 |
current = current.parentElement || current.parentNode; |
| 49 |
} while ( current !== null && current.nodeType === 1 ); |
| 50 |
|
| 51 |
return null; |
| 52 |
} |
| 53 |
|
| 54 |
// Helper function to iterate over a NodeList |
| 55 |
// (since IE 11 doesn't have NodeList.prototype.forEach) |
| 56 |
function forEachNode( list, fn ) { |
| 57 |
for ( var i = 0; i < list.length; i++ ) { |
| 58 |
var node = list[ i ]; |
| 59 |
fn( node, i, list ); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
// Helper function to remove a node from the DOM. |
| 64 |
function removeNode( node ) { |
| 65 |
if ( node && node.parentNode ) { |
| 66 |
node.parentNode.removeChild( node ); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
// Helper functions to show/hide a node, and check its status. |
| 71 |
function hideNode( node ) { |
| 72 |
if ( node ) { |
| 73 |
node.style.display = 'none'; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
function showNode( node ) { |
| 78 |
if ( node ) { |
| 79 |
node.style.removeProperty( 'display' ); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
function isNodeHidden( node ) { |
| 84 |
return ! node || node.style.display === 'none'; |
| 85 |
} |
| 86 |
|
| 87 |
// ------------------------------- CLASSES ------------------------------- // |
| 88 |
|
| 89 |
var PANE_SELECTOR = '.sharing-hidden .inner'; |
| 90 |
var PANE_DATA_ATTR = 'data-sharing-more-button-id'; |
| 91 |
|
| 92 |
// Implements a MoreButton class, which controls the lifecycle and behavior |
| 93 |
// of a "more" button and its dialog. |
| 94 |
function MoreButton( buttonEl ) { |
| 95 |
this.button = buttonEl; |
| 96 |
this.pane = closest( buttonEl, 'div' ).querySelector( PANE_SELECTOR ); |
| 97 |
this.openedBy = null; |
| 98 |
this.recentlyOpenedByHover = false; |
| 99 |
|
| 100 |
MoreButton.instances.push( this ); |
| 101 |
this.pane.setAttribute( PANE_DATA_ATTR, MoreButton.instances.length - 1 ); |
| 102 |
|
| 103 |
this.attachHandlers(); |
| 104 |
} |
| 105 |
|
| 106 |
// Keep a reference to each instance, so we can get back to it from the DOM. |
| 107 |
MoreButton.instances = []; |
| 108 |
|
| 109 |
// Delay time configs. |
| 110 |
MoreButton.hoverOpenDelay = 200; |
| 111 |
MoreButton.recentOpenDelay = 400; |
| 112 |
MoreButton.hoverCloseDelay = 300; |
| 113 |
|
| 114 |
// Use this to avoid creating new instances for buttons which already have one. |
| 115 |
MoreButton.instantiateOrReuse = function ( buttonEl ) { |
| 116 |
var pane = closest( buttonEl, 'div' ).querySelector( PANE_SELECTOR ); |
| 117 |
var paneId = pane && pane.getAttribute( PANE_DATA_ATTR ); |
| 118 |
|
| 119 |
var existingInstance = MoreButton.instances[ paneId ]; |
| 120 |
if ( existingInstance ) { |
| 121 |
return existingInstance; |
| 122 |
} |
| 123 |
|
| 124 |
return new MoreButton( buttonEl ); |
| 125 |
}; |
| 126 |
|
| 127 |
// Retrieve a button instance from the pane DOM element. |
| 128 |
MoreButton.getButtonInstanceFromPane = function ( paneEl ) { |
| 129 |
var paneId = paneEl && paneEl.getAttribute( PANE_DATA_ATTR ); |
| 130 |
return MoreButton.instances[ paneId ]; |
| 131 |
}; |
| 132 |
|
| 133 |
// Close all open More Button dialogs. |
| 134 |
MoreButton.closeAll = function () { |
| 135 |
for ( var i = 0; i < MoreButton.instances.length; i++ ) { |
| 136 |
MoreButton.instances[ i ].close(); |
| 137 |
} |
| 138 |
}; |
| 139 |
|
| 140 |
MoreButton.prototype.open = function () { |
| 141 |
var offset; |
| 142 |
var offsetParent; |
| 143 |
var parentOffset = [ 0, 0 ]; |
| 144 |
|
| 145 |
function getOffsets( el ) { |
| 146 |
var rect = el.getBoundingClientRect(); |
| 147 |
return [ |
| 148 |
rect.left + ( window.scrollX || window.pageXOffset || 0 ), |
| 149 |
rect.top + ( window.scrollY || window.pageYOffset || 0 ), |
| 150 |
]; |
| 151 |
} |
| 152 |
|
| 153 |
function getStyleValue( el, prop ) { |
| 154 |
return parseInt( getComputedStyle( el ).getPropertyValue( prop ) || 0 ); |
| 155 |
} |
| 156 |
|
| 157 |
offset = getOffsets( this.button ); |
| 158 |
offsetParent = this.button.offsetParent || document.documentElement; |
| 159 |
|
| 160 |
while ( |
| 161 |
offsetParent && |
| 162 |
( offsetParent === document.body || offsetParent === document.documentElement ) && |
| 163 |
getComputedStyle( offsetParent ).getPropertyValue( 'position' ) === 'static' |
| 164 |
) { |
| 165 |
offsetParent = offsetParent.parentNode; |
| 166 |
} |
| 167 |
|
| 168 |
if ( offsetParent && offsetParent !== this.button && offsetParent.nodeType === 1 ) { |
| 169 |
parentOffset = getOffsets( offsetParent ); |
| 170 |
parentOffset = [ |
| 171 |
parentOffset[ 0 ] + getStyleValue( offsetParent, 'border-left-width' ), |
| 172 |
parentOffset[ 1 ] + getStyleValue( offsetParent, 'border-top-width' ), |
| 173 |
]; |
| 174 |
} |
| 175 |
|
| 176 |
var positionLeft = |
| 177 |
offset[ 0 ] - parentOffset[ 0 ] - getStyleValue( this.button, 'margin-left' ); |
| 178 |
var positionTop = offset[ 1 ] - parentOffset[ 1 ] - getStyleValue( this.button, 'margin-top' ); |
| 179 |
|
| 180 |
this.pane.style.left = positionLeft + 'px'; |
| 181 |
this.pane.style.top = positionTop + this.button.offsetHeight + 3 + 'px'; |
| 182 |
|
| 183 |
showNode( this.pane ); |
| 184 |
}; |
| 185 |
|
| 186 |
MoreButton.prototype.close = function () { |
| 187 |
hideNode( this.pane ); |
| 188 |
this.openedBy = null; |
| 189 |
}; |
| 190 |
|
| 191 |
MoreButton.prototype.toggle = function () { |
| 192 |
if ( isNodeHidden( this.pane ) ) { |
| 193 |
this.open(); |
| 194 |
} else { |
| 195 |
this.close(); |
| 196 |
} |
| 197 |
}; |
| 198 |
|
| 199 |
MoreButton.prototype.resetCloseTimer = function () { |
| 200 |
clearTimeout( this.closeTimer ); |
| 201 |
this.closeTimer = setTimeout( this.close.bind( this ), MoreButton.hoverCloseDelay ); |
| 202 |
}; |
| 203 |
|
| 204 |
MoreButton.prototype.attachHandlers = function () { |
| 205 |
this.buttonClick = function ( event ) { |
| 206 |
event.preventDefault(); |
| 207 |
event.stopPropagation(); |
| 208 |
|
| 209 |
this.openedBy = 'click'; |
| 210 |
clearTimeout( this.openTimer ); |
| 211 |
clearTimeout( this.closeTimer ); |
| 212 |
|
| 213 |
closeEmailDialog(); |
| 214 |
|
| 215 |
if ( this.recentlyOpenedByHover ) { |
| 216 |
this.recentlyOpenedByHover = false; |
| 217 |
clearTimeout( this.hoverOpenTimer ); |
| 218 |
this.open(); |
| 219 |
} else { |
| 220 |
this.toggle(); |
| 221 |
} |
| 222 |
}.bind( this ); |
| 223 |
|
| 224 |
this.buttonEnter = function () { |
| 225 |
if ( ! this.openedBy ) { |
| 226 |
this.openTimer = setTimeout( |
| 227 |
function () { |
| 228 |
closeEmailDialog(); |
| 229 |
this.open(); |
| 230 |
this.openedBy = 'hover'; |
| 231 |
this.recentlyOpenedByHover = true; |
| 232 |
this.hoverOpenTimer = setTimeout( |
| 233 |
function () { |
| 234 |
this.recentlyOpenedByHover = false; |
| 235 |
}.bind( this ), |
| 236 |
MoreButton.recentOpenDelay |
| 237 |
); |
| 238 |
}.bind( this ), |
| 239 |
MoreButton.hoverOpenDelay |
| 240 |
); |
| 241 |
} |
| 242 |
clearTimeout( this.closeTimer ); |
| 243 |
}.bind( this ); |
| 244 |
|
| 245 |
this.buttonLeave = function () { |
| 246 |
if ( this.openedBy === 'hover' ) { |
| 247 |
this.resetCloseTimer(); |
| 248 |
} |
| 249 |
clearTimeout( this.openTimer ); |
| 250 |
}.bind( this ); |
| 251 |
|
| 252 |
this.paneEnter = function () { |
| 253 |
clearTimeout( this.closeTimer ); |
| 254 |
}.bind( this ); |
| 255 |
|
| 256 |
this.paneLeave = function () { |
| 257 |
if ( this.openedBy === 'hover' ) { |
| 258 |
this.resetCloseTimer(); |
| 259 |
} |
| 260 |
}.bind( this ); |
| 261 |
|
| 262 |
this.documentClick = function () { |
| 263 |
this.close(); |
| 264 |
}.bind( this ); |
| 265 |
|
| 266 |
this.button.addEventListener( 'click', this.buttonClick ); |
| 267 |
document.addEventListener( 'click', this.documentClick ); |
| 268 |
|
| 269 |
if ( document.ontouchstart === undefined ) { |
| 270 |
// Non-touchscreen device: use hover/mouseout with delay |
| 271 |
this.button.addEventListener( 'mouseenter', this.buttonEnter ); |
| 272 |
this.button.addEventListener( 'mouseleave', this.buttonLeave ); |
| 273 |
this.pane.addEventListener( 'mouseenter', this.paneEnter ); |
| 274 |
this.pane.addEventListener( 'mouseleave', this.paneLeave ); |
| 275 |
} |
| 276 |
}; |
| 277 |
|
| 278 |
// ---------------------------- SHARE COUNTS ---------------------------- // |
| 279 |
|
| 280 |
if ( window.sharing_js_options && window.sharing_js_options.counts ) { |
| 281 |
var WPCOMSharing = { |
| 282 |
done_urls: [], |
| 283 |
get_counts: function () { |
| 284 |
var url, requests, id, service, service_request; |
| 285 |
|
| 286 |
if ( 'undefined' === typeof WPCOM_sharing_counts ) { |
| 287 |
return; |
| 288 |
} |
| 289 |
|
| 290 |
for ( url in WPCOM_sharing_counts ) { |
| 291 |
id = WPCOM_sharing_counts[ url ]; |
| 292 |
|
| 293 |
if ( 'undefined' !== typeof WPCOMSharing.done_urls[ id ] ) { |
| 294 |
continue; |
| 295 |
} |
| 296 |
|
| 297 |
requests = { |
| 298 |
// Pinterest handles share counts for both http and https |
| 299 |
pinterest: [ |
| 300 |
window.location.protocol + |
| 301 |
'//api.pinterest.com/v1/urls/count.json?callback=WPCOMSharing.update_pinterest_count&url=' + |
| 302 |
encodeURIComponent( url ), |
| 303 |
], |
| 304 |
// Facebook protocol summing has been shown to falsely double counts, so we only request the current URL |
| 305 |
facebook: [ |
| 306 |
window.location.protocol + |
| 307 |
'//graph.facebook.com/?callback=WPCOMSharing.update_facebook_count&ids=' + |
| 308 |
encodeURIComponent( url ), |
| 309 |
], |
| 310 |
}; |
| 311 |
|
| 312 |
for ( service in requests ) { |
| 313 |
if ( ! document.querySelector( 'a[data-shared=sharing-' + service + '-' + id + ']' ) ) { |
| 314 |
continue; |
| 315 |
} |
| 316 |
|
| 317 |
while ( ( service_request = requests[ service ].pop() ) ) { |
| 318 |
loadScript( service_request ); |
| 319 |
} |
| 320 |
|
| 321 |
if ( window.sharing_js_options.is_stats_active ) { |
| 322 |
WPCOMSharing.bump_sharing_count_stat( service ); |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
WPCOMSharing.done_urls[ id ] = true; |
| 327 |
} |
| 328 |
}, |
| 329 |
|
| 330 |
// get the version of the url that was stored in the dom |
| 331 |
get_permalink: function ( url ) { |
| 332 |
if ( 'https:' === window.location.protocol ) { |
| 333 |
url = url.replace( /^http:\/\//i, 'https://' ); |
| 334 |
} else { |
| 335 |
url = url.replace( /^https:\/\//i, 'http://' ); |
| 336 |
} |
| 337 |
|
| 338 |
return url; |
| 339 |
}, |
| 340 |
update_facebook_count: function ( data ) { |
| 341 |
var url, permalink; |
| 342 |
|
| 343 |
if ( ! data ) { |
| 344 |
return; |
| 345 |
} |
| 346 |
|
| 347 |
for ( url in data ) { |
| 348 |
if ( |
| 349 |
! Object.prototype.hasOwnProperty.call( data, url ) || |
| 350 |
! data[ url ].share || |
| 351 |
! data[ url ].share.share_count |
| 352 |
) { |
| 353 |
continue; |
| 354 |
} |
| 355 |
|
| 356 |
permalink = WPCOMSharing.get_permalink( url ); |
| 357 |
|
| 358 |
if ( ! ( permalink in WPCOM_sharing_counts ) ) { |
| 359 |
continue; |
| 360 |
} |
| 361 |
|
| 362 |
WPCOMSharing.inject_share_count( |
| 363 |
'sharing-facebook-' + WPCOM_sharing_counts[ permalink ], |
| 364 |
data[ url ].share.share_count |
| 365 |
); |
| 366 |
} |
| 367 |
}, |
| 368 |
update_pinterest_count: function ( data ) { |
| 369 |
if ( 'undefined' !== typeof data.count && data.count * 1 > 0 ) { |
| 370 |
WPCOMSharing.inject_share_count( |
| 371 |
'sharing-pinterest-' + WPCOM_sharing_counts[ data.url ], |
| 372 |
data.count |
| 373 |
); |
| 374 |
} |
| 375 |
}, |
| 376 |
inject_share_count: function ( id, count ) { |
| 377 |
forEachNode( document.querySelectorAll( 'a[data-shared=' + id + '] > span' ), function ( |
| 378 |
span |
| 379 |
) { |
| 380 |
var countNode = span.querySelector( '.share-count' ); |
| 381 |
removeNode( countNode ); |
| 382 |
var newNode = document.createElement( 'span' ); |
| 383 |
newNode.className = 'share-count'; |
| 384 |
newNode.textContent = WPCOMSharing.format_count( count ); |
| 385 |
span.appendChild( newNode ); |
| 386 |
} ); |
| 387 |
}, |
| 388 |
format_count: function ( count ) { |
| 389 |
if ( count < 1000 ) { |
| 390 |
return count; |
| 391 |
} |
| 392 |
if ( count >= 1000 && count < 10000 ) { |
| 393 |
return String( count ).substring( 0, 1 ) + 'K+'; |
| 394 |
} |
| 395 |
return '10K+'; |
| 396 |
}, |
| 397 |
bump_sharing_count_stat: function ( service ) { |
| 398 |
new Image().src = |
| 399 |
document.location.protocol + |
| 400 |
'//pixel.wp.com/g.gif?v=wpcom-no-pv&x_sharing-count-request=' + |
| 401 |
service + |
| 402 |
'&r=' + |
| 403 |
Math.random(); |
| 404 |
}, |
| 405 |
}; |
| 406 |
window.WPCOMSharing = WPCOMSharing; |
| 407 |
} |
| 408 |
|
| 409 |
// ------------------------ BUTTON FUNCTIONALITY ------------------------ // |
| 410 |
|
| 411 |
function shareIsEmail( val ) { |
| 412 |
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( |
| 413 |
val |
| 414 |
); |
| 415 |
} |
| 416 |
|
| 417 |
function closeEmailDialog() { |
| 418 |
var dialog = document.querySelector( '#sharing_email' ); |
| 419 |
hideNode( dialog ); |
| 420 |
} |
| 421 |
|
| 422 |
// Sharing initialization. |
| 423 |
// Will run immediately or on `DOMContentLoaded`, depending on current page status. |
| 424 |
function init() { |
| 425 |
// Move email dialog to end of body. |
| 426 |
var emailDialog = document.querySelector( '#sharing_email' ); |
| 427 |
if ( emailDialog ) { |
| 428 |
document.body.appendChild( emailDialog ); |
| 429 |
} |
| 430 |
|
| 431 |
WPCOMSharing_do(); |
| 432 |
} |
| 433 |
if ( document.readyState !== 'loading' ) { |
| 434 |
init(); |
| 435 |
} else { |
| 436 |
document.addEventListener( 'DOMContentLoaded', init ); |
| 437 |
} |
| 438 |
|
| 439 |
// Set up sharing again whenever a new post loads, to pick up any new buttons. |
| 440 |
document.body.addEventListener( 'is.post-load', WPCOMSharing_do ); |
| 441 |
|
| 442 |
// Set up sharing, updating counts and adding all button functionality. |
| 443 |
function WPCOMSharing_do() { |
| 444 |
if ( window.WPCOMSharing ) { |
| 445 |
window.WPCOMSharing.get_counts(); |
| 446 |
} |
| 447 |
|
| 448 |
forEachNode( document.querySelectorAll( '.sharedaddy a' ), function ( anchor ) { |
| 449 |
var href = anchor.getAttribute( 'href' ); |
| 450 |
if ( href && href.indexOf( 'share=' ) !== -1 && href.indexOf( '&nb=1' ) === -1 ) { |
| 451 |
anchor.setAttribute( 'href', href + '&nb=1' ); |
| 452 |
} |
| 453 |
} ); |
| 454 |
|
| 455 |
// Show hidden buttons |
| 456 |
|
| 457 |
// Touchscreen device: use click. |
| 458 |
// Non-touchscreen device: use click if not already appearing due to a hover event |
| 459 |
|
| 460 |
forEachNode( document.querySelectorAll( '.sharedaddy a.sharing-anchor' ), function ( |
| 461 |
buttonEl |
| 462 |
) { |
| 463 |
MoreButton.instantiateOrReuse( buttonEl ); |
| 464 |
} ); |
| 465 |
|
| 466 |
if ( document.ontouchstart !== undefined ) { |
| 467 |
document.body.classList.add( 'jp-sharing-input-touch' ); |
| 468 |
} |
| 469 |
|
| 470 |
// Add click functionality |
| 471 |
forEachNode( document.querySelectorAll( '.sharedaddy ul' ), function ( group ) { |
| 472 |
if ( group.getAttribute( 'data-sharing-events-added' ) === 'true' ) { |
| 473 |
return; |
| 474 |
} |
| 475 |
group.setAttribute( 'data-sharing-events-added', 'true' ); |
| 476 |
|
| 477 |
var printUrl = function ( uniqueId, urlToPrint ) { |
| 478 |
var iframe = document.createElement( 'iframe' ); |
| 479 |
iframe.setAttribute( |
| 480 |
'style', |
| 481 |
'position:fixed; top:100; left:100; height:1px; width:1px; border:none;' |
| 482 |
); |
| 483 |
iframe.setAttribute( 'id', 'printFrame-' + uniqueId ); |
| 484 |
iframe.setAttribute( 'name', iframe.getAttribute( 'id' ) ); |
| 485 |
iframe.setAttribute( 'src', urlToPrint ); |
| 486 |
iframe.setAttribute( |
| 487 |
'onload', |
| 488 |
'frames["printFrame-' + |
| 489 |
uniqueId + |
| 490 |
'"].focus();frames["printFrame-' + |
| 491 |
uniqueId + |
| 492 |
'"].print();' |
| 493 |
); |
| 494 |
document.body.appendChild( iframe ); |
| 495 |
}; |
| 496 |
|
| 497 |
// Print button |
| 498 |
forEachNode( group.querySelectorAll( 'a.share-print' ), function ( printButton ) { |
| 499 |
printButton.addEventListener( 'click', function ( event ) { |
| 500 |
event.preventDefault(); |
| 501 |
event.stopPropagation(); |
| 502 |
|
| 503 |
var ref = printButton.getAttribute( 'href' ) || ''; |
| 504 |
var doPrint = function () { |
| 505 |
if ( ref.indexOf( '#print' ) === -1 ) { |
| 506 |
var uid = new Date().getTime(); |
| 507 |
printUrl( uid, ref ); |
| 508 |
} else { |
| 509 |
window.print(); |
| 510 |
} |
| 511 |
}; |
| 512 |
|
| 513 |
// Is the button in a dropdown? |
| 514 |
var pane = closest( printButton, PANE_SELECTOR ); |
| 515 |
if ( pane ) { |
| 516 |
var moreButton = MoreButton.getButtonInstanceFromPane( pane ); |
| 517 |
if ( moreButton ) { |
| 518 |
moreButton.close(); |
| 519 |
doPrint(); |
| 520 |
} |
| 521 |
} else { |
| 522 |
doPrint(); |
| 523 |
} |
| 524 |
} ); |
| 525 |
} ); |
| 526 |
|
| 527 |
// Press This button |
| 528 |
forEachNode( group.querySelectorAll( 'a.share-press-this' ), function ( pressThisButton ) { |
| 529 |
pressThisButton.addEventListener( 'click', function ( event ) { |
| 530 |
event.preventDefault(); |
| 531 |
event.stopPropagation(); |
| 532 |
|
| 533 |
var s = ''; |
| 534 |
|
| 535 |
if ( window.getSelection ) { |
| 536 |
s = window.getSelection(); |
| 537 |
} else if ( document.getSelection ) { |
| 538 |
s = document.getSelection(); |
| 539 |
} else if ( document.selection ) { |
| 540 |
s = document.selection.createRange().text; |
| 541 |
} |
| 542 |
|
| 543 |
if ( s ) { |
| 544 |
var href = pressThisButton.getAttribute( 'href' ); |
| 545 |
pressThisButton.setAttribute( 'href', href + '&sel=' + encodeURI( s ) ); |
| 546 |
} |
| 547 |
|
| 548 |
if ( |
| 549 |
! window.open( |
| 550 |
pressThisButton.getAttribute( 'href' ), |
| 551 |
't', |
| 552 |
'toolbar=0,resizable=1,scrollbars=1,status=1,width=720,height=570' |
| 553 |
) |
| 554 |
) { |
| 555 |
document.location.href = pressThisButton.getAttribute( 'href' ); |
| 556 |
} |
| 557 |
} ); |
| 558 |
} ); |
| 559 |
|
| 560 |
// Email button |
| 561 |
forEachNode( group.querySelectorAll( 'a.share-email' ), function ( emailButton ) { |
| 562 |
var dialog = document.querySelector( '#sharing_email' ); |
| 563 |
|
| 564 |
emailButton.addEventListener( 'click', function ( event ) { |
| 565 |
event.preventDefault(); |
| 566 |
event.stopPropagation(); |
| 567 |
|
| 568 |
// Load reCAPTCHA if needed. |
| 569 |
if ( typeof grecaptcha !== 'object' && ! recaptchaScriptAdded ) { |
| 570 |
var configEl = document.querySelector( '.g-recaptcha' ); |
| 571 |
|
| 572 |
if ( configEl && configEl.getAttribute( 'data-lazy' ) === 'true' ) { |
| 573 |
recaptchaScriptAdded = true; |
| 574 |
loadScript( decodeURI( configEl.getAttribute( 'data-url' ) ) ); |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
var url = emailButton.getAttribute( 'href' ); |
| 579 |
var currentDomain = window.location.protocol + '//' + window.location.hostname + '/'; |
| 580 |
if ( url.indexOf( currentDomain ) !== 0 ) { |
| 581 |
return true; |
| 582 |
} |
| 583 |
|
| 584 |
if ( ! isNodeHidden( dialog ) ) { |
| 585 |
closeEmailDialog(); |
| 586 |
return; |
| 587 |
} |
| 588 |
|
| 589 |
removeNode( document.querySelector( '#sharing_email .response' ) ); |
| 590 |
|
| 591 |
var form = document.querySelector( '#sharing_email form' ); |
| 592 |
showNode( form ); |
| 593 |
form.querySelector( 'input[type=submit]' ).removeAttribute( 'disabled' ); |
| 594 |
showNode( form.querySelector( 'a.sharing_cancel' ) ); |
| 595 |
|
| 596 |
// Reset reCATPCHA if exists. |
| 597 |
if ( |
| 598 |
'object' === typeof grecaptcha && |
| 599 |
'function' === typeof grecaptcha.reset && |
| 600 |
window.___grecaptcha_cfg.count |
| 601 |
) { |
| 602 |
grecaptcha.reset(); |
| 603 |
} |
| 604 |
|
| 605 |
// Show dialog |
| 606 |
var rect = emailButton.getBoundingClientRect(); |
| 607 |
var scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || 0; |
| 608 |
var scrollTop = window.pageYOffset || document.documentElement.scrollTop || 0; |
| 609 |
dialog.style.left = scrollLeft + rect.left + 'px'; |
| 610 |
dialog.style.top = scrollTop + rect.top + rect.height + 'px'; |
| 611 |
showNode( dialog ); |
| 612 |
|
| 613 |
// Close all open More Button dialogs. |
| 614 |
MoreButton.closeAll(); |
| 615 |
} ); |
| 616 |
|
| 617 |
// Hook up other buttons |
| 618 |
dialog.querySelector( 'a.sharing_cancel' ).addEventListener( 'click', function ( event ) { |
| 619 |
event.preventDefault(); |
| 620 |
event.stopPropagation(); |
| 621 |
|
| 622 |
hideNode( dialog.querySelector( '.errors' ) ); |
| 623 |
hideNode( dialog ); |
| 624 |
hideNode( document.querySelector( '#sharing_background' ) ); |
| 625 |
} ); |
| 626 |
|
| 627 |
var submitButton = dialog.querySelector( 'input[type=submit]' ); |
| 628 |
submitButton.addEventListener( 'click', function ( event ) { |
| 629 |
event.preventDefault(); |
| 630 |
event.stopPropagation(); |
| 631 |
|
| 632 |
var form = closest( submitButton, 'form' ); |
| 633 |
var source_email_input = form.querySelector( 'input[name=source_email]' ); |
| 634 |
var target_email_input = form.querySelector( 'input[name=target_email]' ); |
| 635 |
|
| 636 |
// Disable buttons + enable loading icon |
| 637 |
submitButton.setAttribute( 'disabled', true ); |
| 638 |
hideNode( form.querySelector( 'a.sharing_cancel' ) ); |
| 639 |
forEachNode( form.querySelectorAll( 'img.loading' ), function ( img ) { |
| 640 |
showNode( img ); |
| 641 |
} ); |
| 642 |
|
| 643 |
hideNode( form.querySelector( '.errors' ) ); |
| 644 |
|
| 645 |
forEachNode( form.querySelectorAll( '.error' ), function ( node ) { |
| 646 |
node.classList.remove( 'error' ); |
| 647 |
} ); |
| 648 |
|
| 649 |
if ( ! shareIsEmail( source_email_input.value ) ) { |
| 650 |
source_email_input.classList.add( 'error' ); |
| 651 |
} |
| 652 |
|
| 653 |
if ( ! shareIsEmail( target_email_input.value ) ) { |
| 654 |
target_email_input.classList.add( 'error' ); |
| 655 |
} |
| 656 |
|
| 657 |
if ( ! form.querySelector( '.error' ) ) { |
| 658 |
// Encode form data. This would be much easier if we could rely on URLSearchParams... |
| 659 |
var params = []; |
| 660 |
for ( var i = 0; i < form.elements.length; i++ ) { |
| 661 |
if ( form.elements[ i ].name ) { |
| 662 |
// Encode each form element into a URI-compatible string. |
| 663 |
var encoded = |
| 664 |
encodeURIComponent( form.elements[ i ].name ) + |
| 665 |
'=' + |
| 666 |
encodeURIComponent( form.elements[ i ].value ); |
| 667 |
// In x-www-form-urlencoded, spaces should be `+`, not `%20`. |
| 668 |
params.push( encoded.replace( '%20', '+' ) ); |
| 669 |
} |
| 670 |
} |
| 671 |
var data = params.join( '&' ); |
| 672 |
|
| 673 |
// AJAX send the form |
| 674 |
var request = new XMLHttpRequest(); |
| 675 |
request.open( 'POST', emailButton.getAttribute( 'href' ), true ); |
| 676 |
request.setRequestHeader( |
| 677 |
'Content-Type', |
| 678 |
'application/x-www-form-urlencoded; charset=UTF-8' |
| 679 |
); |
| 680 |
request.setRequestHeader( 'x-requested-with', 'XMLHttpRequest' ); |
| 681 |
|
| 682 |
request.onreadystatechange = function () { |
| 683 |
if ( this.readyState === XMLHttpRequest.DONE && this.status === 200 ) { |
| 684 |
forEachNode( form.querySelectorAll( 'img.loading' ), function ( img ) { |
| 685 |
hideNode( img ); |
| 686 |
} ); |
| 687 |
|
| 688 |
if ( this.response === '1' || this.response === '2' || this.response === '3' ) { |
| 689 |
showNode( dialog.querySelector( '.errors-' + this.response ) ); |
| 690 |
dialog.querySelector( 'input[type=submit]' ).removeAttribute( 'disabled' ); |
| 691 |
showNode( dialog.querySelector( 'a.sharing_cancel' ) ); |
| 692 |
|
| 693 |
if ( typeof grecaptcha === 'object' && typeof grecaptcha.reset === 'function' ) { |
| 694 |
grecaptcha.reset(); |
| 695 |
} |
| 696 |
} else { |
| 697 |
hideNode( form ); |
| 698 |
var temp = document.createElement( 'div' ); |
| 699 |
temp.innerHTML = this.response; |
| 700 |
dialog.appendChild( temp.firstChild ); |
| 701 |
showNode( dialog.querySelector( 'a.sharing_cancel' ) ); |
| 702 |
var closeButton = dialog.querySelector( '.response a.sharing_cancel' ); |
| 703 |
if ( closeButton ) { |
| 704 |
closeButton.addEventListener( 'click', function ( event ) { |
| 705 |
event.preventDefault(); |
| 706 |
event.stopPropagation(); |
| 707 |
|
| 708 |
closeEmailDialog(); |
| 709 |
hideNode( document.querySelector( '#sharing_background' ) ); |
| 710 |
} ); |
| 711 |
} |
| 712 |
} |
| 713 |
} |
| 714 |
}; |
| 715 |
|
| 716 |
request.send( data ); |
| 717 |
|
| 718 |
return; |
| 719 |
} |
| 720 |
|
| 721 |
forEachNode( dialog.querySelectorAll( 'img.loading' ), function ( img ) { |
| 722 |
hideNode( img ); |
| 723 |
} ); |
| 724 |
submitButton.removeAttribute( 'disabled' ); |
| 725 |
showNode( dialog.querySelector( 'a.sharing_cancel' ) ); |
| 726 |
forEachNode( dialog.querySelectorAll( '.errors-1' ), function ( error ) { |
| 727 |
showNode( error ); |
| 728 |
} ); |
| 729 |
} ); |
| 730 |
} ); |
| 731 |
} ); |
| 732 |
|
| 733 |
forEachNode( |
| 734 |
document.querySelectorAll( 'li.share-email, li.share-custom a.sharing-anchor' ), |
| 735 |
function ( node ) { |
| 736 |
node.classList.add( 'share-service-visible' ); |
| 737 |
} |
| 738 |
); |
| 739 |
} |
| 740 |
} )(); |
| 741 |
|