| 1 |
/** |
| 2 |
* Charitable Logger Admin JS. |
| 3 |
* |
| 4 |
* Handles the Logs tab UI interactions. |
| 5 |
* |
| 6 |
* @since 1.8.11 |
| 7 |
*/ |
| 8 |
|
| 9 |
/* global jQuery, charitableLogger */ |
| 10 |
|
| 11 |
( function( $ ) { |
| 12 |
'use strict'; |
| 13 |
|
| 14 |
var Logger = { |
| 15 |
|
| 16 |
/** |
| 17 |
* Initialize. |
| 18 |
*/ |
| 19 |
init: function() { |
| 20 |
this.bindEvents(); |
| 21 |
}, |
| 22 |
|
| 23 |
/** |
| 24 |
* Bind event handlers. |
| 25 |
*/ |
| 26 |
bindEvents: function() { |
| 27 |
// Modal: click log title link. |
| 28 |
$( document ).on( 'click', '.js-charitable-log-link', this.openModal ); |
| 29 |
|
| 30 |
// Modal: close. |
| 31 |
$( document ).on( 'click', '.charitable-log-modal-close, .charitable-log-modal-overlay', this.closeModal ); |
| 32 |
$( document ).on( 'keydown', this.escCloseModal ); |
| 33 |
|
| 34 |
// Settings: toggle logging. |
| 35 |
$( '#charitable-logs-enable' ).on( 'change', this.toggleLogging ); |
| 36 |
|
| 37 |
// Settings: retention days. |
| 38 |
$( '#charitable-logs-retention' ).on( 'change', this.saveRetention ); |
| 39 |
|
| 40 |
// Delete All. |
| 41 |
$( '#charitable-delete-all-logs' ).on( 'click', this.deleteAll ); |
| 42 |
}, |
| 43 |
|
| 44 |
/** |
| 45 |
* Open the log detail modal via AJAX. |
| 46 |
* |
| 47 |
* @param {Event} e Click event. |
| 48 |
*/ |
| 49 |
openModal: function( e ) { |
| 50 |
e.preventDefault(); |
| 51 |
|
| 52 |
var recordId = $( this ).data( 'record-id' ), |
| 53 |
$modal = $( '#charitable-log-modal' ); |
| 54 |
|
| 55 |
// Show modal with loading state. |
| 56 |
$modal.show(); |
| 57 |
$modal.find( '.charitable-log-modal-title' ).text( '...' ); |
| 58 |
$modal.find( '.charitable-log-modal-message' ).html( '' ); |
| 59 |
|
| 60 |
$.post( charitableLogger.ajaxUrl, { |
| 61 |
action: 'charitable_get_log_record', |
| 62 |
nonce: charitableLogger.nonce, |
| 63 |
record_id: recordId |
| 64 |
}, function( response ) { |
| 65 |
if ( ! response.success ) { |
| 66 |
$modal.find( '.charitable-log-modal-title' ).text( charitableLogger.i18n.error ); |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
var data = response.data; |
| 71 |
|
| 72 |
$modal.find( '.charitable-log-modal-title' ).text( data.title ); |
| 73 |
$modal.find( '.charitable-log-modal-date' ).text( data.date ); |
| 74 |
$modal.find( '.charitable-log-modal-level' ).html( |
| 75 |
'<span class="charitable-log-level charitable-log-level-' + data.level_key + '">' + data.level + '</span>' |
| 76 |
); |
| 77 |
$modal.find( '.charitable-log-modal-types' ).text( data.types ); |
| 78 |
$modal.find( '.charitable-log-modal-source' ).text( data.source ); |
| 79 |
$modal.find( '.charitable-log-modal-message' ).html( data.message ); |
| 80 |
|
| 81 |
// Build IDs section. |
| 82 |
var ids = [], |
| 83 |
idFields = { |
| 84 |
campaign_id: 'Campaign', |
| 85 |
donation_id: 'Donation', |
| 86 |
donor_id: 'Donor', |
| 87 |
user_id: 'User', |
| 88 |
object_id: 'Object ID', |
| 89 |
object_type: 'Object Type' |
| 90 |
}; |
| 91 |
|
| 92 |
$.each( idFields, function( key, label ) { |
| 93 |
var val = data[ key ]; |
| 94 |
if ( val && val !== '0' && val !== 0 ) { |
| 95 |
var display = val, |
| 96 |
link = ''; |
| 97 |
|
| 98 |
if ( key === 'campaign_id' ) { |
| 99 |
link = '<a href="' + window.ajaxurl.replace( 'admin-ajax.php', 'post.php?post=' + val + '&action=edit' ) + '" target="_blank">' + val + '</a>'; |
| 100 |
} else if ( key === 'donation_id' ) { |
| 101 |
link = '<a href="' + window.ajaxurl.replace( 'admin-ajax.php', 'post.php?post=' + val + '&action=edit' ) + '" target="_blank">' + val + '</a>'; |
| 102 |
} else if ( key === 'donor_id' ) { |
| 103 |
link = '<a href="' + window.ajaxurl.replace( 'admin-ajax.php', 'admin.php?page=charitable-donors&donor_id=' + val ) + '" target="_blank">' + val + '</a>'; |
| 104 |
} |
| 105 |
|
| 106 |
ids.push( '<div class="charitable-log-modal-id-item"><strong>' + label + ':</strong> ' + ( link || display ) + '</div>' ); |
| 107 |
} |
| 108 |
} ); |
| 109 |
|
| 110 |
if ( ids.length > 0 ) { |
| 111 |
$modal.find( '.charitable-log-modal-ids' ).show(); |
| 112 |
$modal.find( '.charitable-log-modal-ids-grid' ).html( ids.join( '' ) ); |
| 113 |
} else { |
| 114 |
$modal.find( '.charitable-log-modal-ids' ).hide(); |
| 115 |
} |
| 116 |
} ); |
| 117 |
}, |
| 118 |
|
| 119 |
/** |
| 120 |
* Close the modal. |
| 121 |
*/ |
| 122 |
closeModal: function() { |
| 123 |
$( '#charitable-log-modal' ).hide(); |
| 124 |
}, |
| 125 |
|
| 126 |
/** |
| 127 |
* Close modal on Escape key. |
| 128 |
* |
| 129 |
* @param {Event} e Keydown event. |
| 130 |
*/ |
| 131 |
escCloseModal: function( e ) { |
| 132 |
if ( e.key === 'Escape' ) { |
| 133 |
Logger.closeModal(); |
| 134 |
} |
| 135 |
}, |
| 136 |
|
| 137 |
/** |
| 138 |
* Toggle logging on/off. |
| 139 |
*/ |
| 140 |
toggleLogging: function() { |
| 141 |
var enabled = $( this ).is( ':checked' ) ? 1 : 0, |
| 142 |
$status = $( '.charitable-logs-status' ); |
| 143 |
|
| 144 |
$.post( charitableLogger.ajaxUrl, { |
| 145 |
action: 'charitable_toggle_logging', |
| 146 |
nonce: charitableLogger.nonce, |
| 147 |
enabled: enabled |
| 148 |
}, function( response ) { |
| 149 |
if ( response.success ) { |
| 150 |
$status.text( enabled ? $status.data( 'enabled' ) || 'Enabled' : $status.data( 'disabled' ) || 'Disabled' ); |
| 151 |
} |
| 152 |
} ); |
| 153 |
}, |
| 154 |
|
| 155 |
/** |
| 156 |
* Save retention days. |
| 157 |
*/ |
| 158 |
saveRetention: function() { |
| 159 |
var days = $( this ).val(); |
| 160 |
|
| 161 |
$.post( charitableLogger.ajaxUrl, { |
| 162 |
action: 'charitable_save_log_retention', |
| 163 |
nonce: charitableLogger.nonce, |
| 164 |
days: days |
| 165 |
} ); |
| 166 |
}, |
| 167 |
|
| 168 |
/** |
| 169 |
* Delete all logs. |
| 170 |
*/ |
| 171 |
deleteAll: function() { |
| 172 |
if ( ! window.confirm( charitableLogger.i18n.deleteConfirm ) ) { |
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
var $btn = $( this ); |
| 177 |
$btn.prop( 'disabled', true ).text( charitableLogger.i18n.deleting ); |
| 178 |
|
| 179 |
$.post( charitableLogger.ajaxUrl, { |
| 180 |
action: 'charitable_delete_all_logs', |
| 181 |
nonce: charitableLogger.nonce |
| 182 |
}, function( response ) { |
| 183 |
if ( response.success ) { |
| 184 |
window.location.reload(); |
| 185 |
} else { |
| 186 |
$btn.prop( 'disabled', false ).text( charitableLogger.i18n.error ); |
| 187 |
} |
| 188 |
} ); |
| 189 |
} |
| 190 |
}; |
| 191 |
|
| 192 |
$( document ).ready( function() { |
| 193 |
Logger.init(); |
| 194 |
} ); |
| 195 |
|
| 196 |
} )( jQuery ); |
| 197 |
|