encoder-form.js
75 lines
| 1 | jQuery( document ).ready( function( $ ) { |
| 2 | |
| 3 | 'use strict'; |
| 4 | |
| 5 | var $wrap = $('.eeb-form'); |
| 6 | var $email = $wrap.find('#eeb-email'); |
| 7 | var $display = $wrap.find('#eeb-display'); |
| 8 | var prevEmail; |
| 9 | |
| 10 | // get encoded email ( ajax call ) |
| 11 | var getEncoded = function () { |
| 12 | // stop when email field is empty |
| 13 | if (!$email.val()) { |
| 14 | return; |
| 15 | } |
| 16 | |
| 17 | // empty the output field |
| 18 | $wrap.find('#eeb-encoded-output').val(''); |
| 19 | |
| 20 | // get the encoded email link |
| 21 | $.post( eeb_ef.ajaxurl, { |
| 22 | action: 'eeb_get_email_form_output', |
| 23 | eebsec : eeb_ef.security, |
| 24 | eebEmail: $email.val(), |
| 25 | eebDisplay: $display.val() || $email.val(), |
| 26 | eebMethod: $wrap.find('#eeb-encode-method').val() |
| 27 | }, function ( response ) { |
| 28 | $wrap.find('#eeb-encoded-output').val( response ); |
| 29 | $wrap.find('.eeb-output').fadeIn(); |
| 30 | }); |
| 31 | |
| 32 | }; |
| 33 | |
| 34 | // hide output |
| 35 | $wrap.find('.eeb-output').hide(); |
| 36 | |
| 37 | // auto-set display field |
| 38 | $email.keyup(function () { |
| 39 | var email = $email.val(); |
| 40 | var display = $display.val(); |
| 41 | |
| 42 | if (!display || display === prevEmail) { |
| 43 | $display.val(email); |
| 44 | } |
| 45 | |
| 46 | prevEmail = email; |
| 47 | }); |
| 48 | |
| 49 | // get encoded link on these events |
| 50 | $wrap.find('#eeb-email, #eeb-display') |
| 51 | .keyup(function () { |
| 52 | if ($display.val().length > 0) { |
| 53 | // show example how it will appear on the page |
| 54 | $wrap.find('.eeb-example') |
| 55 | .html('<a href="mailto:' + $email.val() + '">' + $display.val() + '</a>') |
| 56 | .parents('tr').fadeIn(); |
| 57 | } else { |
| 58 | $wrap.find('.eeb-example').parents('tr').fadeOut(); |
| 59 | } |
| 60 | |
| 61 | // clear code field |
| 62 | $wrap.find('.eeb-output').fadeOut(); |
| 63 | $wrap.find('#eeb-encoded-output').val(''); |
| 64 | }) |
| 65 | .keyup(); |
| 66 | |
| 67 | $wrap.find('#eeb-encode-method').bind('change keyup', function () { |
| 68 | getEncoded(); |
| 69 | }); |
| 70 | |
| 71 | $wrap.find('#eeb-ajax-encode').click(function () { |
| 72 | getEncoded(); |
| 73 | }); |
| 74 | |
| 75 | }); |