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

85 lines
2.4 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 { putPreloader } from "../../misc";
2021-01-06 10:16:53 +01:00
import Button from "../../button";
2021-03-25 19:07:00 +01:00
import { LangPackKey, _i18n } from "../../../lib/langPack";
2021-01-06 10:16:53 +01:00
export default class AppAddMembersTab extends SliderSuperTab {
private nextBtn: HTMLButtonElement;
private selector: AppSelectPeers;
2021-01-06 10:16:53 +01:00
private peerType: 'channel' | 'chat' | 'privacy';
private takeOut: (peerIds: number[]) => Promise<any> | any;
2020-06-15 13:21:40 +02:00
private skippable: boolean;
2021-01-06 10:16:53 +01:00
protected init() {
this.nextBtn = Button('btn-corner btn-circle', {icon: 'arrow_next'});
2021-01-06 10:16:53 +01:00
this.content.append(this.nextBtn);
this.nextBtn.addEventListener('click', () => {
const peerIds = this.selector.getSelected();
2021-01-06 10:16:53 +01:00
if(this.skippable) {
this.takeOut(peerIds);
this.close();
} else {
const promise = this.takeOut(peerIds);
2021-01-06 10:16:53 +01:00
if(promise instanceof Promise) {
this.nextBtn.classList.remove('tgico-arrow_next');
2021-01-06 10:16:53 +01:00
this.nextBtn.disabled = true;
putPreloader(this.nextBtn);
this.selector.freezed = true;
promise.then(() => {
this.close();
});
} else {
this.close();
}
}
});
}
2021-01-06 10:16:53 +01:00
public open(options: {
2021-03-25 19:07:00 +01:00
title: LangPackKey,
placeholder: LangPackKey,
2021-01-06 10:16:53 +01:00
peerId?: number,
type: AppAddMembersTab['peerType'],
takeOut?: AppAddMembersTab['takeOut'],
skippable: boolean,
selectedPeerIds?: number[]
}) {
const ret = super.open();
2021-03-25 19:07:00 +01:00
this.setTitle(options.title);
2021-01-06 10:16:53 +01:00
this.peerType = options.type;
this.takeOut = options.takeOut;
this.skippable = options.skippable;
this.selector = new AppSelectPeers({
appendTo: this.content,
onChange: this.skippable ? null : (length) => {
this.nextBtn.classList.toggle('is-visible', !!length);
},
2021-03-25 19:07:00 +01:00
peerType: ['contacts'],
placeholder: options.placeholder
});
2021-01-06 10:16:53 +01:00
if(options.selectedPeerIds) {
this.selector.addInitial(options.selectedPeerIds);
2021-01-06 10:16:53 +01:00
}
this.nextBtn.classList.add('tgico-arrow_next');
this.nextBtn.innerHTML = '';
this.nextBtn.disabled = false;
2021-01-06 10:16:53 +01:00
this.nextBtn.classList.toggle('is-visible', this.skippable);
2021-01-06 10:16:53 +01:00
return ret;
}
}