1//=============Tesla Themes Subscription ======================================= 2(function($) { 3 "use strict"; 4 5 function get_configs(){ 6 if(typeof ttSubscrConfig !== 'undefined'){ 7 return ttSubscrConfig; 8 }else{ 9 return ''; 10 } 11 } 12 13 function insert_subscription(form_data,form,options){ 14 $.ajax({ 15 type: 'POST', 16 url: ajaxurl, 17 data: form_data, 18 success: function(response){ 19 //console.log(response); 20 process_result(response,form,options); 21 }, 22 error: function (request, status, error) { 23 //console.log(request.responseText); 24 process_result(request.responseText,form,options); 25 }, 26 dataType: 'json', 27 }); 28 } 29 30 function process_result(result,form,options){ 31 //console.log(result) 32 if(config.result_wrapper){ 33 $(config.result_wrapper.tag,config.result_wrapper.attr).text(result).appendTo(config.result_container_selector); 34 }else{ 35 $(config.result_container_selector).html(result); 36 } 37 if (result === 'Subscribed' || result === config.success_msg){ 38 options.success(result,config,form); //calling success function callback 39 $(config.result_container_selector).addClass(config.success_class); 40 form.reset(); 41 //-------------------All good state end--------------------------------------- 42 }else{//-------------------PHP errors state start--------------------------------------- 43 options.error(result,config,form); //calling error function callback 44 $(config.result_container_selector).addClass(config.error_class); 45 }//-------------------PHP errors state end--------------------------------------- 46 } 47 48 function check_required(form,configs){ 49 var all_required_checker = true; //checker for all required fields 50 51 //Checking if all required fields are not empty 52 $(form).find('[data-tt-subscription-required]').each(function(){ 53 if(!$(this).val()){ 54 all_required_checker = false; 55 $(this).addClass(configs.required_class); 56 } 57 }); 58 59 return all_required_checker; 60 } 61 62 function validate_emails(form,configs){ 63 var all_emails_checker = true; //checker for all emails validation 64 65 $(form).find('[data-tt-subscription-type=email]').each(function(){ 66 if( !validateEmail($(this).val()) ){ 67 all_emails_checker = false; 68 $(this).addClass(configs.invalid_email_class); 69 } 70 }); 71 72 return all_emails_checker; 73 } 74 75 function validateEmail(sEmail) { 76 var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/; 77 if (filter.test(sEmail)) { 78 return true; 79 } 80 else { 81 return false; 82 } 83 } 84 85 function clean_result_container(configs){ 86 $(configs.result_container_selector).removeClass(configs.error_class+' '+configs.success_class+ ' ' +configs.animation_done_class).html(''); 87 } 88 89 function destroy_result_container(form,configs){ 90 $(configs.result_container_selector).removeClass(configs.error_class+' '+configs.success_class).addClass(configs.animation_done_class); 91 if ( configs.input_timeout ){ 92 $(form).find('input').removeClass(configs.required_class+' '+configs.invalid_email_class); 93 } 94 } 95 96 $.fn.extend({ 97 tt_subscription: function(options,arg) { 98 var defaults = { 99 success : function(result,config,form){}, 100 error:function(error,config,form){}, 101 required:function(config,form){}, 102 invalid_email:function(config,form){} 103 }; 104 //if (options && typeof(options) === 'object') { 105 options = $.extend( {}, defaults, options ); 106 //} 107 108 // this creates a plugin for each element in 109 // the selector or runs the function once per 110 // selector. To have it do so for just the 111 // first element (once), return false after 112 // creating the plugin to stop the each iteration 113 this.each(function() { 114 new $.tt_subscription(this, options, arg ); 115 }); 116 return; 117 } 118 }); 119 120 var config = get_configs(); 121 122 $.tt_subscription = function( form, options, arg ) { 123 124 if (options && typeof(options) === 'string') { 125 if (options === 'mymethod1') { 126 tt_subscription_method1( arg ); 127 } 128 else if (options === 'mymethod2') { 129 tt_subscription_method2( arg ); 130 } 131 return; 132 } 133 var result_timeout_id; 134 var input_timeout_id; 135 136 if(config == '') 137 get_configs(); 138 139 $(form).on('submit',function(event){ 140 //preventing from normal submition 141 event.preventDefault(); 142 143 clearTimeout(result_timeout_id); //clearing interval if it was set 144 145 var form_data = $(form).serialize(); 146 147 form_data = form_data + "&action=insert_subscription&subscription-nonce=" + config.subscription_nonce; //adding action for correct ajax call hook 148 149 clean_result_container(config); 150 151 if(check_required(form,config)){ 152 153 if(validate_emails(form,config)){ 154 //-------------------All good state start--------------------------------------- 155 var result = insert_subscription(form_data,form,options); //inserting subscription 156 }else{//-------Email not valid state start----------------------- 157 options.invalid_email(config,form); //calling invalid_email function callback 158 $(config.result_container_selector).addClass(config.error_class); 159 if(config.result_wrapper){ 160 $(config.result_wrapper.tag,config.result_wrapper.attr).text(config.invali
160d_email_msg).appendTo(config.result_container_selector); 161 }else{ 162 $(config.result_container_selector).html(config.invalid_email_msg); 163 } 164 }//-------Email not valid state end----------------------- 165 }else{//-------Required fields not filled state start----------------------- 166 options.required(config,form); //calling required function callback 167 $(config.result_container_selector).addClass(config.error_class); 168 if(config.result_wrapper){ 169 $(config.result_wrapper.tag,config.result_wrapper.attr).text(config.required_msg).appendTo(config.result_container_selector); 170 }else{ 171 $(config.result_container_selector).html(config.required_msg); 172 } 173 }//-------Required fields not filled state end----------------------- 174 175 result_timeout_id = setTimeout(function(){destroy_result_container(form,config);},config.result_timeout); //hiding results after a certain time 176 //clearTimeout(result_timeout_id); 177 }); 178 179 function tt_subscription_method1(arg) 180 { 181 //...do method1 with this and arg 182 } 183 184 function tt_subscription_method2(arg) 185 { 186 //...do method2 with this and arg 187 } 188 189 }; 190 191 192 $(document).ready(function($){ 193 if(config == '') 194 get_configs(); 195 if($('form[data-tt-subscription]').length) 196 $('form[data-tt-subscription]').tt_subscription() 197 }); 198})(jQuery);
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.