jquery.smartWizard-2.0.js
15.9 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/* SmartWizard 2.0 plugin
* jQuery Wizard control Plugin
* http://www.techlaboratory.net/smartwizard
*
* by Dipu Raj
* http://dipuraj.me
*
* License
* https://github.com/techlab/SmartWizard/blob/master/MIT-LICENSE.txt
*
*/
(function($){
$.fn.smartWizard = function(action) {
var options = $.extend({}, $.fn.smartWizard.defaults, action);
var args = arguments;
return this.each(function(){
var obj = $(this);
var curStepIdx = options.selected;
var steps = $("ul > li > a[href^='#step-']", obj); // Get all anchors in this array
var contentWidth = 0;
var loader,msgBox,elmActionBar,elmStepContainer,btNext,btPrevious,btFinish;
elmActionBar = $('.actionBar',obj);
if(elmActionBar.length == 0){
elmActionBar = $('<div></div>').addClass("actionBar");
}
msgBox = $('.msgBox',obj);
if(msgBox.length == 0){
msgBox = $('<div class="msgBox"><div class="content"></div><a href="#" class="close">X</a></div>');
elmActionBar.append(msgBox);
}
$('.close',msgBox).click(function() {
msgBox.fadeOut("normal");
return false;
});
// Method calling logic
if (!action || action === 'init' || typeof action === 'object') {
init();
}else if (action === 'showMessage') {
//showMessage(Array.prototype.slice.call( args, 1 ));
var ar = Array.prototype.slice.call( args, 1 );
showMessage(ar[0]);
return true;
}else if (action === 'setError') {
var ar = Array.prototype.slice.call( args, 1 );
setError(ar[0].stepnum,ar[0].iserror);
return true;
} else {
$.error( 'Method ' + action + ' does not exist' );
}
function init(){
var allDivs =obj.children('div'); //$("div", obj);
obj.children('ul').addClass("anchor");
allDivs.addClass("content");
// Create Elements
loader = $('<div>Loading</div>').addClass("loader");
elmActionBar = $('<div></div>').addClass("actionBar");
elmStepContainer = $('<div></div>').addClass("stepContainer");
btNext = $('<a>'+options.labelNext+'</a>').attr("href","#").addClass("buttonNext");
btPrevious = $('<a>'+options.labelPrevious+'</a>').attr("href","#").addClass("buttonPrevious");
btFinish = $('<a>'+options.labelFinish+'</a>').attr("href","#").addClass("buttonFinish");
// highlight steps with errors
if(options.errorSteps && options.errorSteps.length>0){
$.each(options.errorSteps, function(i, n){
setError(n,true);
});
}
elmStepContainer.append(allDivs);
elmActionBar.append(loader);
obj.append(elmStepContainer);
obj.append(elmActionBar);
if (options.includeFinishButton) {
elmActionBar.append(btFinish);
}
elmActionBar.append(btNext).append(btPrevious);
contentWidth = elmStepContainer.width();
$(btNext).click(function() {
if($(this).hasClass('buttonDisabled')){
return false;
}
doForwardProgress();
return false;
});
$(btPrevious).click(function() {
if($(this).hasClass('buttonDisabled')){
return false;
}
doBackwardProgress();
return false;
});
$(btFinish).click(function() {
if(!$(this).hasClass('buttonDisabled')){
if($.isFunction(options.onFinish)) {
if(!options.onFinish.call(this,$(steps))){
return false;
}
}else{
var frm = obj.parents('form');
if(frm && frm.length){
frm.submit();
}
}
}
return false;
});
$(steps).bind("click", function(e){
if(steps.index(this) == curStepIdx){
return false;
}
var nextStepIdx = steps.index(this);
var isDone = steps.eq(nextStepIdx).attr("isDone") - 0;
if(isDone == 1){
LoadContent(nextStepIdx);
}
return false;
});
// Enable keyboard navigation
if(options.keyNavigation){
$(document).keyup(function(e){
if(e.which==39){ // Right Arrow
doForwardProgress();
}else if(e.which==37){ // Left Arrow
doBackwardProgress();
}
});
}
// Prepare the steps
prepareSteps();
// Show the first slected step
LoadContent(curStepIdx);
}
function prepareSteps(){
if(!options.enableAllSteps){
$(steps, obj).removeClass("selected").removeClass("done").addClass("disabled");
$(steps, obj).attr("isDone",0);
}else{
$(steps, obj).removeClass("selected").removeClass("disabled").addClass("done");
$(steps, obj).attr("isDone",1);
}
$(steps, obj).each(function(i){
$($(this).attr("href"), obj).hide();
$(this).attr("rel",i+1);
});
}
function LoadContent(stepIdx){
var selStep = steps.eq(stepIdx);
var ajaxurl = options.contentURL;
var hasContent = selStep.data('hasContent');
stepNum = stepIdx+1;
if(ajaxurl && ajaxurl.length>0){
if(options.contentCache && hasContent){
showStep(stepIdx);
}else{
$ajaxsatatus = $.ajax({
url: ajaxurl,
type: "POST",
data: ({step_number : stepNum}),
dataType: "text",
beforeSend: function(){ loader.show(); },
error: function(){loader.hide();},
success: function(res){
loader.hide();
if(res && res.length>0){
selStep.data('hasContent',true);
$($(selStep, obj).attr("href"), obj).html(res);
showStep(stepIdx);
}
if (options.onAjaxLoadComplete != undefined) {
options.onAjaxLoadComplete();
}
}
});
}
}else{
showStep(stepIdx);
}
}
function showStep(stepIdx){
var selStep = steps.eq(stepIdx);
var curStep = steps.eq(curStepIdx);
if(stepIdx != curStepIdx){
if($.isFunction(options.onLeaveStep)) {
if(!options.onLeaveStep.call(this,$(curStep))){
return false;
}
}
}
if (options.updateHeight)
elmStepContainer.height($($(selStep, obj).attr("href"), obj).outerHeight());
if(options.transitionEffect == 'slide'){
$($(curStep, obj).attr("href"), obj).slideUp("fast",function(e){
$($(selStep, obj).attr("href"), obj).slideDown("fast");
curStepIdx = stepIdx;
SetupStep(curStep,selStep);
});
} else if(options.transitionEffect == 'fade'){
$($(curStep, obj).attr("href"), obj).fadeOut("fast",function(e){
$($(selStep, obj).attr("href"), obj).fadeIn("fast");
curStepIdx = stepIdx;
SetupStep(curStep,selStep);
});
} else if(options.transitionEffect == 'slideleft'){
var nextElmLeft = 0;
var curElementLeft = 0;
if(stepIdx > curStepIdx){
nextElmLeft1 = contentWidth + 10;
nextElmLeft2 = 0;
curElementLeft = 0 - $($(curStep, obj).attr("href"), obj).outerWidth();
} else {
nextElmLeft1 = 0 - $($(selStep, obj).attr("href"), obj).outerWidth() + 20;
nextElmLeft2 = 0;
curElementLeft = 10 + $($(curStep, obj).attr("href"), obj).outerWidth();
}
if(stepIdx == curStepIdx){
nextElmLeft1 = $($(selStep, obj).attr("href"), obj).outerWidth() + 20;
nextElmLeft2 = 0;
curElementLeft = 0 - $($(curStep, obj).attr("href"), obj).outerWidth();
}else{
$($(curStep, obj).attr("href"), obj).animate({left:curElementLeft},"fast",function(e){
$($(curStep, obj).attr("href"), obj).hide();
});
}
$($(selStep, obj).attr("href"), obj).css("left",nextElmLeft1);
$($(selStep, obj).attr("href"), obj).show();
$($(selStep, obj).attr("href"), obj).animate({left:nextElmLeft2},"fast",function(e){
curStepIdx = stepIdx;
SetupStep(curStep,selStep);
});
} else{
$($(curStep, obj).attr("href"), obj).hide();
$($(selStep, obj).attr("href"), obj).show();
curStepIdx = stepIdx;
SetupStep(curStep,selStep);
}
return true;
}
function SetupStep(curStep,selStep){
$(curStep, obj).removeClass("selected");
$(curStep, obj).addClass("done");
$(selStep, obj).removeClass("disabled");
$(selStep, obj).removeClass("done");
$(selStep, obj).addClass("selected");
$(selStep, obj).attr("isDone",1);
adjustButton();
if($.isFunction(options.onShowStep)) {
if(!options.onShowStep.call(this,$(selStep))){
return false;
}
}
}
function doForwardProgress(){
var nextStepIdx = curStepIdx + 1;
if(steps.length <= nextStepIdx){
if(!options.cycleSteps){
return false;
}
nextStepIdx = 0;
}
LoadContent(nextStepIdx);
}
function doBackwardProgress(){
var nextStepIdx = curStepIdx-1;
if(0 > nextStepIdx){
if(!options.cycleSteps){
return false;
}
nextStepIdx = steps.length - 1;
}
LoadContent(nextStepIdx);
}
function adjustButton(){
if(!options.cycleSteps){
if(0 >= curStepIdx){
$(btPrevious).addClass("buttonDisabled");
}else{
$(btPrevious).removeClass("buttonDisabled");
}
if((steps.length-1) <= curStepIdx){
$(btNext).addClass("buttonDisabled");
}else{
$(btNext).removeClass("buttonDisabled");
}
}
// Finish Button
if(!steps.hasClass('disabled') || options.enableFinishButton){
$(btFinish).removeClass("buttonDisabled");
}else{
$(btFinish).addClass("buttonDisabled");
}
}
function showMessage(msg){
$('.content',msgBox).html(msg);
msgBox.show();
}
function setError(stepnum,iserror){
if(iserror){
$(steps.eq(stepnum-1), obj).addClass('error')
}else{
$(steps.eq(stepnum-1), obj).removeClass("error");
}
}
});
};
// Default Properties and Events
$.fn.smartWizard.defaults = {
selected: 0, // Selected Step, 0 = first step
keyNavigation: true, // Enable/Disable key navigation(left and right keys are used if enabled)
enableAllSteps: false,
updateHeight: true,
transitionEffect: 'fade', // Effect on navigation, none/fade/slide/slideleft
contentURL:null, // content url, Enables Ajax content loading
contentCache:true, // cache step contents, if false content is fetched always from ajax url
cycleSteps: false, // cycle step navigation
includeFinishButton: true, // whether to show a Finish button
enableFinishButton: false, // make finish button enabled always
errorSteps:[], // Array Steps with errors
labelNext:'Next',
labelPrevious:'Previous',
labelFinish:'Finish',
onLeaveStep: null, // triggers when leaving a step
onShowStep: null, // triggers when showing a step
onFinish: null // triggers when Finish button is clicked
};
})(jQuery);