forked from jeppe-smith/umbraco-ajax-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathumbraco-ajax-form.js
119 lines (91 loc) · 2.8 KB
/
umbraco-ajax-form.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
class UmbracoAjaxForm {
constructor(formGuid, options) {
this.steps = []
this.formGuid = formGuid
// Setup options with fallbacks
this.options = Object.assign({
onsuccess() { return true },
onerror() { return true }
}, options)
this.currentStep = $(this.selector)
}
get selector() {
return `#contour_form_${this.formGuid.replace(/-/g, '')} form`
}
get currentStep() {
return this.steps[this.currentStepIndex]
}
get previousStep() {
return this.steps[this.currentStepIndex - 1]
}
set currentStep(form) {
const previousButton = form.find('input[name="__prev"]')
// Set type to button so submitting by hitting
// return doesn't cause a step back
previousButton.attr('type', 'button')
this.currentStepIndex = parseInt( form.find('input[name="FormStep"]').val() )
this.steps[this.currentStepIndex] = form
form[0].onsubmit = this.handleSubmit.bind(this)
previousButton.on('click', this.goToPrevious.bind(this))
}
/*
* Handles the submit event fired when clicking `next` and `submit`
*
* @param event | The submit event
* @return options.onsubmit {Function}
*/
handleSubmit(event) {
const form = this.currentStep
const submitted = form.data('submitted')
const action = form.attr('action')
const method = form.attr('method')
const data = form.serialize()
event.preventDefault()
if (submitted || !form.valid()) { return true }
form.data('submitted', true)
$.ajax({
url: action,
method: method,
data: data
})
.done(this.handleSuccess.bind(this))
.fail(this.handleError.bind(this))
}
/*
* Replaces current step in view with previous from steps array
*
* @param event | The submit event
* @return replaced {Boolean}
*/
goToPrevious(event) {
event.preventDefault()
this.previousStep
.find('input[disabled="disabled"]')
.prop('disabled', false)
this.previousStep
.find('input[name="__prev"]')
.on('click', this.goToPrevious.bind(this))
this.previousStep[0].onsubmit = this.handleSubmit.bind(this)
this.previousStep.data('submitted', false)
this.currentStep.replaceWith(this.previousStep)
this.currentStep = this.previousStep
}
handleSuccess(data) {
const message = $(data).find('.contourMessageOnSubmit').text()
let nextStep = $(data).find(this.selector)
const nextStepIndex = parseInt( nextStep.find('input[name="FormStep"]').val() )
// If there's a form in the response then
// it's the next page in the form and should be showed
if (nextStep.length) {
nextStep = this.steps[nextStepIndex] || nextStep
this.currentStep.replaceWith(nextStep)
this.currentStep = nextStep
} else {
return this.options.onsuccess.call(this.currentStep[0], message)
}
}
handleError(jqHXR, textStatus, error) {
return options.onerror.call(this.currentSteo[0], error)
}
}
export default UmbracoAjaxForm