Вход Регистрация
Файл: sngine-v2.8/Script/includes/assets/js/sngine/installer/fullscreenForm.js
Строк: 570
<?php
/**
 * fullscreenForm.js v1.0.0
 * http://www.codrops.com
 *
 * Licensed under the MIT license.
 * http://www.opensource.org/licenses/mit-license.php
 * 
 * Copyright 2014, Codrops
 * http://www.codrops.com
 */
;( function( window ) {
    
    
'use strict';

    var 
support = { animations Modernizr.cssanimations },
        
animEndEventNames = { 'WebkitAnimation' 'webkitAnimationEnd''OAnimation' 'oAnimationEnd''msAnimation' 'MSAnimationEnd''animation' 'animationend' },
        
// animation end event name
        
animEndEventName animEndEventNamesModernizr.prefixed'animation' ) ];

    
/**
     * extend obj function
     */
    
function extenda) {
        for( var 
key in b ) { 
            if( 
b.hasOwnPropertykey ) ) {
                
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 createElementtagopt ) {
        var 
el document.createElementtag )
        if( 
opt ) {
            if( 
opt.cName ) {
                
el.className opt.cName;
            }
            if( 
opt.inner ) {
                
el.innerHTML opt.inner;
            }
            if( 
opt.appendTo ) {
                
opt.appendTo.appendChildel );
            }
        }    
        return 
el;
    }

    
/**
     * FForm function
     */
    
function FFormeloptions ) {
        
this.el el;
        
this.options extend( {}, this.options );
          
extendthis.optionsoptions );
          
this._init();
    }

    
/**
     * FForm options
     */
    
FForm.prototype.options = {
        
// show progress bar
        
ctrlProgress true,
        
// show navigation dots
        
ctrlNavDots true,
        
// show [current field]/[total fields] status
        
ctrlNavPosition true,
        
// reached the review and submit step
        
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.callthis.fieldsList.children );
        
        
// total fields
        
this.fieldsCount this.fields.length;
        
        
// show first field
        
classie.addthis.fieldsthis.current ], 'fs-current' );

        
// create/add controls
        
this._addControls();

        
// create/add messages
        
this._addErrorMsg();
        
        
// init events
        
this._initEvents();
    };

    
/**
     * addControls function
     * create and insert the structure for the controls
     */
    
FForm.prototype._addControls = function() {
        
// main controls wrapper
        
this.ctrls createElement'div', { cName 'fs-controls'appendTo this.el } );

        
// continue button (jump to next field)
        
this.ctrlContinue createElement'button', { cName 'fs-continue'inner 'Continue'appendTo this.ctrls } );
        
this._showCtrlthis.ctrlContinue );

        
// navigation dots
        
if( this.options.ctrlNavDots ) {
            
this.ctrlNav createElement'nav', { cName 'fs-nav-dots'appendTo this.ctrls } );
            var 
dots '';
            for( var 
0this.fieldsCount; ++) {
                
dots += === this.current '<button class="fs-dot-current"></button>' '<button disabled></button>';
            }
            
this.ctrlNav.innerHTML dots;
            
this._showCtrlthis.ctrlNav );
            
this.ctrlNavDots = [].slice.callthis.ctrlNav.children );
        }

        
// field number status
        
if( this.options.ctrlNavPosition ) {
            
this.ctrlFldStatus createElement'span', { cName 'fs-numbers'appendTo this.ctrls } );

            
// current field placeholder
            
this.ctrlFldStatusCurr createElement'span', { cName 'fs-number-current'inner Numberthis.current ) } );
            
this.ctrlFldStatus.appendChildthis.ctrlFldStatusCurr );

            
// total fields placeholder
            
this.ctrlFldStatusTotal createElement'span', { cName 'fs-number-total'inner this.fieldsCount } );
            
this.ctrlFldStatus.appendChildthis.ctrlFldStatusTotal );
            
this._showCtrlthis.ctrlFldStatus );
        }

        
// progress bar
        
if( this.options.ctrlProgress ) {
            
this.ctrlProgress createElement'div', { cName 'fs-progress'appendTo this.ctrls } );
            
this._showCtrlthis.ctrlProgress );
        }
    }

    
