@progress/kendo-angular-barcodes
Version:
Kendo UI Angular Barcodes
691 lines (677 loc) • 24.7 kB
JavaScript
/**-----------------------------------------------------------------------------------------
* Copyright © 2025 Progress Software Corporation. All rights reserved.
* Licensed under commercial license. See LICENSE.md in the project root for more information
*-------------------------------------------------------------------------------------------*/
import * as i0 from '@angular/core';
import { isDevMode, Directive, Input, ViewChild, Component, ChangeDetectionStrategy, NgModule } from '@angular/core';
import { Barcode, QRCode, barcodeValidator, qrcodeValidator } from '@progress/kendo-charts';
import { isDocumentAvailable, ResizeSensorComponent, ResizeBatchService } from '@progress/kendo-angular-common';
import { exportImage, exportSVG } from '@progress/kendo-drawing';
import { validatePackage } from '@progress/kendo-licensing';
/**
* @hidden
*/
const packageMetadata = {
name: '@progress/kendo-angular-barcodes',
productName: 'Kendo UI for Angular',
productCode: 'KENDOUIANGULAR',
productCodes: ['KENDOUIANGULAR'],
publishDate: 1756993057,
version: '20.0.3',
licensingDocsUrl: 'https://www.telerik.com/kendo-angular-ui/my-license/'
};
/**
* @hidden
*/
class BaseComponent {
element;
renderer;
ngZone;
resizeRateLimit = 10;
surfaceElement;
instance;
get autoResize() {
return this.resizeRateLimit > 0;
}
get canRender() {
return isDocumentAvailable() && Boolean(this.element);
}
constructor(element, renderer, ngZone) {
this.element = element;
this.renderer = renderer;
this.ngZone = ngZone;
validatePackage(packageMetadata);
}
ngAfterViewInit() {
this.refresh();
}
ngOnChanges(changes) {
// Need to create a new instance if the rendering mode changes.
if (changes['renderAs'] && this.instance) {
this.instance.destroy();
this.instance = null;
}
this.refresh();
}
/**
* Detects the size of the container and redraws the component.
* Resizing is automatic unless you set the `resizeRateLimit` option to `0`.
*/
resize() {
if (this.instance) {
this.instance.redraw();
}
}
/**
* @hidden
*/
onResize() {
if (this.autoResize) {
this.resize();
}
}
/**
* Exports the component as an image. The export operation is asynchronous and returns a promise.
*
* @param {ImageExportOptions} options - The parameters for the exported image.
* @returns {Promise<string>} - A promise that will be resolved with a PNG image encoded as a Data URI.
*/
exportImage(options = {}) {
return exportImage(this.exportVisual(), options);
}
/**
* Exports the component as an SVG document. The export operation is asynchronous and returns a promise.
*
* @param options - The parameters for the exported file.
* @returns - A promise that will be resolved with an SVG document that is encoded as a Data URI.
*/
exportSVG(options = {}) {
return exportSVG(this.exportVisual(), options);
}
/**
* Exports the component as a Drawing Group.
*
* @returns - The exported Group.
*/
exportVisual() {
return this.instance.exportVisual();
}
refresh() {
if (!this.canRender) {
return;
}
if (!this.instance) {
const element = this.element.nativeElement;
this.instance = this.createInstance(element, this.options);
}
else {
this.instance.setOptions(this.options);
}
}
isDevMode() {
return isDevMode();
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: BaseComponent, deps: [{ token: i0.ElementRef }, { token: i0.Renderer2 }, { token: i0.NgZone }], target: i0.ɵɵFactoryTarget.Directive });
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "16.2.12", type: BaseComponent, inputs: { resizeRateLimit: "resizeRateLimit" }, viewQueries: [{ propertyName: "surfaceElement", first: true, predicate: ["surface"], descendants: true, static: true }], usesOnChanges: true, ngImport: i0 });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: BaseComponent, decorators: [{
type: Directive
}], ctorParameters: function () { return [{ type: i0.ElementRef }, { type: i0.Renderer2 }, { type: i0.NgZone }]; }, propDecorators: { resizeRateLimit: [{
type: Input
}], surfaceElement: [{
type: ViewChild,
args: ['surface', { static: true }]
}] } });
/**
* Represents the [Kendo UI Barcode component for Angular](slug:overview_barcode_barcodes).
*
* Use this component to display a barcode in your Angular application.
*
* @example
* ```typescript
* import { Component } from '@angular/core';
*
* @Component({
* selector: 'my-app',
* template: `
* <kendo-barcode type="EAN8" value="1234567">
* </kendo-barcode>
* `
* })
* export class AppComponent {
* }
* ```
*/
class BarcodeComponent extends BaseComponent {
element;
renderer;
ngZone;
/**
* Sets the background color of the Barcode. Accepts any valid CSS color string, such as hex or rgb.
*
* @default "white"
*/
background;
/**
* Configures the border of the Barcode.
*/
border;
/**
* Shows the checksum digit next to the value in the text area when set to `true`.
*
* @default false
*/
checksum;
/**
* Sets the color of the Barcode. Accepts any valid CSS color string, such as hex or rgb.
*
* @default "black"
*/
color;
/**
* Sets the height of the Barcode in pixels.
*
* You can also set the Barcode dimensions using CSS.
*/
height;
/**
* Sets the padding of the Barcode. Use a number to set all paddings, or a `Padding` object for the individual sides.
*
* @default 0
*/
padding;
/**
* Sets the rendering mode of the Barcode.
*
* Use `"canvas"` to render as a Canvas element or `"svg"` to render as an inline SVG.
*
* @default "svg"
*/
renderAs;
/**
* Configures the Barcode text label.
*/
text;
/**
* Sets the symbology (encoding) for the Barcode.
*
* @default "Code39"
*/
type;
/**
* Sets the value of the Barcode.
*/
value;
/**
* Sets the width of the Barcode in pixels.
*
* You can also set the Barcode dimensions using CSS.
*/
width;
/**
* Limits how often the Barcode resizes automatically. Sets the maximum number of redraws per second when the container size changes.
* Set to `0` to disable automatic resizing.
*
* @default 10
*/
resizeRateLimit = 10;
get options() {
return {
renderAs: this.renderAs,
background: this.background,
border: this.border,
checksum: this.checksum,
color: this.color,
height: this.height,
padding: this.padding,
text: this.text,
type: this.type,
value: this.value,
width: this.width
};
}
constructor(element, renderer, ngZone) {
super(element, renderer, ngZone);
this.element = element;
this.renderer = renderer;
this.ngZone = ngZone;
}
createInstance(element, options) {
return new Barcode(element, options, this.onError.bind(this));
}
onError(error) {
error.name = packageMetadata.productName + ' Barcode';
if (this.isDevMode()) {
throw error;
}
else {
console.warn(error);
}
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: BarcodeComponent, deps: [{ token: i0.ElementRef }, { token: i0.Renderer2 }, { token: i0.NgZone }], target: i0.ɵɵFactoryTarget.Component });
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "16.2.12", type: BarcodeComponent, isStandalone: true, selector: "kendo-barcode", inputs: { background: "background", border: "border", checksum: "checksum", color: "color", height: "height", padding: "padding", renderAs: "renderAs", text: "text", type: "type", value: "value", width: "width", resizeRateLimit: "resizeRateLimit" }, exportAs: ["kendoBarcode"], usesInheritance: true, ngImport: i0, template: `
<kendo-resize-sensor (resize)="onResize()" [rateLimit]="resizeRateLimit"></kendo-resize-sensor>
`, isInline: true, dependencies: [{ kind: "component", type: ResizeSensorComponent, selector: "kendo-resize-sensor", inputs: ["rateLimit"], outputs: ["resize"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: BarcodeComponent, decorators: [{
type: Component,
args: [{
changeDetection: ChangeDetectionStrategy.OnPush,
exportAs: 'kendoBarcode',
selector: 'kendo-barcode',
template: `
<kendo-resize-sensor (resize)="onResize()" [rateLimit]="resizeRateLimit"></kendo-resize-sensor>
`,
standalone: true,
imports: [ResizeSensorComponent]
}]
}], ctorParameters: function () { return [{ type: i0.ElementRef }, { type: i0.Renderer2 }, { type: i0.NgZone }]; }, propDecorators: { background: [{
type: Input
}], border: [{
type: Input
}], checksum: [{
type: Input
}], color: [{
type: Input
}], height: [{
type: Input
}], padding: [{
type: Input
}], renderAs: [{
type: Input
}], text: [{
type: Input
}], type: [{
type: Input
}], value: [{
type: Input
}], width: [{
type: Input
}], resizeRateLimit: [{
type: Input
}] } });
const DEFAULT_COLOR = '#000';
const DEFAULT_BACKGROUND = '#fff';
const DEFAULT_ERROR_CORRECTION = 'L';
/**
* Represents the [Kendo UI QR Code component for Angular](slug:overview_qrcode_barcodes).
*
* Shows a QR Code for the provided value.
*
* @example
* ```ts
* import { Component } from '@angular/core';
*
* @Component({
* selector: 'my-app',
* template: `
* <kendo-qrcode value="https://www.telerik.com/kendo-angular-ui">
* </kendo-qrcode>
* `
* })
* export class AppComponent {
* }
* ```
*/
class QRCodeComponent extends BaseComponent {
element;
renderer;
ngZone;
/**
* Sets the background color of the QR Code. Accepts any valid CSS color string, such as hex or rgb.
*
* @default "white"
*/
background;
/**
* Sets the border of the QR Code.
*/
border;
/**
* Sets the color of the QR Code. Accepts any valid CSS color string, such as hex or rgb.
*
* @default "black"
*/
color;
/**
* Sets the encoding mode for the value.
*
* > **Important** The UTF-8 encoding is not part of the specifications and some readers may not support it.
*
* @default "ISO_8859_1"
*/
encoding;
/**
* Sets the error correction level.
*
* @default "L"
*/
errorCorrection;
/**
* Sets an optional image overlay to display over the QR Code.
*
* > **Note** Always test if the code scans correctly with the overlay. If needed, increase the `errorCorrection` level to `"M"` or `"H"`.
*/
overlay;
/**
* Sets the padding for the QR Code in pixels.
*
* @default 0
*/
padding;
/**
* Sets the rendering mode for the QR Code.
*
* @default "svg"
* ```
*/
renderAs;
/**
* Sets the size of the QR Code. Numeric values are in pixels.
*
* If no size is specified, the size will be determined from the element width and height.
* If the element has width or height of zero, a default value of 200 pixels will be used.
*
* @default "200px"
*/
size;
/**
* Sets the value to encode in the QR Code.
*/
value;
/**
* Limits how often the QR Code resizes automatically. Sets the maximum redraws per second when the container size changes.
* Set to `0` to disable automatic resizing.
*
* @default 10
*/
resizeRateLimit = 10;
get options() {
return {
background: this.background || DEFAULT_BACKGROUND,
border: this.border,
color: this.color || DEFAULT_COLOR,
encoding: this.encoding,
errorCorrection: this.errorCorrection || DEFAULT_ERROR_CORRECTION,
overlay: this.overlay || {},
padding: this.padding,
renderAs: this.renderAs,
size: this.size,
value: this.value
};
}
constructor(element, renderer, ngZone) {
super(element, renderer, ngZone);
this.element = element;
this.renderer = renderer;
this.ngZone = ngZone;
}
createInstance(element, options) {
return new QRCode(element, options, this.onError.bind(this));
}
onError(error) {
error.name = packageMetadata.productName + ' QRCode';
if (this.isDevMode()) {
throw error;
}
else {
console.warn(error);
}
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: QRCodeComponent, deps: [{ token: i0.ElementRef }, { token: i0.Renderer2 }, { token: i0.NgZone }], target: i0.ɵɵFactoryTarget.Component });
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "16.2.12", type: QRCodeComponent, isStandalone: true, selector: "kendo-qrcode", inputs: { background: "background", border: "border", color: "color", encoding: "encoding", errorCorrection: "errorCorrection", overlay: "overlay", padding: "padding", renderAs: "renderAs", size: "size", value: "value", resizeRateLimit: "resizeRateLimit" }, exportAs: ["kendoQRCode"], usesInheritance: true, ngImport: i0, template: `
<kendo-resize-sensor (resize)="onResize()" [rateLimit]="resizeRateLimit"></kendo-resize-sensor>
`, isInline: true, dependencies: [{ kind: "component", type: ResizeSensorComponent, selector: "kendo-resize-sensor", inputs: ["rateLimit"], outputs: ["resize"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: QRCodeComponent, decorators: [{
type: Component,
args: [{
changeDetection: ChangeDetectionStrategy.OnPush,
selector: 'kendo-qrcode',
exportAs: 'kendoQRCode',
template: `
<kendo-resize-sensor (resize)="onResize()" [rateLimit]="resizeRateLimit"></kendo-resize-sensor>
`,
standalone: true,
imports: [ResizeSensorComponent]
}]
}], ctorParameters: function () { return [{ type: i0.ElementRef }, { type: i0.Renderer2 }, { type: i0.NgZone }]; }, propDecorators: { background: [{
type: Input
}], border: [{
type: Input
}], color: [{
type: Input
}], encoding: [{
type: Input
}], errorCorrection: [{
type: Input
}], overlay: [{
type: Input
}], padding: [{
type: Input
}], renderAs: [{
type: Input
}], size: [{
type: Input
}], value: [{
type: Input
}], resizeRateLimit: [{
type: Input
}] } });
/**
* Use this utility array to access all Barcode related components and directives in a standalone Angular component.
*/
const KENDO_BARCODE = [
BarcodeComponent
];
/**
* Use this utility array to access all QRCode related components and directives in a standalone Angular component.
*/
const KENDO_QRCODE = [
QRCodeComponent
];
/**
* Use this utility array to access all `@progress/kendo-angular-barcodes` related components and directives in a standalone Angular component.
*/
const KENDO_BARCODES = [
...KENDO_BARCODE,
...KENDO_QRCODE
];
//IMPORTANT: NgModule export kept for backwards compatibility
/**
* Represents the [NgModule](link:site.data.urls.angular['ngmoduleapi']) definition for the Barcode component.
*
* Use this module to add Barcode support to your application.
*
* @example
*
* ```ts
* // Import the BarcodeModule.
* import { BarcodeModule } from '@progress/kendo-angular-barcodes';
*
* import { NgModule } from '@angular/core';
* import { BrowserModule } from '@angular/platform-browser';
* import { AppComponent } from './app.component';
*
* @NgModule({
* declarations: [AppComponent],
* imports: [BrowserModule, BarcodeModule],
* bootstrap: [AppComponent]
* })
* export class AppModule {}
* ```
*/
class BarcodeModule {
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: BarcodeModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "16.2.12", ngImport: i0, type: BarcodeModule, imports: [BarcodeComponent], exports: [BarcodeComponent] });
static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: BarcodeModule, providers: [ResizeBatchService], imports: [KENDO_BARCODE] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: BarcodeModule, decorators: [{
type: NgModule,
args: [{
imports: [...KENDO_BARCODE],
exports: [...KENDO_BARCODE],
providers: [ResizeBatchService]
}]
}] });
//IMPORTANT: NgModule export kept for backwards compatibility
/**
* Represents the [NgModule](link:site.data.urls.angular['ngmoduleapi']) definition for the QR Code component.
*
* Use this module to add QR Code support to your application.
*
* @example
* ```ts
* // Import the QRCodeModule
* import { QRCodeModule } from '@progress/kendo-angular-barcodes';
*
* import { NgModule } from '@angular/core';
* import { BrowserModule } from '@angular/platform-browser';
* import { AppComponent } from './app.component';
*
* @NgModule({
* declarations: [AppComponent],
* imports: [BrowserModule, QRCodeModule],
* bootstrap: [AppComponent]
* })
* export class AppModule {}
* ```
*/
class QRCodeModule {
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: QRCodeModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "16.2.12", ngImport: i0, type: QRCodeModule, imports: [QRCodeComponent], exports: [QRCodeComponent] });
static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: QRCodeModule, providers: [ResizeBatchService], imports: [KENDO_QRCODE] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: QRCodeModule, decorators: [{
type: NgModule,
args: [{
imports: [...KENDO_QRCODE],
exports: [...KENDO_QRCODE],
providers: [ResizeBatchService]
}]
}] });
//IMPORTANT: NgModule export kept for backwards compatibility
/**
* Represents the [NgModule](link:site.data.urls.angular['ngmoduleapi']) definition for the Barcode and QR Code components.
*
* Use this module to add Barcode and QR Code features to your application.
*
* @example
*
* ```ts
* // Import the Barcodes module.
* import { BarcodesModule } from '@progress/kendo-angular-barcodes';
*
* import { NgModule } from '@angular/core';
* import { BrowserModule } from '@angular/platform-browser';
* import { AppComponent } from './app.component';
*
* @NgModule({
* declarations: [AppComponent],
* imports: [BrowserModule, BarcodesModule],
* bootstrap: [AppComponent]
* })
* export class AppModule {}
* ```
*/
class BarcodesModule {
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: BarcodesModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "16.2.12", ngImport: i0, type: BarcodesModule, imports: [BarcodeComponent, QRCodeComponent], exports: [BarcodeComponent, QRCodeComponent] });
static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: BarcodesModule, providers: [ResizeBatchService], imports: [KENDO_BARCODES] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: BarcodesModule, decorators: [{
type: NgModule,
args: [{
imports: [...KENDO_BARCODES],
exports: [...KENDO_BARCODES],
providers: [ResizeBatchService]
}]
}] });
/**
* @hidden
*/
const isPresent = (value) => value !== null && value !== undefined;
/**
* Creates a validator for a specific Barcode type.
*
* Use this function to validate a Barcode value for a given `BarcodeType`.
*
* @param type Specifies the type of the Barcode.
* @param size Specifies the size of the barcode, excluding the text label, padding, and border. This parameter is optional.
* @returns Returns a validator function. The function returns an error map with the `barcode` property if validation fails. Otherwise, it returns `null` if valid.
*
* @example
* ```typescript
* const control = new FormControl('1234', createBarcodeValidator('EAN8'));
* console.log(control.errors);
* // {
* // barcode: {
* // message: 'The value of the "EAN13" encoding should be 12 symbols',
* // value: '1234',
* // type: 'EAN13'
* // }
* // }
* ```
*/
const createBarcodeValidator = (type, size) => {
const validator = barcodeValidator(type, size);
return (control) => {
if (!isPresent(control.value) || control.value === '') {
return null;
}
const result = validator(control.value);
if (result.valid === true) {
return null;
}
return {
barcode: {
message: result.error.message,
value: control.value,
type: type
}
};
};
};
/**
* Creates a value validator for a specific QR Code encoding.
*
* @param {QRCodeEncoding} encoding Sets the QR Code encoding. Defaults to `ISO_8859_1`.
* @returns {ValidatorFn} Returns a validator function. The function returns an error map with the `qrcode` property if the value is invalid. Otherwise, it returns `null`.
*
* @example
* ```ts-no-run
* const control = new FormControl('Фоо', createQRCodeValidator());
* console.log(control.errors);
*
* // {
* // qrcode: {
* // message: 'Unsupported character in QR Code: "Ф".',
* // value: '1234',
* // type: 'EAN13'
* // }
* // }
* ```
*/
const createQRCodeValidator = (encoding = 'ISO_8859_1') => {
const validator = qrcodeValidator(encoding);
return (control) => {
if (!control.value) {
return null;
}
const result = validator(control.value);
if (result.valid === true) {
return null;
}
return {
qrcode: {
message: result.error.message,
value: control.value,
encoding: encoding
}
};
};
};
/**
* Generated bundle index. Do not edit.
*/
export { BarcodeComponent, BarcodeModule, BarcodesModule, KENDO_BARCODE, KENDO_BARCODES, KENDO_QRCODE, QRCodeComponent, QRCodeModule, createBarcodeValidator, createQRCodeValidator };