wi-events/assets/eventregistration-ausbildun...

92 lines
3.0 KiB
JavaScript
Raw Normal View History

2023-02-12 21:30:52 +01:00
import scrollToElement from './scrollToElement.js';
var toastedOptions = {
2023-03-23 00:19:31 +01:00
position: "bottom-right",
duration: 3000,
fitToScreen: false,
fullWidth: false,
theme: "material",
2023-02-12 21:30:52 +01:00
};
export default function (toasted) {
var toasted = new toasted(toastedOptions);
return {
finished: false,
submitRequest: null,
errorFields: [],
active: 0,
slideTo(e, index) {
if (e !== null) {
e.preventDefault();
}
if (index < 0 || index > this.slides.length-1) {
return;
}
this.scrollForm(this.$refs.form);
this.active = index;
this.$refs.slider.scrollLeft = this.$refs.slider.scrollWidth / this.slides.length * index;
this.$refs.mobileSlider.scrollLeft = this.$refs.mobileSlider.scrollWidth / this.slides.length * index;
},
scrollForm(el) {
var margin = window.getComputedStyle(el).marginTop.replace('px', '');
scrollToElement(el, 300, (margin?margin:0) * -1);
},
submit() {
var _self = this;
var promise = fetch(window.location.href, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-WINTER-REQUEST-HANDLER': this.submitRequest,
'X-WINTER-REQUEST-PARTIALS': [],
'X-Requested-With': 'XMLHttpRequest',
},
body: JSON.stringify(this.data),
});
promise.then(function(response) {
if (response.status === 422) {
response.json().then((errors) => {
_self.scrollToFirstError(errors);
Object.keys(errors).forEach((field) => {
toasted.error(errors[field].join('<br>'));
});
});
}
if (response.status === 201) {
_self.finished = true;
_self.scrollForm(_self.$refs.form);
}
});
},
scrollToFirstError(errors) {
if (Object.keys(errors).length === 0) {
return;
}
var firstField = Object.keys(errors)[0];
var field = this.$refs.form.querySelector('[name="'+firstField+'"]');
if (field === null) {
return;
}
var slideElement = field.closest('.slider-element');
this.slideTo(null, Array.from(slideElement.parentNode.children).indexOf(slideElement));
},
prevButton: {
[':class']() {
return this.active == 0 ? 'opacity-40' : '';
}
},
nextButton: {
[':class']() {
return this.active == this.core.slides.length - 1 ? 'opacity-40' : '';
}
},
};
}