/**
     * 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._initEvents = function() {
        var 
self this;

        
// show next field
        
this.ctrlContinue.addEventListener'click', function() {
            
self._nextField(); 
        } );

        
// navigation dots
        
if( this.options.ctrlNavDots ) {
            
this.ctrlNavDots.forEach( function( dotpos ) {
                
dot.addEventListener'click', function() {
                    
self._showFieldpos );
                } );
            } );
        }

        
// jump to next field without clicking the continue button (for fields/list items with the attribute "data-input-trigger")
        
this.fields.forEach( function( fld ) {
            if( 
fld.hasAttribute'data-input-trigger' ) ) {
                var 
input fld.querySelector'input[type="radio"]' ) || /*fld.querySelector( '.cs-select' ) ||*/ fld.querySelector'select' ); // assuming only radio and select elements (TODO: exclude multiple selects)
                
if( !input ) return;

                switch( 
input.tagName.toLowerCase() ) {
                    case 
'select' 
                        
input.addEventListener'change', function() { self._nextField(); } );
                        break;

                    case 
'input' 
                        [].
slice.callfld.querySelectorAll'input[type="radio"]' ) ).forEach( function( inp ) {
                            
inp.addEventListener'change', function(ev) { self._nextField(); } );
                        } ); 
                        break;

                    
/*
                    // for our custom select we would do something like:
                    case 'div' : 
                        [].slice.call( fld.querySelectorAll( 'ul > li' ) ).forEach( function( inp ) {
                            inp.addEventListener( 'click', function(ev) { self._nextField(); } );
                        } ); 
                        break;
                    */
                
}
            }
        } );

        
// keyboard navigation events - jump to next field when pressing enter
        
document.addEventListener'keydown', function( ev ) {
            if( !
self.isLastStep && ev.target.tagName.toLowerCase() !== 'textarea' ) {
                var 
keyCode ev.keyCode || ev.which;
                if( 
keyCode === 13 ) {
                    
ev.preventDefault();
                    
self._nextField();
                }
            }
        } );
    };

    
/**
     * nextField function
     * jumps to the next field
     */
    
FForm.prototype._nextField = function( backto ) {
        if( 
this.isLastStep || !this._validade() || this.isAnimating ) {
            return 
false;
        }
        
this.isAnimating true;

        
// check if on last step
        
this.isLastStep this.current === this.fieldsCount && backto === undefined true false;
        
        
// clear any previous error messages
        
this._clearError();

        
// current field
        
var currentFld this.fieldsthis.current ];

        
// save the navigation direction
        
this.navdir backto !== undefined backto this.current 'prev' 'next' 'next';

        
// update current field
        
this.current backto !== undefined backto this.current 1;

        if( 
backto === undefined ) {
            
// update progress bar (unless we navigate backwards)
            
this._progress();

            
// save farthest position so far
            
this.farthest this.current;
        }

        
// add class "fs-display-next" or "fs-display-prev" to the list of fields
        
classie.addthis.fieldsList'fs-display-' this.navdir );

        
// remove class "fs-current" from current field and add it to the next one
        // also add class "fs-show" to the next field and the class "fs-hide" to the current one
        
classie.removecurrentFld'fs-current' );
        
classie.addcurrentFld'fs-hide' );
        
        if( !
this.isLastStep ) {
            
// update nav
            
this._updateNav();

            
// change the current field number/status
            
this._updateFieldNumber();

            var 
nextField this.fieldsthis.current ];
            
classie.addnextField'fs-current' );
            
classie.addnextField'fs-show' );
        }

        
// after animation ends remove added classes from fields
        
var self this,
            
onEndAnimationFn = function( ev ) {
                if( 
support.animations ) {
                    
this.removeEventListeneranimEndEventNameonEndAnimationFn );
                }
                
                
classie.removeself.fieldsList'fs-display-' self.navdir );
                
classie.removecurrentFld'fs-hide' );

                if( 
self.isLastStep ) {
                    
// show the complete form and hide the controls
                    
self._hideCtrlself.ctrlNav );
                    
self._hideCtrlself.ctrlProgress );
                    
self._hideCtrlself.ctrlContinue );
                    
self._hideCtrlself.ctrlFldStatus );
                    
