ngx-qrcode2
Version:
An Angular component for generating QRCodes
196 lines (188 loc) • 6.61 kB
JavaScript
import { __values, __decorate } from 'tslib';
import { Renderer2, Input, ViewChild, Component, ChangeDetectionStrategy, NgModule } from '@angular/core';
import QRCode from 'qrcode';
var NgxQrcodeElementTypes;
(function (NgxQrcodeElementTypes) {
NgxQrcodeElementTypes["URL"] = "url";
NgxQrcodeElementTypes["IMG"] = "img";
NgxQrcodeElementTypes["CANVAS"] = "canvas";
})(NgxQrcodeElementTypes || (NgxQrcodeElementTypes = {}));
var NgxQrcodeErrorCorrectionLevels;
(function (NgxQrcodeErrorCorrectionLevels) {
NgxQrcodeErrorCorrectionLevels["LOW"] = "L";
NgxQrcodeErrorCorrectionLevels["MEDIUM"] = "M";
NgxQrcodeErrorCorrectionLevels["QUARTILE"] = "Q";
NgxQrcodeErrorCorrectionLevels["HIGH"] = "H";
})(NgxQrcodeErrorCorrectionLevels || (NgxQrcodeErrorCorrectionLevels = {}));
var DEFAULT_VALUES = {
elementType: NgxQrcodeElementTypes.URL,
cssClass: 'qrcode',
value: 'https://www.techiediaries.com',
version: '',
errorCorrectionLevel: NgxQrcodeErrorCorrectionLevels.MEDIUM,
margin: 4,
scale: 4,
width: 10,
colorDark: '#000',
colorLight: '#FFF'
};
var QrcodeComponent = /** @class */ (function () {
function QrcodeComponent(renderer) {
this.renderer = renderer;
this.elementType = DEFAULT_VALUES.elementType;
this.cssClass = DEFAULT_VALUES.cssClass;
this.value = DEFAULT_VALUES.value;
this.version = DEFAULT_VALUES.version;
this.errorCorrectionLevel = DEFAULT_VALUES.errorCorrectionLevel;
this.margin = DEFAULT_VALUES.margin;
this.scale = DEFAULT_VALUES.scale;
this.width = DEFAULT_VALUES.width;
this.colorDark = DEFAULT_VALUES.colorDark;
this.colorLight = DEFAULT_VALUES.colorLight;
}
QrcodeComponent.prototype.ngOnChanges = function () {
this.createQRCode();
};
QrcodeComponent.prototype.createQRCode = function () {
var _this = this;
if (!this.value) {
return;
}
var element;
switch (this.elementType) {
case NgxQrcodeElementTypes.CANVAS:
element = this.renderer.createElement('canvas');
this.toCanvas(element).then(function () {
_this.renderElement(element);
}).catch(function (e) {
_this.removeElementChildren();
console.error(e);
});
break;
default:
element = this.renderer.createElement('img');
this.toDataURL().then(function (src) {
element.setAttribute('src', src);
if (_this.alt) {
element.setAttribute('alt', _this.alt);
}
_this.renderElement(element);
}).catch(function (e) {
_this.removeElementChildren();
console.error(e);
});
}
};
QrcodeComponent.prototype.toDataURL = function () {
return QRCode.toDataURL(this.value, {
version: this.version,
errorCorrectionLevel: this.errorCorrectionLevel,
margin: this.margin,
scale: this.scale,
width: this.width,
color: {
dark: this.colorDark,
light: this.colorLight
}
});
};
QrcodeComponent.prototype.toCanvas = function (canvas) {
return QRCode.toCanvas(canvas, this.value, {
version: this.version,
errorCorrectionLevel: this.errorCorrectionLevel,
margin: this.margin,
scale: this.scale,
width: this.width,
color: {
dark: this.colorDark,
light: this.colorLight
}
});
};
QrcodeComponent.prototype.renderElement = function (element) {
this.removeElementChildren();
this.renderer.appendChild(this.qrcElement.nativeElement, element);
};
QrcodeComponent.prototype.removeElementChildren = function () {
var e_1, _a;
try {
for (var _b = __values(this.qrcElement.nativeElement.childNodes), _c = _b.next(); !_c.done; _c = _b.next()) {
var node = _c.value;
this.renderer.removeChild(this.qrcElement.nativeElement, node);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
};
QrcodeComponent.ctorParameters = function () { return [
{ type: Renderer2 }
]; };
__decorate([
Input()
], QrcodeComponent.prototype, "elementType", void 0);
__decorate([
Input()
], QrcodeComponent.prototype, "cssClass", void 0);
__decorate([
Input()
], QrcodeComponent.prototype, "alt", void 0);
__decorate([
Input()
], QrcodeComponent.prototype, "value", void 0);
__decorate([
Input()
], QrcodeComponent.prototype, "version", void 0);
__decorate([
Input()
], QrcodeComponent.prototype, "errorCorrectionLevel", void 0);
__decorate([
Input()
], QrcodeComponent.prototype, "margin", void 0);
__decorate([
Input()
], QrcodeComponent.prototype, "scale", void 0);
__decorate([
Input()
], QrcodeComponent.prototype, "width", void 0);
__decorate([
Input()
], QrcodeComponent.prototype, "colorDark", void 0);
__decorate([
Input()
], QrcodeComponent.prototype, "colorLight", void 0);
__decorate([
ViewChild('qrcElement')
], QrcodeComponent.prototype, "qrcElement", void 0);
QrcodeComponent = __decorate([
Component({
selector: 'ngx-qrcode',
template: "\n <div #qrcElement [class]=\"cssClass\"></div>",
changeDetection: ChangeDetectionStrategy.OnPush
})
], QrcodeComponent);
return QrcodeComponent;
}());
var NgxQRCodeModule = /** @class */ (function () {
function NgxQRCodeModule() {
}
NgxQRCodeModule = __decorate([
NgModule({
declarations: [QrcodeComponent],
exports: [QrcodeComponent]
})
], NgxQRCodeModule);
return NgxQRCodeModule;
}());
/*
* Public API Surface of ngx-qrcode
*/
/**
* Generated bundle index. Do not edit.
*/
export { NgxQRCodeModule, NgxQrcodeElementTypes, NgxQrcodeErrorCorrectionLevels, QrcodeComponent };
//# sourceMappingURL=ngx-qrcode2.js.map