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.

82 lines
2.1 KiB

3 years ago
import { MOUNT_CLASS_TO } from "../config/debug";
3 years ago
import { isSafari, isAppleMobile } from "../helpers/userAgent";
import { cancelEvent } from "../helpers/dom";
3 years ago
export type NavigationItem = {
type: 'left' | 'right' | 'im' | 'chat' | 'popup' | 'media' | 'menu' | 'esg',
3 years ago
onPop: (canAnimate: boolean) => boolean | void,
onEscape?: () => boolean,
noHistory?: boolean,
3 years ago
};
export class AppNavigationController {
private navigations: Array<NavigationItem> = [];
private id = Date.now();
constructor() {
window.addEventListener('popstate', (e) => {
console.log('popstate', e);
const id: number = e.state;
if(id !== this.id) {
this.pushState();
return;
}
const item = this.navigations.pop();
if(!item) {
this.pushState();
return;
}
3 years ago
const good = item.onPop(isSafari && isAppleMobile ? false : undefined);
3 years ago
console.log('[NC]: popstate, navigation:', item, this.navigations);
if(good === false) {
this.pushItem(item);
}
//this.pushState(); // * prevent adding forward arrow
});
3 years ago
window.addEventListener('keydown', (e) => {
const item = this.navigations[this.navigations.length - 1];
if(!item) return;
if(e.key === 'Escape' && (item.onEscape ? item.onEscape() : true)) {
cancelEvent(e);
this.back();
}
}, {capture: true});
3 years ago
this.pushState(); // * push init state
}
public back() {
history.back();
}
public pushItem(item: NavigationItem) {
this.navigations.push(item);
console.log('[NC]: pushstate', item, this.navigations);
3 years ago
if(!item.noHistory) {
this.pushState();
}
3 years ago
}
private pushState() {
history.pushState(this.id, '');
}
public replaceState() {
history.replaceState(this.id, '');
}
3 years ago
public removeItem(item: NavigationItem) {
this.navigations.findAndSplice(i => i === item);
}
3 years ago
}
const appNavigationController = new AppNavigationController();
MOUNT_CLASS_TO && (MOUNT_CLASS_TO.appNavigationController = appNavigationController);
export default appNavigationController;