// replace class fs-form-full with fs-form-overview
                    
classie.removeself.formEl'fs-form-full' );
                    
classie.addself.formEl'fs-form-overview' );
                    
classie.addself.formEl'fs-show' );
                    
// callback
                    
self.options.onReview();
                }
                else {
                    
classie.removenextField'fs-show' );
                    
                    if( 
self.options.ctrlNavPosition ) {
                        
self.ctrlFldStatusCurr.innerHTML self.ctrlFldStatusNew.innerHTML;
                        
self.ctrlFldStatus.removeChildself.ctrlFldStatusNew );
                        
classie.removeself.ctrlFldStatus'fs-show-' self.navdir );
                    }
                }
                
self.isAnimating false;
            };

        if( 
support.animations ) {
            if( 
this.navdir === 'next' ) {
                if( 
this.isLastStep ) {
                    
currentFld.querySelector'.fs-anim-upper' ).addEventListeneranimEndEventNameonEndAnimationFn );
                }
                else {
                    
nextField.querySelector'.fs-anim-lower' ).addEventListeneranimEndEventNameonEndAnimationFn );
                }
            }
            else {
                
nextField.querySelector'.fs-anim-upper' ).addEventListeneranimEndEventNameonEndAnimationFn );
            }
        }
        else {
            
onEndAnimationFn();
        }
    }

    
/**
     * showField function
     * jumps to the field at position pos
     */
    
FForm.prototype._showField = function( pos ) {
        if( 
pos === this.current || pos || pos this.fieldsCount ) {
            return 
false;
        }
        
this._nextFieldpos );
    }

    
/**
     * updateFieldNumber function
     * changes the current field number
     */
    
FForm.prototype._updateFieldNumber = function() {
        if( 
this.options.ctrlNavPosition ) {
            
// first, create next field number placeholder
            
this.ctrlFldStatusNew document.createElement'span' );
            
this.ctrlFldStatusNew.className 'fs-number-new';
            
this.ctrlFldStatusNew.innerHTML Numberthis.current );
            
            
// insert it in the DOM
            
this.ctrlFldStatus.appendChildthis.ctrlFldStatusNew );
            
            
// add class "fs-show-next" or "fs-show-prev" depending on the navigation direction
            
var self this;
            
setTimeout( function() {
                
classie.addself.ctrlFldStatusself.navdir === 'next' 'fs-show-next' 'fs-show-prev' );
            }, 
25 );
        }
    }

    
/**
     * 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 ) + '%';
        }
    }

    
/**
     * updateNav function
     * updates the navigation dots
     */
    
FForm.prototype._updateNav = function() {
        if( 
this.options.ctrlNavDots ) {
            
classie.removethis.ctrlNav.querySelector'button.fs-dot-current' ), 'fs-dot-current' );
            
classie.addthis.ctrlNavDotsthis.current ], 'fs-dot-current' );
            
this.ctrlNavDotsthis.current ].disabled false;
        }
    }

    
/**
     * showCtrl function
     * shows a control
     */
    
FForm.prototype._showCtrl = function( ctrl ) {
        
classie.addctrl'fs-show' );
    }

    
/**
     * hideCtrl function
     * hides a control
     */
    
FForm.prototype._hideCtrl = function( ctrl ) {
        
classie.removectrl'fs-show' );
    }

    
// TODO: this is a very basic validation function. Only checks for required fields..
    
FForm.prototype._validade = function() {
        var 
fld this.fieldsthis.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.callfld.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._showErrorerror );
            return 
false;
        }

        return 
true;
    }

    
// TODO
    
FForm.prototype._showError = function( err ) {
        var 
message '';
        switch( 
err ) {
            case 
'NOVAL' 
                
message 'Please fill the field before continuing';
                break;
            case 
'INVALIDEMAIL' 
                
message 'Please fill a valid email address';
                break;
            
// ...
        
};
        
this.msgError.innerHTML message;
        
this._showCtrlthis.msgError );
    }

    
// clears/hides the current error message
    
FForm.prototype._clearError = function() {
        
this._hideCtrlthis.msgError );
    }

    
// add to global namespace
    
window.FForm FForm;

})( 
window );
?>
Онлайн: 0
Реклама