| @@ -1,7 +1,12 @@ | ||
| 1 | +/* eslint-disable complexity */ | |
| 1 | 2 | |
| 2 | -/* eslint complexity: ["error", 100] */ | |
| 3 | +globalThis.wp = globalThis.wp || {}; | |
| 4 | +globalThis.mainwpVars = globalThis.mainwpVars || {}; | |
| 3 | 5 | |
| 6 | +let executingUpdateCategories = false; | |
| 7 | +let queueUpdateCategories = 0; | |
| 8 | + | |
| 4 | 9 | function reload_init() { |
| 5 | 10 | read_current_url(); |
| 6 | 11 | if (typeof wpOnload == 'function') |
| 7 | 12 | wpOnload(); |
| @@ -15,24 +20,26 @@ | ||
| 15 | 20 | stick_element_init(); |
| 16 | 21 | } |
| 17 | 22 | |
| 18 | 23 | /** AJAX page load **/ |
| 19 | -var mainwp_current_url = ''; | |
| 24 | +let mainwp_current_url = ''; | |
| 20 | 25 | function read_current_url() { |
| 21 | 26 | mainwp_current_url = document.location.href.replace(/^.*?\/([^/]*?)\/?$/i, '$1'); |
| 22 | 27 | return mainwp_current_url; |
| 23 | 28 | } |
| 24 | -function load_url(href, obj, e) { | |
| 25 | - var page = href.match(/page=/i) ? href.replace(/^.*?page=([^&]+).*?$/i, '$1') : ''; | |
| 29 | +function load_url(href, obj, e) { // NOSONAR - complex. | |
| 30 | + let page = href.match(/page=/i) ? href.replace(/^.*\?page=([^&]+).*?$/i, '$1') : ''; // NOSONAR - safe with run time error. | |
| 26 | 31 | if (page || href == 'index.php') { |
| 27 | 32 | if (!jQuery('body').hasClass('mainwp-ui-page')) { |
| 28 | 33 | return; |
| 29 | 34 | } |
| 30 | - if (typeof e !== 'undefined') | |
| 35 | + | |
| 36 | + if (e !== undefined) | |
| 31 | 37 | e.preventDefault(); |
| 32 | - 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>'); | |
| 33 | 40 | if (jQuery(obj).hasClass('menu-top')) { |
| 34 | - var top = jQuery(obj).closest('li.menu-top'); | |
| 41 | + let top = jQuery(obj).closest('li.menu-top'); | |
| 35 | 42 | jQuery('#adminmenu .current').removeClass('current').addClass('wp-not-current-submenu'); |
| 36 | 43 | jQuery('.wp-has-current-submenu').removeClass('wp-has-current-submenu').addClass('wp-not-current-submenu'); |
| 37 | 44 | if (top.hasClass('wp-has-submenu')) { |
| 38 | 45 | top.removeClass('wp-not-current-submenu').addClass('wp-has-current-submenu'); |
| @@ -44,9 +51,9 @@ | ||
| 44 | 51 | top.find('li.wp-first-item').addClass('current'); |
| 45 | 52 | } else { |
| 46 | 53 | jQuery('#adminmenu .current').removeClass('current'); |
| 47 | 54 | jQuery(obj).closest('li').addClass('current'); |
| 48 | - var top = jQuery(obj).closest('li.menu-top'); | |
| 55 | + let top = jQuery(obj).closest('li.menu-top'); | |
| 49 | 56 | if (top.hasClass('wp-not-current-submenu')) { |
| 50 | 57 | jQuery('.wp-has-current-submenu').removeClass('wp-has-current-submenu').addClass('wp-not-current-submenu'); |
| 51 | 58 | top.removeClass('wp-not-current-submenu').addClass('wp-has-current-submenu'); |
| 52 | 59 | } |
| @@ -64,9 +71,9 @@ | ||
| 64 | 71 | action: 'mainwp_load', |
| 65 | 72 | page: page, |
| 66 | 73 | nonce: mainwp_ajax_nonce |
| 67 | 74 | }, function (data) { |
| 68 | - pagenow = page; | |
| 75 | + pagenow = page; // NOSONAR - wp variable. | |
| 69 | 76 | data += '<div class="clear"></div>'; |
| 70 | 77 | jQuery('#wpbody-content').html(data); |
| 71 | 78 | reload_init(); |
| 72 | 79 | }); |
| @@ -92,9 +99,9 @@ | ||
| 92 | 99 | |
| 93 | 100 | } |
| 94 | 101 | } |
| 95 | 102 | |
| 96 | -window.onpopstate = function (e) { | |
| 103 | +globalThis.onpopstate = function (e) { | |
| 97 | 104 | read_current_url(); |
| 98 | 105 | if (e.state) |
| 99 | 106 | load_url(mainwp_current_url, e.state.anchor); |
| 100 | 107 | } |
| @@ -102,12 +109,12 @@ | ||
| 102 | 109 | |
| 103 | 110 | |
| 104 | 111 | |
| 105 | 112 | function scroll_element() { |
| 106 | - var top = jQuery(this).scrollTop(); | |
| 107 | - var start = 20; | |
| 113 | + let top = jQuery(this).scrollTop(); | |
| 114 | + let start = 20; | |
| 108 | 115 | jQuery('.stick-to-window').each(function () { |
| 109 | - var init = jQuery(this).data('init-position'); | |
| 116 | + let init = jQuery(this).data('init-position'); | |
| 110 | 117 | if (top > init.top) |
| 111 | 118 | jQuery(this).stop().animate({ top: (top - init.top) + init.top + start }, 1000); |
| 112 | 119 | else |
| 113 | 120 | jQuery(this).stop().css({ top: init.top }); |
| @@ -114,9 +121,9 @@ | ||
| 114 | 121 | }); |
| 115 | 122 | } |
| 116 | 123 | function stick_element_init() { |
| 117 | 124 | jQuery('.stick-to-window').each(function () { |
| 118 | - var pos = jQuery(this).position(); | |
| 125 | + let pos = jQuery(this).position(); | |
| 119 | 126 | jQuery(this).css({ |
| 120 | 127 | position: 'absolute', |
| 121 | 128 | top: pos.top, |
| 122 | 129 | left: pos.left |
| @@ -134,34 +141,39 @@ | ||
| 134 | 141 | }); |
| 135 | 142 | stick_element_init(); |
| 136 | 143 | scroll_element(); |
| 137 | 144 | } |
| 138 | -jQuery(document).ready(function () { | |
| 139 | - jQuery(window).trigger('scroll', scroll_element).trigger('resize', stick_element_reset); | |
| 145 | +jQuery(function () { | |
| 146 | + jQuery(globalThis).trigger('scroll', scroll_element).trigger('resize', stick_element_reset); | |
| 140 | 147 | stick_element_init(); |
| 141 | 148 | }); |
| 142 | 149 | |
| 143 | -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 | |
| 152 | + let confVal; | |
| 144 | 153 | if (jQuery('#mainwp-disable-update-confirmations').length > 0) { |
| 145 | - var confVal = jQuery('#mainwp-disable-update-confirmations').val(); | |
| 146 | - if (typeof updateType !== 'undefined' && updateType !== false && (confVal == 2 || (confVal == 1 && updateType == 1))) { | |
| 147 | - if (confirmed_callback && typeof confirmed_callback == 'function') | |
| 148 | - confirmed_callback(); | |
| 149 | - return false; | |
| 150 | - } | |
| 154 | + confVal = jQuery('#mainwp-disable-update-confirmations').val(); | |
| 151 | 155 | } |
| 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') { | |
| 157 | + confirmed_callback(); | |
| 158 | + return; | |
| 159 | + } | |
| 152 | 160 | |
| 161 | + | |
| 153 | 162 | jQuery('#mainwp-modal-confirm .content-massage').html(msg); |
| 154 | 163 | |
| 155 | - 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) { | |
| 156 | 168 | jQuery('#mainwp-confirm-form').show(); |
| 157 | 169 | jQuery('#mainwp-confirm-form').find('label').html('Type ' + extra + ' to confirm'); |
| 158 | 170 | } |
| 159 | 171 | |
| 160 | - var opts = { | |
| 172 | + let opts = { | |
| 161 | 173 | onApprove: function () { |
| 162 | - if (typeof extra !== 'undefined' && extra !== false) { | |
| 163 | - var extraValue = jQuery('#mainwp-confirm-input').val(); | |
| 174 | + if (extra !== undefined && extra !== false) { | |
| 175 | + let extraValue = jQuery('#mainwp-confirm-input').val(); | |
| 164 | 176 | if (confirmed_callback && typeof confirmed_callback == 'function') { |
| 165 | 177 | if (extraValue === extra) { |
| 166 | 178 | confirmed_callback(); |
| 167 | 179 | } else { |
| @@ -168,11 +180,10 @@ | ||
| 168 | 180 | jQuery('#mainwp-confirm-input').val('').trigger('focus').transition('shake'); |
| 169 | 181 | return false; |
| 170 | 182 | } |
| 171 | 183 | } |
| 172 | - } else { | |
| 173 | - if (confirmed_callback && typeof confirmed_callback == 'function') | |
| 174 | - confirmed_callback(); | |
| 184 | + } else if (confirmed_callback && typeof confirmed_callback == 'function') { | |
| 185 | + confirmed_callback(); | |
| 175 | 186 | } |
| 176 | 187 | }, |
| 177 | 188 | onDeny() { |
| 178 | 189 | if (cancelled_callback && typeof cancelled_callback == 'function') |
| @@ -186,27 +197,42 @@ | ||
| 186 | 197 | |
| 187 | 198 | jQuery('#mainwp-modal-confirm').modal(opts).modal('show'); |
| 188 | 199 | |
| 189 | 200 | // if it is update confirm and then display the update confirm notice text |
| 190 | - if (typeof updateType !== 'undefined' && updateType !== false && (updateType == 1 || updateType == 2)) { | |
| 201 | + if (updateType !== undefined && updateType !== false && (updateType == 1 || updateType == 2)) { | |
| 191 | 202 | jQuery('#mainwp-modal-confirm .update-confirm-notice ').show(); |
| 192 | 203 | } |
| 193 | - | |
| 194 | - return false; | |
| 195 | 204 | } |
| 196 | 205 | |
| 197 | - | |
| 198 | 206 | /** |
| 199 | 207 | * Select sites |
| 200 | 208 | */ |
| 201 | -jQuery(document).ready(function () { | |
| 202 | - jQuery('.mainwp_selected_sites_item input:checkbox').on('change', function () { | |
| 203 | - if (jQuery(this).is(':checked')) | |
| 204 | - jQuery(this).parent().addClass('selected_sites_item_checked'); | |
| 205 | - else | |
| 206 | - jQuery(this).parent().removeClass('selected_sites_item_checked'); | |
| 207 | - mainwp_site_select(); | |
| 208 | - }); | |
| 209 | +jQuery(document).on('change', '.mainwp_selected_sites_item input:checkbox', function () { | |
| 210 | + if (jQuery(this).is(':checked')) | |
| 211 | + jQuery(this).parent().addClass('selected_sites_item_checked'); | |
| 212 | + else | |
| 213 | + jQuery(this).parent().removeClass('selected_sites_item_checked'); | |
| 214 | + mainwp_site_select(); | |
| 215 | +}); | |
| 216 | + | |
| 217 | +jQuery(document).on('change', '.mainwp_selected_groups_item input:checkbox', function () { | |
| 218 | + if (jQuery(this).is(':checked')) | |
| 219 | + jQuery(this).parent().addClass('selected_groups_item_checked'); | |
| 220 | + else | |
| 221 | + jQuery(this).parent().removeClass('selected_groups_item_checked'); | |
| 222 | + mainwp_group_select(); | |
| 223 | +}); | |
| 224 | + | |
| 225 | +jQuery(document).on('change', '.mainwp_selected_clients_item input:checkbox', function () { | |
| 226 | + if (jQuery(this).is(':checked')) | |
| 227 | + jQuery(this).parent().addClass('selected_clients_item_checked'); | |
| 228 | + else | |
| 229 | + jQuery(this).parent().removeClass('selected_clients_item_checked'); | |
| 230 | + mainwp_client_select(); | |
| 231 | +}); | |
| 232 | + | |
| 233 | + | |
| 234 | +jQuery(function () { | |
| 209 | 235 | // seems not used. |
| 210 | 236 | jQuery('.mainwp_selected_sites_item input:radio').on('change', function () { |
| 211 | 237 | if (jQuery(this).is(':checked')) { |
| 212 | 238 | jQuery(this).parent().addClass('selected_sites_item_checked'); |
| @@ -214,17 +240,8 @@ | ||
| 214 | 240 | } else |
| 215 | 241 | jQuery(this).parent().removeClass('selected_sites_item_checked'); |
| 216 | 242 | mainwp_site_select(); |
| 217 | 243 | }); |
| 218 | - | |
| 219 | - jQuery('.mainwp_selected_groups_item input:checkbox').on('change', function () { | |
| 220 | - if (jQuery(this).is(':checked')) | |
| 221 | - jQuery(this).parent().addClass('selected_groups_item_checked'); | |
| 222 | - else | |
| 223 | - jQuery(this).parent().removeClass('selected_groups_item_checked'); | |
| 224 | - mainwp_group_select(); | |
| 225 | - }); | |
| 226 | - | |
| 227 | 244 | // seems not used. |
| 228 | 245 | jQuery('.mainwp_selected_groups_item input:radio').on('change', function () { |
| 229 | 246 | if (jQuery(this).is(':checked')) { |
| 230 | 247 | jQuery(this).parent().addClass('selected_groups_item_checked'); |
| @@ -231,35 +248,74 @@ | ||
| 231 | 248 | jQuery(this).parent().parent().find('.mainwp_selected_groups_item input:radio:not(:checked)').parent().removeClass('selected_groups_item_checked'); |
| 232 | 249 | } else |
| 233 | 250 | jQuery(this).parent().removeClass('selected_groups_item_checked'); |
| 234 | 251 | }); |
| 252 | +}); | |
| 235 | 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; | |
| 236 | 258 | |
| 237 | - jQuery('.mainwp_selected_clients_item input:checkbox').on('change', function () { | |
| 238 | - if (jQuery(this).is(':checked')) | |
| 239 | - jQuery(this).parent().addClass('selected_clients_item_checked'); | |
| 240 | - else | |
| 241 | - jQuery(this).parent().removeClass('selected_clients_item_checked'); | |
| 242 | - mainwp_client_select(); | |
| 243 | - }); | |
| 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 | + } | |
| 244 | 268 | |
| 245 | -}); | |
| 269 | + let counter = parent.find('.mainwp-sites-selection-counter'); | |
| 270 | + counter.text(count); | |
| 246 | 271 | |
| 247 | -mainwp_site_select = function () { | |
| 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 | + | |
| 300 | +let mainwp_site_select = function () { | |
| 248 | 301 | mainwp_newpost_updateCategories(); |
| 302 | + mainwp_update_selection_counter(); | |
| 249 | 303 | }; |
| 250 | -mainwp_group_select = function () { | |
| 304 | +let mainwp_group_select = function () { | |
| 251 | 305 | mainwp_newpost_updateCategories(); |
| 306 | + mainwp_update_selection_counter(); | |
| 252 | 307 | }; |
| 253 | -mainwp_client_select = function () { | |
| 308 | +let mainwp_client_select = function () { | |
| 254 | 309 | mainwp_newpost_updateCategories(); |
| 310 | + mainwp_update_selection_counter(); | |
| 255 | 311 | }; |
| 256 | 312 | |
| 257 | -mainwp_ss_select = function (me, val) { | |
| 258 | - var parent = jQuery(me).closest('.mainwp_select_sites_wrapper'); | |
| 259 | - var tab = parent.find('#select_sites_tab').val(); | |
| 313 | +let mainwp_ss_select = function (me, val) { | |
| 314 | + let parent = jQuery(me).closest('.mainwp_select_sites_wrapper'); | |
| 315 | + let tab = parent.find('input[name="select_sites_tab"]').val(); | |
| 260 | 316 | if (tab == 'site') { |
| 261 | - 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 () { | |
| 262 | 318 | jQuery(this).attr('checked', val).trigger("change"); |
| 263 | 319 | if (val) { |
| 264 | 320 | jQuery(this).closest('.item.checkbox').checkbox('set checked'); |
| 265 | 321 | } |
| @@ -288,9 +344,9 @@ | ||
| 288 | 344 | } |
| 289 | 345 | }); |
| 290 | 346 | |
| 291 | 347 | } else { //group |
| 292 | - 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 () { | |
| 293 | 349 | jQuery(this).attr('checked', val).trigger("change"); |
| 294 | 350 | if (val) { |
| 295 | 351 | jQuery(this).closest('.item.checkbox').checkbox('set checked'); |
| 296 | 352 | } |
| @@ -298,25 +354,19 @@ | ||
| 298 | 354 | jQuery(this).closest('.item.checkbox').checkbox('set unchecked'); |
| 299 | 355 | } |
| 300 | 356 | }); |
| 301 | 357 | } |
| 302 | - if (val == true) { | |
| 303 | - jQuery('.mainwp-ss-select').hide(); | |
| 304 | - jQuery('.mainwp-ss-deselect').show(); | |
| 305 | - } else { | |
| 306 | - jQuery('.mainwp-ss-select').show(); | |
| 307 | - jQuery('.mainwp-ss-deselect').hide(); | |
| 308 | - } | |
| 309 | 358 | mainwp_newpost_updateCategories(); |
| 359 | + mainwp_update_selection_counter(); | |
| 310 | 360 | return false; |
| 311 | 361 | }; |
| 312 | 362 | |
| 313 | 363 | |
| 314 | -mainwp_ss_select_disconnected = function (me, val) { | |
| 315 | - var parent = jQuery(me).closest('.mainwp_select_sites_wrapper'); | |
| 316 | - var tab = parent.find('#select_sites_tab').val(); | |
| 364 | +let mainwp_ss_select_disconnected = function (me, val) { | |
| 365 | + let parent = jQuery(me).closest('.mainwp_select_sites_wrapper'); | |
| 366 | + let tab = parent.find('input[name="select_sites_tab"]').val(); | |
| 317 | 367 | if (tab == 'site') { |
| 318 | - 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 () { | |
| 319 | 369 | jQuery(this).attr('checked', val).trigger("change"); |
| 320 | 370 | if (val) { |
| 321 | 371 | jQuery(this).closest('.item.warning.checkbox').checkbox('set checked'); |
| 322 | 372 | } |
| @@ -324,74 +374,64 @@ | ||
| 324 | 374 | jQuery(this).closest('.item.warning.checkbox').checkbox('set unchecked'); |
| 325 | 375 | } |
| 326 | 376 | }); |
| 327 | 377 | } |
| 328 | - if (val == true) { | |
| 329 | - jQuery('.mainwp-ss-select-disconnected').hide(); | |
| 330 | - jQuery('.mainwp-ss-deselect-disconnected').show(); | |
| 331 | - } else { | |
| 332 | - jQuery('.mainwp-ss-select-disconnected').show(); | |
| 333 | - jQuery('.mainwp-ss-deselect-disconnected').hide(); | |
| 334 | - } | |
| 335 | 378 | mainwp_newpost_updateCategories(); |
| 379 | + mainwp_update_selection_counter(); | |
| 336 | 380 | return false; |
| 337 | 381 | }; |
| 338 | 382 | |
| 339 | -mainwp_sites_selection_onvisible_callback = function (me) { | |
| 340 | - var selected_tab = jQuery(me).attr('select-by'); | |
| 341 | - var select_by = 'site'; | |
| 342 | - var parent = jQuery(me).closest('.mainwp_select_sites_wrapper'); | |
| 383 | +globalThis.mainwp_sites_selection_onvisible_callback = function (me) { | |
| 384 | + let selected_tab = jQuery(me).attr('select-by'); | |
| 385 | + let select_by = 'site'; | |
| 386 | + let parent = jQuery(me).closest('.mainwp_select_sites_wrapper'); | |
| 343 | 387 | if (selected_tab == 'staging') { |
| 344 | 388 | // uncheck live sites |
| 345 | - parent.find('#mainwp-select-sites-list INPUT:checkbox').prop('checked', false); | |
| 346 | - 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); | |
| 347 | 391 | parent.find('#mainwp-select-clients-list INPUT:checkbox').prop('checked', false); |
| 348 | 392 | } else if (selected_tab == 'site') { |
| 349 | 393 | // uncheck staging sites |
| 350 | 394 | parent.find('#mainwp-select-staging-sites-list INPUT:checkbox').prop('checked', false); |
| 351 | - parent.find('#mainwp-select-groups-list INPUT:checkbox').prop('checked', false); | |
| 395 | + parent.find('.mainwp-select-groups-list INPUT:checkbox').prop('checked', false); | |
| 352 | 396 | parent.find('#mainwp-select-clients-list INPUT:checkbox').prop('checked', false); |
| 353 | 397 | } else if (selected_tab == 'group') { |
| 354 | 398 | // uncheck sites |
| 355 | - parent.find('#mainwp-select-sites-list INPUT:checkbox').prop('checked', false); | |
| 399 | + parent.find('.mainwp-select-sites-list INPUT:checkbox').prop('checked', false); | |
| 356 | 400 | parent.find('#mainwp-select-staging-sites-list INPUT:checkbox').prop('checked', false); |
| 357 | 401 | parent.find('#mainwp-select-clients-list INPUT:checkbox').prop('checked', false); |
| 358 | 402 | select_by = 'group'; |
| 359 | 403 | } else if (selected_tab == 'client') { |
| 360 | 404 | // uncheck sites |
| 361 | - parent.find('#mainwp-select-sites-list INPUT:checkbox').prop('checked', false); | |
| 362 | - 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); | |
| 363 | 407 | parent.find('#mainwp-select-staging-sites-list INPUT:checkbox').prop('checked', false); |
| 364 | 408 | select_by = 'client'; |
| 365 | 409 | } |
| 366 | 410 | |
| 367 | - jQuery('.mainwp-ss-select').show(); | |
| 368 | - jQuery('.mainwp-ss-deselect').hide(); | |
| 411 | + console.log('select by: ' + select_by); | |
| 369 | 412 | |
| 370 | - 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); | |
| 371 | 415 | |
| 372 | - parent.find('#select_by').val(select_by); | |
| 373 | - parent.find('#select_sites_tab').val(selected_tab); | |
| 416 | + mainwp_update_selection_counter(); | |
| 374 | 417 | } |
| 375 | 418 | |
| 376 | 419 | |
| 377 | -var executingUpdateCategories = false; | |
| 378 | -var queueUpdateCategories = 0; | |
| 379 | -mainwp_newpost_updateCategories = function () { | |
| 420 | +let mainwp_newpost_updateCategories = function () { | |
| 380 | 421 | if (executingUpdateCategories) { |
| 381 | 422 | queueUpdateCategories++; |
| 382 | 423 | return; |
| 383 | 424 | } |
| 384 | 425 | executingUpdateCategories = true; |
| 385 | - console.log('mainwp_newpost_updateCategories'); | |
| 386 | - var catsSelection = jQuery('#categorychecklist'); | |
| 426 | + let catsSelection = jQuery('#categorychecklist'); | |
| 387 | 427 | if (catsSelection.length > 0) { |
| 388 | - var tab = jQuery('#select_sites_tab').val(); | |
| 389 | - var sites = []; | |
| 390 | - var groups = []; | |
| 391 | - var clients = []; | |
| 428 | + let tab = jQuery('input[name="select_sites_tab"]').val(); | |
| 429 | + let sites = []; | |
| 430 | + let groups = []; | |
| 431 | + let clients = []; | |
| 392 | 432 | if (tab == 'site') { |
| 393 | - 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) { | |
| 394 | 434 | return jQuery(el).val(); |
| 395 | 435 | }); |
| 396 | 436 | } else if (tab == 'staging') { |
| 397 | 437 | sites = jQuery.map(jQuery('#mainwp-select-staging-sites-list INPUT:checkbox:checked'), function (el) { |
| @@ -401,39 +441,53 @@ | ||
| 401 | 441 | clients = jQuery.map(jQuery('#mainwp-select-clients-list INPUT:checkbox:checked'), function (el) { |
| 402 | 442 | return jQuery(el).val(); |
| 403 | 443 | }); |
| 404 | 444 | } else { //group |
| 405 | - 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) { | |
| 406 | 446 | return jQuery(el).val(); |
| 407 | 447 | }); |
| 408 | 448 | } |
| 409 | 449 | |
| 410 | - var selected_categories = catsSelection.dropdown('get value'); | |
| 450 | + let selected_categories = catsSelection.dropdown('get value'); | |
| 411 | 451 | |
| 412 | - var data = mainwp_secure_data({ | |
| 452 | + | |
| 453 | + let data = mainwp_secure_data({ | |
| 413 | 454 | action: 'mainwp_get_categories', |
| 414 | 455 | sites: encodeURIComponent(sites.join(',')), |
| 415 | 456 | groups: encodeURIComponent(groups.join(',')), |
| 416 | 457 | clients: encodeURIComponent(clients.join(',')), |
| 417 | - selected_categories: selected_categories ? encodeURIComponent(selected_categories.join(',')) : '', | |
| 418 | - post_id: jQuery('#post_ID').val() | |
| 458 | + selected_categories: selected_categories ? encodeURIComponent(selected_categories) : '', | |
| 459 | + post_id: jQuery('#post_ID').val(), | |
| 460 | + custom_post_type: jQuery('#mainwp_custom_post_type_edit_post').length ? jQuery('#mainwp_custom_post_type_edit_post').val() : '' // support CPT ext. | |
| 419 | 461 | }); |
| 420 | 462 | |
| 421 | 463 | jQuery.post(ajaxurl, data, function (pSelectedCategories) { |
| 422 | 464 | return function (response) { |
| 423 | - response = response.trim(); | |
| 424 | - catsSelection.dropdown('remove selected'); | |
| 425 | - catsSelection.find('.sitecategory').remove(); | |
| 426 | - catsSelection.append(response); | |
| 427 | - catsSelection.dropdown('set selected', pSelectedCategories); | |
| 428 | - updateCategoriesPostFunc(); | |
| 465 | + if (response?.content) { | |
| 466 | + catsSelection.dropdown('remove selected'); | |
| 467 | + catsSelection.find('.menu').html(response.content); | |
| 468 | + catsSelection.dropdown('refresh'); | |
| 469 | + let arrVal = pSelectedCategories.split(","); | |
| 470 | + catsSelection.dropdown('set selected', arrVal); | |
| 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 | + | |
| 482 | + } | |
| 429 | 483 | }; |
| 430 | - }(selected_categories)); | |
| 484 | + }(selected_categories), 'json'); | |
| 431 | 485 | } else { |
| 432 | 486 | updateCategoriesPostFunc(); |
| 433 | 487 | } |
| 434 | 488 | }; |
| 435 | -updateCategoriesPostFunc = function () { | |
| 489 | +let updateCategoriesPostFunc = function () { | |
| 436 | 490 | if (queueUpdateCategories > 0) { |
| 437 | 491 | queueUpdateCategories--; |
| 438 | 492 | executingUpdateCategories = false; |
| 439 | 493 | mainwp_newpost_updateCategories(); |
| @@ -446,43 +500,17 @@ | ||
| 446 | 500 | if (event.keyCode == 13 && event.srcElement.tagName.toLowerCase() == "input") { |
| 447 | 501 | event.preventDefault(); |
| 448 | 502 | } |
| 449 | 503 | }); |
| 450 | -jQuery(document).on('keyup', '#mainwp-select-sites-filter', function () { | |
| 451 | - var filter = jQuery(this).val().toLowerCase(); | |
| 452 | - var parent = jQuery(this).closest('.mainwp_select_sites_wrapper'); | |
| 453 | - var tab = jQuery('#select_sites_tab').val(); | |
| 454 | - var siteItems = []; | |
| 455 | - | |
| 456 | - if (tab == 'group') { | |
| 457 | - siteItems = parent.find('.mainwp_selected_groups_item'); | |
| 458 | - } else if (tab == 'site' || tab == 'staging') { | |
| 459 | - siteItems = parent.find('.mainwp_selected_sites_item'); | |
| 460 | - } | |
| 461 | - | |
| 462 | - for (var i = 0; i < siteItems.length; i++) { | |
| 463 | - var currentElement = jQuery(siteItems[i]); | |
| 464 | - var value = currentElement.find('label').text().toLowerCase(); | |
| 465 | - if (value.indexOf(filter) > -1) { | |
| 466 | - currentElement.removeClass('no-select').show(); | |
| 467 | - } else { | |
| 468 | - currentElement.addClass('no-select').hide(); | |
| 469 | - } | |
| 470 | - } | |
| 471 | - if (tab == 'site' || tab == 'staging') { | |
| 472 | - mainwp_newpost_updateCategories(); | |
| 473 | - } | |
| 474 | -}); | |
| 475 | - | |
| 476 | 504 | jQuery(document).on('keyup', '#mainwp-sites-menu-filter', function () { |
| 477 | - var filter = jQuery(this).val().toLowerCase(); | |
| 478 | - var parent = jQuery('#mainwp-sites-sidebar-menu'); | |
| 479 | - var siteItems = parent.find('.mainwp-site-menu-item'); | |
| 505 | + let filter = jQuery(this).val().toLowerCase(); | |
| 506 | + let parent = jQuery('#mainwp-sites-sidebar-menu'); | |
| 507 | + let siteItems = parent.find('.mainwp-site-menu-item'); | |
| 480 | 508 | |
| 481 | - for (var i = 0; i < siteItems.length; i++) { | |
| 482 | - var currentElement = jQuery(siteItems[i]); | |
| 483 | - var value = currentElement.find('label').text().toLowerCase(); | |
| 484 | - if (value.indexOf(filter) > -1) { | |
| 509 | + for (let ss of siteItems) { | |
| 510 | + let currentElement = jQuery(ss); | |
| 511 | + let value = currentElement.find('label').text().toLowerCase(); | |
| 512 | + if (value.includes(filter)) { | |
| 485 | 513 | currentElement.show(); |
| 486 | 514 | } else { |
| 487 | 515 | currentElement.hide(); |
| 488 | 516 | } |
| @@ -489,27 +517,33 @@ | ||
| 489 | 517 | } |
| 490 | 518 | }); |
| 491 | 519 | |
| 492 | 520 | // Accordion initialization on pre-existing markup |
| 493 | -jQuery(document).ready(function () { | |
| 521 | +jQuery(function () { | |
| 522 | + mainwp_sidebar_accordion_init(); | |
| 523 | +}); | |
| 524 | + | |
| 525 | +globalThis.mainwp_sidebar_accordion_init = function () { | |
| 494 | 526 | if (jQuery('.mainwp-sidebar-accordion').length > 0) { |
| 527 | + console.log('sidebar accordion init'); | |
| 495 | 528 | jQuery('.mainwp-sidebar-accordion').accordion({ |
| 496 | 529 | "onOpening": function () { |
| 497 | - var parent = jQuery(this).closest('.mainwp-sidebar-accordion'); | |
| 498 | - var ident = jQuery('.mainwp-sidebar-accordion').index(parent); | |
| 530 | + let parent = jQuery(this).closest('.mainwp-sidebar-accordion'); | |
| 531 | + let ident = jQuery('.mainwp-sidebar-accordion').index(parent); | |
| 499 | 532 | mainwp_accordion_on_collapse(ident, 1); |
| 500 | 533 | }, |
| 501 | 534 | "onClosing": function () { |
| 502 | - var parent = jQuery(this).closest('.mainwp-sidebar-accordion'); | |
| 503 | - var ident = jQuery('.mainwp-sidebar-accordion').index(parent); | |
| 535 | + let parent = jQuery(this).closest('.mainwp-sidebar-accordion'); | |
| 536 | + let ident = jQuery('.mainwp-sidebar-accordion').index(parent); | |
| 504 | 537 | mainwp_accordion_on_collapse(ident, 0); |
| 505 | 538 | } |
| 506 | 539 | }); |
| 507 | 540 | mainwp_accordion_init_collapse(); |
| 508 | 541 | } |
| 509 | -}); | |
| 542 | +} | |
| 510 | 543 | |
| 511 | -mainwp_accordion_on_collapse = function (ident, val) { | |
| 544 | + | |
| 545 | +let mainwp_accordion_on_collapse = function (ident, val) { | |
| 512 | 546 | if (typeof (Storage) !== 'undefined') { |
| 513 | 547 | if (typeof (pagenow) !== 'undefined') { |
| 514 | 548 | localStorage.setItem('mainwp-accordion[' + pagenow + '][' + ident + ']', val); |
| 515 | 549 | } |
| @@ -514,16 +548,16 @@ | ||
| 514 | 548 | localStorage.setItem('mainwp-accordion[' + pagenow + '][' + ident + ']', val); |
| 515 | 549 | } |
| 516 | 550 | } |
| 517 | 551 | }; |
| 518 | -mainwp_accordion_init_collapse = function () { | |
| 552 | +let mainwp_accordion_init_collapse = function () { | |
| 519 | 553 | jQuery('.mainwp-sidebar-accordion .title').addClass('active'); |
| 520 | 554 | jQuery('.mainwp-sidebar-accordion .content').addClass('active'); |
| 521 | 555 | |
| 522 | 556 | jQuery('.mainwp-sidebar-accordion').each(function () { |
| 523 | - var ident = jQuery('.mainwp-sidebar-accordion').index(this); | |
| 557 | + let ident = jQuery('.mainwp-sidebar-accordion').index(this); | |
| 524 | 558 | if (typeof (pagenow) !== 'undefined') { |
| 525 | - val = localStorage.getItem('mainwp-accordion[' + pagenow + '][' + ident + ']'); | |
| 559 | + let val = localStorage.getItem('mainwp-accordion[' + pagenow + '][' + ident + ']'); | |
| 526 | 560 | if (val === '0') { |
| 527 | 561 | jQuery(this).find('.title').removeClass('active'); |
| 528 | 562 | jQuery(this).find('.content').removeClass('active'); |
| 529 | 563 | } |
| @@ -530,27 +564,36 @@ | ||
| 530 | 564 | } |
| 531 | 565 | }); |
| 532 | 566 | }; |
| 533 | 567 | |
| 534 | -mainwp_ui_state_save = function (ident, val) { | |
| 568 | +globalThis.mainwp_ui_state_save = function (ident, val) { | |
| 535 | 569 | if (typeof (Storage) !== 'undefined') { |
| 536 | 570 | localStorage.setItem('mainwp-dashboard[' + ident + ']', val); |
| 537 | 571 | } |
| 538 | 572 | }; |
| 539 | 573 | |
| 540 | -mainwp_ui_state_load = function (ident) { | |
| 574 | +globalThis.mainwp_ui_state_load = function (ident) { | |
| 541 | 575 | if (typeof (Storage) !== 'undefined') { |
| 542 | 576 | return localStorage.getItem('mainwp-dashboard[' + ident + ']'); |
| 543 | 577 | } |
| 544 | - return 1; // show if Storage undefined. | |
| 578 | + return '1'; // show if Storage undefined. | |
| 545 | 579 | }; |
| 546 | 580 | |
| 547 | -jQuery(document).on('keyup', '#mainwp-screenshots-sites-filter', function () { | |
| 548 | - var filter = jQuery(this).val().toLowerCase(); | |
| 549 | - var parent = jQuery(this).closest('.mainwp_select_sites_wrapper'); | |
| 550 | - var tab = jQuery('#select_sites_tab').val(); | |
| 551 | - var siteItems = []; | |
| 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 | +}; | |
| 552 | 589 | |
| 590 | +globalThis.mainwp_sites_filter_select = function (objInput) { | |
| 591 | + let filter = jQuery(objInput).val().toLowerCase(); | |
| 592 | + let parent = jQuery(objInput).closest('.mainwp_select_sites_wrapper'); | |
| 593 | + let tab = jQuery('input[name="select_sites_tab"]').val(); | |
| 594 | + let siteItems = []; | |
| 595 | + | |
| 553 | 596 | if (tab == 'group') { |
| 554 | 597 | siteItems = parent.find('.mainwp_selected_groups_item'); |
| 555 | 598 | } else if (tab == 'site' || tab == 'staging') { |
| 556 | 599 | siteItems = parent.find('.mainwp_selected_sites_item'); |
| @@ -555,12 +598,12 @@ | ||
| 555 | 598 | } else if (tab == 'site' || tab == 'staging') { |
| 556 | 599 | siteItems = parent.find('.mainwp_selected_sites_item'); |
| 557 | 600 | } |
| 558 | 601 | |
| 559 | - for (var i = 0; i < siteItems.length; i++) { | |
| 560 | - var currentElement = jQuery(siteItems[i]); | |
| 561 | - var value = currentElement.find('label').text().toLowerCase(); | |
| 562 | - if (value.indexOf(filter) > -1) { | |
| 602 | + for (let id of siteItems) { | |
| 603 | + let currentElement = jQuery(id); | |
| 604 | + let value = currentElement.find('label').text().toLowerCase(); | |
| 605 | + if (value.includes(filter)) { | |
| 563 | 606 | currentElement.removeClass('no-select').show(); |
| 564 | 607 | } else { |
| 565 | 608 | currentElement.addClass('no-select').hide(); |
| 566 | 609 | } |
| @@ -567,22 +610,59 @@ | ||
| 567 | 610 | } |
| 568 | 611 | if (tab == 'site' || tab == 'staging') { |
| 569 | 612 | mainwp_newpost_updateCategories(); |
| 570 | 613 | } |
| 614 | +} | |
| 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 | + | |
| 636 | +jQuery(document).on('keyup', '#mainwp-select-sites-filter', function () { | |
| 637 | + mainwp_sites_filter_select(this); | |
| 571 | 638 | }); |
| 572 | 639 | |
| 573 | -mainwp_get_icon_start = function () { | |
| 640 | +jQuery(document).on('keyup', '#mainwp-screenshots-sites-filter', function () { | |
| 641 | + mainwp_sites_filter_select(this); | |
| 642 | +}); | |
| 643 | + | |
| 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); } }); | |
| 651 | + } | |
| 652 | +}); | |
| 653 | + | |
| 654 | +globalThis.mainwp_get_icon_start = function () { | |
| 574 | 655 | jQuery('.cached-icon-expired').attr('queue', 1); |
| 575 | - bulkInstallTotal = jQuery('.cached-icon-expired[queue="1"]').length; | |
| 576 | - console.log(bulkInstallTotal + ': expired cached icons'); | |
| 577 | - if (0 === bulkInstallTotal) { | |
| 656 | + mainwpVars.bulkInstallTotal = jQuery('.cached-icon-expired[queue="1"]').length; | |
| 657 | + if (0 === mainwpVars.bulkInstallTotal) { | |
| 578 | 658 | return; |
| 579 | 659 | } |
| 580 | 660 | mainwp_get_icon_start_next(); |
| 581 | 661 | } |
| 582 | 662 | |
| 583 | -mainwp_get_icon_start_next = function () { | |
| 584 | - itemIconProcess = jQuery('.cached-icon-expired[queue="1"]:first'); | |
| 663 | +let mainwp_get_icon_start_next = function () { | |
| 664 | + let itemIconProcess = jQuery('.cached-icon-expired[queue="1"]:first'); | |
| 585 | 665 | if (itemIconProcess.length > 0) { |
| 586 | 666 | mainwp_get_icon_start_specific(itemIconProcess); |
| 587 | 667 | } else { |
| 588 | 668 | console.log('Finished update icons.'); |
| @@ -588,13 +668,14 @@ | ||
| 588 | 668 | console.log('Finished update icons.'); |
| 589 | 669 | } |
| 590 | 670 | }; |
| 591 | 671 | |
| 592 | -mainwp_get_icon_start_specific = function (itemIconProcess) { | |
| 672 | +let mainwp_get_icon_start_specific = function (itemIconProcess) { | |
| 593 | 673 | itemIconProcess.attr('queue', '0'); |
| 594 | - var type = itemIconProcess.attr('icon-type'); | |
| 674 | + let slug; | |
| 675 | + let type = itemIconProcess.attr('icon-type'); | |
| 595 | 676 | if (type == 'plugin' || type == 'theme') { |
| 596 | - var slug = jQuery(itemIconProcess).attr('item-slug'); | |
| 677 | + slug = jQuery(itemIconProcess).attr('item-slug'); | |
| 597 | 678 | } else { |
| 598 | 679 | return; |
| 599 | 680 | } |
| 600 | 681 | |
| @@ -601,22 +682,21 @@ | ||
| 601 | 682 | if ('' === slug || '.' === slug) { |
| 602 | 683 | mainwp_get_icon_start_next(); |
| 603 | 684 | } |
| 604 | 685 | |
| 605 | - var data = mainwp_secure_data({ | |
| 686 | + let data = mainwp_secure_data({ | |
| 606 | 687 | action: 'mainwp_refresh_icon', |
| 607 | 688 | slug: slug, |
| 608 | 689 | type: type |
| 609 | 690 | }); |
| 610 | 691 | jQuery.post(ajaxurl, data, function (response) { |
| 611 | - console.log(response + ': ' + slug); | |
| 612 | 692 | mainwp_get_icon_start_next(); |
| 613 | 693 | }); |
| 614 | 694 | } |
| 615 | 695 | |
| 616 | -jQuery(document).ready(function () { | |
| 696 | +jQuery(function () { | |
| 617 | 697 | jQuery(document).on('click', '.cached-icon-customable', function () { |
| 618 | - var iconObj = jQuery(this); | |
| 698 | + let iconObj = jQuery(this); | |
| 619 | 699 | jQuery('#mainwp_delete_image_field').hide(); |
| 620 | 700 | jQuery('#mainwp-upload-custom-icon-modal').modal('setting', 'closable', false).modal('show'); |
| 621 | 701 | if (iconObj[0].hasAttribute('src') && iconObj.attr('src') != '') { |
| 622 | 702 | jQuery('#mainwp_delete_image_field').find('.ui.image').attr('src', iconObj.attr('src')); |
| @@ -623,8 +703,9 @@ | ||
| 623 | 703 | jQuery('#mainwp_delete_image_field').show(); |
| 624 | 704 | } |
| 625 | 705 | jQuery(document).on('click', '#update_custom_icon_btn', function () { |
| 626 | 706 | mainwp_upload_custom_icon(iconObj); |
| 707 | + return false; | |
| 627 | 708 | }); |
| 628 | 709 | return false; |
| 629 | 710 | }); |
| 630 | 711 | |
| @@ -633,9 +714,9 @@ | ||
| 633 | 714 | jQuery('#mainwp-select-mainwp-themes-modal').modal({ |
| 634 | 715 | allowMultiple: false, |
| 635 | 716 | closable: true, |
| 636 | 717 | onHide: function () { |
| 637 | - window.location.href = location.href; | |
| 718 | + mainwp_forceReload(); | |
| 638 | 719 | } |
| 639 | 720 | }).modal('show'); |
| 640 | 721 | |
| 641 | 722 | return false; |
| @@ -643,34 +724,32 @@ | ||
| 643 | 724 | |
| 644 | 725 | }); |
| 645 | 726 | |
| 646 | 727 | |
| 647 | -mainwp_upload_custom_icon = function (iconObj) { | |
| 728 | +let mainwp_upload_custom_icon = function (iconObj) { | |
| 648 | 729 | |
| 649 | - var type = iconObj.attr('icon-type'); | |
| 730 | + let type = iconObj.attr('icon-type'); | |
| 650 | 731 | |
| 651 | 732 | if (type !== 'plugin' && type !== 'theme') { |
| 652 | 733 | return false; |
| 653 | 734 | } |
| 654 | 735 | |
| 655 | - var slug = jQuery(iconObj).attr('item-slug'); | |
| 736 | + let slug = jQuery(iconObj).attr('item-slug'); | |
| 656 | 737 | |
| 657 | - var deleteIcon = jQuery('#mainwp_delete_image_chk').is(':checked') ? true : false; | |
| 738 | + let deleteIcon = jQuery('#mainwp_delete_image_chk').is(':checked'); | |
| 658 | 739 | |
| 659 | - jQuery('#mainwp-message-zone-upload').removeClass('red green yellow'); | |
| 660 | - var msg = __('Updating the icon. Please wait...'); | |
| 661 | - | |
| 662 | - jQuery('#mainwp-message-zone-upload').html('<i class="notched circle loading icon"></i> ' + msg); | |
| 663 | - jQuery('#mainwp-message-zone-upload').show(); | |
| 740 | + let msg = MainWP.I18n.t('Updating the icon. Please wait...'); | |
| 741 | + mainwp_set_message_zone('#mainwp-message-zone-upload', '<i class="notched circle loading icon"></i> ' + msg, ''); | |
| 664 | 742 | jQuery('#update_custom_icon_btn').attr('disabled', 'disabled'); |
| 665 | 743 | |
| 666 | 744 | //Add via ajax!! |
| 667 | - var formdata = new FormData(jQuery('#uploadicon_form')[0]); | |
| 745 | + let formdata = new FormData(jQuery('#uploadicon_form')[0]); | |
| 668 | 746 | formdata.append("action", 'mainwp_upload_custom_icon'); |
| 669 | 747 | formdata.append("type", type); |
| 670 | 748 | formdata.append("slug", slug); |
| 671 | 749 | formdata.append("delete", deleteIcon ? 1 : 0); |
| 672 | 750 | formdata.append("security", security_nonces['mainwp_upload_custom_icon']); |
| 751 | + formdata.append("delnonce", jQuery(iconObj).attr('del-icon-nonce')); | |
| 673 | 752 | |
| 674 | 753 | jQuery.ajax({ |
| 675 | 754 | type: 'POST', |
| 676 | 755 | url: ajaxurl, |
| @@ -675,32 +754,32 @@ | ||
| 675 | 754 | type: 'POST', |
| 676 | 755 | url: ajaxurl, |
| 677 | 756 | data: formdata, |
| 678 | 757 | success: function (response) { |
| 679 | - if (response && response.result == 'success') { | |
| 680 | - var msg = ''; | |
| 758 | + if (response?.result == 'success') { | |
| 759 | + let msg = ''; | |
| 681 | 760 | |
| 682 | 761 | if (type === 'plugin') { |
| 683 | 762 | if (jQuery('#mainwp-show-plugins').length > 0) { |
| 684 | - msg = __('Loading...'); | |
| 763 | + msg = MainWP.I18n.t('Loading...'); | |
| 685 | 764 | mainwp_fetch_plugins(); |
| 686 | 765 | } |
| 687 | - } else { | |
| 688 | - if (jQuery('#mainwp_show_themes').length > 0) { | |
| 689 | - msg = __('Loading...'); | |
| 690 | - mainwp_fetch_themes(); | |
| 691 | - } | |
| 766 | + } else if (jQuery('#mainwp_show_themes').length > 0) { | |
| 767 | + msg = MainWP.I18n.t('Loading...'); | |
| 768 | + mainwp_fetch_themes(); | |
| 692 | 769 | } |
| 693 | - if (msg !== '') { | |
| 694 | - jQuery('#mainwp-message-zone-upload').html('<i class="notched circle loading icon"></i> ' + __('Loading...')); | |
| 770 | + if (msg === '') { | |
| 771 | + mainwp_set_message_zone('#mainwp-message-zone-upload'); | |
| 695 | 772 | } else { |
| 696 | - jQuery('#mainwp-message-zone-upload').hide(); | |
| 773 | + mainwp_set_message_zone('#mainwp-message-zone-upload', '<i class="notched circle loading icon"></i> ' + MainWP.I18n.t('Loading...'), ''); | |
| 697 | 774 | } |
| 698 | 775 | setTimeout(function () { |
| 699 | - window.location.href = location.href; | |
| 776 | + mainwp_forceReload(); | |
| 700 | 777 | }, 3000); |
| 778 | + } else if (response.error) { | |
| 779 | + feedback('mainwp-message-zone-upload', response.error, 'red'); | |
| 701 | 780 | } else { |
| 702 | - 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'); | |
| 703 | 782 | } |
| 704 | 783 | }, |
| 705 | 784 | error: function () { |
| 706 | 785 | }, |
| @@ -712,13 +791,532 @@ | ||
| 712 | 791 | }); |
| 713 | 792 | |
| 714 | 793 | }; |
| 715 | 794 | |
| 716 | -mainwp_guidedtours_onchange = function (me) { | |
| 717 | - var data = mainwp_secure_data({ | |
| 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 | + | |
| 881 | +let mainwp_upload_custom_types_icon = function (iconObj, uploadAct, iconItemId, iconFileSlug, deleteIcon, callback_uploaded) { | |
| 882 | + let msg = MainWP.I18n.t('Updating the icon. Please wait...'); | |
| 883 | + mainwp_set_message_zone('#mainwp-message-zone-upload', '<i class="notched circle loading icon"></i> ' + msg, ''); | |
| 884 | + jQuery('#update_custom_icon_btn').attr('disabled', 'disabled'); | |
| 885 | + | |
| 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 | + | |
| 890 | + //Add via ajax!! | |
| 891 | + let formdata = new FormData(jQuery('#uploadicon_form')[0]); | |
| 892 | + formdata.append("action", upload_act); | |
| 893 | + formdata.append("iconFileSlug", iconFileSlug); | |
| 894 | + formdata.append("iconItemId", iconItemId); | |
| 895 | + formdata.append("delete", deleteIcon ? 1 : 0); | |
| 896 | + formdata.append("security", security_nonces[upload_act]); | |
| 897 | + formdata.append("elementid", elemid); | |
| 898 | + formdata.append("delnonce", delNonce); | |
| 899 | + | |
| 900 | + jQuery.ajax({ | |
| 901 | + type: 'POST', | |
| 902 | + url: ajaxurl, | |
| 903 | + data: formdata, | |
| 904 | + success: function (response) { | |
| 905 | + jQuery('#update_custom_icon_btn').removeAttr('disabled'); | |
| 906 | + if (response?.result == 'success') { | |
| 907 | + mainwp_set_message_zone('#mainwp-message-zone-upload'); | |
| 908 | + if (typeof callback_uploaded == 'function') { | |
| 909 | + callback_uploaded(response); | |
| 910 | + } | |
| 911 | + } else if (response.error) { | |
| 912 | + feedback('mainwp-message-zone-upload', response.error, 'red'); | |
| 913 | + } else { | |
| 914 | + feedback('mainwp-message-zone-upload', MainWP.I18n.t('Undefined error. Please try again.'), 'red'); | |
| 915 | + } | |
| 916 | + }, | |
| 917 | + error: function () { | |
| 918 | + }, | |
| 919 | + contentType: false, | |
| 920 | + cache: false, | |
| 921 | + processData: false, | |
| 922 | + enctype: 'multipart/form-data', | |
| 923 | + dataType: 'json' | |
| 924 | + }); | |
| 925 | + | |
| 926 | +}; | |
| 927 | + | |
| 928 | +let mainwp_guidedtours_onchange = function (me) { | |
| 929 | + let data = mainwp_secure_data({ | |
| 718 | 930 | action: 'mainwp_guided_tours_option_update', |
| 719 | 931 | enable: jQuery(me).is(":checked") ? 1 : 0, |
| 720 | 932 | }); |
| 721 | 933 | jQuery.post(ajaxurl, data, function () { |
| 722 | - window.location.href = location.href; | |
| 934 | + setTimeout(() => { | |
| 935 | + mainwp_forceReload(); | |
| 936 | + }, 1000); | |
| 723 | 937 | }); |
| 724 | -} | |
| 938 | +} | |
| 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 | +} | |