2020-06-13 11:19:39 +03:00
|
|
|
import { horizontalMenu } from "./misc";
|
|
|
|
|
|
|
|
export interface SliderTab {
|
2020-06-19 14:49:55 +03:00
|
|
|
onOpen?: () => void,
|
|
|
|
onOpenAfterTimeout?: () => void,
|
2020-06-13 11:19:39 +03:00
|
|
|
onClose?: () => void,
|
|
|
|
onCloseAfterTimeout?: () => void
|
|
|
|
}
|
|
|
|
|
2020-06-19 14:49:55 +03:00
|
|
|
const TRANSITIONTIME = 420;
|
|
|
|
|
2020-06-13 11:19:39 +03:00
|
|
|
export default class SidebarSlider {
|
|
|
|
protected _selectTab: (id: number) => void;
|
|
|
|
public historyTabIDs: number[] = [];
|
|
|
|
|
|
|
|
constructor(public sidebarEl: HTMLElement, public tabs: {[id: number]: SliderTab}) {
|
2020-06-19 14:49:55 +03:00
|
|
|
this._selectTab = horizontalMenu(null, this.sidebarEl.querySelector('.sidebar-slider') as HTMLDivElement, null, null, TRANSITIONTIME);
|
2020-06-13 11:19:39 +03:00
|
|
|
this._selectTab(0);
|
|
|
|
|
|
|
|
let onCloseBtnClick = () => {
|
2020-06-16 23:48:08 +03:00
|
|
|
//console.log('sidebar-close-button click:', this.historyTabIDs);
|
2020-06-13 11:19:39 +03:00
|
|
|
let closingID = this.historyTabIDs.pop(); // pop current
|
|
|
|
this.onCloseTab(closingID);
|
|
|
|
this._selectTab(this.historyTabIDs[this.historyTabIDs.length - 1] || 0);
|
|
|
|
};
|
|
|
|
Array.from(this.sidebarEl.querySelectorAll('.sidebar-close-button') as any as HTMLElement[]).forEach(el => {
|
|
|
|
el.addEventListener('click', onCloseBtnClick);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public selectTab(id: number) {
|
2020-06-16 23:48:08 +03:00
|
|
|
if(this.historyTabIDs[this.historyTabIDs.length - 1] == id) {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-19 14:49:55 +03:00
|
|
|
|
|
|
|
const tab = this.tabs[id];
|
|
|
|
if(tab) {
|
|
|
|
if(tab.onOpen) {
|
|
|
|
tab.onOpen();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(tab.onOpenAfterTimeout) {
|
|
|
|
setTimeout(() => {
|
|
|
|
tab.onOpenAfterTimeout();
|
|
|
|
}, TRANSITIONTIME);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-16 23:48:08 +03:00
|
|
|
|
2020-06-13 11:19:39 +03:00
|
|
|
this.historyTabIDs.push(id);
|
|
|
|
this._selectTab(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public removeTabFromHistory(id: number) {
|
|
|
|
this.historyTabIDs.findAndSplice(i => i == id);
|
|
|
|
this.onCloseTab(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public onCloseTab(id: number) {
|
|
|
|
let tab = this.tabs[id];
|
|
|
|
if(tab) {
|
2020-06-19 14:49:55 +03:00
|
|
|
if(tab.onClose) {
|
2020-06-13 11:19:39 +03:00
|
|
|
tab.onClose();
|
|
|
|
}
|
|
|
|
|
2020-06-19 14:49:55 +03:00
|
|
|
if(tab.onCloseAfterTimeout) {
|
2020-06-13 11:19:39 +03:00
|
|
|
setTimeout(() => {
|
|
|
|
tab.onCloseAfterTimeout();
|
2020-06-19 14:49:55 +03:00
|
|
|
}, TRANSITIONTIME);
|
2020-06-13 11:19:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|