| 21627 |
kshitij.so |
1 |
/*!
|
|
|
2 |
* jQuery Stepy - A Wizard Plugin - http://wbotelhos.com/stepy
|
|
|
3 |
* ------------------------------------------------------------------------------------
|
|
|
4 |
*
|
|
|
5 |
* jQuery Stepy is a plugin based on FormToWizard that generates a customizable wizard.
|
|
|
6 |
*
|
|
|
7 |
* Licensed under The MIT License
|
|
|
8 |
*
|
|
|
9 |
* @version 1.0.0
|
|
|
10 |
* @since 2010.07.03
|
|
|
11 |
* @author Washington Botelho
|
|
|
12 |
* @documentation wbotelhos.com/stepy
|
|
|
13 |
* @twitter twitter.com/wbotelhos
|
|
|
14 |
*
|
|
|
15 |
* Usage with default values:
|
|
|
16 |
* ------------------------------------------------------------------------------------
|
|
|
17 |
* $('#step').stepy();
|
|
|
18 |
*
|
|
|
19 |
* <form id="step">
|
|
|
20 |
* <fieldset title="Step 1">
|
|
|
21 |
* <legend>description one</legend>
|
|
|
22 |
* <!-- input fields -->
|
|
|
23 |
* </fieldset>
|
|
|
24 |
*
|
|
|
25 |
* <fieldset title="Step 2">
|
|
|
26 |
* <legend>description one</legend>
|
|
|
27 |
* <!-- input fields -->
|
|
|
28 |
* </fieldset>
|
|
|
29 |
*
|
|
|
30 |
* <input type="submit" class="finish" value="Finish!"/>
|
|
|
31 |
* </form>
|
|
|
32 |
*
|
|
|
33 |
*/
|
|
|
34 |
|
|
|
35 |
;(function($) {
|
|
|
36 |
|
|
|
37 |
var methods = {
|
|
|
38 |
init: function(options) {
|
|
|
39 |
return this.each(function() {
|
|
|
40 |
|
|
|
41 |
var opt = $.extend({}, $.fn.stepy.defaults, options),
|
|
|
42 |
$this = $(this).data('options', opt),
|
|
|
43 |
id = $this.attr('id');
|
|
|
44 |
|
|
|
45 |
if (id === undefined || id == '') {
|
|
|
46 |
id = 'stepy-' + $('.' + $this.attr('class')).index(this);
|
|
|
47 |
$this.attr('id', id);
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
var $titlesWrapper = $('<ul/>', { id: id + '-titles', 'class': 'stepy-titles clearfix' });
|
|
|
51 |
|
|
|
52 |
if (opt.titleTarget) {
|
|
|
53 |
$(opt.titleTarget).html($titlesWrapper);
|
|
|
54 |
} else {
|
|
|
55 |
$titlesWrapper.insertBefore($this);
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
if (opt.validate) {
|
|
|
59 |
jQuery.validator.setDefaults({ ignore: opt.ignore });
|
|
|
60 |
|
|
|
61 |
$this.append('<div class="stepy-error clearfix"/>');
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
var $steps = $this.children('fieldset'),
|
|
|
65 |
$step = undefined,
|
|
|
66 |
$legend = undefined,
|
|
|
67 |
description = '',
|
|
|
68 |
title = '';
|
|
|
69 |
|
|
|
70 |
$steps.each(function(index) {
|
|
|
71 |
$step = $(this);
|
|
|
72 |
|
|
|
73 |
$step
|
|
|
74 |
.addClass('step')
|
|
|
75 |
.attr('id', id + '-step-' + index)
|
|
|
76 |
.append('<p id="' + id + '-buttons-' + index + '" class="' + id + '-buttons"/>');
|
|
|
77 |
|
|
|
78 |
$legend = $step.children('legend');
|
|
|
79 |
|
|
|
80 |
if (!opt.legend) {
|
|
|
81 |
$legend.hide();
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
description = '';
|
|
|
85 |
|
|
|
86 |
if (opt.description) {
|
|
|
87 |
if ($legend.length) {
|
|
|
88 |
description = '<span>' + $legend.html() + '</span>';
|
|
|
89 |
} else {
|
|
|
90 |
$.error(id + ': the legend element of the step ' + (index + 1) + ' is required to set the description!');
|
|
|
91 |
}
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
title = $step.attr('title');
|
|
|
95 |
title = (title != '') ? '<div>' + title + '</div>': '--';
|
|
|
96 |
|
|
|
97 |
$titlesWrapper.append('<li id="' + id + '-title-' + index + '">' + title + description + '</li>');
|
|
|
98 |
|
|
|
99 |
if (index == 0) {
|
|
|
100 |
if ($steps.length > 1) {
|
|
|
101 |
methods.createNextButton.call($this, index);
|
|
|
102 |
}
|
|
|
103 |
} else {
|
|
|
104 |
methods.createBackButton.call($this, index);
|
|
|
105 |
|
|
|
106 |
$step.hide();
|
|
|
107 |
|
|
|
108 |
if (index < $steps.length - 1) {
|
|
|
109 |
methods.createNextButton.call($this, index);
|
|
|
110 |
}
|
|
|
111 |
}
|
|
|
112 |
});
|
|
|
113 |
|
|
|
114 |
var $titles = $titlesWrapper.children();
|
|
|
115 |
|
|
|
116 |
$titles.first().addClass('current-step');
|
|
|
117 |
|
|
|
118 |
var $finish = $this.children('.finish');
|
|
|
119 |
|
|
|
120 |
if (opt.finishButton) {
|
|
|
121 |
if ($finish.length) {
|
|
|
122 |
var isForm = $this.is('form'),
|
|
|
123 |
onSubmit = undefined;
|
|
|
124 |
|
|
|
125 |
if (opt.finish && isForm) {
|
|
|
126 |
onSubmit = $this.attr('onsubmit');
|
|
|
127 |
$this.attr('onsubmit', 'return false;');
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
$finish.click(function(evt) {
|
|
|
131 |
if (opt.finish && !methods.execute.call($this, opt.finish, $steps.length - 1)) {
|
|
|
132 |
evt.preventDefault();
|
|
|
133 |
} else {
|
|
|
134 |
if (isForm) {
|
|
|
135 |
if (onSubmit) {
|
|
|
136 |
$this.attr('onsubmit', onSubmit);
|
|
|
137 |
} else {
|
|
|
138 |
$this.removeAttr('onsubmit');
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
var isSubmit = $finish.attr('type') == 'submit';
|
|
|
142 |
|
|
|
143 |
if (!isSubmit && (!opt.validate || methods.validate.call($this, $steps.length - 1))) {
|
|
|
144 |
$this.submit();
|
|
|
145 |
}
|
|
|
146 |
}
|
|
|
147 |
}
|
|
|
148 |
});
|
|
|
149 |
|
|
|
150 |
$finish.appendTo($this.find('p:last'));
|
|
|
151 |
} else {
|
|
|
152 |
$.error(id + ': element with class name "finish" missing!');
|
|
|
153 |
}
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
if (opt.titleClick) {
|
|
|
157 |
$titles.click(function() {
|
|
|
158 |
var array = $titles.filter('.current-step').attr('id').split('-'), // TODO: try keep the number in an attribute.
|
|
|
159 |
current = parseInt(array[array.length - 1], 10),
|
|
|
160 |
clicked = $(this).index();
|
|
|
161 |
|
|
|
162 |
if (clicked > current) {
|
|
|
163 |
if (opt.next && !methods.execute.call($this, opt.next, clicked)) {
|
|
|
164 |
return false;
|
|
|
165 |
}
|
|
|
166 |
} else if (clicked < current) {
|
|
|
167 |
if (opt.back && !methods.execute.call($this, opt.back, clicked)) {
|
|
|
168 |
return false;
|
|
|
169 |
}
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
if (clicked != current) {
|
|
|
173 |
methods.step.call($this, (clicked) + 1);
|
|
|
174 |
}
|
|
|
175 |
});
|
|
|
176 |
} else {
|
|
|
177 |
$titles.css('cursor', 'default');
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
$steps.delegate('input[type="text"], input[type="password"]', 'keypress', function(evt) {
|
|
|
181 |
var key = (evt.keyCode ? evt.keyCode : evt.which);
|
|
|
182 |
|
|
|
183 |
if (key == 13) {
|
|
|
184 |
evt.preventDefault();
|
|
|
185 |
|
|
|
186 |
var $buttons = $(this).parent().children('.' + id + '-buttons');
|
|
|
187 |
|
|
|
188 |
if ($buttons.length) {
|
|
|
189 |
var $next = $buttons.children('.button-next');
|
|
|
190 |
|
|
|
191 |
if ($next.length) {
|
|
|
192 |
$next.click();
|
|
|
193 |
} else {
|
|
|
194 |
var $finish = $buttons.children('.finish');
|
|
|
195 |
|
|
|
196 |
if ($finish.length) {
|
|
|
197 |
$finish.click();
|
|
|
198 |
}
|
|
|
199 |
}
|
|
|
200 |
}
|
|
|
201 |
}
|
|
|
202 |
});
|
|
|
203 |
|
|
|
204 |
$steps.first().find(':input:visible:enabled').first().select().focus();
|
|
|
205 |
});
|
|
|
206 |
}, createBackButton: function(index) {
|
|
|
207 |
var $this = this,
|
|
|
208 |
id = this.attr('id'),
|
|
|
209 |
opt = this.data('options');
|
|
|
210 |
|
|
|
211 |
$('<a/>', { id: id + '-back-' + index, href: 'javascript:void(0);', 'class': 'button-back btn btn-info', html: opt.backLabel }).click(function() {
|
|
|
212 |
if (!opt.back || methods.execute.call($this, opt.back, index - 1)) {
|
|
|
213 |
methods.step.call($this, (index - 1) + 1);
|
|
|
214 |
}
|
|
|
215 |
}).appendTo($('#' + id + '-buttons-' + index));
|
|
|
216 |
}, createNextButton: function(index) {
|
|
|
217 |
var $this = this,
|
|
|
218 |
id = this.attr('id'),
|
|
|
219 |
opt = this.data('options');
|
|
|
220 |
|
|
|
221 |
$('<a/>', { id: id + '-next-' + index, href: 'javascript:void(0);', 'class': 'button-next btn btn-info', html: opt.nextLabel }).click(function() {
|
|
|
222 |
if (!opt.next || methods.execute.call($this, opt.next, index + 1)) {
|
|
|
223 |
methods.step.call($this, (index + 1) + 1);
|
|
|
224 |
}
|
|
|
225 |
}).appendTo($('#' + id + '-buttons-' + index));
|
|
|
226 |
}, execute: function(callback, index) {
|
|
|
227 |
var isValid = callback.call(this, index + 1);
|
|
|
228 |
|
|
|
229 |
return isValid || isValid === undefined;
|
|
|
230 |
}, step: function(index) {
|
|
|
231 |
index--;
|
|
|
232 |
|
|
|
233 |
var $steps = this.children('fieldset');
|
|
|
234 |
|
|
|
235 |
if (index > $steps.length - 1) {
|
|
|
236 |
index = $steps.length - 1;
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
var opt = this.data('options');
|
|
|
240 |
max = index;
|
|
|
241 |
|
|
|
242 |
if (opt.validate) {
|
|
|
243 |
var isValid = true;
|
|
|
244 |
|
|
|
245 |
for (var i = 0; i < index; i++) {
|
|
|
246 |
isValid &= methods.validate.call(this, i);
|
|
|
247 |
|
|
|
248 |
if (opt.block && !isValid) {
|
|
|
249 |
max = i;
|
|
|
250 |
break;
|
|
|
251 |
}
|
|
|
252 |
}
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
$steps.hide().eq(max).show();
|
|
|
256 |
|
|
|
257 |
var $titles = $('#' + this.attr('id') + '-titles').children();
|
|
|
258 |
|
|
|
259 |
$titles.removeClass('current-step').eq(max).addClass('current-step');
|
|
|
260 |
|
|
|
261 |
if (this.is('form')) {
|
|
|
262 |
var $fields = undefined;
|
|
|
263 |
|
|
|
264 |
if (max == index) {
|
|
|
265 |
$fields = $steps.eq(max).find(':input:enabled:visible');
|
|
|
266 |
} else {
|
|
|
267 |
$fields = $steps.eq(max).find('.error').select().focus();
|
|
|
268 |
}
|
|
|
269 |
|
|
|
270 |
$fields.first().select().focus();
|
|
|
271 |
}
|
|
|
272 |
|
|
|
273 |
if (opt.select) {
|
|
|
274 |
opt.select.call(this, max + 1);
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
return this;
|
|
|
278 |
}, validate: function(index) {
|
|
|
279 |
if (!this.is('form')) {
|
|
|
280 |
return true;
|
|
|
281 |
}
|
|
|
282 |
|
|
|
283 |
var $step = this.children('fieldset').eq(index),
|
|
|
284 |
isValid = true,
|
|
|
285 |
$title = $('#' + this.attr('id') + '-titles').children().eq(index),
|
|
|
286 |
opt = this.data('options'),
|
|
|
287 |
$validate = this.validate();
|
|
|
288 |
|
|
|
289 |
$($step.find(':input:enabled').get().reverse()).each(function() {
|
|
|
290 |
var fieldIsValid = $validate.element($(this));
|
|
|
291 |
|
|
|
292 |
if (fieldIsValid === undefined) {
|
|
|
293 |
fieldIsValid = true;
|
|
|
294 |
}
|
|
|
295 |
|
|
|
296 |
isValid &= fieldIsValid;
|
|
|
297 |
|
|
|
298 |
if (isValid) {
|
|
|
299 |
if (opt.errorImage) {
|
|
|
300 |
$title.removeClass('error-image');
|
|
|
301 |
}
|
|
|
302 |
} else {
|
|
|
303 |
if (opt.errorImage) {
|
|
|
304 |
$title.addClass('error-image');
|
|
|
305 |
}
|
|
|
306 |
|
|
|
307 |
$validate.focusInvalid();
|
|
|
308 |
}
|
|
|
309 |
});
|
|
|
310 |
|
|
|
311 |
return isValid;
|
|
|
312 |
}
|
|
|
313 |
};
|
|
|
314 |
|
|
|
315 |
$.fn.stepy = function(method) {
|
|
|
316 |
if (methods[method]) {
|
|
|
317 |
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
|
|
|
318 |
} else if (typeof method === 'object' || !method) {
|
|
|
319 |
return methods.init.apply(this, arguments);
|
|
|
320 |
} else {
|
|
|
321 |
$.error('Method ' + method + ' does not exist!');
|
|
|
322 |
}
|
|
|
323 |
};
|
|
|
324 |
|
|
|
325 |
$.fn.stepy.defaults = {
|
|
|
326 |
back: undefined,
|
|
|
327 |
backLabel: '< Back',
|
|
|
328 |
block: false,
|
|
|
329 |
description: true,
|
|
|
330 |
errorImage: false,
|
|
|
331 |
finish: undefined,
|
|
|
332 |
finishButton: true,
|
|
|
333 |
legend: true,
|
|
|
334 |
ignore: '',
|
|
|
335 |
next: undefined,
|
|
|
336 |
nextLabel: 'Next >',
|
|
|
337 |
titleClick: false,
|
|
|
338 |
titleTarget: undefined,
|
|
|
339 |
validate: false,
|
|
|
340 |
select: undefined
|
|
|
341 |
};
|
|
|
342 |
|
|
|
343 |
})(jQuery);
|