locale + darktheme support, fixes boolean profilefield

This commit is contained in:
_Bastler
2021-03-29 14:35:25 +02:00
parent 2cbfd628a2
commit f76843a2e9
11 changed files with 63 additions and 18 deletions
+1 -1
View File
@@ -15,7 +15,7 @@
</button>
<mat-menu #menu="matMenu">
<a *ngFor="let locale of locales" mat-menu-item (click)="setLocale(locale)">{{'locale.' + locale + '.long' |
i18n}}</a>
i18n}} <mat-icon inline=true *ngIf="locale == currentLocale">done</mat-icon></a>
</mat-menu>
</ng-container>
</mat-toolbar>
+5 -1
View File
@@ -5,7 +5,7 @@ import {I18nService} from './services/i18n.service';
import {Router} from '@angular/router';
import {DomSanitizer} from '@angular/platform-browser';
import {MatIconRegistry} from '@angular/material/icon';
import {DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE} from '@angular/material/core';
import {DateAdapter} from '@angular/material/core';
@Component({
selector: 'app-root',
@@ -40,6 +40,10 @@ export class AppComponent {
} else {
this.opened = true;
}
if(localStorage.getItem("bstly.darkTheme") == "true") {
window.document.body.classList.add("dark-theme");
}
}
setLocale(locale) {
+26 -2
View File
@@ -1,7 +1,10 @@
import {Injectable} from '@angular/core';
import {Router} from '@angular/router';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
import {AuthService} from '../services/auth.service';
import {ProfileService} from '../services/profile.service';
import {I18nService} from '../services/i18n.service';
@Injectable({
providedIn: 'root'
@@ -35,7 +38,7 @@ export class AuthGuard implements CanActivate {
providedIn: 'root'
})
export class AuthenticatedGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {}
constructor(private authService: AuthService, private profileService: ProfileService, private i18nService: I18nService, private router: Router) {}
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const that = this;
@@ -44,6 +47,27 @@ export class AuthenticatedGuard implements CanActivate {
this.router.navigateByUrl('/login');
return false;
}
this.profileService.getAll(["locale", "darkTheme"]).toPromise().then((profileFields: any) => {
let reload = false;
for(let profileField of profileFields) {
if(profileField.name == "darkTheme" && profileField.value != localStorage.getItem("bstly.darkTheme")) {
localStorage.setItem("bstly.darkTheme", profileField.value);
reload = true;
} else if(profileField.name == "locale" && this.i18nService.locales.indexOf(profileField.value) != -1 && localStorage.getItem("bstly.locale") != profileField.value) {
if(this.i18nService.locale != profileField.value) {
localStorage.setItem("bstly.locale", profileField.value);
reload = true;
}
}
}
if(reload) {
window.location.reload();
}
})
return true;
}).catch(function(error) {
return that.router.parseUrl('/unavailable');
+4 -2
View File
@@ -8,7 +8,7 @@ import {environment} from '../../environments/environment';
export class I18nService {
locale: string = "de-informal";
locales: any = ["de-informal"];
locales: any[] = ["de-informal"];
i18n: any;
constructor(private http: HttpClient) {
@@ -42,7 +42,9 @@ export class I18nService {
}
try {
this.locales = await this.http.get(environment.apiUrl + "/i18n").toPromise();
await this.http.get(environment.apiUrl + "/i18n").toPromise().then((response: any) => {
this.locales = response;
});
} catch(e) {
console.debug("fallback to default locales");
}
+2 -2
View File
@@ -11,8 +11,8 @@ export class ProfileService {
constructor(private http: HttpClient) {
}
getAll() {
return this.http.get(environment.apiUrl + "/profiles");
getAll(filter?: any[]) {
return this.http.get(environment.apiUrl + "/profiles" + (filter ? "?filter=" + filter.join(",") : ""));
}
getAllForUser(username) {
@@ -34,7 +34,8 @@
<input matInput type="email" [(ngModel)]="profileField.value" formControlName="value"
placeholder="{{'profileField.value' | i18n}}">
</mat-form-field>
<mat-slide-toggle *ngSwitchCase="'BOOL'" [(ngModel)]="profileField.value" formControlName="value">
<mat-slide-toggle *ngSwitchCase="'BOOL'" (change)="booleanChange(profileField)"
[checked]="profileField.value == 'true'">
{{'profileField.value' | i18n}}
</mat-slide-toggle>
<mat-form-field *ngSwitchCase="'NUMBER'">
@@ -17,7 +17,7 @@
<span *ngSwitchCase="'NUMBER'">{{profileField.value}}</span>
<button *ngSwitchCase="'BLOB'" mat-raised-button
(click)="openBlob(profileField)">{{'profileField.openBlob' | i18n}}</button>
<mat-slide-toggle *ngSwitchCase="'BOOL'" [(ngModel)]="profileField.value" disabled>
<mat-slide-toggle *ngSwitchCase="'BOOL'" [checked]="profileField.value == 'true'" disabled>
</mat-slide-toggle>
</div>
</td>
@@ -148,6 +148,16 @@ export class ProfileFieldDialog {
index: ['']
});
}
booleanChange(profileField) {
if(profileField.value == 'false') {
profileField.value = 'true';
} else {
profileField.value = 'false';
}
console.log(profileField);
}
}