| @@ -1,0 +1,99 @@ | ||
| 1 | +/* globals analytics, deactivate_dialog, tb_remove */ | |
| 2 | +/** | |
| 3 | + * Adds the Deactivation modal. | |
| 4 | + * | |
| 5 | + * Depends on _inc/lib/tracks/tracks-callables.js and //stats.wp.com/w.js | |
| 6 | + * | |
| 7 | + */ | |
| 8 | +( function ( $ ) { | |
| 9 | + // Initialize Tracks and bump stats. | |
| 10 | + var tracksUser = deactivate_dialog.tracksUserData; | |
| 11 | + | |
| 12 | + analytics.initialize( tracksUser.userid, tracksUser.username ); | |
| 13 | + | |
| 14 | + var deactivateLinkElem = $( | |
| 15 | + 'tr[data-slug=jetpack] > td.plugin-title > div > span.deactivate > a' | |
| 16 | + ); | |
| 17 | + | |
| 18 | + var deactivateJetpackURL = deactivateLinkElem.attr( 'href' ); | |
| 19 | + | |
| 20 | + window.deactivateJetpack = function () { | |
| 21 | + window.location.href = deactivateJetpackURL; | |
| 22 | + }; | |
| 23 | + | |
| 24 | + var observer = new MutationObserver( function ( mutations ) { | |
| 25 | + mutations.forEach( function ( mutation ) { | |
| 26 | + if ( mutation.type === 'childList' ) { | |
| 27 | + mutation.addedNodes.forEach( function ( addedNode ) { | |
| 28 | + if ( 'TB_window' === addedNode.id ) { | |
| 29 | + // NodeList is static, we need to modify this in the DOM | |
| 30 | + | |
| 31 | + $( '#TB_window' ).addClass( 'jetpack-disconnect-modal' ); | |
| 32 | + window.deactivationModalCentralize(); | |
| 33 | + | |
| 34 | + $( '#TB_closeWindowButton, #TB_overlay' ).on( 'click', function () { | |
| 35 | + window.deactivationModalTrackCloseEvent(); | |
| 36 | + } ); | |
| 37 | + | |
| 38 | + document.onkeyup = function ( e ) { | |
| 39 | + var keycode; | |
| 40 | + if ( e === null ) { | |
| 41 | + // ie | |
| 42 | + keycode = event.keyCode; | |
| 43 | + } else { | |
| 44 | + // mozilla | |
| 45 | + keycode = e.which; | |
| 46 | + } | |
| 47 | + if ( keycode === 27 ) { | |
| 48 | + // close | |
| 49 | + window.deactivationModalTrackCloseEvent(); | |
| 50 | + } | |
| 51 | + }; | |
| 52 | + | |
| 53 | + observer.disconnect(); | |
| 54 | + } | |
| 55 | + } ); | |
| 56 | + } | |
| 57 | + } ); | |
| 58 | + } ); | |
| 59 | + | |
| 60 | + window.deactivationModalCentralize = function () { | |
| 61 | + var modal = $( '#TB_window.jetpack-disconnect-modal' ); | |
| 62 | + var top = $( window ).height() / 2 - $( modal ).height() / 2; | |
| 63 | + $( modal ).css( 'top', top + 'px' ); | |
| 64 | + }; | |
| 65 | + | |
| 66 | + window.deactivationModalTrackCloseEvent = function () { | |
| 67 | + analytics.tracks.recordEvent( 'jetpack_termination_dialog_close_click', tracksProps ); | |
| 68 | + document.onkeyup = ''; | |
| 69 | + }; | |
| 70 | + | |
| 71 | + var body = $( 'body' )[ 0 ]; | |
| 72 | + | |
| 73 | + var tracksProps = { | |
| 74 | + location: 'plugins', | |
| 75 | + purpose: 'deactivate', | |
| 76 | + }; | |
| 77 | + | |
| 78 | + deactivateLinkElem.attr( 'href', 'plugins.php#TB_inline?inlineId=jetpack_deactivation_dialog' ); | |
| 79 | + deactivateLinkElem.attr( 'title', deactivate_dialog.title ); | |
| 80 | + deactivateLinkElem.addClass( 'thickbox' ); | |
| 81 | + deactivateLinkElem.html( deactivate_dialog.deactivate_label ); | |
| 82 | + deactivateLinkElem.on( 'click', function () { | |
| 83 | + observer.observe( body, { childList: true } ); | |
| 84 | + analytics.tracks.recordEvent( 'jetpack_termination_dialog_open', tracksProps ); | |
| 85 | + } ); | |
| 86 | + | |
| 87 | + $( '#jetpack_deactivation_dialog_content__button-cancel' ).on( 'click', function () { | |
| 88 | + tb_remove(); | |
| 89 | + window.deactivationModalTrackCloseEvent(); | |
| 90 | + } ); | |
| 91 | + | |
| 92 | + $( '#jetpack_deactivation_dialog_content__button-deactivate' ).on( 'click', function ( e ) { | |
| 93 | + e.preventDefault(); | |
| 94 | + | |
| 95 | + $( this ).prop( 'disabled', true ); | |
| 96 | + analytics.tracks.recordEvent( 'jetpack_termination_dialog_termination_click', tracksProps ); | |
| 97 | + window.deactivateJetpack(); | |
| 98 | + } ); | |
| 99 | +} )( jQuery ); | |