1(function ($) { 2 'use strict'; 3 4 /** 5 * All of the code for your public-facing JavaScript source 6 * should reside in this file. 7 * 8 * Note: It has been assumed you will write jQuery code here, so the 9 * $ function reference has been prepared for usage within the scope 10 * of this function. 11 * 12 * This enables you to define handlers, for when the DOM is ready: 13 * 14 * $(function() { 15 * 16 * }); 17 * 18 * When the window is loaded: 19 * 20 * $( window ).load(function() { 21 * 22 * }); 23 * 24 * ...and/or other possibilities. 25 * 26 * Ideally, it is not considered best practise to attach more than a 27 * single DOM-ready or window-load handler for a particular page. 28 * Although scripts in the WordPress core, Plugins and Themes may be 29 * practising this, we should strive to set a better example in our own work. 30 */ 31 32 function prepareFormData(form, formData) { 33 var list_ids = []; 34 var is_multiple_lists = false; 35 jQuery.each((form.serializeArray() || {}), function (i, field) { 36 // Collect all list ids 37 if(field.name === 'lists[]') { 38 list_ids.push(field.value); 39 is_multiple_lists = true; 40 } else { 41 formData['esfpx_' + field.name] = field.value; 42 } 43 }); 44 45 if(is_multiple_lists) { 46 formData['esfpx_lists[]'] = list_ids; 47 } 48 49 50 return formData; 51 } 52 53 function handleResponse(response, form) { 54 55 var status = response.status; 56 57 var message_class = 'success'; 58 if(status === 'ERROR') { 59 message_class = 'error'; 60 } 61 62 var responseText = response['message_text']; 63 var messageContainer = $(form).next('.es_subscription_message'); 64 messageContainer.attr('class', 'es_subscription_message ' + message_class); 65 messageContainer.html(responseText); 66 var esSuccessEvent = { 67 detail: { 68 es_response : message_class, 69 msg: responseText 70 }, 71 bubbles: true, 72 cancelable: true 73 } ; 74 75 jQuery(form).trigger('es_response', [ esSuccessEvent ]); 76 } 77 78 function handleBindFunction(form, is_ig){ 79 var is_ig = is_ig || false; 80 var formData = {}; 81 formData = prepareFormData(form, formData); 82 formData['es'] = 'subscribe'; 83 formData['action'] = 'es_add_subscriber'; 84 var actionUrl = es_data.es_ajax_url; 85 jQuery(form).find('#spinner-image').show(); 86 $.ajax({ 87 type: 'POST', 88 url: actionUrl, 89 data: formData, 90 dataType: 'json', 91 success: function (response) { 92 if(!is_ig){ 93 if( response && typeof response.status !== 'undefined' && response.status === "SUCCESS" ) { 94 jQuery(form).slideUp('slow'); 95 jQuery(form).hide(); 96 } else { 97 jQuery(form).find('#spinner-image').hide(); 98 } 99 } 100 jQuery(window).trigger('es.send_response', [jQuery(form) , response]); 101 handleResponse(response, form); 102 }, 103 error: function (err) { 104 jQuery(form).find('#spinner-image').hide(); 105 console.log(err, 'error'); 106 }, 107 }); 108 109 110 return false; 111 } 112 113 $(document).ready(function () { 114 // var submitButton = $('.es_subscription_form_submit'); 115 116 $(document).on('submit', '.es_subscription_form', function (e) { 117 e.preventDefault(); 118 var form = $(this); 119 handleBindFunction(form); 120 }); 121 122 }); 123 // Compatibility of ES with IG 124 jQuery( window ).on( "init.icegram", function(e, ig) { 125 if(typeof ig !== 'undefined' && typeof ig.messages !== 'undefined' ) { 126 jQuery('.icegram .es_shortcode_form, .icegram form[data-source="ig-es"]').each(function(i, v){ 127 jQuery(v).bind('submit', function (e) { 128 e.preventDefault(); 129 var form = $(this); 130 handleBindFunction(form, true); 131 }); 132 }); 133 } 134 }); 135 136})(jQuery); 137 138
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.