diff options
author | Garren Smith <garren.smith@gmail.com> | 2013-12-09 14:49:24 +0200 |
---|---|---|
committer | Garren Smith <garren.smith@gmail.com> | 2013-12-09 14:49:24 +0200 |
commit | 476845d4f9cdbcd120908d1c6c9f4d970ff10238 (patch) | |
tree | 755ee31132fda14675cec9face73ef0b1c6a227b | |
parent | c309e117c2ca67e619f0ba067b4d3f1296449553 (diff) | |
download | couchdb-476845d4f9cdbcd120908d1c6c9f4d970ff10238.tar.gz |
Hook up beforeunload
-rw-r--r-- | src/fauxton/app/api.js | 8 | ||||
-rw-r--r-- | src/fauxton/app/modules/documents/views.js | 2 | ||||
-rw-r--r-- | src/fauxton/app/modules/fauxton/components.js | 11 | ||||
-rw-r--r-- | src/fauxton/app/router.js | 19 |
4 files changed, 32 insertions, 8 deletions
diff --git a/src/fauxton/app/api.js b/src/fauxton/app/api.js index f305c33f6..b9ee8ea5f 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); diff --git a/src/fauxton/app/modules/documents/views.js b/src/fauxton/app/modules/documents/views.js index a7f182470..7f3e80b42 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; diff --git a/src/fauxton/app/modules/fauxton/components.js b/src/fauxton/app/modules/fauxton/components.js index 7248010f1..5255626ba 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); diff --git a/src/fauxton/app/router.js b/src/fauxton/app/router.js index 3d8b12559..e3a1636aa 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) { |