2024-05-02 21:37:11 +03:00
|
|
|
///
|
|
|
|
|
/// Copyright © 2016-2024 The Thingsboard Authors
|
|
|
|
|
///
|
|
|
|
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
/// you may not use this file except in compliance with the License.
|
|
|
|
|
/// You may obtain a copy of the License at
|
|
|
|
|
///
|
|
|
|
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
///
|
|
|
|
|
/// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
/// See the License for the specific language governing permissions and
|
|
|
|
|
/// limitations under the License.
|
|
|
|
|
///
|
|
|
|
|
|
2024-05-15 17:26:36 +03:00
|
|
|
import { ChangeDetectorRef, Component, ElementRef, Input, OnDestroy, OnInit, ViewChild } from '@angular/core';
|
2024-05-02 21:37:11 +03:00
|
|
|
import { PageComponent } from '@shared/components/page.component';
|
|
|
|
|
import { AppState } from '@core/core.state';
|
|
|
|
|
import { Store } from '@ngrx/store';
|
2024-05-15 17:26:36 +03:00
|
|
|
import { BadgePosition, BadgeStyle, badgeStyleURLMap, MobileAppSettings } from '@shared/models/mobile-app.models';
|
2024-05-02 21:37:11 +03:00
|
|
|
import { MobileAppService } from '@core/http/mobile-app.service';
|
|
|
|
|
import { WidgetContext } from '@home/models/widget-component.models';
|
|
|
|
|
import { UtilsService } from '@core/services/utils.service';
|
2024-05-15 17:26:36 +03:00
|
|
|
import { Subject } from 'rxjs';
|
2024-05-02 21:37:11 +03:00
|
|
|
import { MINUTE } from '@shared/models/time/time.models';
|
2024-05-07 17:14:31 +03:00
|
|
|
import { MobileAppQrCodeWidgetSettings } from '@home/components/widget/lib/cards/mobile-app-qr-code-widget.models';
|
2024-05-15 17:26:36 +03:00
|
|
|
import { isDefinedAndNotNull } from '@core/utils';
|
|
|
|
|
import { ResizeObserver } from '@juggle/resize-observer';
|
2024-05-02 21:37:11 +03:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-mobile-app-qrcode-widget',
|
|
|
|
|
templateUrl: './mobile-app-qrcode-widget.component.html',
|
|
|
|
|
styleUrls: ['./mobile-app-qrcode-widget.component.scss']
|
|
|
|
|
})
|
2024-05-15 17:26:36 +03:00
|
|
|
export class MobileAppQrcodeWidgetComponent extends PageComponent implements OnInit, OnDestroy {
|
2024-05-02 21:37:11 +03:00
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
ctx: WidgetContext;
|
|
|
|
|
|
|
|
|
|
@Input()
|
2024-05-15 17:26:36 +03:00
|
|
|
set mobileAppSettings(settings: MobileAppSettings | MobileAppQrCodeWidgetSettings) {
|
2024-05-02 21:37:11 +03:00
|
|
|
if (settings) {
|
|
|
|
|
this.mobileAppSettingsValue = settings;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-15 17:26:36 +03:00
|
|
|
get mobileAppSettings(): MobileAppSettings | MobileAppQrCodeWidgetSettings {
|
2024-05-02 21:37:11 +03:00
|
|
|
return this.mobileAppSettingsValue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-15 17:26:36 +03:00
|
|
|
@ViewChild('canvas', {static: true}) canvasRef: ElementRef<HTMLCanvasElement>;
|
2024-05-02 21:37:11 +03:00
|
|
|
|
|
|
|
|
private readonly destroy$ = new Subject<void>();
|
2024-05-15 17:26:36 +03:00
|
|
|
private widgetResize$: ResizeObserver;
|
2024-05-02 21:37:11 +03:00
|
|
|
|
|
|
|
|
badgeStyle = BadgeStyle;
|
|
|
|
|
badgePosition = BadgePosition;
|
|
|
|
|
badgeStyleURLMap = badgeStyleURLMap;
|
2024-05-15 17:26:36 +03:00
|
|
|
showBadgeContainer = true;
|
2024-05-02 21:37:11 +03:00
|
|
|
|
2024-05-15 17:26:36 +03:00
|
|
|
private mobileAppSettingsValue: MobileAppSettings | MobileAppQrCodeWidgetSettings;
|
2024-05-02 21:37:11 +03:00
|
|
|
private deepLinkTTL: number;
|
2024-05-15 17:26:36 +03:00
|
|
|
private deepLinkTTLTimeoutID: NodeJS.Timeout;
|
2024-05-02 21:37:11 +03:00
|
|
|
|
|
|
|
|
constructor(protected store: Store<AppState>,
|
|
|
|
|
protected cd: ChangeDetectorRef,
|
|
|
|
|
private mobileAppService: MobileAppService,
|
2024-05-15 17:26:36 +03:00
|
|
|
private utilsService: UtilsService,
|
|
|
|
|
private elementRef: ElementRef) {
|
2024-05-02 21:37:11 +03:00
|
|
|
super(store);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
2024-05-15 17:26:36 +03:00
|
|
|
if (!this.mobileAppSettings) {
|
|
|
|
|
if (isDefinedAndNotNull(this.ctx.settings.useSystemSettings) && !this.ctx.settings.useSystemSettings) {
|
2024-05-10 11:08:57 +03:00
|
|
|
this.mobileAppSettings = this.ctx.settings;
|
|
|
|
|
} else {
|
|
|
|
|
this.mobileAppService.getMobileAppSettings().subscribe((settings => {
|
|
|
|
|
this.mobileAppSettings = settings;
|
2024-05-15 17:26:36 +03:00
|
|
|
this.cd.markForCheck();
|
2024-05-10 11:08:57 +03:00
|
|
|
}));
|
|
|
|
|
}
|
2024-05-02 21:37:11 +03:00
|
|
|
}
|
2024-05-15 17:26:36 +03:00
|
|
|
this.initMobileAppQRCode();
|
|
|
|
|
this.widgetResize$ = new ResizeObserver(() => {
|
|
|
|
|
const showHideBadgeContainer = this.elementRef.nativeElement.offsetWidth > 250;
|
|
|
|
|
if (showHideBadgeContainer !== this.showBadgeContainer) {
|
|
|
|
|
this.showBadgeContainer = showHideBadgeContainer;
|
|
|
|
|
this.cd.markForCheck();
|
|
|
|
|
}
|
2024-05-02 21:37:11 +03:00
|
|
|
});
|
2024-05-15 17:26:36 +03:00
|
|
|
this.widgetResize$.observe(this.elementRef.nativeElement);
|
2024-05-02 21:37:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
2024-05-15 17:26:36 +03:00
|
|
|
if (this.widgetResize$) {
|
|
|
|
|
this.widgetResize$.disconnect();
|
|
|
|
|
}
|
2024-05-02 21:37:11 +03:00
|
|
|
super.ngOnDestroy();
|
|
|
|
|
this.destroy$.next();
|
|
|
|
|
this.destroy$.complete();
|
2024-05-15 17:26:36 +03:00
|
|
|
clearTimeout(this.deepLinkTTLTimeoutID);
|
2024-05-02 21:37:11 +03:00
|
|
|
}
|
|
|
|
|
|
2024-05-15 17:26:36 +03:00
|
|
|
private initMobileAppQRCode() {
|
|
|
|
|
if (this.deepLinkTTLTimeoutID) {
|
|
|
|
|
clearTimeout(this.deepLinkTTLTimeoutID);
|
|
|
|
|
this.deepLinkTTLTimeoutID = null;
|
|
|
|
|
}
|
|
|
|
|
this.mobileAppService.getMobileAppDeepLink().subscribe(link => {
|
|
|
|
|
this.deepLinkTTL = Number(this.utilsService.getQueryParam('ttl', link)) * MINUTE;
|
|
|
|
|
this.updateQRCode(link);
|
|
|
|
|
this.deepLinkTTLTimeoutID = setTimeout(() => this.initMobileAppQRCode(), this.deepLinkTTL);
|
|
|
|
|
});
|
2024-05-02 21:37:11 +03:00
|
|
|
}
|
|
|
|
|
|
2024-05-15 17:26:36 +03:00
|
|
|
private updateQRCode(link: string) {
|
2024-05-02 21:37:11 +03:00
|
|
|
import('qrcode').then((QRCode) => {
|
2024-05-07 17:14:31 +03:00
|
|
|
QRCode.toCanvas(this.canvasRef.nativeElement, link, { width: 100 });
|
2024-05-02 21:37:11 +03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|