I have a sidebar with a simple form with a dropdown that selects some categories of the entries. There's a button beside it for the user to add a new category. This button shows a modal dialog and in this dialog there's an input box for the user to input the new category name and an "Add New" button that will send the new item back to the sidebar. I would like for the respective dropdown to have the new category included among its options plus its text to be already populated with the new name.
All I could think so far was to use the localStorage
but I then I don't know how to trigger updates to the dropdown on the sidebar (or any other element) when the button is pressed and the dialog is closed with a new value submitted. I've been looking at this and this but I couldn't really get it to work to my case.
I have supressed the Code.gs
pieces because they work just fine and are not relevant.
sidebar.html
<script>
document.getElementById("btn-add-category").addEventListener("click", goToAddNewItem);
function goToAddNewItem(){
google.script.run.loadNewItemForm();
}
(async () => {
//After modal dialog has finished, receiver will be resolved
let receiver = new Promise((res, rej) => {
window.modalDone = res;
});
var message = await receiver;
document.getElementById("category-sidebar").value = message
})();
</script>
modal.html
This is the html
file of the modal dialog:
<body>
<div class="form-group" id="input-category">
<label for="category">Category:</label>
<div class="input-group input-line">
<input type="text" class="form-control form-control-sm" id="category">
</div>
</div>
<div class="form-group">
<button id="btn-add-new" class="btn btn-dark btn-sm">Add New</button>
</div>
<script>
function findSideBar(limit) {
let f = window.top.frames;
for (let i = 0; i < limit; ++i) {
try {
if (
f[i] /*/iframedAppPanel*/ &&
f[i].length &&
f[i][0] && //#sandboxFrame
f[i][0][0] && //#userHtmlFrame
window !== f[i][0][0] //!== self
) {
console.info('Sidebar found ');
var sidebar = f[i][0][0];
sidebar.modalDone(document.getElementById("category").value);
}
} catch (e) {
console.error(e);
continue;
}
}
};
function afterAddNewClicked(){
findSideBar(10);
google.script.host.close();
}
document.getElementById("btn-add-new").addEventListener("click", afterAddNewClicked);
</script>
</body>
EDIT:
modal.html
now has the function findSidebar