Telegram Web K with changes to work inside I2P
https://web.telegram.i2p/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
316 lines
10 KiB
316 lines
10 KiB
4 years ago
|
import type { Poll } from "../../lib/appManagers/appPollsManager";
|
||
|
import type Chat from "../chat/chat";
|
||
|
import PopupElement from ".";
|
||
|
import { cancelEvent, findUpTag, getRichValue, isInputEmpty, whichChild } from "../../helpers/dom";
|
||
|
import CheckboxField from "../checkbox";
|
||
|
import InputField from "../inputField";
|
||
|
import RadioField from "../radioField";
|
||
|
import Scrollable from "../scrollable";
|
||
|
import { toast } from "../toast";
|
||
|
import SendContextMenu from "../chat/sendContextMenu";
|
||
4 years ago
|
|
||
4 years ago
|
const MAX_LENGTH_QUESTION = 255;
|
||
|
const MAX_LENGTH_OPTION = 100;
|
||
|
const MAX_LENGTH_SOLUTION = 200;
|
||
|
|
||
4 years ago
|
export default class PopupCreatePoll extends PopupElement {
|
||
4 years ago
|
private questionInputField: InputField;
|
||
4 years ago
|
private questions: HTMLElement;
|
||
|
private scrollable: Scrollable;
|
||
4 years ago
|
private tempId = 0;
|
||
4 years ago
|
|
||
4 years ago
|
private anonymousCheckboxField: ReturnType<typeof CheckboxField>;
|
||
|
private multipleCheckboxField: PopupCreatePoll['anonymousCheckboxField'];
|
||
|
private quizCheckboxField: PopupCreatePoll['anonymousCheckboxField'];
|
||
|
|
||
|
private correctAnswers: Uint8Array[];
|
||
4 years ago
|
private quizSolutionField: InputField;
|
||
4 years ago
|
|
||
4 years ago
|
constructor(private chat: Chat) {
|
||
4 years ago
|
super('popup-create-poll popup-new-media', null, {closable: true, withConfirm: 'CREATE', body: true});
|
||
|
|
||
|
this.title.innerText = 'New Poll';
|
||
|
|
||
4 years ago
|
this.questionInputField = new InputField({
|
||
4 years ago
|
placeholder: 'Ask a Question',
|
||
|
label: 'Ask a Question',
|
||
|
name: 'question',
|
||
|
maxLength: MAX_LENGTH_QUESTION
|
||
|
});
|
||
4 years ago
|
|
||
4 years ago
|
if(this.chat.type !== 'scheduled') {
|
||
|
const sendMenu = new SendContextMenu({
|
||
|
onSilentClick: () => {
|
||
|
this.chat.input.sendSilent = true;
|
||
|
this.send();
|
||
|
},
|
||
|
onScheduleClick: () => {
|
||
|
this.chat.input.scheduleSending(() => {
|
||
|
this.send();
|
||
|
});
|
||
|
},
|
||
|
openSide: 'bottom-left',
|
||
|
onContextElement: this.btnConfirm,
|
||
|
});
|
||
|
|
||
|
sendMenu.setPeerId(this.chat.peerId);
|
||
|
|
||
|
this.header.append(sendMenu.sendMenu);
|
||
|
}
|
||
|
|
||
|
this.header.append(this.questionInputField.container);
|
||
4 years ago
|
|
||
4 years ago
|
const hr = document.createElement('hr');
|
||
4 years ago
|
const d = document.createElement('div');
|
||
4 years ago
|
d.classList.add('caption');
|
||
4 years ago
|
d.innerText = 'Options';
|
||
|
|
||
4 years ago
|
this.questions = document.createElement('form');
|
||
4 years ago
|
this.questions.classList.add('poll-create-questions');
|
||
|
|
||
4 years ago
|
const dd = document.createElement('div');
|
||
|
dd.classList.add('poll-create-settings');
|
||
|
|
||
|
const settingsCaption = document.createElement('div');
|
||
|
settingsCaption.classList.add('caption');
|
||
|
settingsCaption.innerText = 'Settings';
|
||
|
|
||
4 years ago
|
if(!this.chat.appPeersManager.isBroadcast(this.chat.peerId)) {
|
||
4 years ago
|
this.anonymousCheckboxField = CheckboxField('Anonymous Voting', 'anonymous');
|
||
|
this.anonymousCheckboxField.input.checked = true;
|
||
|
dd.append(this.anonymousCheckboxField.label);
|
||
|
}
|
||
|
|
||
|
this.multipleCheckboxField = CheckboxField('Multiple Answers', 'multiple');
|
||
|
this.quizCheckboxField = CheckboxField('Quiz Mode', 'quiz');
|
||
|
|
||
|
this.multipleCheckboxField.input.addEventListener('change', () => {
|
||
|
const checked = this.multipleCheckboxField.input.checked;
|
||
|
this.quizCheckboxField.input.toggleAttribute('disabled', checked);
|
||
|
});
|
||
|
|
||
|
this.quizCheckboxField.input.addEventListener('change', () => {
|
||
|
const checked = this.quizCheckboxField.input.checked;
|
||
|
|
||
|
(Array.from(this.questions.children) as HTMLElement[]).map(el => {
|
||
|
el.classList.toggle('radio-field', checked);
|
||
|
});
|
||
|
|
||
|
quizElements.forEach(el => el.classList.toggle('hide', !checked));
|
||
|
|
||
|
this.multipleCheckboxField.input.toggleAttribute('disabled', checked);
|
||
|
});
|
||
|
|
||
|
dd.append(this.multipleCheckboxField.label, this.quizCheckboxField.label);
|
||
|
|
||
|
const quizElements: HTMLElement[] = [];
|
||
|
|
||
|
const quizSolutionCaption = document.createElement('div');
|
||
|
quizSolutionCaption.classList.add('caption');
|
||
|
quizSolutionCaption.innerText = 'Explanation';
|
||
|
|
||
|
const quizHr = document.createElement('hr');
|
||
|
|
||
|
const quizSolutionContainer = document.createElement('div');
|
||
|
quizSolutionContainer.classList.add('poll-create-questions');
|
||
|
|
||
4 years ago
|
this.quizSolutionField = new InputField({
|
||
4 years ago
|
placeholder: 'Add a Comment (Optional)',
|
||
|
label: 'Add a Comment (Optional)',
|
||
|
name: 'solution',
|
||
|
maxLength: MAX_LENGTH_SOLUTION
|
||
|
});
|
||
4 years ago
|
|
||
|
const quizSolutionSubtitle = document.createElement('div');
|
||
|
quizSolutionSubtitle.classList.add('subtitle');
|
||
|
quizSolutionSubtitle.innerText = 'Users will see this comment after choosing a wrong answer, good for educational purposes.';
|
||
|
|
||
4 years ago
|
quizSolutionContainer.append(this.quizSolutionField.container, quizSolutionSubtitle);
|
||
4 years ago
|
|
||
|
quizElements.push(quizHr, quizSolutionCaption, quizSolutionContainer);
|
||
|
quizElements.forEach(el => el.classList.add('hide'));
|
||
|
|
||
4 years ago
|
this.body.parentElement.insertBefore(hr, this.body);
|
||
4 years ago
|
this.body.append(d, this.questions, document.createElement('hr'), settingsCaption, dd, ...quizElements);
|
||
4 years ago
|
|
||
4 years ago
|
this.btnConfirm.addEventListener('click', this.onSubmitClick);
|
||
4 years ago
|
|
||
4 years ago
|
this.scrollable = new Scrollable(this.body);
|
||
4 years ago
|
this.appendMoreField();
|
||
4 years ago
|
|
||
|
this.onEscape = () => {
|
||
|
return !this.getFilledAnswers().length;
|
||
|
};
|
||
|
}
|
||
|
|
||
|
private getFilledAnswers() {
|
||
|
const answers = Array.from(this.questions.children).map((el, idx) => {
|
||
4 years ago
|
const input = el.querySelector('.input-field-input') as HTMLElement;
|
||
4 years ago
|
return input instanceof HTMLInputElement ? input.value : getRichValue(input);
|
||
4 years ago
|
}).filter(v => !!v.trim());
|
||
|
|
||
|
return answers;
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
private onSubmitClick = () => {
|
||
|
this.send();
|
||
|
};
|
||
|
|
||
|
public send(force = false) {
|
||
|
const question = this.questionInputField.value;
|
||
4 years ago
|
|
||
4 years ago
|
if(!question) {
|
||
4 years ago
|
toast('Please enter a question.');
|
||
|
return;
|
||
|
}
|
||
|
|
||
4 years ago
|
if(question.length > MAX_LENGTH_QUESTION) {
|
||
|
toast('Question is too long.');
|
||
|
return;
|
||
|
}
|
||
|
|
||
4 years ago
|
if(this.quizCheckboxField.input.checked && !this.correctAnswers?.length) {
|
||
|
toast('Please choose the correct answer.');
|
||
4 years ago
|
return;
|
||
|
}
|
||
|
|
||
4 years ago
|
const answers = this.getFilledAnswers();
|
||
4 years ago
|
|
||
|
if(answers.length < 2) {
|
||
4 years ago
|
toast('Please enter at least two options.');
|
||
4 years ago
|
return;
|
||
|
}
|
||
|
|
||
4 years ago
|
const tooLongOption = answers.find(a => a.length > MAX_LENGTH_OPTION);
|
||
|
if(tooLongOption) {
|
||
|
toast('Option is too long.');
|
||
|
return;
|
||
|
}
|
||
|
|
||
4 years ago
|
const quizSolution = this.quizSolutionField.value || undefined;
|
||
4 years ago
|
if(quizSolution?.length > MAX_LENGTH_SOLUTION) {
|
||
4 years ago
|
toast('Explanation is too long.');
|
||
|
return;
|
||
|
}
|
||
|
|
||
4 years ago
|
if(this.chat.type === 'scheduled' && !force) {
|
||
|
this.chat.input.scheduleSending(() => {
|
||
|
this.send(true);
|
||
|
});
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
this.btnClose.click();
|
||
|
this.btnConfirm.removeEventListener('click', this.onSubmitClick);
|
||
4 years ago
|
|
||
|
//const randomID = [nextRandomInt(0xFFFFFFFF), nextRandomInt(0xFFFFFFFF)];
|
||
|
//const randomIDS = bigint(randomID[0]).shiftLeft(32).add(bigint(randomID[1])).toString();
|
||
|
|
||
4 years ago
|
const pFlags: Poll['pFlags'] = {};
|
||
|
|
||
|
if(this.anonymousCheckboxField && !this.anonymousCheckboxField.input.checked) {
|
||
|
pFlags.public_voters = true;
|
||
|
}
|
||
|
|
||
|
if(this.multipleCheckboxField.input.checked) {
|
||
|
pFlags.multiple_choice = true;
|
||
|
}
|
||
|
|
||
|
if(this.quizCheckboxField.input.checked) {
|
||
|
pFlags.quiz = true;
|
||
|
}
|
||
|
|
||
4 years ago
|
const poll: Poll = {
|
||
|
_: 'poll',
|
||
4 years ago
|
pFlags,
|
||
4 years ago
|
question,
|
||
|
answers: answers.map((value, idx) => {
|
||
|
return {
|
||
|
_: 'pollAnswer',
|
||
|
text: value,
|
||
|
option: new Uint8Array([idx])
|
||
|
};
|
||
|
}),
|
||
|
id: undefined
|
||
|
};
|
||
4 years ago
|
//poll.id = randomIDS;
|
||
4 years ago
|
|
||
4 years ago
|
const inputMediaPoll = this.chat.appPollsManager.getInputMediaPoll(poll, this.correctAnswers, quizSolution);
|
||
4 years ago
|
|
||
|
//console.log('Will try to create poll:', inputMediaPoll);
|
||
4 years ago
|
|
||
4 years ago
|
this.chat.appMessagesManager.sendOther(this.chat.peerId, inputMediaPoll, {
|
||
|
scheduleDate: this.chat.input.scheduleDate,
|
||
|
silent: this.chat.input.sendSilent
|
||
|
});
|
||
|
|
||
|
this.chat.input.onMessageSent(false, false);
|
||
|
}
|
||
4 years ago
|
|
||
|
onInput = (e: Event) => {
|
||
|
const target = e.target as HTMLInputElement;
|
||
|
|
||
4 years ago
|
const radioLabel = findUpTag(target, 'LABEL');
|
||
4 years ago
|
const isEmpty = isInputEmpty(target);
|
||
|
if(!isEmpty) {
|
||
4 years ago
|
target.parentElement.classList.add('is-filled');
|
||
4 years ago
|
radioLabel.classList.remove('hidden-widget');
|
||
|
radioLabel.firstElementChild.removeAttribute('disabled');
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
const isLast = !radioLabel.nextElementSibling;
|
||
4 years ago
|
if(isLast && !isEmpty && this.questions.childElementCount < 10) {
|
||
4 years ago
|
this.appendMoreField();
|
||
|
}
|
||
|
};
|
||
|
|
||
|
onDeleteClick = (e: MouseEvent) => {
|
||
|
const target = e.target as HTMLSpanElement;
|
||
4 years ago
|
findUpTag(target, 'LABEL').remove();
|
||
4 years ago
|
|
||
|
Array.from(this.questions.children).forEach((el, idx) => {
|
||
4 years ago
|
const label = el.querySelector('label') as HTMLLabelElement;
|
||
4 years ago
|
label.innerText = 'Option ' + (idx + 1);
|
||
|
});
|
||
|
};
|
||
|
|
||
|
private appendMoreField() {
|
||
4 years ago
|
const tempId = this.tempId++;
|
||
4 years ago
|
const idx = this.questions.childElementCount + 1;
|
||
4 years ago
|
const questionField = new InputField({
|
||
4 years ago
|
placeholder: 'Add an Option',
|
||
|
label: 'Option ' + idx,
|
||
4 years ago
|
name: 'question-' + tempId,
|
||
4 years ago
|
maxLength: MAX_LENGTH_OPTION
|
||
|
});
|
||
|
questionField.input.addEventListener('input', this.onInput);
|
||
4 years ago
|
|
||
4 years ago
|
const radioField = RadioField('', 'question');
|
||
4 years ago
|
radioField.main.append(questionField.container);
|
||
|
radioField.main.addEventListener('click', cancelEvent);
|
||
4 years ago
|
radioField.label.classList.add('hidden-widget');
|
||
|
radioField.input.disabled = true;
|
||
|
if(!this.quizCheckboxField.input.checked) {
|
||
|
radioField.label.classList.remove('radio-field');
|
||
|
}
|
||
|
radioField.input.addEventListener('change', () => {
|
||
|
const checked = radioField.input.checked;
|
||
|
if(checked) {
|
||
|
const idx = whichChild(radioField.label);
|
||
|
this.correctAnswers = [new Uint8Array([idx])];
|
||
|
}
|
||
|
});
|
||
|
|
||
4 years ago
|
const deleteBtn = document.createElement('span');
|
||
|
deleteBtn.classList.add('btn-icon', 'tgico-close');
|
||
4 years ago
|
questionField.container.append(deleteBtn);
|
||
4 years ago
|
|
||
|
deleteBtn.addEventListener('click', this.onDeleteClick, {once: true});
|
||
|
|
||
4 years ago
|
this.questions.append(radioField.label);
|
||
4 years ago
|
|
||
4 years ago
|
this.scrollable.scrollIntoView(this.questions.lastElementChild as HTMLElement, true);
|
||
|
//this.scrollable.scrollTo(this.scrollable.scrollHeight, 'top', true, true);
|
||
4 years ago
|
}
|
||
|
}
|