1'use strict'; 2 3var Form = function(){ 4 5 var $ = jQuery, 6 instances = []; 7 8 function onReady() { 9 $('.module.form').each(function () { 10 instances.push(initInstance($(this))); 11 }); 12 } 13 14 function initInstance(view) { 15 16 // Params 17 var $view = view; 18 var id = $view.data('id'); 19 var params = window.dataModules[id], 20 cs = false, // Activa el custom select 21 cdp = true, // Activa el custom datepicker 22 cc = true, // Activa el cambio del imput de provincias 23 formVerified = false; 24 25 $view.find('script').remove(); 26 27 // Uniform Styles 28 $view.find('input[type=radio]').uniform(); 29 30 // Methods 31 32 /** 33 * scrollToError 34 */ 35 function scrollToError() { 36 var $firstError = $($view.find('select.error, input.error, checkbox.error').get(0)); 37 var $position = $firstError.offset(); 38 if (undefined !== $position) { 39 $('html, body').animate({ 40 scrollTop : $position.top - (window.innerHeight / 2) - $firstError.height() 41 }, 500); 42 } 43 } 44 45 /** 46 * verifyForm 47 */ 48 function verifyForm(e) { 49 50 e.stopPropagation(); 51 52 if(!formVerified){ 53 54 var input; 55 56 e.preventDefault(); 57 58 if($view.find('.g-recaptcha').length){ 59 60 input = $($view.find('.g-recaptcha')[0]); 61 62 // Compruebo el captcha por ajax. Al enviar el formulario vuelvo a comprobarlo en el servidor. 63 $.ajax({ 64 url: params['urlAjax'], 65 method: 'POST', 66 data: { 67 "action": "check_captcha", 68 "g-recaptcha-response": input.find('[name="g-recaptcha-response"]').val() 69 }, 70 dataType: 'json' 71 }) 72 .done(function (data) { 73 74 if(data.result === 'ok'){ 75 76 checkInputs(); 77 78 }else{ 79 80 grecaptcha.reset(); 81 showError(input); 82 83 } 84 85 }) 86 .error(function (error) { 87 88 grecaptcha.reset(); 89 showError(input); 90 91 }); 92 93 return false; 94 95 }else{ 96 97 checkInputs(); 98 99 } 100 101 }else{ 102 103 return true; 104 105 } 106 107 } 108 109 function checkInputs() { 110 111 var form, input, error; 112 113 form = $view; 114 error = false; 115 116 var $inputs = form.find('input, select'); 117 118 $inputs.each(function () { 119 input = $(this); 120
121 // Validando inputs 122 if(!input.prop('disabled')) { 123 124 if (input.get(0).localName === 'input') { 125 if (input.attr('type') === 'email') { 126 if ((input.prop('required') || input.val().length > 0) && !FormatText.testEmail(input.val())) { 127 showError(input); 128 error = true; 129 } 130 } 131 else if (input.attr('type') === 'radio' || input.attr('type') === 'checkbox') { 132 if ( form.find('input[name="' + input.attr('name') + '"]:required').prop('checked') === false ) { 133 showError(input, input.parents('div.checker').length > 0 ? true : false); 134 error = true; 135 } 136 } 137 else if(input.attr('type') === 'file'){ 138 139 if ((input.prop('required') && input.val().length < 1)) { 140 input.parents('.label-file').find('.custom-inputfile').addClass('error'); 141 showError(input); 142 error = true; 143 } 144 145 if (input[0].files.length > 0) { 146 147 if (input.attr('data-size') !== '') { 148 149 var allowedSize = input.attr('data-size') * 1000000; 150 var fileSize = input[0].files[0].size; 151 152 if (fileSize > allowedSize){ 153 154 input.parents('.label-file').find('.custom-inputfile').addClass('error'); 155 showError(input); 156 error = true; 157 158 } 159 160 } 161 162 if (input.attr('data-types') !== '') { 163 164 var allowedTypes = input.attr('data-types').split(','); 165 var fileType = input[0].files[0].name.split('.').pop(); 166 167 if (allowedTypes.indexOf(fileType) === -1) { 168 169 input.parents('.label-file').find('.custom-inputfile').addClass('error'); 170 showError(input); 171 error = true; 172 173 } 174 175 } 176 177 } 178 179 } 180 else if( input.attr('type') === 'number' ) { 181 if ( input.prop('required') && (isNaN(Number(input.val())) || input.val() === '') ) { 182 input.parents('.label-file').find('.custom-inputfile').addClass('error'); 183 showError(input); 184 error = true; 185 } 186 } 187 else if (input.find('option').length > 0) { 188 if ((input.prop('required') && input.val() === '')) { 189 showError(input); 190 191 error = true; 192 } 193 } 194 else { 195 if (input.prop('required') && !FormatText.testStandard(input.val())) { 196 showError(input); 197 error = true; 198 } 199 } 200 } 201 // Validando el select 202 else if (input.get(0).localName === 'select') { 203 if ((input.prop('required') && ( input.val() === '' || input.val() === '--' ))) { 204 showError(input, true); 205 error = true; 206 } 207 } 208 209 } 210 211 }); 212 213 if (!error) { 214 215 if(!formVerified){ 216 217 formVerified = true; 218 form.submit(); 219 220 } 221 222 } else { 223 224 grecaptcha.reset(); 225 scrollToError(); 226 227 } 228 229 } 230 231 function showError(input, customSelect) { 232 if (!customSelect) { 233 input.addClass('error'); 234 } 235 else { 236 if (input.attr('type') === 'checkbox') { 237 input.parents('div.checker').find('span').addClass('error'); 238 } else if (input.attr('type') === 'radio') { 239 input.parents('.label-radio').find('div.radio').addClass('error'); 240 } else if (input.attr('type') === 'file') { 241 input.parents('.label-file').find('.custom-inputfile').addClass('error'); 242 } else { 243 input.parents('div.selector').find('span').addClass('error'); 244 } 245 } 246 247 input.parents('form').find('#error').text(input.attr('data-error')); 248 input.parents('form').find('#error').addClass('show').addClass('active'); 249 input.off('change keyup paste click', resetInput); 250 input.on('change keyup paste click', resetInput); 251 setTimeout(function () { 252 $view.find('div#error').removeClass('show'); 253 }, 5000); 254 } 255 256 function resetInput(e) { 257 var input; 258 input = $(this); 259 input.removeClass('error'); 260 input.parents('form').find('#error').html(''); 261 input.parents('form').find('#error').removeClass('show').removeClass('active'); 262 input.parents('form').find('#error').text(); 263 // Check if is custom 264 if (input.parents('div.selector').length > 0) { 265 input.parents('div.selector').find('span').removeClass('error'); 266 } 267 if (input.parents('div.checker').length > 0) { 268 input.parents('div.checker').find('span').removeClass('error'); 269 } 270 if (input.parents('div.radio').length > 0) { 271 input.parents('div.radio').find('span').removeClass('error'); 272 } 273 input.off('change keyup paste click', resetInput); 274 } 275 276 function showStateInput() { 277 if (true === cc) { 278 var $input = $stateSelector.find('select, input'); 279 var $span = $stateSelector.find('span.custom-select'); 280 var $newInput = $('<input type="text" data-error="' + $input.data('error') + '" class="' + $input.attr('class') + '" id="' + $input.attr('id') + '" name="' + $input.attr('name') + '" required=""/>'); 281 $input.remove(); 282 $span.remove(); 283 $stateSelector.append($newInput); 284 } 285 } 286 287 function showStateSelect(data) { 288 if (true === cc) { 289 var $input = $stateSelector.find('select, input'); 290 var $span = $stateSelector.find('span.custom-select'); 291 var $newInput = $('<select data-error="' + $input.data('error') + '" class="' + $input.attr('class') + '" id="' + $input.attr('id') + '" name="' + $input.attr('name') + '" required=""></select>'); 292 $input.remove(); 293 $span.remove(); 294 $newInput.append(data); 295 $stateSelector.append($newInput); 296 } 297 } 298 299 function onCountryChange() { 300 var $obj = $(this); 301 var lang = $(this).val(); 302 lang = lang.toLocaleLowerCase(); 303 $.ajax({ 304 url: params['url'] + '?t=' + Math.random() * 1000, 305 method: 'GET', 306 data: { 307 lang: lang 308 }, 309 dataType: 'html' 310 }) 311 .done(function (data) { 312 if (data === 'ERR') { 313 showStateInput(); 314 } 315 else { 316 showStateSelect(data); 317 } 318 }) 319 .error(function (e) { 320 if (e.status === 404) { 321 showStateInput(); 322 } 323 }); 324 } 325 326 // Initial functions 327 328 $view.on('submit', verifyForm); 329 $view.find('input').on('change keyup paste click', resetInput); 330 $view.find('input[type="submit"]').prop('disabled', false); 331 332 // Custom selects 333 if (true === cs) { 334 $view.find('select').each(function () { 335 // $(this).customSelect({ 336 // customClass: 'custom-select' 337 // }); 338 }); 339 } 340 341 // Custom datepicker 342 if (true === cdp) { 343 var date = new Date(); 344 $view.find('input.datepicker').datepicker({ 345 // changeMonth: true, 346 changeYear: true,
347 yearRange: "1900:" + date.getFullYear() 348 }); 349 } 350 351 $view.find('input[type="file"]') 352 .change(function () { 353 var filepath = $(this).val(); 354 var filename = filepath.split('\\'); 355 $(this).parent().find('span.custom-inputfile span.filepath').remove(); 356 $(this).parent().find('span.custom-inputfile').append('<span class="filepath">' + filename[filename.length - 1] + '</span>'); 357 }); 358 // $('.mySelectBoxClass').customSelect({customClass:'myOwnClassName'}); 359 360 // Si existen los campos de selector de pais y selector de provincia los asociamos para que cambien según el campo escogido 361 var $countrySelector = $view.find('label[data-id="select-country"]'); 362 var $stateSelector = $view.find('label[data-id="select-state"]'); 363 if ( $countrySelector.length > 0 && $stateSelector.length > 0 ) { 364 $countrySelector.find('select').val('ES').change(onCountryChange); 365 $countrySelector.find('select').trigger('change'); 366 } 367 368 function eventOffCountryChange(value) { 369 cc = value; 370 } 371 372 $view.find('.custom-inputfile .content') 373 .off() 374 .on('click', function () { 375 $(this).parents('label').find('input').trigger('click'); 376 }); 377 378 Events.subscribe('OFF_COUNTRY_CHANGE', eventOffCountryChange); 379 Events.publish('FORM_INITIALIZED', []); 380 } 381 382 $(onReady); 383} 384 385Form();
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.