From 508c72e5a73cba74908a1e93efbf36a852b89deb Mon Sep 17 00:00:00 2001 From: Alexis Deveria Date: Fri, 26 Mar 2010 17:51:41 +0000 Subject: [PATCH] Fixed major part of issue 459 by providing warning onbeforeunload when appropriate git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1473 eee81c28-f429-11dd-99c0-75d572ba1ddd --- editor/svg-editor.js | 21 ++++++++++++++++++++- editor/svgcanvas.js | 4 ++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/editor/svg-editor.js b/editor/svg-editor.js index 10e628e2..037cf2f8 100644 --- a/editor/svg-editor.js +++ b/editor/svg-editor.js @@ -143,6 +143,7 @@ svgCanvas.open = opts.open; } if(opts.save) { + show_save_warning = false; svgCanvas.bind("saved", opts.save); } } @@ -361,7 +362,8 @@ modKey = "", //(isMac ? "meta+" : "ctrl+"); path = svgCanvas.pathActions, default_img_url = curConfig.imgPath + "logo.png", - workarea = $("#workarea"); + workarea = $("#workarea"), + show_save_warning = false; // This sets up alternative dialog boxes. They mostly work the same way as // their UI counterparts, expect instead of returning the result, a callback @@ -445,6 +447,8 @@ var strokePaint = new $.jGraduate.Paint({solidColor: curConfig.initStroke.color}); var saveHandler = function(window,svg) { + show_save_warning = false; + // by default, we add the XML prolog back, systems integrating SVG-edit (wikis, CMSs) // can just provide their own custom save handler and might not want the XML prolog svg = "\n" + svg; @@ -525,6 +529,8 @@ selectedElement = elem; } } + + show_save_warning = true; // we update the contextual panel with potentially new // positional/sizing information (we DON'T want to update the @@ -2994,6 +3000,19 @@ $('#group_opacity').SpinButton({ step: 5, min: 0, max: 100, callback: changeOpacity }); $('#zoom').SpinButton({ min: 0.001, max: 10000, step: 50, stepfunc: stepZoom, callback: changeZoom }); + window.onbeforeunload = function() { + // Suppress warning if page is empty + if(svgCanvas.getHistoryPosition() === 0) { + show_save_warning = false; + } + + // show_save_warning is set to "false" when the page is saved. + if(!curConfig.no_save_warning && show_save_warning) { + // Browser already asks question about closing the page + return "There are unsaved changes."; + } + }; + // use HTML5 File API: http://www.w3.org/TR/FileAPI/ // if browser has HTML5 File API support, then we will show the open menu item // and provide a file input to click. When that change event fires, it will diff --git a/editor/svgcanvas.js b/editor/svgcanvas.js index e3fbf711..c9ed90f9 100644 --- a/editor/svgcanvas.js +++ b/editor/svgcanvas.js @@ -1320,6 +1320,10 @@ function BatchCommand(text) { undoStack.push(cmd); undoStackPointer = undoStack.length; }; + + this.getHistoryPosition = function() { + return undoStackPointer; + }; // private functions var getId = function() {