added a cancel option to popups (closes #28)

main
Codinget 5 years ago
parent 51dacea167
commit ecb9a37e60
  1. 9
      src/js/main.js
  2. 14
      src/js/popup.js

@ -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%';

@ -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;

Loading…
Cancel
Save