Added the ability to cancel bakes

This commit is contained in:
n1474335 2017-07-28 16:38:53 +01:00
parent 98884d851a
commit f1ebab0c2d
4 changed files with 57 additions and 11 deletions

View File

@ -27,7 +27,6 @@ const App = function(categories, operations, defaultFavourites, defaultOptions)
this.doptions = defaultOptions; this.doptions = defaultOptions;
this.options = Utils.extend({}, defaultOptions); this.options = Utils.extend({}, defaultOptions);
this.chefWorker = new ChefWorker();
this.manager = new Manager(this); this.manager = new Manager(this);
this.baking = false; this.baking = false;
@ -35,8 +34,6 @@ const App = function(categories, operations, defaultFavourites, defaultOptions)
this.autoBakePause = false; this.autoBakePause = false;
this.progress = 0; this.progress = 0;
this.ingId = 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() { App.prototype.setup = function() {
document.dispatchEvent(this.manager.appstart); document.dispatchEvent(this.manager.appstart);
this.registerChefWorker();
this.initialiseSplitter(); this.initialiseSplitter();
this.loadLocalStorage(); this.loadLocalStorage();
this.populateOperationsList(); 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. * Fires once all setup activities have completed.
*/ */
App.prototype.loaded = function() { App.prototype.loaded = function() {
// Check that both the app and the worker have loaded successfully before // Check that both the app and the worker have loaded successfully, and that
// removing the loading screen. // we haven't already loaded before attempting to remove the loading screen.
if (!this.worderLoaded || !this.appLoaded) return; if (!this.workerLoaded || !this.appLoaded ||
!document.getElementById("loader-wrapper")) return;
// Trigger CSS animations to remove preloader // Trigger CSS animations to remove preloader
document.body.classList.add("loaded"); document.body.classList.add("loaded");
@ -112,12 +120,14 @@ App.prototype.setBakingStatus = function(bakingStatus) {
outputElement.disabled = true; outputElement.disabled = true;
outputLoader.style.visibility = "visible"; outputLoader.style.visibility = "visible";
outputLoader.style.opacity = 1; outputLoader.style.opacity = 1;
}, 200); this.manager.controls.toggleBakeButtonFunction(true);
}.bind(this), 200);
} else { } else {
clearTimeout(this.bakingStatusTimeout); clearTimeout(this.bakingStatusTimeout);
outputElement.disabled = false; outputElement.disabled = false;
outputLoader.style.opacity = 0; outputLoader.style.opacity = 0;
outputLoader.style.visibility = "hidden"; 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. * Handler for messages sent back by the ChefWorker.
* *
@ -163,7 +184,7 @@ App.prototype.handleChefMessage = function(e) {
case "silentBakeComplete": case "silentBakeComplete":
break; break;
case "workerLoaded": case "workerLoaded":
this.worderLoaded = true; this.workerLoaded = true;
this.loaded(); this.loaded();
break; break;
default: default:

View File

@ -78,7 +78,11 @@ ControlsWaiter.prototype.setAutoBake = function(value) {
* Handler to trigger baking. * Handler to trigger baking.
*/ */
ControlsWaiter.prototype.bakeClick = function() { 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"; 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; export default ControlsWaiter;

View File

@ -122,7 +122,7 @@
<div id="bake-group"> <div id="bake-group">
<button type="button" class="btn btn-success btn-lg" id="bake"> <button type="button" class="btn btn-success btn-lg" id="bake">
<img aria-hidden="true" src="<%- require('../static/images/cook_male-32x32.png') %>" alt="Chef Icon"/> <img aria-hidden="true" src="<%- require('../static/images/cook_male-32x32.png') %>" alt="Chef Icon"/>
Bake! <span>Bake!</span>
</button> </button>
<label class="btn btn-success btn-lg" id="auto-bake-label" for="auto-bake"> <label class="btn btn-success btn-lg" id="auto-bake-label" for="auto-bake">
<input type="checkbox" checked="checked" id="auto-bake"> <input type="checkbox" checked="checked" id="auto-bake">

View File

@ -46,7 +46,7 @@
width: 60px; width: 60px;
border-top-left-radius: 0; border-top-left-radius: 0;
border-bottom-left-radius: 0; border-bottom-left-radius: 0;
border-left: 1px solid var(--btn-success-bg-colour); border-left: 1px solid transparent;
} }
#auto-bake-label:hover { #auto-bake-label:hover {