hat-trick 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,444 +13,505 @@
13
13
 
14
14
 
15
15
  (function($){
16
- $.widget("ui.formwizard", {
17
-
18
- _init: function() {
19
-
20
- var wizard = this;
21
- var formOptionsSuccess = this.options.formOptions.success;
22
- var formOptionsComplete = this.options.formOptions.complete;
23
- var formOptionsBeforeSend = this.options.formOptions.beforeSend;
24
- var formOptionsBeforeSubmit = this.options.formOptions.beforeSubmit;
25
- var formOptionsBeforeSerialize = this.options.formOptions.beforeSerialize;
26
- this.options.formOptions = $.extend(this.options.formOptions,{
27
- success : function(responseText, textStatus, xhr){
28
- if(formOptionsSuccess){
29
- formOptionsSuccess(responseText, textStatus, xhr);
30
- }
31
- if(wizard.options.formOptions && wizard.options.formOptions.resetForm || !wizard.options.formOptions){
32
- wizard._reset();
33
- }
34
- },
35
- complete : function(xhr, textStatus){
36
- if(formOptionsComplete){
37
- formOptionsComplete(xhr, textStatus);
38
- }
39
- wizard._enableNavigation();
40
- },
41
- beforeSubmit : function(arr, theForm, options) {
42
- if(formOptionsBeforeSubmit){
43
- var shouldSubmit = formOptionsBeforeSubmit(arr, theForm, options);
44
- if(!shouldSubmit)
45
- wizard._enableNavigation();
46
- return shouldSubmit;
47
- }
48
- },
49
- beforeSend : function(xhr) {
50
- if(formOptionsBeforeSend){
51
- var shouldSubmit = formOptionsBeforeSend(xhr);
52
- if(!shouldSubmit)
53
- wizard._enableNavigation();
54
- return shouldSubmit;
55
- }
56
- },
57
- beforeSerialize: function(form, options) {
58
- if(formOptionsBeforeSerialize){
59
- var shouldSubmit = formOptionsBeforeSerialize(form, options);
60
- if(!shouldSubmit)
61
- wizard._enableNavigation();
62
- return shouldSubmit;
63
- }
64
- }
65
- });
66
-
67
- this.steps = this.element.find(".step").hide();
68
-
69
- this.firstStep = this.steps.eq(0).attr("id");
70
- this.activatedSteps = new Array();
71
- this.isLastStep = false;
72
- this.previousStep = undefined;
73
- this.currentStep = this.steps.eq(0).attr("id");
74
- this.nextButton = this.element.find(this.options.next)
75
- .click(function() {
76
- return wizard._next();
77
- });
78
-
79
- this.nextButtonInitinalValue = this.nextButton.val();
80
- this.nextButton.val(this.options.textNext);
81
-
82
- this.backButton = this.element.find(this.options.back)
83
- .click(function() {
84
- wizard._back();return false;
85
- });
86
-
87
- this.backButtonInitinalValue = this.backButton.val();
88
- this.backButton.val(this.options.textBack);
89
-
90
- if(this.options.validationEnabled && jQuery().validate == undefined){
91
- this.options.validationEnabled = false;
92
- if( (window['console'] !== undefined) ){
93
- console.log("%s", "validationEnabled option set, but the validation plugin is not included");
94
- }
95
- }else if(this.options.validationEnabled){
96
- this.element.validate(this.options.validationOptions);
97
- }
98
- if(this.options.formPluginEnabled && jQuery().ajaxSubmit == undefined){
99
- this.options.formPluginEnabled = false;
100
- if( (window['console'] !== undefined) ){
101
- console.log("%s", "formPluginEnabled option set but the form plugin is not included");
102
- }
103
- }
104
-
105
- if(this.options.disableInputFields == true){
106
- $(this.steps).find(":input:not('.wizard-ignore')").attr("disabled","disabled");
107
- }
108
-
109
- if(this.options.historyEnabled){
110
- $(window).bind('hashchange', undefined, function(event){
111
- var hashStep = event.getState( "_" + $(wizard.element).attr( 'id' )) || wizard.firstStep;
112
- if(hashStep !== wizard.currentStep){
113
- if(wizard.options.validationEnabled && hashStep === wizard._navigate(wizard.currentStep)){
114
- if(!wizard.element.valid()){
115
- wizard._updateHistory(wizard.currentStep);
116
- wizard.element.validate().focusInvalid();
117
-
118
- return false;
119
- }
120
- }
121
- if(hashStep !== wizard.currentStep)
122
- wizard._show(hashStep);
123
- }
124
- });
125
- }
126
-
127
- this.element.addClass("ui-formwizard");
128
- this.element.find(":input").addClass("ui-wizard-content");
129
- this.steps.addClass("ui-formwizard-content");
130
- this.backButton.addClass("ui-formwizard-button ui-wizard-content");
131
- this.nextButton.addClass("ui-formwizard-button ui-wizard-content");
132
-
133
- if(!this.options.disableUIStyles){
134
- this.element.addClass("ui-helper-reset ui-widget ui-widget-content ui-helper-reset ui-corner-all");
135
- this.element.find(":input").addClass("ui-helper-reset ui-state-default");
136
- this.steps.addClass("ui-helper-reset ui-corner-all");
137
- this.backButton.addClass("ui-helper-reset ui-state-default");
138
- this.nextButton.addClass("ui-helper-reset ui-state-default");
139
- }
140
- this._show(undefined);
141
- return $(this);
142
- },
143
-
144
- _next : function(){
145
- if(this.options.validationEnabled){
146
- if(!this.element.valid()){
147
- this.element.validate().focusInvalid();
148
- return false;
149
- }
150
- }
151
-
152
- if(this.options.remoteAjax != undefined){
153
- var options = this.options.remoteAjax[this.currentStep];
154
- var wizard = this;
155
- if(options !== undefined){
156
- var success = options.success;
157
- var beforeSend = options.beforeSend;
158
- var complete = options.complete;
159
-
160
- options = $.extend({},options,{
161
- success: function(data, statusText){
162
- if((success !== undefined && success(data, statusText)) || (success == undefined)){
163
- wizard._continueToNextStep();
164
- }
165
- },
166
- beforeSend : function(xhr){
167
- wizard._disableNavigation();
168
- if(beforeSend !== undefined)
169
- beforeSend(xhr);
170
- $(wizard.element).trigger('before_remote_ajax', {"currentStep" : wizard.currentStep});
171
- },
172
- complete : function(xhr, statusText){
173
- if(complete !== undefined)
174
- complete(xhr, statusText);
175
- $(wizard.element).trigger('after_remote_ajax', {"currentStep" : wizard.currentStep});
176
- wizard._enableNavigation();
177
- }
178
- })
179
- this.element.ajaxSubmit(options);
180
- return false;
181
- }
182
- }
183
-
184
- return this._continueToNextStep();
185
- },
186
-
187
- _back : function(){
188
- if(this.activatedSteps.length > 0){
189
- if(this.options.historyEnabled){
190
- this._updateHistory(this.activatedSteps[this.activatedSteps.length - 2]);
191
- }else{
192
- this._show(this.activatedSteps[this.activatedSteps.length - 2], true);
193
- }
194
- }
195
- return false;
196
- },
197
-
198
- _continueToNextStep : function(){
199
- if(this.isLastStep){
200
- for(var i = 0; i < this.activatedSteps.length; i++){
201
- this.steps.filter("#" + this.activatedSteps[i]).find(":input").not(".wizard-ignore").removeAttr("disabled");
202
- }
203
- if(!this.options.formPluginEnabled){
204
- return true;
205
- }else{
206
- this._disableNavigation();
207
- this.element.ajaxSubmit(this.options.formOptions);
208
- return false;
209
- }
210
- }
211
-
212
- var step = this._navigate(this.currentStep);
213
- if(step == this.currentStep){
214
- return false;
215
- }
216
- if(this.options.historyEnabled){
217
- this._updateHistory(step);
218
- }else{
219
- this._show(step, true);
220
- }
221
- return false;
222
- },
223
-
224
- _updateHistory : function(step){
225
- var state = {};
226
- state["_" + $(this.element).attr('id')] = step;
227
- $.bbq.pushState(state);
228
- },
229
-
230
- _disableNavigation : function(){
231
- this.nextButton.attr("disabled","disabled");
232
- this.backButton.attr("disabled","disabled");
233
- if(!this.options.disableUIStyles){
234
- this.nextButton.removeClass("ui-state-active").addClass("ui-state-disabled");
235
- this.backButton.removeClass("ui-state-active").addClass("ui-state-disabled");
236
- }
237
- },
238
-
239
- _enableNavigation : function(){
240
- if(this.isLastStep){
241
- this.nextButton.val(this.options.textSubmit);
242
- }else{
243
- this.nextButton.val(this.options.textNext);
244
- }
245
-
246
- if($.trim(this.currentStep) !== this.steps.eq(0).attr("id")){
247
- this.backButton.removeAttr("disabled");
248
- if(!this.options.disableUIStyles){
249
- this.backButton.removeClass("ui-state-disabled").addClass("ui-state-active");
250
- }
251
- }
252
-
253
- this.nextButton.removeAttr("disabled");
254
- if(!this.options.disableUIStyles){
255
- this.nextButton.removeClass("ui-state-disabled").addClass("ui-state-active");
256
- }
257
- },
258
-
259
- _animate : function(oldStep, newStep, stepShownCallback){
260
- this._disableNavigation();
261
- var old = this.steps.filter("#" + oldStep);
262
- var current = this.steps.filter("#" + newStep);
263
- old.find(":input").not(".wizard-ignore").attr("disabled","disabled");
264
- current.find(":input").not(".wizard-ignore").removeAttr("disabled");
265
- var wizard = this;
266
- old.animate(wizard.options.outAnimation, wizard.options.outDuration, wizard.options.easing, function(){
267
- current.animate(wizard.options.inAnimation, wizard.options.inDuration, wizard.options.easing, function(){
268
- if(wizard.options.focusFirstInput)
269
- current.find(":input:first").focus();
270
- wizard._enableNavigation();
271
-
272
- stepShownCallback.apply(wizard);
273
- });
274
- return;
275
- });
276
- },
277
-
278
- _checkIflastStep : function(step){
279
- this.isLastStep = false;
280
- if($("#" + step).hasClass(this.options.submitStepClass) || this.steps.filter(":last").attr("id") == step){
281
- this.isLastStep = true;
282
- }
283
- },
284
-
285
- _getLink : function(step){
286
- var link = undefined;
287
- var links = this.steps.filter("#" + step).find(this.options.linkClass);
288
-
289
- if(links != undefined){
290
- if(links.filter(":radio,:checkbox").size() > 0){
291
- link = links.filter(this.options.linkClass + ":checked").val();
292
- }else{
293
- link = $(links).val();
294
- }
295
- }
296
- return link;
297
- },
298
-
299
- _navigate : function(step){
300
- var link = this._getLink(step);
301
- if(link != undefined){
302
- if((link != "" && link != null && link != undefined) && this.steps.filter("#" + link).attr("id") != undefined){
303
- return link;
304
- }
305
- return this.currentStep;
306
- }else if(link == undefined && !this.isLastStep){
307
- var step1 = this.steps.filter("#" + step).next().attr("id");
308
- return step1;
309
- }
310
- },
311
-
312
- _show : function(step){
313
- var backwards = false;
314
- var triggerStepShown = step !== undefined;
315
- if(step == undefined || step == ""){
316
- this.activatedSteps.pop();
317
- step = this.firstStep;
318
- this.activatedSteps.push(step);
319
- }else{
320
- if($.inArray(step, this.activatedSteps) > -1){
321
- backwards = true;
322
- this.activatedSteps.pop();
323
- }else {
324
- this.activatedSteps.push(step);
325
- }
326
- }
327
-
328
- if(this.currentStep !== step || step === this.firstStep){
329
- this.previousStep = this.currentStep;
330
- this._checkIflastStep(step);
331
- this.currentStep = step;
332
- var stepShownCallback = function(){if(triggerStepShown)$(this.element).trigger('step_shown', $.extend({"isBackNavigation" : backwards},this._state()));};
333
- this._animate(this.previousStep, step, stepShownCallback);
334
- };
335
-
336
-
337
- },
338
-
339
- _reset : function(){
340
- this.element.resetForm()
341
- $("label,:input,textarea",this).removeClass("error");
342
- for(var i = 0; i < this.activatedSteps.length; i++){
343
- this.steps.filter("#" + this.activatedSteps[i]).hide().find(":input").attr("disabled","disabled");
344
- }
345
- this.activatedSteps = new Array();
346
- this.previousStep = undefined;
347
- this.isLastStep = false;
348
- if(this.options.historyEnabled){
349
- this._updateHistory(this.firstStep);
350
- }else{
351
- this._show(this.firstStep);
352
- }
353
-
354
- },
355
-
356
- _state : function(state){
357
- var currentState = { "settings" : this.options,
358
- "activatedSteps" : this.activatedSteps,
359
- "isLastStep" : this.isLastStep,
360
- "isFirstStep" : this.currentStep === this.firstStep,
361
- "previousStep" : this.previousStep,
362
- "currentStep" : this.currentStep,
363
- "backButton" : this.backButton,
364
- "nextButton" : this.nextButton,
365
- "steps" : this.steps,
366
- "firstStep" : this.firstStep
367
- }
368
-
369
- if(state !== undefined)
370
- return currentState[state];
371
-
372
- return currentState;
373
- },
374
-
375
- /*Methods*/
376
-
377
- show : function(step){
378
- if(this.options.historyEnabled){
379
- this._updateHistory(step);
380
- }else{
381
- this._show(step);
382
- }
383
- },
384
-
385
- state : function(state){
386
- return this._state(state);
387
- },
388
-
389
- reset : function(){
390
- this._reset();
391
- },
392
-
393
- next : function(){
394
- this._next();
395
- },
396
-
397
- back : function(){
398
- this._back();
399
- },
400
-
401
- destroy: function() {
402
- this.element.find("*").removeAttr("disabled").show();
403
- this.nextButton.unbind("click").val(this.nextButtonInitinalValue).removeClass("ui-state-disabled").addClass("ui-state-active");
404
- this.backButton.unbind("click").val(this.backButtonInitinalValue).removeClass("ui-state-disabled").addClass("ui-state-active");
405
- this.backButtonInitinalValue = undefined;
406
- this.nextButtonInitinalValue = undefined;
407
- this.activatedSteps = undefined;
408
- this.previousStep = undefined;
409
- this.currentStep = undefined;
410
- this.isLastStep = undefined;
411
- this.options = undefined;
412
- this.nextButton = undefined;
413
- this.backButton = undefined;
414
- this.formwizard = undefined;
415
- this.element = undefined;
416
- this.steps = undefined;
417
- this.firstStep = undefined;
418
- },
419
-
420
- update_steps : function(){
421
- this.steps = this.element.find(".step").addClass("ui-formwizard-content");
422
- this.steps.not("#" + this.currentStep).hide().find(":input").addClass("ui-wizard-content").attr("disabled","disabled");
423
- this._checkIflastStep(this.currentStep);
424
- this._enableNavigation();
425
- if(!this.options.disableUIStyles){
426
- this.steps.addClass("ui-helper-reset ui-corner-all");
427
- this.steps.find(":input").addClass("ui-helper-reset ui-state-default");
428
- }
429
- },
430
-
431
- options: {
432
- historyEnabled : false,
433
- validationEnabled : false,
434
- validationOptions : undefined,
435
- formPluginEnabled : false,
436
- linkClass : ".link",
437
- submitStepClass : "submit_step",
438
- back : ":reset",
439
- next : ":submit",
440
- textSubmit : 'Submit',
441
- textNext : 'Next',
442
- textBack : 'Back',
443
- remoteAjax : undefined,
444
- inAnimation : {opacity: 'show'},
445
- outAnimation: {opacity: 'hide'},
446
- inDuration : 400,
447
- outDuration: 400,
448
- easing: 'swing',
449
- focusFirstInput : false,
450
- disableInputFields : true,
451
- formOptions : { reset: true, success: function(data) { if( (window['console'] !== undefined) ){console.log("%s", "form submit successful");}},
452
- disableUIStyles : false
453
- }
16
+ $.widget("ui.formwizard", {
17
+
18
+ _init: function() {
19
+
20
+ var wizard = this;
21
+ var formOptionsSuccess = this.options.formOptions.success;
22
+ var formOptionsComplete = this.options.formOptions.complete;
23
+ var formOptionsBeforeSend = this.options.formOptions.beforeSend;
24
+ var formOptionsBeforeSubmit = this.options.formOptions.beforeSubmit;
25
+ var formOptionsBeforeSerialize = this.options.formOptions.beforeSerialize;
26
+ this.options.formOptions = $.extend(this.options.formOptions,{
27
+ success : function(responseText, textStatus, xhr){
28
+ if(formOptionsSuccess){
29
+ formOptionsSuccess(responseText, textStatus, xhr);
30
+ }
31
+ if(wizard.options.formOptions && wizard.options.formOptions.resetForm || !wizard.options.formOptions){
32
+ wizard._reset();
33
+ }
34
+ },
35
+ complete : function(xhr, textStatus){
36
+ if(formOptionsComplete){
37
+ formOptionsComplete(xhr, textStatus);
38
+ }
39
+ wizard._enableNavigation();
40
+ },
41
+ beforeSubmit : function(arr, theForm, options) {
42
+ if(formOptionsBeforeSubmit){
43
+ var shouldSubmit = formOptionsBeforeSubmit(arr, theForm, options);
44
+ if(!shouldSubmit)
45
+ wizard._enableNavigation();
46
+ return shouldSubmit;
47
+ }
48
+ },
49
+ beforeSend : function(xhr) {
50
+ if(formOptionsBeforeSend){
51
+ var shouldSubmit = formOptionsBeforeSend(xhr);
52
+ if(!shouldSubmit)
53
+ wizard._enableNavigation();
54
+ return shouldSubmit;
55
+ }
56
+ },
57
+ beforeSerialize: function(form, options) {
58
+ if(formOptionsBeforeSerialize){
59
+ var shouldSubmit = formOptionsBeforeSerialize(form, options);
60
+ if(!shouldSubmit)
61
+ wizard._enableNavigation();
62
+ return shouldSubmit;
63
+ }
64
+ }
65
+ });
66
+
67
+ this.steps = this.element.find(".step").hide();
68
+
69
+ this.firstStep = this.options.firstStep || this.steps.eq(0).attr("id");
70
+ this.activatedSteps = [];
71
+ this.isLastStep = false;
72
+ this.previousStep = undefined;
73
+ this.currentStep = this.firstStep;
74
+
75
+ this._updateButtons();
76
+
77
+ if(this.options.validationEnabled && jQuery().validate == undefined){
78
+ this.options.validationEnabled = false;
79
+ if( (window['console'] !== undefined) ){
80
+ console.log("%s", "validationEnabled option set, but the validation plugin is not included");
81
+ }
82
+ }else if(this.options.validationEnabled){
83
+ this.element.validate(this.options.validationOptions);
84
+ }
85
+ if(this.options.formPluginEnabled && jQuery().ajaxSubmit == undefined){
86
+ this.options.formPluginEnabled = false;
87
+ if( (window['console'] !== undefined) ){
88
+ console.log("%s", "formPluginEnabled option set but the form plugin is not included");
89
+ }
90
+ }
91
+
92
+ if(this.options.disableInputFields == true){
93
+ $(this.steps).find(":input:not('.wizard-ignore')").attr("disabled","disabled");
94
+ }
95
+
96
+ if(this.options.historyEnabled){
97
+ History.Adapter.bind(window, 'statechange', function() {
98
+ var state;
99
+ var step;
100
+ state = History.getState();
101
+ step = state.data.step;
102
+ if(step !== wizard.currentStep){
103
+ if(wizard.options.validationEnabled && step === wizard._navigate(wizard.currentStep)){
104
+ if(!wizard.element.valid()){
105
+ wizard._updateHistory(wizard.currentStep);
106
+ wizard.element.validate().focusInvalid();
107
+
108
+ return false;
109
+ }
110
+ }
111
+ if(step !== wizard.currentStep)
112
+ wizard._show(step);
113
+ }
114
+ });
115
+ }
116
+
117
+ this.element.addClass("ui-formwizard");
118
+ this.element.find(":input").addClass("ui-wizard-content");
119
+ this.steps.addClass("ui-formwizard-content");
120
+ this.backButton.addClass("ui-formwizard-button ui-wizard-content");
121
+ this.nextButton.addClass("ui-formwizard-button ui-wizard-content");
122
+
123
+ if(!this.options.disableUIStyles){
124
+ this.element.addClass("ui-helper-reset ui-widget ui-widget-content ui-helper-reset ui-corner-all");
125
+ this.element.find(":input").addClass("ui-helper-reset ui-state-default");
126
+ this.steps.addClass("ui-helper-reset ui-corner-all");
127
+ this.backButton.addClass("ui-helper-reset ui-state-default");
128
+ this.nextButton.addClass("ui-helper-reset ui-state-default");
129
+ }
130
+ this._show(undefined);
131
+ return $(this);
132
+ },
133
+
134
+ _updateButtons : function(){
135
+ var wizard = this;
136
+
137
+ this.nextButton = this.element.find(this.options.next);
138
+ this.nextButton.each(function() {
139
+ var events = $(this).data("events");
140
+ if (typeof events === "undefined" ||
141
+ typeof events['click'] === "undefined" ||
142
+ events['click'].length === 0) {
143
+ $(this).click(function() {
144
+ var nextClickCallbackData = {
145
+ "currentStep": wizard.currentStep,
146
+ "button": $(this).attr("id")
147
+ };
148
+ $(wizard.element).trigger('next_click', nextClickCallbackData);
149
+ return wizard._next();
150
+ });
151
+ }
152
+ });
153
+
154
+ this.nextButtonInitinalValue = this.nextButton.val();
155
+ this.nextButton.val(this.options.textNext);
156
+
157
+ this.backButton = this.element.find(this.options.back);
158
+ this.backButton.each(function() {
159
+ var events = $(this).data('events');
160
+ if (typeof events === "undefined" ||
161
+ typeof events['click'] === "undefined" ||
162
+ events['click'].length === 0) {
163
+ $(this).click(function() {
164
+ var backClickCallbackData = {
165
+ "currentStep": wizard.currentStep,
166
+ "button": $(this).attr("id")
167
+ };
168
+ $(wizard.element).trigger('back_click', backClickCallbackData);
169
+ wizard._back();
170
+ return false;
171
+ });
172
+ }
173
+ });
174
+
175
+ this.backButtonInitinalValue = this.backButton.val();
176
+ this.backButton.val(this.options.textBack);
177
+ },
178
+
179
+ _next : function(){
180
+ if(this.options.validationEnabled){
181
+ if(!this.element.valid()){
182
+ this.element.validate().focusInvalid();
183
+ return false;
184
+ }
185
+ }
186
+
187
+ if(this.options.remoteAjax != undefined){
188
+ var options = this.options.remoteAjax[this.currentStep];
189
+ var wizard = this;
190
+ if(options !== undefined){
191
+ var success = options.success;
192
+ var beforeSend = options.beforeSend;
193
+ var complete = options.complete;
194
+
195
+ options = $.extend({},options,{
196
+ beforeSend : function(xhr){
197
+ wizard._disableNavigation();
198
+ if(beforeSend !== undefined)
199
+ beforeSend(xhr);
200
+ $(wizard.element).trigger('before_remote_ajax', {
201
+ "xhr": xhr,
202
+ "currentStep" : wizard.currentStep
203
+ });
204
+ },
205
+ complete : function(xhr, statusText){
206
+ if(complete !== undefined)
207
+ complete(xhr, statusText);
208
+ $(wizard.element).trigger('after_remote_ajax', {
209
+ "xhr": xhr,
210
+ "statusText": statusText,
211
+ "currentStep" : wizard.currentStep
212
+ });
213
+
214
+ if (statusText === "success") {
215
+ wizard._continueToNextStep();
216
+ }
217
+
218
+ wizard._enableNavigation();
219
+ }
220
+ });
221
+ this.element.ajaxSubmit(options);
222
+ return false;
223
+ }
224
+ }
225
+
226
+ return this._continueToNextStep();
227
+ },
228
+
229
+ _back : function(){
230
+ if(this.activatedSteps.length > 0){
231
+ if(this.options.historyEnabled){
232
+ this._updateHistory(this.activatedSteps[this.activatedSteps.length - 2]);
233
+ }else{
234
+ this._show(this.activatedSteps[this.activatedSteps.length - 2], true);
235
+ }
236
+ }
237
+ return false;
238
+ },
239
+
240
+ _continueToNextStep : function(){
241
+ if(this.isLastStep){
242
+ for(var i = 0; i < this.activatedSteps.length; i++){
243
+ this.steps.filter("#" + this.activatedSteps[i]).find(":input").not(".wizard-ignore").removeAttr("disabled");
244
+ }
245
+ if(!this.options.formPluginEnabled){
246
+ return true;
247
+ }else{
248
+ this._disableNavigation();
249
+ this.element.ajaxSubmit(this.options.formOptions);
250
+ return false;
251
+ }
252
+ }
253
+
254
+ var step = this._navigate(this.currentStep);
255
+ if(step == this.currentStep){
256
+ return false;
257
+ }
258
+ if(this.options.historyEnabled){
259
+ this._updateHistory(step);
260
+ }else{
261
+ this._show(step, true);
262
+ }
263
+ return false;
264
+ },
265
+
266
+ _updateHistory : function(step) {
267
+ var state = {};
268
+ var title = $("title").text();
269
+ state["step"] = step;
270
+ History.pushState(state, title, step);
271
+ },
272
+
273
+ _disableNavigation : function(){
274
+ this.nextButton.attr("disabled","disabled");
275
+ this.backButton.attr("disabled","disabled");
276
+ if(!this.options.disableUIStyles){
277
+ this.nextButton.removeClass("ui-state-active").addClass("ui-state-disabled");
278
+ this.backButton.removeClass("ui-state-active").addClass("ui-state-disabled");
279
+ }
280
+ },
281
+
282
+ _setNavButtonValues : function(){
283
+ if(this.isLastStep){
284
+ this.nextButton.val(this.options.textSubmit);
285
+ }else{
286
+ this.nextButton.val(this.options.textNext);
287
+ }
288
+ },
289
+
290
+ _enableNavigation : function(){
291
+ if(this.nextButton.val() === ""){
292
+ this._setNavButtonValues();
293
+ }
294
+
295
+ if($.trim(this.currentStep) !== this.steps.eq(0).attr("id")){
296
+ this.backButton.removeAttr("disabled");
297
+ if(!this.options.disableUIStyles){
298
+ this.backButton.removeClass("ui-state-disabled").addClass("ui-state-active");
299
+ }
300
+ }
301
+
302
+ this.nextButton.removeAttr("disabled");
303
+ if(!this.options.disableUIStyles){
304
+ this.nextButton.removeClass("ui-state-disabled").addClass("ui-state-active");
305
+ }
306
+ },
307
+
308
+ _animate : function(oldStep, newStep, stepShownCallback){
309
+ this._disableNavigation();
310
+ var old = this.steps.filter("#" + oldStep);
311
+ var current = this.steps.filter("#" + newStep);
312
+ old.find(":input").not(".wizard-ignore").attr("disabled","disabled");
313
+ current.find(":input").not(".wizard-ignore").removeAttr("disabled");
314
+ var wizard = this;
315
+ old.animate(wizard.options.outAnimation, wizard.options.outDuration, wizard.options.easing, function(){
316
+ current.animate(wizard.options.inAnimation, wizard.options.inDuration, wizard.options.easing, function(){
317
+ if(wizard.options.focusFirstInput)
318
+ current.find(":input:first").focus();
319
+ wizard._enableNavigation();
320
+
321
+ stepShownCallback.apply(wizard);
322
+ });
323
+ return;
324
+ });
325
+ },
326
+
327
+ _checkIflastStep : function(step){
328
+ this.isLastStep = false;
329
+ if($("#" + step).hasClass(this.options.submitStepClass) || this.steps.filter(":last").attr("id") == step){
330
+ this.isLastStep = true;
331
+ }
332
+ },
333
+
334
+ _getLink : function(step){
335
+ var link = undefined;
336
+ var links = this.steps.filter("#" + step).find(this.options.linkClass);
337
+
338
+ if(links != undefined){
339
+ if(links.filter(":radio,:checkbox").size() > 0){
340
+ link = links.filter(this.options.linkClass + ":checked").val();
341
+ }else{
342
+ link = $(links).val();
343
+ }
344
+ }
345
+ return link;
346
+ },
347
+
348
+ _navigate : function(step){
349
+ var link = this._getLink(step);
350
+ if(link != undefined){
351
+ if((link != "" && link != null && link != undefined) && this.steps.filter("#" + link).attr("id") != undefined){
352
+ return link;
353
+ }
354
+ return this.currentStep;
355
+ }else if(link == undefined && !this.isLastStep){
356
+ var step1 = this.steps.filter("#" + step).next().attr("id");
357
+ return step1;
358
+ }
359
+ },
360
+
361
+ _stepFromPath : function() {
362
+ var path = window.location.pathname;
363
+ var pathComponents = path.split('/');
364
+ for (var i = pathComponents.length; i >= 0; i--) {
365
+ if (pathComponents[i] !== "") {
366
+ return pathComponents[i];
367
+ }
368
+ }
369
+ },
370
+
371
+ _show : function(step){
372
+ var backwards = false;
373
+ var triggerStepShown = step !== undefined;
374
+ var fragment;
375
+ if(step == undefined || step == ""){
376
+ step = this._stepFromPath() || this.firstStep;
377
+ this.activatedSteps.pop();
378
+ this.activatedSteps.push(step);
379
+ }else{
380
+ if($.inArray(step, this.activatedSteps) > -1){
381
+ backwards = true;
382
+ this.activatedSteps.pop();
383
+ }else {
384
+ this.activatedSteps.push(step);
385
+ }
386
+ }
387
+
388
+ if(this.currentStep !== step || step === this.firstStep){
389
+ this.previousStep = this.currentStep;
390
+ this._checkIflastStep(step);
391
+ this.currentStep = step;
392
+ var stepShownCallback = function(){if(triggerStepShown)$(this.element).trigger('step_shown', $.extend({"isBackNavigation" : backwards},this._state()));};
393
+ this._animate(this.previousStep, step, stepShownCallback);
394
+ };
395
+
396
+
397
+ },
398
+
399
+ _reset : function(){
400
+ this.element.resetForm()
401
+ $("label,:input,textarea",this).removeClass("error");
402
+ for(var i = 0; i < this.activatedSteps.length; i++){
403
+ this.steps.filter("#" + this.activatedSteps[i]).hide().find(":input").attr("disabled","disabled");
404
+ }
405
+ this.activatedSteps = new Array();
406
+ this.previousStep = undefined;
407
+ this.isLastStep = false;
408
+ if(this.options.historyEnabled){
409
+ this._updateHistory(this.firstStep);
410
+ }else{
411
+ this._show(this.firstStep);
412
+ }
413
+
414
+ },
415
+
416
+ _state : function(state){
417
+ var currentState = { "settings" : this.options,
418
+ "activatedSteps" : this.activatedSteps,
419
+ "isLastStep" : this.isLastStep,
420
+ "isFirstStep" : this.currentStep === this.firstStep,
421
+ "previousStep" : this.previousStep,
422
+ "currentStep" : this.currentStep,
423
+ "backButton" : this.backButton,
424
+ "nextButton" : this.nextButton,
425
+ "steps" : this.steps,
426
+ "firstStep" : this.firstStep
427
+ }
428
+
429
+ if(state !== undefined)
430
+ return currentState[state];
431
+
432
+ return currentState;
433
+ },
434
+
435
+ /*Methods*/
436
+
437
+ show : function(step){
438
+ if(this.options.historyEnabled){
439
+ this._updateHistory(step);
440
+ }else{
441
+ this._show(step);
442
+ }
443
+ },
444
+
445
+ state : function(state){
446
+ return this._state(state);
447
+ },
448
+
449
+ reset : function(){
450
+ this._reset();
451
+ },
452
+
453
+ next : function(){
454
+ this._next();
455
+ },
456
+
457
+ back : function(){
458
+ this._back();
459
+ },
460
+
461
+ destroy: function() {
462
+ this.element.find("*").removeAttr("disabled").show();
463
+ this.nextButton.unbind("click").val(this.nextButtonInitinalValue).removeClass("ui-state-disabled").addClass("ui-state-active");
464
+ this.backButton.unbind("click").val(this.backButtonInitinalValue).removeClass("ui-state-disabled").addClass("ui-state-active");
465
+ this.backButtonInitinalValue = undefined;
466
+ this.nextButtonInitinalValue = undefined;
467
+ this.activatedSteps = undefined;
468
+ this.previousStep = undefined;
469
+ this.currentStep = undefined;
470
+ this.isLastStep = undefined;
471
+ this.options = undefined;
472
+ this.nextButton = undefined;
473
+ this.backButton = undefined;
474
+ this.formwizard = undefined;
475
+ this.element = undefined;
476
+ this.steps = undefined;
477
+ this.firstStep = undefined;
478
+ },
479
+
480
+ update_steps : function(){
481
+ this.steps = this.element.find(".step").addClass("ui-formwizard-content");
482
+ this.steps.not("#" + this.currentStep).hide().find(":input").addClass("ui-wizard-content").attr("disabled","disabled");
483
+ this._updateButtons();
484
+ this._checkIflastStep(this.currentStep);
485
+ this._enableNavigation();
486
+ if(!this.options.disableUIStyles){
487
+ this.steps.addClass("ui-helper-reset ui-corner-all");
488
+ this.steps.find(":input").addClass("ui-helper-reset ui-state-default");
489
+ }
490
+ },
491
+
492
+ options: {
493
+ historyEnabled : false,
494
+ validationEnabled : false,
495
+ validationOptions : undefined,
496
+ formPluginEnabled : false,
497
+ linkClass : ".link",
498
+ submitStepClass : "submit_step",
499
+ back : ":reset",
500
+ next : ":submit",
501
+ textSubmit : 'Submit',
502
+ textNext : 'Next',
503
+ textBack : 'Back',
504
+ remoteAjax : undefined,
505
+ inAnimation : {opacity: 'show'},
506
+ outAnimation: {opacity: 'hide'},
507
+ inDuration : 400,
508
+ outDuration: 400,
509
+ easing: 'swing',
510
+ focusFirstInput : false,
511
+ disableInputFields : true,
512
+ formOptions : { reset: true, success: function(data) { if( (window['console'] !== undefined) ){console.log("%s", "form submit successful");}},
513
+ disableUIStyles : false
514
+ }
454
515
  }
455
516
  });
456
517
  })(jQuery);