(function($) { $(document).ready(function($) { /* Add next Ad */ $("body").on("click", "a.joy-ads-add", function(e) { e.preventDefault(); var widget_holder = $(this).closest('.widget-inside'); var cloner = widget_holder.find('.joy-ads-clone'); cloner.find('#tab-1').addClass('active'); cloner.find('#tab-1').addClass('active'); widget_holder.find('.joy-ads-container').append(cloner.html()); }); /* Remove Ad */ $("body").on("click", ".joy-remove-ad", function(e) { var deleteItem = confirm('Are you sure you want to delete this ad?'); deleteItem ? $(this).parent('li').remove() : ''; }); /* Tabs */ $("body").on("click", "ul.joy-tabs li", function() { var tab_id = $(this).attr('data-tab'); var this_tab = $(this); this_tab.siblings('li').removeClass('active'); this_tab.parent('ul').siblings(".joy-tabs-wrapper").find('.joy-tab-content.active').removeClass('active'); this_tab.addClass('active'); var activeTab = this_tab.parent("ul").siblings(".joy-tabs-wrapper").find("#" + tab_id); activeTab.addClass('active'); var tabType = activeTab.attr('data-type'); activeTab.closest('.joy-tabs-wrapper').find('#tab-type').val(tabType); }); /* Init sortable */ joy_ads_sortable(); $(document).on('widget-added', function(e) { joy_ads_sortable(); }); $(document).on('widget-updated', function(e) { joy_ads_sortable(); }); /* Show/hide custom size */ $("body").on("click", "input.joy-ad-size", function(e) { if ($(this).val() == 'custom') { $(this).parent().next().show(); } else { $(this).parent().next().hide(); } }); /* Show/hide rotation speed */ $("body").on("click", "input.joy-ad-rotate", function(e) { if ($(this).is(":checked")) { $(this).parent().next().show(); } else { $(this).parent().next().hide(); } }); /* Choose image from media file */ var thumbImage; $("body").on("click", "a.joy-ads-select-image-btn", function(e) { e.preventDefault(); var this_btn = $(this); var image = wp.media({ title: 'Upload Image', }).open() .on('select', function(e) { var uploaded_image = image.state().get('selection').first(); var thumbImage = uploaded_image.toJSON().url; this_btn.closest('li').find('.joy-ads-field-width').val(thumbImage); }); }); /* Sortable function */ function joy_ads_sortable() { $(".joy-ads-sortable").sortable({ revert: false, cursor: "move", delay: 100, placeholder: "joy-ads-sortable-drop" }); } }); })(jQuery);