email-encoder-bundle.js
65 lines
| 1 | // Email Encoder Bundle Plugin |
| 2 | jQuery(function( $ ){ |
| 3 | |
| 4 | var prevEmail, getEncoded, |
| 5 | $wrap = $( 'div.email-encoder-form' ), |
| 6 | $email = $wrap.find( '#email' ), |
| 7 | $display = $wrap.find( '#display' ); |
| 8 | |
| 9 | // hide output |
| 10 | $wrap.find( '.nodis' ).hide(); |
| 11 | |
| 12 | // auto-set display field |
| 13 | $email.keyup(function(){ |
| 14 | var email = $email.val(), |
| 15 | display = $display.val(); |
| 16 | |
| 17 | if ( ! display || display == prevEmail ) |
| 18 | $display.val( email ); |
| 19 | |
| 20 | prevEmail = email; |
| 21 | }); |
| 22 | |
| 23 | // get encoded email ( ajax call ) |
| 24 | getEncoded = function () { |
| 25 | // stop when email field is empty |
| 26 | if ( ! $email.val() ) |
| 27 | return; |
| 28 | |
| 29 | // empty output |
| 30 | $wrap.find( '#encoded_output' ).val( '' ); |
| 31 | |
| 32 | // get the encoded email link |
| 33 | $.get( '', { |
| 34 | ajax: true, |
| 35 | email: $email.val(), |
| 36 | display: $display.val() || $email.val(), |
| 37 | method: $wrap.find( '#encode_method' ).val() |
| 38 | }, |
| 39 | function(data){ |
| 40 | $wrap.find( '#encoded_output' ).val( data ); |
| 41 | $wrap.find( '.output' ).slideDown(); |
| 42 | }); |
| 43 | }; |
| 44 | |
| 45 | // get encoded link on these events |
| 46 | $wrap.find( '#email, #display' ).keyup(function(){ |
| 47 | // show example how it will appear on the page |
| 48 | $wrap.find( '#example' ).html( '<a href="mailto:'+ $email.val() +'">'+ $display.val() +'</a>' ); |
| 49 | |
| 50 | // clear code field |
| 51 | $wrap.find( '.output' ).slideUp(); |
| 52 | $wrap.find( '#encoded_output' ).val( '' ); |
| 53 | }) |
| 54 | .keyup(); |
| 55 | |
| 56 | $wrap.find( '#encode_method' ).bind( 'change keyup', function(){ |
| 57 | getEncoded(); |
| 58 | }); |
| 59 | |
| 60 | $wrap.find( '#ajax_encode' ).click(function(){ |
| 61 | getEncoded(); |
| 62 | }); |
| 63 | |
| 64 | }); |
| 65 |