|
| 1 | +/** |
| 2 | + * Admin JavaScript for Github to WordPress Sync Plugin |
| 3 | + */ |
| 4 | + |
| 5 | +(function($) { |
| 6 | + 'use strict'; |
| 7 | + |
| 8 | + $(document).ready(function() { |
| 9 | + |
| 10 | + // Add Project Form Submit |
| 11 | + $('#gtws-add-project-form').on('submit', function(e) { |
| 12 | + e.preventDefault(); |
| 13 | + |
| 14 | + const $form = $(this); |
| 15 | + const $submitBtn = $form.find('button[type="submit"]'); |
| 16 | + const originalText = $submitBtn.html(); |
| 17 | + |
| 18 | + // Get form data |
| 19 | + const formData = { |
| 20 | + action: 'gtws_add_project', |
| 21 | + nonce: gtws_ajax.nonce, |
| 22 | + github_url: $('#github_url').val(), |
| 23 | + project_type: $('#project_type').val(), |
| 24 | + project_name: $('#project_name').val(), |
| 25 | + branch: $('#branch').val() |
| 26 | + }; |
| 27 | + |
| 28 | + // Disable submit button |
| 29 | + $submitBtn.prop('disabled', true).html('<span class="gtws-loading"></span> Adding...'); |
| 30 | + |
| 31 | + // Send AJAX request |
| 32 | + $.post(gtws_ajax.ajax_url, formData) |
| 33 | + .done(function(response) { |
| 34 | + if (response.success) { |
| 35 | + showNotice('Project added successfully!', 'success'); |
| 36 | + $form[0].reset(); |
| 37 | + $('#branch').val('main'); |
| 38 | + $('#branch-list').empty(); |
| 39 | + |
| 40 | + // Reload page after short delay to show new project |
| 41 | + setTimeout(function() { |
| 42 | + location.reload(); |
| 43 | + }, 1500); |
| 44 | + } else { |
| 45 | + showNotice('Error: ' + response.data, 'error'); |
| 46 | + } |
| 47 | + }) |
| 48 | + .fail(function() { |
| 49 | + showNotice('Failed to add project. Please try again.', 'error'); |
| 50 | + }) |
| 51 | + .always(function() { |
| 52 | + $submitBtn.prop('disabled', false).html(originalText); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + // Fetch Branches |
| 57 | + $('#fetch-branches').on('click', function() { |
| 58 | + const $btn = $(this); |
| 59 | + const githubUrl = $('#github_url').val(); |
| 60 | + |
| 61 | + if (!githubUrl) { |
| 62 | + showNotice('Please enter a GitHub URL first', 'warning'); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + const originalText = $btn.html(); |
| 67 | + $btn.prop('disabled', true).html('<span class="gtws-loading"></span> Fetching...'); |
| 68 | + |
| 69 | + $.post(gtws_ajax.ajax_url, { |
| 70 | + action: 'gtws_get_branches', |
| 71 | + nonce: gtws_ajax.nonce, |
| 72 | + github_url: githubUrl |
| 73 | + }) |
| 74 | + .done(function(response) { |
| 75 | + if (response.success) { |
| 76 | + displayBranches(response.data); |
| 77 | + showNotice('✓ Branches fetched successfully!', 'success'); |
| 78 | + } else { |
| 79 | + showNotice('❌ Error: ' + response.data, 'error', true); |
| 80 | + } |
| 81 | + }) |
| 82 | + .fail(function() { |
| 83 | + showNotice('❌ Failed to fetch branches', 'error', true); |
| 84 | + }) |
| 85 | + .always(function() { |
| 86 | + $btn.prop('disabled', false).html(originalText); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + // Display branches |
| 91 | + function displayBranches(branches) { |
| 92 | + const $branchList = $('#branch-list'); |
| 93 | + $branchList.empty(); |
| 94 | + |
| 95 | + if (branches && branches.length > 0) { |
| 96 | + branches.forEach(function(branch) { |
| 97 | + const $branchItem = $('<div>') |
| 98 | + .addClass('gtws-branch-item') |
| 99 | + .text(branch) |
| 100 | + .on('click', function() { |
| 101 | + $('#branch').val(branch); |
| 102 | + $('.gtws-branch-item').removeClass('active'); |
| 103 | + $(this).addClass('active'); |
| 104 | + }); |
| 105 | + |
| 106 | + $branchList.append($branchItem); |
| 107 | + }); |
| 108 | + } else { |
| 109 | + $branchList.html('<p>No branches found</p>'); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Check for Updates |
| 114 | + $(document).on('click', '.gtws-check-update', function() { |
| 115 | + const $btn = $(this); |
| 116 | + const projectId = $btn.data('project-id'); |
| 117 | + const $projectItem = $btn.closest('.gtws-project-item'); |
| 118 | + const $status = $projectItem.find('.gtws-project-status'); |
| 119 | + |
| 120 | + const originalText = $btn.html(); |
| 121 | + $btn.prop('disabled', true).html('<span class="gtws-loading"></span> Checking...'); |
| 122 | + |
| 123 | + $status.removeClass('success error warning').addClass('loading show') |
| 124 | + .html('Checking for updates...'); |
| 125 | + |
| 126 | + $.post(gtws_ajax.ajax_url, { |
| 127 | + action: 'gtws_check_updates', |
| 128 | + nonce: gtws_ajax.nonce, |
| 129 | + project_id: projectId |
| 130 | + }) |
| 131 | + .done(function(response) { |
| 132 | + if (response.success) { |
| 133 | + const data = response.data; |
| 134 | + |
| 135 | + if (data.has_update) { |
| 136 | + $status.removeClass('loading').addClass('warning') |
| 137 | + .html(` |
| 138 | + <strong>Update Available!</strong><br> |
| 139 | + Latest commit: <code>${data.commit.sha.substring(0, 7)}</code><br> |
| 140 | + Message: ${data.commit.message}<br> |
| 141 | + Date: ${formatDate(data.commit.date)} |
| 142 | + `); |
| 143 | + showNotice('🎉 Update Available! New commits are ready to sync.', 'warning', true); |
| 144 | + |
| 145 | + // Update commit info without page reload |
| 146 | + updateProjectCommitInfo($projectItem, data.commit); |
| 147 | + } else { |
| 148 | + $status.removeClass('loading').addClass('success') |
| 149 | + .html('<strong>✓ Already up to date!</strong>'); |
| 150 | + showNotice('✓ Already up to date! You have the latest version.', 'success'); |
| 151 | + |
| 152 | + // Hide status after 5 seconds |
| 153 | + setTimeout(function() { |
| 154 | + $status.removeClass('show'); |
| 155 | + }, 5000); |
| 156 | + } |
| 157 | + } else { |
| 158 | + $status.removeClass('loading').addClass('error') |
| 159 | + .html('<strong>Error:</strong> ' + response.data); |
| 160 | + showNotice('❌ Error: ' + response.data, 'error', true); |
| 161 | + } |
| 162 | + }) |
| 163 | + .fail(function() { |
| 164 | + $status.removeClass('loading').addClass('error') |
| 165 | + .html('<strong>Error:</strong> Failed to check for updates'); |
| 166 | + showNotice('❌ Failed to check for updates. Please try again.', 'error', true); |
| 167 | + }) |
| 168 | + .always(function() { |
| 169 | + $btn.prop('disabled', false).html(originalText); |
| 170 | + }); |
| 171 | + }); |
| 172 | + |
| 173 | + // Sync Project |
| 174 | + $(document).on('click', '.gtws-sync-project', function() { |
| 175 | + const $btn = $(this); |
| 176 | + const projectId = $btn.data('project-id'); |
| 177 | + const $projectItem = $btn.closest('.gtws-project-item'); |
| 178 | + const $status = $projectItem.find('.gtws-project-status'); |
| 179 | + |
| 180 | + if (!confirm('Are you sure you want to sync this project? This will overwrite local changes.')) { |
| 181 | + return; |
| 182 | + } |
| 183 | + |
| 184 | + const originalText = $btn.html(); |
| 185 | + $btn.prop('disabled', true).html('<span class="gtws-loading"></span> Syncing...'); |
| 186 | + |
| 187 | + // Disable other buttons |
| 188 | + $projectItem.find('button').prop('disabled', true); |
| 189 | + |
| 190 | + $status.removeClass('success error warning').addClass('loading show') |
| 191 | + .html('Syncing project from GitHub... This may take a moment.'); |
| 192 | + |
| 193 | + $.post(gtws_ajax.ajax_url, { |
| 194 | + action: 'gtws_sync_project', |
| 195 | + nonce: gtws_ajax.nonce, |
| 196 | + project_id: projectId |
| 197 | + }) |
| 198 | + .done(function(response) { |
| 199 | + if (response.success) { |
| 200 | + $status.removeClass('loading').addClass('success') |
| 201 | + .html(` |
| 202 | + <strong>✓ Sync Completed Successfully!</strong><br> |
| 203 | + ${response.data.message}<br> |
| 204 | + Target: ${response.data.details.target_dir} |
| 205 | + `); |
| 206 | + showNotice('Project synced successfully!', 'success'); |
| 207 | + |
| 208 | + // Reload page after short delay |
| 209 | + setTimeout(function() { |
| 210 | + location.reload(); |
| 211 | + }, 2000); |
| 212 | + } else { |
| 213 | + $status.removeClass('loading').addClass('error') |
| 214 | + .html('<strong>Sync Failed:</strong> ' + response.data); |
| 215 | + showNotice('❌ Sync failed: ' + response.data, 'error', true); |
| 216 | + } |
| 217 | + }) |
| 218 | + .fail(function() { |
| 219 | + $status.removeClass('loading').addClass('error') |
| 220 | + .html('<strong>Error:</strong> Failed to sync project'); |
| 221 | + showNotice('❌ Failed to sync project. Please check your connection and try again.', 'error', true); |
| 222 | + }) |
| 223 | + .always(function() { |
| 224 | + $btn.prop('disabled', false).html(originalText); |
| 225 | + $projectItem.find('button').prop('disabled', false); |
| 226 | + }); |
| 227 | + }); |
| 228 | + |
| 229 | + // Delete Project |
| 230 | + $(document).on('click', '.gtws-delete-project', function() { |
| 231 | + const $btn = $(this); |
| 232 | + const projectId = $btn.data('project-id'); |
| 233 | + const $projectItem = $btn.closest('.gtws-project-item'); |
| 234 | + |
| 235 | + if (!confirm('Are you sure you want to delete this project configuration? This will not delete the actual files.')) { |
| 236 | + return; |
| 237 | + } |
| 238 | + |
| 239 | + $btn.prop('disabled', true); |
| 240 | + $projectItem.css('opacity', '0.5'); |
| 241 | + |
| 242 | + $.post(gtws_ajax.ajax_url, { |
| 243 | + action: 'gtws_delete_project', |
| 244 | + nonce: gtws_ajax.nonce, |
| 245 | + project_id: projectId |
| 246 | + }) |
| 247 | + .done(function(response) { |
| 248 | + if (response.success) { |
| 249 | + $projectItem.slideUp(300, function() { |
| 250 | + $(this).remove(); |
| 251 | + |
| 252 | + // Check if no projects left |
| 253 | + if ($('.gtws-project-item').length === 0) { |
| 254 | + location.reload(); |
| 255 | + } |
| 256 | + }); |
| 257 | + showNotice('Project deleted successfully', 'success'); |
| 258 | + } else { |
| 259 | + showNotice('❌ Error: ' + response.data, 'error', true); |
| 260 | + $projectItem.css('opacity', '1'); |
| 261 | + $btn.prop('disabled', false); |
| 262 | + } |
| 263 | + }) |
| 264 | + .fail(function() { |
| 265 | + showNotice('❌ Failed to delete project', 'error', true); |
| 266 | + $projectItem.css('opacity', '1'); |
| 267 | + $btn.prop('disabled', false); |
| 268 | + }); |
| 269 | + }); |
| 270 | + |
| 271 | + // Show notification |
| 272 | + function showNotice(message, type, persistent) { |
| 273 | + // Remove existing notices of the same type to avoid clutter |
| 274 | + $('.gtws-notice.' + type).remove(); |
| 275 | + |
| 276 | + const $closeBtn = $('<button>') |
| 277 | + .addClass('gtws-notice-close') |
| 278 | + .html('×') |
| 279 | + .attr('type', 'button') |
| 280 | + .attr('aria-label', 'Close notification'); |
| 281 | + |
| 282 | + const $notice = $('<div>') |
| 283 | + .addClass('gtws-notice') |
| 284 | + .addClass(type || 'success') |
| 285 | + .html('<div class="gtws-notice-content">' + message + '</div>') |
| 286 | + .append($closeBtn); |
| 287 | + |
| 288 | + $('body').append($notice); |
| 289 | + |
| 290 | + // Close button handler |
| 291 | + $closeBtn.on('click', function() { |
| 292 | + $notice.addClass('hiding'); |
| 293 | + setTimeout(function() { |
| 294 | + $notice.remove(); |
| 295 | + }, 300); |
| 296 | + }); |
| 297 | + |
| 298 | + // Auto-dismiss only if not persistent |
| 299 | + if (!persistent) { |
| 300 | + setTimeout(function() { |
| 301 | + if ($notice.length) { |
| 302 | + $notice.addClass('hiding'); |
| 303 | + setTimeout(function() { |
| 304 | + $notice.remove(); |
| 305 | + }, 300); |
| 306 | + } |
| 307 | + }, 8000); // Increased from 4000 to 8000ms |
| 308 | + } |
| 309 | + } |
| 310 | + |
| 311 | + // Update project commit info without reload |
| 312 | + function updateProjectCommitInfo($projectItem, commit) { |
| 313 | + // This updates the DOM with new commit info |
| 314 | + // Could be expanded to update specific elements if needed |
| 315 | + } |
| 316 | + |
| 317 | + // Format date |
| 318 | + function formatDate(dateString) { |
| 319 | + const date = new Date(dateString); |
| 320 | + return date.toLocaleDateString() + ' ' + date.toLocaleTimeString(); |
| 321 | + } |
| 322 | + |
| 323 | + // Auto-fill project name from GitHub URL |
| 324 | + $('#github_url').on('blur', function() { |
| 325 | + const url = $(this).val(); |
| 326 | + const $projectName = $('#project_name'); |
| 327 | + |
| 328 | + // Only auto-fill if project name is empty |
| 329 | + if (!$projectName.val() && url) { |
| 330 | + const match = url.match(/github\.com\/[^\/]+\/([^\/]+)/); |
| 331 | + if (match && match[1]) { |
| 332 | + let repoName = match[1].replace('.git', ''); |
| 333 | + $projectName.val(repoName); |
| 334 | + } |
| 335 | + } |
| 336 | + }); |
| 337 | + |
| 338 | + }); |
| 339 | + |
| 340 | +})(jQuery); |
0 commit comments