tweb/src/components/popups/stickers.ts

159 lines
4.9 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 PopupElement from ".";
import appStickersManager from "../../lib/appManagers/appStickersManager";
import { RichTextProcessor } from "../../lib/richtextprocessor";
import Scrollable from "../scrollable";
import { wrapSticker } from "../wrappers";
import LazyLoadQueue from "../lazyLoadQueue";
import { putPreloader } from "../misc";
import animationIntersector from "../animationIntersector";
2021-04-04 17:39:17 +02:00
import { toggleDisability } from "../../helpers/dom";
import appImManager from "../../lib/appManagers/appImManager";
import { StickerSet } from "../../layer";
import mediaSizes from "../../helpers/mediaSizes";
2021-03-28 20:37:11 +02:00
import { i18n } from "../../lib/langPack";
import Button from "../button";
2021-04-04 17:39:17 +02:00
import findUpClassName from "../../helpers/dom/findUpClassName";
const ANIMATION_GROUP = 'STICKERS-POPUP';
export default class PopupStickers extends PopupElement {
private stickersFooter: HTMLElement;
private stickersDiv: HTMLElement;
private h6: HTMLElement;
private set: StickerSet.stickerSet;
constructor(private stickerSetInput: {
//_: 'inputStickerSetID',
id: string,
access_hash: string
}) {
super('popup-stickers', null, {closable: true, overlayClosable: true, body: true});
this.h6 = document.createElement('h6');
2021-03-28 20:37:11 +02:00
this.h6.append(i18n('Loading'));
2020-06-16 22:48:08 +02:00
this.header.append(this.h6);
2020-06-16 22:48:08 +02:00
this.onClose = () => {
animationIntersector.setOnlyOnePlayableGroup('');
this.stickersFooter.removeEventListener('click', this.onFooterClick);
this.stickersDiv.removeEventListener('click', this.onStickersClick);
2020-06-16 22:48:08 +02:00
};
const div = document.createElement('div');
div.classList.add('sticker-set');
this.stickersDiv = document.createElement('div');
2021-04-02 23:45:51 +02:00
this.stickersDiv.classList.add('sticker-set-stickers', 'is-loading');
2021-04-02 23:45:51 +02:00
putPreloader(this.stickersDiv, true);
this.stickersFooter = document.createElement('div');
this.stickersFooter.classList.add('sticker-set-footer');
div.append(this.stickersDiv);
2021-04-02 23:45:51 +02:00
const btn = Button('btn-primary btn-primary-transparent disable-hover', {noRipple: true, text: 'Loading'});
this.stickersFooter.append(btn);
2020-06-16 22:48:08 +02:00
this.body.append(div);
const scrollable = new Scrollable(this.body);
2020-06-16 22:48:08 +02:00
this.body.append(this.stickersFooter);
// const editButton = document.createElement('button');
// editButton.classList.add('btn-primary');
// this.stickersFooter.append(editButton);
this.loadStickerSet();
}
onFooterClick = () => {
2021-03-28 20:37:11 +02:00
const toggle = toggleDisability([this.stickersFooter], true);
appStickersManager.toggleStickerSet(this.set).then(() => {
2021-03-28 20:37:11 +02:00
this.hide();
}).catch(() => {
2021-03-28 20:37:11 +02:00
toggle();
});
};
onStickersClick = (e: MouseEvent) => {
const target = findUpClassName(e.target, 'sticker-set-sticker');
if(!target) return;
const fileId = target.dataset.docId;
if(appImManager.chat.input.sendMessageWithDocument(fileId)) {
2021-03-28 20:37:11 +02:00
this.hide();
} else {
console.warn('got no doc by id:', fileId);
}
};
private loadStickerSet() {
return appStickersManager.getStickerSet(this.stickerSetInput).then(set => {
//console.log('PopupStickers loadStickerSet got set:', set);
this.set = set.set;
animationIntersector.setOnlyOnePlayableGroup(ANIMATION_GROUP);
this.h6.innerHTML = RichTextProcessor.wrapEmojiText(set.set.title);
2021-04-02 23:45:51 +02:00
this.stickersFooter.classList.toggle('add', !set.set.installed_date);
2021-03-28 20:37:11 +02:00
let button: HTMLElement;
2021-04-02 23:45:51 +02:00
if(set.set.installed_date) {
2021-03-28 20:37:11 +02:00
button = Button('btn-primary btn-primary-transparent danger', {noRipple: true});
2021-04-02 23:45:51 +02:00
button.append(i18n('RemoveStickersCount', [i18n('Stickers', [set.set.count])]));
2021-03-28 20:37:11 +02:00
} else {
button = Button('btn-primary btn-color-primary', {noRipple: true});
2021-04-02 23:45:51 +02:00
button.append(i18n('AddStickersCount', [i18n('Stickers', [set.set.count])]));
2021-03-28 20:37:11 +02:00
}
this.stickersFooter.textContent = '';
this.stickersFooter.append(button);
button.addEventListener('click', this.onFooterClick);
if(set.documents.length) {
this.stickersDiv.addEventListener('click', this.onStickersClick);
}
const lazyLoadQueue = new LazyLoadQueue();
2021-04-02 23:45:51 +02:00
this.stickersDiv.classList.remove('is-loading');
this.stickersDiv.innerHTML = '';
for(let doc of set.documents) {
2021-02-04 01:30:23 +01:00
if(doc._ === 'documentEmpty') {
continue;
}
const div = document.createElement('div');
div.classList.add('sticker-set-sticker');
const size = mediaSizes.active.esgSticker.width;
wrapSticker({
doc,
div,
lazyLoadQueue,
group: ANIMATION_GROUP,
play: true,
loop: true,
width: size,
height: size
});
this.stickersDiv.append(div);
}
});
}
2021-03-28 20:37:11 +02:00
}