| 1 |
/* global jpConnect */ |
| 2 |
|
| 3 |
jQuery( document ).ready( function( $ ) { |
| 4 |
var connectButton = $( '.jp-connect-button' ); |
| 5 |
var tosText = $( '.jp-connect-full__tos-blurb' ); |
| 6 |
connectButton.click( function( event ) { |
| 7 |
event.preventDefault(); |
| 8 |
$( '#jetpack-connection-cards' ).fadeOut( 600 ); |
| 9 |
if ( ! jetpackConnectButton.isRegistering ) { |
| 10 |
if ( 'original' === jpConnect.forceVariation ) { |
| 11 |
// Forcing original connection flow, `JETPACK_SHOULD_USE_CONNECTION_IFRAME = false` |
| 12 |
// or we're dealing with Safari which has issues with handling 3rd party cookies. |
| 13 |
jetpackConnectButton.handleOriginalFlow(); |
| 14 |
} else if ( 'in_place' === jpConnect.forceVariation ) { |
| 15 |
// Forcing new connection flow, `JETPACK_SHOULD_USE_CONNECTION_IFRAME = true`. |
| 16 |
jetpackConnectButton.handleConnectInPlaceFlow(); |
| 17 |
} else { |
| 18 |
// Forcing A/B test driven connection flow variation, `JETPACK_SHOULD_USE_CONNECTION_IFRAME` not defined. |
| 19 |
jetpackConnectButton.startConnectionFlow(); |
| 20 |
} |
| 21 |
} |
| 22 |
} ); |
| 23 |
var jetpackConnectIframe = $( '<iframe class="jp-jetpack-connect__iframe" />' ); |
| 24 |
|
| 25 |
var jetpackConnectButton = { |
| 26 |
isRegistering: false, |
| 27 |
isPaidPlan: false, |
| 28 |
startConnectionFlow: function() { |
| 29 |
var abTestName = 'jetpack_connect_in_place_v2'; |
| 30 |
$.ajax( { |
| 31 |
url: 'https://public-api.wordpress.com/wpcom/v2/abtest/' + abTestName, |
| 32 |
type: 'GET', |
| 33 |
error: jetpackConnectButton.handleConnectionError, |
| 34 |
xhrFields: { |
| 35 |
withCredentials: true, |
| 36 |
}, |
| 37 |
crossDomain: true, |
| 38 |
success: function( data ) { |
| 39 |
if ( data && 'in_place' === data.variation ) { |
| 40 |
jetpackConnectButton.handleConnectInPlaceFlow(); |
| 41 |
return; |
| 42 |
} |
| 43 |
jetpackConnectButton.handleOriginalFlow(); |
| 44 |
}, |
| 45 |
} ); |
| 46 |
}, |
| 47 |
handleOriginalFlow: function() { |
| 48 |
window.location = connectButton.attr( 'href' ); |
| 49 |
}, |
| 50 |
handleConnectInPlaceFlow: function() { |
| 51 |
jetpackConnectButton.isRegistering = true; |
| 52 |
tosText.hide(); |
| 53 |
connectButton.hide(); |
| 54 |
jetpackConnectButton.triggerLoadingState(); |
| 55 |
|
| 56 |
var registerUrl = jpConnect.apiBaseUrl + '/connection/register'; |
| 57 |
|
| 58 |
// detect Calypso Env and add to API URL |
| 59 |
if ( window.Initial_State && window.Initial_State.calypsoEnv ) { |
| 60 |
registerUrl = |
| 61 |
registerUrl + '?' + $.param( { calypso_env: window.Initial_State.calypsoEnv } ); |
| 62 |
} |
| 63 |
|
| 64 |
$.ajax( { |
| 65 |
url: registerUrl, |
| 66 |
type: 'POST', |
| 67 |
data: { |
| 68 |
registration_nonce: jpConnect.registrationNonce, |
| 69 |
_wpnonce: jpConnect.apiNonce, |
| 70 |
}, |
| 71 |
error: jetpackConnectButton.handleConnectionError, |
| 72 |
success: jetpackConnectButton.handleConnectionSuccess, |
| 73 |
} ); |
| 74 |
}, |
| 75 |
triggerLoadingState: function() { |
| 76 |
var loadingText = $( '<span>' ) |
| 77 |
.addClass( 'jp-connect-full__button-container-loading' ) |
| 78 |
.text( jpConnect.buttonTextRegistering ) |
| 79 |
.appendTo( '.jp-connect-full__button-container' ); |
| 80 |
|
| 81 |
var spinner = $( '<div>' ).addClass( 'jp-spinner' ); |
| 82 |
var spinnerOuter = $( '<div>' ) |
| 83 |
.addClass( 'jp-spinner__outer' ) |
| 84 |
.appendTo( spinner ); |
| 85 |
$( '<div>' ) |
| 86 |
.addClass( 'jp-spinner__inner' ) |
| 87 |
.appendTo( spinnerOuter ); |
| 88 |
loadingText.after( spinner ); |
| 89 |
}, |
| 90 |
handleConnectionSuccess: function( data ) { |
| 91 |
jetpackConnectButton.fetchPlanType(); |
| 92 |
window.addEventListener( 'message', jetpackConnectButton.receiveData ); |
| 93 |
jetpackConnectIframe.attr( 'src', data.authorizeUrl ); |
| 94 |
jetpackConnectIframe.on( 'load', function() { |
| 95 |
jetpackConnectIframe.show(); |
| 96 |
$( '.jp-connect-full__button-container' ).hide(); |
| 97 |
} ); |
| 98 |
jetpackConnectIframe.hide(); |
| 99 |
$( '.jp-connect-full__button-container' ).after( jetpackConnectIframe ); |
| 100 |
}, |
| 101 |
fetchPlanType: function() { |
| 102 |
$.ajax( { |
| 103 |
url: jpConnect.apiBaseUrl + '/site', |
| 104 |
type: 'GET', |
| 105 |
data: { |
| 106 |
_wpnonce: jpConnect.apiSiteDataNonce, |
| 107 |
}, |
| 108 |
success: function( data ) { |
| 109 |
var siteData = JSON.parse( data.data ); |
| 110 |
jetpackConnectButton.isPaidPlan = ! siteData.plan.is_free; |
| 111 |
}, |
| 112 |
} ); |
| 113 |
}, |
| 114 |
receiveData: function( event ) { |
| 115 |
if ( |
| 116 |
event.origin === jpConnect.jetpackApiDomain && |
| 117 |
event.source === jetpackConnectIframe.get( 0 ).contentWindow && |
| 118 |
event.data === 'close' |
| 119 |
) { |
| 120 |
window.removeEventListener( 'message', this.receiveData ); |
| 121 |
jetpackConnectButton.handleAuthorizationComplete(); |
| 122 |
} |
| 123 |
}, |
| 124 |
handleAuthorizationComplete: function() { |
| 125 |
jetpackConnectButton.isRegistering = false; |
| 126 |
|
| 127 |
if ( jetpackConnectButton.isPaidPlan ) { |
| 128 |
window.location.assign( jpConnect.dashboardUrl ); |
| 129 |
} else { |
| 130 |
window.location.assign( jpConnect.plansPromptUrl ); |
| 131 |
} |
| 132 |
window.location.reload( true ); |
| 133 |
}, |
| 134 |
handleConnectionError: function( error ) { |
| 135 |
jetpackConnectButton.isRegistering = false; |
| 136 |
jetpackConnectButton.handleOriginalFlow(); |
| 137 |
}, |
| 138 |
}; |
| 139 |
} ); |
| 140 |
|