1jQuery(function($){ 2 3 var Formoid = (function(){ 4 5 var API_URL = ('https:' == location.protocol ? 'https:' : 'http:') + '//formoid.net/api/push'; 6 7 var $ajax = (function(){ 8 var ie = (/MSIE (\d+)\./.exec(navigator.userAgent) || [0, 0])[1]; 9 if (8 == ie || (9 == ie && 'file:' != location.protocol)){ 10 return function(url, settings){ 11 var xdr = new XDomainRequest(), defer = $.Deferred(); 12 xdr.open(settings.type, url); 13 xdr.onload = function(){ defer.resolve(this.responseText); }; 14 xdr.onerror = function(){ defer.reject(); }; 15 xdr.send(settings.data); 16 return defer; 17 }; 18 } else { 19 $.support.cors = true; 20 return $.ajax; 21 } 22 })(); 23 24 var prop = function(name, args){ 25 name = '__' + name + '__'; 26 if (args.length){ 27 this[name] = args[0]; 28 return this; 29 } 30 return this[name]; 31 }; 32 33 var attachMethods = function(obj, context, methods){ 34 $.each(methods, function(i, method){ 35 obj[method] = function(){ 36 return context[method].apply(context, arguments); 37 }; 38 }); 39 return obj; 40 }; 41 42 var Form = function(settings){ 43 settings = settings || {}; 44 this.__email__ = settings.email || ''; 45 this.__title__ = settings.title || ''; 46 this.__data__ = settings.data || []; 47 }; 48 49 Form.prototype.email = function(value){ 50 return prop.call(this, 'email', arguments); 51 }; 52 53 Form.prototype.title = function(value){ 54 return prop.call(this, 'title', arguments); 55 }; 56 57 Form.prototype.data = function(value){ 58 return prop.call(this, 'data', arguments); 59 }; 60 61 Form.prototype.send = function(data, beforeStart){ 62 var defer = attachMethods($.Deferred(), this, ['email', 'title', 'data', 'send']); 63 if (beforeStart){ 64 beforeStart.call(this, defer); 65 if ('pending' != defer.state()) 66 return defer; 67 } 68 $ajax(API_URL, { 69 type : 'POST', 70 data : JSON.stringify({ 71 email : this.__email__, 72 form : { 73 title : this.__title__, 74 data : (arguments.length ? data : this.__data__) 75 } 76 }) 77 }).done(function(responseText){ 78 try { 79 var data = JSON.parse(responseText); 80 if (data.error) defer.reject(data.error); 81 else defer.resolve(data.response); 82 } catch (e) { 83 defer.reject('Incorrect server response.'); 84 } 85 }).fail(function(){ 86 var error = 'Failed to query the server. '; 87 if ('onLine' in navigator && !navigator.onLine) 88 error += 'No connection to the Internet.'; 89 else error += 'Check the connection and try again.'; 90 defer.reject(error); 91 }); 92 return defer; 93 }; 94 95 return { 96 Form : function(settings){ 97 return new Form(settings); 98 } 99 } 100 101 })(); 102 103 var isValid = function(input){ 104 if (input.checkValidity) return input.checkValidity(); // native validation 105 var isValid = true, value = $(input).val(), type = $(input).attr('type'); 106 // regexp rules from https://github.com/running-coder/jquery-form-validation 107 if (value) isValid = !('email' === type && !/^([^@]+?)@(([a-z0-9]-*)*[a-z0-9]+\.)+([a-z0-9]+)$/i.test(value)); 108 else if ($(input).attr('required')) isValid = false; 109 $(input)[ (isValid ? 'remove' : 'add') + 'Class' ]('form-invalid'); 110 return isValid; 111 }; 112 113 $('[data-form-type="formoid"]').each(function(){ 114 var form, 115 $this = $(this), 116 $form = $this.is('form') ? $this : $this.find('form'), 117 $alert = $this.find('[data-form-alert]'), 118 $title = $this.is('[data-form-title]') ? $this : $this.find('[data-form-title]'), 119 $submit = $this.find('[type="submit"]'), 120 alertSuccess = $alert.html(); 121 $submit 122 .click(function(){ $form.addClass('form-active'); }); 123 $form.submit(function(event){ 124 event.preventDefault(); 125 $form.addClass('form-active'); 126 if ($submit.hasClass('btn-loading')) return; 127 var isValidForm = true, data = []; 128 form = form || Formoid.Form({ 129 email : $this.find('[data-form-email]').val(), 130 title : $title.attr('data-form-title') || $title.text() 131 }); 132 $alert.html(''); 133 $this.find('[data-form-field]').each(function(){ 134 if (!isValid(this)) 135 isValidForm = false;
136 data.push([ 137 $(this).attr('data-form-field') || $(this).attr('name'), 138 $(this).val() 139 ]); 140 }); 141 if (!isValidForm) return; 142 $submit.addClass('btn-loading').prop('disabled', true); 143 form.send(data).done(function(message){ 144 $form.removeClass('form-active'); 145 $this.find('[data-form-field]').val(''); 146 $alert.removeAttr('hidden'); 147 $alert.append($('<div class="alert alert-form alert-success text-xs-center"/>').text(alertSuccess || message)); 148 }).fail(function(error){ 149 $alert.append($('<div class="alert alert-form alert-danger text-xs-center"/>').text(error)); 150 }).always(function(){ 151 $submit.removeClass('btn-loading').prop('disabled', false); 152 }); 153 }); 154 }); 155 156});
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.