|
58 | 58 | reader.readAsDataURL(imgPath);
|
59 | 59 |
|
60 | 60 | reader.onload = function(fileObj) {
|
61 |
| - |
62 |
| - image.onload = function() { |
63 |
| - $(window).trigger('imageValidation', [this]); |
64 |
| - successCallback(this); |
65 |
| - }; |
66 |
| - |
67 |
| - image.onerror= function() { |
68 |
| - errCallback(); |
69 |
| - }; |
70 |
| - |
71 |
| - image.src = fileObj.target.result; |
72 |
| - |
| 61 | + setTimeout(function() { |
| 62 | + image.onload = function() { |
| 63 | + $(window).trigger('imageValidation', [this]); |
| 64 | + successCallback(this); |
| 65 | + }; |
| 66 | + |
| 67 | + image.onerror= function() { |
| 68 | + errCallback(); |
| 69 | + }; |
| 70 | + |
| 71 | + image.src = fileObj.target.result; |
| 72 | + }, 2000); |
73 | 73 | };
|
74 | 74 | };
|
75 | 75 |
|
|
194 | 194 | }
|
195 | 195 | };
|
196 | 196 |
|
197 |
| - var disableFormSubmit = function() { |
198 |
| - return false; |
199 |
| - }; |
200 |
| - |
201 | 197 | /**
|
202 | 198 | * Attach dimension check onto formUtils only for unit testing purpose
|
203 | 199 | * @param {HTMLImageElement} img
|
|
285 | 281 | */
|
286 | 282 | $.formUtils.addValidator({
|
287 | 283 | name : 'dimension',
|
288 |
| - validatorFunction : function(val, $input, conf, language, $form) { |
289 |
| - var hasCorrectDim = false; |
290 |
| - if( SUPPORTS_FILE_READER ) { |
291 |
| - var file = $input.get(0).files || []; |
292 |
| - hasCorrectDim = true; |
293 |
| - |
294 |
| - if( $input.attr('data-validation').indexOf('mime') === -1) { |
295 |
| - alert('You should validate file type being jpg, gif or png on input '+$input[0].name); |
296 |
| - return false; |
297 |
| - } |
298 |
| - else if( file.length > 1 ) { |
299 |
| - alert('Validating image dimensions does not support inputs allowing multiple files'); |
300 |
| - return false; |
301 |
| - } else if( file.length === 0) { |
302 |
| - return true; |
303 |
| - } |
304 |
| - |
305 |
| - if( $input.valAttr('has-valid-dim') ) { |
306 |
| - return true; |
307 |
| - } |
308 |
| - else if( $input.valAttr('has-not-valid-dim') ) { |
309 |
| - this.errorMessage = language.wrongFileDim + ' ' + $input.valAttr('has-not-valid-dim'); |
310 |
| - return false; |
311 |
| - } |
312 |
| - else if($.formUtils.eventType === 'keyup') { |
313 |
| - return null; |
314 |
| - } |
315 |
| - |
316 |
| - var wasFormSubmit = false; |
317 |
| - |
318 |
| - if( $.formUtils.isValidatingEntireForm ) { |
319 |
| - wasFormSubmit = true; |
320 |
| - $.formUtils.haltValidation = true; |
321 |
| - $form |
322 |
| - .bind('submit', disableFormSubmit) |
323 |
| - .addClass('on-blur'); |
324 |
| - } |
325 |
| - |
326 |
| - _loadImage(file[0], function(img) { |
327 |
| - var error = false; |
328 |
| - |
329 |
| - if ( $input.valAttr('dimension') ) { |
330 |
| - error = $.formUtils.checkImageDimension(img, $input.valAttr('dimension'), language); |
331 |
| - } |
332 |
| - |
333 |
| - if ( !error && $input.valAttr('ratio') ) { |
334 |
| - error = $.formUtils.checkImageRatio(img, $input.valAttr('ratio'), language); |
335 |
| - } |
336 |
| - |
337 |
| - // Set validation result flag on input |
338 |
| - if( error ) { |
339 |
| - $input.valAttr('has-not-valid-dim', error); |
340 |
| - } |
341 |
| - else { |
342 |
| - $input.valAttr('has-valid-dim', 'true'); |
343 |
| - } |
| 284 | + validatorFunction : function(val, $input, conf, language, $form, eventContext) { |
| 285 | + if (SUPPORTS_FILE_READER) { |
| 286 | + var thisValidator = this, |
| 287 | + asyncValidation = $.formUtils.asyncValidation(this.name, $input, $form); |
| 288 | + |
| 289 | + return asyncValidation.run(eventContext, function (done) { |
| 290 | + var file = $input.get(0).files || []; |
| 291 | + if ($input.attr('data-validation').indexOf('mime') === -1) { |
| 292 | + alert('You should validate file type being jpg, gif or png on input ' + $input[0].name); |
| 293 | + done(false); |
| 294 | + } else if (file.length > 1) { |
| 295 | + alert('Validating image dimensions does not support inputs allowing multiple files'); |
| 296 | + done(false); |
| 297 | + } else if (file.length === 0) { |
| 298 | + done(true); |
| 299 | + } else { |
| 300 | + _loadImage(file[0], function (img) { |
| 301 | + var error = false; |
344 | 302 |
|
345 |
| - // Remove validation flag when input changed |
346 |
| - if( !$input.valAttr('has-keyup-event') ) { |
347 |
| - $input |
348 |
| - .valAttr('has-keyup-event', '1') |
349 |
| - .bind('keyup change', function(evt) { |
350 |
| - if( evt.keyCode !== 9 && evt.keyCode !== 16 ) { |
351 |
| - $(this) |
352 |
| - .valAttr('has-not-valid-dim', false) |
353 |
| - .valAttr('has-valid-dim', false); |
354 |
| - } |
355 |
| - }); |
356 |
| - } |
| 303 | + if ($input.valAttr('dimension')) { |
| 304 | + error = $.formUtils.checkImageDimension(img, $input.valAttr('dimension'), language); |
| 305 | + } |
357 | 306 |
|
358 |
| - if( wasFormSubmit ) { |
359 |
| - $.formUtils.haltValidation = false; |
360 |
| - $form |
361 |
| - .removeClass('on-blur') |
362 |
| - .get(0).onsubmit = function() {}; |
| 307 | + if (!error && $input.valAttr('ratio')) { |
| 308 | + error = $.formUtils.checkImageRatio(img, $input.valAttr('ratio'), language); |
| 309 | + } |
363 | 310 |
|
364 |
| - $form.unbind('submit', disableFormSubmit); |
365 |
| - $form.trigger('submit'); // fire submit once more |
| 311 | + // Set validation result flag on input |
| 312 | + if (error) { |
| 313 | + thisValidator.errorMessage = language.wrongFileDim + ' ' + $input.valAttr('has-not-valid-dim'); |
| 314 | + done(false); |
| 315 | + } else { |
| 316 | + done(true); |
| 317 | + } |
366 | 318 |
|
367 |
| - } else { |
368 |
| - $input.trigger('blur'); // triggers the validation once more |
| 319 | + }, function (err) { |
| 320 | + throw err; |
| 321 | + }); |
369 | 322 | }
|
370 |
| - |
371 |
| - }, function(err) { |
372 |
| - throw err; |
373 | 323 | });
|
374 |
| - |
375 |
| - return true; |
376 | 324 | }
|
377 | 325 |
|
378 |
| - return hasCorrectDim; |
| 326 | + return true; // Unable to do the validation, lacking FileReader support |
379 | 327 | },
|
380 | 328 | errorMessage : '',
|
381 | 329 | errorMessageKey: '' // error message created dynamically
|
|
386 | 334 | * This event listener will remove error messages for file
|
387 | 335 | * inputs when file changes
|
388 | 336 | */
|
389 |
| - $(window).one('validatorsLoaded formValidationSetup', function(evt, $form) { |
390 |
| - var $inputs; |
391 |
| - if( $form ) { |
392 |
| - $inputs = $form.find('input[type="file"]'); |
393 |
| - } else { |
394 |
| - $inputs = $('input[type="file"]'); |
395 |
| - } |
396 |
| - |
397 |
| - $inputs.filter('*[data-validation]').bind('change', function() { |
398 |
| - $(this) |
399 |
| - .removeClass('error') |
400 |
| - .parent() |
401 |
| - .find('.form-error').remove(); |
402 |
| - }); |
| 337 | + $(window).one('validatorsLoaded formValidationSetup', function(evt, $form, conf) { |
| 338 | + var $inputs; |
| 339 | + if( $form ) { |
| 340 | + $inputs = $form.find('input[type="file"]'); |
| 341 | + } else { |
| 342 | + $inputs = $('input[type="file"]'); |
| 343 | + } |
| 344 | + $.formUtils.dialogs.removeInputStylingAndMessage($inputs, conf); |
403 | 345 | });
|
404 | 346 |
|
405 | 347 | })(jQuery, window);
|
0 commit comments