Refactor config

This commit is contained in:
Çetin
2023-12-25 15:31:46 +03:00
parent 82034fef3a
commit 7d5cad440a
2 changed files with 54 additions and 40 deletions

View File

@@ -1,23 +1,24 @@
import { Component, Input } from '@angular/core'; import { Component, Input } from '@angular/core';
import { LayoutService } from "../service/app.layout.service"; import { LayoutService } from '../service/app.layout.service';
import { MenuService } from "../app.menu.service"; import { MenuService } from '../app.menu.service';
@Component({ @Component({
selector: 'app-config', selector: 'app-config',
templateUrl: './app.config.component.html' templateUrl: './app.config.component.html',
}) })
export class AppConfigComponent { export class AppConfigComponent {
@Input() minimal: boolean = false; @Input() minimal: boolean = false;
scales: number[] = [12, 13, 14, 15, 16]; scales: number[] = [12, 13, 14, 15, 16];
constructor(public layoutService: LayoutService, public menuService: MenuService) { } constructor(
public layoutService: LayoutService,
public menuService: MenuService
) {}
get visible(): boolean { get visible(): boolean {
return this.layoutService.state.configSidebarVisible; return this.layoutService.state.configSidebarVisible;
} }
set visible(_val: boolean) { set visible(_val: boolean) {
this.layoutService.state.configSidebarVisible = _val; this.layoutService.state.configSidebarVisible = _val;
} }
@@ -25,7 +26,6 @@ export class AppConfigComponent {
get scale(): number { get scale(): number {
return this.layoutService.config().scale; return this.layoutService.config().scale;
} }
set scale(_val: number) { set scale(_val: number) {
this.layoutService.config.update((config) => ({ this.layoutService.config.update((config) => ({
...config, ...config,
@@ -36,7 +36,6 @@ export class AppConfigComponent {
get menuMode(): string { get menuMode(): string {
return this.layoutService.config().menuMode; return this.layoutService.config().menuMode;
} }
set menuMode(_val: string) { set menuMode(_val: string) {
this.layoutService.config.update((config) => ({ this.layoutService.config.update((config) => ({
...config, ...config,
@@ -47,7 +46,6 @@ export class AppConfigComponent {
get inputStyle(): string { get inputStyle(): string {
return this.layoutService.config().inputStyle; return this.layoutService.config().inputStyle;
} }
set inputStyle(_val: string) { set inputStyle(_val: string) {
this.layoutService.config().inputStyle = _val; this.layoutService.config().inputStyle = _val;
} }
@@ -55,7 +53,6 @@ export class AppConfigComponent {
get ripple(): boolean { get ripple(): boolean {
return this.layoutService.config().ripple; return this.layoutService.config().ripple;
} }
set ripple(_val: boolean) { set ripple(_val: boolean) {
this.layoutService.config.update((config) => ({ this.layoutService.config.update((config) => ({
...config, ...config,
@@ -63,29 +60,40 @@ export class AppConfigComponent {
})); }));
} }
set theme(val: string) {
this.layoutService.config.update((config) => ({
...config,
theme: val,
}));
}
get theme(): string {
return this.layoutService.config().theme;
}
set colorScheme(val: string) {
this.layoutService.config.update((config) => ({
...config,
colorScheme: val,
}));
}
get colorScheme(): string {
return this.layoutService.config().colorScheme;
}
onConfigButtonClick() { onConfigButtonClick() {
this.layoutService.showConfigSidebar(); this.layoutService.showConfigSidebar();
} }
changeTheme(theme: string, colorScheme: string) { changeTheme(theme: string, colorScheme: string) {
this.layoutService.config.update((config) => ({ ...config, theme:theme,colorScheme:colorScheme })); this.theme = theme;
this.colorScheme = colorScheme;
} }
decrementScale() { decrementScale() {
this.scale--; this.scale--;
this.applyScale();
} }
incrementScale() { incrementScale() {
this.scale++; this.scale++;
this.applyScale();
}
applyScale() {
this.layoutService.config.update((config) => ({
...config,
scale: this.scale,
}));
} }
} }

View File

@@ -23,7 +23,6 @@ interface LayoutState {
providedIn: 'root', providedIn: 'root',
}) })
export class LayoutService { export class LayoutService {
_config: AppConfig = { _config: AppConfig = {
ripple: false, ripple: false,
inputStyle: 'outlined', inputStyle: 'outlined',
@@ -41,7 +40,7 @@ export class LayoutService {
profileSidebarVisible: false, profileSidebarVisible: false,
configSidebarVisible: false, configSidebarVisible: false,
staticMenuMobileActive: false, staticMenuMobileActive: false,
menuHoverActive: false menuHoverActive: false,
}; };
private configUpdate = new Subject<AppConfig>(); private configUpdate = new Subject<AppConfig>();
@@ -52,6 +51,24 @@ export class LayoutService {
overlayOpen$ = this.overlayOpen.asObservable(); overlayOpen$ = this.overlayOpen.asObservable();
constructor() {
effect(() => {
const config = this.config();
if (this.updateStyle(config)) {
this.changeTheme();
}
this.changeScale(config.scale);
this.onConfigUpdate();
});
}
updateStyle(config: AppConfig) {
return (
config.theme !== this._config.theme ||
config.colorScheme !== this._config.colorScheme
);
}
onMenuToggle() { onMenuToggle() {
if (this.isOverlay()) { if (this.isOverlay()) {
this.state.overlayMenuActive = !this.state.overlayMenuActive; this.state.overlayMenuActive = !this.state.overlayMenuActive;
@@ -61,10 +78,11 @@ export class LayoutService {
} }
if (this.isDesktop()) { if (this.isDesktop()) {
this.state.staticMenuDesktopInactive = !this.state.staticMenuDesktopInactive; this.state.staticMenuDesktopInactive =
} !this.state.staticMenuDesktopInactive;
else { } else {
this.state.staticMenuMobileActive = !this.state.staticMenuMobileActive; this.state.staticMenuMobileActive =
!this.state.staticMenuMobileActive;
if (this.state.staticMenuMobileActive) { if (this.state.staticMenuMobileActive) {
this.overlayOpen.next(null); this.overlayOpen.next(null);
@@ -100,20 +118,9 @@ export class LayoutService {
this.configUpdate.next(this.config()); this.configUpdate.next(this.config());
} }
constructor() {
effect(() => {
const config = this.config();
this.changeTheme();
this.changeScale(config.scale);
this.onConfigUpdate();
});
}
changeTheme() { changeTheme() {
const config = this.config(); const config = this.config();
const themeLink = <HTMLLinkElement>( const themeLink = <HTMLLinkElement>document.getElementById('theme-css');
document.getElementById('theme-css')
);
const themeLinkHref = themeLink.getAttribute('href')!; const themeLinkHref = themeLink.getAttribute('href')!;
const newHref = themeLinkHref const newHref = themeLinkHref
.split('/') .split('/')
@@ -149,5 +156,4 @@ export class LayoutService {
changeScale(value: number) { changeScale(value: number) {
document.documentElement.style.fontSize = `${value}px`; document.documentElement.style.fontSize = `${value}px`;
} }
} }