-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
163 lines (138 loc) · 3.8 KB
/
script.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
var $body = $("body");
var $progressBar = $("progress");
var $animContainer = $(".animation-container");
var value = 0;
var transitionEnd = "webkitTransitionEnd transitionend";
/**
* Resets the form back to the default state.
* ==========================================
*/
function formReset() {
value = 0;
$progressBar.val(value);
// $('form input').not('button').not('radio').val('').removeClass('hasInput');
$(".js-form-step").removeClass("left leaving");
$(".js-form-step")
.not('.js-form-step[data-step="1"]')
.addClass("hidden waiting");
$('.js-form-step[data-step="1"]').removeClass("hidden");
$(".form-progress-indicator")
.not(".one")
.removeClass("active");
$animContainer.css({
paddingBottom: $('.js-form-step[data-step="1"]').height() + "px"
});
console.warn("Form reset.");
return false;
}
/**
* Sets up the click handlers on the form. Next/reset.
* ===================================================
*/
function setupClickHandlers() {
// Show next form on continue click
$('button[type="next"]').on("click", function(event) {
event.preventDefault();
var $currentForm = $(this).parents(".js-form-step");
showNextForm($currentForm);
});
// Reset form on reset button click
$(".js-reset").on("click", function() {
formReset();
});
return false;
}
/**
* Shows the next form.
* @param - Node - The current form.
* ======================================
*/
function showNextForm($currentForm) {
var currentFormStep = parseInt($currentForm.attr("data-step")) || false;
var $nextForm = $('.js-form-step[data-step="' + (currentFormStep + 1) + '"]');
console.log("Current step is " + currentFormStep);
console.log("The next form is # " + $nextForm.attr("data-step"));
$body.addClass("freeze");
// Ensure top of form is in view
$("html, body").animate(
{
scrollTop: $progressBar.offset().top
},
"fast"
);
// Hide current form fields
$currentForm.addClass("leaving");
setTimeout(function() {
$currentForm.addClass("hidden");
}, 500);
// Animate container to height of form
$animContainer.css({
paddingBottom: $nextForm.height() + "px"
});
// Show next form fields
$nextForm
.removeClass("hidden")
.addClass("coming")
.one(transitionEnd, function() {
$nextForm.removeClass("coming waiting");
});
// Increment value (based on 4 steps 0 - 100)
value += 33;
// Reset if we've reached the end
if (value >= 100) {
formReset();
} else {
$(".form-progress")
.find(".form-progress-indicator.active")
.next(".form-progress-indicator")
.addClass("active");
// Set progress bar to the next value
$progressBar.val(value);
}
// Update hidden progress descriptor (for a11y)
$(".js-form-progress-completion").html($progressBar.val() + "% complete");
$body.removeClass("freeze");
return false;
}
/**
* Sets up and handles the float labels on the inputs.
=====================================================
*/
function setupFloatLabels() {
// Check the inputs to see if we should keep the label floating or not
$("form input")
.not("button")
.not("radio")
.on("blur", function() {
// Different validation for different inputs
switch (this.tagName) {
case "SELECT":
if (this.value.length > 0) {
this.className = "hasInput";
} else {
this.className = "";
}
break;
case "INPUT":
if (this.value !== "") {
this.className = "hasInput";
} else {
this.className = "";
}
break;
default:
break;
}
});
return false;
}
/**
* Gets the party started.
* =======================
*/
function init() {
formReset();
setupFloatLabels();
setupClickHandlers();
}
init();