diff --git a/demos/blocklyfactory/app_controller.js b/demos/blocklyfactory/app_controller.js index c8d5acd47..47055782b 100644 --- a/demos/blocklyfactory/app_controller.js +++ b/demos/blocklyfactory/app_controller.js @@ -287,7 +287,7 @@ AppController.prototype.onTab = function() { if (this.lastSelectedTab == AppController.BLOCK_FACTORY && this.selectedTab != AppController.BLOCK_FACTORY) { - var hasUnsavedChanges = !BlockFactory.isStarterBlock() && + var hasUnsavedChanges = !FactoryUtils.savedBlockChanges(this.blockLibraryController); if (hasUnsavedChanges && !confirm('You have unsaved changes in Block Factory.')) { @@ -531,7 +531,8 @@ AppController.prototype.assignBlockFactoryClickHandlers = function() { .addEventListener('click', function() { // If there are unsaved changes warn user, check if they'd like to // proceed with unsaved changes, and act accordingly. - var proceedWithUnsavedChanges = FactoryUtils.warnIfUnsavedChanges(); + var proceedWithUnsavedChanges = + self.blockLibraryController.warnIfUnsavedChanges(); if (!proceedWithUnsavedChanges) { return; } @@ -625,8 +626,7 @@ AppController.prototype.onresize = function(event) { * before actually refreshing. */ AppController.prototype.confirmLeavePage = function() { - if (!BlockFactory.isStarterBlock() && - !FactoryUtils.savedBlockChanges(this.blockLibraryController)) { + if (!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 ' + @@ -649,7 +649,7 @@ AppController.prototype.init = function() { this.assignBlockFactoryClickHandlers(); this.onresize(); - self = this; + var self = this; window.addEventListener('resize', function() { self.onresize(); }); diff --git a/demos/blocklyfactory/block_library_controller.js b/demos/blocklyfactory/block_library_controller.js index e7548488d..1c0345e45 100644 --- a/demos/blocklyfactory/block_library_controller.js +++ b/demos/blocklyfactory/block_library_controller.js @@ -254,6 +254,21 @@ BlockLibraryController.prototype.setNoneSelected = function() { this.view.setSelectedBlockType(null); }; +/** + * If there are unsaved changes to the block in open in Block Factory + * and the block is not the starter block, check if user wants to proceed, + * knowing that it will cause them to lose their changes. + * + * @return {boolean} Whether or not to proceed. + */ +BlockLibraryController.prototype.warnIfUnsavedChanges = function() { + if (!FactoryUtils.savedBlockChanges(this)) { + return confirm('You have unsaved changes. By proceeding without saving ' + + ' your block first, you will lose these changes.'); + } + return true; +}; + /** * Add select handler for an option of a given block type. The handler will to * update the view and the selected block accordingly. @@ -283,7 +298,7 @@ BlockLibraryController.prototype.addOptionSelectHandler = function(blockType) { return function() { // If there are unsaved changes warn user, check if they'd like to // proceed with unsaved changes, and act accordingly. - var proceedWithUnsavedChanges = FactoryUtils.warnIfUnsavedChanges(); + var proceedWithUnsavedChanges = self.warnIfUnsavedChanges(); if (!proceedWithUnsavedChanges) { return; } diff --git a/demos/blocklyfactory/factory_utils.js b/demos/blocklyfactory/factory_utils.js index 416015362..08c003a95 100644 --- a/demos/blocklyfactory/factory_utils.js +++ b/demos/blocklyfactory/factory_utils.js @@ -963,7 +963,8 @@ FactoryUtils.isProcedureBlock = function(block) { }; /** - * Returns whether or not a block's changes has been saved to the Block Library. + * Returns whether or not a modified block's changes has been saved to the + * Block Library. * TODO(quachtina96): move into the Block Factory Controller once made. * * @param {!BlockLibraryController} blockLibraryController - Block Library @@ -972,6 +973,9 @@ FactoryUtils.isProcedureBlock = function(block) { * the given Block Library. */ FactoryUtils.savedBlockChanges = function(blockLibraryController) { + if (BlockFactory.isStarterBlock()) { + return true; + } var blockType = blockLibraryController.getCurrentBlockType(); var currentXml = Blockly.Xml.workspaceToDom(BlockFactory.mainWorkspace); @@ -983,18 +987,3 @@ FactoryUtils.savedBlockChanges = function(blockLibraryController) { return false; }; -/** - * If there are unsaved changes to the block in open in Block Factory - * and the block is not the starter block, check if user wants to proceed, - * knowing that it will cause them to lose their changes. - * - * @return {boolean} Whether or not to proceed. - */ -FactoryUtils.warnIfUnsavedChanges = function() { - if (!BlockFactory.isStarterBlock() && - !FactoryUtils.savedBlockChanges(self.blockLibraryController)) { - return confirm('You have unsaved changes. By proceeding without saving ' + - ' your block first, you will lose these changes.'); - } - return true; -};