tweb/src/components/dialogsContextMenu.ts

164 lines
5.1 KiB
TypeScript
Raw Normal View History

import appDialogsManager from "../lib/appManagers/appDialogsManager";
2020-11-23 18:25:14 +01:00
import appMessagesManager, {Dialog} from "../lib/appManagers/appMessagesManager";
import appPeersManager from "../lib/appManagers/appPeersManager";
2020-11-15 04:33:47 +01:00
import rootScope from "../lib/rootScope";
import { findUpTag } from "../helpers/dom";
2020-11-23 18:25:14 +01:00
import { positionMenu, openBtnMenu } from "./misc";
import ButtonMenu, { ButtonMenuItemOptions } from "./buttonMenu";
import PopupDeleteDialog from "./popups/deleteDialog";
import { i18n } from "../lib/langPack";
export default class DialogsContextMenu {
2020-11-23 18:25:14 +01:00
private element: HTMLElement;
private buttons: (ButtonMenuItemOptions & {verify: () => boolean})[];
private selectedId: number;
private filterId: number;
2020-11-23 18:25:14 +01:00
private dialog: Dialog;
private init() {
this.buttons = [{
icon: 'unread',
2021-03-25 19:07:00 +01:00
text: 'MarkAsUnread',
2020-11-23 18:25:14 +01:00
onClick: this.onUnreadClick,
verify: () => {
const isUnread = !!(this.dialog.pFlags?.unread_mark || this.dialog.unread_count);
return !isUnread;
}
}, {
icon: 'readchats',
2021-03-25 19:07:00 +01:00
text: 'MarkAsRead',
2020-11-23 18:25:14 +01:00
onClick: this.onUnreadClick,
verify: () => {
const isUnread = !!(this.dialog.pFlags?.unread_mark || this.dialog.unread_count);
return isUnread;
}
}, {
icon: 'pin',
2021-03-25 19:07:00 +01:00
text: 'ChatList.Context.Pin',
2020-11-23 18:25:14 +01:00
onClick: this.onPinClick,
verify: () => {
const isPinned = this.filterId > 1 ? appMessagesManager.filtersStorage.filters[this.filterId].pinned_peers.includes(this.dialog.peerId) : !!this.dialog.pFlags?.pinned;
2020-11-23 18:25:14 +01:00
return !isPinned;
}
}, {
icon: 'unpin',
2021-03-25 19:07:00 +01:00
text: 'ChatList.Context.Unpin',
2020-11-23 18:25:14 +01:00
onClick: this.onPinClick,
verify: () => {
const isPinned = this.filterId > 1 ? appMessagesManager.filtersStorage.filters[this.filterId].pinned_peers.includes(this.dialog.peerId) : !!this.dialog.pFlags?.pinned;
2020-11-23 18:25:14 +01:00
return isPinned;
}
}, {
icon: 'mute',
2021-03-25 19:07:00 +01:00
text: 'ChatList.Context.Mute',
2020-11-23 18:25:14 +01:00
onClick: this.onMuteClick,
verify: () => {
const isMuted = this.dialog.notify_settings && this.dialog.notify_settings.mute_until > (Date.now() / 1000 | 0);
2021-02-04 01:30:23 +01:00
return !isMuted && this.selectedId !== rootScope.myId;
2020-11-23 18:25:14 +01:00
}
}, {
icon: 'unmute',
2021-03-25 19:07:00 +01:00
text: 'ChatList.Context.Unmute',
2020-11-23 18:25:14 +01:00
onClick: this.onMuteClick,
verify: () => {
const isMuted = this.dialog.notify_settings && this.dialog.notify_settings.mute_until > (Date.now() / 1000 | 0);
2021-02-04 01:30:23 +01:00
return isMuted && this.selectedId !== rootScope.myId;
2020-11-23 18:25:14 +01:00
}
}, {
icon: 'archive',
text: 'Archive',
onClick: this.onArchiveClick,
2021-02-04 01:30:23 +01:00
verify: () => this.filterId === 0 && this.selectedId !== rootScope.myId
2020-11-23 18:25:14 +01:00
}, {
icon: 'unarchive',
text: 'Unarchive',
onClick: this.onArchiveClick,
2021-02-04 01:30:23 +01:00
verify: () => this.filterId === 1 && this.selectedId !== rootScope.myId
2020-11-23 18:25:14 +01:00
}, {
icon: 'delete danger',
text: 'Delete',
onClick: this.onDeleteClick,
verify: () => true
}];
this.element = ButtonMenu(this.buttons);
this.element.id = 'dialogs-contextmenu';
2021-03-01 20:49:36 +01:00
this.element.classList.add('contextmenu');
2020-11-23 18:25:14 +01:00
document.getElementById('page-chats').append(this.element);
}
2020-11-23 18:25:14 +01:00
private onArchiveClick = () => {
let dialog = appMessagesManager.getDialogByPeerId(this.selectedId)[0];
2020-11-23 18:25:14 +01:00
if(dialog) {
appMessagesManager.editPeerFolders([dialog.peerId], +!dialog.folder_id);
2020-11-23 18:25:14 +01:00
}
};
2020-11-23 18:25:14 +01:00
private onPinClick = () => {
appMessagesManager.toggleDialogPin(this.selectedId, this.filterId);
2020-11-23 18:25:14 +01:00
};
private onMuteClick = () => {
appMessagesManager.mutePeer(this.selectedId);
2020-11-23 18:25:14 +01:00
};
2020-11-23 18:25:14 +01:00
private onUnreadClick = () => {
const dialog = appMessagesManager.getDialogByPeerId(this.selectedId)[0];
2020-11-23 18:25:14 +01:00
if(!dialog) return;
2020-11-23 18:25:14 +01:00
if(dialog.unread_count) {
appMessagesManager.readHistory(this.selectedId, dialog.top_message);
appMessagesManager.markDialogUnread(this.selectedId, true);
2020-11-23 18:25:14 +01:00
} else {
appMessagesManager.markDialogUnread(this.selectedId);
2020-11-23 18:25:14 +01:00
}
};
2020-11-23 18:25:14 +01:00
private onDeleteClick = () => {
new PopupDeleteDialog(this.selectedId);
2020-11-23 18:25:14 +01:00
};
onContextMenu = (e: MouseEvent | Touch) => {
2020-11-23 18:25:14 +01:00
if(this.init) {
this.init();
this.init = null;
}
let li: HTMLElement = null;
try {
li = findUpTag(e.target, 'LI');
} catch(e) {}
if(!li) return;
if(e instanceof MouseEvent) e.preventDefault();
if(this.element.classList.contains('active')) {
return false;
}
if(e instanceof MouseEvent) e.cancelBubble = true;
this.filterId = appDialogsManager.filterId;
this.selectedId = +li.dataset.peerId;
this.dialog = appMessagesManager.getDialogByPeerId(this.selectedId)[0];
2020-11-23 18:25:14 +01:00
this.buttons.forEach(button => {
const good = button.verify();
2020-11-23 18:25:14 +01:00
button.element.classList.toggle('hide', !good);
});
// delete button
this.buttons[this.buttons.length - 1].element.lastChild.replaceWith(i18n(appPeersManager.getDeleteButtonText(this.selectedId)));
li.classList.add('menu-open');
positionMenu(e, this.element);
openBtnMenu(this.element, () => {
li.classList.remove('menu-open');
this.selectedId = this.dialog = this.filterId = undefined;
});
};
}