From b6fb5603595e5fd8f5d3d5de7cb56cd36deafc51 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Thu, 25 Jun 2015 10:34:14 +0930 Subject: [PATCH] Implement onbeforeunload composer confirmation. closes flarum/core#113 --- .../core/js/forum/src/components/composer-body.js | 2 +- framework/core/js/forum/src/components/composer.js | 11 ++++++++++- .../js/forum/src/components/discussion-composer.js | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/framework/core/js/forum/src/components/composer-body.js b/framework/core/js/forum/src/components/composer-body.js index 71dab79be..631548222 100644 --- a/framework/core/js/forum/src/components/composer-body.js +++ b/framework/core/js/forum/src/components/composer-body.js @@ -49,7 +49,7 @@ export default class ComposerBody extends Component { } preventExit() { - return this.content() && this.content() != this.props.originalContent && !confirm(this.props.confirmExit); + return this.content() && this.content() != this.props.originalContent && this.props.confirmExit; } onsubmit(value) { diff --git a/framework/core/js/forum/src/components/composer.js b/framework/core/js/forum/src/components/composer.js index 609eb2a8f..84729ab3b 100644 --- a/framework/core/js/forum/src/components/composer.js +++ b/framework/core/js/forum/src/components/composer.js @@ -70,6 +70,10 @@ class Composer extends Component { $(document) .on('mousemove', this.handlers.onmousemove = this.onmousemove.bind(this)) .on('mouseup', this.handlers.onmouseup = this.onmouseup.bind(this)); + + window.onbeforeunload = e => { + return this.component && this.component.preventExit(); + }; } configHandle(element, isInitialized) { @@ -129,7 +133,12 @@ class Composer extends Component { } preventExit() { - return this.component && this.component.preventExit(); + if (this.component) { + var preventExit = this.component.preventExit(); + if (preventExit) { + return !confirm(preventExit); + } + } } render(anchorToBottom) { diff --git a/framework/core/js/forum/src/components/discussion-composer.js b/framework/core/js/forum/src/components/discussion-composer.js index e6b76de38..8197fe5e1 100644 --- a/framework/core/js/forum/src/components/discussion-composer.js +++ b/framework/core/js/forum/src/components/discussion-composer.js @@ -68,7 +68,7 @@ export default class DiscussionComposer extends ComposerBody { } preventExit() { - return (this.title() || this.content()) && !confirm(this.props.confirmExit); + return (this.title() || this.content()) && this.props.confirmExit; } data() {