From 25e4e558c58bd54a35efaeb516ce44f4009d6406 Mon Sep 17 00:00:00 2001 From: Steffen van Bergerem Date: Wed, 8 Oct 2014 01:50:14 +0200 Subject: Ask for confirmation when leaving a submittable publisher --- app/assets/javascripts/app/views/publisher_view.js | 23 ++++++++++++++++------ app/views/status_messages/new.html.haml | 1 + 2 files changed, 18 insertions(+), 6 deletions(-) (limited to 'app') diff --git a/app/assets/javascripts/app/views/publisher_view.js b/app/assets/javascripts/app/views/publisher_view.js index 8fcca9316..a79db2cfc 100644 --- a/app/assets/javascripts/app/views/publisher_view.js +++ b/app/assets/javascripts/app/views/publisher_view.js @@ -48,6 +48,10 @@ app.views.Publisher = Backbone.View.extend({ // init autoresize plugin this.el_input.autoResize({ 'extraSpace' : 10, 'maxHeight' : Infinity }); + // if there is data in the publisher we ask for a confirmation + // before the user is able to leave the page + $(window).on('beforeunload', _.bind(this._beforeUnload, this)); + // sync textarea content if( this.el_hiddenInput.val() == "" ) { this.el_hiddenInput.val( this.el_input.val() ); @@ -77,14 +81,14 @@ app.views.Publisher = Backbone.View.extend({ this.close(); this.showSpinner(true); }); - + // open publisher on post error this.on('publisher:error', function() { this.open(); this.showSpinner(false); }); - // resetting the poll view + // resetting the poll view this.on('publisher:sync', function() { this.view_poll_creator.render(); }); @@ -159,7 +163,7 @@ app.views.Publisher = Backbone.View.extend({ if(evt){ evt.preventDefault(); } // Auto-adding a poll answer always leaves an empty box when the user starts - // typing in the last box. We'll delete the last one to avoid submitting an + // typing in the last box. We'll delete the last one to avoid submitting an // empty poll answer and failing validation. this.view_poll_creator.removeLastAnswer(); @@ -378,7 +382,7 @@ app.views.Publisher = Backbone.View.extend({ // enable input this.setInputEnabled(true); - + // enable buttons this.setButtonsEnabled(true); @@ -428,7 +432,7 @@ app.views.Publisher = Backbone.View.extend({ else this.$('#publisher_spinner').addClass('hidden'); }, - + checkSubmitAvailability: function() { if( this._submittable() ) { this.setButtonsEnabled(true); @@ -472,8 +476,15 @@ app.views.Publisher = Backbone.View.extend({ this.el_input.mentionsInput("val", function(value){ self.el_hiddenInput.val(value); }); - } + }, + _beforeUnload: function(e) { + if(this._submittable()){ + var confirmationMessage = Diaspora.I18n.t("confirm_unload"); + (e || window.event).returnValue = confirmationMessage; //Gecko + IE + return confirmationMessage; //Webkit, Safari, Chrome, etc. + } + } }); // jQuery helper for serializing a
into JSON diff --git a/app/views/status_messages/new.html.haml b/app/views/status_messages/new.html.haml index 1e6008944..20ae77532 100644 --- a/app/views/status_messages/new.html.haml +++ b/app/views/status_messages/new.html.haml @@ -14,6 +14,7 @@ app.publisher.open(); $("#publisher").bind('ajax:success', function(){ $("#mentionModal").modal('hide'); + app.publisher.clear(); location.reload(); }); }); -- cgit v1.2.3