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.

224 lines
6.7 KiB

import apiManager from "../lib/mtproto/apiManager";
import { whichChild, isElementInViewport, isInDOM, findUpTag } from "../lib/utils";
5 years ago
let onRippleClick = function(this: HTMLElement, e: MouseEvent) {
var $circle = this.firstElementChild as HTMLSpanElement;//this.querySelector('.c-ripple__circle') as HTMLSpanElement;
5 years ago
var rect = this.parentElement.getBoundingClientRect();
var x = e.clientX - rect.left; //x position within the element.
var y = e.clientY - rect.top;
5 years ago
/* var x = e.pageX - this.parentElement.offsetLeft;
var y = e.pageY - this.parentElement.offsetTop - this.parentElement.scrollHeight; */
5 years ago
$circle.style.top = y + 'px';
$circle.style.left = x + 'px';
5 years ago
this.classList.add('active');
5 years ago
//console.log('onrippleclick', e/* e.pageY, this.parentElement.offsetTop */);
};
export function ripple(elem: Element) {
/* elem.addEventListener('click', function(e) {
var $circle = elem.querySelector('.c-ripple__circle') as HTMLSpanElement;
5 years ago
var x = e.pageX - elem.offsetLeft;
var y = e.pageY - elem.offsetTop;
5 years ago
$circle.style.top = y + 'px';
$circle.style.left = x + 'px';
5 years ago
elem.classList.add('active');
}); */
5 years ago
let r = document.createElement('div');
r.classList.add('c-ripple');
5 years ago
let span = document.createElement('span');
span.classList.add('c-ripple__circle');
5 years ago
r.append(span);
elem.append(r);
5 years ago
r.addEventListener('click', onRippleClick);
5 years ago
let onEnd = () => {
r.classList.remove('active');
};
5 years ago
for(let type of ['animationend', 'webkitAnimationEnd', 'oanimationend', 'MSAnimationEnd']) {
r.addEventListener(type, onEnd);
}
}
export function putPreloader(elem: Element) {
const html = `
<svg xmlns="http://www.w3.org/2000/svg" class="preloader-circular" viewBox="25 25 50 50">
<circle class="preloader-path" cx="50" cy="50" r="20" fill="none" stroke-miterlimit="10"/>
</svg>`;
elem.innerHTML += html;
}
export function horizontalMenu(tabs: HTMLUListElement, content: HTMLDivElement, onClick?: (id: number, tabContent: HTMLDivElement) => void, onTransitionEnd?: () => void) {
let hideTimeout: number = 0;
let prevTabContent: HTMLDivElement = null;
let prevId = -1;
tabs.addEventListener('click', function(e) {
let target = e.target as HTMLLIElement;
5 years ago
if(target.tagName != 'LI') {
target = findUpTag(target, 'LI');
}
5 years ago
console.log('tabs click:', target);
if(!target || target.classList.contains('active')) return false;
5 years ago
let prev = tabs.querySelector('li.active') as HTMLLIElement;
prev && prev.classList.remove('active');
target.classList.add('active');
let id = whichChild(target);
if(id == prevId) return false;
let tabContent = content.children[id] as HTMLDivElement;
tabContent.classList.add('active');
console.log('mambo rap', prevId, id);
//content.style.marginLeft = id > 0 ? (-id * 100) + '%' : '';
let toRight = prevId < id;
if(prevId != -1) {
content.style.width = '200%';
console.log('mambo rap setting', toRight);
content.classList.remove('animated');
if(toRight) {
content.classList.add('animated');
content.style.marginLeft = '-100%';
} else {
content.style.marginLeft = '-100%';
setTimeout(() => {
content.classList.add('animated');
content.style.marginLeft = '';
}, 10);
5 years ago
}
}
prevId = id;
let p = prevTabContent;
clearTimeout(hideTimeout);
if(p) hideTimeout = setTimeout(() => {
if(toRight) {
p.classList.remove('active');
content.classList.remove('animated');
content.style.width = '100%';
}
/* content.style.marginLeft = '0%';
content.style.width = '100%'; */
if(!toRight) {
p.classList.remove('active');
content.classList.remove('animated');
content.style.width = '100%';
}
content.style.marginLeft = '';
if(onTransitionEnd) onTransitionEnd();
}, 200);
if(onClick) onClick(id, tabContent);
prevTabContent = tabContent;
});
}
export function getNearestDc() {
return apiManager.invokeApi('help.getNearestDc').then((nearestDcResult: any) => {
5 years ago
if(nearestDcResult.nearest_dc != nearestDcResult.this_dc) {
//MTProto.apiManager.baseDcID = nearestDcResult.nearest_dc;
apiManager.getNetworker(nearestDcResult.nearest_dc);
5 years ago
}
return nearestDcResult;
});
}
export function formatPhoneNumber(str: string) {
str = str.replace(/\D/g, '');
let phoneCode = str.slice(0, 6);
console.log('str', str, phoneCode);
let sortedCountries = Config.Countries.slice().sort((a, b) => b.phoneCode.length - a.phoneCode.length);
let country = sortedCountries.find((c) => {
return c.phoneCode.split(' and ').find((c) => phoneCode.indexOf(c.replace(/\D/g, '')) == 0);
});
let pattern = country ? country.pattern || country.phoneCode : '';
if(country) {
pattern.split('').forEach((symbol, idx) => {
if(symbol == ' ' && str[idx] != ' ' && str.length > idx) {
str = str.slice(0, idx) + ' ' + str.slice(idx);
}
});
if(country.pattern) {
str = str.slice(0, country.pattern.length);
}
}
return {formatted: str, country};
}
let onMouseMove = (e: MouseEvent) => {
let rect = openedMenu.getBoundingClientRect();
let {clientX, clientY} = e;
let diffX = clientX >= rect.right ? clientX - rect.right : rect.left - clientX;
let diffY = clientY >= rect.bottom ? clientY - rect.bottom : rect.top - clientY;
if(diffX >= 100 || diffY >= 100) {
openedMenu.classList.remove('active');
openedMenu.parentElement.classList.remove('menu-open');
//openedMenu.parentElement.click();
}
//console.log('mousemove', diffX, diffY);
};
let openedMenu: HTMLDivElement = null;
export function openBtnMenu(menuElement: HTMLDivElement) {
if(openedMenu) {
openedMenu.classList.remove('active');
openedMenu.parentElement.classList.remove('menu-open');
}
openedMenu = menuElement;
openedMenu.classList.add('active');
openedMenu.parentElement.classList.add('menu-open');
window.addEventListener('click', () => {
if(openedMenu) {
openedMenu.parentElement.classList.remove('menu-open');
openedMenu.classList.remove('active');
openedMenu = null;
}
window.removeEventListener('mousemove', onMouseMove);
}, {once: true});
window.addEventListener('mousemove', onMouseMove);
}