bstlboard-front/src/app/ui/main/main.ui.ts

196 lines
5.0 KiB
TypeScript

import { Component, HostListener } from '@angular/core';
import { Router } from '@angular/router';
import { DomSanitizer } from '@angular/platform-browser';
import { MatIconRegistry } from '@angular/material/icon';
import { DateAdapter } from '@angular/material/core';
import { fromEvent } from 'rxjs';
import { AuthService } from '../../services/auth.service';
import { UserService } from '../../services/user.service';
import { I18nService } from '../../services/i18n.service';
import { SettingsService } from '../../services/settings.service';
import packageJson from '../../../../package.json';
@Component({
selector: 'ui-main',
templateUrl: './main.ui.html',
styleUrls: ['./main.ui.scss']
})
export class UiMain {
opened: boolean = true;
darkTheme: boolean = false;
title = 'bstlboard';
currentLocale: String;
datetimeformat: String;
locales;
authenticated: boolean = false;
moderator: boolean = false;
touchThresh: number = 150;
touchStartX: number;
touchX: number;
version = packageJson.version;
constructor(
private i18n: I18nService,
private authService: AuthService,
private userService: UserService,
private settingsService: SettingsService,
private router: Router,
private iconRegistry: MatIconRegistry,
private sanitizer: DomSanitizer,
private _adapter: DateAdapter<any>) {
iconRegistry.addSvgIcon('logo', sanitizer.bypassSecurityTrustResourceUrl('assets/icons/logo.svg'));
}
async ngOnInit() {
this.datetimeformat = this.i18n.get('format.datetime', []);
this.currentLocale = this.i18n.getLocale();
this.locales = this.i18n.getLocales();
this.authService.auth.subscribe(auth => {
this.authenticated = true;
for (let role of auth.authorities) {
if (role.authority == 'ROLE_ADMIN' || role.authority == 'ROLE_MOD') {
this.moderator = true;
}
}
}, (error) => {
this.authenticated = false;
})
this._adapter.setLocale(this.currentLocale);
const width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
if (width < 768) {
this.opened = false;
} else {
this.opened = true;
}
if (localStorage.getItem("bstlboard.darkTheme") == "true") {
this.darkTheme = true;
window.document.body.classList.add("dark-theme");
}
await this.settingsService.getSettings();
this.touchEvents();
}
setLocale(locale) {
localStorage.setItem("bstlboard.locale", locale);
if (this.authenticated) {
this.userService.get().subscribe((user: any) => {
user.locale = locale;
this.userService.update(user).subscribe(() => {
window.location.reload();
})
});
} else {
window.location.reload();
}
}
close() {
this.opened = false;
}
preventClose(event) {
event.preventDefault();
event.stopPropagation();
}
search(text, event) {
if (text) {
this.router.navigate(["search"], { queryParams: { q: text } });
}
event.target.value = '';
if (!this.isBiggerScreen()) {
this.close();
}
}
toggleDarkTheme() {
this.darkTheme = !this.darkTheme;
localStorage.setItem("bstlboard.darkTheme", this.darkTheme ? "true" : "false");
if (this.authenticated) {
this.userService.get().subscribe((user: any) => {
user.darkTheme = this.darkTheme;
this.userService.update(user).subscribe(() => {
window.location.reload();
})
});
} else {
window.location.reload();
}
}
logout() {
localStorage.removeItem("bstlboard.autologin");
this.authService.logout().subscribe(data => {
this.router.navigate([""]).then(() => {
window.location.reload();
});
})
}
isBiggerScreen() {
const width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
if (width < 768) {
return false;
} else {
return true;
}
}
openExternal(event, url, target = '_self') {
window.open(url, target);
this.preventClose(event);
}
@HostListener('window:resize', ['$event'])
onResize(event) {
if (event.target.innerWidth < 768) {
this.opened = false;
} else {
this.opened = true;
}
}
touchEvents(): void {
fromEvent(document, 'touchstart').subscribe((event: TouchEvent) => {
if (event.touches[0]) {
this.touchStartX = event.touches[0].screenX;
}
})
fromEvent(document, 'touchmove').subscribe((event: TouchEvent) => {
if (event.touches[0]) {
this.touchX = event.touches[0].screenX;
}
})
fromEvent(document, 'touchend').subscribe((event: TouchEvent) => {
if (this.touchX != 0) {
const touchDiff = this.touchStartX - this.touchX;
this.touchStartX = 0;
this.touchX = 0;
if (touchDiff < 0 && touchDiff < (this.touchThresh * -1) && !this.opened) {
this.opened = true;
} else if (touchDiff > 0 && touchDiff > this.touchThresh && this.opened) {
this.opened = false;
}
}
})
}
}