(function($) { // handle embed form controls ALEmbedForm(); // handle copy to clipboard ALHandleCopy(); })( 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 ALHandleCopy(){ 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'); } }); }); }