2020-08-24 17:09:31 +03:00
|
|
|
import { horizontalMenu } from "./horizontalMenu";
|
2020-06-13 11:19:39 +03:00
|
|
|
|
|
|
|
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-11-25 21:36:18 +02:00
|
|
|
export class SuperSliderTab implements SliderTab {
|
|
|
|
public id: number;
|
|
|
|
public closeBtn: HTMLElement;
|
|
|
|
|
|
|
|
// fix incompability
|
|
|
|
public onOpen: SliderTab['onOpen'];
|
|
|
|
|
|
|
|
constructor(protected slider: SidebarSlider, public container: HTMLElement) {
|
|
|
|
this.closeBtn = this.container.querySelector('.sidebar-close-button');
|
|
|
|
this.id = this.slider.addTab(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public close() {
|
|
|
|
return this.slider.closeTab(this.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public open() {
|
|
|
|
return this.slider.selectTab(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 02:37:22 +03:00
|
|
|
const TRANSITION_TIME = 250;
|
2020-06-19 14:49:55 +03:00
|
|
|
|
2020-06-13 11:19:39 +03:00
|
|
|
export default class SidebarSlider {
|
|
|
|
protected _selectTab: (id: number) => void;
|
|
|
|
public historyTabIDs: number[] = [];
|
|
|
|
|
2020-11-25 21:36:18 +02:00
|
|
|
constructor(public sidebarEl: HTMLElement, public tabs: {[id: number]: SliderTab} = {}, private canHideFirst = false) {
|
2020-09-24 02:37:22 +03:00
|
|
|
this._selectTab = horizontalMenu(null, this.sidebarEl.querySelector('.sidebar-slider') as HTMLDivElement, null, null, TRANSITION_TIME);
|
|
|
|
if(!canHideFirst) {
|
|
|
|
this._selectTab(0);
|
|
|
|
}
|
2020-06-13 11:19:39 +03:00
|
|
|
|
|
|
|
Array.from(this.sidebarEl.querySelectorAll('.sidebar-close-button') as any as HTMLElement[]).forEach(el => {
|
2020-11-25 21:36:18 +02:00
|
|
|
el.addEventListener('click', () => this.closeTab());
|
2020-06-13 11:19:39 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-25 21:36:18 +02:00
|
|
|
public closeTab = (tabID?: number) => {
|
|
|
|
if(tabID !== undefined && this.historyTabIDs[this.historyTabIDs.length - 1] != tabID) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//console.log('sidebar-close-button click:', this.historyTabIDs);
|
|
|
|
let closingID = this.historyTabIDs.pop(); // pop current
|
|
|
|
this.onCloseTab(closingID);
|
|
|
|
this._selectTab(this.historyTabIDs[this.historyTabIDs.length - 1] ?? (this.canHideFirst ? -1 : 0));
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
public selectTab(id: number | SuperSliderTab): boolean {
|
|
|
|
if(id instanceof SuperSliderTab) {
|
|
|
|
id = id.id;
|
|
|
|
}
|
|
|
|
|
2020-06-16 23:48:08 +03:00
|
|
|
if(this.historyTabIDs[this.historyTabIDs.length - 1] == id) {
|
2020-09-24 02:37:22 +03:00
|
|
|
return false;
|
2020-06-16 23:48:08 +03:00
|
|
|
}
|
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();
|
2020-09-24 02:37:22 +03:00
|
|
|
}, TRANSITION_TIME);
|
2020-06-19 14:49:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-13 11:19:39 +03:00
|
|
|
this.historyTabIDs.push(id);
|
|
|
|
this._selectTab(id);
|
2020-09-24 02:37:22 +03:00
|
|
|
return true;
|
2020-06-13 11:19:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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-09-24 02:37:22 +03:00
|
|
|
}, TRANSITION_TIME);
|
2020-06-13 11:19:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 21:36:18 +02:00
|
|
|
|
|
|
|
public addTab(tab: SuperSliderTab) {
|
|
|
|
let id: number;
|
|
|
|
if(tab.container.parentElement) {
|
|
|
|
id = Array.from(this.sidebarEl.children).findIndex(el => el == tab.container);
|
|
|
|
} else {
|
|
|
|
id = this.sidebarEl.childElementCount;
|
|
|
|
this.sidebarEl.append(tab.container);
|
|
|
|
|
|
|
|
if(tab.closeBtn) {
|
|
|
|
tab.closeBtn.addEventListener('click', () => this.closeTab());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.tabs[id] = tab;
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
2020-06-13 11:19:39 +03:00
|
|
|
}
|