-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultisteps.js
200 lines (162 loc) · 4.79 KB
/
multisteps.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
( function( $ ) {
var Multisteps = function( $el, options ) {
$el.addClass( 'multisteps' );
// set some defaults
if ( typeof options === 'undefined' ) options = {};
var defaults = {
start: 0,
loop: false,
orientation: 'vertical',
animateIn: 'fadeInUp',
animateOut: 'fadeOutUp',
onChange: function () {},
onAfterChange: function () {}
};
for (var option in defaults)
if (typeof options[option] === 'undefined')
options[option] = defaults[option];
// check animation support
var support = function() {
var animation = false,
animationstring = 'animation',
keyframeprefix = '',
domPrefixes = 'Webkit Moz O ms Khtml'.split(' '),
pfx = '',
elm = document.body;
if( elm.style.animationName !== undefined ) { animation = true; }
if( animation === false ) {
for( var i = 0; i < domPrefixes.length; i++ ) {
if( elm.style[ domPrefixes[i] + 'AnimationName' ] !== undefined ) {
pfx = domPrefixes[ i ];
animationstring = pfx + 'Animation';
keyframeprefix = '-' + pfx.toLowerCase() + '-';
animation = true;
break;
}
}
}
return {
supported: animation,
prefix: pfx,
onEnd: pfx.length === 0 ? 'animationend' : pfx.toLowerCase() + 'AnimationEnd'
};
};
var animation = support();
// define methods
var steps = function() { return $el.children( '.step' ); };
var current = function() { return steps().filter( '.actual-active' ); }
var index = function() { return current().index(); };
var size = function() { return steps().length; };
var hasPrev = function() { return index() === 0; };
var hasNext = function() { return index() === size() - 1; };
var go = function (to) {
var now = current();
var then = typeof to === 'number' ? steps().eq(to) : steps().filter('#' + to);
if ( now.size() === 0 || then.size() === 0 ) {
console.error( 'multisteps: Target step "' + to + '" doesn\'t exist.' )
return;
}
now.removeClass( 'actual-active' );
then.addClass( 'actual-active' );
if ( animation.supported ) {
now.add( then ).addClass( 'animated ' + options.animateIn );
now.addClass( options.animateOut );
now.one( animation.onEnd, function() {
now.removeClass( 'active ' + options.animateOut );
then.addClass( 'preparing' );
setTimeout( function() {
if ( options.orientation === 'vertical' ) {
$el.css( 'height', then.innerHeight() + 2 );
} else {
$el.css( 'width', then.innerWidth() + 2 );
}
}, 0 );
$el.one( animation.onEnd, function() {
then.removeClass( 'preparing' ).addClass( 'active' );
options.onAfterChange( then.index(), now.index(), size() );
} );
options.onChange( then.index(), now.index(), size() );
} );
} else {
now.removeClass( 'active' );
then.addClass( 'active' );
options.onChange( then.index(), now.index(), size() );
options.onAfterChange( then.index(), now.index(), size() );
}
};
var prev = function() {
var i = index();
if ( i === 0 ) {
go( i - 1 );
} else {
if ( options.loop ) {
last();
}
}
};
var next = function() {
var i = index();
if ( i == size() - 1 ) {
if ( options.loop ) {
this.first();
}
} else {
this.goto( i + 1 );
}
};
var first = function() {
this.goto( 0 );
};
var last = function() {
this.goto( size() - 1 );
};
var refresh = function() {
if ( options.orientation === 'vertical' )
$el.css( 'height', current().innerHeight() + 2 );
};
var startingStep = typeof options.start === 'number'
? steps().eq( options.start )
: steps().filter('#' + options.start);
startingStep.addClass( 'active actual-active' );
if ( options.orientation === 'vertical' ) {
$el.addClass( 'vertical' );
$el.css( 'height', startingStep.innerHeight() );
} else {
$el.addClass( 'horizontal' );
$el.css( 'width', startingStep.innerWidth() );
}
$( window ).resize( function() { self.refresh(); } );
var self = this;
// attach methods to controller
this.current = current;
this.currentIndex = index;
this.hasPrev = hasPrev;
this.hasNext = hasNext;
this.goto = go;
this.prev = prev;
this.next = next;
this.first = first;
this.last = last;
this.refresh = refresh;
};
// wrap in jQuery plugin
$.fn.multisteps = function() {
var args = Array.prototype.slice.apply( arguments );
var returnValue = this.each( function() {
$el = $( this );
if ( typeof args[ 0 ] === 'object' || typeof args[ 0 ] === 'undefined' ) {
var options = args[ 0 ];
var ctrl = new Multisteps( $el, options );
$el.data( 'multisteps', ctrl );
} else {
if ( typeof $el.data( 'multisteps' ) === 'undefined' ) return;
var ctrl = $el.data( 'multisteps' );
var command = args[ 0 ];
var parameters = args.slice( 1 );
if ( typeof ctrl[ command ] === 'undefined' ) return;
return ctrl[ command ].apply( ctrl, parameters );
}
});
return returnValue;
};
} )( jQuery );