added a cancel option to popups (closes #28)

This commit is contained in:
Nathan DECHER 2020-04-08 15:12:50 +02:00
parent edd9d5c15a
commit 5bf5f49f03
2 changed files with 19 additions and 4 deletions

View File

@ -108,15 +108,17 @@
// start a new game
startGame=async (category, levelId, filename) => {
// stop any running games
// stop any running games and clear popups
stopGame();
Popup.dismiss();
// load rules and level from cache or server
const rules=levelList[category].rules || {};
const level=await levels.get(filename);
// stop any running games again
// stop any running games and clear popups again
stopGame();
Popup.dismiss();
// create the game and attach the callbacks and config
const snek=currentGame=new SnekGame(level, canvas, rules);
@ -153,11 +155,13 @@
// return to the menu
menu=() => {
stopGame();
Popup.dismiss();
};
// show config editor
settings=async () => {
stopGame();
Popup.dismiss();
await configEditor.show();
location.hash='menu';
};
@ -165,6 +169,7 @@
// show help page
help=async () => {
stopGame();
Popup.dismiss();
let iframe=document.createElement('iframe');
iframe.src='help.html';
iframe.style.width='100%';

View File

@ -77,15 +77,20 @@ class Popup {
buttons.forEach(btn => buttonSection.appendChild(btn));
parent.appendChild(outer);
Popup.displayed.push(this);
const code=await Promise.race(buttons.map(btn => new Promise(ok => {
const btnActions=buttons.map(btn => new Promise(ok => {
btn.addEventListener('click', e => {
e.preventDefault();
return ok(btn.dataset.code);
});
})));
}));
const dismissAction=new Promise(ok => this.dismiss=ok);
const code=await Promise.race(btnActions.concat([dismissAction]));
parent.removeChild(outer);
Popup.displayed.splice(Popup.displayed.indexOf(this), 1);
return code;
}
}
@ -93,4 +98,9 @@ class Popup {
Popup.EM=Symbol('EM');
Popup.STRONG=Symbol('STRONG');
Popup.displayed=[];
Popup.dismiss=arg => {
Popup.displayed.forEach(p => p.dismiss(arg));
};
return module.exports=Popup;