| @@ -1,10 +1,9 @@ | ||
| 1 | +/* eslint-disable complexity */ | |
| 1 | 2 | |
| 2 | -/* eslint complexity: ["error", 100] */ | |
| 3 | +globalThis.wp = globalThis.wp || {}; | |
| 4 | +globalThis.mainwpVars = globalThis.mainwpVars || {}; | |
| 3 | 5 | |
| 4 | -window.wp = window.wp || {}; | |
| 5 | -window.mainwpVars = window.mainwpVars || {}; | |
| 6 | - | |
| 7 | 6 | let executingUpdateCategories = false; |
| 8 | 7 | let queueUpdateCategories = 0; |
| 9 | 8 | |
| 10 | 9 | function reload_init() { |
| @@ -32,11 +31,13 @@ | ||
| 32 | 31 | if (page || href == 'index.php') { |
| 33 | 32 | if (!jQuery('body').hasClass('mainwp-ui-page')) { |
| 34 | 33 | return; |
| 35 | 34 | } |
| 36 | - if (typeof e !== 'undefined') | |
| 35 | + | |
| 36 | + if (e !== undefined) | |
| 37 | 37 | e.preventDefault(); |
| 38 | - jQuery('#wpbody-content').html('<div class="mainwp-loading"><img src="images/loading.gif" /> ' + __('Please wait...') + '</div>'); | |
| 38 | + | |
| 39 | + jQuery('#wpbody-content').html('<div class="mainwp-loading"><img src="images/loading.gif" /> ' + MainWP.I18n.t('Please wait...') + '</div>'); | |
| 39 | 40 | if (jQuery(obj).hasClass('menu-top')) { |
| 40 | 41 | let top = jQuery(obj).closest('li.menu-top'); |
| 41 | 42 | jQuery('#adminmenu .current').removeClass('current').addClass('wp-not-current-submenu'); |
| 42 | 43 | jQuery('.wp-has-current-submenu').removeClass('wp-has-current-submenu').addClass('wp-not-current-submenu'); |
| @@ -98,9 +99,9 @@ | ||
| 98 | 99 | |
| 99 | 100 | } |
| 100 | 101 | } |
| 101 | 102 | |
| 102 | -window.onpopstate = function (e) { | |
| 103 | +globalThis.onpopstate = function (e) { | |
| 103 | 104 | read_current_url(); |
| 104 | 105 | if (e.state) |
| 105 | 106 | load_url(mainwp_current_url, e.state.anchor); |
| 106 | 107 | } |
| @@ -141,18 +142,19 @@ | ||
| 141 | 142 | stick_element_init(); |
| 142 | 143 | scroll_element(); |
| 143 | 144 | } |
| 144 | 145 | jQuery(function () { |
| 145 | - jQuery(window).trigger('scroll', scroll_element).trigger('resize', stick_element_reset); | |
| 146 | + jQuery(globalThis).trigger('scroll', scroll_element).trigger('resize', stick_element_reset); | |
| 146 | 147 | stick_element_init(); |
| 147 | 148 | }); |
| 148 | 149 | |
| 149 | -window.mainwp_confirm = function (msg, confirmed_callback, cancelled_callback, updateType, multiple, extra) { // updateType: 1 single update, 2 multi update | |
| 150 | +// eslint-disable-next-line complexity | |
| 151 | +globalThis.mainwp_confirm = function (msg, confirmed_callback, cancelled_callback, updateType, multiple, extra) { // updateType: 1 single update, 2 multi update | |
| 150 | 152 | let confVal; |
| 151 | 153 | if (jQuery('#mainwp-disable-update-confirmations').length > 0) { |
| 152 | 154 | confVal = jQuery('#mainwp-disable-update-confirmations').val(); |
| 153 | 155 | } |
| 154 | - if (jQuery('#mainwp-disable-update-confirmations').length > 0 && typeof updateType !== 'undefined' && updateType !== false && (confVal == 2 || (confVal == 1 && updateType == 1)) && confirmed_callback && typeof confirmed_callback == 'function') { | |
| 156 | + if (jQuery('#mainwp-disable-update-confirmations').length > 0 && updateType !== undefined && updateType !== false && (confVal == 2 || (confVal == 1 && updateType == 1)) && confirmed_callback && typeof confirmed_callback == 'function') { | |
| 155 | 157 | confirmed_callback(); |
| 156 | 158 | return; |
| 157 | 159 | } |
| 158 | 160 | |
| @@ -158,9 +160,12 @@ | ||
| 158 | 160 | |
| 159 | 161 | |
| 160 | 162 | jQuery('#mainwp-modal-confirm .content-massage').html(msg); |
| 161 | 163 | |
| 162 | - if (typeof extra !== 'undefined' && extra !== false) { | |
| 164 | + jQuery('#mainwp-confirm-form').hide(); | |
| 165 | + jQuery('#mainwp-confirm-input').val(''); | |
| 166 | + | |
| 167 | + if (extra !== undefined && extra !== false) { | |
| 163 | 168 | jQuery('#mainwp-confirm-form').show(); |
| 164 | 169 | jQuery('#mainwp-confirm-form').find('label').html('Type ' + extra + ' to confirm'); |
| 165 | 170 | } |
| 166 | 171 | |
| @@ -165,9 +170,9 @@ | ||
| 165 | 170 | } |
| 166 | 171 | |
| 167 | 172 | let opts = { |
| 168 | 173 | onApprove: function () { |
| 169 | - if (typeof extra !== 'undefined' && extra !== false) { | |
| 174 | + if (extra !== undefined && extra !== false) { | |
| 170 | 175 | let extraValue = jQuery('#mainwp-confirm-input').val(); |
| 171 | 176 | if (confirmed_callback && typeof confirmed_callback == 'function') { |
| 172 | 177 | if (extraValue === extra) { |
| 173 | 178 | confirmed_callback(); |
| @@ -172,9 +177,9 @@ | ||
| 172 | 177 | if (extraValue === extra) { |
| 173 | 178 | confirmed_callback(); |
| 174 | 179 | } else { |
| 175 | 180 | jQuery('#mainwp-confirm-input').val('').trigger('focus').transition('shake'); |
| 176 | - return; | |
| 181 | + return false; | |
| 177 | 182 | } |
| 178 | 183 | } |
| 179 | 184 | } else if (confirmed_callback && typeof confirmed_callback == 'function') { |
| 180 | 185 | confirmed_callback(); |
| @@ -192,14 +197,13 @@ | ||
| 192 | 197 | |
| 193 | 198 | jQuery('#mainwp-modal-confirm').modal(opts).modal('show'); |
| 194 | 199 | |
| 195 | 200 | // if it is update confirm and then display the update confirm notice text |
| 196 | - if (typeof updateType !== 'undefined' && updateType !== false && (updateType == 1 || updateType == 2)) { | |
| 201 | + if (updateType !== undefined && updateType !== false && (updateType == 1 || updateType == 2)) { | |
| 197 | 202 | jQuery('#mainwp-modal-confirm .update-confirm-notice ').show(); |
| 198 | 203 | } |
| 199 | 204 | } |
| 200 | 205 | |
| 201 | - | |
| 202 | 206 | /** |
| 203 | 207 | * Select sites |
| 204 | 208 | */ |
| 205 | 209 | jQuery(document).on('change', '.mainwp_selected_sites_item input:checkbox', function () { |
| @@ -246,23 +250,72 @@ | ||
| 246 | 250 | jQuery(this).parent().removeClass('selected_groups_item_checked'); |
| 247 | 251 | }); |
| 248 | 252 | }); |
| 249 | 253 | |
| 254 | +let mainwp_update_selection_counter = function () { | |
| 255 | + let parent = jQuery('.mainwp_select_sites_wrapper'); | |
| 256 | + let tab = parent.find('input[name="select_sites_tab"]').val(); | |
| 257 | + let count = 0; | |
| 258 | + | |
| 259 | + if (tab == 'site') { | |
| 260 | + count = parent.find('.mainwp-select-sites-list INPUT:checkbox:checked').length; | |
| 261 | + } else if (tab == 'staging') { | |
| 262 | + count = parent.find('#mainwp-select-staging-sites-list INPUT:checkbox:checked').length; | |
| 263 | + } else if (tab == 'client') { | |
| 264 | + count = parent.find('#mainwp-select-clients-list INPUT:checkbox:checked').length; | |
| 265 | + } else if (tab == 'group') { | |
| 266 | + count = parent.find('.mainwp-select-groups-list INPUT:checkbox:checked').length; | |
| 267 | + } | |
| 268 | + | |
| 269 | + let counter = parent.find('.mainwp-sites-selection-counter'); | |
| 270 | + counter.text(count); | |
| 271 | + | |
| 272 | + let wrapper = parent.find('.mainwp-selection-counter-wrapper'); | |
| 273 | + let selectedText = parent.find('.mainwp-selected-text'); | |
| 274 | + | |
| 275 | + if (wrapper.length > 0) { | |
| 276 | + if (count > 0) { | |
| 277 | + counter.removeClass('grey').addClass('green'); | |
| 278 | + selectedText.removeClass('grey').addClass('green'); | |
| 279 | + wrapper.css('cursor', 'pointer') | |
| 280 | + .attr('data-tooltip', 'Create a tag with selected sites') | |
| 281 | + .attr('data-position', 'bottom right') | |
| 282 | + .attr('data-inverted', ''); | |
| 283 | + | |
| 284 | + if (!wrapper.find('.tag.icon').length) { | |
| 285 | + counter.before('<i class="tag small green icon"></i> '); | |
| 286 | + } | |
| 287 | + } else { | |
| 288 | + counter.removeClass('green').addClass('grey'); | |
| 289 | + selectedText.removeClass('green').addClass('grey'); | |
| 290 | + wrapper.css('cursor', 'default') | |
| 291 | + .removeAttr('data-tooltip') | |
| 292 | + .removeAttr('data-position') | |
| 293 | + .removeAttr('data-inverted'); | |
| 294 | + | |
| 295 | + wrapper.find('.tag.icon').remove(); | |
| 296 | + } | |
| 297 | + } | |
| 298 | +}; | |
| 299 | + | |
| 250 | 300 | let mainwp_site_select = function () { |
| 251 | 301 | mainwp_newpost_updateCategories(); |
| 302 | + mainwp_update_selection_counter(); | |
| 252 | 303 | }; |
| 253 | 304 | let mainwp_group_select = function () { |
| 254 | 305 | mainwp_newpost_updateCategories(); |
| 306 | + mainwp_update_selection_counter(); | |
| 255 | 307 | }; |
| 256 | 308 | let mainwp_client_select = function () { |
| 257 | 309 | mainwp_newpost_updateCategories(); |
| 310 | + mainwp_update_selection_counter(); | |
| 258 | 311 | }; |
| 259 | 312 | |
| 260 | 313 | let mainwp_ss_select = function (me, val) { |
| 261 | 314 | let parent = jQuery(me).closest('.mainwp_select_sites_wrapper'); |
| 262 | - let tab = parent.find('#select_sites_tab').val(); | |
| 315 | + let tab = parent.find('input[name="select_sites_tab"]').val(); | |
| 263 | 316 | if (tab == 'site') { |
| 264 | - parent.find('#mainwp-select-sites-list .item:not(.no-select) INPUT:enabled:checkbox').each(function () { | |
| 317 | + parent.find('.mainwp-select-sites-list .item:not(.no-select) INPUT:enabled:checkbox').each(function () { | |
| 265 | 318 | jQuery(this).attr('checked', val).trigger("change"); |
| 266 | 319 | if (val) { |
| 267 | 320 | jQuery(this).closest('.item.checkbox').checkbox('set checked'); |
| 268 | 321 | } |
| @@ -291,9 +344,9 @@ | ||
| 291 | 344 | } |
| 292 | 345 | }); |
| 293 | 346 | |
| 294 | 347 | } else { //group |
| 295 | - parent.find('#mainwp-select-groups-list .item:not(.no-select) INPUT:enabled:checkbox').each(function () { | |
| 348 | + parent.find('.mainwp-select-groups-list .item:not(.no-select) INPUT:enabled:checkbox').each(function () { | |
| 296 | 349 | jQuery(this).attr('checked', val).trigger("change"); |
| 297 | 350 | if (val) { |
| 298 | 351 | jQuery(this).closest('.item.checkbox').checkbox('set checked'); |
| 299 | 352 | } |
| @@ -301,16 +354,10 @@ | ||
| 301 | 354 | jQuery(this).closest('.item.checkbox').checkbox('set unchecked'); |
| 302 | 355 | } |
| 303 | 356 | }); |
| 304 | 357 | } |
| 305 | - if (val) { | |
| 306 | - jQuery('.mainwp-ss-select').hide(); | |
| 307 | - jQuery('.mainwp-ss-deselect').show(); | |
| 308 | - } else { | |
| 309 | - jQuery('.mainwp-ss-select').show(); | |
| 310 | - jQuery('.mainwp-ss-deselect').hide(); | |
| 311 | - } | |
| 312 | 358 | mainwp_newpost_updateCategories(); |
| 359 | + mainwp_update_selection_counter(); | |
| 313 | 360 | return false; |
| 314 | 361 | }; |
| 315 | 362 | |
| 316 | 363 | |
| @@ -315,11 +362,11 @@ | ||
| 315 | 362 | |
| 316 | 363 | |
| 317 | 364 | let mainwp_ss_select_disconnected = function (me, val) { |
| 318 | 365 | let parent = jQuery(me).closest('.mainwp_select_sites_wrapper'); |
| 319 | - let tab = parent.find('#select_sites_tab').val(); | |
| 366 | + let tab = parent.find('input[name="select_sites_tab"]').val(); | |
| 320 | 367 | if (tab == 'site') { |
| 321 | - parent.find('#mainwp-select-sites-list .warning.item:not(.no-select) INPUT:enabled:checkbox').each(function () { | |
| 368 | + parent.find('.mainwp-select-sites-list .warning.item:not(.no-select) INPUT:enabled:checkbox').each(function () { | |
| 322 | 369 | jQuery(this).attr('checked', val).trigger("change"); |
| 323 | 370 | if (val) { |
| 324 | 371 | jQuery(this).closest('.item.warning.checkbox').checkbox('set checked'); |
| 325 | 372 | } |
| @@ -327,54 +374,47 @@ | ||
| 327 | 374 | jQuery(this).closest('.item.warning.checkbox').checkbox('set unchecked'); |
| 328 | 375 | } |
| 329 | 376 | }); |
| 330 | 377 | } |
| 331 | - if (val) { | |
| 332 | - jQuery('.mainwp-ss-select-disconnected').hide(); | |
| 333 | - jQuery('.mainwp-ss-deselect-disconnected').show(); | |
| 334 | - } else { | |
| 335 | - jQuery('.mainwp-ss-select-disconnected').show(); | |
| 336 | - jQuery('.mainwp-ss-deselect-disconnected').hide(); | |
| 337 | - } | |
| 338 | 378 | mainwp_newpost_updateCategories(); |
| 379 | + mainwp_update_selection_counter(); | |
| 339 | 380 | return false; |
| 340 | 381 | }; |
| 341 | 382 | |
| 342 | -window.mainwp_sites_selection_onvisible_callback = function (me) { | |
| 383 | +globalThis.mainwp_sites_selection_onvisible_callback = function (me) { | |
| 343 | 384 | let selected_tab = jQuery(me).attr('select-by'); |
| 344 | 385 | let select_by = 'site'; |
| 345 | 386 | let parent = jQuery(me).closest('.mainwp_select_sites_wrapper'); |
| 346 | 387 | if (selected_tab == 'staging') { |
| 347 | 388 | // uncheck live sites |
| 348 | - parent.find('#mainwp-select-sites-list INPUT:checkbox').prop('checked', false); | |
| 349 | - parent.find('#mainwp-select-groups-list INPUT:checkbox').prop('checked', false); | |
| 389 | + parent.find('.mainwp-select-sites-list INPUT:checkbox').prop('checked', false); | |
| 390 | + parent.find('.mainwp-select-groups-list INPUT:checkbox').prop('checked', false); | |
| 350 | 391 | parent.find('#mainwp-select-clients-list INPUT:checkbox').prop('checked', false); |
| 351 | 392 | } else if (selected_tab == 'site') { |
| 352 | 393 | // uncheck staging sites |
| 353 | 394 | parent.find('#mainwp-select-staging-sites-list INPUT:checkbox').prop('checked', false); |
| 354 | - parent.find('#mainwp-select-groups-list INPUT:checkbox').prop('checked', false); | |
| 395 | + parent.find('.mainwp-select-groups-list INPUT:checkbox').prop('checked', false); | |
| 355 | 396 | parent.find('#mainwp-select-clients-list INPUT:checkbox').prop('checked', false); |
| 356 | 397 | } else if (selected_tab == 'group') { |
| 357 | 398 | // uncheck sites |
| 358 | - parent.find('#mainwp-select-sites-list INPUT:checkbox').prop('checked', false); | |
| 399 | + parent.find('.mainwp-select-sites-list INPUT:checkbox').prop('checked', false); | |
| 359 | 400 | parent.find('#mainwp-select-staging-sites-list INPUT:checkbox').prop('checked', false); |
| 360 | 401 | parent.find('#mainwp-select-clients-list INPUT:checkbox').prop('checked', false); |
| 361 | 402 | select_by = 'group'; |
| 362 | 403 | } else if (selected_tab == 'client') { |
| 363 | 404 | // uncheck sites |
| 364 | - parent.find('#mainwp-select-sites-list INPUT:checkbox').prop('checked', false); | |
| 365 | - parent.find('#mainwp-select-groups-list INPUT:checkbox').prop('checked', false); | |
| 405 | + parent.find('.mainwp-select-sites-list INPUT:checkbox').prop('checked', false); | |
| 406 | + parent.find('.mainwp-select-groups-list INPUT:checkbox').prop('checked', false); | |
| 366 | 407 | parent.find('#mainwp-select-staging-sites-list INPUT:checkbox').prop('checked', false); |
| 367 | 408 | select_by = 'client'; |
| 368 | 409 | } |
| 369 | 410 | |
| 370 | - jQuery('.mainwp-ss-select').show(); | |
| 371 | - jQuery('.mainwp-ss-deselect').hide(); | |
| 411 | + console.log('select by: ' + select_by); | |
| 372 | 412 | |
| 373 | - console.log('select by: ' + select_by); | |
| 413 | + parent.find('input[name="select_by"]').val(select_by); | |
| 414 | + parent.find('input[name="select_sites_tab"]').val(selected_tab); | |
| 374 | 415 | |
| 375 | - parent.find('#select_by').val(select_by); | |
| 376 | - parent.find('#select_sites_tab').val(selected_tab); | |
| 416 | + mainwp_update_selection_counter(); | |
| 377 | 417 | } |
| 378 | 418 | |
| 379 | 419 | |
| 380 | 420 | let mainwp_newpost_updateCategories = function () { |
| @@ -382,17 +422,16 @@ | ||
| 382 | 422 | queueUpdateCategories++; |
| 383 | 423 | return; |
| 384 | 424 | } |
| 385 | 425 | executingUpdateCategories = true; |
| 386 | - console.log('mainwp_newpost_updateCategories'); | |
| 387 | 426 | let catsSelection = jQuery('#categorychecklist'); |
| 388 | 427 | if (catsSelection.length > 0) { |
| 389 | - let tab = jQuery('#select_sites_tab').val(); | |
| 428 | + let tab = jQuery('input[name="select_sites_tab"]').val(); | |
| 390 | 429 | let sites = []; |
| 391 | 430 | let groups = []; |
| 392 | 431 | let clients = []; |
| 393 | 432 | if (tab == 'site') { |
| 394 | - sites = jQuery.map(jQuery('#mainwp-select-sites-list INPUT:checkbox:checked'), function (el) { | |
| 433 | + sites = jQuery.map(jQuery('.mainwp-select-sites-list INPUT:checkbox:checked'), function (el) { | |
| 395 | 434 | return jQuery(el).val(); |
| 396 | 435 | }); |
| 397 | 436 | } else if (tab == 'staging') { |
| 398 | 437 | sites = jQuery.map(jQuery('#mainwp-select-staging-sites-list INPUT:checkbox:checked'), function (el) { |
| @@ -402,9 +441,9 @@ | ||
| 402 | 441 | clients = jQuery.map(jQuery('#mainwp-select-clients-list INPUT:checkbox:checked'), function (el) { |
| 403 | 442 | return jQuery(el).val(); |
| 404 | 443 | }); |
| 405 | 444 | } else { //group |
| 406 | - groups = jQuery.map(jQuery('#mainwp-select-groups-list INPUT:checkbox:checked'), function (el) { | |
| 445 | + groups = jQuery.map(jQuery('.mainwp-select-groups-list INPUT:checkbox:checked'), function (el) { | |
| 407 | 446 | return jQuery(el).val(); |
| 408 | 447 | }); |
| 409 | 448 | } |
| 410 | 449 | |
| @@ -409,10 +448,8 @@ | ||
| 409 | 448 | } |
| 410 | 449 | |
| 411 | 450 | let selected_categories = catsSelection.dropdown('get value'); |
| 412 | 451 | |
| 413 | - console.log('selected cats:'); | |
| 414 | - console.log(selected_categories); | |
| 415 | 452 | |
| 416 | 453 | let data = mainwp_secure_data({ |
| 417 | 454 | action: 'mainwp_get_categories', |
| 418 | 455 | sites: encodeURIComponent(sites.join(',')), |
| @@ -430,10 +467,19 @@ | ||
| 430 | 467 | catsSelection.find('.menu').html(response.content); |
| 431 | 468 | catsSelection.dropdown('refresh'); |
| 432 | 469 | let arrVal = pSelectedCategories.split(","); |
| 433 | 470 | catsSelection.dropdown('set selected', arrVal); |
| 434 | - console.log('re-selected: ' + pSelectedCategories); | |
| 435 | 471 | updateCategoriesPostFunc(); |
| 472 | + let popWrapper = jQuery('#categorychecklist .mainwp-html-popup'); | |
| 473 | + if( popWrapper.length > 0 ) { | |
| 474 | + jQuery(popWrapper).each( | |
| 475 | + function(){ | |
| 476 | + const popupContent = jQuery(this).attr('html-popup-content') ?? ''; | |
| 477 | + mainwp_init_html_popup(this, mainwp_escape_html(popupContent)); | |
| 478 | + } | |
| 479 | + ); | |
| 480 | + } | |
| 481 | + | |
| 436 | 482 | } |
| 437 | 483 | }; |
| 438 | 484 | }(selected_categories), 'json'); |
| 439 | 485 | } else { |
| @@ -462,9 +508,9 @@ | ||
| 462 | 508 | |
| 463 | 509 | for (let ss of siteItems) { |
| 464 | 510 | let currentElement = jQuery(ss); |
| 465 | 511 | let value = currentElement.find('label').text().toLowerCase(); |
| 466 | - if (value.indexOf(filter) > -1) { | |
| 512 | + if (value.includes(filter)) { | |
| 467 | 513 | currentElement.show(); |
| 468 | 514 | } else { |
| 469 | 515 | currentElement.hide(); |
| 470 | 516 | } |
| @@ -475,9 +521,9 @@ | ||
| 475 | 521 | jQuery(function () { |
| 476 | 522 | mainwp_sidebar_accordion_init(); |
| 477 | 523 | }); |
| 478 | 524 | |
| 479 | -window.mainwp_sidebar_accordion_init = function(){ | |
| 525 | +globalThis.mainwp_sidebar_accordion_init = function () { | |
| 480 | 526 | if (jQuery('.mainwp-sidebar-accordion').length > 0) { |
| 481 | 527 | console.log('sidebar accordion init'); |
| 482 | 528 | jQuery('.mainwp-sidebar-accordion').accordion({ |
| 483 | 529 | "onOpening": function () { |
| @@ -518,15 +564,15 @@ | ||
| 518 | 564 | } |
| 519 | 565 | }); |
| 520 | 566 | }; |
| 521 | 567 | |
| 522 | -window.mainwp_ui_state_save = function (ident, val) { | |
| 568 | +globalThis.mainwp_ui_state_save = function (ident, val) { | |
| 523 | 569 | if (typeof (Storage) !== 'undefined') { |
| 524 | 570 | localStorage.setItem('mainwp-dashboard[' + ident + ']', val); |
| 525 | 571 | } |
| 526 | 572 | }; |
| 527 | 573 | |
| 528 | -window.mainwp_ui_state_load = function (ident) { | |
| 574 | +globalThis.mainwp_ui_state_load = function (ident) { | |
| 529 | 575 | if (typeof (Storage) !== 'undefined') { |
| 530 | 576 | return localStorage.getItem('mainwp-dashboard[' + ident + ']'); |
| 531 | 577 | } |
| 532 | 578 | return '1'; // show if Storage undefined. |
| @@ -531,12 +577,21 @@ | ||
| 531 | 577 | } |
| 532 | 578 | return '1'; // show if Storage undefined. |
| 533 | 579 | }; |
| 534 | 580 | |
| 535 | -window.mainwp_sites_filter_select = function (objInput) { | |
| 581 | +globalThis.mainwp_ui_state_init = function (ident, callback) { | |
| 582 | + if (typeof (Storage) !== 'undefined') { | |
| 583 | + let state = mainwp_ui_state_load(ident); | |
| 584 | + if (typeof callback === 'function') { | |
| 585 | + callback(state); | |
| 586 | + } | |
| 587 | + } | |
| 588 | +}; | |
| 589 | + | |
| 590 | +globalThis.mainwp_sites_filter_select = function (objInput) { | |
| 536 | 591 | let filter = jQuery(objInput).val().toLowerCase(); |
| 537 | 592 | let parent = jQuery(objInput).closest('.mainwp_select_sites_wrapper'); |
| 538 | - let tab = jQuery('#select_sites_tab').val(); | |
| 593 | + let tab = jQuery('input[name="select_sites_tab"]').val(); | |
| 539 | 594 | let siteItems = []; |
| 540 | 595 | |
| 541 | 596 | if (tab == 'group') { |
| 542 | 597 | siteItems = parent.find('.mainwp_selected_groups_item'); |
| @@ -546,9 +601,9 @@ | ||
| 546 | 601 | |
| 547 | 602 | for (let id of siteItems) { |
| 548 | 603 | let currentElement = jQuery(id); |
| 549 | 604 | let value = currentElement.find('label').text().toLowerCase(); |
| 550 | - if (value.indexOf(filter) > -1) { | |
| 605 | + if (value.includes(filter)) { | |
| 551 | 606 | currentElement.removeClass('no-select').show(); |
| 552 | 607 | } else { |
| 553 | 608 | currentElement.addClass('no-select').hide(); |
| 554 | 609 | } |
| @@ -557,8 +612,28 @@ | ||
| 557 | 612 | mainwp_newpost_updateCategories(); |
| 558 | 613 | } |
| 559 | 614 | } |
| 560 | 615 | |
| 616 | +globalThis.mainwp_tags_filter_select = function (objInput) { | |
| 617 | + let filter = jQuery(objInput).val().toLowerCase(); | |
| 618 | + let parent = jQuery(objInput).closest('.mainwp-search-options'); | |
| 619 | + let tags = []; | |
| 620 | + | |
| 621 | + tags = parent.find('a.item'); | |
| 622 | + | |
| 623 | + console.log(tags); | |
| 624 | + | |
| 625 | + for (let id of tags) { | |
| 626 | + let currentElement = jQuery(id); | |
| 627 | + let value = currentElement.text().toLowerCase(); | |
| 628 | + if (value.includes(filter)) { | |
| 629 | + currentElement.show(); | |
| 630 | + } else { | |
| 631 | + currentElement.hide(); | |
| 632 | + } | |
| 633 | + } | |
| 634 | +} | |
| 635 | + | |
| 561 | 636 | jQuery(document).on('keyup', '#mainwp-select-sites-filter', function () { |
| 562 | 637 | mainwp_sites_filter_select(this); |
| 563 | 638 | }); |
| 564 | 639 | |
| @@ -565,18 +640,21 @@ | ||
| 565 | 640 | jQuery(document).on('keyup', '#mainwp-screenshots-sites-filter', function () { |
| 566 | 641 | mainwp_sites_filter_select(this); |
| 567 | 642 | }); |
| 568 | 643 | |
| 569 | -jQuery(function(){ | |
| 570 | - if(jQuery(document).find('#mainwp-select-sites-header .ui.menu .item').length){ | |
| 571 | - jQuery(document).find('#mainwp-select-sites-header .ui.menu .item').tab( {'onVisible': function() { mainwp_sites_selection_onvisible_callback( this ); } } ); | |
| 644 | +jQuery(document).on('keyup', '#mainwp-select-tags-filter', function () { | |
| 645 | + mainwp_tags_filter_select(this); | |
| 646 | +}); | |
| 647 | + | |
| 648 | +jQuery(function () { | |
| 649 | + if (jQuery(document).find('.mainwp-select-sites-header .ui.menu .item').length) { | |
| 650 | + jQuery(document).find('.mainwp-select-sites-header .ui.menu .item').tab({ 'onVisible': function () { mainwp_sites_selection_onvisible_callback(this); } }); | |
| 572 | 651 | } |
| 573 | 652 | }); |
| 574 | 653 | |
| 575 | -window.mainwp_get_icon_start = function () { | |
| 654 | +globalThis.mainwp_get_icon_start = function () { | |
| 576 | 655 | jQuery('.cached-icon-expired').attr('queue', 1); |
| 577 | 656 | mainwpVars.bulkInstallTotal = jQuery('.cached-icon-expired[queue="1"]').length; |
| 578 | - console.log(mainwpVars.bulkInstallTotal + ': expired cached icons'); | |
| 579 | 657 | if (0 === mainwpVars.bulkInstallTotal) { |
| 580 | 658 | return; |
| 581 | 659 | } |
| 582 | 660 | mainwp_get_icon_start_next(); |
| @@ -610,9 +688,8 @@ | ||
| 610 | 688 | slug: slug, |
| 611 | 689 | type: type |
| 612 | 690 | }); |
| 613 | 691 | jQuery.post(ajaxurl, data, function (response) { |
| 614 | - console.log(response + ': ' + slug); | |
| 615 | 692 | mainwp_get_icon_start_next(); |
| 616 | 693 | }); |
| 617 | 694 | } |
| 618 | 695 | |
| @@ -637,9 +714,9 @@ | ||
| 637 | 714 | jQuery('#mainwp-select-mainwp-themes-modal').modal({ |
| 638 | 715 | allowMultiple: false, |
| 639 | 716 | closable: true, |
| 640 | 717 | onHide: function () { |
| 641 | - window.location.href = location.href; | |
| 718 | + mainwp_forceReload(); | |
| 642 | 719 | } |
| 643 | 720 | }).modal('show'); |
| 644 | 721 | |
| 645 | 722 | return false; |
| @@ -659,9 +736,9 @@ | ||
| 659 | 736 | let slug = jQuery(iconObj).attr('item-slug'); |
| 660 | 737 | |
| 661 | 738 | let deleteIcon = jQuery('#mainwp_delete_image_chk').is(':checked'); |
| 662 | 739 | |
| 663 | - let msg = __('Updating the icon. Please wait...'); | |
| 740 | + let msg = MainWP.I18n.t('Updating the icon. Please wait...'); | |
| 664 | 741 | mainwp_set_message_zone('#mainwp-message-zone-upload', '<i class="notched circle loading icon"></i> ' + msg, ''); |
| 665 | 742 | jQuery('#update_custom_icon_btn').attr('disabled', 'disabled'); |
| 666 | 743 | |
| 667 | 744 | //Add via ajax!! |
| @@ -670,8 +747,9 @@ | ||
| 670 | 747 | formdata.append("type", type); |
| 671 | 748 | formdata.append("slug", slug); |
| 672 | 749 | formdata.append("delete", deleteIcon ? 1 : 0); |
| 673 | 750 | formdata.append("security", security_nonces['mainwp_upload_custom_icon']); |
| 751 | + formdata.append("delnonce", jQuery(iconObj).attr('del-icon-nonce')); | |
| 674 | 752 | |
| 675 | 753 | jQuery.ajax({ |
| 676 | 754 | type: 'POST', |
| 677 | 755 | url: ajaxurl, |
| @@ -676,30 +754,32 @@ | ||
| 676 | 754 | type: 'POST', |
| 677 | 755 | url: ajaxurl, |
| 678 | 756 | data: formdata, |
| 679 | 757 | success: function (response) { |
| 680 | - if (response && response.result == 'success') { | |
| 758 | + if (response?.result == 'success') { | |
| 681 | 759 | let msg = ''; |
| 682 | 760 | |
| 683 | 761 | if (type === 'plugin') { |
| 684 | 762 | if (jQuery('#mainwp-show-plugins').length > 0) { |
| 685 | - msg = __('Loading...'); | |
| 763 | + msg = MainWP.I18n.t('Loading...'); | |
| 686 | 764 | mainwp_fetch_plugins(); |
| 687 | 765 | } |
| 688 | 766 | } else if (jQuery('#mainwp_show_themes').length > 0) { |
| 689 | - msg = __('Loading...'); | |
| 767 | + msg = MainWP.I18n.t('Loading...'); | |
| 690 | 768 | mainwp_fetch_themes(); |
| 691 | 769 | } |
| 692 | - if (msg !== '') { | |
| 693 | - mainwp_set_message_zone('#mainwp-message-zone-upload', '<i class="notched circle loading icon"></i> ' + __('Loading...'), ''); | |
| 770 | + if (msg === '') { | |
| 771 | + mainwp_set_message_zone('#mainwp-message-zone-upload'); | |
| 694 | 772 | } else { |
| 695 | - mainwp_set_message_zone('#mainwp-message-zone-upload'); | |
| 773 | + mainwp_set_message_zone('#mainwp-message-zone-upload', '<i class="notched circle loading icon"></i> ' + MainWP.I18n.t('Loading...'), ''); | |
| 696 | 774 | } |
| 697 | 775 | setTimeout(function () { |
| 698 | - window.location.href = location.href; | |
| 776 | + mainwp_forceReload(); | |
| 699 | 777 | }, 3000); |
| 778 | + } else if (response.error) { | |
| 779 | + feedback('mainwp-message-zone-upload', response.error, 'red'); | |
| 700 | 780 | } else { |
| 701 | - feedback('mainwp-message-zone-upload', __('Undefined error. Please try again.'), 'red'); | |
| 781 | + feedback('mainwp-message-zone-upload', MainWP.I18n.t('Undefined error. Please try again.'), 'red'); | |
| 702 | 782 | } |
| 703 | 783 | }, |
| 704 | 784 | error: function () { |
| 705 | 785 | }, |
| @@ -711,15 +791,103 @@ | ||
| 711 | 791 | }); |
| 712 | 792 | |
| 713 | 793 | }; |
| 714 | 794 | |
| 795 | +/** | |
| 796 | + * Initialize button state based on site selection checkboxes. | |
| 797 | + * Disables specified buttons until at least one site/tag(group)/client is selected. | |
| 798 | + * | |
| 799 | + * @param {Array|string} buttonIds - Array of button IDs or single button ID string | |
| 800 | + * @param {string} containerSelector - Optional container selector (default: '#mainwp-select-sites') | |
| 801 | + */ | |
| 802 | +let mainwp_init_button_site_selection_dependency = function (buttonIds, containerSelector = '#mainwp-select-sites') { | |
| 803 | + | |
| 804 | + // Convert single button ID to array | |
| 805 | + if (typeof buttonIds === 'string') { | |
| 806 | + buttonIds = [buttonIds]; | |
| 807 | + } | |
| 808 | + | |
| 809 | + // Function to check if any checkbox is selected | |
| 810 | + let checkSelectionState = function () { | |
| 811 | + let hasSelection = false; | |
| 812 | + | |
| 813 | + // Check all three types of checkboxes | |
| 814 | + let selectors = [ | |
| 815 | + 'input[name="selected_sites[]"]:checked', | |
| 816 | + 'input[name="selected_groups[]"]:checked', | |
| 817 | + 'input[name="selected_clients[]"]:checked' | |
| 818 | + ]; | |
| 819 | + | |
| 820 | + jQuery.each(selectors, function (index, selector) { | |
| 821 | + if (jQuery(containerSelector + ' ' + selector).length > 0) { | |
| 822 | + hasSelection = true; | |
| 823 | + return false; // break the loop | |
| 824 | + } | |
| 825 | + }); | |
| 826 | + | |
| 827 | + // Enable or disable buttons based on selection state | |
| 828 | + jQuery.each(buttonIds, function (index, buttonId) { | |
| 829 | + let $button = jQuery('#' + buttonId); | |
| 830 | + let $wrapper = jQuery('#' + buttonId + '_wrapper'); | |
| 831 | + | |
| 832 | + if ($button.length > 0) { | |
| 833 | + if (hasSelection) { | |
| 834 | + // Enable button | |
| 835 | + $button.removeClass('disabled').css('pointer-events', ''); | |
| 836 | + // Remove wrapper tooltip if exists | |
| 837 | + if ($wrapper.length > 0) { | |
| 838 | + $wrapper.removeAttr('data-tooltip'); | |
| 839 | + } | |
| 840 | + } else { | |
| 841 | + // Disable button | |
| 842 | + $button.addClass('disabled').css('pointer-events', 'none'); | |
| 843 | + // Add wrapper tooltip if exists | |
| 844 | + if ($wrapper.length > 0) { | |
| 845 | + $wrapper.attr('data-tooltip', 'Select at least one site, client or tag.').attr('data-inverted', '').attr('data-position', 'bottom center'); | |
| 846 | + } | |
| 847 | + } | |
| 848 | + } | |
| 849 | + }); | |
| 850 | + }; | |
| 851 | + | |
| 852 | + // Initialize buttons as disabled with tooltip wrapper | |
| 853 | + jQuery.each(buttonIds, function (index, buttonId) { | |
| 854 | + let $button = jQuery('#' + buttonId); | |
| 855 | + if ($button.length > 0) { | |
| 856 | + // Wrap button in a span for tooltip (if not already wrapped) | |
| 857 | + if ($button.parent().attr('id') !== buttonId + '_wrapper') { | |
| 858 | + $button.wrap('<span id="' + buttonId + '_wrapper"></span>'); | |
| 859 | + } | |
| 860 | + let $wrapper = jQuery('#' + buttonId + '_wrapper'); | |
| 861 | + | |
| 862 | + // Disable button and add tooltip to wrapper | |
| 863 | + $button.addClass('disabled'); | |
| 864 | + $wrapper.attr('data-tooltip', 'Select at least one site, client or tag.').attr('data-inverted', '').attr('data-position', 'bottom center'); | |
| 865 | + } | |
| 866 | + }); | |
| 867 | + | |
| 868 | + // Use event delegation to handle checkbox changes (including dynamically loaded ones) | |
| 869 | + jQuery(document).on('change', containerSelector + ' input[name="selected_sites[]"], ' + | |
| 870 | + containerSelector + ' input[name="selected_groups[]"], ' + | |
| 871 | + containerSelector + ' input[name="selected_clients[]"]', function () { | |
| 872 | + checkSelectionState(); | |
| 873 | + }); | |
| 874 | + | |
| 875 | + // Also check on page load in case checkboxes are pre-selected | |
| 876 | + jQuery(document).ready(function () { | |
| 877 | + checkSelectionState(); | |
| 878 | + }); | |
| 879 | +}; | |
| 880 | + | |
| 715 | 881 | let mainwp_upload_custom_types_icon = function (iconObj, uploadAct, iconItemId, iconFileSlug, deleteIcon, callback_uploaded) { |
| 716 | - let msg = __('Updating the icon. Please wait...'); | |
| 882 | + let msg = MainWP.I18n.t('Updating the icon. Please wait...'); | |
| 717 | 883 | mainwp_set_message_zone('#mainwp-message-zone-upload', '<i class="notched circle loading icon"></i> ' + msg, ''); |
| 718 | 884 | jQuery('#update_custom_icon_btn').attr('disabled', 'disabled'); |
| 719 | 885 | |
| 720 | - let upload_act = typeof uploadAct !== "undefined" && '' != uploadAct ? uploadAct : 'mainwp_upload_custom_types_icon'; | |
| 721 | - let elemid = undefined !== jQuery(iconObj).attr('data-element-id') ? jQuery(iconObj).attr('data-element-id') : ''; | |
| 886 | + let upload_act = uploadAct !== undefined && uploadAct !== '' ? uploadAct : 'mainwp_upload_custom_types_icon'; | |
| 887 | + let elemid = jQuery(iconObj).attr('data-element-id') === undefined ? '' : jQuery(iconObj).attr('data-element-id'); | |
| 888 | + let delNonce = jQuery(iconObj).attr('del-icon-nonce') === undefined ? '' : jQuery(iconObj).attr('del-icon-nonce'); | |
| 889 | + | |
| 722 | 890 | //Add via ajax!! |
| 723 | 891 | let formdata = new FormData(jQuery('#uploadicon_form')[0]); |
| 724 | 892 | formdata.append("action", upload_act); |
| 725 | 893 | formdata.append("iconFileSlug", iconFileSlug); |
| @@ -726,8 +894,10 @@ | ||
| 726 | 894 | formdata.append("iconItemId", iconItemId); |
| 727 | 895 | formdata.append("delete", deleteIcon ? 1 : 0); |
| 728 | 896 | formdata.append("security", security_nonces[upload_act]); |
| 729 | 897 | formdata.append("elementid", elemid); |
| 898 | + formdata.append("delnonce", delNonce); | |
| 899 | + | |
| 730 | 900 | jQuery.ajax({ |
| 731 | 901 | type: 'POST', |
| 732 | 902 | url: ajaxurl, |
| 733 | 903 | data: formdata, |
| @@ -732,15 +902,17 @@ | ||
| 732 | 902 | url: ajaxurl, |
| 733 | 903 | data: formdata, |
| 734 | 904 | success: function (response) { |
| 735 | 905 | jQuery('#update_custom_icon_btn').removeAttr('disabled'); |
| 736 | - if (response && response.result == 'success') { | |
| 906 | + if (response?.result == 'success') { | |
| 737 | 907 | mainwp_set_message_zone('#mainwp-message-zone-upload'); |
| 738 | 908 | if (typeof callback_uploaded == 'function') { |
| 739 | 909 | callback_uploaded(response); |
| 740 | 910 | } |
| 911 | + } else if (response.error) { | |
| 912 | + feedback('mainwp-message-zone-upload', response.error, 'red'); | |
| 741 | 913 | } else { |
| 742 | - feedback('mainwp-message-zone-upload', __('Undefined error. Please try again.'), 'red'); | |
| 914 | + feedback('mainwp-message-zone-upload', MainWP.I18n.t('Undefined error. Please try again.'), 'red'); | |
| 743 | 915 | } |
| 744 | 916 | }, |
| 745 | 917 | error: function () { |
| 746 | 918 | }, |
| @@ -759,9 +931,392 @@ | ||
| 759 | 931 | enable: jQuery(me).is(":checked") ? 1 : 0, |
| 760 | 932 | }); |
| 761 | 933 | jQuery.post(ajaxurl, data, function () { |
| 762 | 934 | setTimeout(() => { |
| 763 | - window.location.href = location.href; | |
| 935 | + mainwp_forceReload(); | |
| 764 | 936 | }, 1000); |
| 765 | 937 | }); |
| 766 | 938 | } |
| 767 | 939 | |
| 940 | +let mainwp_help_modal_content_onclick = function (hide, isToolsPage) { | |
| 941 | + | |
| 942 | + // to fix confict ids when current page is tool page. | |
| 943 | + let enab_tour = jQuery('#mainwp-guided-tours-check').checkbox('is checked') ? 1 : 0; | |
| 944 | + let enab_video = jQuery('#mainwp-guided-video-check').checkbox('is checked') ? 1 : 0; | |
| 945 | + let enab_chatbase = jQuery('#mainwp-guided-chatbase-check').checkbox('is checked') ? 1 : 0; | |
| 946 | + | |
| 947 | + const me = jQuery('#mainwp-update-permissions-button'); | |
| 948 | + | |
| 949 | + let data = mainwp_secure_data({ | |
| 950 | + action: 'mainwp_help_modal_content_update', | |
| 951 | + enable_tour: enab_tour, | |
| 952 | + enable_video: enab_video, | |
| 953 | + enable_chatbase: enab_chatbase, | |
| 954 | + }); | |
| 955 | + | |
| 956 | + jQuery(me).html('<i class="notched circle loading icon"></i> Saving...').addClass('disabled'); | |
| 957 | + | |
| 958 | + jQuery.post(ajaxurl, data, function () { | |
| 959 | + if (isToolsPage !== undefined && isToolsPage) { | |
| 960 | + mainwp_forceReload('admin.php?page=MainWPTools'); | |
| 961 | + } else { | |
| 962 | + if (jQuery('#mainwp-guided-chatbase-option').is(':checked')) { | |
| 963 | + jQuery('#mainwp-start-chat-card').removeClass('mainwp-disabled-link'); | |
| 964 | + } else { | |
| 965 | + jQuery('#mainwp-start-chat-card').addClass('mainwp-disabled-link'); | |
| 966 | + jQuery('#mainwp-chatbase-chat-screen').fadeIn('100').find('iframe').attr('src', ''); | |
| 967 | + } | |
| 968 | + | |
| 969 | + if (jQuery('#mainwp-guided-tours-option').is(':checked')) { | |
| 970 | + jQuery('#mainwp-start-tour-card').removeClass('mainwp-disabled-link'); | |
| 971 | + } else { | |
| 972 | + jQuery('#mainwp-start-tour-card').addClass('mainwp-disabled-link'); | |
| 973 | + } | |
| 974 | + | |
| 975 | + if (jQuery('#mainwp-guided-video-option').is(':checked')) { | |
| 976 | + jQuery('#mainwp-start-video-card').removeClass('mainwp-disabled-link'); | |
| 977 | + } else { | |
| 978 | + jQuery('#mainwp-start-video-card').addClass('mainwp-disabled-link'); | |
| 979 | + jQuery('#mainwp-chatbase-video-screen').fadeIn('100').find('iframe').attr('src', ''); | |
| 980 | + } | |
| 981 | + | |
| 982 | + jQuery(me).html('Update Permissions').removeClass('disabled'); | |
| 983 | + } | |
| 984 | + }); | |
| 985 | +} | |
| 986 | + | |
| 987 | +let mainwp_help_modal_start_content_onclick = function (tour_id, video_id, show_chat) { | |
| 988 | + jQuery('#mainwp-chatbase-chat-screen').fadeOut('100'); | |
| 989 | + jQuery('#mainwp-chatbase-video-screen').fadeOut('100'); | |
| 990 | + jQuery('#mainwp-help-modal-options').fadeOut('50'); | |
| 991 | + | |
| 992 | + if (tour_id) { | |
| 993 | + globalThis.USETIFUL.tour.start(tour_id); | |
| 994 | + } | |
| 995 | + | |
| 996 | + if (video_id) { | |
| 997 | + jQuery('#mainwp-chatbase-video-screen').fadeIn('100').find('iframe').attr('src', 'https://www.youtube.com/embed/' + video_id); | |
| 998 | + } | |
| 999 | + | |
| 1000 | + if (show_chat) { | |
| 1001 | + jQuery('#mainwp-chatbase-chat-screen').fadeIn('100').find('iframe').attr('src', 'https://supportassistant.mainwp.com/chatbot-iframe/Tv5dqV-xiQxwgPeMQFCZ4'); | |
| 1002 | + jQuery('#mainwp-start-tour-button').fadeIn('100'); | |
| 1003 | + } | |
| 1004 | +} | |
| 1005 | + | |
| 1006 | +jQuery(document).on('click', '#mainwp-select-light-theme-button', function (e) { | |
| 1007 | + e.preventDefault(); | |
| 1008 | + const url = new URL(globalThis.location.href); | |
| 1009 | + url.searchParams.set('mainwp_quick_theme_change', 'default'); | |
| 1010 | + url.searchParams.set('_wpnonce', mainwpParams.quickThemeChangeNonce); | |
| 1011 | + globalThis.location.href = url.toString(); | |
| 1012 | +}); | |
| 1013 | + | |
| 1014 | +jQuery(document).on('click', '#mainwp-select-dark-theme-button', function (e) { | |
| 1015 | + e.preventDefault(); | |
| 1016 | + const url = new URL(globalThis.location.href); | |
| 1017 | + url.searchParams.set('mainwp_quick_theme_change', 'default-dark'); | |
| 1018 | + url.searchParams.set('_wpnonce', mainwpParams.quickThemeChangeNonce); | |
| 1019 | + globalThis.location.href = url.toString(); | |
| 1020 | +}); | |
| 1021 | + | |
| 1022 | +/** | |
| 1023 | + * Widget layout UI handler object. | |
| 1024 | + * | |
| 1025 | + * Provides methods for managing the widget layout modal UI state and status messages. | |
| 1026 | + */ | |
| 1027 | +if (!globalThis.mainwpUIHandleWidgetsLayout) { | |
| 1028 | + globalThis.mainwpUIHandleWidgetsLayout = (function () { | |
| 1029 | + const statusElemId = '#mainwp-common-edit-widgets-layout-status'; | |
| 1030 | + let _instance = { | |
| 1031 | + loadingStatus: function () { | |
| 1032 | + jQuery(statusElemId).html('<i class="notched circle loading icon"></i> ' + MainWP.I18n.t('Loading layouts. Please wait...')).show(); | |
| 1033 | + }, | |
| 1034 | + savingStatus: function () { | |
| 1035 | + jQuery(statusElemId).html('<i class="notched circle loading icon"></i> ' + MainWP.I18n.t('Saving layout. Please wait...')).show(); | |
| 1036 | + }, | |
| 1037 | + deletingStatus: function () { | |
| 1038 | + jQuery(statusElemId).html('<i class="notched circle loading icon"></i> ' + MainWP.I18n.t('Deleting layout. Please wait...')).show(); | |
| 1039 | + | |
| 1040 | + }, | |
| 1041 | + showWorkingStatus: function (status, addClass) { | |
| 1042 | + if (addClass) { | |
| 1043 | + jQuery(statusElemId).removeClass('red green yellow'); | |
| 1044 | + jQuery(statusElemId).addClass(addClass); | |
| 1045 | + } | |
| 1046 | + jQuery(statusElemId).html(status).show(); | |
| 1047 | + }, | |
| 1048 | + hideWorkingStatus: function () { | |
| 1049 | + jQuery(statusElemId).removeClass('red green').hide(); | |
| 1050 | + }, | |
| 1051 | + showLayout: function (showBtn) { | |
| 1052 | + jQuery('#mainwp-common-edit-widgets-layout-modal > div.header').html(MainWP.I18n.t('Save Layout')); | |
| 1053 | + jQuery('#mainwp-common-edit-widgets-layout-edit-fields').show(); | |
| 1054 | + jQuery('#mainwp-common-edit-widgets-layout-save-button').show(); | |
| 1055 | + jQuery('#mainwp-common-layout-widgets-select-fields').hide(); | |
| 1056 | + jQuery('#mainwp-common-edit-widgets-select-layout-button').hide(); | |
| 1057 | + jQuery('#mainwp-common-edit-widgets-layout-delete-button').hide(); | |
| 1058 | + | |
| 1059 | + if (jQuery('#mainwp-widgets-selected-layout').length > 0) { | |
| 1060 | + let name = jQuery('#mainwp-widgets-selected-layout').attr('layout-name'); | |
| 1061 | + let lay_idx = jQuery('#mainwp-widgets-selected-layout').attr('layout-idx'); | |
| 1062 | + jQuery('#mainwp-common-edit-widgets-layout-name').val(name); | |
| 1063 | + jQuery(showBtn).attr('selected-layout-id', lay_idx); | |
| 1064 | + } | |
| 1065 | + this.hideWorkingStatus(); | |
| 1066 | + mainwp_common_show_edit_widgets_layout_modal(); | |
| 1067 | + }, | |
| 1068 | + loadSegment: function (loadCallback) { | |
| 1069 | + jQuery('#mainwp-common-edit-widgets-layout-edit-fields').hide(); | |
| 1070 | + jQuery('#mainwp-common-edit-widgets-layout-save-button').hide(); | |
| 1071 | + jQuery('#mainwp-common-edit-widgets-layout-modal > div.header').html(MainWP.I18n.t('Load Layout')); | |
| 1072 | + jQuery('#mainwp-common-layout-widgets-select-fields').show(); | |
| 1073 | + jQuery('#mainwp-common-edit-widgets-select-layout-button').show(); | |
| 1074 | + jQuery('#mainwp-common-edit-widgets-layout-delete-button').show(); | |
| 1075 | + this.hideWorkingStatus(); | |
| 1076 | + mainwp_common_show_edit_widgets_layout_modal(loadCallback); | |
| 1077 | + }, | |
| 1078 | + showResults: function (result) { | |
| 1079 | + jQuery(statusElemId).hide(); | |
| 1080 | + jQuery('#mainwp-common-edit-widgets-layout-lists-wrapper').html(result); | |
| 1081 | + jQuery('#mainwp-common-edit-widgets-layout-lists-wrapper .ui.dropdown').dropdown(); | |
| 1082 | + jQuery('#mainwp-common-layout-widgets-select-fields').show(); | |
| 1083 | + } | |
| 1084 | + } | |
| 1085 | + return _instance; | |
| 1086 | + })(); | |
| 1087 | +} | |
| 1088 | + | |
| 1089 | +/** | |
| 1090 | + * Show the widget layout modal. | |
| 1091 | + * | |
| 1092 | + * Opens the modal dialog for managing widget layouts with optional callback. | |
| 1093 | + * | |
| 1094 | + * @param {Function} loadCallback - Optional callback function to execute when modal shows. | |
| 1095 | + */ | |
| 1096 | +let mainwp_common_show_edit_widgets_layout_modal = function (loadCallback) { | |
| 1097 | + jQuery('#mainwp-common-edit-widgets-layout-modal').modal({ | |
| 1098 | + allowMultiple: false, | |
| 1099 | + onShow: function () { | |
| 1100 | + if (typeof loadCallback == 'function') { | |
| 1101 | + loadCallback(); | |
| 1102 | + } | |
| 1103 | + } | |
| 1104 | + }).modal('show'); | |
| 1105 | +}; | |
| 1106 | + | |
| 1107 | +/** | |
| 1108 | + * Save widget layout configuration. | |
| 1109 | + * | |
| 1110 | + * Collects grid stack item positions and sizes from GridStack engine, then saves the layout via AJAX. | |
| 1111 | + * | |
| 1112 | + * @param {string} itemClass - CSS selector for grid items (e.g., '.grid-stack-item'). | |
| 1113 | + * @param {Object} data - Data object containing layout metadata (name, seg_id, settings_slug). | |
| 1114 | + * @param {Function} callBack - Callback function to execute after save completes. | |
| 1115 | + */ | |
| 1116 | +let mainwp_common_ui_widgets_save_layout = function (itemClass, data, callBack) { | |
| 1117 | + | |
| 1118 | + let orders = []; | |
| 1119 | + let wgIds = []; | |
| 1120 | + | |
| 1121 | + const gridElement = document.querySelector('.grid-stack'); | |
| 1122 | + | |
| 1123 | + if (!gridElement) { | |
| 1124 | + return; | |
| 1125 | + } | |
| 1126 | + | |
| 1127 | + const grid = gridElement.gridstack; | |
| 1128 | + | |
| 1129 | + if (!grid?.engine?.nodes) { | |
| 1130 | + return; | |
| 1131 | + } | |
| 1132 | + | |
| 1133 | + grid.engine.nodes.forEach(function (node) { | |
| 1134 | + if (!node?.el?.id) { | |
| 1135 | + return; | |
| 1136 | + } | |
| 1137 | + | |
| 1138 | + let obj = { | |
| 1139 | + "x": node.x ?? 0, | |
| 1140 | + "y": node.y ?? 0, | |
| 1141 | + "w": node.w ?? 4, | |
| 1142 | + "h": node.h ?? 4 | |
| 1143 | + }; | |
| 1144 | + orders.push(obj); | |
| 1145 | + wgIds.push(node.el.id); | |
| 1146 | + }); | |
| 1147 | + | |
| 1148 | + if (wgIds.length === 0) { | |
| 1149 | + return; | |
| 1150 | + } | |
| 1151 | + | |
| 1152 | + data.action = 'mainwp_ui_save_widgets_layout'; | |
| 1153 | + data.order = orders; | |
| 1154 | + data.wgids = wgIds; | |
| 1155 | + | |
| 1156 | + jQuery.post(ajaxurl, mainwp_secure_data(data), function (res) { | |
| 1157 | + if (typeof callBack === 'function') { | |
| 1158 | + callBack(res); | |
| 1159 | + } | |
| 1160 | + }, 'json'); | |
| 1161 | +} | |
| 1162 | + | |
| 1163 | +/** | |
| 1164 | + * Initialize widget layout management handlers. | |
| 1165 | + * | |
| 1166 | + * Sets up event handlers for saving, loading, and deleting widget layout configurations. | |
| 1167 | + * Manages button states based on user input and selections. | |
| 1168 | + */ | |
| 1169 | +function mainwp_init_widget_layout_handlers() { | |
| 1170 | + let mainwp_load_manage_widgets_layout = function () { | |
| 1171 | + jQuery('#mainwp-common-layout-widgets-select-fields').hide(); | |
| 1172 | + let data = mainwp_secure_data({ | |
| 1173 | + action: 'mainwp_ui_load_widgets_layout', | |
| 1174 | + settings_slug: jQuery('#mainwp-manage-widgets-load-saved-layout-button').attr('settings-slug') | |
| 1175 | + }); | |
| 1176 | + mainwpUIHandleWidgetsLayout.showWorkingStatus('<i class="notched circle loading icon"></i> ' + MainWP.I18n.t('Loading layouts. Please wait...'), ''); | |
| 1177 | + jQuery.post(ajaxurl, data, function (response) { | |
| 1178 | + if (response.error != undefined) { | |
| 1179 | + mainwpUIHandleWidgetsLayout.showWorkingStatus(response.error, 'red'); | |
| 1180 | + } else if (response.result) { | |
| 1181 | + mainwpUIHandleWidgetsLayout.showResults(response.result); | |
| 1182 | + jQuery('#mainwp-common-edit-widgets-select-layout-button').addClass('disabled'); | |
| 1183 | + jQuery('#mainwp-common-edit-widgets-layout-delete-button').addClass('disabled'); | |
| 1184 | + } else { | |
| 1185 | + mainwpUIHandleWidgetsLayout.showWorkingStatus(MainWP.I18n.t('No saved layouts found.'), 'yellow'); | |
| 1186 | + } | |
| 1187 | + }, 'json'); | |
| 1188 | + }; | |
| 1189 | + | |
| 1190 | + jQuery('#mainwp-manage-widgets-load-saved-layout-button').on('click', function () { | |
| 1191 | + jQuery('#mainwp-common-edit-widgets-layout-save-button').addClass('disabled'); | |
| 1192 | + jQuery('#mainwp-common-edit-widgets-layout-name').val(''); | |
| 1193 | + mainwpUIHandleWidgetsLayout.showLayout(this); | |
| 1194 | + }); | |
| 1195 | + | |
| 1196 | + jQuery('#mainwp-manage-widgets-ui-choose-layout').on('click', function () { | |
| 1197 | + mainwpUIHandleWidgetsLayout.loadSegment(mainwp_load_manage_widgets_layout); | |
| 1198 | + }); | |
| 1199 | + | |
| 1200 | + jQuery(document).on('input', '#mainwp-common-edit-widgets-layout-name', function () { | |
| 1201 | + if (jQuery(this).val().trim().length > 0) { | |
| 1202 | + jQuery('#mainwp-common-edit-widgets-layout-save-button').removeClass('disabled'); | |
| 1203 | + } else { | |
| 1204 | + jQuery('#mainwp-common-edit-widgets-layout-save-button').addClass('disabled'); | |
| 1205 | + } | |
| 1206 | + }); | |
| 1207 | + | |
| 1208 | + jQuery(document).on('change', '#mainwp-common-layout-widgets-select-fields .ui.dropdown', function () { | |
| 1209 | + let selected_value = jQuery(this).dropdown('get value'); | |
| 1210 | + if (selected_value && selected_value !== '') { | |
| 1211 | + jQuery('#mainwp-common-edit-widgets-select-layout-button').removeClass('disabled'); | |
| 1212 | + jQuery('#mainwp-common-edit-widgets-layout-delete-button').removeClass('disabled'); | |
| 1213 | + } else { | |
| 1214 | + jQuery('#mainwp-common-edit-widgets-select-layout-button').addClass('disabled'); | |
| 1215 | + jQuery('#mainwp-common-edit-widgets-layout-delete-button').addClass('disabled'); | |
| 1216 | + } | |
| 1217 | + }); | |
| 1218 | + | |
| 1219 | + jQuery('#mainwp-common-edit-widgets-layout-save-button').on('click', function (e) { | |
| 1220 | + if (jQuery(this).hasClass('disabled')) { | |
| 1221 | + return false; | |
| 1222 | + } | |
| 1223 | + | |
| 1224 | + mainwpUIHandleWidgetsLayout.hideWorkingStatus(); | |
| 1225 | + | |
| 1226 | + let seg_name = jQuery('#mainwp-common-edit-widgets-layout-name').val().trim(); | |
| 1227 | + | |
| 1228 | + if ('' == seg_name) { | |
| 1229 | + mainwpUIHandleWidgetsLayout.showWorkingStatus(MainWP.I18n.t('Please enter a layout name.'), 'yellow'); | |
| 1230 | + return false; | |
| 1231 | + } | |
| 1232 | + | |
| 1233 | + let data = { | |
| 1234 | + name: seg_name, | |
| 1235 | + seg_id: jQuery('#mainwp-manage-widgets-load-saved-layout-button').attr('selected-layout-id'), | |
| 1236 | + settings_slug: jQuery('#mainwp-manage-widgets-load-saved-layout-button').attr('settings-slug') | |
| 1237 | + }; | |
| 1238 | + | |
| 1239 | + mainwpUIHandleWidgetsLayout.showWorkingStatus('<i class="notched circle loading icon"></i> ' + MainWP.I18n.t('Saving layout. Please wait...'), ''); | |
| 1240 | + | |
| 1241 | + mainwp_common_ui_widgets_save_layout('.grid-stack-item', data, function (response) { | |
| 1242 | + if (response.error != undefined) { | |
| 1243 | + mainwpUIHandleWidgetsLayout.showWorkingStatus(response.error, 'red'); | |
| 1244 | + } else if (response.result == 'SUCCESS') { | |
| 1245 | + mainwpUIHandleWidgetsLayout.showWorkingStatus(MainWP.I18n.t('Layout saved successfully.'), 'green'); | |
| 1246 | + setTimeout(function () { | |
| 1247 | + jQuery('#mainwp-common-edit-widgets-layout-status').fadeOut(300); | |
| 1248 | + jQuery('#mainwp-common-edit-widgets-layout-modal').modal('hide'); | |
| 1249 | + mainwp_forceReload(); | |
| 1250 | + }, 2000); | |
| 1251 | + } else { | |
| 1252 | + mainwpUIHandleWidgetsLayout.showWorkingStatus(MainWP.I18n.t('An error occurred while saving the layout.'), 'red'); | |
| 1253 | + } | |
| 1254 | + }); | |
| 1255 | + e.preventDefault(); | |
| 1256 | + e.stopPropagation(); | |
| 1257 | + return true; | |
| 1258 | + }); | |
| 1259 | + | |
| 1260 | + jQuery('#mainwp-common-edit-widgets-select-layout-button').on('click', function () { | |
| 1261 | + if (jQuery(this).hasClass('disabled')) { | |
| 1262 | + return false; | |
| 1263 | + } | |
| 1264 | + | |
| 1265 | + mainwpUIHandleWidgetsLayout.hideWorkingStatus(); | |
| 1266 | + let seg_id = jQuery('#mainwp-common-layout-widgets-select-fields .ui.dropdown').dropdown('get value'); | |
| 1267 | + let screen_slug = jQuery('#mainwp-manage-widgets-load-saved-layout-button').attr('settings-slug'); | |
| 1268 | + let loc_url = removeUrlParams(location.href, ['screen_slug', 'updated', '_opennonce']); | |
| 1269 | + if ('' == loc_url) { | |
| 1270 | + loc_url = location.href; | |
| 1271 | + } | |
| 1272 | + globalThis.location.href = loc_url + '&select_layout=1&screen_slug=' + encodeURIComponent(screen_slug) + '&updated=' + encodeURIComponent(seg_id) + '&_opennonce=' + mainwpWidgetLayout.openNonce; | |
| 1273 | + }); | |
| 1274 | + | |
| 1275 | + jQuery('#mainwp-common-edit-widgets-layout-delete-button').on('click', function (e) { | |
| 1276 | + | |
| 1277 | + e.preventDefault(); | |
| 1278 | + e.stopPropagation(); | |
| 1279 | + | |
| 1280 | + const $button = jQuery(this); | |
| 1281 | + | |
| 1282 | + if ($button.hasClass('disabled')) { | |
| 1283 | + return; | |
| 1284 | + } | |
| 1285 | + | |
| 1286 | + mainwpUIHandleWidgetsLayout.hideWorkingStatus(); | |
| 1287 | + | |
| 1288 | + let seg_id = jQuery('#mainwp-common-layout-widgets-select-fields .ui.dropdown').dropdown('get value'); | |
| 1289 | + if (!seg_id) { | |
| 1290 | + return; | |
| 1291 | + } | |
| 1292 | + | |
| 1293 | + if ('yes' === $button.attr('running')) { | |
| 1294 | + return; | |
| 1295 | + } | |
| 1296 | + | |
| 1297 | + $button.attr('running', 'yes'); | |
| 1298 | + | |
| 1299 | + let data = mainwp_secure_data({ | |
| 1300 | + action: 'mainwp_ui_delete_widgets_layout', | |
| 1301 | + seg_id: seg_id, | |
| 1302 | + settings_slug: jQuery('#mainwp-manage-widgets-load-saved-layout-button').attr('settings-slug') | |
| 1303 | + }); | |
| 1304 | + mainwpUIHandleWidgetsLayout.showWorkingStatus('<i class="notched circle loading icon"></i> ' + MainWP.I18n.t('Deleting layout. Please wait...'), ''); | |
| 1305 | + jQuery.post(ajaxurl, data, (response) => { | |
| 1306 | + | |
| 1307 | + $button.removeAttr('running'); | |
| 1308 | + | |
| 1309 | + if (response.error != undefined) { | |
| 1310 | + mainwpUIHandleWidgetsLayout.showWorkingStatus(response.error, 'red'); | |
| 1311 | + } else if (response.result == 'SUCCESS') { | |
| 1312 | + mainwpUIHandleWidgetsLayout.showWorkingStatus(MainWP.I18n.t('Layout deleted successfully.'), 'green'); | |
| 1313 | + setTimeout(function () { | |
| 1314 | + jQuery('#mainwp-common-edit-widgets-layout-status').fadeOut(300); | |
| 1315 | + jQuery('#mainwp-common-edit-widgets-layout-modal').modal('hide'); | |
| 1316 | + }, 2000); | |
| 1317 | + } else { | |
| 1318 | + mainwpUIHandleWidgetsLayout.showWorkingStatus(MainWP.I18n.t('An error occurred while deleting the layout.'), 'red'); | |
| 1319 | + } | |
| 1320 | + }, 'json'); | |
| 1321 | + }); | |
| 1322 | +} | |