From f1ebab0c2d43e5dc9d97559f05152b012eef1fdf Mon Sep 17 00:00:00 2001 From: n1474335 Date: Fri, 28 Jul 2017 16:38:53 +0100 Subject: [PATCH] Added the ability to cancel bakes --- src/web/App.js | 37 +++++++++++++++++++----- src/web/ControlsWaiter.js | 27 ++++++++++++++++- src/web/html/index.html | 2 +- src/web/stylesheets/layout/_controls.css | 2 +- 4 files changed, 57 insertions(+), 11 deletions(-) diff --git a/src/web/App.js b/src/web/App.js index 8945ea89..2cba0997 100755 --- a/src/web/App.js +++ b/src/web/App.js @@ -27,7 +27,6 @@ const App = function(categories, operations, defaultFavourites, defaultOptions) this.doptions = defaultOptions; this.options = Utils.extend({}, defaultOptions); - this.chefWorker = new ChefWorker(); this.manager = new Manager(this); this.baking = false; @@ -35,8 +34,6 @@ const App = function(categories, operations, defaultFavourites, defaultOptions) this.autoBakePause = false; this.progress = 0; this.ingId = 0; - - this.chefWorker.addEventListener("message", this.handleChefMessage.bind(this)); }; @@ -47,6 +44,7 @@ const App = function(categories, operations, defaultFavourites, defaultOptions) */ App.prototype.setup = function() { document.dispatchEvent(this.manager.appstart); + this.registerChefWorker(); this.initialiseSplitter(); this.loadLocalStorage(); this.populateOperationsList(); @@ -60,13 +58,23 @@ App.prototype.setup = function() { }; +/** + * Sets up the ChefWorker and associated listeners. + */ +App.prototype.registerChefWorker = function() { + this.chefWorker = new ChefWorker(); + this.chefWorker.addEventListener("message", this.handleChefMessage.bind(this)); +}; + + /** * Fires once all setup activities have completed. */ App.prototype.loaded = function() { - // Check that both the app and the worker have loaded successfully before - // removing the loading screen. - if (!this.worderLoaded || !this.appLoaded) return; + // Check that both the app and the worker have loaded successfully, and that + // we haven't already loaded before attempting to remove the loading screen. + if (!this.workerLoaded || !this.appLoaded || + !document.getElementById("loader-wrapper")) return; // Trigger CSS animations to remove preloader document.body.classList.add("loaded"); @@ -112,12 +120,14 @@ App.prototype.setBakingStatus = function(bakingStatus) { outputElement.disabled = true; outputLoader.style.visibility = "visible"; outputLoader.style.opacity = 1; - }, 200); + this.manager.controls.toggleBakeButtonFunction(true); + }.bind(this), 200); } else { clearTimeout(this.bakingStatusTimeout); outputElement.disabled = false; outputLoader.style.opacity = 0; outputLoader.style.visibility = "hidden"; + this.manager.controls.toggleBakeButtonFunction(false); } }; @@ -146,6 +156,17 @@ App.prototype.bake = function(step) { }; +/** + * Cancels the current bake by terminating the ChefWorker and creating a new one. + */ +App.prototype.cancelBake = function() { + this.chefWorker.terminate(); + this.registerChefWorker(); + this.setBakingStatus(false); + this.manager.controls.showStaleIndicator(); +}; + + /** * Handler for messages sent back by the ChefWorker. * @@ -163,7 +184,7 @@ App.prototype.handleChefMessage = function(e) { case "silentBakeComplete": break; case "workerLoaded": - this.worderLoaded = true; + this.workerLoaded = true; this.loaded(); break; default: diff --git a/src/web/ControlsWaiter.js b/src/web/ControlsWaiter.js index 2c8cf9c6..3ae86bcd 100755 --- a/src/web/ControlsWaiter.js +++ b/src/web/ControlsWaiter.js @@ -78,7 +78,11 @@ ControlsWaiter.prototype.setAutoBake = function(value) { * Handler to trigger baking. */ ControlsWaiter.prototype.bakeClick = function() { - this.app.bake(); + if (document.getElementById("bake").textContent.indexOf("Bake") > 0) { + this.app.bake(); + } else { + this.app.cancelBake(); + } }; @@ -386,4 +390,25 @@ ControlsWaiter.prototype.hideStaleIndicator = function() { staleIndicator.style.visibility = "hidden"; }; + +/** + * Switches the Bake button between 'Bake' and 'Cancel' functions. + * + * @param {boolean} cancel - Whether to change to cancel or not + */ +ControlsWaiter.prototype.toggleBakeButtonFunction = function(cancel) { + const bakeButton = document.getElementById("bake"), + btnText = bakeButton.querySelector("span"); + + if (cancel) { + btnText.innerText = "Cancel"; + bakeButton.classList.remove("btn-success"); + bakeButton.classList.add("btn-danger"); + } else { + btnText.innerText = "Bake!"; + bakeButton.classList.remove("btn-danger"); + bakeButton.classList.add("btn-success"); + } +}; + export default ControlsWaiter; diff --git a/src/web/html/index.html b/src/web/html/index.html index ac10ec14..e42386f2 100755 --- a/src/web/html/index.html +++ b/src/web/html/index.html @@ -122,7 +122,7 @@