← All changes
|
modules/api-backups/assets/js/module-api-backups.js
+123
-105
5.0.2
→
6.2.2
View file →
| @@ -1,66 +1,68 @@ | ||
| 1 | 1 | /* eslint complexity: ["error", 100] */ |
| 2 | -var apibackups_bulkMaxThreads = 1; // rate limiting. | |
| 3 | -var apibackups_bulkTotalThreads = 0; | |
| 4 | -var apibackups_bulkCurrentThreads = 0; | |
| 5 | -var apibackups_bulkFinishedThreads = 0; | |
| 2 | +let apibackups_bulkMaxThreads = 1; // rate limiting. | |
| 3 | +let apibackups_bulkTotalThreads = 0; | |
| 4 | +let apibackups_bulkCurrentThreads = 0; | |
| 5 | +let apibackups_bulkFinishedThreads = 0; | |
| 6 | 6 | |
| 7 | -mainwp_api_backups_do_backups = function (pObj) { | |
| 8 | - // init queue status. | |
| 9 | - jQuery('#mainwp-3rd-party-backups-table tbody td.check-column INPUT[type=checkbox]').each(function () { | |
| 10 | - jQuery(this).attr('status', 'queue'); | |
| 11 | - }); | |
| 7 | +let mainwp_api_backups_do_backups = function (pObj) { | |
| 8 | + // init queue status. | |
| 9 | + jQuery('#mainwp-3rd-party-backups-table tbody td.check-column INPUT[type=checkbox]').each(function () { | |
| 10 | + jQuery(this).attr('status', 'queue'); | |
| 11 | + }); | |
| 12 | 12 | |
| 13 | - // Add loading icon to all selected sites. | |
| 14 | - jQuery('#mainwp-3rd-party-backups-table tbody td.check-column INPUT[type=checkbox]:checked').each(function () { | |
| 15 | - var parent = jQuery(this).closest('tr'); | |
| 16 | - var statusEl = parent.find('.running'); | |
| 17 | - statusEl.html('<i class="clock outline icon"></i>'); | |
| 18 | - }); | |
| 13 | + // Add loading icon to all selected sites. | |
| 14 | + jQuery('#mainwp-3rd-party-backups-table tbody td.check-column INPUT[type=checkbox]:checked').each(function () { | |
| 15 | + let parent = jQuery(this).closest('tr'); | |
| 16 | + let statusEl = parent.find('.running'); | |
| 17 | + statusEl.html('<i class="clock outline icon"></i>'); | |
| 18 | + }); | |
| 19 | 19 | |
| 20 | - // Get selected sites. | |
| 21 | - var selector = '#mainwp-3rd-party-backups-table tbody td.check-column INPUT[type=checkbox]:checked[status=queue]'; | |
| 22 | - var selectedIds = jQuery.map(jQuery(selector), function (el) { | |
| 23 | - return jQuery(el).val(); | |
| 24 | - }); | |
| 20 | + // Get selected sites. | |
| 21 | + let selector = '#mainwp-3rd-party-backups-table tbody td.check-column INPUT[type=checkbox]:checked[status=queue]'; | |
| 22 | + let selectedIds = jQuery.map(jQuery(selector), function (el) { | |
| 23 | + return jQuery(el).val(); | |
| 24 | + }); | |
| 25 | 25 | |
| 26 | - // Check if there are selected sites. | |
| 27 | - if (selectedIds.length == 0) { | |
| 26 | + // Check if there are selected sites. | |
| 27 | + if (selectedIds.length == 0) { | |
| 28 | 28 | |
| 29 | - // Show message. | |
| 30 | - jQuery('#mainwp-api-backups-message-zone').addClass('red').show(); | |
| 31 | - jQuery('#mainwp-api-backups-message-zone .content .message') | |
| 32 | - .html('Please select at least one website.') | |
| 33 | - ; | |
| 34 | - return; | |
| 35 | - } | |
| 29 | + // Show message. | |
| 30 | + jQuery('#mainwp-api-backups-message-zone').addClass('red').show(); | |
| 31 | + jQuery('#mainwp-api-backups-message-zone .content .message') | |
| 32 | + .html('Please select at least one website.') | |
| 33 | + ; | |
| 34 | + return; | |
| 35 | + } | |
| 36 | 36 | |
| 37 | - // Set queue metadata and proceed. | |
| 38 | - apibackups_bulkTotalThreads = selectedIds.length; | |
| 39 | - apibackups_bulkCurrentThreads = 0; | |
| 40 | - apibackups_bulkFinishedThreads = 0; | |
| 37 | + // Set queue metadata and proceed. | |
| 38 | + apibackups_bulkTotalThreads = selectedIds.length; | |
| 39 | + apibackups_bulkCurrentThreads = 0; | |
| 40 | + apibackups_bulkFinishedThreads = 0; | |
| 41 | 41 | |
| 42 | 42 | |
| 43 | - jQuery(pObj).addClass('disabled'); | |
| 44 | - mainwp_api_backups_do_backups_specific_next(selector); | |
| 43 | + jQuery(pObj).addClass('disabled'); | |
| 44 | + mainwp_api_backups_do_backups_specific_next(selector); | |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | |
| 48 | -mainwp_api_backups_do_backups_specific_next = function (selector) { | |
| 49 | - while ((objProcess = jQuery(selector + ':first')) && objProcess.length > 0 && apibackups_bulkCurrentThreads < apibackups_bulkMaxThreads) { | |
| 50 | - objProcess.attr('status', 'proceed'); | |
| 51 | - mainwp_api_backups_do_backups_specific(objProcess, true, selector); | |
| 52 | - } | |
| 48 | +let mainwp_api_backups_do_backups_specific_next = function (selector) { | |
| 49 | + let objProcess = jQuery(selector + ':first'); | |
| 50 | + while (objProcess && objProcess.length > 0 && apibackups_bulkCurrentThreads < apibackups_bulkMaxThreads) { // NOSONAR - variables modified outside the function. | |
| 51 | + objProcess.attr('status', 'proceed'); | |
| 52 | + mainwp_api_backups_do_backups_specific(objProcess, true, selector); | |
| 53 | + objProcess = jQuery(selector + ':first'); | |
| 54 | + } | |
| 53 | 55 | |
| 54 | - if (apibackups_bulkTotalThreads > 0 && apibackups_bulkFinishedThreads == apibackups_bulkTotalThreads) { | |
| 55 | - jQuery('#action_backup_selected_sites').removeClass('disabled'); | |
| 56 | + if (apibackups_bulkTotalThreads > 0 && apibackups_bulkFinishedThreads == apibackups_bulkTotalThreads) { | |
| 57 | + jQuery('#action_backup_selected_sites').removeClass('disabled'); | |
| 56 | 58 | |
| 57 | - // Show message. | |
| 58 | - jQuery('#mainwp-api-backups-message-zone').removeClass('red').addClass('green').show(); | |
| 59 | - jQuery('#mainwp-api-backups-message-zone .content .message') | |
| 60 | - .html( 'API Backup requests sent. Please allow some time for the backup to complete, then Refresh Available Backups.') | |
| 61 | - ; | |
| 62 | - } | |
| 59 | + // Show message. | |
| 60 | + jQuery('#mainwp-api-backups-message-zone').removeClass('red').addClass('green').show(); | |
| 61 | + jQuery('#mainwp-api-backups-message-zone .content .message') | |
| 62 | + .html('API Backup requests sent. Please allow some time for the backup to complete, then Refresh Available Backups.') | |
| 63 | + ; | |
| 64 | + } | |
| 63 | 65 | |
| 64 | 66 | |
| 65 | 67 | |
| 66 | 68 | } |
| @@ -73,72 +75,88 @@ | ||
| 73 | 75 | * |
| 74 | 76 | * mainwp_api_backups_selected_websites() `action` is defined in assets/classes/class-api-backups-handler.php admin_init(), |
| 75 | 77 | * & fires off ajax_backups_selected_websites() function also defined in assets/classes/class-api-backups-handler.php |
| 76 | 78 | */ |
| 77 | -mainwp_api_backups_do_backups_specific = function (pObj, bulk, selector) { | |
| 78 | - var parent = pObj.closest('tr'); | |
| 79 | - var statusEl = parent.find('.running'); | |
| 80 | - var providerName = parent.attr('provider-name'); | |
| 79 | +let mainwp_api_backups_do_backups_specific = function (pObj, bulk, selector) { | |
| 80 | + let parent = pObj.closest('tr'); | |
| 81 | + let statusEl = parent.find('.running'); | |
| 82 | + let providerName = parent.attr('provider-name'); | |
| 81 | 83 | |
| 82 | - var data = mainwp_secure_data({ | |
| 83 | - action: 'mainwp_api_backups_selected_websites', | |
| 84 | - websiteId: parent.attr('website-id'), | |
| 85 | - bulk_backups: 1, | |
| 86 | - }); | |
| 84 | + let data = mainwp_secure_data({ | |
| 85 | + action: 'mainwp_api_backups_selected_websites', | |
| 86 | + websiteId: parent.attr('website-id'), | |
| 87 | + bulk_backups: 1, | |
| 88 | + }); | |
| 87 | 89 | |
| 88 | - if (bulk) { | |
| 89 | - apibackups_bulkCurrentThreads++; | |
| 90 | - } | |
| 90 | + if (bulk) { | |
| 91 | + apibackups_bulkCurrentThreads++; | |
| 92 | + } | |
| 91 | 93 | |
| 92 | - jQuery(pObj).prop('checked', false); | |
| 93 | - statusEl.html('<i class="notched circle loading icon"></i>'); | |
| 94 | + jQuery(pObj).prop('checked', false); | |
| 95 | + statusEl.html('<i class="notched circle loading icon"></i>'); | |
| 94 | 96 | |
| 95 | - jQuery.post(ajaxurl, data, function (response) { | |
| 96 | - statusEl.html(''); | |
| 97 | - var err_msg = ''; | |
| 98 | - var succ_msg = ''; | |
| 99 | - var rsp = ''; | |
| 97 | + jQuery.post(ajaxurl, data, function (response) { | |
| 98 | + statusEl.html(''); | |
| 99 | + let err_msg = ''; | |
| 100 | + let succ_msg = ''; | |
| 101 | + let rsp = ''; | |
| 100 | 102 | |
| 101 | - //console.log ( response ); | |
| 103 | + if (response && !response.success) { | |
| 104 | + rsp = response.data; | |
| 105 | + // Check for gridPane error.. | |
| 106 | + if (rsp && rsp['0'].code === 429) { | |
| 107 | + err_msg = 'API rate limit has been met. Please wait 30 seconds and try again.'; | |
| 108 | + } else { | |
| 109 | + // Everything else. (API provided error message) | |
| 110 | + err_msg = rsp['0'].message; | |
| 111 | + } | |
| 112 | + } else if (response?.success) { | |
| 113 | + succ_msg = MainWP.I18n.t('Successful'); | |
| 114 | + } else if (response?.error) { // Check for cPanel error.. | |
| 115 | + err_msg = MainWP.I18n.t(response.error); | |
| 116 | + } else { | |
| 117 | + err_msg = _('Undefined error.'); | |
| 118 | + } | |
| 102 | 119 | |
| 103 | - if (response && response.success == false) { | |
| 104 | - rsp = response.data; | |
| 105 | - console.log(rsp); | |
| 106 | - // Check for gridPane error.. | |
| 107 | - if (rsp && rsp['0'].code === 429) { | |
| 108 | - err_msg = 'API rate limit has been met. Please wait 30 seconds and try again.'; | |
| 109 | - } else { | |
| 110 | - // Everything else. (API provided error message) | |
| 111 | - err_msg = rsp['0'].message; | |
| 112 | - } | |
| 113 | - } else if (response && response.success) { | |
| 114 | - succ_msg = __('Successfull'); | |
| 115 | - } else if (response && response.error) { // Check for cPanel error.. | |
| 116 | - err_msg = __(response.error); | |
| 117 | - } else { | |
| 118 | - err_msg = _('Undefined error.'); | |
| 119 | - } | |
| 120 | + if ('' != err_msg) { | |
| 121 | + statusEl.html('<span data-inverted="" data-position="right center" data-tooltip="' + err_msg + '"><i class="red times icon"></i></span>'); | |
| 122 | + } else if ('' != succ_msg) { | |
| 123 | + statusEl.html('<span data-inverted="" data-position="right center" data-tooltip="' + succ_msg + '"><i class="green check icon"></i></span>'); | |
| 124 | + } | |
| 120 | 125 | |
| 121 | - if ('' != err_msg) { | |
| 122 | - statusEl.html('<span data-inverted="" data-position="right center" data-tooltip="' + err_msg + '"><i class="red times icon"></i></span>'); | |
| 123 | - } else if ('' != succ_msg) { | |
| 124 | - statusEl.html('<span data-inverted="" data-position="right center" data-tooltip="' + succ_msg + '"><i class="green check icon"></i></span>'); | |
| 125 | - } | |
| 126 | + if (bulk) { | |
| 127 | + apibackups_bulkCurrentThreads--; | |
| 128 | + apibackups_bulkFinishedThreads++; | |
| 129 | + if (providerName === 'GridPane') { | |
| 130 | + // GridPane API BETA has Strict rate limits. However, rate limits will increase and vary between | |
| 131 | + // endpoints once the API is out of BETA. | |
| 132 | + setTimeout(function () { | |
| 133 | + mainwp_api_backups_do_backups_specific_next(selector); | |
| 134 | + }, 30000); // 30 second Rate limit. | |
| 135 | + } else { | |
| 136 | + mainwp_api_backups_do_backups_specific_next(selector); | |
| 137 | + } | |
| 138 | + } | |
| 126 | 139 | |
| 127 | - if (bulk) { | |
| 128 | - apibackups_bulkCurrentThreads--; | |
| 129 | - apibackups_bulkFinishedThreads++; | |
| 130 | - if (providerName === 'GridPane') { | |
| 131 | - // GridPane API BETA has Strict rate limits. However, rate limits will increase and vary between | |
| 132 | - // endpoints once the API is out of BETA. | |
| 133 | - setTimeout(function () { | |
| 134 | - mainwp_api_backups_do_backups_specific_next(selector); | |
| 135 | - }, 30000); // 30 second Rate limit. | |
| 136 | - } else { | |
| 137 | - mainwp_api_backups_do_backups_specific_next(selector); | |
| 138 | - } | |
| 139 | - } | |
| 140 | + }, 'json'); | |
| 141 | + return false; | |
| 142 | +} | |
| 140 | 143 | |
| 141 | - }, 'json'); | |
| 142 | - return false; | |
| 144 | +let mainwp_api_backups_update_button_state = function () { | |
| 145 | + let checkedCount = jQuery('#mainwp-3rd-party-backups-table tbody td.check-column INPUT[type=checkbox]:checked').length; | |
| 146 | + if (checkedCount > 0) { | |
| 147 | + jQuery('#action_backup_selected_sites').removeClass('disabled'); | |
| 148 | + } else { | |
| 149 | + jQuery('#action_backup_selected_sites').addClass('disabled'); | |
| 150 | + } | |
| 143 | 151 | } |
| 152 | + | |
| 153 | +jQuery(document).ready(function () { | |
| 154 | + jQuery('#mainwp-3rd-party-backups-table').on('change', 'tbody td.check-column INPUT[type=checkbox]', function () { | |
| 155 | + mainwp_api_backups_update_button_state(); | |
| 156 | + }); | |
| 157 | + | |
| 158 | + jQuery('#cb-select-all-top').on('change', function () { | |
| 159 | + mainwp_api_backups_update_button_state(); | |
| 160 | + }); | |
| 161 | +}); | |
| 144 | 162 | |