Skip to content

Commit

Permalink
[FEATURE] Custom Event listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Sommer committed Sep 14, 2018
1 parent ad78263 commit 48ea44a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ Onedrop:
includeJavascript: true
```
## JS Events
You can use 2 events to register you event listens.
Before Send Ajax Form `Onedrop.AjaxForm:before`

After render Ajax Form to HTML `Onedrop.AjaxForm:after`

### Form reload

As the content of your form is being replaced by the confirmation message you usually want
Expand Down
13 changes: 11 additions & 2 deletions Resources/Private/TypeScript/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ class AjaxForm {
private _form: HTMLFormElement;
private _request: XMLHttpRequest;
private _submitter: HTMLInputElement;
private _customEventBefore: Event = new Event('Onedrop.AjaxForm:before');
private _customEventAfter: Event = new Event('Onedrop.AjaxForm:after');

/**
* @param {Element} element
*/
constructor(element: Element) {
this._delegator = element;
if (element.hasAttribute('data-ajax-uri')) {
Expand All @@ -22,7 +27,8 @@ class AjaxForm {
}

/**
* Ajax helper to send only one simultaneous request
* Ajax helper to send only one simultaneous request
*
* @param {FormData} formData
* @param {Function} callback
*/
Expand All @@ -43,6 +49,7 @@ class AjaxForm {
private handleResponse() {
if (this._request.status >= 200 && this._request.status < 400) {
this._delegator.innerHTML = this._request.response;
window.dispatchEvent(this._customEventAfter);
this._form = <HTMLFormElement>this._delegator.querySelector('form');
// another form step has been returned, so we keep handling the form
if (this._form) {
Expand All @@ -63,6 +70,7 @@ class AjaxForm {

/**
* Capture the form submission and convert it to a ajax operation
*
* @param {Event} e
*/
private onFormSubmit(e: Event) {
Expand All @@ -72,6 +80,7 @@ class AjaxForm {
formData.append(this._submitter.name, this._submitter.value);
this._submitter = null;
}
window.dispatchEvent(this._customEventBefore);
this.ajax(formData, this.handleResponse.bind(this));
}

Expand All @@ -95,7 +104,7 @@ class AjaxForm {
* INITIALIZE ALL ELEMENTS
*/
(function () {
document.addEventListener("DOMContentLoaded", function() {
document.addEventListener("DOMContentLoaded", function () {
let ajaxForms = document.querySelectorAll('[data-ajax="ajax-form"]');
for (let i = 0; i < ajaxForms.length; i++) {
new AjaxForm(ajaxForms.item(i));
Expand Down
2 changes: 1 addition & 1 deletion Resources/Private/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"target": "es5"
},
"files": [
"TypeScript/form"
"./TypeScript/form.ts"
],
"compileOnSave": true
}
4 changes: 4 additions & 0 deletions Resources/Public/JavaScript/form.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';
var AjaxForm = (function () {
function AjaxForm(element) {
this._customEventBefore = new Event('Onedrop.AjaxForm:before');
this._customEventAfter = new Event('Onedrop.AjaxForm:after');
this._delegator = element;
if (element.hasAttribute('data-ajax-uri')) {
this._formUri = element.getAttribute('data-ajax-uri');
Expand All @@ -27,6 +29,7 @@ var AjaxForm = (function () {
var _this = this;
if (this._request.status >= 200 && this._request.status < 400) {
this._delegator.innerHTML = this._request.response;
window.dispatchEvent(this._customEventAfter);
this._form = this._delegator.querySelector('form');
if (this._form) {
this.bindFormSubmitListener();
Expand All @@ -50,6 +53,7 @@ var AjaxForm = (function () {
formData.append(this._submitter.name, this._submitter.value);
this._submitter = null;
}
window.dispatchEvent(this._customEventBefore);
this.ajax(formData, this.handleResponse.bind(this));
};
AjaxForm.prototype.bindFormSubmitListener = function () {
Expand Down

0 comments on commit 48ea44a

Please sign in to comment.