| 1 |
let mainwp_module_cost_tracker_valid_input_data = function () { |
| 2 |
let errors = []; |
| 3 |
let selected_sites = []; |
| 4 |
let selected_groups = []; |
| 5 |
let selected_clients = []; |
| 6 |
|
| 7 |
if (jQuery.trim(jQuery('#mainwp_module_cost_tracker_edit_name').val()) == '') { |
| 8 |
errors.push('Title is required.'); |
| 9 |
} |
| 10 |
|
| 11 |
if (jQuery('input[name="select_by"]').val() == 'site') { |
| 12 |
jQuery("input[name='selected_sites[]']:checked").each(function () { |
| 13 |
selected_sites.push(jQuery(this).val()); |
| 14 |
}); |
| 15 |
if (selected_sites.length == 0) { |
| 16 |
errors.push('Please select websites or groups or clients.'); |
| 17 |
} |
| 18 |
} else if (jQuery('input[name="select_by"]').val() == 'group') { |
| 19 |
jQuery("input[name='selected_groups[]']:checked").each(function () { |
| 20 |
selected_groups.push(jQuery(this).val()); |
| 21 |
}); |
| 22 |
if (selected_groups.length == 0) { |
| 23 |
errors.push('Please select websites or groups or clients.'); |
| 24 |
} |
| 25 |
} else if (jQuery('input[name="select_by"]').val() == 'client') { |
| 26 |
jQuery("input[name='selected_clients[]']:checked").each(function () { |
| 27 |
selected_clients.push(jQuery(this).val()); |
| 28 |
}); |
| 29 |
if (selected_clients.length == 0) { |
| 30 |
errors.push('Please select websites or groups or clients.'); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
if (errors.length > 0) { |
| 35 |
jQuery('#mainwp-module-cost-tracker-error-zone').show(); |
| 36 |
jQuery('#mainwp-module-cost-tracker-error-zone .error-message').html(errors.join('<br />')); |
| 37 |
return false; |
| 38 |
} else { |
| 39 |
jQuery('#mainwp-module-cost-tracker-error-zone').fadeOut(1000); |
| 40 |
jQuery('#mainwp-module-cost-tracker-error-zone .error-message').html(""); |
| 41 |
} |
| 42 |
return true; |
| 43 |
} |
| 44 |
|
| 45 |
jQuery(document).on('click', '#mainwp-module-cost-tracker-save-tracker-button', function () { |
| 46 |
if (mainwp_module_cost_tracker_valid_input_data() === false) { |
| 47 |
scrollElementTop('mainwp-module-cost-tracker-error-zone'); |
| 48 |
return false; |
| 49 |
} |
| 50 |
}); |
| 51 |
|
| 52 |
jQuery(function ($) { |
| 53 |
// Check all checkboxes |
| 54 |
jQuery('#mainwp-module-cost-tracker-sites-table th input[type="checkbox"]').change(function () { |
| 55 |
let checkboxes = jQuery('#mainwp-module-cost-tracker-sites-table').find(':checkbox'); |
| 56 |
if (jQuery(this).prop('checked')) { |
| 57 |
checkboxes.prop('checked', true); |
| 58 |
} else { |
| 59 |
checkboxes.prop('checked', false); |
| 60 |
} |
| 61 |
}); |
| 62 |
|
| 63 |
jQuery('.mainwp-module-cost-tracker-score.label').tab(); |
| 64 |
|
| 65 |
jQuery(document).on('click', '.subscription_menu_item_delete', function () { |
| 66 |
let objDel = jQuery(this); |
| 67 |
mainwp_confirm(MainWP.I18n.t('Are you sure?'), function () { |
| 68 |
mainwp_module_cost_tracker_delete_start_specific(objDel, '', false); |
| 69 |
}, false, false, true); |
| 70 |
}) |
| 71 |
|
| 72 |
// Trigger the bulk actions |
| 73 |
jQuery('#mainwp_module_cost_tracker_action_btn').on('click', function () { |
| 74 |
let bulk_act = jQuery('#mwp_cost_tracker_bulk_action').dropdown("get value"); |
| 75 |
mainwp_module_cost_tracker_table_bulk_action(bulk_act); |
| 76 |
}); |
| 77 |
|
| 78 |
jQuery('#mainwp-module-cost-tracker-settings-form .ui.calendar').calendar({ |
| 79 |
type: 'date', |
| 80 |
monthFirst: false, |
| 81 |
formatter: { |
| 82 |
date: function (date) { |
| 83 |
if (!date) return ''; |
| 84 |
let day = date.getDate(); |
| 85 |
let month = date.getMonth() + 1; |
| 86 |
let year = date.getFullYear(); |
| 87 |
|
| 88 |
if (month < 10) { |
| 89 |
month = '0' + month; |
| 90 |
} |
| 91 |
if (day < 10) { |
| 92 |
day = '0' + day; |
| 93 |
} |
| 94 |
return year + '-' + month + '-' + day; |
| 95 |
} |
| 96 |
} |
| 97 |
}); |
| 98 |
|
| 99 |
jQuery(document).on('click', '.mainwp-edit-sub-note', function () { |
| 100 |
let parent = jQuery(this).closest('tr'); |
| 101 |
let id = jQuery(parent).attr('item-id'); |
| 102 |
let note = jQuery('#sub-notes-' + id + '-note').html(); |
| 103 |
jQuery('#mainwp-notes-subs-html').html(note == '' ? MainWP.I18n.t('No saved notes. Click the Edit button to edit site notes.') : note); |
| 104 |
jQuery('#mainwp-notes-subs-note').val(note); |
| 105 |
jQuery('#mainwp-notes-subs-subid').val(id); |
| 106 |
mainwp_module_cost_tracker_notes_show(); |
| 107 |
return false; |
| 108 |
}); |
| 109 |
|
| 110 |
$(document).on('click', '.module-cost-tracker-add-custom-product-types', function () { |
| 111 |
jQuery('.cost-tracker-product-types-bottom').before(jQuery(this).attr('add-custom-product-types-tmpl')); |
| 112 |
let justAdded = jQuery(this).prev().prev(); |
| 113 |
jQuery(justAdded).find('.mainwp-module-cost-tracker-select-custom-product-types-icons').dropdown({ |
| 114 |
onChange: function (val) { |
| 115 |
let parent = jQuery(this).closest('.cost_tracker_settings_product_categories_icon_wrapper'); |
| 116 |
jQuery(parent).find('input[name="cost_tracker_custom_product_types[icon][]"]').val('deficon:' + val); |
| 117 |
} |
| 118 |
}); |
| 119 |
}); |
| 120 |
|
| 121 |
$(document).on('click', '.module-cost-tracker-add-custom-payment-methods', function () { |
| 122 |
jQuery('.cost-tracker-payment-methods-bottom').before(jQuery(this).attr('add-custom-payment-methods-tmpl')); |
| 123 |
}); |
| 124 |
}); |
| 125 |
|
| 126 |
jQuery(document).on('click', '#mainwp-notes-subs-cancel', function () { |
| 127 |
jQuery('#mainwp-notes-subs-status').html(''); |
| 128 |
jQuery('#mainwp-notes-subs-status').removeClass('red green'); |
| 129 |
jQuery('#mainwp-notes-subs-modal').modal('hide'); |
| 130 |
return false; |
| 131 |
}); |
| 132 |
|
| 133 |
jQuery(document).on('click', '#mainwp-notes-subs-save', function () { |
| 134 |
mainwp_module_cost_tracker_notes_save(); |
| 135 |
return false; |
| 136 |
}); |
| 137 |
|
| 138 |
let mainwp_module_cost_tracker_notes_show = function () { |
| 139 |
jQuery('#mainwp-notes-subs-modal').modal('setting', 'closable', false).modal('show'); |
| 140 |
jQuery('#mainwp-notes-subs-html').show(); |
| 141 |
jQuery('#mainwp-notes-subs-editor').hide(); |
| 142 |
jQuery('#mainwp-notes-subs-save').hide(); |
| 143 |
jQuery('#mainwp-notes-subs-edit').show(); |
| 144 |
}; |
| 145 |
|
| 146 |
jQuery(document).on('click', '#mainwp-notes-subs-edit', function () { |
| 147 |
jQuery('#mainwp-notes-subs-html').hide(); |
| 148 |
jQuery('#mainwp-notes-subs-editor').show(); |
| 149 |
jQuery(this).hide(); |
| 150 |
jQuery('#mainwp-notes-subs-save').show(); |
| 151 |
jQuery('#mainwp-notes-subs-status').html(''); |
| 152 |
jQuery('#mainwp-notes-subs-status').removeClass('red green'); |
| 153 |
return false; |
| 154 |
}); |
| 155 |
|
| 156 |
let mainwp_module_cost_tracker_notes_save = function () { |
| 157 |
let normalid = jQuery('#mainwp-notes-subs-subid').val(); |
| 158 |
let newnote = jQuery('#mainwp-notes-subs-note').val(); |
| 159 |
newnote = newnote.replace(/(?:\r\n|\r|\n)/g, '<br>'); |
| 160 |
let data = mainwp_secure_data({ |
| 161 |
action: 'mainwp_module_cost_tracker_notes_save', |
| 162 |
subid: normalid, |
| 163 |
note: newnote, |
| 164 |
}); |
| 165 |
|
| 166 |
jQuery('#mainwp-notes-subs-status').html('<i class="notched circle loading icon"></i> ' + MainWP.I18n.t('Saving note. Please wait...')).show(); |
| 167 |
|
| 168 |
jQuery.post(ajaxurl, data, function (response) { |
| 169 |
if (response.error != undefined) { |
| 170 |
jQuery('#mainwp-notes-subs-status').html(response.error).addClass('red'); |
| 171 |
} else if (response.result == 'SUCCESS') { |
| 172 |
jQuery('#mainwp-notes-subs-status').html(MainWP.I18n.t('Note saved successfully.')).addClass('green'); |
| 173 |
if (jQuery('#mainwp-notes-subs-' + normalid + '-note').length > 0) { |
| 174 |
jQuery('#mainwp-notes-subs-' + normalid + '-note').html(response?.esc_note_content??''); |
| 175 |
} |
| 176 |
jQuery('#mainwp-notes-subs-html').html(response?.esc_note_content??''); |
| 177 |
} else { |
| 178 |
jQuery('#mainwp-notes-subs-status').html(MainWP.I18n.t('Undefined error occurred while saving your note!')).addClass('red'); |
| 179 |
} |
| 180 |
}, 'json'); |
| 181 |
|
| 182 |
setTimeout(function () { |
| 183 |
jQuery('#mainwp-notes-subs-status').fadeOut(300); |
| 184 |
}, 3000); |
| 185 |
|
| 186 |
jQuery('#mainwp-notes-subs-html').show(); |
| 187 |
jQuery('#mainwp-notes-subs-editor').hide(); |
| 188 |
jQuery('#mainwp-notes-subs-save').hide(); |
| 189 |
jQuery('#mainwp-notes-subs-edit').show(); |
| 190 |
|
| 191 |
}; |
| 192 |
|
| 193 |
let mod_costtracker_bulkMaxThreads = 4; |
| 194 |
let mod_costtracker_bulkTotalThreads = 0; |
| 195 |
let mod_costtracker_bulkCurrentThreads = 0; |
| 196 |
let mod_costtracker_bulkFinishedThreads = 0; |
| 197 |
|
| 198 |
// Manage Bulk Actions |
| 199 |
let mainwp_module_cost_tracker_table_bulk_action = function (act) { |
| 200 |
let selector = ''; |
| 201 |
if( 'delete-sub' === act) { |
| 202 |
selector += '#mainwp-module-cost-tracker-sites-table tbody tr'; |
| 203 |
jQuery(selector).addClass('queue'); |
| 204 |
mainwp_module_cost_tracker_delete_start_next(selector); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
let mainwp_module_cost_tracker_delete_start_next = function (selector) { |
| 209 |
if (mod_costtracker_bulkTotalThreads == 0) { |
| 210 |
mod_costtracker_bulkTotalThreads = jQuery('#mainwp-module-cost-tracker-sites-table tbody').find('input[type="checkbox"]:checked').length; |
| 211 |
} |
| 212 |
while ((objProcess = jQuery(selector + '.queue:first')) && (objProcess.length > 0) && (mod_costtracker_bulkCurrentThreads < mod_costtracker_bulkMaxThreads)) { // NOSONAR - variable modified outside of the function. |
| 213 |
objProcess.removeClass('queue'); |
| 214 |
if (objProcess.closest('tr').find('input[type="checkbox"]:checked').length == 0) { |
| 215 |
continue; |
| 216 |
} |
| 217 |
mainwp_module_cost_tracker_delete_start_specific(objProcess, selector, true); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
let mainwp_module_cost_tracker_delete_start_specific = function (pObj, selector, pBulk) { |
| 222 |
let row = pObj.closest('tr'); |
| 223 |
let subid = jQuery(row).attr('item-id'); |
| 224 |
let bulk = pBulk; |
| 225 |
|
| 226 |
if (bulk) { |
| 227 |
mod_costtracker_bulkCurrentThreads++; |
| 228 |
} |
| 229 |
|
| 230 |
let data = mainwp_secure_data({ |
| 231 |
action: 'mainwp_module_cost_tracker_delete', |
| 232 |
sub_id: subid, |
| 233 |
}); |
| 234 |
|
| 235 |
row.html('<td></td><td colspan="999"><i class="notched circle loading icon"></i> Please wait...</td>'); |
| 236 |
|
| 237 |
jQuery.post(ajaxurl, data, function (response) { |
| 238 |
pObj.removeClass('queue'); |
| 239 |
if (response) { |
| 240 |
if (response['error']) { |
| 241 |
row.html('<td></td><td colspan="999"><i class="times red icon"></i> ' + response['error'] + ' Page will reload in 3 seconds.</td>'); |
| 242 |
} else if (response['status'] == 'success') { |
| 243 |
row.html('<td></td><td colspan="999"><i class="green check icon"></i> The Cost has been deleted.</td>'); |
| 244 |
} else { |
| 245 |
row.html('<td></td><td colspan="999"><i class="times red icon"></i> The Cost could not be deleted.</td>'); |
| 246 |
} |
| 247 |
} else { |
| 248 |
row.html('<td></td><td colspan="999"><i class="times red icon"></i> The Cost could not be deleted.</td>'); |
| 249 |
} |
| 250 |
|
| 251 |
if (bulk) { |
| 252 |
mod_costtracker_bulkCurrentThreads--; |
| 253 |
mod_costtracker_bulkFinishedThreads++; |
| 254 |
mainwp_module_cost_tracker_delete_start_next(selector); |
| 255 |
if (mod_costtracker_bulkTotalThreads == mod_costtracker_bulkFinishedThreads) { |
| 256 |
setTimeout(function () { |
| 257 |
window.location.reload(true); |
| 258 |
}, 3000); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
}, 'json'); |
| 263 |
return false; |
| 264 |
} |