From cbdf8a597efc9659e50c299baf085ddd8f6dc2d8 Mon Sep 17 00:00:00 2001 From: Tina Quach Date: Wed, 31 Aug 2016 13:39:47 -0700 Subject: [PATCH] Blockly Factory: Warn user when leaving/refreshing page (#607) * warn user upon refreshing page and leaving page (by going to prev page or by exiting the window or tab) * alert upon leaving page ONLY when unsaved changes * nit remove log * no alert when on starter block, added clarifying comment --- demos/blocklyfactory/app_controller.js | 15 +++++++++++++++ demos/blocklyfactory/index.html | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/demos/blocklyfactory/app_controller.js b/demos/blocklyfactory/app_controller.js index b2be5b05a..c8d5acd47 100644 --- a/demos/blocklyfactory/app_controller.js +++ b/demos/blocklyfactory/app_controller.js @@ -619,6 +619,21 @@ AppController.prototype.onresize = function(event) { } }; +/** + * Handler for the window's 'onbeforeunload' event. When a user has unsaved + * changes and refreshes or leaves the page, confirm that they want to do so + * before actually refreshing. + */ +AppController.prototype.confirmLeavePage = function() { + if (!BlockFactory.isStarterBlock() && + !FactoryUtils.savedBlockChanges(this.blockLibraryController)) { + // When a string is assigned to the returnValue Event property, a dialog box + // appears, asking the users for confirmation to leave the page. + return 'You will lose any unsaved changes. Are you sure you want ' + + 'to exit this page?'; + } +}; + /** * Initialize Blockly and layout. Called on page load. */ diff --git a/demos/blocklyfactory/index.html b/demos/blocklyfactory/index.html index 63996cb02..69d18a09b 100644 --- a/demos/blocklyfactory/index.html +++ b/demos/blocklyfactory/index.html @@ -41,7 +41,7 @@ window.addEventListener('load', init); - +

Blockly > Demos > Blockly Factory