Home
»
»
f
f
Written By Le Salarié on mardi 2 janvier 2018 | janvier 02, 2018
$(document).ready(function() {
var formWrap = $( '#formNet' );
var result = $('.fs-result');
formWrap.submit(function(){
type = parseInt(queryType.value);
$('#fs-value #salary').empty();
$.ajax({
url: 'compute.php',
data: formWrap.serialize(),
dataType: "json",
error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(thrownError);
},
dataType: 'json',
success: function(data) {
switch(type) {
case 1:
console.log(data.brut);
$('#fs-value #salary').append(data.brut);
break;
case 2:
console.log(data.net);
$('#fs-value #salary').append(data.net);
break;
default:
}
result.show();
result.css('display', 'block');
formWrap.hide();
},
type: 'POST'
});
return false;
});
$('#fs-again').click(function() {
formWrap.show();
result.hide();
});
});
/**
**
*/
;( function( window ) {
'use strict';
var support = { animations : Modernizr.cssanimations },
animEndEventNames = { 'WebkitAnimation' : 'webkitAnimationEnd', 'OAnimation' : 'oAnimationEnd', 'msAnimation' : 'MSAnimationEnd', 'animation' : 'animationend' },
// animation end event name
animEndEventName = animEndEventNames[ Modernizr.prefixed( 'animation' ) ];
/**
* extend obj function
*/
function extend( a, b ) {
for( var key in b ) {
if( b.hasOwnProperty( key ) ) {
a[key] = b[key];
}
}
return a;
}
/**
* createElement function
* creates an element with tag = tag, className = opt.cName, innerHTML = opt.inner and appends it to opt.appendTo
*/
function createElement( tag, opt ) {
var el = document.createElement( tag )
if( opt ) {
if( opt.cName ) {
el.className = opt.cName;
}
if( opt.inner ) {
el.innerHTML = opt.inner;
}
if( opt.appendTo ) {
opt.appendTo.appendChild( el );
}
}
return el;
}
/**
* FForm function
*/
function FForm( el, options ) {
this.el = el;
this.options = extend( {}, this.options );
extend( this.options, options );
this._init();
}
/**
* FForm options
*/
FForm.prototype.options = {
onReview : function() { return false; }
};
/**
* init function
* initialize and cache some vars
*/
FForm.prototype._init = function() {
// the form element
this.formEl = this.el.querySelector( 'form' );
// list of fields
this.fieldsList = this.formEl.querySelector( 'ol.fs-fields' );
// current field position
this.current = 0;
// all fields
this.fields = [].slice.call( this.fieldsList.children );
// total fields
this.fieldsCount = this.fields.length;
// create/add messages
this._addErrorMsg();
};
/**
* addErrorMsg function
* create and insert the structure for the error message
*/
FForm.prototype._addErrorMsg = function() {
// error message
this.msgError = createElement( 'span', { cName : 'fs-message-error', appendTo : this.el } );
}
/**
* init events
*/
FForm.prototype._submit = function() {
console.log("submit");
};
/**
* progress function
* updates the progress bar by setting its width
*/
FForm.prototype._progress = function() {
if( this.options.ctrlProgress ) {
this.ctrlProgress.style.width = this.current * ( 100 / this.fieldsCount ) + '%';
}
}
// TODO: this is a very basic validation function. Only checks for required fields..
FForm.prototype._validade = function() {
console.log("validation");
var fld = this.fields[ this.current ],
input = fld.querySelector( 'input[required]' ) || fld.querySelector( 'textarea[required]' ) || fld.querySelector( 'select[required]' ),
error;
if( !input ) return true;
switch( input.tagName.toLowerCase() ) {
case 'input' :
if( input.type === 'radio' || input.type === 'checkbox' ) {
var checked = 0;
[].slice.call( fld.querySelectorAll( 'input[type="' + input.type + '"]' ) ).forEach( function( inp ) {
if( inp.checked ) {
++checked;
}
} );
if( !checked ) {
error = 'NOVAL';
}
}
else if( input.value === '' ) {
error = 'NOVAL';
}
break;
case 'select' :
// assuming here '' or '-1' only
if( input.value === '' || input.value === '-1' ) {
error = 'NOVAL';
}
break;
case 'textarea' :
if( input.value === '' ) {
error = 'NOVAL';
}
break;
}
if( error != undefined ) {
this._showError( error );
return false;
}
console.log("valid");
return true;
}
// TODO
FForm.prototype._showError = function( err ) {
var message = '';
switch( err ) {
case 'NOVAL' :
message = 'Merci de remplir ce champ';
break;
case 'INVALIDEMAIL' :
message = 'Please fill a valid email address';
break;
// ...
};
this.msgError.innerHTML = message;
this._showCtrl( this.msgError );
}
// clears/hides the current error message
FForm.prototype._clearError = function() {
this._hideCtrl( this.msgError );
}
// add to global namespace
window.FForm = FForm;
})( window );
0 commentaires :
Enregistrer un commentaire