Extend messageBox to set onclick handler on buttons

This commit is contained in:
eight 2017-09-01 14:46:00 +08:00
parent 8607d779f9
commit dfb7ac9b44

View File

@ -4,7 +4,7 @@ function messageBox({
title, // [mandatory] string title, // [mandatory] string
contents, // [mandatory] 1) DOM element 2) string contents, // [mandatory] 1) DOM element 2) string
className = '', // string, CSS class name of the message box element className = '', // string, CSS class name of the message box element
buttons = [], // array of strings used as labels buttons = [], // array of strings or objects like {textContent[string], onclick[function]}.
onshow, // function(messageboxElement) invoked after the messagebox is shown onshow, // function(messageboxElement) invoked after the messagebox is shown
blockScroll, // boolean, blocks the page scroll blockScroll, // boolean, blocks the page scroll
}) { // RETURNS: Promise resolved to {button[number], enter[boolean], esc[boolean]} }) { // RETURNS: Promise resolved to {button[number], enter[boolean], esc[boolean]}
@ -67,14 +67,21 @@ function messageBox({
onclick: messageBox.listeners.closeIcon}), onclick: messageBox.listeners.closeIcon}),
$element({id: `${id}-contents`, appendChild: tHTML(contents)}), $element({id: `${id}-contents`, appendChild: tHTML(contents)}),
$element({id: `${id}-buttons`, appendChild: $element({id: `${id}-buttons`, appendChild:
buttons.map((textContent, buttonIndex) => textContent && buttons.map((textContent, buttonIndex) => {
$element({ if (!textContent) {
return;
}
let onclick = messageBox.listeners.button;
if (typeof textContent === 'object') {
({onclick = onclick, textContent} = textContent);
}
return $element({
tag: 'button', tag: 'button',
buttonIndex, buttonIndex,
textContent, textContent,
onclick: messageBox.listeners.button, onclick,
}) });
) })
}), }),
]}), ]}),
]}); ]});