-
Notifications
You must be signed in to change notification settings - Fork 1
/
wizardstep.js
174 lines (156 loc) · 5.98 KB
/
wizardstep.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
/* ========================================================================
* Bootstrap Wizard Step v1.0
* http://johnitvn.github.io/bootstrap-wizard-step
* ========================================================================
* Copyright John Martin
* Licensed under Apache License 2.0 (https://github.com/johnitvn/bootstrap-wizard-step/blob/master/LICENSE)
* ======================================================================== */
$.fn.boostrapWizardStep = function(options) {
var defaults = {
startStep: 0,
onNextStep: null,
onBackStep: null,
onJumpBack: null,
tabIdentifier: "#wizard-tab",
contentIdentifier: "#wizard-content",
btnNextIdentifier: "#next",
btnPreviousIdentifier: "#previous"
};
var settings, tab, content, next, previous, current, tabSteps, old;
function prepareConfig(rootElm) {
var markup = {};
if (typeof $(rootElm).attr("data-start") !== typeof undefined) {
markup['startStep'] = parseInt($(rootElm).attr("data-start"));
}
if (typeof $(rootElm).attr("data-content") !== typeof undefined) {
markup['contentIdentifier'] = $(rootElm).attr("data-content");
}
if (typeof $(rootElm).attr("data-tab") !== typeof undefined) {
markup['tabIdentifier'] = $(rootElm).attr("data-tab");
}
if (typeof $(rootElm).attr("data-previous") !== typeof undefined) {
markup['btnPreviousIdentifier'] = $(rootElm).attr("data-previous");
}
if (typeof $(rootElm).attr("data-next") !== typeof undefined) {
markup['btnNextIdentifier'] = $(rootElm).attr("data-next");
}
settings = $.extend({}, defaults, markup, options);
tab = $(rootElm).find(settings.tabIdentifier);
content = $(rootElm).find(settings.contentIdentifier);
next = $(rootElm).find(settings.btnNextIdentifier);
previous = $(rootElm).find(settings.btnPreviousIdentifier);
current = settings.startStep;
tabSteps = $(tab).find("li>a");
}
function startStep(index) {
old = current;
current = index;
refreshTab();
refreshButtons();
}
function refreshButtons() {
if (current === 0) {
$(previous).addClass("disabled");
} else {
$(previous).removeClass("disabled");
}
}
function refreshTab() {
/**
* Disable and set inactive for all finised step
*/
for (var i = 0; i < current; i++) {
$(tabSteps[i]).parent().removeClass("disabled active fade");
$(tabSteps[i]).parent().removeClass("active");
$(tabSteps[i]).parent().removeClass("fade");
$(content).find($(tabSteps[i]).attr("href")).removeClass("active");
}
/*
* enable current step
*/
$(tabSteps[current]).parent().removeClass("disabled");
$(tabSteps[current]).parent().removeClass("fade");
$(tabSteps[current]).parent().addClass("active");
$(content).find($(tabSteps[current]).attr("href")).addClass("active");
/**
* Disable all next step
*/
for (var i = current + 1; i < tabSteps.length; i++) {
$(tabSteps[i]).parent().addClass("disabled");
$(tabSteps[i]).parent().removeClass("active");
$(tabSteps[i]).parent().removeClass("fade");
$(content).find($(tabSteps[i]).attr("href")).removeClass("active");
}
}
return this.each(function() {
var rootElm = $(this);
/**
* Don't do anything if exist wiward
*/
if ($(this).hasClass("in-wizard")) {
return false;
} else {
$(this).addClass("in-wizard");
}
prepareConfig(rootElm);
/**
* Disable user click to tab
*/
$(tab).on("click", "li>a", function(e) {
var index = $(this).parent().index();
if (index >= current) {
return;
}
/**
* Notify even wizard.jumpback with index of step jump to
*/
var event = jQuery.Event("jumpback.bs.wizard");
$(rootElm).trigger(event, [index, current]);
if (event.isDefaultPrevented() === false && (settings.onJumpBack === null || (settings.onJumpBack !== null && settings.onJumpBack(index, current)))) {
startStep(index);
}
e.preventDefault();
});
/**
* The first open the first step
*/
startStep(settings.startStep);
$(next).click(function(e) {
/**
* skip if the end of wizard
*/
if (current === tabSteps.length - 1) {
return;
}
/**
* Notify even wizard.next with evendata index is current of step
*/
var event = jQuery.Event("next.bs.wizard");
$(rootElm).trigger(event, [current]);
if (event.isDefaultPrevented() === false && (settings.onNextStep === null || (settings.onNextStep !== null && settings.onNextStep(current)))) {
startStep(current + 1);
}
e.preventDefault();
});
$(previous).click(function(e) {
/**
* skip if the end of wizard
*/
if (current === 0) {
return;
}
/**
* Notify even wizard.next with evendata index is current of step
*/
var event = jQuery.Event("previous.bs.wizard");
$(rootElm).trigger(event, [current]);
if (event.isDefaultPrevented() === false && (settings.onBackStep === null || (settings.onBackStep !== null && settings.onBackStep(current)))) {
startStep(current - 1);
}
e.preventDefault();
});
});
};
$(function() {
$('[data-toggle="bootstrap-wizard-step"]').boostrapWizardStep();
});