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.
117 lines
2.5 KiB
117 lines
2.5 KiB
4 years ago
|
type TargetType = HTMLElement;
|
||
|
export type OnVisibilityChange = (target: TargetType, visible: boolean) => void;
|
||
|
|
||
|
export default class VisibilityIntersector {
|
||
|
private observer: IntersectionObserver;
|
||
|
private items: Map<TargetType, boolean> = new Map();
|
||
|
private locked = false;
|
||
|
|
||
|
constructor(onVisibilityChange: OnVisibilityChange) {
|
||
|
this.observer = new IntersectionObserver((entries) => {
|
||
|
if(this.locked) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
const changed: {target: TargetType, visible: boolean}[] = [];
|
||
|
|
||
|
entries.forEach(entry => {
|
||
|
const target = entry.target as TargetType;
|
||
|
|
||
|
if(this.items.get(target) == entry.isIntersecting) {
|
||
|
return;
|
||
|
} else {
|
||
|
this.items.set(target, entry.isIntersecting);
|
||
|
}
|
||
|
|
||
|
/* if(entry.isIntersecting) {
|
||
|
console.log('ooo', entry);
|
||
|
} */
|
||
|
|
||
|
/* if(this.locked) {
|
||
|
return;
|
||
|
} */
|
||
|
|
||
|
changed[entry.isIntersecting ? 'unshift' : 'push']({target, visible: entry.isIntersecting});
|
||
|
|
||
|
//onVisibilityChange(target, entry.isIntersecting);
|
||
|
});
|
||
|
|
||
|
changed.forEach(smth => {
|
||
|
onVisibilityChange(smth.target, smth.visible);
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
|
||
|
public getVisible() {
|
||
|
const items: TargetType[] = [];
|
||
|
this.items.forEach((value, key) => {
|
||
|
if(value) {
|
||
|
items.push(key);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
return items;
|
||
|
}
|
||
|
|
||
|
public clearVisible() {
|
||
|
const visible = this.getVisible();
|
||
|
for(const target of visible) {
|
||
|
this.items.set(target, false);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public isVisible(target: TargetType) {
|
||
|
return this.items.get(target);
|
||
|
}
|
||
|
|
||
|
public disconnect() {
|
||
|
this.observer.disconnect();
|
||
|
this.items.clear();
|
||
|
}
|
||
|
|
||
|
public refresh() {
|
||
|
this.observer.disconnect();
|
||
|
|
||
|
//window.requestAnimationFrame(() => {
|
||
|
const targets = [...this.items.keys()];
|
||
|
for(const target of targets) {
|
||
|
//this.items.set(target, false);
|
||
|
this.observer.observe(target);
|
||
|
}
|
||
|
//});
|
||
|
}
|
||
|
|
||
|
public refreshVisible() {
|
||
|
const visible = this.getVisible();
|
||
|
for(const target of visible) {
|
||
|
this.observer.unobserve(target);
|
||
|
}
|
||
|
|
||
|
for(const target of visible) {
|
||
|
this.observer.observe(target);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public observe(target: TargetType) {
|
||
|
this.items.set(target, false);
|
||
|
this.observer.observe(target);
|
||
|
}
|
||
|
|
||
|
public unobserve(target: TargetType) {
|
||
|
this.observer.unobserve(target);
|
||
|
this.items.delete(target);
|
||
|
}
|
||
|
|
||
|
public unlock() {
|
||
|
this.locked = false;
|
||
|
}
|
||
|
|
||
|
public unlockAndRefresh() {
|
||
|
this.unlock();
|
||
|
this.refresh();
|
||
|
}
|
||
|
|
||
|
public lock() {
|
||
|
this.locked = true;
|
||
|
}
|
||
|
}
|