2021-04-08 15:52:31 +02:00
|
|
|
/*
|
|
|
|
* https://github.com/morethanwords/tweb
|
|
|
|
* Copyright (C) 2019-2021 Eduard Kuzmenko
|
|
|
|
* https://github.com/morethanwords/tweb/blob/master/LICENSE
|
|
|
|
*/
|
|
|
|
|
2020-10-07 15:57:33 +02:00
|
|
|
import appDialogsManager from "../lib/appManagers/appDialogsManager";
|
2020-11-23 18:25:14 +01:00
|
|
|
import appMessagesManager, {Dialog} from "../lib/appManagers/appMessagesManager";
|
2020-10-07 15:57:33 +02:00
|
|
|
import appPeersManager from "../lib/appManagers/appPeersManager";
|
2020-11-15 04:33:47 +01:00
|
|
|
import rootScope from "../lib/rootScope";
|
2020-11-23 18:25:14 +01:00
|
|
|
import { positionMenu, openBtnMenu } from "./misc";
|
|
|
|
import ButtonMenu, { ButtonMenuItemOptions } from "./buttonMenu";
|
2020-12-18 05:30:49 +01:00
|
|
|
import PopupDeleteDialog from "./popups/deleteDialog";
|
2021-03-29 20:42:01 +02:00
|
|
|
import { i18n } from "../lib/langPack";
|
2021-04-04 17:39:17 +02:00
|
|
|
import findUpTag from "../helpers/dom/findUpTag";
|
2021-04-23 17:27:23 +02:00
|
|
|
import appNotificationsManager from "../lib/appManagers/appNotificationsManager";
|
2021-07-16 15:41:31 +02:00
|
|
|
import PopupPeer from "./popups/peer";
|
|
|
|
import AppChatFoldersTab from "./sidebarLeft/tabs/chatFolders";
|
|
|
|
import appSidebarLeft from "./sidebarLeft";
|
|
|
|
import { toastNew } from "./toast";
|
2020-10-07 15:57:33 +02:00
|
|
|
|
|
|
|
export default class DialogsContextMenu {
|
2020-11-23 18:25:14 +01:00
|
|
|
private element: HTMLElement;
|
|
|
|
private buttons: (ButtonMenuItemOptions & {verify: () => boolean})[];
|
|
|
|
|
2021-10-21 15:16:43 +02:00
|
|
|
private selectedId: PeerId;
|
2020-12-11 03:06:16 +01:00
|
|
|
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,
|
2021-10-21 15:16:43 +02:00
|
|
|
verify: () => !appMessagesManager.isDialogUnread(this.dialog)
|
2020-11-23 18:25:14 +01:00
|
|
|
}, {
|
|
|
|
icon: 'readchats',
|
2021-03-25 19:07:00 +01:00
|
|
|
text: 'MarkAsRead',
|
2020-11-23 18:25:14 +01:00
|
|
|
onClick: this.onUnreadClick,
|
2021-10-21 15:16:43 +02:00
|
|
|
verify: () => appMessagesManager.isDialogUnread(this.dialog)
|
2020-11-23 18:25:14 +01:00
|
|
|
}, {
|
|
|
|
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: () => {
|
2021-10-21 15:16:43 +02:00
|
|
|
const isPinned = this.filterId > 1 ? appMessagesManager.filtersStorage.getFilter(this.filterId).pinnedPeerIds.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: () => {
|
2021-10-21 15:16:43 +02:00
|
|
|
const isPinned = this.filterId > 1 ? appMessagesManager.filtersStorage.getFilter(this.filterId).pinnedPeerIds.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: () => {
|
2021-04-23 17:27:23 +02:00
|
|
|
return this.selectedId !== rootScope.myId && !appNotificationsManager.isPeerLocalMuted(this.dialog.peerId);
|
2020-11-23 18:25:14 +01:00
|
|
|
}
|
|
|
|
}, {
|
|
|
|
icon: 'unmute',
|
2021-03-25 19:07:00 +01:00
|
|
|
text: 'ChatList.Context.Unmute',
|
2021-04-23 18:08:20 +02:00
|
|
|
onClick: this.onUnmuteClick,
|
2020-11-23 18:25:14 +01:00
|
|
|
verify: () => {
|
2021-04-23 17:27:23 +02:00
|
|
|
return this.selectedId !== rootScope.myId && appNotificationsManager.isPeerLocalMuted(this.dialog.peerId);
|
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-10-07 15:57:33 +02:00
|
|
|
|
2020-11-23 18:25:14 +01:00
|
|
|
private onArchiveClick = () => {
|
2021-04-27 17:45:53 +02:00
|
|
|
let dialog = appMessagesManager.getDialogOnly(this.selectedId);
|
2020-11-23 18:25:14 +01:00
|
|
|
if(dialog) {
|
2020-12-11 03:06:16 +01:00
|
|
|
appMessagesManager.editPeerFolders([dialog.peerId], +!dialog.folder_id);
|
2020-11-23 18:25:14 +01:00
|
|
|
}
|
|
|
|
};
|
2020-10-07 15:57:33 +02:00
|
|
|
|
2020-11-23 18:25:14 +01:00
|
|
|
private onPinClick = () => {
|
2021-07-16 15:41:31 +02:00
|
|
|
appMessagesManager.toggleDialogPin(this.selectedId, this.filterId).catch(err => {
|
|
|
|
if(err.type === 'PINNED_DIALOGS_TOO_MUCH') {
|
|
|
|
if(this.filterId >= 1) {
|
|
|
|
toastNew({langPackKey: 'PinFolderLimitReached'});
|
|
|
|
} else {
|
|
|
|
new PopupPeer('pinned-dialogs-too-much', {
|
|
|
|
buttons: [{
|
|
|
|
langKey: 'OK',
|
|
|
|
isCancel: true
|
|
|
|
}, {
|
|
|
|
langKey: 'FiltersSetupPinAlert',
|
|
|
|
callback: () => {
|
|
|
|
new AppChatFoldersTab(appSidebarLeft).open();
|
|
|
|
}
|
|
|
|
}],
|
|
|
|
descriptionLangKey: 'PinToTopLimitReached2',
|
|
|
|
descriptionLangArgs: [i18n('Chats', [rootScope.config.pinned_dialogs_count_max])]
|
|
|
|
}).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-11-23 18:25:14 +01:00
|
|
|
};
|
2021-04-23 18:08:20 +02:00
|
|
|
|
|
|
|
private onUnmuteClick = () => {
|
|
|
|
appMessagesManager.mutePeer(this.selectedId, false);
|
|
|
|
};
|
2020-11-23 18:25:14 +01:00
|
|
|
|
|
|
|
private onMuteClick = () => {
|
2021-04-23 18:08:20 +02:00
|
|
|
appMessagesManager.mutePeer(this.selectedId, true);
|
2020-11-23 18:25:14 +01:00
|
|
|
};
|
2020-10-07 15:57:33 +02:00
|
|
|
|
2020-11-23 18:25:14 +01:00
|
|
|
private onUnreadClick = () => {
|
2021-04-27 17:45:53 +02:00
|
|
|
const dialog = appMessagesManager.getDialogOnly(this.selectedId);
|
2020-11-23 18:25:14 +01:00
|
|
|
if(!dialog) return;
|
2020-10-07 15:57:33 +02:00
|
|
|
|
2020-11-23 18:25:14 +01:00
|
|
|
if(dialog.unread_count) {
|
2020-12-11 03:06:16 +01:00
|
|
|
appMessagesManager.readHistory(this.selectedId, dialog.top_message);
|
|
|
|
appMessagesManager.markDialogUnread(this.selectedId, true);
|
2020-11-23 18:25:14 +01:00
|
|
|
} else {
|
2020-12-11 03:06:16 +01:00
|
|
|
appMessagesManager.markDialogUnread(this.selectedId);
|
2020-11-23 18:25:14 +01:00
|
|
|
}
|
|
|
|
};
|
2020-10-07 15:57:33 +02:00
|
|
|
|
2020-11-23 18:25:14 +01:00
|
|
|
private onDeleteClick = () => {
|
2021-09-19 13:09:31 +02:00
|
|
|
new PopupDeleteDialog(this.selectedId/* , 'delete' */);
|
2020-11-23 18:25:14 +01:00
|
|
|
};
|
2020-10-07 15:57:33 +02:00
|
|
|
|
|
|
|
onContextMenu = (e: MouseEvent | Touch) => {
|
2020-11-23 18:25:14 +01:00
|
|
|
if(this.init) {
|
|
|
|
this.init();
|
|
|
|
this.init = null;
|
|
|
|
}
|
|
|
|
|
2020-10-07 15:57:33 +02:00
|
|
|
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;
|
|
|
|
|
2020-12-11 03:06:16 +01:00
|
|
|
this.filterId = appDialogsManager.filterId;
|
2020-10-07 15:57:33 +02:00
|
|
|
|
2021-10-21 15:16:43 +02:00
|
|
|
this.selectedId = li.dataset.peerId.toPeerId();
|
2021-04-27 17:45:53 +02:00
|
|
|
this.dialog = appMessagesManager.getDialogOnly(this.selectedId);
|
2020-10-07 15:57:33 +02:00
|
|
|
|
2020-11-23 18:25:14 +01:00
|
|
|
this.buttons.forEach(button => {
|
|
|
|
const good = button.verify();
|
2020-10-07 15:57:33 +02:00
|
|
|
|
2020-11-23 18:25:14 +01:00
|
|
|
button.element.classList.toggle('hide', !good);
|
|
|
|
});
|
2020-10-07 15:57:33 +02:00
|
|
|
|
|
|
|
// delete button
|
2021-03-29 20:42:01 +02:00
|
|
|
this.buttons[this.buttons.length - 1].element.lastChild.replaceWith(i18n(appPeersManager.getDeleteButtonText(this.selectedId)));
|
2020-10-07 15:57:33 +02:00
|
|
|
|
|
|
|
li.classList.add('menu-open');
|
|
|
|
positionMenu(e, this.element);
|
|
|
|
openBtnMenu(this.element, () => {
|
|
|
|
li.classList.remove('menu-open');
|
2020-12-11 03:06:16 +01:00
|
|
|
this.selectedId = this.dialog = this.filterId = undefined;
|
2020-10-07 15:57:33 +02:00
|
|
|
});
|
|
|
|
};
|
2021-03-29 20:42:01 +02:00
|
|
|
}
|