forked from dapriett/angular2-qrcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular2-qrcode.ts
81 lines (73 loc) · 2.14 KB
/
angular2-qrcode.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import {
NgModule,
Component,
Input,
ElementRef,
OnChanges,
SimpleChanges
} from '@angular/core';
import * as QRious from 'qrious';
@Component({
moduleId: 'module.id',
selector: 'qr-code',
template: ``
})
export class QRCodeComponent implements OnChanges {
ngOnChanges(changes: SimpleChanges): void {
if ('background' in changes ||
'backgroundAlpha' in changes ||
'foreground' in changes ||
'foregroundAlpha' in changes ||
'level' in changes ||
'mime' in changes ||
'padding' in changes ||
'size' in changes ||
'value' in changes ||
'canvas' in changes) {
this.generate();
}
}
@Input() background: string = 'white';
@Input() backgroundAlpha: number = 1.0;
@Input() foreground: string = 'black';
@Input() foregroundAlpha: number = 1.0;
@Input() level: string = 'L';
@Input() mime: string = 'image/png';
@Input() padding: number = null;
@Input() size: number = 100;
@Input() value: string = '';
@Input() canvas: boolean = false;
constructor(private elementRef: ElementRef) {
}
generate() {
try {
let el: HTMLElement = this.elementRef.nativeElement;
el.innerHTML = '';
let qr = new QRious({
background: this.background,
backgroundAlpha: this.backgroundAlpha,
foreground: this.foreground,
foregroundAlpha: this.foregroundAlpha,
level: this.level,
mime: this.mime,
padding: this.padding,
size: this.size,
value: this.value
});
if (this.canvas) {
el.appendChild(qr.canvas);
} else {
el.appendChild(qr.image);
}
} catch (e) {
console.error(`Could not generate QR Code: ${e.message}`);
}
}
}
@NgModule({
exports: [QRCodeComponent],
declarations: [QRCodeComponent],
entryComponents: [QRCodeComponent]
})
export class QRCodeModule {
}