admin.js
124 lines
| 1 | jQuery(function() { |
| 2 | var el_notice = jQuery( ".frash-notice" ), |
| 3 | type = el_notice.find( "input[name=type]" ).val(), |
| 4 | plugin_id = el_notice.find( "input[name=plugin_id]" ).val(), |
| 5 | url_wp = el_notice.find( "input[name=url_wp]" ).val(), |
| 6 | inp_email = el_notice.find( "input[name=EMAIL]" ), |
| 7 | btn_act = el_notice.find( ".frash-notice-act" ), |
| 8 | btn_dismiss = el_notice.find( ".frash-notice-dismiss" ), |
| 9 | ajax_data = {}; |
| 10 | |
| 11 | ajax_data.plugin_id = plugin_id; |
| 12 | ajax_data.type = type; |
| 13 | |
| 14 | function init_email() { |
| 15 | if ( ! inp_email.length ) { return; } |
| 16 | |
| 17 | // Adjust the size of the email field to its contents. |
| 18 | function adjust_email_size() { |
| 19 | var width, tmp = jQuery( "<span></span>" ); |
| 20 | |
| 21 | tmp.addClass( "input-field" ).text( inp_email.val() ); |
| 22 | tmp.appendTo( "body" ); |
| 23 | width = parseInt( tmp.width() ); |
| 24 | tmp.remove(); |
| 25 | |
| 26 | inp_email.width( width + 34 ); |
| 27 | } |
| 28 | |
| 29 | function email_keycheck( ev ) { |
| 30 | if ( 13 === ev.keyCode ) { |
| 31 | btn_act.click(); |
| 32 | } else { |
| 33 | adjust_email_size(); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | inp_email.keyup( email_keycheck ).focus().select(); |
| 38 | adjust_email_size(); |
| 39 | } |
| 40 | |
| 41 | // Display the notice after the page was loaded. |
| 42 | function initialize() { |
| 43 | el_notice.fadeIn( 500 ); |
| 44 | init_email(); |
| 45 | } |
| 46 | |
| 47 | // Hide the notice after a CTA button was clicked |
| 48 | function remove_notice() { |
| 49 | el_notice.fadeTo( 100 , 0, function() { |
| 50 | el_notice.slideUp( 100, function() { |
| 51 | el_notice.remove(); |
| 52 | }); |
| 53 | }); |
| 54 | } |
| 55 | |
| 56 | // Open a tab to rate the plugin. |
| 57 | function act_rate() { |
| 58 | var url = url_wp.replace( /\/plugins\//, "/support/plugin/" ) + "/reviews/?rate=5#new-post", |
| 59 | link = jQuery( '<a href="' + url + '" target="_blank">Rate</a>' ); |
| 60 | |
| 61 | link.appendTo( "body" ); |
| 62 | link[0].click(); |
| 63 | link.remove(); |
| 64 | } |
| 65 | |
| 66 | // Submit the user to our email list. |
| 67 | function act_email() { |
| 68 | |
| 69 | var form = inp_email.parent('form'); |
| 70 | //Submit email to mailing list |
| 71 | jQuery.ajax({ |
| 72 | type: form.attr('method'), |
| 73 | url: form.attr('action'), |
| 74 | data: form.serialize(), |
| 75 | cache: false, |
| 76 | dataType: 'json', |
| 77 | contentType: 'application/json; charset=utf-8', |
| 78 | success: function (data) { |
| 79 | console.log(data.msg); |
| 80 | } |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | // Notify WordPress about the users choice and close the message. |
| 85 | function notify_wordpress( action, message ) { |
| 86 | el_notice.attr( "data-message", message ); |
| 87 | el_notice.addClass( "loading" ); |
| 88 | |
| 89 | ajax_data.action = action; |
| 90 | jQuery.post( |
| 91 | window.ajaxurl, |
| 92 | ajax_data, |
| 93 | remove_notice |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | // Handle click on the primary CTA button. |
| 98 | // Either open the wp.org page or submit the email address. |
| 99 | btn_act.click(function( ev ) { |
| 100 | ev.preventDefault(); |
| 101 | |
| 102 | //Do not submit form if the value is not set |
| 103 | var email_inpt = btn_act.parent().find('input[type="email"]'); |
| 104 | if( ( !email_inpt.length || !email_inpt.val() ) && type === 'email' ) { |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | switch ( type ) { |
| 109 | case 'rate': act_rate(); break; |
| 110 | case 'email': act_email(); break; |
| 111 | } |
| 112 | |
| 113 | notify_wordpress( "frash_act", btn_act.data( "msg" ) ); |
| 114 | }); |
| 115 | |
| 116 | // Dismiss the notice without any action. |
| 117 | btn_dismiss.click(function( ev ) { |
| 118 | ev.preventDefault(); |
| 119 | |
| 120 | notify_wordpress( "frash_dismiss", btn_dismiss.data( "msg" ) ); |
| 121 | }); |
| 122 | |
| 123 | window.setTimeout( initialize, 500 ); |
| 124 | }); |