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

View File

@ -15,7 +15,7 @@
</button> </button>
<mat-menu #menu="matMenu"> <mat-menu #menu="matMenu">
<a *ngFor="let locale of locales" mat-menu-item (click)="setLocale(locale)">{{'locale.' + locale + '.long' | <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> </mat-menu>
</ng-container> </ng-container>
</mat-toolbar> </mat-toolbar>

View File

@ -5,7 +5,7 @@ import {I18nService} from './services/i18n.service';
import {Router} from '@angular/router'; import {Router} from '@angular/router';
import {DomSanitizer} from '@angular/platform-browser'; import {DomSanitizer} from '@angular/platform-browser';
import {MatIconRegistry} from '@angular/material/icon'; 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({ @Component({
selector: 'app-root', selector: 'app-root',
@ -40,6 +40,10 @@ export class AppComponent {
} else { } else {
this.opened = true; this.opened = true;
} }
if(localStorage.getItem("bstly.darkTheme") == "true") {
window.document.body.classList.add("dark-theme");
}
} }
setLocale(locale) { setLocale(locale) {

View File

@ -2,6 +2,9 @@ import {Injectable} from '@angular/core';
import {Router} from '@angular/router'; 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 {AuthService} from '../services/auth.service';
import {ProfileService} from '../services/profile.service';
import {I18nService} from '../services/i18n.service';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@ -35,7 +38,7 @@ export class AuthGuard implements CanActivate {
providedIn: 'root' providedIn: 'root'
}) })
export class AuthenticatedGuard implements CanActivate { 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) { canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const that = this; const that = this;
@ -44,6 +47,27 @@ export class AuthenticatedGuard implements CanActivate {
this.router.navigateByUrl('/login'); this.router.navigateByUrl('/login');
return false; 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; return true;
}).catch(function(error) { }).catch(function(error) {
return that.router.parseUrl('/unavailable'); return that.router.parseUrl('/unavailable');

View File

@ -8,7 +8,7 @@ import {environment} from '../../environments/environment';
export class I18nService { export class I18nService {
locale: string = "de-informal"; locale: string = "de-informal";
locales: any = ["de-informal"]; locales: any[] = ["de-informal"];
i18n: any; i18n: any;
constructor(private http: HttpClient) { constructor(private http: HttpClient) {
@ -42,7 +42,9 @@ export class I18nService {
} }
try { 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) { } catch(e) {
console.debug("fallback to default locales"); console.debug("fallback to default locales");
} }

View File

@ -11,8 +11,8 @@ export class ProfileService {
constructor(private http: HttpClient) { constructor(private http: HttpClient) {
} }
getAll() { getAll(filter?: any[]) {
return this.http.get(environment.apiUrl + "/profiles"); return this.http.get(environment.apiUrl + "/profiles" + (filter ? "?filter=" + filter.join(",") : ""));
} }
getAllForUser(username) { getAllForUser(username) {

View File

@ -34,7 +34,8 @@
<input matInput type="email" [(ngModel)]="profileField.value" formControlName="value" <input matInput type="email" [(ngModel)]="profileField.value" formControlName="value"
placeholder="{{'profileField.value' | i18n}}"> placeholder="{{'profileField.value' | i18n}}">
</mat-form-field> </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}} {{'profileField.value' | i18n}}
</mat-slide-toggle> </mat-slide-toggle>
<mat-form-field *ngSwitchCase="'NUMBER'"> <mat-form-field *ngSwitchCase="'NUMBER'">

View File

@ -17,7 +17,7 @@
<span *ngSwitchCase="'NUMBER'">{{profileField.value}}</span> <span *ngSwitchCase="'NUMBER'">{{profileField.value}}</span>
<button *ngSwitchCase="'BLOB'" mat-raised-button <button *ngSwitchCase="'BLOB'" mat-raised-button
(click)="openBlob(profileField)">{{'profileField.openBlob' | i18n}}</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> </mat-slide-toggle>
</div> </div>
</td> </td>

View File

@ -148,6 +148,16 @@ export class ProfileFieldDialog {
index: [''] index: ['']
}); });
} }
booleanChange(profileField) {
if(profileField.value == 'false') {
profileField.value = 'true';
} else {
profileField.value = 'false';
}
console.log(profileField);
}
} }

View File

@ -93,11 +93,13 @@
"edit": "Bearbeiten", "edit": "Bearbeiten",
"index": "Index", "index": "Index",
"name": { "name": {
".": "Name", ".": "Key",
"publicKey": "Öffentlicher PGP Schlüssel", "darkTheme" : "Dunkles Thema",
"email": "E-Mail Adresse", "email": "E-Mail Adresse",
"locale" : "Sprache",
"primaryEmail": "E-Mail Adresse primär verwenden", "primaryEmail": "E-Mail Adresse primär verwenden",
"prtyMap": "Partey Karte" "prtyMap": "Partey Karte",
"publicKey": "Öffentlicher PGP Schlüssel"
}, },
"openBlob": "Anzeigen", "openBlob": "Anzeigen",
"type": { "type": {
@ -177,7 +179,7 @@
}, },
"status": { "status": {
".": "Status", ".": "Status",
"change" : "Status ändern", "change" : "Status aktualisieren",
"hint": "Dein User Status gibt an, wie mit deinen Account Daten umgegangen wird wenn deine Berechtigungen ausgelaufen sind.", "hint": "Dein User Status gibt an, wie mit deinen Account Daten umgegangen wird wenn deine Berechtigungen ausgelaufen sind.",
"NORMAL": { "NORMAL": {
".": "Normal", ".": "Normal",

View File

@ -93,11 +93,13 @@
"edit": "Edit", "edit": "Edit",
"index": "Index", "index": "Index",
"name": { "name": {
".": "Name", ".": "Key",
"publicKey": "Public PGP key", "darkTheme" : "Dark Theme",
"email": "Email address", "email": "Email address",
"locale" : "Locale",
"primaryEmail": "Use email address primary", "primaryEmail": "Use email address primary",
"prtyMap": "Partey map" "prtyMap": "Partey map",
"publicKey": "Public PGP key"
}, },
"openBlob": "Display", "openBlob": "Display",
"type": { "type": {

View File

@ -21,7 +21,7 @@ $light-theme: mat-light-theme((color: (primary: $light-primary,
// Define an alternate dark theme. // Define an alternate dark theme.
$dark-theme: mat-dark-theme((color: (primary: $dark-primary, $dark-theme: mat-dark-theme((color: (primary: $dark-primary,
accent: $dark-accent, accent: $light-accent,
warn: $dark-warn, warn: $dark-warn,
))); )));