function Screen(modules) { var current = document.querySelector("body > div.on"); return { dialog: dialog, select: select }; function select(name) { current.className = ""; current = document.getElementById(name); current.className = "on"; } function closeAndRun(dialog, action) { return function() { dialog.className = ''; action(); }; } function dialog(config) { var layer = document.getElementById('dialog'); var dialog = modules.dom.make('div', {}); dialog.appendChild(modules.dom.make('p', {textContent: config.text})); var answers = modules.dom.make('p', {class: 'answers'}); for(var key in config.answers) { answers.appendChild(modules.dom.make('button', { textContent: key, onClick: closeAndRun(layer, config.answers[key]) })); } dialog.appendChild(answers); layer.appendChild(dialog); layer.className = "on"; } }