(function($) { // handle embed form controls ALEmbedForm(); // handle html/shortcode copy to clipboard ALHandleEmbedCopy(); // handle link copy to clipboard ALHandleLinkCopy(); // handle reset count button ALHandleReset(); // handle admin settings tabs ALHandleTabs(); })( jQuery ); function ALEmbedForm(){ var htmlForm = jQuery('.affiliate_links_embed_html'); var SCForm = jQuery('.affiliate_links_embed_shortcode'); var htmlProto = jQuery('.affiliate_links_proto_html'); var htmlProtoLink = jQuery('a', htmlProto); var embedHtml = ''; var embedShortcode = ''; jQuery('.affiliate_links_control[type=checkbox]').on('change', function(){ var _self = jQuery(this); var attr = _self.data('attr'); var value = _self.data('value'); if(_self.is(':checked')) { htmlProtoLink.attr(attr, value); } else { htmlProtoLink.removeAttr(attr); } embedHtml = htmlProto.html(); embedShortcode = shortcodeHTML(embedHtml); htmlForm.val(embedHtml); SCForm.val(embedShortcode); }); jQuery('.affiliate_links_control[type=text]').on('keyup keydown input', function(){ var _self = jQuery(this); var attr = _self.data('attr'); var value = _self.val(); if(typeof value == 'undefined' || value == '' || value == null ) { if(attr == 'anchor') { htmlProtoLink.text('Click here!'); } else { htmlProtoLink.removeAttr(attr); } } else { if(attr == 'anchor') { htmlProtoLink.text(value); } else { htmlProtoLink.attr(attr, value); } } embedHtml = htmlProto.html(); embedShortcode = shortcodeHTML(embedHtml); htmlForm.val(embedHtml); SCForm.val(embedShortcode); }); function shortcodeHTML(embedHtml){ embedHtml = embedHtml.replace('', '[/af_link]'); embedHtml = embedHtml.replace('", "]"); return embedHtml; } } function ALHandleEmbedCopy(){ var btns = jQuery('.affiliate_links_copy'); btns.each(function(){ var btn = jQuery(this); var textareaClass = btn.data('source'); btn.on('click', function(e){ e.preventDefault(); var copyTextarea = document.querySelector('.'+textareaClass); copyTextarea.select(); try { document.execCommand('copy'); } catch (err) { console.log('Unable to copy'); } }); }); } function ALHandleLinkCopy(){ var btn = jQuery('.affiliate_links_copy_button'); btn.on('click', function(e){ e.preventDefault(); var copyLink = document.querySelector('.affiliate_links_copy_link'); try { window.getSelection().removeAllRanges(); var range = document.createRange(); range.selectNode(copyLink); window.getSelection().addRange(range); document.execCommand('copy'); window.getSelection().removeAllRanges(); } catch (err) { console.log('Unable to copy'); } }); } function ALHandleReset(){ var btn = jQuery('.affiliate_links_reset_button'); var countField = jQuery('.affiliate_links_total_count'); btn.on('click', function(e){ e.preventDefault(); var sureText = btn.data('confirm'); var sure = confirm(sureText); if (sure === true) { var metaName = btn.data('meta'); var inputHTML = ''; btn.append(inputHTML); countField.text('0'); btn.hide(); } }); } function ALHandleTabs(){ jQuery( '#af_links-settings-form a.nav-tab' ).on( 'click', function() { jQuery( '#af_links-settings-form a.nav-tab' ).removeClass( 'nav-tab-active' ); jQuery( '.af_links-nav-tab' ).removeClass( 'active' ); var id = jQuery( this ).attr( 'id' ).replace( '-tab', '' ); jQuery( '#' + id ).addClass( 'active' ); jQuery( this ).addClass( 'nav-tab-active' ); } ); }