| 1 |
/* global ajaxurl,jQuery,tl_obj */ |
| 2 |
(function ($) { |
| 3 |
|
| 4 |
'use strict'; |
| 5 |
|
| 6 |
var $body = $( 'body' ), namespace = tl_obj.vendor.namespace, |
| 7 |
$tl_container = $( '.tl-' + namespace + '-auth' ), copy_button_timer = null, |
| 8 |
second_status = null; |
| 9 |
|
| 10 |
$body.on( 'click', tl_obj.selector, function ( e ) { |
| 11 |
|
| 12 |
e.preventDefault(); |
| 13 |
|
| 14 |
grantAccess( $( this ) ); |
| 15 |
|
| 16 |
return false; |
| 17 |
} ); |
| 18 |
|
| 19 |
/** |
| 20 |
* Adds a hidden <span> for screen readers alerting them that the link will open a new window. |
| 21 |
*/ |
| 22 |
$tl_container.find( 'a[target=_blank]' ).each( function () { |
| 23 |
$( this ).append( $( '<span>', { |
| 24 |
'class': 'screen-reader-text', |
| 25 |
'text': tl_obj.lang.a11y.opens_new_window |
| 26 |
} ) ); |
| 27 |
} ); |
| 28 |
|
| 29 |
function grantAccess( $button ) { |
| 30 |
|
| 31 |
$button.addClass( 'disabled' ); |
| 32 |
|
| 33 |
if ( 'extend' === $button.data( 'access' ) ) { |
| 34 |
outputStatus( tl_obj.lang.status.extending.content, 'pending' ); |
| 35 |
} else { |
| 36 |
outputStatus( tl_obj.lang.status.pending.content, 'pending' ); |
| 37 |
} |
| 38 |
|
| 39 |
second_status = setTimeout( function () { |
| 40 |
outputStatus( tl_obj.lang.status.syncing.content, 'pending' ); |
| 41 |
}, 3000 ); |
| 42 |
|
| 43 |
var remote_error = function ( response ) { |
| 44 |
|
| 45 |
clearTimeout( second_status ); |
| 46 |
|
| 47 |
if ( tl_obj.debug ) { |
| 48 |
console.error( 'Request failed.', response ); |
| 49 |
} |
| 50 |
|
| 51 |
// User not logged-in |
| 52 |
if ( response.responseText && '0' === response.responseText ) { |
| 53 |
outputStatus( tl_obj.lang.status.failed_permissions.content, 'error' ); |
| 54 |
} else if ( typeof response.data === 'object' ) { |
| 55 |
outputStatus( tl_obj.lang.status.failed.content + ' ' + response.data.message, 'error' ); |
| 56 |
} else if ( typeof response.responseJSON === 'object' ) { |
| 57 |
outputStatus( tl_obj.lang.status.failed.content + ' ' + response.responseJSON.data.message, 'error' ); |
| 58 |
} else if ( 'parsererror' === response.statusText ) { |
| 59 |
outputStatus( tl_obj.lang.status.failed.content + ' ' + response.responseText, 'error' ); |
| 60 |
} |
| 61 |
}; |
| 62 |
|
| 63 |
var remote_success = function ( response ) { |
| 64 |
|
| 65 |
clearTimeout( second_status ); |
| 66 |
|
| 67 |
if ( response.success && typeof response.data == 'object' ) { |
| 68 |
location.href = tl_obj.query_string; |
| 69 |
} else { |
| 70 |
remote_error( response ); |
| 71 |
} |
| 72 |
|
| 73 |
}; |
| 74 |
|
| 75 |
var data = { |
| 76 |
'action': 'tl_' + namespace + '_gen_support', |
| 77 |
'vendor': namespace, |
| 78 |
'_nonce': tl_obj._nonce, |
| 79 |
'reference_id': tl_obj.reference_id, |
| 80 |
'debug_data_consent': $( '#tl-' + namespace + '-debug-data-consent:checked' ).length, |
| 81 |
}; |
| 82 |
|
| 83 |
|
| 84 |
//Add create_ticket value if allowed and set |
| 85 |
if ( tl_obj.create_ticket ) { |
| 86 |
//add value of textarea with name "create_ticket" |
| 87 |
var $ticket_message = $( '#tl-' + namespace + '-ticket-message' ); |
| 88 |
if ( $ticket_message && $ticket_message.is( ':visible' ) && $ticket_message.val() ) { |
| 89 |
data.ticket = { |
| 90 |
'message': $ticket_message.val(), |
| 91 |
}; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
if ( tl_obj.debug ) { |
| 96 |
console.log( data ); |
| 97 |
} |
| 98 |
|
| 99 |
$.ajax( { |
| 100 |
url: tl_obj.ajaxurl, |
| 101 |
type: 'POST', |
| 102 |
dataType: 'json', |
| 103 |
data: data, |
| 104 |
success: remote_success, |
| 105 |
error: remote_error |
| 106 |
} ).always( function ( response ) { |
| 107 |
|
| 108 |
if ( !tl_obj.debug ) { |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
console.log( 'TrustedLogin response: ', response ); |
| 113 |
|
| 114 |
if ( typeof response.data === 'object' ) { |
| 115 |
console.log( 'TrustedLogin support login URL:' ); |
| 116 |
console.log( response.data.site_url + '/' + response.data.endpoint + '/' + response.data.identifier ); |
| 117 |
} |
| 118 |
} ); |
| 119 |
} |
| 120 |
|
| 121 |
function outputStatus( content, type ) { |
| 122 |
|
| 123 |
var responseClass = 'tl-' + namespace + '-auth__response'; |
| 124 |
|
| 125 |
var $responseDiv = $tl_container.find( '.' + responseClass ); |
| 126 |
|
| 127 |
if ( 0 === $responseDiv.length ) { |
| 128 |
if ( tl_obj.debug ) { |
| 129 |
console.log( responseClass + ' not found' ); |
| 130 |
} |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
// Reset the class and set the type for contextual styling. |
| 135 |
$responseDiv |
| 136 |
.attr( 'class', responseClass ).addClass( 'tl-' + namespace + '-auth__response_' + type ) |
| 137 |
.text( content ); |
| 138 |
|
| 139 |
/** |
| 140 |
* Handle button actions/labels/etc to it's own function |
| 141 |
*/ |
| 142 |
if ( 'error' === type ) { |
| 143 |
$( tl_obj.selector ).text( tl_obj.lang.buttons.go_to_site ).removeClass( 'disabled' ); |
| 144 |
$body.off( 'click', tl_obj.selector ); |
| 145 |
} |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Select the text of an input field on click |
| 151 |
* @param {jQueryEvent} e |
| 152 |
*/ |
| 153 |
$( '#tl-' + namespace + '-access-key', $tl_container ).on( 'click', function ( e ) { |
| 154 |
e.preventDefault(); |
| 155 |
|
| 156 |
$( this ).trigger( 'focus' ).trigger( 'select' ); |
| 157 |
|
| 158 |
return false; |
| 159 |
} ); |
| 160 |
|
| 161 |
/** |
| 162 |
* Expand and collapse toggling sections based on the target's [data-toggle] attribute. |
| 163 |
* @param {jQueryEvent} e |
| 164 |
*/ |
| 165 |
$( '.tl-' + namespace + '-toggle' ).on( 'click', function ( e ) { |
| 166 |
e.preventDefault(); |
| 167 |
|
| 168 |
$( this ).find( '.dashicons' ).toggleClass( 'dashicons-arrow-down-alt2' ).toggleClass( 'dashicons-arrow-up-alt2' ); |
| 169 |
|
| 170 |
$( $( this ).data( 'toggle' ) ).toggleClass( 'hidden' ); |
| 171 |
} ); |
| 172 |
|
| 173 |
/** |
| 174 |
* Used for copy-to-clipboard functionality |
| 175 |
*/ |
| 176 |
$( '.tl-' + namespace + '-auth__accesskey_copy', $tl_container ).on( 'click', function () { |
| 177 |
var $copyButton = $( this ); |
| 178 |
var $copyText = $( this ).find( 'span' ); |
| 179 |
|
| 180 |
copyToClipboard( $( '.tl-' + namespace + '-auth__accesskey_field' ).val() ); |
| 181 |
|
| 182 |
wp.a11y.speak( tl_obj.lang.a11y.copied_text, 'assertive' ); |
| 183 |
|
| 184 |
$copyText.text( tl_obj.lang.buttons.copied ).removeClass( 'screen-reader-text' ); |
| 185 |
|
| 186 |
$copyButton.addClass( 'tl-' + namespace + '-auth__copied' ); |
| 187 |
|
| 188 |
if ( copy_button_timer ) { |
| 189 |
clearTimeout( copy_button_timer ); |
| 190 |
copy_button_timer = null; |
| 191 |
} |
| 192 |
|
| 193 |
copy_button_timer = setTimeout( function () { |
| 194 |
$copyButton.removeClass( 'tl-' + namespace + '-auth__copied' ); |
| 195 |
|
| 196 |
$copyText.text( tl_obj.lang.buttons.copy ).addClass( 'screen-reader-text' ); |
| 197 |
}, 2000 ); |
| 198 |
} ); |
| 199 |
|
| 200 |
function copyToClipboard( copyText ) { |
| 201 |
|
| 202 |
var $temp = $( '<input>' ); |
| 203 |
$body.append( $temp ); |
| 204 |
$temp.val( copyText ).select(); |
| 205 |
document.execCommand( 'copy' ); |
| 206 |
$temp.remove(); |
| 207 |
|
| 208 |
if ( tl_obj.debug ) { |
| 209 |
console.log( 'Copied to clipboard', copyText ); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
})(jQuery); |
| 214 |
|