| 1 |
/** |
| 2 |
* MetaSync Debug Mode Widget JavaScript |
| 3 |
* |
| 4 |
* Handles interactions for the debug mode dashboard widget |
| 5 |
* |
| 6 |
* @package MetaSync |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
/* global metasyncDebug */ |
| 11 |
|
| 12 |
(function($) { |
| 13 |
'use strict'; |
| 14 |
|
| 15 |
/** |
| 16 |
* Debug Mode Widget Handler |
| 17 |
*/ |
| 18 |
const DebugModeWidget = { |
| 19 |
/** |
| 20 |
* Initialize the widget |
| 21 |
*/ |
| 22 |
init: function() { |
| 23 |
this.bindEvents(); |
| 24 |
this.startPolling(); |
| 25 |
}, |
| 26 |
|
| 27 |
/** |
| 28 |
* Bind event handlers |
| 29 |
*/ |
| 30 |
bindEvents: function() { |
| 31 |
// Extend debug mode button |
| 32 |
$(document).on('click', '#metasync-extend-debug', function(e) { |
| 33 |
e.preventDefault(); |
| 34 |
DebugModeWidget.extendDebugMode(); |
| 35 |
}); |
| 36 |
|
| 37 |
// Disable debug mode button |
| 38 |
$(document).on('click', '#metasync-disable-debug', function(e) { |
| 39 |
e.preventDefault(); |
| 40 |
DebugModeWidget.disableDebugMode(); |
| 41 |
}); |
| 42 |
}, |
| 43 |
|
| 44 |
/** |
| 45 |
* Extend debug mode for another 24 hours |
| 46 |
*/ |
| 47 |
extendDebugMode: function() { |
| 48 |
const button = $('#metasync-extend-debug'); |
| 49 |
const originalText = button.text(); |
| 50 |
|
| 51 |
button.prop('disabled', true).text('Extending...'); |
| 52 |
|
| 53 |
$.ajax({ |
| 54 |
url: metasyncDebug.restUrl + 'extend', |
| 55 |
method: 'POST', |
| 56 |
beforeSend: function(xhr) { |
| 57 |
xhr.setRequestHeader('X-WP-Nonce', metasyncDebug.restNonce); |
| 58 |
}, |
| 59 |
success: function(response) { |
| 60 |
if (response.success) { |
| 61 |
DebugModeWidget.showNotice('Debug mode extended for another 24 hours.', 'success'); |
| 62 |
DebugModeWidget.updateWidget(response.status); |
| 63 |
} else { |
| 64 |
DebugModeWidget.showNotice('Failed to extend debug mode.', 'error'); |
| 65 |
} |
| 66 |
}, |
| 67 |
error: function() { |
| 68 |
DebugModeWidget.showNotice('Failed to extend debug mode.', 'error'); |
| 69 |
}, |
| 70 |
complete: function() { |
| 71 |
button.prop('disabled', false).text(originalText); |
| 72 |
} |
| 73 |
}); |
| 74 |
}, |
| 75 |
|
| 76 |
/** |
| 77 |
* Disable debug mode |
| 78 |
*/ |
| 79 |
disableDebugMode: function() { |
| 80 |
if (!confirm('Are you sure you want to disable debug mode?')) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
const button = $('#metasync-disable-debug'); |
| 85 |
const originalText = button.text(); |
| 86 |
|
| 87 |
button.prop('disabled', true).text('Disabling...'); |
| 88 |
|
| 89 |
$.ajax({ |
| 90 |
url: metasyncDebug.restUrl + 'disable', |
| 91 |
method: 'POST', |
| 92 |
beforeSend: function(xhr) { |
| 93 |
xhr.setRequestHeader('X-WP-Nonce', metasyncDebug.restNonce); |
| 94 |
}, |
| 95 |
success: function(response) { |
| 96 |
if (response.success) { |
| 97 |
DebugModeWidget.showNotice('Debug mode disabled successfully.', 'success'); |
| 98 |
// Remove widget or reload page |
| 99 |
setTimeout(function() { |
| 100 |
location.reload(); |
| 101 |
}, 1000); |
| 102 |
} else { |
| 103 |
DebugModeWidget.showNotice('Failed to disable debug mode.', 'error'); |
| 104 |
} |
| 105 |
}, |
| 106 |
error: function() { |
| 107 |
DebugModeWidget.showNotice('Failed to disable debug mode.', 'error'); |
| 108 |
}, |
| 109 |
complete: function() { |
| 110 |
button.prop('disabled', false).text(originalText); |
| 111 |
} |
| 112 |
}); |
| 113 |
}, |
| 114 |
|
| 115 |
/** |
| 116 |
* Update widget content with new status |
| 117 |
*/ |
| 118 |
updateWidget: function(status) { |
| 119 |
if (!status) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
// Update time remaining |
| 124 |
if (status.time_remaining_formatted) { |
| 125 |
$('.time-remaining').text(status.time_remaining_formatted); |
| 126 |
} |
| 127 |
|
| 128 |
// Update file size |
| 129 |
if (status.log_file_size_formatted && status.max_log_size_formatted) { |
| 130 |
$('.file-size').text(status.log_file_size_formatted + ' / ' + status.max_log_size_formatted); |
| 131 |
} |
| 132 |
|
| 133 |
// Update progress bar |
| 134 |
if (status.percentage_used !== undefined) { |
| 135 |
$('.progress-fill').css('width', status.percentage_used + '%'); |
| 136 |
} |
| 137 |
}, |
| 138 |
|
| 139 |
/** |
| 140 |
* Start polling for status updates |
| 141 |
*/ |
| 142 |
startPolling: function() { |
| 143 |
// Poll every 60 seconds |
| 144 |
setInterval(function() { |
| 145 |
DebugModeWidget.fetchStatus(); |
| 146 |
}, 60000); |
| 147 |
}, |
| 148 |
|
| 149 |
/** |
| 150 |
* Fetch current debug mode status |
| 151 |
*/ |
| 152 |
fetchStatus: function() { |
| 153 |
$.ajax({ |
| 154 |
url: metasyncDebug.restUrl + 'status', |
| 155 |
method: 'GET', |
| 156 |
beforeSend: function(xhr) { |
| 157 |
xhr.setRequestHeader('X-WP-Nonce', metasyncDebug.restNonce); |
| 158 |
}, |
| 159 |
success: function(response) { |
| 160 |
if (response && response.enabled) { |
| 161 |
DebugModeWidget.updateWidget(response); |
| 162 |
} else { |
| 163 |
// Debug mode was disabled, reload page |
| 164 |
location.reload(); |
| 165 |
} |
| 166 |
} |
| 167 |
}); |
| 168 |
}, |
| 169 |
|
| 170 |
/** |
| 171 |
* Show notice message |
| 172 |
*/ |
| 173 |
showNotice: function(message, type) { |
| 174 |
const noticeClass = 'notice notice-' + type + ' is-dismissible'; |
| 175 |
const notice = $('<div>', { |
| 176 |
class: noticeClass, |
| 177 |
html: '<p><strong>MetaSync Debug Mode:</strong> ' + message + '</p>' |
| 178 |
}); |
| 179 |
|
| 180 |
// Insert after page title |
| 181 |
if ($('.wrap h1').length) { |
| 182 |
$('.wrap h1').first().after(notice); |
| 183 |
} else { |
| 184 |
$('#wpbody-content').prepend(notice); |
| 185 |
} |
| 186 |
|
| 187 |
// Auto-dismiss after 5 seconds |
| 188 |
setTimeout(function() { |
| 189 |
notice.fadeOut(function() { |
| 190 |
$(this).remove(); |
| 191 |
}); |
| 192 |
}, 5000); |
| 193 |
} |
| 194 |
}; |
| 195 |
|
| 196 |
// Initialize when document is ready |
| 197 |
$(document).ready(function() { |
| 198 |
// Only initialize if widget exists |
| 199 |
if ($('#metasync_debug_mode_widget').length) { |
| 200 |
DebugModeWidget.init(); |
| 201 |
} |
| 202 |
}); |
| 203 |
|
| 204 |
})(jQuery); |
| 205 |
|