| 1 |
document.addEventListener( 'DOMContentLoaded', function() { jQuery( document ).ready( function( $ ) { |
| 2 |
var dologin_kl_copy_feedback_ms = 2000; |
| 3 |
var dologin_kl_copy_reset_timer = null; |
| 4 |
|
| 5 |
function dologin_keycode( num ) { |
| 6 |
var num = num || 13 ; |
| 7 |
var code = window.event ? event.keyCode : event.which ; |
| 8 |
if( num == code ) return true ; |
| 9 |
return false ; |
| 10 |
} |
| 11 |
|
| 12 |
function dologin_display_tab(tab) { |
| 13 |
jQuery('[data-dologin-tab]').removeClass('nav-tab-active'); |
| 14 |
jQuery('[data-dologin-tab="'+tab+'"]').addClass('nav-tab-active'); |
| 15 |
jQuery('[data-dologin-layout]').hide(); |
| 16 |
jQuery('[data-dologin-layout="'+tab+'"]').show(); |
| 17 |
} |
| 18 |
|
| 19 |
/*** Admin Panel JS ***/ |
| 20 |
// page tab switch functionality |
| 21 |
if($('[data-dologin-tab]').length > 0){ |
| 22 |
// display default tab |
| 23 |
var dologin_tab_current = document.cookie.replace(/(?:(?:^|.*;\s*)dologin_tab\s*\=\s*([^;]*).*$)|^.*$/, "$1") ; |
| 24 |
if(window.location.hash.substr(1)) { |
| 25 |
dologin_tab_current = window.location.hash.substr(1) ; |
| 26 |
} |
| 27 |
if(!dologin_tab_current || !$('[data-dologin-tab="'+dologin_tab_current+'"]').length) { |
| 28 |
dologin_tab_current = $('[data-dologin-tab]').first().data('dologin-tab') ; |
| 29 |
} |
| 30 |
dologin_display_tab(dologin_tab_current) ; |
| 31 |
// tab switch |
| 32 |
$('[data-dologin-tab]').click(function(event) { |
| 33 |
dologin_display_tab($(this).data('dologin-tab')) ; |
| 34 |
document.cookie = 'dologin_tab='+$(this).data('dologin-tab') ; |
| 35 |
$(this).blur() ; |
| 36 |
}) ; |
| 37 |
} |
| 38 |
|
| 39 |
/** Accesskey **/ |
| 40 |
$( '[dologin-accesskey]' ).map( function() { |
| 41 |
var thiskey = $( this ).attr( 'dologin-accesskey' ) ; |
| 42 |
$( this ).attr( 'title', 'Shortcut : ' + thiskey.toLocaleUpperCase() ) ; |
| 43 |
var that = this ; |
| 44 |
$( document ).on( 'keydown', function( e ) { |
| 45 |
if( $(":input:focus").length > 0 ) return ; |
| 46 |
if( event.metaKey ) return ; |
| 47 |
if( event.ctrlKey ) return ; |
| 48 |
if( event.altKey ) return ; |
| 49 |
if( event.shiftKey ) return ; |
| 50 |
if( dologin_keycode( thiskey.charCodeAt( 0 ) ) ) $( that )[ 0 ].click() ; |
| 51 |
}); |
| 52 |
}); |
| 53 |
|
| 54 |
$( '.dologin-clear-log' ).click( function( event ) { |
| 55 |
if ( ! window.confirm( dologin_admin.clear_log_confirm ) ) { |
| 56 |
event.preventDefault(); |
| 57 |
} |
| 58 |
} ); |
| 59 |
|
| 60 |
$( '#dologin_get_ip' ).click( function( e ) { |
| 61 |
e.preventDefault(); |
| 62 |
var $button = $( this ); |
| 63 |
var $status = $( '#dologin_mygeolocation' ); |
| 64 |
$button.prop( 'disabled', true ).attr( 'aria-busy', 'true' ); |
| 65 |
$status.attr( 'class', 'dologin-warn' ).text( dologin_admin.ip_lookup_progress ); |
| 66 |
$.ajax( { |
| 67 |
url: dologin_admin.url_myip, |
| 68 |
dataType: 'json', |
| 69 |
headers: { |
| 70 |
'X-WP-Nonce': dologin_admin.nonce |
| 71 |
} |
| 72 |
} ).done( function( data ) { |
| 73 |
var html = []; |
| 74 |
$.each( data, function( k, v ) { |
| 75 |
html.push( k + ':' + v ); |
| 76 |
} ); |
| 77 |
$status.attr( 'class', '' ).text( html.join( ', ' ) ); |
| 78 |
} ).fail( function() { |
| 79 |
$status.attr( 'class', 'dologin-danger' ).text( dologin_admin.ip_lookup_failed ); |
| 80 |
} ).always( function() { |
| 81 |
$button.prop( 'disabled', false ).removeAttr( 'aria-busy' ); |
| 82 |
} ); |
| 83 |
} ); |
| 84 |
|
| 85 |
function dologin_clearPublicKeyCopyFeedback() { |
| 86 |
if ( dologin_kl_copy_reset_timer ) { |
| 87 |
window.clearTimeout( dologin_kl_copy_reset_timer ); |
| 88 |
dologin_kl_copy_reset_timer = null; |
| 89 |
} |
| 90 |
return $( '.dologin-kl-copy-status' ).attr( 'class', 'dologin-kl-copy-status' ).text( '' ); |
| 91 |
} |
| 92 |
|
| 93 |
function dologin_resetPublicKeyCopyButton() { |
| 94 |
dologin_clearPublicKeyCopyFeedback(); |
| 95 |
var $button = $( '.dologin-kl-copy-public-key' ); |
| 96 |
if ( ! $button.length ) { |
| 97 |
$button = $( '<button>' ) |
| 98 |
.attr( 'type', 'button' ) |
| 99 |
.addClass( 'button dologin-kl-copy-public-key' ) |
| 100 |
.insertAfter( '#dologin-kl-encryption-public-key' ); |
| 101 |
} |
| 102 |
return $button.text( dologin_admin.copy_public_key ).prop( 'disabled', false ); |
| 103 |
} |
| 104 |
|
| 105 |
function dologin_showPublicKeyCopyFeedback( $button, message, copied ) { |
| 106 |
var $status = dologin_clearPublicKeyCopyFeedback(); |
| 107 |
$button.prop( 'disabled', false ); |
| 108 |
$status |
| 109 |
.addClass( copied ? 'dologin-success' : 'dologin-danger' ) |
| 110 |
.text( message ); |
| 111 |
dologin_kl_copy_reset_timer = window.setTimeout( function() { |
| 112 |
dologin_resetPublicKeyCopyButton(); |
| 113 |
}, dologin_kl_copy_feedback_ms ); |
| 114 |
} |
| 115 |
|
| 116 |
$( '.dologin-kl-reset-keys' ).click( function() { |
| 117 |
if ( ! window.confirm( dologin_admin.reset_keys_confirm ) ) { |
| 118 |
return; |
| 119 |
} |
| 120 |
dologin_clearPublicKeyCopyFeedback(); |
| 121 |
var $button = $( this ); |
| 122 |
var $status = $( '.dologin-kl-site-key-status' ); |
| 123 |
$button.prop( 'disabled', true ); |
| 124 |
$status.attr( 'class', 'dologin-kl-site-key-status dologin-warn' ).text( dologin_admin.resetting_keys ); |
| 125 |
$.ajax( { |
| 126 |
url: dologin_admin.url_kl_reset_keys, |
| 127 |
method: 'POST', |
| 128 |
dataType: 'json', |
| 129 |
headers: { |
| 130 |
'X-WP-Nonce': dologin_admin.nonce |
| 131 |
} |
| 132 |
} ).done( function( data ) { |
| 133 |
if ( ! data || data._res !== 'ok' ) { |
| 134 |
$status.attr( 'class', 'dologin-kl-site-key-status dologin-danger' ).text( data && data._msg ? data._msg : dologin_admin.reset_keys_failed ); |
| 135 |
return; |
| 136 |
} |
| 137 |
$( '#dologin-kl-site-key-fingerprint' ).text( data.fingerprint ); |
| 138 |
$( '.dologin-kl-encryption-public-key' ).text( data.encryption_public_key ); |
| 139 |
dologin_resetPublicKeyCopyButton(); |
| 140 |
$status.attr( 'class', 'dologin-kl-site-key-status dologin-success' ).text( data.message ); |
| 141 |
if ( $( '.dologin-kl-account' ).length && data.binding_message ) { |
| 142 |
var $note = $( '.dologin-kl-key-note' ); |
| 143 |
if ( !$note.length ) { |
| 144 |
$note = $( '<div>' ).addClass( 'dologin-warn dologin-kl-key-note' ).insertAfter( '.dologin-kl-account' ); |
| 145 |
} |
| 146 |
$note.text( data.binding_message ); |
| 147 |
} |
| 148 |
} ).fail( function() { |
| 149 |
$status.attr( 'class', 'dologin-kl-site-key-status dologin-danger' ).text( dologin_admin.reset_keys_failed ); |
| 150 |
} ).always( function() { |
| 151 |
$button.prop( 'disabled', false ); |
| 152 |
} ); |
| 153 |
} ); |
| 154 |
|
| 155 |
$( document ).on( 'click', '.dologin-kl-copy-public-key', function() { |
| 156 |
var $button = $( this ); |
| 157 |
var text = $( '#dologin-kl-encryption-public-key' ).text(); |
| 158 |
dologin_clearPublicKeyCopyFeedback(); |
| 159 |
$button.prop( 'disabled', true ); |
| 160 |
dologin_copyToClipboard( text ).done( function( copied ) { |
| 161 |
if ( text !== $( '#dologin-kl-encryption-public-key' ).text() ) { |
| 162 |
dologin_resetPublicKeyCopyButton(); |
| 163 |
return; |
| 164 |
} |
| 165 |
dologin_showPublicKeyCopyFeedback( $button, copied ? dologin_admin.copied : dologin_admin.copy_failed, copied ); |
| 166 |
} ); |
| 167 |
} ); |
| 168 |
|
| 169 |
function dologin_copyToClipboardFallback( text ) { |
| 170 |
var copied = false; |
| 171 |
var $temp = $( '<textarea>' ) |
| 172 |
.attr( 'readonly', 'readonly' ) |
| 173 |
.css( { position: 'fixed', left: '-9999px', opacity: 0 } ) |
| 174 |
.appendTo( 'body' ) |
| 175 |
.val( text ); |
| 176 |
$temp[ 0 ].select(); |
| 177 |
try { |
| 178 |
copied = document.execCommand( 'copy' ); |
| 179 |
} catch ( error ) { |
| 180 |
copied = false; |
| 181 |
} |
| 182 |
$temp.remove(); |
| 183 |
return copied; |
| 184 |
} |
| 185 |
|
| 186 |
function dologin_copyToClipboard( text ) { |
| 187 |
var deferred = $.Deferred(); |
| 188 |
var fallback = function() { |
| 189 |
deferred.resolve( dologin_copyToClipboardFallback( text ) ); |
| 190 |
}; |
| 191 |
if ( navigator.clipboard && typeof navigator.clipboard.writeText === 'function' ) { |
| 192 |
try { |
| 193 |
navigator.clipboard.writeText( text ).then( function() { |
| 194 |
deferred.resolve( true ); |
| 195 |
}, fallback ); |
| 196 |
} catch ( error ) { |
| 197 |
fallback(); |
| 198 |
} |
| 199 |
} else { |
| 200 |
fallback(); |
| 201 |
} |
| 202 |
return deferred.promise(); |
| 203 |
} |
| 204 |
|
| 205 |
} ); } ); |
| 206 |
|