152 lines
4.7 KiB
TypeScript
Raw Normal View History

2021-04-08 17:52:31 +04:00
/*
* https://github.com/morethanwords/tweb
* Copyright (C) 2019-2021 Eduard Kuzmenko
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*/
import CheckboxField from "./checkboxField";
2021-01-06 13:16:53 +04:00
import RadioField from "./radioField";
import { ripple } from "./ripple";
import { SliderSuperTab } from "./slider";
import RadioForm from "./radioForm";
import { i18n, LangPackKey } from "../lib/langPack";
import replaceContent from "../helpers/dom/replaceContent";
2021-01-06 13:16:53 +04:00
export default class Row {
public container: HTMLElement;
public title: HTMLDivElement;
public subtitle: HTMLElement;
public checkboxField: CheckboxField;
public radioField: RadioField;
2021-01-06 13:16:53 +04:00
2021-02-18 21:30:41 +04:00
public freezed = false;
2021-01-06 13:16:53 +04:00
constructor(options: Partial<{
icon: string,
subtitle: string,
2021-03-22 22:38:03 +04:00
subtitleLangKey: LangPackKey
2021-01-06 13:16:53 +04:00
radioField: Row['radioField'],
checkboxField: Row['checkboxField'],
noCheckboxSubtitle: boolean,
2021-01-06 13:16:53 +04:00
title: string,
2021-03-22 22:38:03 +04:00
titleLangKey: LangPackKey,
titleRight: string | HTMLElement,
2021-02-19 19:27:56 +04:00
clickable: boolean | ((e: Event) => void),
2021-01-06 13:16:53 +04:00
navigationTab: SliderSuperTab
}> = {}) {
2021-03-19 18:27:02 +04:00
this.container = document.createElement(options.radioField || options.checkboxField ? 'label' : 'div');
2021-01-06 13:16:53 +04:00
this.container.classList.add('row');
this.subtitle = document.createElement('div');
this.subtitle.classList.add('row-subtitle');
2021-05-05 18:05:21 +04:00
this.subtitle.setAttribute('dir', 'auto');
2021-01-06 13:16:53 +04:00
if(options.subtitle) {
this.subtitle.innerHTML = options.subtitle;
2021-03-22 22:38:03 +04:00
} else if(options.subtitleLangKey) {
this.subtitle.append(i18n(options.subtitleLangKey));
2021-01-06 13:16:53 +04:00
}
this.container.append(this.subtitle);
2021-01-06 13:16:53 +04:00
let havePadding = false;
if(options.radioField || options.checkboxField) {
havePadding = true;
if(options.radioField) {
this.radioField = options.radioField;
this.container.append(this.radioField.label);
}
if(options.checkboxField) {
this.checkboxField = options.checkboxField;
const isToggle = options.checkboxField.label.classList.contains('checkbox-field-toggle');
if(isToggle) {
this.container.classList.add('row-with-toggle');
options.titleRight = this.checkboxField.label;
} else {
this.container.append(this.checkboxField.label);
}
if(!options.noCheckboxSubtitle && !isToggle) {
this.checkboxField.input.addEventListener('change', () => {
replaceContent(this.subtitle, i18n(this.checkboxField.input.checked ? 'Checkbox.Enabled' : 'Checkbox.Disabled'));
});
}
}
const i = options.radioField || options.checkboxField;
i.label.classList.add('disable-hover');
}
if(options.title || options.titleLangKey) {
let c: HTMLElement;
if(options.titleRight) {
c = document.createElement('div');
c.classList.add('row-title-row');
this.container.append(c);
} else {
c = this.container;
}
2021-03-01 21:42:34 +04:00
this.title = document.createElement('div');
this.title.classList.add('row-title');
2021-05-05 18:05:21 +04:00
this.title.setAttribute('dir', 'auto');
if(options.title) {
this.title.innerHTML = options.title;
} else {
this.title.append(i18n(options.titleLangKey));
}
c.append(this.title);
2021-03-01 21:42:34 +04:00
if(options.titleRight) {
const titleRight = document.createElement('div');
titleRight.classList.add('row-title', 'row-title-right');
if(typeof(options.titleRight) === 'string') {
2021-03-01 21:42:34 +04:00
titleRight.innerHTML = options.titleRight;
} else {
titleRight.append(options.titleRight);
2021-03-01 21:42:34 +04:00
}
2021-01-06 13:16:53 +04:00
c.append(titleRight);
2021-01-06 13:16:53 +04:00
}
}
if(options.icon) {
havePadding = true;
this.title.classList.add('tgico', 'tgico-' + options.icon);
this.container.classList.add('row-with-icon');
}
2021-01-06 13:16:53 +04:00
if(havePadding) {
this.container.classList.add('row-with-padding');
}
if(options.navigationTab) {
2021-02-19 19:27:56 +04:00
options.clickable = () => options.navigationTab.open();
2021-01-06 13:16:53 +04:00
}
if(options.clickable || options.radioField || options.checkboxField) {
2021-02-19 19:27:56 +04:00
if(typeof(options.clickable) === 'function') {
this.container.addEventListener('click', (e) => {
if(this.freezed) return;
(options.clickable as any)(e);
});
}
2021-01-06 13:16:53 +04:00
this.container.classList.add('row-clickable', 'hover-effect');
ripple(this.container, undefined, undefined, true);
/* if(options.radioField || options.checkboxField) {
this.container.prepend(this.container.lastElementChild);
} */
2021-01-06 13:16:53 +04:00
}
}
}
export const RadioFormFromRows = (rows: Row[], onChange: (value: string) => void) => {
return RadioForm(rows.map(r => ({container: r.container, input: r.radioField.input})), onChange);
2021-03-22 22:38:03 +04:00
};