tweb-i2p/src/components/slider.ts

145 lines
4.3 KiB
TypeScript
Raw Normal View History

import { attachClickEvent } from "../helpers/dom";
import { horizontalMenu } from "./horizontalMenu";
import { TransitionSlider } from "./transition";
2021-02-16 19:36:26 +04:00
import appNavigationController, { NavigationItem } from "./appNavigationController";
import SliderSuperTab, { SliderSuperTabConstructable, SliderTab } from "./sliderTab";
const TRANSITION_TIME = 250;
2020-06-19 14:49:55 +03:00
2021-02-20 21:10:26 +04:00
export type {SliderTab};
export {SliderSuperTab};
export default class SidebarSlider {
2021-01-06 13:16:53 +04:00
protected _selectTab: ReturnType<typeof horizontalMenu>;
2021-02-20 21:10:26 +04:00
public historyTabIds: (number | SliderSuperTab)[] = []; // * key is any, since right sidebar is ugly nowz
2021-01-01 18:24:49 +04:00
public tabsContainer: HTMLElement;
2021-02-16 19:36:26 +04:00
public sidebarEl: HTMLElement;
2021-02-20 21:10:26 +04:00
public tabs: Map<any, SliderTab>; // * key is any, since right sidebar is ugly now
2021-02-16 19:36:26 +04:00
private canHideFirst = false;
private navigationType: NavigationItem['type']
constructor(options: {
sidebarEl: SidebarSlider['sidebarEl'],
tabs?: SidebarSlider['tabs'],
canHideFirst?: SidebarSlider['canHideFirst'],
navigationType: SidebarSlider['navigationType']
}) {
for(const i in options) {
// @ts-ignore
this[i] = options[i];
}
2021-02-20 21:10:26 +04:00
if(!this.tabs) {
this.tabs = new Map();
}
2021-01-01 18:24:49 +04:00
this.tabsContainer = this.sidebarEl.querySelector('.sidebar-slider');
this._selectTab = TransitionSlider(this.tabsContainer, 'navigation', TRANSITION_TIME);
2021-02-16 19:36:26 +04:00
if(!this.canHideFirst) {
this._selectTab(0);
}
Array.from(this.sidebarEl.querySelectorAll('.sidebar-close-button') as any as HTMLElement[]).forEach(el => {
2021-02-16 19:36:26 +04:00
attachClickEvent(el, this.onCloseBtnClick);
});
}
2021-02-16 19:36:26 +04:00
private onCloseBtnClick = () => {
appNavigationController.back(this.navigationType);
2021-02-16 19:36:26 +04:00
// this.closeTab();
};
2021-02-20 21:10:26 +04:00
public closeTab = (id?: number | SliderSuperTab, animate?: boolean) => {
if(id !== undefined && this.historyTabIds[this.historyTabIds.length - 1] !== id) {
return false;
}
//console.log('sidebar-close-button click:', this.historyTabIDs);
2021-02-16 19:36:26 +04:00
const closingId = this.historyTabIds.pop(); // pop current
this.onCloseTab(closingId, animate);
2021-02-20 21:10:26 +04:00
const tab = this.historyTabIds[this.historyTabIds.length - 1];
this._selectTab(tab !== undefined ? (tab instanceof SliderSuperTab ? tab.container : tab) : (this.canHideFirst ? -1 : 0), animate);
return true;
};
2021-01-01 18:24:49 +04:00
public selectTab(id: number | SliderSuperTab): boolean {
2021-02-20 21:10:26 +04:00
/* if(id instanceof SliderSuperTab) {
id = id.id;
2021-02-20 21:10:26 +04:00
} */
if(this.historyTabIds[this.historyTabIds.length - 1] === id) {
return false;
2020-06-16 23:48:08 +03:00
}
2020-06-19 14:49:55 +03:00
2021-02-20 21:10:26 +04:00
const tab: SliderTab = id instanceof SliderSuperTab ? id : this.tabs.get(id);
2020-06-19 14:49:55 +03:00
if(tab) {
if(tab.onOpen) {
tab.onOpen();
}
if(tab.onOpenAfterTimeout) {
setTimeout(() => {
tab.onOpenAfterTimeout();
}, TRANSITION_TIME);
2020-06-19 14:49:55 +03:00
}
}
2021-02-16 19:36:26 +04:00
//if(!this.canHideFirst || this.historyTabIds.length) {
appNavigationController.pushItem({
type: this.navigationType,
onPop: (canAnimate) => {
this.closeTab(undefined, canAnimate);
return true;
}
});
//}
2020-06-19 14:49:55 +03:00
this.historyTabIds.push(id);
2021-02-20 21:10:26 +04:00
this._selectTab(id instanceof SliderSuperTab ? id.container : id);
return true;
}
2021-02-20 21:10:26 +04:00
public removeTabFromHistory(id: number | SliderSuperTab) {
this.historyTabIds.findAndSplice(i => i === id);
2021-02-16 19:36:26 +04:00
this.onCloseTab(id, undefined);
}
public sliceTabsUntilTab(tabConstructor: SliderSuperTabConstructable, preserveTab: SliderSuperTab) {
for(let i = this.historyTabIds.length - 1; i >= 0; --i) {
const tab = this.historyTabIds[i];
if(tab === preserveTab) continue;
else if(tab instanceof tabConstructor) {
break;
}
this.removeTabFromHistory(tab);
}
}
2021-02-20 21:10:26 +04:00
public onCloseTab(id: number | SliderSuperTab, animate: boolean) {
const tab: SliderTab = id instanceof SliderSuperTab ? id : this.tabs.get(id);
if(tab) {
2020-06-19 14:49:55 +03:00
if(tab.onClose) {
tab.onClose();
}
2020-06-19 14:49:55 +03:00
if(tab.onCloseAfterTimeout) {
setTimeout(() => {
tab.onCloseAfterTimeout();
}, TRANSITION_TIME);
}
}
}
2021-01-01 18:24:49 +04:00
public addTab(tab: SliderSuperTab) {
2021-02-20 21:10:26 +04:00
if(!tab.container.parentElement) {
2021-01-01 18:24:49 +04:00
this.tabsContainer.append(tab.container);
if(tab.closeBtn) {
2021-02-16 19:36:26 +04:00
tab.closeBtn.addEventListener('click', this.onCloseBtnClick);
}
}
}
2021-02-16 19:36:26 +04:00
}