-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathqrcode.ts
374 lines (323 loc) · 12 KB
/
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import { removeChildElements, refreshCanvasBarcode, exportAsImage } from '../barcode/utility/barcode-util';
import { Complex, Property, Component, INotifyPropertyChanged, L10n, Event, EmitType } from '@syncfusion/ej2-base';
import { ErrorCorrectionLevel, QRCodeVersion, RenderingMode, BarcodeEvent, BarcodeExportType } from '../barcode/enum/enum';
import { DisplayTextModel } from '../barcode/primitives/displaytext-model';
import { MarginModel } from '../barcode/primitives/margin-model';
import { DisplayText } from '../barcode/primitives/displaytext';
import { Margin } from '../barcode/primitives/margin';
import { QRCodeGeneratorModel } from './qrcode-model';
import { BarcodeRenderer } from '../barcode/rendering/renderer';
import { QRCode } from './qr-code-util';
import { ValidateEvent } from '../barcode';
import { QRCodeLogo } from '../barcode/primitives/icon';
import { QRCodeLogoModel } from '../barcode/primitives/icon-model';
/**
* Represents the Qrcode control
* ```
*/
export class QRCodeGenerator extends Component<HTMLElement> implements INotifyPropertyChanged {
/**
* Constructor for creating the widget
*
* @param {QRCodeGeneratorModel} options - Provide the instance.
* @param {HTMLElement} element - Provide the element .
*/
constructor(options?: QRCodeGeneratorModel, element?: HTMLElement | string) {
super(options, <HTMLElement | string>element);
}
/**
* Defines the height of the QR code model.
*
* @default '100%'
*/
@Property('100%')
public height: string | number;
/**
* Specifies the logo overlay configuration for the QR code.
*
* @default ''
*/
@Complex<QRCodeLogoModel>({}, QRCodeLogo)
public logo: QRCodeLogoModel;
/**
* Defines the width of the QR code model.
*
* @default '100%'
*/
@Property('100%')
public width: string | number;
/**
* Defines the QR code rendering mode.
*
* * SVG - Renders the bar-code objects as SVG elements
* * Canvas - Renders the bar-code in a canvas
*
* @default 'SVG'
*/
@Property('SVG')
public mode: RenderingMode;
/**
* Defines the xDimension of the QR code model.
*
*/
@Property(1)
public xDimension: number;
/**
* Defines the error correction level of the QR code.
*
* @aspDefaultValueIgnore
* @aspNumberEnum
* @default undefined
*/
@Property()
public errorCorrectionLevel: ErrorCorrectionLevel;
/**
* Defines the margin properties for the QR code.
*
* @default ''
*/
@Complex<MarginModel>({}, Margin)
public margin: MarginModel;
/**
* Defines the background color of the QR code.
*
* @default 'white'
*/
@Property('white')
public backgroundColor: string;
/**
* Triggers if you enter any invalid character.
*
* @event
*/
@Event()
public invalid: EmitType<Object>;
/**
* Defines the forecolor of the QR code.
*
* @default 'black'
*/
@Property('black')
public foreColor: string;
/**
* Defines the text properties for the QR code.
*
* @default ''
*/
@Complex<DisplayTextModel>({}, DisplayText)
public displayText: DisplayTextModel;
/**
* * Defines the version of the QR code.
*
* @aspDefaultValueIgnore
* @aspNumberEnum
* @default undefined
*/
@Property()
public version: QRCodeVersion;
private widthChange: boolean = false;
private heightChange: boolean = false;
private isSvgMode: boolean;
private barcodeRenderer: BarcodeRenderer;
/**
* Defines the type of barcode to be rendered.
*
* @default undefined
*/
@Property(undefined)
public value: string;
/** @private */
public localeObj: L10n;
/** @private */
// eslint-disable-next-line
private defaultLocale: Object;
private barcodeCanvas: HTMLElement;
/**
* Renders the barcode control .
*
* @returns {void}
*/
public render(): void {
this.notify('initial-load', {});
/**
* Used to load context menu
*/
this.trigger('load');
this.notify('initial-end', {});
this.renderElements();
this.renderComplete();
}
private triggerEvent(eventName: BarcodeEvent, message: string): void {
const arg: ValidateEvent = {
message: message
};
this.trigger(BarcodeEvent[`${eventName}`], arg);
}
private renderElements(): void {
const barCode: QRCode = new QRCode();
this.value = this.value || '';
barCode.text = this.value;
barCode.XDimension = this.xDimension;
barCode.mIsUserMentionedErrorCorrectionLevel = (this.errorCorrectionLevel !== undefined) ? true : false;
barCode.mErrorCorrectionLevel = (this.errorCorrectionLevel !== undefined) ? this.errorCorrectionLevel : ErrorCorrectionLevel.Medium;
barCode.version = (this.version !== undefined) ? this.version : undefined;
barCode.mIsUserMentionedVersion = (this.version !== undefined) ? true : false;
const mode: boolean = (this.mode === 'SVG') ? true : false;
const validInput: boolean = barCode.draw(
this.value, this.barcodeCanvas, this.element.offsetHeight as number,
this.element.offsetWidth as number, this.margin, this.displayText, mode, this.foreColor, this.logo);
if (this.mode === 'Canvas') {
(this.barcodeCanvas as HTMLCanvasElement).getContext('2d').setTransform(1, 0, 0, 1, 0, 0);
(this.barcodeCanvas as HTMLCanvasElement).getContext('2d').scale(1.5, 1.5);
}
if (!validInput) {
const encoding: string = 'Invalid Input';
this.triggerEvent(BarcodeEvent.invalid, encoding as string);
}
if (this.mode === 'Canvas') {
this.barcodeCanvas.style.transform = 'scale(' + (2 / 3) + ')';
this.barcodeCanvas.style.transformOrigin = '0 0';
}
}
private setCulture(): void {
this.localeObj = new L10n(this.getModuleName(), this.defaultLocale, this.locale);
}
// eslint-disable-next-line
private getElementSize(real: string | number, rulerSize?: number): string {
//this method will return the size of the qrcode
let value: string;
if (real.toString().indexOf('px') > 0 || real.toString().indexOf('%') > 0) {
value = real.toString();
} else {
value = real.toString() + 'px';
}
return value;
}
private initialize(): void {
//Task 913517: Handle null properties for diagram and barcode properties -phase1
if (!this.width) {
this.width = '100px';
}
if (!this.height || this.height === '100%') {
this.height = '100px';
}
//Task 913519: Barcode visibility issue and Dependabot issues
this.element.style.display = 'block';
//Initialize the height of qrcode generator
this.element.style.height = this.getElementSize(this.height);
//Initialize the width of qrcode generator
this.element.style.width = this.getElementSize(this.width);
this.barcodeCanvas = this.barcodeRenderer.renderRootElement(
{
id: this.element.id + 'content',
height: this.mode === 'SVG' ? this.element.offsetHeight : this.element.offsetHeight * 1.5,
width: this.mode === 'SVG' ? this.element.offsetWidth : this.element.offsetWidth * 1.5
},
this.backgroundColor, this.element.offsetWidth, this.element.offsetHeight) as HTMLElement;
this.element.appendChild(this.barcodeCanvas);
}
protected preRender(): void {
this.element.classList.add('e-qrcode');
this.barcodeRenderer = new BarcodeRenderer(this.element.id, this.mode === 'SVG');
this.initialize();
this.initializePrivateVariables();
this.setCulture();
}
/**
* Get the properties to be maintained in the persisted state.
*
* @returns {string} Get the properties to be maintained in the persisted state.
*/
public getPersistData(): string {
const keyEntity: string[] = ['loaded'];
return this.addOnPersist(keyEntity);
}
/**
* Returns the module name of the barcode
*
* @returns {string} Returns the module name of the barcode
*/
public getModuleName(): string {
return 'QRCodeGenerator';
}
/**
* It is used to destroy the Barcode component.
*
* @function destroy
* @returns {void}
*/
public destroy(): void {
this.notify('destroy', {});
super.destroy();
const content: HTMLElement = document.getElementById(this.element.id + 'content');
if (content) {
this.element.removeChild(content);
}
}
private initializePrivateVariables(): void {
this.defaultLocale = {
};
}
/**
* Export the barcode as an image in the specified image type and downloads it in the browser.
*
* @returns {void} Export the barcode as an image in the specified image type and downloads it in the browser.
* @param {string} filename - Specifies the filename of the barcode image to be download.
* @param {BarcodeExportType} barcodeExportType - Defines the format of the barcode to be exported
*/
public exportImage(filename: string, barcodeExportType: BarcodeExportType ): void {
exportAsImage(barcodeExportType, filename, this.element, false, this);
}
/**
* Export the barcode as an image in the specified image type and returns it as base64 string.
*
* @returns {string} Export the barcode as an image in the specified image type and returns it as base64 string.
* @param {BarcodeExportType} barcodeExportType - Defines the format of the barcode to be exported
*/
public exportAsBase64Image(barcodeExportType: BarcodeExportType): Promise<string> {
const returnValue: Promise<string> = exportAsImage(barcodeExportType, '', this.element, true, this);
return returnValue;
}
// eslint-disable-next-line
public onPropertyChanged(newProp: QRCodeGeneratorModel, oldProp: QRCodeGeneratorModel): void {
let width: number | string;
let height: number | string;
if (this.mode === 'Canvas' && newProp.mode !== 'Canvas') {
refreshCanvasBarcode(this, this.barcodeCanvas as HTMLCanvasElement);
} else {
this.barcodeRenderer = removeChildElements(newProp, this.barcodeCanvas, this.mode, this.element.id);
}
if (newProp.width) {
if (this.mode === 'Canvas' && newProp.mode !== 'Canvas') {
this.widthChange = true;
}
width = (this.mode === 'Canvas' && newProp.mode !== 'Canvas') ? (((newProp.width as number) * 1.5)) : newProp.width;
this.barcodeCanvas.setAttribute('width', String(width));
}
if (newProp.height) {
if (this.mode === 'Canvas' && newProp.mode !== 'Canvas') {
this.heightChange = true;
}
height = (this.mode === 'Canvas' && newProp.mode !== 'Canvas') ? (((newProp.height as number) * 1.5)) : newProp.height;
this.barcodeCanvas.setAttribute('height', String(height));
}
for (const prop of Object.keys(newProp)) {
switch (prop) {
case 'width':
this.element.style.width = this.getElementSize(width);
this.barcodeCanvas.setAttribute('width', String(this.element.offsetWidth));
break;
case 'height':
this.element.style.height = this.getElementSize(height);
this.barcodeCanvas.setAttribute('height', String(this.element.offsetHeight));
break;
case 'backgroundColor':
this.barcodeCanvas.style.background = newProp.backgroundColor;
break;
case 'mode':
this.initialize();
}
}
this.renderElements();
}
}