toggle popup menu

This commit is contained in:
Bruno Windels 2020-11-18 20:18:09 +01:00
parent 59a92bdf97
commit 18407e17a8
2 changed files with 38 additions and 26 deletions

View File

@ -60,7 +60,12 @@ export class Popup {
}, 10); }, 10);
} }
get isOpen() {
return !!this._view;
}
close() { close() {
if (this._view) {
this._view.unmount(); this._view.unmount();
this._trackingTemplateView.removeSubView(this); this._trackingTemplateView.removeSubView(this);
if (this._scroller) { if (this._scroller) {
@ -68,6 +73,8 @@ export class Popup {
} }
document.body.removeEventListener("click", this, false); document.body.removeEventListener("click", this, false);
this._popup.remove(); this._popup.remove();
this._view = null;
}
} }
get _popup() { get _popup() {

View File

@ -22,6 +22,7 @@ export class MessageComposer extends TemplateView {
constructor(viewModel) { constructor(viewModel) {
super(viewModel); super(viewModel);
this._input = null; this._input = null;
this._attachmentPopup = null;
} }
render(t, vm) { render(t, vm) {
@ -35,7 +36,7 @@ export class MessageComposer extends TemplateView {
t.button({ t.button({
className: "sendFile", className: "sendFile",
title: vm.i18n`Pick attachment`, title: vm.i18n`Pick attachment`,
onClick: evt => this._showAttachmentMenu(evt), onClick: evt => this._toggleAttachmentMenu(evt),
}, vm.i18n`Send file`), }, vm.i18n`Send file`),
t.button({ t.button({
className: "send", className: "send",
@ -59,14 +60,17 @@ export class MessageComposer extends TemplateView {
} }
} }
_showAttachmentMenu(evt) { _toggleAttachmentMenu(evt) {
if (this._attachmentPopup && this._attachmentPopup.isOpen) {
this._attachmentPopup.close();
} else {
const vm = this.value; const vm = this.value;
const popup = new Popup(new Menu([ this._attachmentPopup = new Popup(new Menu([
Menu.option(vm.i18n`Send picture`, () => vm.sendPicture()).setIcon("picture"), Menu.option(vm.i18n`Send picture`, () => vm.sendPicture()).setIcon("picture"),
Menu.option(vm.i18n`Send file`, () => vm.sendFile()).setIcon("file"), Menu.option(vm.i18n`Send file`, () => vm.sendFile()).setIcon("file"),
])); ]));
popup.trackInTemplateView(this); this._attachmentPopup.trackInTemplateView(this);
popup.showRelativeTo(evt.target, { this._attachmentPopup.showRelativeTo(evt.target, {
horizontal: { horizontal: {
relativeTo: "end", relativeTo: "end",
align: "start", align: "start",
@ -79,4 +83,5 @@ export class MessageComposer extends TemplateView {
} }
}); });
} }
}
} }