| 1 |
function wp_insert_ads_click_handler(type, identifier, title, isNew) { |
| 2 |
var isDuplicate = false; |
| 3 |
if(identifier.indexOf("###DUPLICATE###") != -1) { |
| 4 |
isDuplicate = true; |
| 5 |
identifier = identifier.replace("###DUPLICATE###",""); |
| 6 |
} |
| 7 |
var preTitle = jQuery('#wp_insert_'+type+'_ad_'+identifier).attr('data-pre-title'); |
| 8 |
jQuery('<div id="wp_insert_'+type+'_'+identifier+'_dialog"></div>').html('<div class="wp_insert_ajaxloader"></div>').dialog({ |
| 9 |
'modal': true, |
| 10 |
'resizable': false, |
| 11 |
'width': jQuery("body").width() * 0.8, |
| 12 |
'maxWidth': jQuery("body").width() * 0.8, |
| 13 |
'maxHeight': jQuery("body").height() * 0.9, |
| 14 |
'title': preTitle+' : '+title, |
| 15 |
position: { my: 'center', at: 'center', of: window }, |
| 16 |
open: function (event, ui) { |
| 17 |
jQuery('.ui-dialog').css({'z-index': 999999, 'max-width': '90%'}); |
| 18 |
jQuery('.ui-widget-overlay').css({'z-index': 999998, 'opacity': 0.8, 'background': '#000000'}); |
| 19 |
jQuery('.ui-dialog-buttonpane button:contains("Update")').button('disable'); |
| 20 |
jQuery.post( |
| 21 |
jQuery('#wp_insert_admin_ajax').val(), { |
| 22 |
'action': 'wp_insert_'+type+'_get_ad_form', |
| 23 |
'wp_insert_identifier': ((isDuplicate)?'###DUPLICATE###':'')+identifier, |
| 24 |
'wp_insert_type': type, |
| 25 |
'wp_insert_nonce': jQuery('#wp_insert_nonce').val() |
| 26 |
}, function(response) { |
| 27 |
jQuery('.wp_insert_ajaxloader').hide(); |
| 28 |
jQuery('.ui-dialog-content').html(response); |
| 29 |
jQuery('.ui-accordion .ui-accordion-content').css('max-height', (jQuery("body").height() * 0.45)); |
| 30 |
jQuery('.ui-dialog-buttonpane button:contains("Update")').button('enable'); |
| 31 |
jQuery('.wp_insert_'+type+'_status').parent().css({'display': 'inline-block', 'margin': '5px 0 0'}).prependTo('.ui-dialog-buttonpane'); |
| 32 |
jQuery('.ui-dialog').css({'position': 'fixed'}); |
| 33 |
jQuery('#wp_insert_'+type+'_'+identifier+'_dialog').delay(500).dialog({position: { my: 'center', at: 'center', of: window }}); |
| 34 |
} |
| 35 |
); |
| 36 |
}, |
| 37 |
buttons: { |
| 38 |
'Update': { |
| 39 |
text: 'Update', |
| 40 |
icons: { primary: "ui-icon-gear" }, |
| 41 |
click: function() { |
| 42 |
if(isNew) { |
| 43 |
var newIdentifier = jQuery('.wp_insert_'+type+'_identifier').val(); |
| 44 |
var adLink = jQuery("<a></a>"); |
| 45 |
adLink.attr('id', 'wp_insert_'+type+'_ad_'+newIdentifier); |
| 46 |
adLink.attr('href', 'javascript:;'); |
| 47 |
adLink.attr('class', 'wp_insert_ad_unit_title'); |
| 48 |
adLink.attr('data-pre-title', preTitle); |
| 49 |
adLink.attr('onClick', "wp_insert_ads_click_handler(\'"+type+"\', \'"+newIdentifier+"\', \'"+esc_html(jQuery('#wp_insert_'+type+'_'+newIdentifier+'_title').val())+"\', false)"); |
| 50 |
adLink.html(preTitle+' : '+esc_html(jQuery('#wp_insert_'+type+'_'+newIdentifier+'_title').val())); |
| 51 |
var deleteButton = jQuery('<span></span>'); |
| 52 |
deleteButton.attr('class', 'dashicons dashicons-no wp_insert_delete_icon'); |
| 53 |
deleteButton.attr('title', 'Delete Ad Unit'); |
| 54 |
deleteButton.attr('onClick', "wp_insert_ad_delete_handler(\'"+type+"\', \'"+newIdentifier+"\')"); |
| 55 |
var duplicateButton = jQuery('<span></span>'); |
| 56 |
duplicateButton.attr('class', 'dashicons dashicons-format-gallery wp_insert_duplicate_icon'); |
| 57 |
duplicateButton.attr('title', 'Duplicate Ad Unit'); |
| 58 |
duplicateButton.attr('onClick', "wp_insert_ads_click_handler(\'"+type+"\', \'###DUPLICATE###"+newIdentifier+"\', \'"+newIdentifier+"\', \'"+esc_html(jQuery('#wp_insert_'+type+'_'+newIdentifier+'_title').val())+" Duplicate\', false)"); |
| 59 |
jQuery('#wp_insert_'+type+'_ad_new').parent().before(jQuery('<p></p>').append(adLink, deleteButton, duplicateButton)); |
| 60 |
wp_insert_ad_update_handler(type, newIdentifier); |
| 61 |
} else { |
| 62 |
jQuery("#wp_insert_"+type+"_ad_"+identifier).html(preTitle+' : '+esc_html(jQuery('#wp_insert_'+type+'_'+identifier+'_title').val())); |
| 63 |
jQuery("#wp_insert_"+type+"_ad_"+identifier).attr('onClick', "wp_insert_ads_click_handler(\'"+type+"\', \'"+identifier+"\', \'"+esc_html(jQuery('#wp_insert_'+type+'_'+identifier+'_title').val())+"\', false)"); |
| 64 |
wp_insert_ad_update_handler(type, identifier); |
| 65 |
} |
| 66 |
jQuery(this).dialog('close'); |
| 67 |
} |
| 68 |
}, |
| 69 |
Cancel: { |
| 70 |
text: 'Cancel', |
| 71 |
icons: { primary: "ui-icon-cancel" }, |
| 72 |
click: function() { |
| 73 |
jQuery(this).dialog('close'); |
| 74 |
} |
| 75 |
} |
| 76 |
}, |
| 77 |
close: function() { |
| 78 |
jQuery(this).dialog('destroy'); |
| 79 |
} |
| 80 |
}); |
| 81 |
} |
| 82 |
|
| 83 |
function wp_insert_ad_update_handler(type, identifier) { |
| 84 |
inputElements = jQuery('#wp_insert_'+type+'_'+identifier+'_accordion').find('.input').map(function() { return jQuery(this).attr('name'); }).get(); |
| 85 |
inputElements = jQuery.grep(inputElements, function(v, i) { return jQuery.inArray(v, inputElements) === i }); |
| 86 |
|
| 87 |
autoArgs = {}; |
| 88 |
if(jQuery('#wp_insert_'+type+'_'+identifier+'_status').length > 0) { |
| 89 |
autoArgs['wp_insert_'+type+'_'+identifier+'_status'] = jQuery('#wp_insert_'+type+'_'+identifier+'_status').prop('checked'); |
| 90 |
} |
| 91 |
jQuery.each(inputElements, function(key, value) { |
| 92 |
currentElementID = value.replace('[', '_').replace(']', ''); |
| 93 |
while(currentElementID.includes('[') || currentElementID.includes(']')) { |
| 94 |
var currentElementID = currentElementID.replace('[', '_').replace(']', ''); |
| 95 |
} |
| 96 |
|
| 97 |
currentElement = jQuery('[name="'+value+'"]'); |
| 98 |
if(currentElement.prop('nodeName') == 'SELECT') { |
| 99 |
if(currentElement.prop('multiple')) { |
| 100 |
autoArgs[currentElementID] = jQuery.map(jQuery('[name="'+value+'"] :selected'), function(e) { return jQuery(e).val(); }); |
| 101 |
} else { |
| 102 |
autoArgs[currentElementID] = currentElement.val(); |
| 103 |
} |
| 104 |
} else if(currentElement.prop('nodeName') == 'INPUT') { |
| 105 |
if(currentElement.attr('type') == 'checkbox') { |
| 106 |
autoArgs[currentElementID] = currentElement.prop('checked'); |
| 107 |
} else if(currentElement.attr('type') == 'radio') { |
| 108 |
autoArgs[currentElementID] = jQuery('[name="'+value+'"]:checked').val(); |
| 109 |
} else { |
| 110 |
autoArgs[currentElementID] = currentElement.val(); |
| 111 |
} |
| 112 |
} else { |
| 113 |
autoArgs[currentElementID] = currentElement.val(); |
| 114 |
} |
| 115 |
}); |
| 116 |
|
| 117 |
parameters = []; |
| 118 |
jQuery.each(autoArgs, function(key, value) { |
| 119 |
parameters.push(key); |
| 120 |
}); |
| 121 |
|
| 122 |
autoArgs['action'] = 'wp_insert_'+type+'_save_ad_data'; |
| 123 |
autoArgs['wp_insert_nonce'] = jQuery('#wp_insert_nonce').val(); |
| 124 |
autoArgs['wp_insert_identifier'] = identifier; |
| 125 |
autoArgs['wp_insert_type'] = type; |
| 126 |
autoArgs['wp_insert_parameters'] = parameters; |
| 127 |
/*alert(JSON.stringify(autoArgs, null, 4));*/ |
| 128 |
jQuery.post( |
| 129 |
jQuery('#wp_insert_admin_ajax').val(), autoArgs, function(response) { } |
| 130 |
); |
| 131 |
} |
| 132 |
|
| 133 |
function wp_insert_ad_delete_handler(type, identifier) { |
| 134 |
jQuery("<p>Are you Sure you want to remove this Ad Unit?</p>").dialog({ |
| 135 |
'modal': true, |
| 136 |
'resizable': false, |
| 137 |
'title': 'Deletion Confirmation', |
| 138 |
position: { my: 'center', at: 'center', of: window }, |
| 139 |
open: function (event, ui) { |
| 140 |
jQuery('.ui-dialog').css({'z-index': 999999, 'max-width': '90%'}); |
| 141 |
jQuery('.ui-widget-overlay').css({'z-index': 999998, 'opacity': 0.8, 'background': '#000000'}); |
| 142 |
}, |
| 143 |
buttons : { |
| 144 |
'Confirm': function() { |
| 145 |
jQuery('#wp_insert_'+type+'_ad_'+identifier).parent().remove(); |
| 146 |
jQuery.post( |
| 147 |
jQuery('#wp_insert_admin_ajax').val(), { |
| 148 |
'action': 'wp_insert_'+type+'_delete_ad_data', |
| 149 |
'wp_insert_nonce': jQuery('#wp_insert_nonce').val(), |
| 150 |
'wp_insert_type': type, |
| 151 |
'wp_insert_identifier': identifier |
| 152 |
}, function(response) { |
| 153 |
} |
| 154 |
); |
| 155 |
jQuery(this).dialog("close"); |
| 156 |
}, |
| 157 |
'Cancel': function() { |
| 158 |
jQuery(this).dialog("close"); |
| 159 |
} |
| 160 |
}, |
| 161 |
close: function() { |
| 162 |
jQuery(this).dialog('destroy'); |
| 163 |
} |
| 164 |
}); |
| 165 |
} |
| 166 |
|