2021-05-28 12:31:15 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {renderStaticAvatar} from "../../../avatar.js";
|
2021-09-16 10:45:06 +02:00
|
|
|
import {tag} from "../../../general/html";
|
2021-09-06 17:12:14 +02:00
|
|
|
import {mountView} from "../../../general/utils";
|
2021-05-28 12:31:15 +02:00
|
|
|
import {TemplateView} from "../../../general/TemplateView.js";
|
2021-05-28 13:14:55 +02:00
|
|
|
import {Popup} from "../../../general/Popup.js";
|
|
|
|
import {Menu} from "../../../general/Menu.js";
|
2021-06-03 19:16:19 +02:00
|
|
|
import {ReactionsView} from "./ReactionsView.js";
|
2021-05-28 12:31:15 +02:00
|
|
|
|
|
|
|
export class BaseMessageView extends TemplateView {
|
2021-08-03 13:27:33 -07:00
|
|
|
constructor(value, interactive = true, tagName = "li") {
|
2021-05-28 13:14:55 +02:00
|
|
|
super(value);
|
|
|
|
this._menuPopup = null;
|
2021-07-22 12:51:24 -07:00
|
|
|
this._tagName = tagName;
|
2021-07-27 16:51:34 -07:00
|
|
|
// TODO An enum could be nice to make code easier to read at call sites.
|
2021-08-03 13:27:33 -07:00
|
|
|
this._interactive = interactive;
|
2021-05-28 13:14:55 +02:00
|
|
|
}
|
|
|
|
|
2021-05-28 12:31:15 +02:00
|
|
|
render(t, vm) {
|
2021-07-22 12:51:24 -07:00
|
|
|
const li = t.el(this._tagName, {className: {
|
2021-05-28 12:31:15 +02:00
|
|
|
"Timeline_message": true,
|
|
|
|
own: vm.isOwn,
|
|
|
|
unsent: vm.isUnsent,
|
|
|
|
unverified: vm.isUnverified,
|
2021-08-03 13:27:33 -07:00
|
|
|
disabled: !this._interactive,
|
2021-05-28 12:31:15 +02:00
|
|
|
continuation: vm => vm.isContinuation,
|
2021-05-31 11:57:17 +02:00
|
|
|
}}, [
|
2021-06-04 10:08:07 +02:00
|
|
|
// dynamically added and removed nodes are handled below
|
2021-05-28 12:31:15 +02:00
|
|
|
this.renderMessageBody(t, vm),
|
|
|
|
// should be after body as it is overlayed on top
|
2021-08-03 13:27:33 -07:00
|
|
|
this._interactive ? t.button({className: "Timeline_messageOptions"}, "⋯") : [],
|
2021-05-31 11:57:17 +02:00
|
|
|
]);
|
2021-08-06 21:07:30 +05:30
|
|
|
const avatar = t.a({href: vm.memberPanelLink, className: "Timeline_messageAvatar"}, [renderStaticAvatar(vm, 30)]);
|
2021-05-31 11:57:17 +02:00
|
|
|
// given that there can be many tiles, we don't add
|
|
|
|
// unneeded DOM nodes in case of a continuation, and we add it
|
|
|
|
// with a side-effect binding to not have to create sub views,
|
|
|
|
// as the avatar or sender doesn't need any bindings or event handlers.
|
|
|
|
// don't use `t` from within the side-effect callback
|
|
|
|
t.mapSideEffect(vm => vm.isContinuation, (isContinuation, wasContinuation) => {
|
|
|
|
if (isContinuation && wasContinuation === false) {
|
|
|
|
li.removeChild(li.querySelector(".Timeline_messageAvatar"));
|
|
|
|
li.removeChild(li.querySelector(".Timeline_messageSender"));
|
|
|
|
} else if (!isContinuation) {
|
2021-08-06 20:51:39 +05:30
|
|
|
li.insertBefore(avatar, li.firstChild);
|
2021-05-31 13:03:41 +02:00
|
|
|
li.insertBefore(tag.div({className: `Timeline_messageSender usercolor${vm.avatarColorNumber}`}, vm.displayName), li.firstChild);
|
2021-05-31 11:57:17 +02:00
|
|
|
}
|
|
|
|
});
|
2021-06-04 10:08:07 +02:00
|
|
|
// similarly, we could do this with a simple ifView,
|
|
|
|
// but that adds a comment node to all messages without reactions
|
|
|
|
let reactionsView = null;
|
|
|
|
t.mapSideEffect(vm => vm.reactions, reactions => {
|
2021-08-03 13:27:33 -07:00
|
|
|
if (reactions && this._interactive && !reactionsView) {
|
2021-06-04 10:08:07 +02:00
|
|
|
reactionsView = new ReactionsView(vm.reactions);
|
|
|
|
this.addSubView(reactionsView);
|
|
|
|
li.appendChild(mountView(reactionsView));
|
|
|
|
} else if (!reactions && reactionsView) {
|
|
|
|
li.removeChild(reactionsView.root());
|
|
|
|
reactionsView.unmount();
|
|
|
|
this.removeSubView(reactionsView);
|
|
|
|
reactionsView = null;
|
|
|
|
}
|
|
|
|
});
|
2021-05-31 11:57:17 +02:00
|
|
|
return li;
|
2021-05-28 12:31:15 +02:00
|
|
|
}
|
|
|
|
|
2021-05-28 13:14:55 +02:00
|
|
|
/* This is called by the parent ListView, which just has 1 listener for the whole list */
|
|
|
|
onClick(evt) {
|
|
|
|
if (evt.target.className === "Timeline_messageOptions") {
|
|
|
|
this._toggleMenu(evt.target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_toggleMenu(button) {
|
|
|
|
if (this._menuPopup && this._menuPopup.isOpen) {
|
|
|
|
this._menuPopup.close();
|
|
|
|
} else {
|
|
|
|
const options = this.createMenuOptions(this.value);
|
|
|
|
if (!options.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.root().classList.add("menuOpen");
|
2021-05-28 15:27:02 +02:00
|
|
|
const onClose = () => this.root().classList.remove("menuOpen");
|
|
|
|
this._menuPopup = new Popup(new Menu(options), onClose);
|
2021-05-28 13:14:55 +02:00
|
|
|
this._menuPopup.trackInTemplateView(this);
|
|
|
|
this._menuPopup.showRelativeTo(button, {
|
|
|
|
horizontal: {
|
|
|
|
relativeTo: "end",
|
|
|
|
align: "start",
|
|
|
|
after: 0
|
|
|
|
},
|
|
|
|
vertical: {
|
|
|
|
relativeTo: "start",
|
|
|
|
align: "end",
|
|
|
|
before: -24
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2021-05-28 12:31:15 +02:00
|
|
|
|
2021-05-28 13:14:55 +02:00
|
|
|
createMenuOptions(vm) {
|
|
|
|
const options = [];
|
2021-06-17 16:48:58 +02:00
|
|
|
if (vm.canReact && vm.shape !== "redacted") {
|
2021-06-17 16:46:06 +02:00
|
|
|
options.push(new QuickReactionsMenuOption(vm));
|
2021-07-22 12:51:24 -07:00
|
|
|
options.push(Menu.option(vm.i18n`Reply`, () => vm.startReply()));
|
2021-06-17 09:41:25 +02:00
|
|
|
}
|
2021-06-02 11:56:15 +02:00
|
|
|
if (vm.canAbortSending) {
|
2021-05-28 15:27:02 +02:00
|
|
|
options.push(Menu.option(vm.i18n`Cancel`, () => vm.abortSending()));
|
2021-05-31 13:55:08 +02:00
|
|
|
} else if (vm.canRedact) {
|
2021-05-28 16:25:23 +02:00
|
|
|
options.push(Menu.option(vm.i18n`Delete`, () => vm.redact()).setDestructive());
|
2021-05-28 13:14:55 +02:00
|
|
|
}
|
|
|
|
return options;
|
2021-05-28 12:31:15 +02:00
|
|
|
}
|
2021-05-28 13:14:55 +02:00
|
|
|
|
|
|
|
renderMessageBody() {}
|
2021-05-28 12:31:15 +02:00
|
|
|
}
|
2021-06-17 16:46:06 +02:00
|
|
|
|
|
|
|
class QuickReactionsMenuOption {
|
|
|
|
constructor(vm) {
|
|
|
|
this._vm = vm;
|
|
|
|
}
|
|
|
|
toDOM(t) {
|
|
|
|
const emojiButtons = ["👍", "👎", "😄", "🎉", "😕", "❤️", "🚀", "👀"].map(emoji => {
|
|
|
|
return t.button({onClick: () => this._vm.react(emoji)}, emoji);
|
|
|
|
});
|
|
|
|
const customButton = t.button({onClick: () => {
|
|
|
|
const key = prompt("Enter your reaction (emoji)");
|
|
|
|
if (key) {
|
|
|
|
this._vm.react(key);
|
|
|
|
}
|
|
|
|
}}, "…");
|
|
|
|
return t.li({className: "quick-reactions"}, [...emojiButtons, customButton]);
|
|
|
|
}
|
|
|
|
}
|