Hook up beforeunload
Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/476845d4 Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/476845d4 Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/476845d4 Branch: refs/heads/beforeunload Commit: 476845d4f9cdbcd120908d1c6c9f4d970ff10238 Parents: c309e11 Author: Garren Smith <[email protected]> Authored: Mon Dec 9 14:49:24 2013 +0200 Committer: Garren Smith <[email protected]> Committed: Mon Dec 9 14:49:24 2013 +0200 ---------------------------------------------------------------------- src/fauxton/app/api.js | 8 ++++++-- src/fauxton/app/modules/documents/views.js | 2 ++ src/fauxton/app/modules/fauxton/components.js | 11 +++++++---- src/fauxton/app/router.js | 19 +++++++++++++++++-- 4 files changed, 32 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb/blob/476845d4/src/fauxton/app/api.js ---------------------------------------------------------------------- diff --git a/src/fauxton/app/api.js b/src/fauxton/app/api.js index f305c33..b9ee8ea 100644 --- a/src/fauxton/app/api.js +++ b/src/fauxton/app/api.js @@ -61,8 +61,12 @@ function(app, Fauxton) { app.router.navigate(url,options); }; - FauxtonAPI.beforeUnload = app.router.beforeUnload; - FauxtonAPI.removeBeforeUnload = app.router.removeBeforeUnload; + FauxtonAPI.beforeUnload = function () { + app.router.beforeUnload.apply(app.router, arguments); + } + FauxtonAPI.removeBeforeUnload = function () { + app.router.removeBeforeUnload.apply(app.router, arguments); + } FauxtonAPI.addHeaderLink = function(link) { app.masterLayout.navBar.addLink(link); http://git-wip-us.apache.org/repos/asf/couchdb/blob/476845d4/src/fauxton/app/modules/documents/views.js ---------------------------------------------------------------------- diff --git a/src/fauxton/app/modules/documents/views.js b/src/fauxton/app/modules/documents/views.js index a7f1824..7f3e80b 100644 --- a/src/fauxton/app/modules/documents/views.js +++ b/src/fauxton/app/modules/documents/views.js @@ -814,6 +814,7 @@ function(app, FauxtonAPI, Components, Documents, Databases, pouchdb, resizeColum saveDoc: function(event) { var json, notification, that = this, + editor = this.editor, validDoc = this.getDocFromEditor(); if (validDoc) { @@ -822,6 +823,7 @@ function(app, FauxtonAPI, Components, Documents, Databases, pouchdb, resizeColum notification = FauxtonAPI.addNotification({msg: "Saving document."}); this.model.save().then(function () { + editor.editSaved(); FauxtonAPI.navigate('/database/' + that.database.id + '/' + that.model.id); }).fail(function(xhr) { var responseText = JSON.parse(xhr.responseText).reason; http://git-wip-us.apache.org/repos/asf/couchdb/blob/476845d4/src/fauxton/app/modules/fauxton/components.js ---------------------------------------------------------------------- diff --git a/src/fauxton/app/modules/fauxton/components.js b/src/fauxton/app/modules/fauxton/components.js index 7248010..5255626 100644 --- a/src/fauxton/app/modules/fauxton/components.js +++ b/src/fauxton/app/modules/fauxton/components.js @@ -228,7 +228,6 @@ function(app, FauxtonAPI, ace) { this.editor.getSession().on('change', function () { that.setHeightToLineCount(); that.edited = true; - console.log('edited'); }); $(window).on('beforeunload.editor', function() { @@ -237,16 +236,16 @@ function(app, FauxtonAPI, ace) { } }); - api.beforeUnload("editor", function (deferred) { + FauxtonAPI.beforeUnload("editor", function (deferred) { if (that.edited) { - return 'Your changes have not been saved. Click cancel to return to the document.'); + return 'Your changes have not been saved. Click cancel to return to the document.'; } }); }, cleanup: function () { $(window).off('beforeunload.editor'); - api.removeBeforeunload("editor"); + FauxtonAPI.removeBeforeUnload("editor"); }, setHeightToLineCount: function () { @@ -283,6 +282,10 @@ function(app, FauxtonAPI, ace) { }); }, + editSaved: function () { + this.edited = false; + }, + setValue: function (data, lineNumber) { lineNumber = lineNumber ? lineNumber : -1; this.editor.setValue(data, lineNumber); http://git-wip-us.apache.org/repos/asf/couchdb/blob/476845d4/src/fauxton/app/router.js ---------------------------------------------------------------------- diff --git a/src/fauxton/app/router.js b/src/fauxton/app/router.js index 3d8b125..e3a1636 100644 --- a/src/fauxton/app/router.js +++ b/src/fauxton/app/router.js @@ -53,11 +53,26 @@ function(req, app, Initialize, FauxtonAPI, Fauxton, Layout, Databases, Documents routes: {}, beforeUnload: function (name, fn) { - beforeUnload[name] = fn; + beforeUnloads[name] = fn; + }, + + removeBeforeUnload: function (name) { + delete beforeUnloads[name]; }, navigate: function (fragment, trigger) { - Backbone.Router.prototype.navigate(fragment, trigger); + var continueNav = true, + msg = _.find(_.map(beforeUnloads, function (fn) { return fn(); }), function (beforeReturn) { + if (beforeReturn) { return true; } + }); + + if (msg) { + continueNav = window.confirm(msg); + } + + if (continueNav) { + Backbone.Router.prototype.navigate(fragment, trigger); + } }, addModuleRouteObject: function(RouteObject) {
