@deepkit/desktop-ui
Version:
Library for desktop UI widgets in Angular 10+
157 lines (133 loc) • 4.82 kB
text/typescript
/*
* Deepkit Framework
* Copyright (C) 2021 Deepkit UG, Marc J. Schmidt
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the MIT License.
*
* You should have received a copy of the MIT License along with this program.
*/
import {
AfterViewInit,
ChangeDetectorRef,
Component,
ElementRef,
EventEmitter,
Input,
OnChanges,
Output,
SimpleChanges,
TemplateRef,
ViewChild,
} from '@angular/core';
import { Subject } from 'rxjs';
import { WindowState } from './window-state';
import { triggerResize } from '../../core/utils';
interface WinSidebar {
template: TemplateRef<any>;
}
export class WindowContentComponent implements OnChanges, AfterViewInit {
transparent: boolean | '' = false;
sidebarVisible: boolean = true;
class: string = '';
sidebarWidth = 250;
sidebarMaxWidth = 550;
sidebarMinWidth = 100;
sidebarWidthChange = new EventEmitter<number>();
toolbar?: WinSidebar;
public sidebar?: ElementRef<HTMLElement>;
public sidebarContainer?: ElementRef<HTMLElement>;
public content?: ElementRef<HTMLElement>;
withAnimation: boolean = false;
public readonly sidebarVisibleChanged = new Subject();
constructor(
private windowState: WindowState,
public cd: ChangeDetectorRef,
) {
}
getSidebarWidth(): number {
return Math.min(this.sidebarMaxWidth, Math.max(this.sidebarMinWidth, this.sidebarWidth));
}
transitionEnded() {
if (this.withAnimation) {
this.withAnimation = false;
triggerResize();
this.cd.detectChanges();
}
}
unregisterSidebar(sidebar: WinSidebar) {
if (this.toolbar === sidebar) {
this.toolbar = undefined;
setTimeout(() => this.sidebarMoved(), 0);
}
}
registerSidebar(sidebar: WinSidebar) {
this.toolbar = sidebar;
setTimeout(() => this.sidebarMoved(), 0);
}
sidebarMoved() {
if (this.windowState.buttonGroupAlignedToSidebar) {
this.windowState.buttonGroupAlignedToSidebar.sidebarMoved();
}
this.sidebarWidthChange.next(this.sidebarWidth);
triggerResize();
this.cd.detectChanges();
}
ngOnChanges(changes: SimpleChanges): void {
if (this.sidebar && this.sidebarContainer) {
if (changes.sidebarVisible) {
this.handleSidebarVisibility(true);
this.sidebarVisibleChanged.next(this.sidebarVisible);
}
}
}
ngAfterViewInit(): void {
this.handleSidebarVisibility();
}
protected handleSidebarVisibility(withAnimation = false) {
if (withAnimation && this.windowState.buttonGroupAlignedToSidebar) {
this.withAnimation = true;
this.windowState.buttonGroupAlignedToSidebar.activateOneTimeAnimation();
this.windowState.buttonGroupAlignedToSidebar.sidebarMoved();
}
// if (this.content) {
// if (this.sidebarVisible) {
// this.content.nativeElement.style.marginLeft = '0px';
// } else {
// this.content.nativeElement.style.marginLeft = (-this.sidebarWidth) + 'px';
// }
// }
}
public isSidebarVisible(): boolean {
return undefined !== this.sidebar && this.sidebarVisible;
}
}