Commit 73c4f51e047b90858c687d2c6e9f34a3f06070ae
1 parent
abac3098
UI: Add lazy load qrcode library; added validation qr code text
Showing
3 changed files
with
20 additions
and
9 deletions
... | ... | @@ -16,6 +16,7 @@ |
16 | 16 | |
17 | 17 | --> |
18 | 18 | <div fxLayout="column" fxLayoutAlign="center center" style="width: 100%; height: 100%;"> |
19 | - <canvas fxFlex #canvas [ngStyle]="{display: qrCodeText ? 'block' : 'none'}"></canvas> | |
20 | - <div *ngIf="!qrCodeText" translate>entity.no-data</div> | |
19 | + <canvas fxFlex #canvas [ngStyle]="{display: qrCodeText && !invalidQrCodeText ? 'block' : 'none'}"></canvas> | |
20 | + <div *ngIf="!qrCodeText && !invalidQrCodeText" translate>entity.no-data</div> | |
21 | + <div *ngIf="invalidQrCodeText" translate>widgets.invalid-qr-code-text</div> | |
21 | 22 | </div> | ... | ... |
... | ... | @@ -19,7 +19,6 @@ import { PageComponent } from '@shared/components/page.component'; |
19 | 19 | import { WidgetContext } from '@home/models/widget-component.models'; |
20 | 20 | import { Store } from '@ngrx/store'; |
21 | 21 | import { AppState } from '@core/core.state'; |
22 | -import QRCode from 'qrcode'; | |
23 | 22 | import { |
24 | 23 | fillPattern, |
25 | 24 | parseData, |
... | ... | @@ -30,6 +29,7 @@ import { |
30 | 29 | import { FormattedData } from '@home/components/widget/lib/maps/map-models'; |
31 | 30 | import { DatasourceData } from '@shared/models/widget.models'; |
32 | 31 | import { DataKeyType } from '@shared/models/telemetry/telemetry.models'; |
32 | +import { isString } from '@core/utils'; | |
33 | 33 | |
34 | 34 | interface QrCodeWidgetSettings { |
35 | 35 | qrCodeTextPattern: string; |
... | ... | @@ -53,6 +53,7 @@ export class QrCodeWidgetComponent extends PageComponent implements OnInit, Afte |
53 | 53 | ctx: WidgetContext; |
54 | 54 | |
55 | 55 | qrCodeText: string; |
56 | + invalidQrCodeText = false; | |
56 | 57 | |
57 | 58 | private viewInited: boolean; |
58 | 59 | private scheduleUpdateCanvas: boolean; |
... | ... | @@ -109,8 +110,14 @@ export class QrCodeWidgetComponent extends PageComponent implements OnInit, Afte |
109 | 110 | private updateQrCodeText(newQrCodeText: string): void { |
110 | 111 | if (this.qrCodeText !== newQrCodeText) { |
111 | 112 | this.qrCodeText = newQrCodeText; |
112 | - if (this.qrCodeText) { | |
113 | - this.updateCanvas(); | |
113 | + if (isString(newQrCodeText)) { | |
114 | + this.invalidQrCodeText = false; | |
115 | + if (this.qrCodeText) { | |
116 | + this.updateCanvas(); | |
117 | + } | |
118 | + this.cd.detectChanges(); | |
119 | + } else { | |
120 | + this.invalidQrCodeText = true; | |
114 | 121 | } |
115 | 122 | this.cd.detectChanges(); |
116 | 123 | } |
... | ... | @@ -118,9 +125,11 @@ export class QrCodeWidgetComponent extends PageComponent implements OnInit, Afte |
118 | 125 | |
119 | 126 | private updateCanvas() { |
120 | 127 | if (this.viewInited) { |
121 | - QRCode.toCanvas(this.canvasRef.nativeElement, this.qrCodeText); | |
122 | - this.canvasRef.nativeElement.style.width = 'auto'; | |
123 | - this.canvasRef.nativeElement.style.height = 'auto'; | |
128 | + import('qrcode').then((QRCode) => { | |
129 | + QRCode.toCanvas(this.canvasRef.nativeElement, this.qrCodeText); | |
130 | + this.canvasRef.nativeElement.style.width = 'auto'; | |
131 | + this.canvasRef.nativeElement.style.height = 'auto'; | |
132 | + }); | |
124 | 133 | } else { |
125 | 134 | this.scheduleUpdateCanvas = true; |
126 | 135 | } | ... | ... |
... | ... | @@ -3123,7 +3123,8 @@ |
3123 | 3123 | "update-attribute": "Update attribute", |
3124 | 3124 | "update-timeseries": "Update timeseries", |
3125 | 3125 | "value": "Value" |
3126 | - } | |
3126 | + }, | |
3127 | + "invalid-qr-code-text": "Invalid input text for QR code. Input should have a string type" | |
3127 | 3128 | }, |
3128 | 3129 | "icon": { |
3129 | 3130 | "icon": "Icon", | ... | ... |