forked from caiorg/formToWizard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.formtowizard.js
189 lines (156 loc) · 6.94 KB
/
jquery.formtowizard.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
/*
Form To Wizard https://github.com/artoodetoo/formToWizard
Free to use under MIT license.
Originally created by Janko.
Featured by iFadey.
Polishing by artoodetoo.
*/
(function($) {
$.fn.formToWizard = function( options, cmdParam1 ) {
// Stop when selector found nothing!
if (this.length == 0) return this;
if( typeof options !== 'string' ) {
options = $.extend({
submitButton: '',
showProgress: true,
showStepNo: true,
validateStep: null,
select: null,
progress: null,
nextBtnName: 'Next >',
prevBtnName: '< Back',
buttonTag: 'a',
nextBtnClass: 'btn next',
prevBtnClass: 'btn prev'
}, options);
}
var element = this
, steps = $( element ).find( "fieldset" )
, count = steps.size()
, submmitButtonName = options.submitButton
, commands = null;
if( typeof options !== 'string' ) {
//assign options to current/selected form (element)
$( element ).data( 'options', options );
/**************** Validate Options ********************/
if( typeof( options.validateStep ) !== "function" )
options.validateStep = function() { return true; };
if( options.showProgress && typeof(options.progress) !== "function") {
if( options.showStepNo )
$(element).before("<ul class='steps'></ul>");
else
$(element).before("<ul class='steps breadcrumb'></ul>");
}
/************** End Validate Options ******************/
steps.each(function(i) {
var $this = $(this);
$this.wrap('<div class="step' + i + ' stepDetails"></div>');
$this.append('<p class="step' + i + 'commands commands"></p>');
if( options.showProgress && typeof(options.progress) !== "function") {
if( options.showStepNo )
$(".steps", element).append("<li class='stepDesc" + i + "'>Step " + (i + 1) + "<span>" + $this.find("legend").html() + "</span></li>");
else
$(".steps", element).append("<li class='stepDesc" + i + "'>" + $this.find("legend").html() + "</li>");
}
if (i == 0) {
createNextButton(i);
selectStep(i);
}
else if (i == count - 1) {
$(".step" + i, element).hide();
createPrevButton(i);
// move submit button to the last step
$(submmitButtonName, element).addClass('next').detach().appendTo($(".step" + i + "commands", element)).on('click', function (e) {
e.preventDefault();
console.log($(this).parents('div[class*=step]'));
if( options.validateStep(element, $(this).parents('div[class*=step]')) === true ) {
element.submit();
}
});
}
else {
$(".step" + i, element).hide();
createPrevButton(i);
createNextButton(i);
}
});
} else if( typeof options === 'string' ) {
var cmd = options;
initCommands();
if( typeof commands[ cmd ] === 'function' ) {
commands[ cmd ]( cmdParam1 );
} else {
throw cmd + ' is invalid command!';
}
}
/******************** Command Methods ********************/
function initCommands() {
//restore options object from form element
options = $( element ).data( 'options' );
commands = {
GotoStep: function( stepNo ) {
var stepName = "step" + (--stepNo);
if( $( '.' + stepName )[ 0 ] === undefined ) {
throw 'Step No ' + stepNo + ' not found!';
}
if( $( '.' + stepName, element ).css( 'display' ) === 'none' ) {
$( element ).find( '.stepDetails' ).hide();
$( '.' + stepName, element ).show();
selectStep( stepNo );
}
},
NextStep: function() {
$( '.stepDetails:visible', element ).find( 'a.next' ).click();
},
PreviousStep: function() {
$( '.stepDetails:visible', element ).find( 'a.prev' ).click();
}
};
}
/******************** End Command Methods ********************/
/******************** Private Methods ********************/
function createPrevButton(i) {
var stepName = 'step' + i;
$('.' + stepName + 'commands', element).append(
'<' + options.buttonTag + ' href="#" class="' + stepName + 'Prev ' + options.prevBtnClass + '">' +
options.prevBtnName +
'</' + options.buttonTag + '>'
);
$("." + stepName + "Prev", element).bind("click", function(e) {
$("." + stepName, element).hide();
$(".step" + (i - 1), element).show();
selectStep(i - 1);
return false;
});
}
function createNextButton(i) {
var stepName = 'step' + i;
$('.' + stepName + 'commands', element).append(
'<' + options.buttonTag + ' href="#" class="' + stepName + 'Next ' + options.nextBtnClass + '">' +
options.nextBtnName +
'</' + options.buttonTag + '>');
$("." + stepName + "Next", element).bind( "click", function(e) {
if( options.validateStep(element, $("." + stepName, element)) === true ) {
$("." + stepName, element).hide();
$(".step" + (i + 1), element).show();
//if (i + 2 == count)
selectStep(i + 1);
}
return false;
});
}
function selectStep(i) {
if ( typeof(options.progress) === "function" ) {
options.progress(i, count);
} else if( options.showProgress ) {
$(".steps li", element).removeClass("current");
$(".stepDesc" + i, element).addClass("current");
}
if( options.select ) {
options.select(element, $('.step'+i));
}
}
/******************** End Private Methods ********************/
return this;
}
})(jQuery);