-
Notifications
You must be signed in to change notification settings - Fork 17
/
jquery.quickWizard.js
231 lines (185 loc) · 9.74 KB
/
jquery.quickWizard.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
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
(function ($) {
$.fn.quickWizard = function (options, callback) {
var settings = {
'prevButton': '<button id="form-wizard-prev" type="button">Previous</button>',
'nextButton': '<button id="form-wizard-next" type="button">Next</button>',
'activeClass': 'form-wizard-active',
'element': 'fieldset',
'submit': '[type = "submit"]',
'root': null,
'prevArgs': [0],
'nextArgs': [0],
'disabledClass': 'form-wizard-disabled',
'containerClass' : 'form-wizard-container',
'breadCrumb': true,
'breadCrumbElement': 'legend',
'breadCrumbListOpen': '<ol>',
'breadCrumbListClose': '</ol>',
'breadCrumbListClass': 'bread-crumb',
'breadCrumbListElementOpen': '<li>',
'breadCrumbListElementClose': '</li>',
'breadCrumbActiveClass': 'bread-crumb-active',
'breadCrumbCompletedClass': 'bread-crumb-completed',
'breadCrumbPosition': 'before',
'clickableBreadCrumbs': false
};
if (options) {
$.extend(settings, options);
}
callback = callback || function () { };
function disablePrev(prevObj){
if ($(prevObj).is(":button")) {
$(prevObj).attr('disabled', 'disabled');
} else {
$(prevObj).addClass(settings.disabledClass);
}
}
return this.each(function () {
var container = $(this);
var children = container.children(settings.element);
var activeClassSelector = '.' + settings.activeClass;
var submitButton = container.find(settings.submit);
var insertedNextCallback;
var originalNextCallback;
var root;
var breadCrumbList;
if(settings.root === null){
root = children.first();
}else{
root = $(settings.root);
}
/* Set up container class */
if(settings.containerClass != ""){
container.addClass(settings.containerClass);
}
/* Set up bread crumb menu */
if (settings.breadCrumb) {
breadCrumbList = settings.breadCrumbListOpen;
children.find(settings.breadCrumbElement).each(function (index) {
breadCrumbList += settings.breadCrumbListElementOpen + $(this).text() + settings.breadCrumbListElementClose;
});
breadCrumbList += settings.breadCrumbListClose;
if (settings.breadCrumbPosition === 'after') {
breadCrumbList = $(breadCrumbList).insertAfter(container);
} else {
breadCrumbList = $(breadCrumbList).insertBefore(container);
}
breadCrumbList.children().first().addClass(settings.breadCrumbActiveClass);
breadCrumbList.addClass(settings.breadCrumbListClass);
}
/* Check if the last argument is a callback function */
if (typeof (settings.nextArgs[settings.nextArgs.length - 1]) == "function") {
/* If it is store the user provided callback */
originalNextCallback = settings.nextArgs[settings.nextArgs.length - 1];
/* then replace it with a wrapper function that calls both the user provided function and ours */
settings.nextArgs[settings.nextArgs.length - 1] = function () { insertedNextCallback.call(); originalNextCallback.call(); };
} else {
/* If there is no callback function append ours */
settings.nextArgs[settings.nextArgs.length] = function () { insertedNextCallback.call(); }
}
/* Insert the previous and next buttons after the submit button and hide it until we're ready */
var prev = $(settings.prevButton).insertBefore(submitButton);
var next = $(settings.nextButton).insertBefore(submitButton);
submitButton.hide();
/* If the root element is first disable the previous button */
if(root.is(':first-child')){
disablePrev(prev);
}
children.hide();
root.toggleClass(settings.activeClass).show();
$(next).click(function () {
var active = container.find(activeClassSelector);
/* Check to see if the forms are valid before moving on */
if (active.find(":input").valid()) {
var nextSet = active.next(settings.element);
var afterNextSet = nextSet.next(settings.element);
if (nextSet.length) {
$(active).toggleClass(settings.activeClass);
$(nextSet).toggleClass(settings.activeClass);
/* Get the current element's position and store it */
active.data('posiiton', active.css('position'));
/* Set our callback function */
insertedNextCallback = function () { active.css('position', active.data('posiiton')); };
/* Call show and hide with the user provided arguments */
active.css('position', 'absolute').hide.apply(active, settings.nextArgs);
nextSet.show.apply(nextSet, settings.prevArgs);
/* If bread crumb menu is used make those changes */
if (settings.breadCrumb) {
breadCrumbList.find('.' + settings.breadCrumbActiveClass).removeClass(settings.breadCrumbActiveClass).addClass(settings.breadCrumbCompletedClass).next().addClass(settings.breadCrumbActiveClass);
}
/* If the previous button is a button enable it */
if ($(prev).is(":button")) {
$(prev).removeAttr('disabled');
} else {
/* If it's anything else, remove the disabled class */
$(prev).removeClass(settings.disabledClass);
}
}
/* If there are no more sections, hide the next button and show the submit button */
if (afterNextSet.length <= 0) {
$(next).hide();
submitButton.show();
}
}
});
$(prev).click(function () {
var active = container.find(activeClassSelector);
var prevSet = active.prev(settings.element);
var beforePrevSet = prevSet.prev(settings.element);
if (prevSet.length) {
$(active).toggleClass(settings.activeClass);
$(prevSet).toggleClass(settings.activeClass);
prevSet.data('posiiton', prevSet.css('position'));
insertedNextCallback = function () { prevSet.css('position', prevSet.data('posiiton')); };
active.hide.apply(active, settings.prevArgs);
prevSet.css('position', 'absolute').show.apply(prevSet, settings.nextArgs);
if (settings.breadCrumb) {
breadCrumbList.find('.' + settings.breadCrumbActiveClass).removeClass(settings.breadCrumbActiveClass).prev().addClass(settings.breadCrumbActiveClass);
}
$(next).show();
submitButton.hide();
}
if (beforePrevSet.length <= 0) {
disablePrev(prev);
}
});
if(settings.breadCrumb && settings.clickableBreadCrumbs){
$('.' + settings.breadCrumbListClass).children().click(function(){
var active = container.find(activeClassSelector);
var current = $(settings.element).eq($(this).index());
var prevSet = current.prev(settings.element);
var nextSet = current.next(settings.element);
$(active).toggleClass(settings.activeClass);
current.toggleClass(settings.activeClass);
/* Get the current element's position and store it */
active.data('posiiton', active.css('position'));
/* Set our callback function */
insertedNextCallback = function () { active.css('position', active.data('posiiton')); };
/* Call show and hide with the user provided arguments */
active.css('position', 'absolute').hide.apply(active, settings.nextArgs);
current.parents().show.apply(current, settings.prevArgs);
breadCrumbList.find('.' + settings.breadCrumbActiveClass).removeClass(settings.breadCrumbActiveClass);
$(this).addClass(settings.breadCrumbActiveClass);
if(prevSet.length){
$(next).show();
submitButton.hide();
}
if ($(prev).is(":button")) {
$(prev).removeAttr('disabled');
} else {
/* If it's anything else, remove the disabled class */
$(prev).removeClass(settings.disabledClass);
}
if (prevSet.length <= 0) {
disablePrev(prev);
}
if (nextSet.length <= 0) {
$(next).hide();
submitButton.show();
}
});
}
callback.call(this);
});
};
})(jQuery);