| @@ -1,45 +1,54 @@ | ||
| 1 | +// This file is a part of the admin side of CookieAdmin | |
| 2 | + | |
| 1 | 3 | jQuery(document).ready(function($){ |
| 2 | 4 | |
| 3 | 5 | var law = ''; |
| 6 | + var mediaUploader; | |
| 4 | 7 | |
| 5 | - $(".cookieadmin_showmore").on("click", function(){ | |
| 6 | - if($(".cookieadmin_showmore").html().includes("more")){ | |
| 7 | - $(".cookieadmin_preference").height("auto"); | |
| 8 | - $(this).html("Show less"); | |
| 9 | - }else{ | |
| 10 | - $(".cookieadmin_preference").height(""); | |
| 11 | - $(this).html("Show more"); | |
| 8 | + // Handling the General Setting position toggles | |
| 9 | + function toggle_type_options(){ | |
| 10 | + let jEle = $(this), | |
| 11 | + type_value = $(this).val(); | |
| 12 | + | |
| 13 | + if(!type_value){ | |
| 14 | + return; | |
| 12 | 15 | } |
| 13 | - }); | |
| 14 | - | |
| 15 | - $("#cookieadmin_layout_footer").on("change", function(){ | |
| 16 | - $("input[name=cookieadmin_position]").prop("checked", false); | |
| 17 | - $(".consent-position").slideDown(); | |
| 18 | - $(".cookieadmin_foter_layout").fadeIn(800); | |
| 19 | - $(".cookieadmin_box_layout").hide(); | |
| 20 | - $(".consent-modal-layout").show(); | |
| 21 | - }); | |
| 22 | - | |
| 23 | - $("#cookieadmin_layout_box").on("change", function(){ | |
| 24 | - $("input[name=cookieadmin_position]").prop("checked", false); | |
| 25 | - $(".consent-position").slideDown(); | |
| 26 | - $(".cookieadmin_foter_layout").hide(); | |
| 27 | - $(".cookieadmin_box_layout").fadeIn(800); | |
| 28 | - $(".consent-modal-layout").slideDown(); | |
| 29 | - }); | |
| 16 | + | |
| 17 | + let box_layout = $('.cookieadmin_box_layout').closest('label'), | |
| 18 | + footer_layout = $('.cookieadmin_footer_layout').closest('label'); | |
| 19 | + | |
| 20 | + $('input[name=cookieadmin_position]').prop('checked', false); | |
| 21 | + | |
| 22 | + switch(type_value){ | |
| 23 | + case 'footer': | |
| 24 | + $('.consent-position').slideDown(); | |
| 25 | + $('.consent-modal-layout').slideDown(); | |
| 26 | + box_layout.hide(); | |
| 27 | + footer_layout.show(); | |
| 28 | + break; | |
| 29 | + | |
| 30 | + case 'popup': | |
| 31 | + $('.consent-position').slideUp(); | |
| 32 | + $('.consent-modal-layout').slideUp(); | |
| 33 | + break; | |
| 34 | + | |
| 35 | + default: | |
| 36 | + $('.consent-position').slideDown(); | |
| 37 | + $('.consent-modal-layout').slideDown(); | |
| 38 | + footer_layout.hide(); | |
| 39 | + box_layout.show(); | |
| 40 | + break; | |
| 41 | + } | |
| 42 | + } | |
| 30 | 43 | |
| 31 | - $("#cookieadmin_layout_popup").on("change", function(){ | |
| 32 | - $("input[name=cookieadmin_position]").prop("checked", false); | |
| 33 | - $(".consent-position").slideUp(); | |
| 34 | - $(".consent-modal-layout").slideUp(); | |
| 35 | - }); | |
| 44 | + $('input[name="cookieadmin_layout"]').on('change', toggle_type_options); | |
| 36 | 45 | |
| 37 | 46 | setTimeout( function(){ |
| 38 | 47 | $('.updated, .error').not('.no-autohide').fadeOut('slow'); |
| 39 | 48 | }, 5000); |
| 40 | 49 | |
| 41 | - //==== Notice Section Preview | |
| 50 | + //==== Notice Section Preview Live Change | |
| 42 | 51 | |
| 43 | 52 | $('#cookieadmin_notice_title_layout').on('input', function () { |
| 44 | 53 | const titleValue = $(this).val().trim(); |
| 45 | 54 | $('#cookieadmin_notice_title').text(titleValue); |
| @@ -186,9 +195,9 @@ | ||
| 186 | 195 | |
| 187 | 196 | if(!!law.cookieadmin_position){ |
| 188 | 197 | $("#cookieadmin_position_"+law.cookieadmin_position).prop("checked", true); |
| 189 | 198 | }else{ |
| 190 | - $(".consent-position").hide(); | |
| 199 | + $(".consent-position").closest('label').hide(); | |
| 191 | 200 | } |
| 192 | 201 | $("#cookieadmin_modal_" + law.cookieadmin_modal).prop("checked", true); |
| 193 | 202 | $(".cookieadmin_cookie_modal").addClass("cookieadmin_" + law.cookieadmin_modal); |
| 194 | 203 | |
| @@ -229,16 +238,14 @@ | ||
| 229 | 238 | $(".cookieadmin_save_btn").text(law.cookieadmin_save_btn); |
| 230 | 239 | $(".cookieadmin_save_btn").css('background-color', $("#cookieadmin_save_btn_bg_color").val()); |
| 231 | 240 | $(".cookieadmin_save_btn").css('color', $("#cookieadmin_save_btn_color").val()); |
| 232 | 241 | |
| 233 | - $(".cookieadmin_showmore").css('color', law.cookieadmin_links_color); | |
| 234 | 242 | $(".act").css('color', law.cookieadmin_links_color); |
| 235 | 243 | |
| 236 | 244 | const sliders = $("#cookieadmin_wrapper .cookieadmin_slider"); |
| 237 | - $(sliders[0]).css('background-color', law.cookieadmin_slider_on_bg_color); | |
| 245 | + $(sliders[0]).css('background-color', law.cookieadmin_slider_off_bg_color); | |
| 238 | 246 | $(sliders[1]).css('background-color', law.cookieadmin_slider_off_bg_color); |
| 239 | 247 | $(sliders[2]).css('background-color', law.cookieadmin_slider_off_bg_color); |
| 240 | - $(sliders[3]).css('background-color', law.cookieadmin_slider_off_bg_color); | |
| 241 | 248 | |
| 242 | 249 | //Also set layout of consents |
| 243 | 250 | if(!!law.cookieadmin_position){ |
| 244 | 251 | $.each(law.cookieadmin_position.split("_"), function(i,v){ |
| @@ -265,17 +272,16 @@ | ||
| 265 | 272 | $(".cookieadmin_cookie_modal").css("display", "none"); |
| 266 | 273 | } |
| 267 | 274 | } |
| 268 | 275 | |
| 269 | - $("#cookieadmin_show_preview").on("click", function(){ | |
| 270 | - if($("#cookieadmin_layout_popup").prop("checked")){ | |
| 271 | - show_modal(); | |
| 272 | - }else{ | |
| 273 | - $(".cookieadmin_law_container").toggle(); | |
| 274 | - } | |
| 275 | - }); | |
| 276 | + function cookieadminShowPreview(){ | |
| 277 | + $('#cookieadmin_show_preview').toggle(); | |
| 278 | + $('.cookieadmin_law_container').toggle(); | |
| 279 | + $('#cookieadmin_hide_preview').toggle(); | |
| 280 | + } | |
| 281 | + $('#cookieadmin_show_preview, #cookieadmin_hide_preview').on('click', cookieadminShowPreview); | |
| 276 | 282 | |
| 277 | - $(".cookieadmin_customize_btn").on("click", function(){ | |
| 283 | + $('.cookieadmin_customize_btn').on('click', function(){ | |
| 278 | 284 | show_modal(); |
| 279 | 285 | }); |
| 280 | 286 | |
| 281 | 287 | $(".cookieadmin_close_pref").on("click", function(){ |
| @@ -281,9 +287,9 @@ | ||
| 281 | 287 | $(".cookieadmin_close_pref").on("click", function(){ |
| 282 | 288 | $(".cookieadmin_cookie_modal").hide(); |
| 283 | 289 | }); |
| 284 | 290 | |
| 285 | - $("#cookieadmin_consent_type").on("change", function(){ | |
| 291 | + function cookieadminChangeLaw(){ | |
| 286 | 292 | |
| 287 | 293 | var law = $("#cookieadmin_consent_type").find(":selected").attr("name"); |
| 288 | 294 | |
| 289 | 295 | $("#cookieadmin_consent_expiry").val(cookieadmin_policy[law].cookieadmin_days); |
| @@ -291,13 +297,13 @@ | ||
| 291 | 297 | $("[id^=cookieadmin_layout_]").prop("checked", false); |
| 292 | 298 | $("#cookieadmin_layout_"+cookieadmin_policy[law].cookieadmin_layout).prop("checked", true); |
| 293 | 299 | |
| 294 | 300 | if(cookieadmin_policy[law].cookieadmin_layout == "box"){ |
| 295 | - $(".cookieadmin_foter_layout").hide(); | |
| 296 | - $(".cookieadmin_box_layout").show(); | |
| 301 | + $(".cookieadmin_footer_layout").closest('label').hide(); | |
| 302 | + $(".cookieadmin_box_layout").closest('label').show(); | |
| 297 | 303 | }else{ |
| 298 | - $(".cookieadmin_foter_layout").show(); | |
| 299 | - $(".cookieadmin_box_layout").hide(); | |
| 304 | + $(".cookieadmin_footer_layout").closest('label').show(); | |
| 305 | + $(".cookieadmin_box_layout").closest('label').hide(); | |
| 300 | 306 | } |
| 301 | 307 | |
| 302 | 308 | $("[id^=cookieadmin_position_]").prop("checked", false); |
| 303 | 309 | $("#cookieadmin_position_"+cookieadmin_policy[law].cookieadmin_position).prop("checked", true); |
| @@ -361,37 +367,41 @@ | ||
| 361 | 367 | $("#cookieadmin_links_color_box").val(cookieadmin_policy[law].cookieadmin_links_color); |
| 362 | 368 | $("#cookieadmin_links_color").val(cookieadmin_policy[law].cookieadmin_links_color); |
| 363 | 369 | |
| 364 | 370 | law == 'cookieadmin_gdpr' ? $(".setting-prior").show() : $(".setting-prior").hide(); |
| 365 | - }); | |
| 371 | + } | |
| 372 | + $("#cookieadmin_consent_type").on("change", cookieadminChangeLaw); | |
| 366 | 373 | |
| 367 | - $("consent_page").submit(function(){ | |
| 374 | + function cookieadminSubmitConsent(e){ | |
| 368 | 375 | var checked = false; |
| 369 | 376 | |
| 370 | 377 | if (!$("input[name=cookieadmin_position]:checked").length && !$("#cookieadmin_layout_popup").prop("checked")) { |
| 371 | - alert('Please select one of the radio buttons.'); | |
| 372 | - event.preventDefault(); // Prevent form submission | |
| 378 | + alert('Please select a notice position.'); | |
| 379 | + e.preventDefault(); // Prevent form submission | |
| 373 | 380 | } |
| 374 | - }); | |
| 381 | + } | |
| 382 | + $("#consent_submenu").submit(cookieadminSubmitConsent); | |
| 375 | 383 | |
| 376 | - $("[id$=_preload]").on("click", function(){ | |
| 377 | - | |
| 378 | - if(!$(".setting-prior").find("[id$=_preload]:checked").length && $(".cookieadmin-settings").find(".cookieadmin-collapsible-notice").length){ | |
| 379 | - $(".cookieadmin-settings").find(".cookieadmin-collapsible-notice").remove(); | |
| 380 | - return; | |
| 384 | + function cookieadminPreloadCookies(){ | |
| 385 | + let notice = $('.cookieadmin-collapsible-notice'); | |
| 386 | + | |
| 387 | + if($('[name="cookieadmin_preload[]"]:checked').length > 1){ | |
| 388 | + if(notice.css('display') == 'none'){ | |
| 389 | + notice.show(); | |
| 390 | + } | |
| 391 | + } else { | |
| 392 | + if(notice.css('display') !== 'none'){ | |
| 393 | + notice.hide(); | |
| 394 | + } | |
| 381 | 395 | } |
| 382 | - | |
| 383 | - if(!$(".cookieadmin-settings").find(".cookieadmin-collapsible-notice").length){ | |
| 384 | - $(".setting-prior").after("<p class=\"cookieadmin-collapsible-notice\">Loading cookies prior to receiving user consent will make your website non-compliant with GDPR.</p>"); | |
| 385 | - $(".cookieadmin-collapsible-notice").show(); | |
| 386 | - } | |
| 387 | - | |
| 388 | - }); | |
| 396 | + } | |
| 397 | + $('[name="cookieadmin_preload[]"]').on('click', cookieadminPreloadCookies); | |
| 389 | 398 | |
| 390 | - $(".cookieadmin-scan").on("click", function(){ | |
| 399 | + function cookieadminScanCookies(){ | |
| 391 | 400 | |
| 392 | 401 | let cookieadmin_btn_val = $(this).prop('value'); |
| 393 | - $(this).prop("disabled", true).prop('value', 'Scanning...'); | |
| 402 | + var this_btn = $(this); | |
| 403 | + this_btn.prop("disabled", true).prop('value', 'Scanning...'); | |
| 394 | 404 | |
| 395 | 405 | $.ajax({ |
| 396 | 406 | url: cookieadmin_policy.admin_url, |
| 397 | 407 | method: "POST", |
| @@ -401,70 +411,73 @@ | ||
| 401 | 411 | cookieadmin_security : cookieadmin_policy.cookieadmin_nonce, |
| 402 | 412 | }, |
| 403 | 413 | success: function(result){ |
| 404 | 414 | |
| 405 | - if (result.success) { | |
| 406 | - | |
| 407 | - $('.cookieadmin-cookie-categorized tbody').find("tr:gt(1)").remove(); | |
| 408 | - $.each(result.data, function(index, res){ | |
| 409 | - | |
| 410 | - var row = ''; | |
| 411 | - exp = 'Session'; | |
| 412 | - | |
| 413 | - if(!!res.expires){ | |
| 414 | - exp = Math.round((res.expires - Date.now())/86400); | |
| 415 | - if(exp < 1 && !!res['Max-Age']){ | |
| 416 | - exp = res['Max-Age'] + " days"; | |
| 417 | - }else{ | |
| 418 | - exp = 'Session'; | |
| 419 | - } | |
| 420 | - } | |
| 421 | - | |
| 422 | - row = '<tr>'; | |
| 423 | - row += '<td>' + res.cookie_name + '</td>'; | |
| 424 | - row += '<td>' + res.description + '</td>'; | |
| 425 | - row += '<td>' + exp + '</td>'; | |
| 426 | - row += '<td> <span class="dashicons dashicons-edit cookieadmin_edit_icon" id="edit_'+res.id+'"></span> <span class="dashicons dashicons-trash cookieadmin_delete_icon" id="delete_'+res.id+'"></span> </td>'; | |
| 427 | - row += '</tr>'; | |
| 428 | - categorized_cookies[res.id] = res; | |
| 429 | - categorized_cookies[res.id]['expires'] = exp; | |
| 430 | - | |
| 431 | - if(!!res.category){ | |
| 432 | - type = "#" + res.category.toLowerCase() + "_tbody"; | |
| 433 | - $(type).find('.cookieadmin-empty-row').remove(); | |
| 434 | - $(type).append(row); | |
| 435 | - }else{ | |
| 436 | - $("#unknown_tbody").find('.cookieadmin-empty-row').remove(); | |
| 437 | - $("#unknown_tbody").append(row); | |
| 438 | - } | |
| 439 | - }); | |
| 440 | - | |
| 441 | - } else { | |
| 442 | - alert(result.data.message); | |
| 415 | + if(result.success){ | |
| 416 | + if(typeof cookieadmin_pro_policy === 'object'){ | |
| 417 | + alert(cookieadmin_pro_policy.lang.background_scan); | |
| 418 | + }else{ | |
| 419 | + alert(cookieadmin_policy.lang.scan_completed); | |
| 420 | + window.location.reload(); | |
| 421 | + } | |
| 422 | + }else{ | |
| 423 | + alert(result.message); | |
| 443 | 424 | } |
| 444 | 425 | |
| 445 | 426 | }, |
| 446 | 427 | error: function(xhr, status, error) { |
| 428 | + console.log('CookieAdmin AJAX Error:', status, error); | |
| 429 | + console.log('CookieAdmin response: ' + xhr.responseText); // Check the error message | |
| 447 | 430 | alert("Request failed: " + error); |
| 448 | 431 | }, |
| 449 | 432 | complete: function() { |
| 450 | - $(".cookieadmin-scan").prop("disabled", false).prop("value", cookieadmin_btn_val); | |
| 433 | + this_btn.prop("disabled", false).prop("value", cookieadmin_btn_val); | |
| 451 | 434 | } |
| 452 | 435 | }); |
| 453 | - }); | |
| 436 | + } | |
| 437 | + $(".cookieadmin-scan").on("click", cookieadminScanCookies); | |
| 454 | 438 | |
| 455 | - $("#cookieadmin_dialog_save_btn").on("click", function(){ | |
| 439 | + function cookieadminSaveCookie(){ | |
| 456 | 440 | |
| 441 | + $('#cookieadmin-message').removeClass().addClass('spinner is-active').text('').css('height','auto'); | |
| 457 | 442 | $(this).prop("disabled", true); |
| 458 | 443 | |
| 444 | + //set 0 to add real id to delete | |
| 445 | + cookie_id = $(this).attr("cookieadmin_cookie_id"); | |
| 446 | + // Get the patterns | |
| 447 | + const patterns = $("#cookieadmin-dialog-cookie-patterns").val(); | |
| 448 | + | |
| 459 | 449 | let cookie_info = { |
| 460 | 450 | name: $("#cookieadmin-dialog-cookie-name").val(), |
| 461 | - id: $(this).attr("cookieadmin_cookie_id"), | |
| 451 | + id: cookie_id, | |
| 462 | 452 | description: $("#cookieadmin-dialog-cookie-desc").val(), |
| 463 | 453 | duration: $("#cookieadmin-dialog-cookie-duration").val(), |
| 464 | 454 | type: $("#cookieadmin-dialog-cookie-category").val() |
| 465 | 455 | }; |
| 456 | + | |
| 457 | + if(patterns){ | |
| 458 | + cookie_info.patterns = patterns; | |
| 459 | + } | |
| 466 | 460 | |
| 461 | + $.each(cookie_info, function (i, val){ | |
| 462 | + if(!val){ | |
| 463 | + cookie_info = null; | |
| 464 | + return false; | |
| 465 | + } | |
| 466 | + }); | |
| 467 | + | |
| 468 | + if(!cookie_info){ | |
| 469 | + $(this).prop("disabled", false); | |
| 470 | + $('#cookieadmin-message').removeClass().addClass('cookieadmin-error-message').text('Please fill all the fields'); | |
| 471 | + return false; | |
| 472 | + } | |
| 473 | + | |
| 474 | + // Used to call add cookie, in the Pro file. | |
| 475 | + if(cookie_id == 0 && typeof cookieadminProSaveCookie === 'function'){ | |
| 476 | + cookieadminProSaveCookie(cookie_info); | |
| 477 | + return; | |
| 478 | + } | |
| 479 | + | |
| 467 | 480 | $.ajax({ |
| 468 | 481 | url: cookieadmin_policy.admin_url, |
| 469 | 482 | method: "POST", |
| 470 | 483 | data : { |
| @@ -475,48 +488,41 @@ | ||
| 475 | 488 | }, |
| 476 | 489 | success: function(result){ |
| 477 | 490 | |
| 478 | 491 | if(result.success){ |
| 479 | - | |
| 480 | - var row = ''; | |
| 481 | - | |
| 482 | - row = '<tr>'; | |
| 483 | - row += '<td>' + cookie_info.name + '</td>'; | |
| 484 | - row += '<td>' + cookie_info.description + '</td>'; | |
| 485 | - row += '<td>' + cookie_info.duration + '</td>'; | |
| 486 | - row += '<td> <span class="dashicons dashicons-edit cookieadmin_edit_icon" id="edit_'+cookie_info.id+'"></span> <span class="dashicons dashicons-trash cookieadmin_delete_icon" id="edit_'+cookie_info.id+'"></span> </td>'; | |
| 487 | - row += '</tr>'; | |
| 488 | - | |
| 489 | - $("#edit_"+cookie_info.id).parents("tr").remove(); | |
| 490 | - tbody = "#" + cookie_info.type.toLowerCase() + "_tbody"; | |
| 491 | - $(tbody).find('.cookieadmin-empty-row').remove(); | |
| 492 | - $(tbody).append(row); | |
| 493 | - | |
| 494 | - categorized_cookies[cookie_info.id]['cookie_name'] = cookie_info.name; | |
| 495 | - categorized_cookies[cookie_info.id]['description'] = cookie_info.description; | |
| 496 | - categorized_cookies[cookie_info.id]['expires'] = cookie_info.duration; | |
| 497 | - categorized_cookies[cookie_info.id]['category'] = cookie_info.type; | |
| 498 | - | |
| 492 | + cookieadminAddCookieToTable(cookie_info); | |
| 493 | + $('#cookieadmin-message').removeClass().addClass('cookieadmin-success-message').text(result.message); | |
| 499 | 494 | }else{ |
| 500 | - alert(result.data.message); | |
| 495 | + alert(result.message); | |
| 501 | 496 | } |
| 497 | + }, | |
| 498 | + error: function(xhr, status, error) { | |
| 499 | + console.log('CookieAdmin AJAX Error:', status, error); | |
| 500 | + console.log('CookieAdmin response: ' + xhr.responseText); // Check the error message | |
| 501 | + alert("Request failed: " + error); | |
| 502 | 502 | } |
| 503 | 503 | }); |
| 504 | 504 | $(this).prop("disabled", false); |
| 505 | - }); | |
| 505 | + } | |
| 506 | + $("#cookieadmin_dialog_save_btn").on("click", cookieadminSaveCookie); | |
| 506 | 507 | |
| 507 | - | |
| 508 | 508 | $("input[type=color]").on("input", function(){ |
| 509 | 509 | elemt = $(this).attr("id").replace("_box", ""); |
| 510 | 510 | $("#"+elemt).val($(this).val()); |
| 511 | + | |
| 512 | + // Updating the background color of the reconsent icon list | |
| 513 | + if(elemt === 'cookieadmin_re_consent_bg_color'){ | |
| 514 | + $('.cookieadmin-reconsent-icon').css('background-color', $(this).val()); | |
| 515 | + } | |
| 511 | 516 | }); |
| 512 | 517 | |
| 513 | 518 | |
| 514 | - $(document).on("click", ".cookieadmin_delete_icon", function(){ | |
| 519 | + function cookieadminDeleteCookie(){ | |
| 515 | 520 | |
| 516 | 521 | if(confirm("Are you sure you want to delete ?")){ |
| 517 | 522 | |
| 518 | 523 | cookie_raw_id = $(this).attr("id").replace("delete_", ""); |
| 524 | + var row = $(this).parents("tr") | |
| 519 | 525 | |
| 520 | 526 | $.ajax({ |
| 521 | 527 | url: cookieadmin_policy.admin_url, |
| 522 | 528 | method: "POST", |
| @@ -521,28 +527,31 @@ | ||
| 521 | 527 | url: cookieadmin_policy.admin_url, |
| 522 | 528 | method: "POST", |
| 523 | 529 | data : { |
| 524 | 530 | action : 'cookieadmin_ajax_handler', |
| 525 | - cookieadmin_act : 'cookieadmin-edit-cookie', | |
| 531 | + cookieadmin_act : 'cookieadmin-delete-cookie', | |
| 526 | 532 | cookie_raw_id : cookie_raw_id, |
| 527 | 533 | cookieadmin_security : cookieadmin_policy.cookieadmin_nonce, |
| 528 | 534 | }, |
| 529 | 535 | success: function(result){ |
| 530 | - | |
| 531 | 536 | if (result.success) { |
| 537 | + row.remove(); | |
| 532 | 538 | delete categorized_cookies[cookie_raw_id]; |
| 533 | 539 | }else{ |
| 534 | - alert(result.data.message); | |
| 540 | + alert(result.message); | |
| 535 | 541 | } |
| 542 | + }, | |
| 543 | + error: function(xhr, status, error) { | |
| 544 | + console.log('CookieAdmin AJAX Error:', status, error); | |
| 545 | + console.log('CookieAdmin response: ' + xhr.responseText); // Check the error message | |
| 546 | + alert("Request failed: " + error); | |
| 536 | 547 | } |
| 537 | 548 | }); |
| 538 | - | |
| 539 | - $(this).parents("tr").remove(); | |
| 540 | 549 | } |
| 541 | - }); | |
| 550 | + } | |
| 551 | + $('.cookieadmin-metabox-holder').on("click", ".cookieadmin_delete_icon", cookieadminDeleteCookie); | |
| 542 | 552 | |
| 543 | - | |
| 544 | - $(document).on("click", ".cookieadmin_edit_icon, .cookieadmin_dialog_modal_close_btn", function(){ | |
| 553 | + function cookieadminOpenCookieDialog(){ | |
| 545 | 554 | |
| 546 | 555 | const modal = document.getElementById('edit-cookie-modal'); |
| 547 | 556 | |
| 548 | 557 | if(modal.classList.contains("active")){ |
| @@ -548,25 +557,44 @@ | ||
| 548 | 557 | if(modal.classList.contains("active")){ |
| 549 | 558 | modal.classList.remove('active'); |
| 550 | 559 | }else{ |
| 551 | 560 | modal.classList.add('active'); |
| 552 | - var cookie_id = $(this).attr("id").replace("edit_", ""); | |
| 553 | 561 | |
| 554 | - $("#cookieadmin_dialog_save_btn").attr("cookieadmin_cookie_id", cookie_id); | |
| 555 | - $("#cookieadmin-dialog-cookie-name").val(categorized_cookies[cookie_id]['cookie_name']); | |
| 556 | - $("#cookieadmin-dialog-cookie-desc").val(categorized_cookies[cookie_id]['description']); | |
| 557 | - $("#cookieadmin-dialog-cookie-duration").val(categorized_cookies[cookie_id]['expires']); | |
| 558 | - if(!!categorized_cookies[cookie_id]['category']){ | |
| 559 | - $("#cookieadmin-dialog-cookie-category").val(categorized_cookies[cookie_id]['category']); | |
| 562 | + if($(this).hasClass('cookieadmin-add-cookie')){ | |
| 563 | + $("#cookieadmin_dialog_save_btn").attr("cookieadmin_cookie_id", 0); | |
| 564 | + $('.cookieadmin_modal-header').children()[0].innerHTML = 'Add Cookie'; | |
| 565 | + $('.cookieadmin_modal-container').find('input,textarea,select').val(''); | |
| 560 | 566 | }else{ |
| 561 | - $("#cookieadmin-dialog-cookie-category").val("unknown"); | |
| 567 | + | |
| 568 | + $('.cookieadmin_modal-header').children()[0].innerHTML = 'Edit Cookie'; | |
| 569 | + var cookie_id = $(this).attr("id").replace("edit_", ""); | |
| 570 | + | |
| 571 | + $("#cookieadmin_dialog_save_btn").attr("cookieadmin_cookie_id", cookie_id); | |
| 572 | + $("#cookieadmin-dialog-cookie-name").val(categorized_cookies[cookie_id]['cookie_name']); | |
| 573 | + $("#cookieadmin-dialog-cookie-desc").val(categorized_cookies[cookie_id]['description']); | |
| 574 | + $("#cookieadmin-dialog-cookie-duration").val(categorized_cookies[cookie_id]['expires']); | |
| 575 | + $("#cookieadmin-dialog-cookie-patterns").val(categorized_cookies[cookie_id]['patterns']); | |
| 576 | + if(!!categorized_cookies[cookie_id]['category']){ | |
| 577 | + $("#cookieadmin-dialog-cookie-category").val(categorized_cookies[cookie_id]['category']); | |
| 578 | + }else{ | |
| 579 | + $("#cookieadmin-dialog-cookie-category").val("unknown"); | |
| 580 | + } | |
| 562 | 581 | } |
| 582 | + | |
| 583 | + $('.cookieadmin-metabox-holder').on('mousedown.cookieadmin-close-dialog2', function(e){ | |
| 584 | + if ( !$(".cookieadmin_modal-container").is(e.target) && $(".cookieadmin_modal-container").has(e.target).length === 0 && !$(".cookieadmin_modal-container").is(e.target) && $(".cookieadmin_modal-container").has(e.target).length === 0) { | |
| 585 | + modal.classList.remove('active'); | |
| 586 | + $('#cookieadmin-message').removeClass().addClass('cookieadmin-success-message').text(''); | |
| 587 | + $('.cookieadmin-metabox-holder').off('mousedown.cookieadmin-close-dialog2'); | |
| 588 | + } | |
| 589 | + }); | |
| 563 | 590 | } |
| 564 | - }); | |
| 591 | + } | |
| 592 | + $(document).on("click", ".cookieadmin_edit_icon, .cookieadmin_dialog_modal_close_btn, .cookieadmin-add-cookie", cookieadminOpenCookieDialog); | |
| 565 | 593 | |
| 566 | 594 | |
| 567 | - $(document).on('click', '.cookieadmin-cookie-categorized tbody > tr:first-child', function() { | |
| 568 | - var $tbody = jQuery(this).closest('tbody'); | |
| 595 | + function cookieadminExpandCollapseCookiesList() { | |
| 596 | + var $tbody = $(this).closest('tbody'); | |
| 569 | 597 | var $rows = $tbody.find('tr:not(:first-child)'); |
| 570 | 598 | |
| 571 | 599 | if ($rows.is(':visible')) { |
| 572 | 600 | $rows.slideUp(); |
| @@ -574,8 +602,169 @@ | ||
| 574 | 602 | } else { |
| 575 | 603 | $rows.slideDown(); |
| 576 | 604 | $tbody.removeClass('collapsed'); |
| 577 | 605 | } |
| 606 | + } | |
| 607 | + $('.cookieadmin-metabox-holder').on('click', '.cookieadmin-cookie-categorized tbody > tr:first-child', cookieadminExpandCollapseCookiesList); | |
| 608 | + | |
| 609 | + function cookieadminUploadReconsentIcon(e){ | |
| 610 | + | |
| 611 | + e.preventDefault(); | |
| 612 | + | |
| 613 | + if(mediaUploader){ | |
| 614 | + mediaUploader.open(); | |
| 615 | + return; | |
| 616 | + } | |
| 617 | + | |
| 618 | + mediaUploader = wp.media({ | |
| 619 | + title: 'Select or Upload Icon', | |
| 620 | + button: {text: 'Use this icon'}, | |
| 621 | + multiple: false, | |
| 622 | + library: {type: 'image'} | |
| 623 | + }); | |
| 624 | + | |
| 625 | + mediaUploader.on('select', function() { | |
| 626 | + const attachment = mediaUploader.state().get('selection').first().toJSON(); | |
| 627 | + $('#cookieadmin_reconsent_img_url').val(attachment.url); | |
| 628 | + }); | |
| 629 | + | |
| 630 | + mediaUploader.open(); | |
| 631 | + | |
| 632 | + } | |
| 633 | + $('.cookieadmin-metabox-holder').on('click', '#cookieadmin_upload_icon_btn', cookieadminUploadReconsentIcon); | |
| 634 | + | |
| 635 | + // Content Blocking Toggle | |
| 636 | + $("#cookieadmin_content_blocking").on("change", function(){ | |
| 637 | + if($(this).prop("checked")){ | |
| 638 | + $(".cookieadmin-content-blocking-options").fadeIn(250); | |
| 639 | + }else{ | |
| 640 | + $(".cookieadmin-content-blocking-options").fadeOut(250); | |
| 641 | + } | |
| 578 | 642 | }); |
| 579 | 643 | |
| 644 | + // Tooltip | |
| 645 | + let cookieadminToolTip; | |
| 580 | 646 | |
| 581 | -}); | |
| 647 | + function cookieadminCreateTooltip(){ | |
| 648 | + cookieadminToolTip = $('<div>', { | |
| 649 | + class: 'cookieadmin-tooltip' | |
| 650 | + }).appendTo('body'); | |
| 651 | + } | |
| 652 | + | |
| 653 | + $('.cookieadmin-tooltip-box').on('mouseenter', function(e){ | |
| 654 | + | |
| 655 | + if(!cookieadminToolTip){ | |
| 656 | + cookieadminCreateTooltip(); | |
| 657 | + } | |
| 658 | + | |
| 659 | + var tip = $(this).attr('data-tip') || ''; | |
| 660 | + cookieadminToolTip.text(tip).css('opacity', 1); | |
| 661 | + | |
| 662 | + }).on('mousemove', function(e){ | |
| 663 | + | |
| 664 | + if(!cookieadminToolTip){ | |
| 665 | + return; | |
| 666 | + } | |
| 667 | + | |
| 668 | + var offset = 12; | |
| 669 | + var tooltipRect = cookieadminToolTip[0].getBoundingClientRect(); | |
| 670 | + | |
| 671 | + var top = e.clientY - tooltipRect.height - offset; | |
| 672 | + var left = e.clientX + offset; | |
| 673 | + | |
| 674 | + // Flip below if not enough space above | |
| 675 | + if(top < 8){ | |
| 676 | + top = e.clientY + offset; | |
| 677 | + } | |
| 678 | + | |
| 679 | + // Prevent right overflow | |
| 680 | + if(left + tooltipRect.width > window.innerWidth - 8){ | |
| 681 | + left = e.clientX - tooltipRect.width - offset; | |
| 682 | + } | |
| 683 | + | |
| 684 | + cookieadminToolTip.css({ | |
| 685 | + top: top + 'px', | |
| 686 | + left: left + 'px' | |
| 687 | + }); | |
| 688 | + }) | |
| 689 | + .on('mouseleave', function(){ | |
| 690 | + | |
| 691 | + if(!cookieadminToolTip){ | |
| 692 | + return; | |
| 693 | + } | |
| 694 | + | |
| 695 | + cookieadminToolTip.css('opacity', 0); | |
| 696 | + }); | |
| 697 | + | |
| 698 | + // Recommended plugins install/activate handler | |
| 699 | + $(document).on('click', '.cookieadmin-plugin-install-btn, .cookieadmin-plugin-activate-btn', function(e){ | |
| 700 | + e.preventDefault(); | |
| 701 | + var $btn = $(this); | |
| 702 | + var slug = $btn.data('slug'); | |
| 703 | + var is_install = $btn.hasClass('cookieadmin-plugin-install-btn'); | |
| 704 | + var originalText = $btn.text(); | |
| 705 | + | |
| 706 | + $btn.text(cookieadmin_policy.lang.processing).prop('disabled', true); | |
| 707 | + | |
| 708 | + $.ajax({ | |
| 709 | + url: ajaxurl, | |
| 710 | + method: 'POST', | |
| 711 | + data: { | |
| 712 | + action: 'cookieadmin_ajax_handler', | |
| 713 | + cookieadmin_act: is_install ? 'install_recommended_plugin' : 'activate_recommended_plugin', | |
| 714 | + cookieadmin_security: cookieadmin_policy.cookieadmin_nonce, | |
| 715 | + plugin: slug, | |
| 716 | + }, | |
| 717 | + dataType: 'json', | |
| 718 | + success: function(data){ | |
| 719 | + if(data.success){ | |
| 720 | + $btn.replaceWith('<span class="cookieadmin-badge cookieadmin-success">' + cookieadmin_policy.lang.active + '</span>'); | |
| 721 | + }else{ | |
| 722 | + $btn.text(originalText).prop('disabled', false); | |
| 723 | + alert(data.data && data.data.message ? data.data.message : cookieadmin_policy.lang.install_failed); | |
| 724 | + } | |
| 725 | + }, | |
| 726 | + error: function(){ | |
| 727 | + $btn.text(originalText).prop('disabled', false); | |
| 728 | + alert(cookieadmin_policy.lang.error_occurred); | |
| 729 | + }, | |
| 730 | + }); | |
| 731 | + }); | |
| 732 | + | |
| 733 | + | |
| 734 | +}); | |
| 735 | + | |
| 736 | +function cookieadminAddCookieToTable(cookie_info){ | |
| 737 | + | |
| 738 | + var row = ''; | |
| 739 | + | |
| 740 | + categorized_cookies[cookie_info.id] = !categorized_cookies[cookie_info.id] ? {} : categorized_cookies[cookie_info.id]; | |
| 741 | + | |
| 742 | + categorized_cookies[cookie_info.id]['id'] = cookie_info.id; | |
| 743 | + categorized_cookies[cookie_info.id]['cookie_name'] = cookie_info.name; | |
| 744 | + categorized_cookies[cookie_info.id]['description'] = cookie_info.description; | |
| 745 | + categorized_cookies[cookie_info.id]['expires'] = cookie_info.duration; | |
| 746 | + categorized_cookies[cookie_info.id]['category'] = cookie_info.type; | |
| 747 | + categorized_cookies[cookie_info.id]['patterns'] = cookie_info.patterns; | |
| 748 | + | |
| 749 | + if(cookie_info.duration > 0){ | |
| 750 | + cookie_info.duration += ' ' + cookieadmin_policy.lang.days; | |
| 751 | + }else{ | |
| 752 | + cookie_info.duration = cookieadmin_policy.lang.session; | |
| 753 | + } | |
| 754 | + | |
| 755 | + row = '<tr>'; | |
| 756 | + row += '<td>' + cookie_info.name + '</td>'; | |
| 757 | + row += '<td>' + cookie_info.description + '</td>'; | |
| 758 | + row += '<td>' + cookie_info.duration + '</td>'; | |
| 759 | + row += '<td> <span class="dashicons dashicons-edit cookieadmin_edit_icon" id="edit_'+cookie_info.id+'"></span> <span class="dashicons dashicons-trash cookieadmin_delete_icon" id="edit_'+cookie_info.id+'"></span> </td>'; | |
| 760 | + row += '</tr>'; | |
| 761 | + | |
| 762 | + jQuery("#edit_"+cookie_info.id).parents("tr").remove(); | |
| 763 | + tbody = "#" + cookie_info.type.toLowerCase() + "_tbody"; | |
| 764 | + jQuery(tbody).find('.cookieadmin-empty-row').remove(); | |
| 765 | + jQuery(tbody).append(row); | |
| 766 | + | |
| 767 | + if(!categorized_cookies[cookie_id]){ | |
| 768 | + jQuery('.cookieadmin_modal-container').find('input,textarea,select').val(''); | |
| 769 | + } | |
| 770 | +} | |