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.

87 lines
2.4 KiB

3 years ago
/*
* https://github.com/morethanwords/tweb
* Copyright (C) 2019-2021 Eduard Kuzmenko
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*/
2 years ago
import getDeepProperty from '../helpers/object/getDeepProperty';
import {LangPackKey, _i18n} from '../lib/langPack';
import apiManagerProxy from '../lib/mtproto/mtprotoworker';
import rootScope from '../lib/rootScope';
3 years ago
export default class RadioField {
public input: HTMLInputElement;
public label: HTMLLabelElement;
public main: HTMLElement;
constructor(options: {
2 years ago
text?: string,
3 years ago
langKey?: LangPackKey,
2 years ago
name: string,
value?: string,
stateKey?: string,
alignRight?: boolean
3 years ago
}) {
const label = this.label = document.createElement('label');
label.classList.add('radio-field');
if(options.alignRight) {
label.classList.add('radio-field-right');
}
2 years ago
3 years ago
const input = this.input = document.createElement('input');
input.type = 'radio';
/* input.id = */input.name = 'input-radio-' + options.name;
2 years ago
3 years ago
if(options.value) {
input.value = options.value;
2 years ago
3 years ago
if(options.stateKey) {
apiManagerProxy.getState().then((state) => {
3 years ago
input.checked = getDeepProperty(state, options.stateKey) === options.value;
});
2 years ago
3 years ago
input.addEventListener('change', () => {
rootScope.managers.appStateManager.setByKey(options.stateKey, options.value);
3 years ago
});
}
}
2 years ago
3 years ago
const main = this.main = document.createElement('div');
main.classList.add('radio-field-main');
2 years ago
3 years ago
if(options.text) {
main.innerHTML = options.text;
/* const caption = document.createElement('div');
caption.classList.add('radio-field-main-caption');
caption.innerHTML = text;
2 years ago
3 years ago
if(subtitle) {
label.classList.add('radio-field-with-subtitle');
caption.insertAdjacentHTML('beforeend', `<div class="radio-field-main-subtitle">${subtitle}</div>`);
}
2 years ago
3 years ago
main.append(caption); */
} else if(options.langKey) {
_i18n(main, options.langKey);
}
2 years ago
3 years ago
label.append(input, main);
}
get checked() {
return this.input.checked;
}
set checked(checked: boolean) {
this.setValueSilently(checked);
const event = new Event('change', {bubbles: true, cancelable: true});
this.input.dispatchEvent(event);
}
public setValueSilently(checked: boolean) {
this.input.checked = checked;
}
};