Skip to content

Commit

Permalink
Refactor config
Browse files Browse the repository at this point in the history
  • Loading branch information
cetincakiroglu committed Dec 25, 2023
1 parent 82034fe commit 7d5cad4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 40 deletions.
50 changes: 29 additions & 21 deletions src/app/layout/config/app.config.component.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import { Component, Input } from '@angular/core';
import { LayoutService } from "../service/app.layout.service";
import { MenuService } from "../app.menu.service";
import { LayoutService } from '../service/app.layout.service';
import { MenuService } from '../app.menu.service';

@Component({
selector: 'app-config',
templateUrl: './app.config.component.html'
templateUrl: './app.config.component.html',
})
export class AppConfigComponent {

@Input() minimal: boolean = false;

scales: number[] = [12, 13, 14, 15, 16];

constructor(public layoutService: LayoutService, public menuService: MenuService) { }
constructor(
public layoutService: LayoutService,
public menuService: MenuService
) {}

get visible(): boolean {
return this.layoutService.state.configSidebarVisible;
}

set visible(_val: boolean) {
this.layoutService.state.configSidebarVisible = _val;
}

get scale(): number {
return this.layoutService.config().scale;
}

set scale(_val: number) {
this.layoutService.config.update((config) => ({
...config,
Expand All @@ -36,7 +36,6 @@ export class AppConfigComponent {
get menuMode(): string {
return this.layoutService.config().menuMode;
}

set menuMode(_val: string) {
this.layoutService.config.update((config) => ({
...config,
Expand All @@ -47,45 +46,54 @@ export class AppConfigComponent {
get inputStyle(): string {
return this.layoutService.config().inputStyle;
}

set inputStyle(_val: string) {
this.layoutService.config().inputStyle = _val;
}

get ripple(): boolean {
return this.layoutService.config().ripple;
}

set ripple(_val: boolean) {
this.layoutService.config.update((config) => ({
...config,
ripple: _val,
}));
}

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() {
this.layoutService.showConfigSidebar();
}

changeTheme(theme: string, colorScheme: string) {
this.layoutService.config.update((config) => ({ ...config, theme:theme,colorScheme:colorScheme }));
this.theme = theme;
this.colorScheme = colorScheme;
}


decrementScale() {
this.scale--;
this.applyScale();
}

incrementScale() {
this.scale++;
this.applyScale();
}

applyScale() {
this.layoutService.config.update((config) => ({
...config,
scale: this.scale,
}));
}
}
44 changes: 25 additions & 19 deletions src/app/layout/service/app.layout.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ interface LayoutState {
providedIn: 'root',
})
export class LayoutService {

_config: AppConfig = {
ripple: false,
inputStyle: 'outlined',
Expand All @@ -41,7 +40,7 @@ export class LayoutService {
profileSidebarVisible: false,
configSidebarVisible: false,
staticMenuMobileActive: false,
menuHoverActive: false
menuHoverActive: false,
};

private configUpdate = new Subject<AppConfig>();
Expand All @@ -52,6 +51,24 @@ export class LayoutService {

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() {
if (this.isOverlay()) {
this.state.overlayMenuActive = !this.state.overlayMenuActive;
Expand All @@ -61,10 +78,11 @@ export class LayoutService {
}

if (this.isDesktop()) {
this.state.staticMenuDesktopInactive = !this.state.staticMenuDesktopInactive;
}
else {
this.state.staticMenuMobileActive = !this.state.staticMenuMobileActive;
this.state.staticMenuDesktopInactive =
!this.state.staticMenuDesktopInactive;
} else {
this.state.staticMenuMobileActive =
!this.state.staticMenuMobileActive;

if (this.state.staticMenuMobileActive) {
this.overlayOpen.next(null);
Expand Down Expand Up @@ -100,20 +118,9 @@ export class LayoutService {
this.configUpdate.next(this.config());
}

constructor() {
effect(() => {
const config = this.config();
this.changeTheme();
this.changeScale(config.scale);
this.onConfigUpdate();
});
}

changeTheme() {
const config = this.config();
const themeLink = <HTMLLinkElement>(
document.getElementById('theme-css')
);
const themeLink = <HTMLLinkElement>document.getElementById('theme-css');
const themeLinkHref = themeLink.getAttribute('href')!;
const newHref = themeLinkHref
.split('/')
Expand Down Expand Up @@ -149,5 +156,4 @@ export class LayoutService {
changeScale(value: number) {
document.documentElement.style.fontSize = `${value}px`;
}

}

0 comments on commit 7d5cad4

Please sign in to comment.