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 () { 14 defer.resolve(this.responseText); 15 }; 16 xdr.onerror = function () { 17 defer.reject(); 18 }; 19 xdr.send(settings.data); 20 return defer; 21 }; 22 } else { 23 $.support.cors = true; 24 return $.ajax; 25 } 26 })(); 27 28 var prop = function (name, args) { 29 name = '__' + name + '__'; 30 if (args.length) { 31 this[name] = args[0]; 32 return this; 33 } 34 return this[name]; 35 }; 36 37 var attachMethods = function (obj, context, methods) { 38 $.each(methods, function (i, method) { 39 obj[method] = function () { 40 return context[method].apply(context, arguments); 41 }; 42 }); 43 return obj; 44 }; 45 46 var Form = function (settings) { 47 settings = settings || {}; 48 this.__email__ = settings.email || ''; 49 this.__title__ = settings.title || ''; 50 this.__data__ = settings.data || []; 51 }; 52 53 Form.prototype.email = function (value) { 54 return prop.call(this, 'email', arguments); 55 }; 56 57 Form.prototype.title = function (value) { 58 return prop.call(this, 'title', arguments); 59 }; 60 61 Form.prototype.data = function (value) { 62 return prop.call(this, 'data', arguments); 63 }; 64 65 Form.prototype.send = function (data, beforeStart) { 66 var defer = attachMethods($.Deferred(), this, ['email', 'title', 'data', 'send']); 67 if (beforeStart) { 68 beforeStart.call(this, defer); 69 if ('pending' != defer.state()) 70 return defer; 71 } 72 $ajax(API_URL, { 73 type: 'POST', 74 data: JSON.stringify({ 75 email: this.__email__, 76 form: { 77 title: this.__title__, 78 data: (arguments.length ? data : this.__data__) 79 } 80 }) 81 }).done(function (responseText) { 82 try { 83 var data = JSON.parse(responseText); 84 if (data.error) defer.reject(data.error); 85 else defer.resolve(data.response); 86 } catch (e) { 87 defer.reject('Incorrect server response.'); 88 } 89 }).fail(function () { 90 var error = 'Failed to query the server. '; 91 if ('onLine' in navigator && !navigator.onLine) 92 error += 'No connection to the Internet.'; 93 else error += 'Check the connection and try again.'; 94 defer.reject(error); 95 }); 96 return defer; 97 }; 98 99 return { 100 Form: function (settings) { 101 return new Form(settings); 102 } 103 } 104 105 })(); 106 107 var isValidEmail = function (input) { 108 var isValid = true, 109 value = $(input).val(); 110 111 if (value) { 112 isValid = /^([^@]+?)@(([a-z0-9]-*)*[a-z0-9]+\.)+([a-z0-9]+)$/i.test(value); 113 } 114 115 return isValid; 116 }; 117 118 $('[data-form-type="formoid"]').each(function () { 119 var form, 120 $this = $(this), 121 $form = $this.is('form') ? $this : $this.find('form'), 122 $alert = $this.find('[data-form-alert]'), 123 $title = $this.is('[data-form-title]') ? $this : $this.find('[data-form-title]'), 124 $submit = $this.find('[type="submit"]'), 125 $inputArr = $form.find('[data-form-field]'), 126 alertSuccess = $alert.html(); 127 128 // on change form inputs if input[type=file].files[0]size>1mb disable sumbit button and show form alert 129 $form.change(function (event) { 130 if (event.target.type === 'file') { 131 if (event.target.files[0].size > 1000000) { 132 $alert.removeAttr('hidden'); 133 $alert.removeClass('alert-success').addClass('alert-danger'); 134 $alert.html('File size must be less than 1mb'); 135 $submit.addClass('btn-loading').prop('disabled', true); 136 } 137 } 138 }); 139 140 $inputArr.each(function (index, elem) { 141 $(elem).on("focus", function () { 142 $alert.attr('hidden', 'hidden'); 143 $alert.addClass('alert-success').removeClass('alert-danger'); 144 $alert.html(''); 145 $submit.removeClass('btn-loading').prop('disabled', false); 146 }) 147 }); 148 149 // event on form submit 150 $form.submit(function (event) { 151 event.preventDefault(); 152 console.log("Submitting...") 153 if ($submit.hasClass('btn-loading')) return; 154 155 var data = [], 156 inputs = $inputArr; 157 158 $form.addClass('form-active'); 159 $submit.addClass('btn-loading').prop('disabled', true); 160 $alert.html(''); 161 form = form || Formoid.Form({ 162 email: $this.find('[data-form-email]').val(), 163 title: $title.attr('data-form-title') || $title.text() 164 }); 165 166 function parseInputs(inputs) { 167 var def = $.Deferred(), 168 arrDeferred = []; 169 170 inputs.each(function (index, input) { 171 var $input = $(input), 172 typeInput = $input.attr('type'), 173 objDeferred = $.Deferred(); 174 175 arrDeferred.push(objDeferred); 176 177 // valide email 178 if ($input.attr('name') === "email") { 179 if (!isValidEmail(input)) { 180 $submit.removeClass('btn-loading').prop('disabled', true); 181 def.reject(new Error('Form is not val
181id')); 182 return false; 183 } 184 } 185 186 switch (typeInput) { 187 case 'file': { 188 var reader = new FileReader, 189 name = $input.attr('data-form-field') || $input.attr('name'), 190 files = $input[0].files[0]; 191 192 reader.onloadend = function () { 193 data.push([ 194 name, 195 reader.result 196 ]); 197 objDeferred.resolve(); 198 }; 199 reader.onerror = function () { 200 $alert.html(reader.error); 201 def.reject(reader.error); 202 }; 203 204 if (files) { 205 reader.readAsDataURL(files); 206 } 207 } 208 break; 209 case 'checkbox': { 210 data.push([ 211 $input.attr('data-form-field') || $input.attr('name'), 212 $input.prop("checked") ? $input.val() : 'No' 213 ]); 214 objDeferred.resolve(); 215 } 216 break; 217 case 'radio': { 218 if ($input.prop("checked")) { 219 data.push([ 220 $input.attr('data-form-field') || $input.attr('name'), 221 $input.val() 222 ]); 223 } 224 objDeferred.resolve(); 225 } 226 break; 227 default: { 228 data.push([ 229 $input.attr('data-form-field') || $input.attr('name'), 230 $input.val() 231 ]); 232 objDeferred.resolve(); 233 } 234 } 235 }); 236 237 return def.resolve(arrDeferred); 238 } 239 240 parseInputs(inputs).then(function (arrDef) { 241 $.when.apply(this, arrDef).then(function () { 242 form.send(data) 243 .always(function () { 244 $submit.removeClass('btn-loading').prop('disabled', false); 245 $alert.removeAttr('hidden'); 246 }) 247 .then(function (message) { 248 $form[0].reset(); 249 $form.removeClass('form-active'); 250 $alert.text(alertSuccess || message); 251 $alert.removeClass('alert-danger').addClass('alert-success') 252 }) 253 .fail(function (error) { 254 $alert.text(error); 255 $alert.removeClass('alert-success').addClass('alert-danger') 256 }) 257 }, function (err) { 258 $alert.html(err.message); 259 $alert.removeClass('alert-success').addClass('alert-danger'); 260 $alert.removeAttr('hidden'); 261 }) 262 }); 263 }); 264 }); 265});
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.