Repository navigation
Expand file tree
/
Copy pathjquery.wizard.js
More file actions
1 lines (1 loc) · 8.25 KB
/
Copy pathjquery.wizard.js
File metadata and controls
1 lines (1 loc) · 8.25 KB
1
!function(t,e){var s=0,n={},r={},i=Array.prototype.slice,a=function(e){return t.isArray(e)?e:[e]},o="id",c="form",p="disabled",d="wizard",l="default",h="number",u="object",f="string",b="boolean",m="afterBackward",_="afterDestroy",v="afterForward",g="afterSelect",w="beforeBackward",y="beforeDestroy",x="beforeForward",A="beforeSelect";t.each("branch form header step wrapper".split(" "),function(){n[this]="."+(r[this]=d+"-"+this)}),t.widget("kf."+d,{version:"1.1.0",options:{animations:{show:{options:{duration:0},properties:{opacity:"show"}},hide:{options:{duration:0},properties:{opacity:"hide"}}},backward:".backward",branches:".branch",disabled:!1,enableSubmit:!1,forward:".forward",header:":header:first",initialStep:0,stateAttribute:"data-state",stepClasses:{current:"current",exclude:"exclude",stop:"stop",submit:"submit",unidirectional:"unidirectional"},steps:".step",submit:":submit",transitions:{},unidirectional:!1,afterBackward:null,afterDestroy:null,afterForward:null,afterSelect:null,beforeBackward:null,beforeDestroy:null,beforeForward:null,beforeSelect:null,create:null},_create:function(){var e,i,p=this,h=p.options,u=p.element,f=u.find(h.steps),b=f.eq(0).parent();u[0].elements?e=u:(e=u.find(c)).length||(e=u.closest(c)),(i=u.find(h.header)).length||(i=e.find(h.header)),p.elements={form:e.addClass(r.form),submit:e.find(h.submit),forward:e.find(h.forward),backward:e.find(h.backward),header:i.addClass(r.header),steps:u.find(h.steps).hide().addClass(r.step),branches:u.find(h.branches).add(b).addClass(r.branch),stepsWrapper:b.addClass(r.wrapper),wizard:u.addClass(d)},b.attr(o)||b.attr(o,d+"-"+ ++s),p.elements.forward.click(function(t){t.preventDefault(),p.forward(t)}),p.elements.backward.click(function(t){t.preventDefault(),p.backward(t)}),p._currentState={branchesActivated:[],stepsActivated:[]},p._stepCount=p.elements.steps.length,p._lastStepIndex=p._stepCount-1,p._branchLabels=[],p.elements.steps.each(function(e){p._branchLabels[e]=t(this).parent().attr(o)}),p._excludesFilter=function(){return!t(this).hasClass(h.stepClasses.exclude)},h.transitions[l]||(h.transitions[l]=function(t){return p.stepIndex(t.step.nextAll(n.step))}),p.select.apply(p,a(h.initialStep))},_fastForward:function(s,n,r){var i=0,a=this,o=a._currentState.stepIndex,c=[o];t.isFunction(n)&&(r=n,n=e),function p(){a._transition(a._state(o,c),function(e,d){if(-1===(o=a.stepIndex(e,d)))throw new Error('[_fastForward]: Invalid step "'+e+'"');if(t.inArray(o,c)>=0)throw new Error('[_fastForward]: Recursion detected on step "'+e+'"');c.push(o),o===a._lastStepIndex||(n?++i:o)===s?r.call(a,o,c):p()})}()},_find:function(e,s,n){function r(t,e){return e===p?(i=e,!1):void 0}var i,o,c,p,d,l=[],b=s instanceof jQuery?s:t(s);if(null!==e&&b.length)for(e=a(e),o=0,c=e.length;c>o;o++)i=null,p=e[o],d=typeof p,d===h?i=b.get(p):d===f?i=document.getElementById(p.replace("#","")):d===u&&(p instanceof jQuery&&p.length&&(p=p[0]),p.nodeType&&b.each(r)),i&&l.push(i);return n===!1?l:t(l)},_move:function(s,n,r,i,a){function o(s,n){a.call(c,s,t.isArray(i)?i:i!==!1?n:e)}var c=this,p=c._currentState;typeof n===b&&(a=i,i=r,r=n,n=e),r===!0?s>0?c._fastForward(s,r,o):a.call(c,p.stepsActivated[Math.max(0,s+(p.stepsActivated.length-1))]):-1!==(s=c.stepIndex(s,n))&&(s>p.stepIndex?c._fastForward(s,o):o.call(c,s))},_state:function(e,s){if(!this.isValidStepIndex(e))return null;var r=(this.options,t.extend(!0,{},this._currentState));s=a(s||e),r.step=this.elements.steps.eq(e),r.branch=r.step.parent(),r.branchStepCount=r.branch.children(n.step).length,r.isMovingForward=e>r.stepIndex,r.stepIndexInBranch=r.branch.children(n.step).index(r.step);for(var i,o,c,p=0,d=s.length;d>p;p++)e=s[p],i=this._branchLabels[e],!r.stepIndex||r.stepIndex<e?t.inArray(e,r.stepsActivated)<0&&(r.stepsActivated.push(e),t.inArray(i,r.branchesActivated)<0&&r.branchesActivated.push(i)):r.stepIndex>e&&(o=t.inArray(i,r.branchesActivated)+1,c=t.inArray(e,r.stepsActivated)+1,o>0&&r.branchesActivated.splice(o,r.branchesActivated.length-1),c>0&&r.stepsActivated.splice(c,r.stepsActivated.length-1)),r.stepIndex=e,r.branchLabel=i;return r.stepsComplete=Math.max(0,this._find(r.stepsActivated,this.elements.steps).filter(this._excludesFilter).length-1),r.stepsPossible=Math.max(0,this._find(r.branchesActivated,this.elements.branches).children(n.step).filter(this._excludesFilter).length-1),t.extend(r,{branchLabel:this._branchLabels[e],isFirstStep:0===e,isFirstStepInBranch:0===r.stepIndexInBranch,isLastStep:e===this._lastStepIndex,isLastStepInBranch:r.stepIndexInBranch===r.branchStepCount-1,percentComplete:100*r.stepsComplete/r.stepsPossible,stepsRemaining:r.stepsPossible-r.stepsComplete}),r},_transition:function(s,n){var r,o=this,c=o.options,p=s.step.attr(c.stateAttribute),d=p?c.transitions[p]:c.transitions[l];r=t.isFunction(d)?d.call(o,s,function(){return n.apply(o,i.call(arguments))}):p,r!==e&&r!==!1&&n.apply(o,a(r))},_update:function(e,s,n){var r=this,i=r._currentState,a=[s,function(t){r._update(e,s,t!==!1)}],o=r.options;if(i.step){if(!s||o.disabled||s.stepIndex===i.stepIndex||n!==!0&&(!this._trigger(A,e,a)||s.isMovingForward&&!this._trigger(x,e,a)||!s.isMovingForward&&!this._trigger(w,e,a)))return;i.step.removeClass(o.stepClasses.current).animate(o.animations.hide.properties,t.extend({},o.animations.hide.options))}this._currentState=s,s.step.addClass(o.stepClasses.current).animate(o.animations.show.properties,t.extend({},o.animations.show.options)),s.isFirstStep||o.unidirectional||s.step.hasClass(o.stepClasses.unidirectional)?this.elements.backward.attr(p,!0):this.elements.backward.removeAttr(p),s.isLastStepInBranch&&!s.step.attr(o.stateAttribute)||s.step.hasClass(o.stepClasses.stop)?this.elements.forward.attr(p,!0):this.elements.forward.removeAttr(p),o.enableSubmit||s.step.hasClass(o.stepClasses.submit)?this.elements.submit.removeAttr(p):this.elements.submit.attr(p,!0),i.step&&(this._trigger(g,e,s),this._trigger(s.isMovingForward?v:m,e,s))},backward:function(t,s){typeof t===h&&(s=t,t=e),s===e&&(s=1),this._currentState.isFirstStep||typeof s!==h||this._move(-s,!0,!1,function(e,s){this._update(t,this._state(e,s))})},branch:function(t){return arguments.length?this._find(t,this.elements.branches):this._currentState.branch},branches:function(t){return arguments.length?this.branch(t).children(n.branch):this.elements.branches},branchesActivated:function(){return this._find(this._currentState.branchesActivated,this.elements.branches)},destroy:function(s,n){var i=this,a=i.elements,o=[i.state(),function(t){return i.destroy(s,t!==!1)}];typeof s===b&&(n=s,s=e),(n===!0||i._trigger(y,s,o))&&(i.element.removeClass(d),a.form.removeClass(r.form),a.header.removeClass(r.header),a.steps.show().removeClass(r.step),a.stepsWrapper.removeClass(r.wrapper),a.branches.removeClass(r.branch),t.Widget.prototype.destroy.call(i),i._trigger(_))},form:function(){return this.elements.form},forward:function(t,s,n){typeof t===h&&(n=s,s=t,t=e),s===e&&(s=1),this._currentState.isLastStep||typeof s!==h||this._move(s,!0,n,function(e,s){this._update(t,this._state(e,s))})},isValidStep:function(t,e){return this.isValidStepIndex(this.stepIndex(t,e))},isValidStepIndex:function(t){return typeof t===h&&t>=0&&t<=this._lastStepIndex},stepCount:function(){return this._stepCount},select:function(s,n,r,i,a){s instanceof t.Event||(a=i,i=r,r=n,n=s,s=e),n!==e&&(t.isArray(n)?(a=i,i=r,r=n[1],n=n[0]):typeof r===b?(a=i,i=r,r=e):t.isArray(r)&&(a=r,r=e),this._move(n,r,i,a,function(t,e){this._update(s,this._state(t,e))}))},state:function(s,n,r){return arguments.length?(t.isArray(s)?(r=n,n=s[1],s=s[0]):t.isArray(n)&&(r=n,n=e),this._state(this.stepIndex(s,n),r)):this._currentState},step:function(s,n){if(!arguments.length)return this._currentState.step;t.isArray(s)&&(n=s[1],s=s[0]);var i,a=typeof s;return a===h?i=this._find(s,n!==e?this.steps(n):this.elements.steps):(i=this._find(s,this.elements.steps.add(this.elements.branches)),i&&i.hasClass(r.branch)&&(i=this._find(n||0,this.steps(i)))),i},stepIndex:function(s,r,i){if(!arguments.length)return this._currentState.stepIndex;var a;return t.isArray(s)?(i=r,r=s[1],s=s[0]):typeof r===b&&(i=r,r=e),(a=this.step(s,r))?(i?a.siblings(n.step).andSelf():this.elements.steps).index(a):-1},steps:function(t){return arguments.length?this.branch(t).children(n.step):this.elements.steps},stepsActivated:function(){return this._find(this._currentState.stepsActivated,this.elements.steps)},submit:function(){this.elements.form.submit()}})}(jQuery);