email-encoder-bundle.js
124 lines
| 1 | (function( $ ){ |
| 2 | |
| 3 | $(function(){ |
| 4 | |
| 5 | /** |
| 6 | * Encoded Form |
| 7 | */ |
| 8 | (function(){ |
| 9 | var prevEmail, getEncoded, |
| 10 | $wrap = $( 'div.email-encoder-form' ), |
| 11 | $email = $wrap.find( '#email' ), |
| 12 | $display = $wrap.find( '#display' ); |
| 13 | |
| 14 | // hide output |
| 15 | $wrap.find( '.nodis' ).hide(); |
| 16 | |
| 17 | // auto-set display field |
| 18 | $email.keyup(function(){ |
| 19 | var email = $email.val(), |
| 20 | display = $display.val(); |
| 21 | |
| 22 | if ( ! display || display == prevEmail ) |
| 23 | $display.val( email ); |
| 24 | |
| 25 | prevEmail = email; |
| 26 | }); |
| 27 | |
| 28 | // get encoded email ( ajax call ) |
| 29 | getEncoded = function () { |
| 30 | // stop when email field is empty |
| 31 | if ( ! $email.val() ) |
| 32 | return; |
| 33 | |
| 34 | // empty output |
| 35 | $wrap.find( '#encoded_output' ).val( '' ); |
| 36 | |
| 37 | // get the encoded email link |
| 38 | $.get( '', { |
| 39 | ajax: true, |
| 40 | email: $email.val(), |
| 41 | display: $display.val() || $email.val(), |
| 42 | method: $wrap.find( '#encode_method' ).val() |
| 43 | }, |
| 44 | function(data){ |
| 45 | $wrap.find( '#encoded_output' ).val( data ); |
| 46 | $wrap.find( '.output' ).slideDown(); |
| 47 | }); |
| 48 | }; |
| 49 | |
| 50 | // get encoded link on these events |
| 51 | $wrap.find( '#email, #display' ).keyup(function(){ |
| 52 | // show example how it will appear on the page |
| 53 | $wrap.find( '#example' ).html( '<a href="mailto:'+ $email.val() +'">'+ $display.val() +'</a>' ); |
| 54 | |
| 55 | // clear code field |
| 56 | $wrap.find( '.output' ).slideUp(); |
| 57 | $wrap.find( '#encoded_output' ).val( '' ); |
| 58 | }) |
| 59 | .keyup(); |
| 60 | |
| 61 | $wrap.find( '#encode_method' ).bind( 'change keyup', function(){ |
| 62 | getEncoded(); |
| 63 | }); |
| 64 | |
| 65 | $wrap.find( '#ajax_encode' ).click(function(){ |
| 66 | getEncoded(); |
| 67 | }); |
| 68 | }()); |
| 69 | |
| 70 | /** |
| 71 | * Admin Panel |
| 72 | */ |
| 73 | (function(){ |
| 74 | // skip rest when not admin |
| 75 | if ( $( '#adminmenu' ).size() == 0 ) |
| 76 | return; |
| 77 | |
| 78 | // prevent toggle when dragging |
| 79 | var toggle = true; |
| 80 | |
| 81 | // set info text for selected encoding method |
| 82 | $( '.method-info-select' ).bind( 'change blur keyup', function(){ |
| 83 | var method = $( this ).val(), |
| 84 | $desc = $( this ).parent().find( 'span.description' ); |
| 85 | |
| 86 | if ( methodInfo && methodInfo[ method ] ) { |
| 87 | $desc.html( methodInfo[ method ][ 'description' ] || '' ); |
| 88 | } else { |
| 89 | $desc.html( '' ); |
| 90 | } |
| 91 | }) |
| 92 | .blur(); |
| 93 | |
| 94 | // set sortable boxes |
| 95 | $( '.meta-box-sortables' ).sortable({ |
| 96 | items: '.postbox', |
| 97 | handle: 'h3', |
| 98 | placeholder: 'sortable-placeholder', |
| 99 | forcePlaceholderSize: true, |
| 100 | stop: function () { |
| 101 | toggle = false; |
| 102 | } |
| 103 | }); |
| 104 | |
| 105 | // set box content toggle |
| 106 | $( 'h3.hndle, div.handlediv' ).click(function(){ |
| 107 | if( toggle ) |
| 108 | $( this ).parent().find( '.inside' ).toggle(); |
| 109 | |
| 110 | toggle = true; |
| 111 | }); |
| 112 | |
| 113 | // set margins |
| 114 | $( 'div.postbox div.inside' ) |
| 115 | .css({ 'margin-left': '10px', 'margin-right': '10px' }); |
| 116 | |
| 117 | // add form-table class to Encoder Form tables |
| 118 | $( '.email-encoder-form table' ).addClass( 'form-table' ); |
| 119 | }()); |
| 120 | |
| 121 | }); |
| 122 | |
| 123 | })( jQuery ); |
| 124 |