Skip to content

Commit

Permalink
Add custom message support
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillplatonov committed May 30, 2019
1 parent 398a18b commit 5ab7184
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ let form = document.querySelector('#form')
new DirtyForm(form)
```

If you want to customize the message:

```javascript
new DirtyForm(form, {
message: 'You have unsaved changes. Are you sure you want to leave?',
})
```

### Stimulus example

```html
Expand Down
7 changes: 5 additions & 2 deletions dist/dirty-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,15 @@ var DirtyForm =
/*#__PURE__*/
function () {
function DirtyForm(form) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

_classCallCheck(this, DirtyForm);

this.form = form;
this.isDirty = false;
this.initialValues = {};
this.fields = [].concat(_toConsumableArray(this.form.elements), _toConsumableArray(this.form.querySelectorAll('trix-editor')));
this.message = options['message'] || 'You have unsaved changes!';
this.setupFields();
this.setFormHandlers();
}
Expand Down Expand Up @@ -158,13 +161,13 @@ function () {

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

if (typeof Turbolinks !== 'undefined') {
document.addEventListener('turbolinks:before-visit', function (event) {
if (_this2.isDirty && !confirm('You have unsaved changes!')) {
if (_this2.isDirty && !confirm(_this2.message)) {
event.preventDefault();
} else {
_this2.isDirty = false;
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.3.0",
"version": "0.4.0",
"description": "A lightweight plugin to prevent losing data when editing forms. No dependencies.",
"main": "dist/dirty-form.js",
"files": [
Expand Down
7 changes: 4 additions & 3 deletions src/dirty-form.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
class DirtyForm {
constructor(form) {
constructor(form, options = {}) {
this.form = form;
this.isDirty = false;
this.initialValues = {};
this.fields = [
...this.form.elements,
...this.form.querySelectorAll('trix-editor')
]
this.message = options['message'] || 'You have unsaved changes!';

this.setupFields();
this.setFormHandlers();
Expand Down Expand Up @@ -39,12 +40,12 @@ class DirtyForm {
// Handle leaving page
window.onbeforeunload = () => {
if (this.isDirty) {
return 'You have unsaved changes!';
return this.message;
}
};
if (typeof Turbolinks !== 'undefined') {
document.addEventListener('turbolinks:before-visit', (event) => {
if (this.isDirty && !confirm('You have unsaved changes!')) {
if (this.isDirty && !confirm(this.message)) {
event.preventDefault()
} else {
this.isDirty = false;
Expand Down

0 comments on commit 5ab7184

Please sign in to comment.