tweb/src/components/sidebarLeft/tabs/includedChats.ts

274 lines
8.7 KiB
TypeScript
Raw Normal View History

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
*/
import { SliderSuperTab } from "../../slider";
import AppSelectPeers from "../../appSelectPeers";
import appDialogsManager from "../../../lib/appManagers/appDialogsManager";
import appUsersManager from "../../../lib/appManagers/appUsersManager";
import { MyDialogFilter as DialogFilter } from "../../../lib/storages/filters";
import { copy } from "../../../helpers/object";
2021-01-01 20:44:31 +01:00
import ButtonIcon from "../../buttonIcon";
import CheckboxField from "../../checkboxField";
2021-01-20 02:38:59 +01:00
import Button from "../../button";
2021-02-20 18:10:26 +01:00
import AppEditFolderTab from "./editFolder";
2021-03-23 11:17:04 +01:00
import { i18n, LangPackKey, _i18n, join } from "../../../lib/langPack";
import appMessagesManager from "../../../lib/appManagers/appMessagesManager";
import RichTextProcessor from "../../../lib/richtextprocessor";
2020-06-19 13:49:55 +02:00
2021-01-01 20:44:31 +01:00
export default class AppIncludedChatsTab extends SliderSuperTab {
2021-02-20 18:10:26 +01:00
private editFolderTab: AppEditFolderTab;
2020-06-19 13:49:55 +02:00
private confirmBtn: HTMLElement;
private selector: AppSelectPeers;
private type: 'included' | 'excluded';
private filter: DialogFilter;
private originalFilter: DialogFilter;
2021-03-23 11:17:04 +01:00
private dialogsByFilters: Map<DialogFilter, Set<number>>;
protected init() {
2021-01-01 20:44:31 +01:00
this.content.remove();
this.container.classList.add('included-chatlist-container');
this.confirmBtn = ButtonIcon('check btn-confirm blue', {noRipple: true});
2021-01-01 20:44:31 +01:00
this.confirmBtn.style.display = 'none';
this.header.append(this.confirmBtn);
2020-06-19 13:49:55 +02:00
this.confirmBtn.addEventListener('click', () => {
const selected = this.selector.getSelected();
2020-06-21 14:25:17 +02:00
//this.filter.pFlags = {};
2021-02-04 01:30:23 +01:00
if(this.type === 'included') {
2020-06-21 14:25:17 +02:00
for(const key in this.filter.pFlags) {
if(key.indexOf('exclude_') === 0) {
continue;
}
// @ts-ignore
delete this.filter.pFlags[key];
}
} else {
for(const key in this.filter.pFlags) {
if(key.indexOf('exclude_') !== 0) {
continue;
}
// @ts-ignore
delete this.filter.pFlags[key];
}
}
2020-06-19 13:49:55 +02:00
const peers: number[] = [];
for(const key of selected) {
if(typeof(key) === 'number') {
peers.push(key);
} else {
// @ts-ignore
this.filter.pFlags[key] = true;
}
}
2021-02-04 01:30:23 +01:00
if(this.type === 'included') {
this.filter.pinned_peers = this.filter.pinned_peers.filter(peerId => {
return peers.includes(peerId); // * because I have pinned peer in include_peers too
/* const index = peers.indexOf(peerId);
if(index !== -1) {
peers.splice(index, 1);
return true;
} else {
return false;
} */
});
} else {
this.filter.pinned_peers = this.filter.pinned_peers.filter(peerId => {
return !peers.includes(peerId);
});
}
2021-02-04 01:30:23 +01:00
const other = this.type === 'included' ? 'exclude_peers' : 'include_peers';
this.filter[other] = this.filter[other].filter(peerId => {
return !peers.includes(peerId);
});
2021-02-04 01:30:23 +01:00
this.filter[this.type === 'included' ? 'include_peers' : 'exclude_peers'] = peers;
//this.filter.pinned_peers = this.filter.pinned_peers.filter(peerId => this.filter.include_peers.includes(peerId));
2020-06-19 13:49:55 +02:00
2021-02-20 18:10:26 +01:00
this.editFolderTab.setFilter(this.filter, false);
2021-01-01 20:44:31 +01:00
this.close();
2020-06-19 13:49:55 +02:00
});
2021-03-23 11:17:04 +01:00
this.dialogsByFilters = new Map();
2021-03-23 17:13:35 +01:00
return appMessagesManager.filtersStorage.getDialogFilters().then(filters => {
2021-03-23 11:17:04 +01:00
for(const filter of filters) {
this.dialogsByFilters.set(filter, new Set(appMessagesManager.dialogsStorage.getFolder(filter.id).map(d => d.peerId)));
}
});
2020-06-19 13:49:55 +02:00
}
checkbox(selected?: boolean) {
const checkboxField = new CheckboxField({
2021-02-01 04:07:44 +01:00
round: true
});
2021-01-20 02:38:59 +01:00
if(selected) {
checkboxField.input.checked = selected;
}
return checkboxField.label;
2020-06-19 13:49:55 +02:00
}
renderResults = async(peerIds: number[]) => {
2021-02-04 01:30:23 +01:00
//const other = this.type === 'included' ? this.filter.exclude_peers : this.filter.include_peers;
2020-06-19 13:49:55 +02:00
await appUsersManager.getContacts();
peerIds.forEach(peerId => {
//if(other.includes(peerId)) return;
2020-06-19 13:49:55 +02:00
const {dom} = appDialogsManager.addDialogNew({
dialog: peerId,
container: this.selector.scrollable,
drawStatus: false,
2021-01-20 02:38:59 +01:00
rippleEnabled: true,
avatarSize: 46
});
2020-06-19 13:49:55 +02:00
const selected = this.selector.selected.has(peerId);
2021-01-20 02:38:59 +01:00
dom.containerEl.append(this.checkbox(selected));
2020-06-19 13:49:55 +02:00
if(selected) dom.listEl.classList.add('active');
2021-03-23 11:17:04 +01:00
const foundInFilters: HTMLElement[] = [];
this.dialogsByFilters.forEach((dialogs, filter) => {
if(dialogs.has(peerId)) {
const span = document.createElement('span');
span.innerHTML = RichTextProcessor.wrapEmojiText(filter.title);
foundInFilters.push(span);
}
});
const joined = join(foundInFilters, false);
joined.forEach(el => {
dom.lastMessageSpan.append(el);
});
2020-06-19 13:49:55 +02:00
});
};
onOpen() {
if(this.init) {
this.init();
this.init = null;
}
2021-02-04 01:30:23 +01:00
this.confirmBtn.style.display = this.type === 'excluded' ? '' : 'none';
2021-03-22 19:38:03 +01:00
this.setTitle(this.type === 'included' ? 'FilterAlwaysShow' : 'FilterNeverShow');
2020-06-19 13:49:55 +02:00
const filter = this.filter;
const fragment = document.createDocumentFragment();
const dd = document.createElement('div');
dd.classList.add('sidebar-left-h2');
2021-03-22 19:38:03 +01:00
_i18n(dd, 'FilterChatTypes');
2020-06-19 13:49:55 +02:00
const categories = document.createElement('div');
categories.classList.add('folder-categories');
2021-03-22 19:38:03 +01:00
let details: {[flag: string]: {ico: string, text: LangPackKey}};
2021-02-04 01:30:23 +01:00
if(this.type === 'excluded') {
2020-06-19 13:49:55 +02:00
details = {
2021-03-21 15:36:14 +01:00
exclude_muted: {ico: 'mute', text: 'ChatList.Filter.MutedChats'},
exclude_archived: {ico: 'archive', text: 'ChatList.Filter.Archive'},
exclude_read: {ico: 'readchats', text: 'ChatList.Filter.ReadChats'}
2020-06-19 13:49:55 +02:00
};
} else {
details = {
2021-03-21 15:36:14 +01:00
contacts: {ico: 'newprivate', text: 'ChatList.Filter.Contacts'},
non_contacts: {ico: 'noncontacts', text: 'ChatList.Filter.NonContacts'},
groups: {ico: 'group', text: 'ChatList.Filter.Groups'},
broadcasts: {ico: 'newchannel', text: 'ChatList.Filter.Channels'},
bots: {ico: 'bots', text: 'ChatList.Filter.Bots'}
2020-06-19 13:49:55 +02:00
};
}
2021-01-20 02:38:59 +01:00
const f = document.createDocumentFragment();
2020-06-19 13:49:55 +02:00
for(const key in details) {
2021-01-20 02:38:59 +01:00
const button = Button('btn-primary btn-transparent folder-category-button', {icon: details[key].ico, text: details[key].text});
button.dataset.peerId = key;
button.append(this.checkbox());
f.append(button);
2020-06-19 13:49:55 +02:00
}
2021-01-20 02:38:59 +01:00
categories.append(f);
2020-06-19 13:49:55 +02:00
const hr = document.createElement('hr');
hr.style.margin = '7px 0 9px';
const d = document.createElement('div');
d.classList.add('sidebar-left-h2');
2021-03-22 19:38:03 +01:00
_i18n(d, 'FilterChats');
2020-06-19 13:49:55 +02:00
fragment.append(dd, categories, hr, d);
/////////////////
2021-01-01 20:44:31 +01:00
const selectedPeers = (this.type === 'included' ? filter.include_peers : filter.exclude_peers).slice();
2020-06-19 13:49:55 +02:00
this.selector = new AppSelectPeers({
appendTo: this.container,
onChange: this.onSelectChange,
peerType: ['dialogs'],
2021-03-25 19:07:00 +01:00
renderResultsFunc: this.renderResults,
placeholder: 'Search'
});
2020-06-19 13:49:55 +02:00
this.selector.selected = new Set(selectedPeers);
const _add = this.selector.add.bind(this.selector);
this.selector.add = (peerId, title, scroll) => {
2021-03-21 15:36:14 +01:00
const div = _add(peerId, details[peerId] ? i18n(details[peerId].text) : undefined, scroll);
if(details[peerId]) {
2021-01-01 20:44:31 +01:00
div.querySelector('avatar-element').classList.add('tgico-' + details[peerId].ico);
2020-06-19 13:49:55 +02:00
}
return div;
};
this.selector.list.parentElement.insertBefore(fragment, this.selector.list);
this.selector.addInitial(selectedPeers);
2020-06-19 13:49:55 +02:00
for(const flag in filter.pFlags) {
// @ts-ignore
if(details.hasOwnProperty(flag) && !!filter.pFlags[flag]) {
(categories.querySelector(`[data-peer-id="${flag}"]`) as HTMLElement).click();
2020-06-19 13:49:55 +02:00
}
}
}
onSelectChange = (length: number) => {
//const changed = !deepEqual(this.filter, this.originalFilter);
2021-02-04 01:30:23 +01:00
if(this.type === 'included') {
2020-06-19 13:49:55 +02:00
this.confirmBtn.style.display = length ? '' : 'none';
}
};
onCloseAfterTimeout() {
if(this.selector) {
this.selector.container.remove();
this.selector = null;
}
2021-02-20 18:10:26 +01:00
return super.onCloseAfterTimeout();
2020-06-19 13:49:55 +02:00
}
2021-01-01 20:44:31 +01:00
/**
* Do not ignore arguments!
*/
2021-02-20 18:10:26 +01:00
public open(filter?: DialogFilter, type?: 'included' | 'excluded', editFolderTab?: AppIncludedChatsTab['editFolderTab']) {
2020-06-19 13:49:55 +02:00
this.originalFilter = filter;
this.filter = copy(this.originalFilter);
this.type = type;
2021-02-20 18:10:26 +01:00
this.editFolderTab = editFolderTab;
2021-01-01 20:44:31 +01:00
return super.open();
2020-06-19 13:49:55 +02:00
}
2021-03-21 15:36:14 +01:00
}