Skip to content

Commit

Permalink
Add turbolinks 5 support
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillplatonov committed May 30, 2019
1 parent 296b457 commit 398a18b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 19 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

> A lightweight plugin to prevent losing data when editing forms. No dependencies.
It supports [Trix editor](https://trix-editor.org) and [Turbolinks 5](https://github.com/turbolinks/turbolinks).

## Install

You can get it via `npm`:
Expand All @@ -25,7 +27,7 @@ let form = document.querySelector('#form')
new DirtyForm(form)
```

### Stimulus
### Stimulus example

```html
<%= form_with url: posts_path, html: { data: { controller: 'dirty-form' } } do |form| %>
Expand Down
29 changes: 20 additions & 9 deletions dist/dirty-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,27 @@ function () {
}, {
key: "setFormHandlers",
value: function setFormHandlers() {
var _this2 = this;

// Handle submit
window.addEventListener('submit', this.handleSubmit.bind(this));
this.form.addEventListener('submit', this.handleSubmit.bind(this));
window.onbeforeunload = this.handleUnload.bind(this);
this.form.addEventListener('submit', this.handleSubmit.bind(this)); // Handle leaving page

window.onbeforeunload = function () {
if (_this2.isDirty) {
return 'You have unsaved changes!';
}
};

if (typeof Turbolinks !== 'undefined') {
document.addEventListener('turbolinks:before-visit', function (event) {
if (_this2.isDirty && !confirm('You have unsaved changes!')) {
event.preventDefault();
} else {
_this2.isDirty = false;
}
});
}
}
}, {
key: "checkValue",
Expand All @@ -168,13 +186,6 @@ function () {
value: function handleSubmit() {
this.isDirty = false;
}
}, {
key: "handleUnload",
value: function handleUnload() {
if (this.isDirty) {
return 'You have unsaved changes!';
}
}
}]);

return DirtyForm;
Expand Down
2 changes: 1 addition & 1 deletion dist/dirty-form.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dirty-form",
"version": "0.2.3",
"version": "0.3.0",
"description": "A lightweight plugin to prevent losing data when editing forms. No dependencies.",
"main": "dist/dirty-form.js",
"files": [
Expand Down
24 changes: 17 additions & 7 deletions src/dirty-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,25 @@ class DirtyForm {
}

setFormHandlers() {
// Handle submit
window.addEventListener('submit', this.handleSubmit.bind(this));
this.form.addEventListener('submit', this.handleSubmit.bind(this));
window.onbeforeunload = this.handleUnload.bind(this);

// Handle leaving page
window.onbeforeunload = () => {
if (this.isDirty) {
return 'You have unsaved changes!';
}
};
if (typeof Turbolinks !== 'undefined') {
document.addEventListener('turbolinks:before-visit', (event) => {
if (this.isDirty && !confirm('You have unsaved changes!')) {
event.preventDefault()
} else {
this.isDirty = false;
}
});
}
}

checkValue(event) {
Expand All @@ -47,12 +63,6 @@ class DirtyForm {
handleSubmit() {
this.isDirty = false;
}

handleUnload() {
if (this.isDirty) {
return 'You have unsaved changes!';
}
}
}

module.exports = DirtyForm;

0 comments on commit 398a18b

Please sign in to comment.