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

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

Loading…
Cancel
Save