-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
161 lines (135 loc) · 4.26 KB
/
index.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
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(['exports'], factory);
} else if (typeof exports !== "undefined") {
factory(exports);
} else {
var mod = {
exports: {}
};
factory(mod.exports);
global.umbracoAjaxForm = mod.exports;
}
})(this, function (exports) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
var _createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
var UmbracoAjaxForm = function () {
function UmbracoAjaxForm(formGuid, options) {
_classCallCheck(this, UmbracoAjaxForm);
this.steps = [];
this.formGuid = formGuid;
// Setup options with fallbacks
this.options = Object.assign({
onsuccess: function onsuccess() {
return true;
},
onerror: function onerror() {
return true;
}
}, options);
this.currentStep = $(this.selector);
}
_createClass(UmbracoAjaxForm, [{
key: 'handleSubmit',
value: function handleSubmit(event) {
var form = this.currentStep;
var submitted = form.data('submitted');
var action = form.attr('action');
var method = form.attr('method');
var 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));
}
}, {
key: 'goToPrevious',
value: function 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;
}
}, {
key: 'handleSuccess',
value: function handleSuccess(data) {
var message = $(data).find('.contourMessageOnSubmit').text();
var nextStep = $(data).find(this.selector);
var 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);
}
}
}, {
key: 'handleError',
value: function handleError(jqHXR, textStatus, error) {
return options.onerror.call(this.currentSteo[0], error);
}
}, {
key: 'selector',
get: function get() {
return '#contour_form_' + this.formGuid.replace(/-/g, '') + ' form';
}
}, {
key: 'currentStep',
get: function get() {
return this.steps[this.currentStepIndex];
},
set: function set(form) {
var 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));
}
}, {
key: 'previousStep',
get: function get() {
return this.steps[this.currentStepIndex - 1];
}
}]);
return UmbracoAjaxForm;
}();
exports.default = UmbracoAjaxForm;
});