admin.js
115 lines
| 1 | jQuery(document).ready(function($) { |
| 2 | "use strict"; |
| 3 | |
| 4 | function advads_load_ad_type_parameter_metabox(ad_type) { |
| 5 | $('#advanced-ads-ad-parameters').html('<span class="spinner advads-ad-parameters-spinner"></span>'); |
| 6 | $.ajax({ |
| 7 | type: 'POST', |
| 8 | url: ajaxurl, |
| 9 | data: { |
| 10 | 'action': 'load_ad_parameters_metabox', |
| 11 | 'ad_type': ad_type, |
| 12 | 'ad_id': $('#post_ID').val() |
| 13 | }, |
| 14 | success: function(data, textStatus, XMLHttpRequest) { |
| 15 | // toggle main content field |
| 16 | if (data) { |
| 17 | $('#advanced-ads-ad-parameters').html(data); |
| 18 | } |
| 19 | }, |
| 20 | error: function(MLHttpRequest, textStatus, errorThrown) { |
| 21 | $('#advanced-ads-ad-parameters').html(errorThrown); |
| 22 | } |
| 23 | }); |
| 24 | } |
| 25 | ; |
| 26 | |
| 27 | $(document).on('change', '#advanced-ad-type input', function() { |
| 28 | var ad_type = $(this).val() |
| 29 | advads_load_ad_type_parameter_metabox(ad_type); |
| 30 | }); |
| 31 | |
| 32 | // empty / clear input condition fields in the same row as the clear button |
| 33 | $('#advanced-ad-conditions .clear-radio').click(function() { |
| 34 | $(this).closest('tr').find('input[type="radio"]').prop('checked', false); |
| 35 | $(this).closest('tr').find('input[type="text"]').val(''); |
| 36 | }); |
| 37 | |
| 38 | // toggle display conditions |
| 39 | $('#advanced-ad-conditions-enable input').click(function(){ |
| 40 | advads_toggle_display_conditions(this.value); |
| 41 | }); |
| 42 | // display on load |
| 43 | advads_toggle_display_conditions($('#advanced-ad-conditions-enable input:checked').val()); |
| 44 | |
| 45 | // display / hide options if all-option is checked for display condition |
| 46 | $('.advanced-ad-display-condition .advads-conditions-all input').click(function(){ |
| 47 | advads_toggle_single_display_conditions(this); |
| 48 | }); |
| 49 | // display / hide options if all-option is checked for display condition – on load |
| 50 | $('.advanced-ad-display-condition .advads-conditions-all input').each(function(){ |
| 51 | advads_toggle_single_display_conditions(this); |
| 52 | }); |
| 53 | |
| 54 | }); |
| 55 | |
| 56 | /** |
| 57 | * toggle content elements (hide/show) |
| 58 | * |
| 59 | * @param selector jquery selector |
| 60 | */ |
| 61 | function advads_toggle(selector) { |
| 62 | jQuery(selector).slideToggle(); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * toggle content elements with a checkbox (hide/show) |
| 67 | * |
| 68 | * @param selector jquery selector |
| 69 | */ |
| 70 | function advads_toggle_box(e, selector) { |
| 71 | if(jQuery(e).is(':checked')){ |
| 72 | jQuery(selector).slideDown(); |
| 73 | } else { |
| 74 | jQuery(selector).slideUp(); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * disable content of one box when selecting another |
| 80 | * only grey/disable it, don’t hide it |
| 81 | * |
| 82 | * @param selector jquery selector |
| 83 | */ |
| 84 | function advads_toggle_box_enable(e, selector) { |
| 85 | if(jQuery(e).is(':checked')){ |
| 86 | jQuery(selector).find('input').removeAttr('disabled', ''); |
| 87 | } else { |
| 88 | jQuery(selector).find('input').attr('disabled', 'disabled'); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * toggle display conditions |
| 94 | * @param {bool} value |
| 95 | */ |
| 96 | function advads_toggle_display_conditions(value){ |
| 97 | if(value == 1){ |
| 98 | jQuery('#advanced-ad-conditions').fadeIn(); |
| 99 | } else { |
| 100 | jQuery('#advanced-ad-conditions').fadeOut(); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * disable new display conditions |
| 106 | * @param {string} checkbox element |
| 107 | */ |
| 108 | function advads_toggle_single_display_conditions(checkbox){ |
| 109 | // console.log(jQuery(checkbox).parent('div').find('label:not(.advads-conditions-all) input').css('border', 'solid')); |
| 110 | if(jQuery(checkbox).is(':checked')){ |
| 111 | jQuery(checkbox).parents('.advanced-ad-display-condition').find('.advads-conditions-single').addClass('disabled').find('input').attr('disabled', 'disabled'); |
| 112 | } else { |
| 113 | jQuery(checkbox).parents('.advanced-ad-display-condition').find('.advads-conditions-single').removeClass('disabled').find('input').removeAttr('disabled'); |
| 114 | } |
| 115 | } |