| @@ -1,40 +1,46 @@ | ||
| 1 | 1 | |
| 2 | 2 | /** |
| 3 | 3 | * MainWP_Page.page |
| 4 | 4 | */ |
| 5 | -jQuery(document).ready(function () { | |
| 6 | 5 | |
| 6 | +let countSent = 0; | |
| 7 | +let countReceived = 0; | |
| 8 | + | |
| 9 | +let updatePagesBulkActionsState = function () { | |
| 10 | + let checkedCount = jQuery('#mainwp-pages-table input[name="page[]"]:checked').length; | |
| 11 | + let dropdown = jQuery('#mainwp-manage-pages #mainwp-bulk-actions'); | |
| 12 | + let applyButton = jQuery('#mainwp-do-pages-bulk-actions'); | |
| 13 | + | |
| 14 | + if (checkedCount > 0) { | |
| 15 | + dropdown.removeClass('disabled'); | |
| 16 | + dropdown.parent('.ui.dropdown').removeClass('disabled'); | |
| 17 | + applyButton.removeClass('disabled'); | |
| 18 | + } else { | |
| 19 | + dropdown.addClass('disabled'); | |
| 20 | + dropdown.parent('.ui.dropdown').addClass('disabled'); | |
| 21 | + dropdown.dropdown('set selected', 'none'); | |
| 22 | + applyButton.addClass('disabled'); | |
| 23 | + } | |
| 24 | +}; | |
| 25 | + | |
| 26 | +jQuery(function () { | |
| 27 | + | |
| 7 | 28 | // to fix issue not loaded calendar js library |
| 8 | 29 | if (jQuery('.ui.calendar').length > 0) { |
| 9 | 30 | if (mainwpParams.use_wp_datepicker == 1) { |
| 10 | 31 | jQuery('#mainwp-manage-pages .ui.calendar input[type=text],#mainwp-manage-posts .ui.calendar input[type=text]').datepicker({ dateFormat: "yy-mm-dd" }); |
| 11 | 32 | } else { |
| 12 | - jQuery('#mainwp-manage-pages .ui.calendar, #mainwp-manage-posts .ui.calendar').calendar({ | |
| 13 | - type: 'date', | |
| 14 | - monthFirst: false, | |
| 15 | - today: true, | |
| 16 | - touchReadonly: false, | |
| 17 | - formatter: { | |
| 18 | - date: function (date) { | |
| 19 | - if (!date) return ''; | |
| 20 | - var day = date.getDate(); | |
| 21 | - var month = date.getMonth() + 1; | |
| 22 | - var year = date.getFullYear(); | |
| 23 | - | |
| 24 | - if (month < 10) { | |
| 25 | - month = '0' + month; | |
| 26 | - } | |
| 27 | - if (day < 10) { | |
| 28 | - day = '0' + day; | |
| 29 | - } | |
| 30 | - return year + '-' + month + '-' + day; | |
| 31 | - } | |
| 32 | - } | |
| 33 | - }); | |
| 33 | + mainwp_init_ui_calendar('#mainwp-manage-pages .ui.calendar, #mainwp-manage-posts .ui.calendar'); | |
| 34 | 34 | } |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | + updatePagesBulkActionsState(); | |
| 38 | + | |
| 39 | + jQuery(document).on('change', '#mainwp-pages-table .check-column INPUT:checkbox', function () { | |
| 40 | + updatePagesBulkActionsState(); | |
| 41 | + }); | |
| 42 | + | |
| 37 | 43 | jQuery(document).on('click', '#mainwp_show_pages', function () { |
| 38 | 44 | mainwp_fetch_pages(); |
| 39 | 45 | }); |
| 40 | 46 | jQuery(document).on('click', '.page_submitpublish', function () { |
| @@ -53,20 +59,20 @@ | ||
| 53 | 59 | mainwppage_postAction(jQuery(this), 'restore'); |
| 54 | 60 | return false; |
| 55 | 61 | }); |
| 56 | 62 | jQuery(document).on('click', '#mainwp-do-pages-bulk-actions', function () { |
| 57 | - var action = jQuery('#mainwp-bulk-actions').val(); | |
| 63 | + let action = jQuery('#mainwp-bulk-actions').val(); | |
| 58 | 64 | if (action != 'trash' && action != 'restore' && action != 'delete') { |
| 59 | - return false; | |
| 65 | + return; | |
| 60 | 66 | } |
| 61 | 67 | |
| 62 | - var tmp = jQuery("input[name='page[]']:checked"); | |
| 68 | + let tmp = jQuery("input[name='page[]']:checked"); | |
| 63 | 69 | countSent = tmp.length; |
| 64 | 70 | |
| 65 | 71 | if (countSent == 0) |
| 66 | - return false; | |
| 72 | + return; | |
| 67 | 73 | |
| 68 | - var _callback = function () { | |
| 74 | + let _callback = function () { | |
| 69 | 75 | jQuery('#mainwp-do-pages-bulk-actions').attr('disabled', 'true'); |
| 70 | 76 | tmp.each( |
| 71 | 77 | function (index, elem) { |
| 72 | 78 | mainwppage_postAction(elem, action); |
| @@ -74,24 +80,23 @@ | ||
| 74 | 80 | ); |
| 75 | 81 | }; |
| 76 | 82 | |
| 77 | 83 | if (action == 'delete') { |
| 78 | - var msg = __('You are about to delete %1 page(s). Are you sure you want to proceed?', countSent); | |
| 84 | + let msg = MainWP.I18n.t('You are about to delete %1 page(s). Are you sure you want to proceed?', countSent); | |
| 79 | 85 | mainwp_confirm(msg, _callback); |
| 80 | - return false; | |
| 86 | + return; | |
| 81 | 87 | } |
| 82 | 88 | _callback(); |
| 83 | - return false; | |
| 84 | 89 | }); |
| 85 | 90 | }); |
| 86 | 91 | |
| 87 | 92 | |
| 88 | -mainwppage_postAction = function (elem, what) { | |
| 89 | - var rowElement = jQuery(elem).closest('tr'); | |
| 90 | - var pageId = rowElement.find('.pageId').val(); | |
| 91 | - var websiteId = rowElement.find('.websiteId').val(); | |
| 93 | +let mainwppage_postAction = function (elem, what) { | |
| 94 | + let rowElement = jQuery(elem).closest('tr'); | |
| 95 | + let pageId = rowElement.find('.pageId').val(); | |
| 96 | + let websiteId = rowElement.find('.websiteId').val(); | |
| 92 | 97 | |
| 93 | - if (rowElement.find('.allowedBulkActions').val().indexOf('|' + what + '|') == -1) { | |
| 98 | + if (!rowElement.find('.allowedBulkActions').val().includes('|' + what + '|')) { | |
| 94 | 99 | jQuery(elem).prop("checked", false); |
| 95 | 100 | countReceived++; |
| 96 | 101 | |
| 97 | 102 | if (countReceived == countSent) { |
| @@ -104,9 +109,9 @@ | ||
| 104 | 109 | |
| 105 | 110 | return; |
| 106 | 111 | } |
| 107 | 112 | |
| 108 | - var data = mainwp_secure_data({ | |
| 113 | + let data = mainwp_secure_data({ | |
| 109 | 114 | action: 'mainwp_page_' + what, |
| 110 | 115 | postId: pageId, |
| 111 | 116 | websiteId: websiteId |
| 112 | 117 | }); |
| @@ -132,55 +137,22 @@ | ||
| 132 | 137 | |
| 133 | 138 | return false; |
| 134 | 139 | }; |
| 135 | 140 | |
| 136 | -mainwp_fetch_pages = function () { | |
| 137 | - var errors = []; | |
| 138 | - var selected_sites = []; | |
| 139 | - var selected_groups = []; | |
| 140 | - var selected_clients = []; | |
| 141 | +let mainwp_fetch_pages = function () { | |
| 141 | 142 | |
| 142 | - if (jQuery('#select_by').val() == 'site') { | |
| 143 | - jQuery("input[name='selected_sites[]']:checked").each(function () { | |
| 144 | - selected_sites.push(jQuery(this).val()); | |
| 145 | - }); | |
| 146 | - if (selected_sites.length == 0) { | |
| 147 | - errors.push('<div class="mainwp-notice mainwp-notice-red">' + __('Please select websites or groups or clients.') + '</div>'); | |
| 148 | - } | |
| 149 | - } else if (jQuery('#select_by').val() == 'client') { | |
| 150 | - jQuery("input[name='selected_clients[]']:checked").each(function () { | |
| 151 | - selected_clients.push(jQuery(this).val()); | |
| 152 | - }); | |
| 153 | - if (selected_clients.length == 0) { | |
| 154 | - errors.push('<div class="mainwp-notice mainwp-notice-red">' + __('Please select websites or groups or clients.') + '</div>'); | |
| 155 | - } | |
| 156 | - } else { | |
| 157 | - jQuery("input[name='selected_groups[]']:checked").each(function () { | |
| 158 | - selected_groups.push(jQuery(this).val()); | |
| 159 | - }); | |
| 160 | - if (selected_groups.length == 0) { | |
| 161 | - errors.push('<div class="mainwp-notice mainwp-notice-red">' + __('Please select websites or groups or clients.') + '</div>'); | |
| 162 | - } | |
| 163 | - } | |
| 143 | + let params = mainwp_fetch_pages_prepare(); | |
| 164 | 144 | |
| 165 | - var _status = ''; | |
| 166 | - var statuses = jQuery("#mainwp_page_search_type").dropdown("get value"); | |
| 167 | - if (statuses == null) | |
| 168 | - errors.push('Please select a page status.'); | |
| 169 | - else { | |
| 170 | - _status = statuses.join(','); | |
| 145 | + if (typeof params !== 'object') { | |
| 146 | + return; | |
| 171 | 147 | } |
| 172 | 148 | |
| 173 | - if (errors.length > 0) { | |
| 174 | - jQuery('#mainwp_pages_error').html(errors.join('<br />')); | |
| 175 | - jQuery('#mainwp_pages_error').show(); | |
| 176 | - return; | |
| 177 | - } else { | |
| 178 | - jQuery('#mainwp_pages_error').html(""); | |
| 179 | - jQuery('#mainwp_pages_error').hide(); | |
| 180 | - } | |
| 149 | + let _status = params['_status']; | |
| 150 | + let selected_sites = params['selected_sites']; | |
| 151 | + let selected_clients = params['selected_clients']; | |
| 152 | + let selected_groups = params['selected_groups']; | |
| 181 | 153 | |
| 182 | - var data = mainwp_secure_data({ | |
| 154 | + let data = mainwp_secure_data({ | |
| 183 | 155 | action: 'mainwp_pages_search', |
| 184 | 156 | keyword: jQuery('#mainwp_page_search_by_keyword').val(), |
| 185 | 157 | dtsstart: jQuery('#mainwp_page_search_by_dtsstart').val(), |
| 186 | 158 | dtsstop: jQuery('#mainwp_page_search_by_dtsstop').val(), |
| @@ -201,12 +173,9 @@ | ||
| 201 | 173 | // re-initialize datatable |
| 202 | 174 | jQuery("#mainwp-pages-table").DataTable().destroy(); |
| 203 | 175 | jQuery('#mainwp-pages-table').DataTable({ |
| 204 | 176 | "responsive": true, |
| 205 | - "colReorder": { | |
| 206 | - fixedColumnsLeft: 1, | |
| 207 | - fixedColumnsRight: 1 | |
| 208 | - }, | |
| 177 | + "colReorder": { columns: ":not(.check-column):not(:last-child)" }, | |
| 209 | 178 | "stateSave": true, |
| 210 | 179 | "pagingType": "full_numbers", |
| 211 | 180 | "scrollX": true, |
| 212 | 181 | "lengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]], |
| @@ -214,22 +183,169 @@ | ||
| 214 | 183 | "columnDefs": [{ |
| 215 | 184 | "targets": 'no-sort', |
| 216 | 185 | "orderable": false |
| 217 | 186 | }], |
| 218 | - "preDrawCallback": function () { | |
| 187 | + "drawCallback": function () { | |
| 188 | + setTimeout(() => { | |
| 189 | + jQuery('#mainwp-pages-table .ui.dropdown').dropdown(); | |
| 190 | + jQuery('#mainwp-pages-table .ui.checkbox').checkbox(); | |
| 191 | + mainwp_table_check_columns_init(); // ajax: to fix checkbox all. | |
| 192 | + mainwp_datatable_fix_menu_overflow('#mainwp-pages-table'); | |
| 193 | + updatePagesBulkActionsState(); | |
| 194 | + }, 1000); | |
| 195 | + }, | |
| 196 | + select: { | |
| 197 | + items: 'row', | |
| 198 | + style: 'multi+shift', | |
| 199 | + selector: 'tr>td:not(.not-selectable)' | |
| 200 | + } | |
| 201 | + }).on('select', function (e, dt, type, indexes) { | |
| 202 | + if ('row' == type) { | |
| 203 | + dt.rows(indexes) | |
| 204 | + .nodes() | |
| 205 | + .to$().find('td.check-column .ui.checkbox').checkbox('set checked'); | |
| 206 | + updatePagesBulkActionsState(); | |
| 207 | + } | |
| 208 | + }).on('deselect', function (e, dt, type, indexes) { | |
| 209 | + if ('row' == type) { | |
| 210 | + dt.rows(indexes) | |
| 211 | + .nodes() | |
| 212 | + .to$().find('td.check-column .ui.checkbox').checkbox('set unchecked'); | |
| 213 | + updatePagesBulkActionsState(); | |
| 214 | + } | |
| 215 | + }).on('columns-reordered', function () { | |
| 216 | + setTimeout(() => { | |
| 217 | + jQuery('#mainwp-pages-table .ui.dropdown').dropdown(); | |
| 218 | + jQuery('#mainwp-pages-table .ui.checkbox').checkbox(); | |
| 219 | + mainwp_datatable_fix_menu_overflow('#mainwp-pages-table'); | |
| 219 | 220 | mainwp_table_check_columns_init(); // ajax: to fix checkbox all. |
| 220 | - mainwp_datatable_fix_menu_overflow(); | |
| 221 | - } | |
| 221 | + updatePagesBulkActionsState(); | |
| 222 | + }, 1000); | |
| 222 | 223 | }); |
| 223 | 224 | }); |
| 224 | 225 | }; |
| 225 | 226 | |
| 227 | +let mainwp_fetch_pages_prepare = function () { // NOSONAR - complexity 19/15. | |
| 228 | + | |
| 229 | + let errors = []; | |
| 230 | + let selected_sites = []; | |
| 231 | + let selected_groups = []; | |
| 232 | + let selected_clients = []; | |
| 233 | + | |
| 234 | + if (jQuery('input[name="select_by"]').val() == 'site') { | |
| 235 | + jQuery("input[name='selected_sites[]']:checked").each(function () { | |
| 236 | + selected_sites.push(jQuery(this).val()); | |
| 237 | + }); | |
| 238 | + if (selected_sites.length == 0) { | |
| 239 | + errors.push('<div class="ui yellow message">' + MainWP.I18n.t('Please select websites or groups or clients.') + '</div>'); | |
| 240 | + } | |
| 241 | + } else if (jQuery('input[name="select_by"]').val() == 'client') { | |
| 242 | + jQuery("input[name='selected_clients[]']:checked").each(function () { | |
| 243 | + selected_clients.push(jQuery(this).val()); | |
| 244 | + }); | |
| 245 | + if (selected_clients.length == 0) { | |
| 246 | + errors.push('<div class="ui yellow message">' + MainWP.I18n.t('Please select websites or groups or clients.') + '</div>'); | |
| 247 | + } | |
| 248 | + } else { | |
| 249 | + jQuery("input[name='selected_groups[]']:checked").each(function () { | |
| 250 | + selected_groups.push(jQuery(this).val()); | |
| 251 | + }); | |
| 252 | + if (selected_groups.length == 0) { | |
| 253 | + errors.push('<div class="ui yellow message">' + MainWP.I18n.t('Please select websites or groups or clients.') + '</div>'); | |
| 254 | + } | |
| 255 | + } | |
| 256 | + | |
| 257 | + let _status = ''; | |
| 258 | + let statuses = jQuery("#mainwp_page_search_type").dropdown("get value"); | |
| 259 | + if (statuses == null) | |
| 260 | + errors.push('Please select a page status.'); | |
| 261 | + else { | |
| 262 | + _status = statuses.join(','); | |
| 263 | + } | |
| 264 | + | |
| 265 | + if (errors.length > 0) { | |
| 266 | + jQuery('#mainwp_pages_error').html(errors.join('<br />')); | |
| 267 | + jQuery('#mainwp_pages_error').show(); | |
| 268 | + return; | |
| 269 | + } else { | |
| 270 | + jQuery('#mainwp_pages_error').html(""); | |
| 271 | + jQuery('#mainwp_pages_error').hide(); | |
| 272 | + } | |
| 273 | + | |
| 274 | + return { | |
| 275 | + 'selected_sites': selected_sites, | |
| 276 | + 'selected_groups': selected_groups, | |
| 277 | + 'selected_clients': selected_clients, | |
| 278 | + '_status': _status, | |
| 279 | + } | |
| 280 | +} | |
| 226 | 281 | /** |
| 227 | 282 | * MainWP_Post.page |
| 228 | 283 | */ |
| 229 | -var countSent = 0; | |
| 230 | -var countReceived = 0; | |
| 231 | -jQuery(document).ready(function () { | |
| 284 | + | |
| 285 | +let updatePostsBulkActionsState = function () { | |
| 286 | + let checkedCount = jQuery('#mainwp-posts-table input[name="post[]"]:checked').length; | |
| 287 | + let dropdown = jQuery('#mainwp-manage-posts #mainwp-bulk-actions'); | |
| 288 | + let applyButton = jQuery('#mainwp-do-posts-bulk-actions'); | |
| 289 | + | |
| 290 | + if (checkedCount > 0) { | |
| 291 | + dropdown.removeClass('disabled'); | |
| 292 | + dropdown.parent('.ui.dropdown').removeClass('disabled'); | |
| 293 | + applyButton.removeClass('disabled'); | |
| 294 | + } else { | |
| 295 | + dropdown.addClass('disabled'); | |
| 296 | + dropdown.parent('.ui.dropdown').addClass('disabled'); | |
| 297 | + dropdown.dropdown('set selected', 'none'); | |
| 298 | + applyButton.addClass('disabled'); | |
| 299 | + } | |
| 300 | +}; | |
| 301 | + | |
| 302 | +let mainwp_copyTextToClipboard = function (value) { | |
| 303 | + | |
| 304 | + if (!value) { | |
| 305 | + return Promise.reject(new Error('Empty clipboard value')); | |
| 306 | + } | |
| 307 | + | |
| 308 | + if (navigator.clipboard && globalThis.isSecureContext) { | |
| 309 | + return navigator.clipboard.writeText(value); | |
| 310 | + } | |
| 311 | + | |
| 312 | + return new Promise(function (resolve, reject) { | |
| 313 | + | |
| 314 | + let tempField = jQuery('<textarea readonly class="mainwp-copy-temp-field" aria-hidden="true"></textarea>'); | |
| 315 | + tempField.css({ | |
| 316 | + position: 'absolute', | |
| 317 | + left: '-9999px', | |
| 318 | + top: '0' | |
| 319 | + }); | |
| 320 | + jQuery('body').append(tempField); | |
| 321 | + tempField.val(value).trigger('focus').trigger('select'); | |
| 322 | + | |
| 323 | + try { | |
| 324 | + if (document.execCommand('copy')) { | |
| 325 | + tempField.remove(); | |
| 326 | + resolve(); | |
| 327 | + return; | |
| 328 | + } | |
| 329 | + } catch (err) { | |
| 330 | + tempField.remove(); | |
| 331 | + reject(err); | |
| 332 | + return; | |
| 333 | + } | |
| 334 | + | |
| 335 | + tempField.remove(); | |
| 336 | + reject(new Error('Copy command failed')); | |
| 337 | + }); | |
| 338 | +}; | |
| 339 | + | |
| 340 | +jQuery(function () { | |
| 341 | + | |
| 342 | + updatePostsBulkActionsState(); | |
| 343 | + | |
| 344 | + jQuery(document).on('change', '#mainwp-posts-table .check-column INPUT:checkbox', function () { | |
| 345 | + updatePostsBulkActionsState(); | |
| 346 | + }); | |
| 347 | + | |
| 232 | 348 | jQuery(document).on('click', '#mainwp_show_posts', function () { |
| 233 | 349 | mainwp_fetch_posts(); |
| 234 | 350 | }); |
| 235 | 351 | jQuery(document).on('click', '.post_submitdelete', function () { |
| @@ -266,21 +382,38 @@ | ||
| 266 | 382 | mainwppost_postAction(jQuery(this), 'get_edit', 'page'); |
| 267 | 383 | return false; |
| 268 | 384 | }); |
| 269 | 385 | |
| 386 | + jQuery(document).on('click', '.mainwp-copy-row-value', function () { | |
| 387 | + let button = jQuery(this); | |
| 388 | + let rowElement = button.closest('tr'); | |
| 389 | + let target = button.data('copy-target'); | |
| 390 | + let value = ''; | |
| 391 | + | |
| 392 | + if (target) { | |
| 393 | + value = rowElement.find(target).val() || ''; | |
| 394 | + } | |
| 395 | + | |
| 396 | + mainwp_copyTextToClipboard(value).catch(function () { | |
| 397 | + alert(MainWP.I18n.t('Unable to copy to clipboard.')); | |
| 398 | + }); | |
| 399 | + | |
| 400 | + return false; | |
| 401 | + }); | |
| 402 | + | |
| 270 | 403 | jQuery(document).on('click', '#mainwp-do-posts-bulk-actions', function () { |
| 271 | - var action = jQuery('#mainwp-bulk-actions').val(); | |
| 404 | + let action = jQuery('#mainwp-bulk-actions').val(); | |
| 272 | 405 | if (action != 'publish' && action != 'unpublish' && action != 'trash' && action != 'restore' && action != 'delete') { |
| 273 | - return false; | |
| 406 | + return; | |
| 274 | 407 | } |
| 275 | 408 | |
| 276 | - var tmp = jQuery("input[name='post[]']:checked"); | |
| 409 | + let tmp = jQuery("input[name='post[]']:checked"); | |
| 277 | 410 | countSent = tmp.length; |
| 278 | 411 | |
| 279 | 412 | if (countSent == 0) |
| 280 | - return false; | |
| 413 | + return; | |
| 281 | 414 | |
| 282 | - var _callback = function () { | |
| 415 | + let _callback = function () { | |
| 283 | 416 | jQuery('#mainwp-do-posts-bulk-actions').attr('disabled', 'true'); |
| 284 | 417 | tmp.each( |
| 285 | 418 | function (index, elem) { |
| 286 | 419 | mainwppost_postAction(elem, action); |
| @@ -287,22 +420,21 @@ | ||
| 287 | 420 | } |
| 288 | 421 | ); |
| 289 | 422 | } |
| 290 | 423 | if (action == 'delete') { |
| 291 | - var msg = __('You are about to delete %1 post(s). Are you sure you want to proceed?', countSent); | |
| 424 | + let msg = MainWP.I18n.t('You are about to delete %1 post(s). Are you sure you want to proceed?', countSent); | |
| 292 | 425 | mainwp_confirm(msg, _callback); |
| 293 | - return false; | |
| 426 | + return; | |
| 294 | 427 | } |
| 295 | 428 | _callback(); |
| 296 | - return false; | |
| 297 | 429 | }); |
| 298 | 430 | }); |
| 299 | 431 | |
| 300 | -mainwppost_postAction = function (elem, what, postType) { | |
| 301 | - var rowElement = jQuery(elem).closest('tr'); | |
| 302 | - var postId = rowElement.find('.postId').val(); | |
| 303 | - var websiteId = rowElement.find('.websiteId').val(); | |
| 304 | - if (rowElement.find('.allowedBulkActions').val().indexOf('|' + what + '|') == -1) { | |
| 432 | +let mainwppost_postAction = function (elem, what, postType) { | |
| 433 | + let rowElement = jQuery(elem).closest('tr'); | |
| 434 | + let postId = rowElement.find('.postId').val(); | |
| 435 | + let websiteId = rowElement.find('.websiteId').val(); | |
| 436 | + if (!rowElement.find('.allowedBulkActions').val().includes('|' + what + '|')) { | |
| 305 | 437 | jQuery(elem).prop("checked", false); |
| 306 | 438 | countReceived++; |
| 307 | 439 | |
| 308 | 440 | if (countReceived == countSent) { |
| @@ -319,20 +451,20 @@ | ||
| 319 | 451 | if (what == 'get_edit' && postType === 'page') { |
| 320 | 452 | postId = rowElement.find('.pageId').val(); |
| 321 | 453 | } |
| 322 | 454 | |
| 323 | - var data = { | |
| 455 | + let data = { | |
| 324 | 456 | action: 'mainwp_post_' + what, |
| 325 | 457 | postId: postId, |
| 326 | 458 | websiteId: websiteId |
| 327 | 459 | }; |
| 328 | - if (typeof postType !== "undefined") { | |
| 460 | + if (postType !== undefined) { | |
| 329 | 461 | data['postType'] = postType; |
| 330 | 462 | } |
| 331 | 463 | data = mainwp_secure_data(data); |
| 332 | 464 | |
| 333 | 465 | rowElement.html('<td colspan="99"><i class="notched circle loading icon"></i> Please wait...</td>'); |
| 334 | - jQuery.post(ajaxurl, data, function (response) { | |
| 466 | + jQuery.post(ajaxurl, data, function (response) { // NOSONAR - complexity 11/10. | |
| 335 | 467 | if (response.error) { |
| 336 | 468 | rowElement.html('<td colspan="99"><i class="times red icon"></i>' + response.error + '</td>'); |
| 337 | 469 | } else if (response.result) { |
| 338 | 470 | rowElement.html('<td colspan="99"><i class="green check icon"></i> ' + response.result + '</td>'); |
| @@ -341,15 +473,19 @@ | ||
| 341 | 473 | } |
| 342 | 474 | } else { |
| 343 | 475 | rowElement.hide(); |
| 344 | 476 | if (what == 'get_edit' && response.id) { |
| 477 | + let reloadUrl = ''; | |
| 345 | 478 | if (response.redirect_to) { |
| 346 | - location.href = response.redirect_to; | |
| 479 | + reloadUrl = response.redirect_to; | |
| 347 | 480 | } else if (postType == 'post') { |
| 348 | - location.href = 'admin.php?page=PostBulkEdit&post_id=' + response.id; | |
| 481 | + reloadUrl = 'admin.php?page=PostBulkEdit&post_id=' + response.id; | |
| 349 | 482 | } else if (postType == 'page') { |
| 350 | - location.href = 'admin.php?page=PageBulkEdit&post_id=' + response.id; | |
| 483 | + reloadUrl = 'admin.php?page=PageBulkEdit&post_id=' + response.id; | |
| 351 | 484 | } |
| 485 | + if (reloadUrl !== '') { | |
| 486 | + mainwp_forceReload(reloadUrl); | |
| 487 | + } | |
| 352 | 488 | } |
| 353 | 489 | } |
| 354 | 490 | countReceived++; |
| 355 | 491 | if (countReceived == countSent) { |
| @@ -361,114 +497,53 @@ | ||
| 361 | 497 | |
| 362 | 498 | return false; |
| 363 | 499 | }; |
| 364 | 500 | |
| 365 | -mainwp_show_post = function (siteId, postId, userId) { | |
| 366 | - var siteElement = jQuery('input[name="selected_sites[]"][siteid="' + siteId + '"]'); | |
| 501 | +let mainwp_show_post = function (siteId, postId, userId) { | |
| 502 | + let siteElement = jQuery('input[name="selected_sites[]"][siteid="' + siteId + '"]'); | |
| 367 | 503 | siteElement.prop('checked', true); |
| 368 | 504 | siteElement.trigger("change"); |
| 369 | 505 | mainwp_fetch_posts(postId, userId); |
| 370 | 506 | }; |
| 371 | 507 | /* eslint-disable complexity */ |
| 372 | -mainwp_fetch_posts = function (postId, userId, start_sites) { | |
| 373 | - var errors = []; | |
| 374 | - var selected_sites = []; | |
| 375 | - var selected_groups = []; | |
| 376 | - var selected_clients = []; | |
| 508 | +let mainwp_fetch_posts = function (postId, userId, start_sites) { | |
| 377 | 509 | |
| 378 | - var i = 0; | |
| 379 | - var num_sites = jQuery('#search-bulk-sites').attr('number-sites'); | |
| 380 | - num_sites = parseInt(num_sites); | |
| 510 | + let params = mainwp_fetch_posts_prepare(postId, userId, start_sites); | |
| 381 | 511 | |
| 382 | - var bulk_search = num_sites > 0 ? true : false; | |
| 512 | + if (typeof params !== 'object') { | |
| 513 | + return; | |
| 514 | + } | |
| 383 | 515 | |
| 384 | - if (jQuery('#select_by').val() == 'site') { | |
| 385 | - if (start_sites == undefined) { | |
| 386 | - start_sites = 0; | |
| 387 | - } | |
| 388 | - jQuery("input[name='selected_sites[]']:checked").each(function () { | |
| 389 | - if (bulk_search) { | |
| 390 | - if (i >= start_sites && i < start_sites + num_sites) { | |
| 391 | - selected_sites.push(jQuery(this).val()); | |
| 392 | - } | |
| 393 | - i++; | |
| 394 | - } else { | |
| 395 | - selected_sites.push(jQuery(this).val()); | |
| 396 | - } | |
| 397 | - }); | |
| 398 | - if (selected_sites.length == 0) { | |
| 399 | - if (!bulk_search || (bulk_search && start_sites == 0)) { | |
| 400 | - errors.push('<div class="ui yellow message">' + __('Please select at least one website or group or client.') + '</div>'); | |
| 401 | - } | |
| 402 | - } | |
| 516 | + start_sites = params['start_sites']; | |
| 403 | 517 | |
| 404 | - } else if (jQuery('#select_by').val() == 'client') { | |
| 405 | - jQuery("input[name='selected_clients[]']:checked").each(function () { | |
| 406 | - selected_clients.push(jQuery(this).val()); | |
| 407 | - }); | |
| 408 | - if (selected_clients.length == 0) { | |
| 409 | - errors.push('<div class="ui yellow message">' + __('Please select at least one client or website, group.') + '</div>'); | |
| 410 | - } | |
| 518 | + let errors = params['errors']; | |
| 519 | + let selected_sites = params['selected_sites']; | |
| 520 | + let selected_clients = params['selected_clients']; | |
| 521 | + let selected_groups = params['selected_groups']; | |
| 522 | + let bulk_search = params['bulk_search']; | |
| 411 | 523 | |
| 412 | - } else if (jQuery('#select_by').val() == 'group') { | |
| 413 | - jQuery("input[name='selected_groups[]']:checked").each(function () { | |
| 414 | - selected_groups.push(jQuery(this).val()); | |
| 415 | - }); | |
| 416 | - if (selected_groups.length == 0) { | |
| 417 | - errors.push('<div class="ui yellow message">' + __('Please select at least one website or group or client.') + '</div>'); | |
| 418 | - } else if (bulk_search) { | |
| 419 | 524 | |
| 420 | - if (start_sites == undefined) { | |
| 421 | - console.log(num_sites); | |
| 422 | - start_sites = 0; | |
| 423 | - // get sites of groups. | |
| 424 | - var data = mainwp_secure_data({ | |
| 425 | - action: 'mainwp_get_sites_of_groups', | |
| 426 | - 'groups[]': selected_groups | |
| 427 | - }); | |
| 428 | - jQuery('#mainwp-loading-posts-row').show(); | |
| 429 | - jQuery.post(ajaxurl, data, function (response) { | |
| 430 | - var site_ids = response; | |
| 431 | - console.log(site_ids); | |
| 432 | - if (site_ids) { | |
| 433 | - jQuery("input[name='selected_sites[]'][bulk-search=true]").attr('bulk-search', false); | |
| 434 | - jQuery.each(site_ids, function (index, value) { | |
| 435 | - jQuery("input[name='selected_sites[]'][value=" + value + "]").attr('bulk-search', true); | |
| 436 | - }); | |
| 437 | - } | |
| 438 | - mainwp_fetch_posts(postId, userId, start_sites); | |
| 439 | - }, 'json'); | |
| 440 | - return; | |
| 441 | - } | |
| 442 | - | |
| 443 | - console.log(jQuery("input[name='selected_sites[]'][bulk-search=true]").length); | |
| 444 | - | |
| 445 | - jQuery("input[name='selected_sites[]'][bulk-search=true]").each(function () { | |
| 446 | - if (i >= start_sites && i < start_sites + num_sites) { | |
| 447 | - selected_sites.push(jQuery(this).val()); | |
| 448 | - } | |
| 449 | - i++; | |
| 450 | - }); | |
| 451 | - } | |
| 452 | - } | |
| 453 | - var _status = ''; | |
| 454 | - var statuses = jQuery("#mainwp_post_search_type").dropdown("get value"); | |
| 525 | + let _status = ''; | |
| 526 | + let statuses = jQuery("#mainwp_post_search_type").dropdown("get value"); | |
| 455 | 527 | if (statuses == null) |
| 456 | - errors.push('<div class="ui yellow message">' + __('Please select at least one post status.') + '</div>'); | |
| 528 | + errors.push('<div class="ui yellow message">' + MainWP.I18n.t('Please select at least one post status.') + '</div>'); | |
| 457 | 529 | else { |
| 458 | 530 | _status = statuses.join(','); |
| 459 | 531 | } |
| 460 | 532 | |
| 461 | 533 | if (errors.length > 0) { |
| 462 | - jQuery('#mainwp-message-zone').html(errors); | |
| 463 | - jQuery('#mainwp-message-zone').show(); | |
| 534 | + mainwp_set_message_zone('#mainwp-message-zone', errors.join('<br />'), 'red'); | |
| 464 | 535 | return; |
| 465 | 536 | } else { |
| 466 | - jQuery('#mainwp-message-zone').html(""); | |
| 467 | - jQuery('#mainwp-message-zone').hide(); | |
| 537 | + mainwp_set_message_zone('#mainwp-message-zone'); | |
| 468 | 538 | } |
| 469 | 539 | |
| 470 | - var data = mainwp_secure_data({ | |
| 540 | + if (bulk_search && selected_sites.length == 0) { | |
| 541 | + mainwp_fetch_posts_done(); | |
| 542 | + return; | |
| 543 | + } | |
| 544 | + | |
| 545 | + let data = mainwp_secure_data({ | |
| 471 | 546 | action: 'mainwp_posts_search', |
| 472 | 547 | keyword: jQuery('#mainwp_post_search_by_keyword').val(), |
| 473 | 548 | dtsstart: jQuery('#mainwp_post_search_by_dtsstart').val(), |
| 474 | 549 | dtsstop: jQuery('#mainwp_post_search_by_dtsstop').val(), |
| @@ -482,19 +557,15 @@ | ||
| 482 | 557 | maximum: jQuery("#mainwp_maximumPosts").val(), |
| 483 | 558 | search_on: jQuery("#mainwp_post_search_on").val() |
| 484 | 559 | }); |
| 485 | 560 | |
| 486 | - if (bulk_search) { | |
| 487 | - if (start_sites > 0) { | |
| 488 | - data.table_content = 1; | |
| 489 | - } | |
| 490 | - if (selected_sites.length == 0) { | |
| 491 | - mainwp_fetch_posts_done(); | |
| 492 | - return; | |
| 493 | - } | |
| 561 | + if (bulk_search && start_sites > 0) { | |
| 562 | + data.table_content = 1; | |
| 494 | 563 | } |
| 495 | 564 | |
| 565 | + | |
| 496 | 566 | jQuery('#mainwp-loading-posts-row').show(); |
| 567 | + | |
| 497 | 568 | jQuery.post(ajaxurl, data, function (response) { |
| 498 | 569 | response = response.trim(); |
| 499 | 570 | if (bulk_search && start_sites > 0) { |
| 500 | 571 | jQuery('#mainwp-posts-list').append(response); |
| @@ -507,17 +578,106 @@ | ||
| 507 | 578 | mainwp_fetch_posts(postId, userId, start_sites); |
| 508 | 579 | } else { |
| 509 | 580 | mainwp_fetch_posts_done(); |
| 510 | 581 | } |
| 582 | + }); | |
| 511 | 583 | |
| 512 | - }); | |
| 513 | 584 | }; |
| 585 | + | |
| 586 | +let mainwp_fetch_posts_prepare = function (postId, userId, start_sites) { // NOSONAR - complexity. | |
| 587 | + | |
| 588 | + let errors = []; | |
| 589 | + let selected_sites = []; | |
| 590 | + let selected_groups = []; | |
| 591 | + let selected_clients = []; | |
| 592 | + | |
| 593 | + let i = 0; | |
| 594 | + let num_sites = jQuery('#search-bulk-sites').attr('number-sites'); | |
| 595 | + num_sites = Number.parseInt(num_sites); | |
| 596 | + | |
| 597 | + let select_sites_error = '<div class="ui yellow message">' + MainWP.I18n.t('Please select at least one website or group or client.') + '</div>'; | |
| 598 | + | |
| 599 | + let bulk_search = num_sites > 0; | |
| 600 | + | |
| 601 | + if (jQuery('input[name="select_by"]').val() == 'site' && start_sites == undefined) { | |
| 602 | + start_sites = 0; | |
| 603 | + } | |
| 604 | + | |
| 605 | + if (jQuery('input[name="select_by"]').val() == 'site') { | |
| 606 | + jQuery("input[name='selected_sites[]']:checked").each(function () { | |
| 607 | + if (bulk_search) { | |
| 608 | + if (i >= start_sites && i < start_sites + num_sites) { | |
| 609 | + selected_sites.push(jQuery(this).val()); | |
| 610 | + } | |
| 611 | + i++; | |
| 612 | + } else { | |
| 613 | + selected_sites.push(jQuery(this).val()); | |
| 614 | + } | |
| 615 | + }); | |
| 616 | + if (selected_sites.length == 0 && (!bulk_search || (bulk_search && start_sites == 0))) { | |
| 617 | + errors.push(select_sites_error); | |
| 618 | + } | |
| 619 | + } else if (jQuery('input[name="select_by"]').val() == 'client') { | |
| 620 | + jQuery("input[name='selected_clients[]']:checked").each(function () { | |
| 621 | + selected_clients.push(jQuery(this).val()); | |
| 622 | + }); | |
| 623 | + if (selected_clients.length == 0) { | |
| 624 | + errors.push(select_sites_error); | |
| 625 | + } | |
| 626 | + } else if (jQuery('input[name="select_by"]').val() == 'group') { | |
| 627 | + jQuery("input[name='selected_groups[]']:checked").each(function () { | |
| 628 | + selected_groups.push(jQuery(this).val()); | |
| 629 | + }); | |
| 630 | + | |
| 631 | + if (selected_groups.length == 0) { | |
| 632 | + errors.push(select_sites_error); | |
| 633 | + } else if (selected_groups.length > 0 && bulk_search && start_sites == undefined) { | |
| 634 | + start_sites = 0; | |
| 635 | + // get sites of groups. | |
| 636 | + let data = mainwp_secure_data({ | |
| 637 | + action: 'mainwp_get_sites_of_groups', | |
| 638 | + 'groups[]': selected_groups | |
| 639 | + }); | |
| 640 | + jQuery('#mainwp-loading-posts-row').show(); | |
| 641 | + jQuery.post(ajaxurl, data, function (response) { | |
| 642 | + let site_ids = response; | |
| 643 | + if (site_ids) { | |
| 644 | + jQuery("input[name='selected_sites[]'][bulk-search=true]").attr('bulk-search', false); | |
| 645 | + jQuery.each(site_ids, function (index, value) { | |
| 646 | + jQuery("input[name='selected_sites[]'][value=" + value + "]").attr('bulk-search', true); | |
| 647 | + }); | |
| 648 | + } | |
| 649 | + mainwp_fetch_posts(postId, userId, start_sites); | |
| 650 | + }, 'json'); | |
| 651 | + return; | |
| 652 | + } | |
| 653 | + | |
| 654 | + if (selected_groups.length > 0 && bulk_search) { | |
| 655 | + jQuery("input[name='selected_sites[]'][bulk-search=true]").each(function () { | |
| 656 | + if (i >= start_sites && i < start_sites + num_sites) { | |
| 657 | + selected_sites.push(jQuery(this).val()); | |
| 658 | + } | |
| 659 | + i++; | |
| 660 | + }); | |
| 661 | + } | |
| 662 | + } | |
| 663 | + | |
| 664 | + return { | |
| 665 | + 'errors': errors, | |
| 666 | + 'selected_sites': selected_sites, | |
| 667 | + 'selected_clients': selected_clients, | |
| 668 | + 'selected_groups': selected_groups, | |
| 669 | + 'start_sites': start_sites, | |
| 670 | + 'bulk_search': bulk_search, | |
| 671 | + }; | |
| 672 | +} | |
| 673 | + | |
| 514 | 674 | /* eslint-enable complexity */ |
| 515 | 675 | |
| 516 | -mainwp_fetch_posts_done = function () { | |
| 676 | +let mainwp_fetch_posts_done = function () { | |
| 517 | 677 | jQuery('#mainwp-loading-posts-row').hide(); |
| 518 | 678 | jQuery('#mainwp_posts_main').show(); |
| 519 | - var responsive = true; | |
| 679 | + let responsive = true; | |
| 520 | 680 | if (jQuery(window).width() > 1140) { |
| 521 | 681 | responsive = false; |
| 522 | 682 | } |
| 523 | 683 | // re-initialize datatable. |
| @@ -523,12 +683,9 @@ | ||
| 523 | 683 | // re-initialize datatable. |
| 524 | 684 | jQuery("#mainwp-posts-table").DataTable().destroy(); |
| 525 | 685 | jQuery('#mainwp-posts-table').DataTable({ |
| 526 | 686 | "responsive": responsive, |
| 527 | - "colReorder": { | |
| 528 | - fixedColumnsLeft: 1, | |
| 529 | - fixedColumnsRight: 1 | |
| 530 | - }, | |
| 687 | + "colReorder": { columns: ":not(.check-column):not(:last-child)" }, | |
| 531 | 688 | "stateSave": true, |
| 532 | 689 | "pagingType": "full_numbers", |
| 533 | 690 | "order": [], |
| 534 | 691 | "scrollX": true, |
| @@ -536,12 +693,42 @@ | ||
| 536 | 693 | "columnDefs": [{ |
| 537 | 694 | "targets": 'no-sort', |
| 538 | 695 | "orderable": false |
| 539 | 696 | }], |
| 540 | - "preDrawCallback": function () { | |
| 541 | - jQuery('#mainwp-posts-table-wrapper table .ui.dropdown').dropdown(); | |
| 542 | - jQuery('#mainwp-posts-table-wrapper table .ui.checkbox').checkbox(); | |
| 543 | - mainwp_datatable_fix_menu_overflow(); | |
| 697 | + "drawCallback": function () { | |
| 698 | + setTimeout(() => { | |
| 699 | + jQuery('#mainwp-posts-table .ui.dropdown').dropdown(); | |
| 700 | + jQuery('#mainwp-posts-table .ui.checkbox').checkbox(); | |
| 701 | + mainwp_datatable_fix_menu_overflow('#mainwp-posts-table'); | |
| 702 | + mainwp_table_check_columns_init(); // ajax: to fix checkbox all. | |
| 703 | + updatePostsBulkActionsState(); | |
| 704 | + }, 1000); | |
| 705 | + }, | |
| 706 | + select: { | |
| 707 | + items: 'row', | |
| 708 | + style: 'multi+shift', | |
| 709 | + selector: 'tr>td:not(.not-selectable)' | |
| 710 | + } | |
| 711 | + }).on('select', function (e, dt, type, indexes) { | |
| 712 | + if ('row' == type) { | |
| 713 | + dt.rows(indexes) | |
| 714 | + .nodes() | |
| 715 | + .to$().find('td.check-column .ui.checkbox').checkbox('set checked'); | |
| 716 | + updatePostsBulkActionsState(); | |
| 717 | + } | |
| 718 | + }).on('deselect', function (e, dt, type, indexes) { | |
| 719 | + if ('row' == type) { | |
| 720 | + dt.rows(indexes) | |
| 721 | + .nodes() | |
| 722 | + .to$().find('td.check-column .ui.checkbox').checkbox('set unchecked'); | |
| 723 | + updatePostsBulkActionsState(); | |
| 724 | + } | |
| 725 | + }).on('columns-reordered', function () { | |
| 726 | + setTimeout(() => { | |
| 727 | + jQuery('#mainwp-posts-table .ui.dropdown').dropdown(); | |
| 728 | + jQuery('#mainwp-posts-table .ui.checkbox').checkbox(); | |
| 729 | + mainwp_datatable_fix_menu_overflow('#mainwp-posts-table'); | |
| 544 | 730 | mainwp_table_check_columns_init(); // ajax: to fix checkbox all. |
| 545 | - } | |
| 731 | + updatePostsBulkActionsState(); | |
| 732 | + }, 1000); | |
| 546 | 733 | }); |
| 547 | 734 | } |