|
16 | 16 | factory(jQuery);
|
17 | 17 | }
|
18 | 18 | }(function ($) {
|
19 |
| - |
20 |
| - "use strict"; |
| 19 | + 'use strict'; |
21 | 20 |
|
22 | 21 | const msfCssClasses = {
|
23 | 22 | header: "msf-header",
|
|
28 | 27 | view: "msf-view",
|
29 | 28 | navigation: "msf-navigation",
|
30 | 29 | navButton: "msf-nav-button"
|
31 |
| - |
32 | 30 | };
|
33 | 31 |
|
34 | 32 | const msfNavTypes = {
|
35 | 33 | back: "back",
|
36 | 34 | next: "next",
|
37 | 35 | submit: "submit"
|
38 | 36 |
|
39 |
| - } |
| 37 | + }; |
| 38 | + |
| 39 | + const msfEventTypes = { |
| 40 | + viewChanged : "msf:viewChanged" |
| 41 | + }; |
40 | 42 |
|
41 | 43 | $.fn.multiStepForm = function (options) {
|
42 | 44 | var form = this;
|
|
48 | 50 |
|
49 | 51 | var settings = $.extend({}, defaults, options);
|
50 | 52 |
|
| 53 | + //find the msf-content object |
51 | 54 | form.content = this.find("." + msfCssClasses.content).first();
|
52 | 55 |
|
53 | 56 | if (form.content.length === 0) {
|
54 | 57 | throw new Error('Multi-Step Form requires a child element of class \'' + msfCssClasses.content + '\'');
|
55 | 58 | }
|
56 | 59 |
|
| 60 | + //find the msf-views within the content object |
57 | 61 | form.views = $(this.content).find("." + msfCssClasses.view);
|
58 | 62 |
|
59 | 63 | if (form.views.length === 0) {
|
60 | 64 | throw new Error('Multi-Step Form\'s element of class \'' + msfCssClasses.content + '\' requires n elements of class \'' + msfCssClasses.view + '\'');
|
61 | 65 | }
|
62 | 66 |
|
63 | 67 | form.header = this.find("." + msfCssClasses.header).first();
|
| 68 | + form.navigation = this.find("." + msfCssClasses.navigation).first(); |
64 | 69 | form.steps = [];
|
65 | 70 |
|
66 |
| - form.navigation = this.find("." + msfCssClasses.navigation).first(); |
| 71 | + form.getActiveView = function() { |
| 72 | + return form.views.filter(function () { return this.style && this.style.display !== '' && this.style.display !== 'none' }); |
| 73 | + }; |
| 74 | + |
| 75 | + form.setActiveView = function(index) { |
| 76 | + var view = form.getActiveView(); |
| 77 | + var previousIndex = form.views.index(view); |
| 78 | + |
| 79 | + view = form.views.eq(index); |
| 80 | + view.show(); |
| 81 | + view.find(':input').first().focus(); |
| 82 | + |
| 83 | + //trigger the 'view has changed' event |
| 84 | + form.trigger(msfEventTypes.viewChanged, { |
| 85 | + currentIndex : index, |
| 86 | + previousIndex : previousIndex, |
| 87 | + totalSteps : form.steps.length |
| 88 | + }); |
| 89 | + } |
67 | 90 |
|
68 | 91 | form.init = function () {
|
69 | 92 |
|
|
133 | 156 | var view = element,
|
134 | 157 | $view = $(element);
|
135 | 158 |
|
136 |
| - view.init = function () { |
137 |
| - if (index === settings.activeIndex) { |
138 |
| - $view.show(); |
139 |
| - } |
140 |
| - }; |
141 |
| - |
142 |
| - |
143 | 159 | $view.on('show', function (e) {
|
144 | 160 | if (this !== e.target)
|
145 | 161 | return;
|
146 | 162 |
|
| 163 | + //hide whichever view is currently showing |
| 164 | + form.getActiveView().hide(); |
| 165 | + |
| 166 | + //choose which navigation buttons should be displayed based on index of view |
147 | 167 | if (index > 0) {
|
148 | 168 | form.backNavButton.show();
|
149 | 169 | }
|
|
156 | 176 | form.submitNavButton.hide();
|
157 | 177 | form.nextNavButton.show();
|
158 | 178 |
|
| 179 | + //if this is not the last view do not allow the enter key to submit the form as it is not completed yet |
159 | 180 | $(this).find(':input').keypress(function (e) {
|
160 | 181 | if (e.which == 13) // Enter key = keycode 13
|
161 | 182 | {
|
|
165 | 186 | });
|
166 | 187 | }
|
167 | 188 |
|
168 |
| - |
| 189 | + //determine if each step is completed or active based in index |
169 | 190 | $.each(form.steps, function (i, element) {
|
170 | 191 | if (i < index) {
|
171 | 192 | $(element).removeClass(msfCssClasses.stepActive);
|
|
175 | 196 | else if (i === index) {
|
176 | 197 | $(element).removeClass(msfCssClasses.stepComplete);
|
177 | 198 | $(element).addClass(msfCssClasses.stepActive);
|
178 |
| - var currentProgress = Math.round((i / form.steps.length)*100); |
179 |
| - $(document).trigger('msf:updateProgress', [currentProgress]); // Trigger an event to notify the progress has changed |
180 | 199 | }
|
181 | 200 | else {
|
182 | 201 | $(element).removeClass(msfCssClasses.stepComplete);
|
|
189 | 208 | if (this !== e.target)
|
190 | 209 | return;
|
191 | 210 |
|
| 211 | + //hide all navigation buttons, display choices will be set on show event |
192 | 212 | form.backNavButton.hide();
|
193 | 213 | form.nextNavButton.hide();
|
194 | 214 | form.submitNavButton.hide();
|
195 | 215 | });
|
196 |
| - |
197 |
| - view.init(); |
198 | 216 | });
|
199 | 217 |
|
| 218 | + form.setActiveView(settings.activeIndex); |
200 | 219 | };
|
201 | 220 |
|
202 | 221 | form.init();
|
203 | 222 |
|
204 |
| - form.getActiveView = function() { |
205 |
| - return form.views.filter(function () { return this.style && this.style.display !== '' && this.style.display !== 'none' }); |
206 |
| - }; |
207 |
| - |
208 |
| - form.setActiveView = function(index) { |
209 |
| - var view = form.getActiveView(); |
210 |
| - |
211 |
| - // var view = (currentView) ? currentView : form.getActiveView(); |
212 |
| - |
213 |
| - view.hide(); |
214 |
| - form.views.eq(index).show(); |
215 |
| - form.views.eq(index).find(':input').first().focus(); |
216 |
| - } |
217 |
| - |
218 | 223 | form.nextNavButton.click(function () {
|
219 | 224 | var view = form.getActiveView();
|
220 | 225 |
|
| 226 | + //validate the input in the current view |
221 | 227 | if (form.validate(settings.validate).subset(view)) {
|
222 | 228 | var i = form.views.index(view);
|
223 | 229 |
|
|
228 | 234 | form.backNavButton.click(function () {
|
229 | 235 | var view = form.getActiveView();
|
230 | 236 | var i = form.views.index(view);
|
231 |
| - |
| 237 | + |
232 | 238 | form.setActiveView(i-1);
|
233 | 239 | });
|
234 | 240 |
|
|
0 commit comments